forked from gesior/open-tibia-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itemImageGenerator.ts
executable file
·155 lines (136 loc) · 6.46 KB
/
itemImageGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {ImageGenerator} from "./modules/imageGenerator/imageGenerator";
import {DatThingCategory, GameFeature} from "./modules/constants/const";
import {WebsiteImageGeneratorBase} from "./websiteImageGeneratorBase";
import {OtbManager} from "./modules/otbFile/otbManager";
import {DatManager} from "./modules/datFile/datManager";
import {SpriteManager} from "./modules/sprFile/spriteManager";
let GIF = require('gif.js');
class ItemImageGenerator extends WebsiteImageGeneratorBase {
private onlyPickableCheckbox: HTMLInputElement;
private forceEnableExtendedSpritesCheckbox: HTMLInputElement;
private enableTransparencyCheckbox: HTMLInputElement;
private useDatItemIdsAsImageIdsCheckbox: HTMLInputElement;
private onlyPickable = true;
private useDatItemIdsAsImageIds = false;
init() {
super.init();
this.onlyPickableCheckbox = <HTMLInputElement>document.getElementById('onlyPickable');
this.forceEnableExtendedSpritesCheckbox = <HTMLInputElement>document.getElementById('forceEnableExtendedSprites');
this.enableTransparencyCheckbox = <HTMLInputElement>document.getElementById('enableTransparency');
this.useDatItemIdsAsImageIdsCheckbox = <HTMLInputElement>document.getElementById('useDatItemIdsAsImageIds');
}
afterSetClientVersion() {
if (this.forceEnableExtendedSpritesCheckbox.checked) {
this.client.enableFeature(GameFeature.GameSpritesU32);
}
if (this.enableTransparencyCheckbox.checked) {
this.client.enableFeature(GameFeature.GameSpritesAlphaChannel);
}
this.otbRequired = !this.useDatItemIdsAsImageIdsCheckbox.checked;
}
startImageGenerator(imageGenerator: ImageGenerator, otbManager: OtbManager, datManager: DatManager, spriteManager: SpriteManager, zip) {
this.onlyPickable = this.onlyPickableCheckbox.checked;
this.useDatItemIdsAsImageIds = this.useDatItemIdsAsImageIdsCheckbox.checked;
this.generateItemImage(imageGenerator, zip, 0);
}
generateItemImage(imageGenerator: ImageGenerator, zip, serverId: number) {
const self = this;
if (this.useDatItemIdsAsImageIds) {
this.progressValue(serverId, this.datManager.getCategory(DatThingCategory.ThingCategoryItem).length);
} else {
this.progressValue(serverId, this.otbManager.getLastId());
}
if ((this.useDatItemIdsAsImageIds && serverId > this.datManager.getCategory(DatThingCategory.ThingCategoryItem).length) ||
(!this.useDatItemIdsAsImageIds && serverId > this.otbManager.getLastId())) {
this.progressText('Packing images to ZIP file, please wait (it may take a while)');
zip.generateAsync({type: "blob"}).then(function (blob: Blob) {
console.log('zip size', blob.size);
self.progressText('ZIP generated, it should start download now.');
self.downloadBlob('items.zip', blob);
});
return;
}
let clientItemId = serverId;
if (!this.useDatItemIdsAsImageIds) {
if (!this.otbManager.isValidOtbId(serverId)) {
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
return;
}
clientItemId = this.otbManager.getItem(serverId).getClientId();
if (!clientItemId) {
console.log('otb ID not mapped to any dat ID', serverId);
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
return;
}
}
let itemThingType = this.datManager.getItem(clientItemId);
if (!itemThingType) {
console.log('dat ID not found in dat file', serverId, clientItemId);
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
return;
}
if (this.onlyPickable && !itemThingType.isPickupable()) {
console.log('skip not pickable', serverId);
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
return;
}
let itemSprite = null;
if (this.useDatItemIdsAsImageIds) {
itemSprite = imageGenerator.generateItemImageByClientId(serverId);
} else {
itemSprite = imageGenerator.generateItemImageByServerId(serverId);
}
if (!itemSprite) {
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
return;
}
const canvas = <HTMLCanvasElement>document.createElement('canvas');
canvas.width = itemSprite.getWidth();
canvas.height = itemSprite.getHeight();
document.getElementsByTagName('body')[0].appendChild(canvas);
const ctx = canvas.getContext("2d");
const palette = ctx.getImageData(0, 0, itemSprite.getWidth(), itemSprite.getHeight());
palette.data.set(new Uint8ClampedArray(itemSprite.getPixels().m_buffer.buffer));
ctx.putImageData(palette, 0, 0);
if (self.imageFormat == 'png') {
const callback = function (blob) {
canvas.remove();
zip.file('items/' + serverId + '.png', blob);
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
};
canvas.toBlob(callback);
} else {
const gif = new GIF({
workers: 1,
quality: 10,
width: itemSprite.getWidth(),
height: itemSprite.getHeight(),
workerScript: './js/gif.worker.js',
transparent: 'rgba(0,0,0,0)'
});
gif.addFrame(canvas, {copy: true});
canvas.remove();
gif.on('finished', function (blob) {
zip.file(serverId + '.gif', blob);
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
});
gif.render();
}
}
}
const itemImageGenerator = new ItemImageGenerator();
itemImageGenerator.init();