forked from gesior/open-tibia-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itemImageFramesGenerator.ts
134 lines (119 loc) · 5.93 KB
/
itemImageFramesGenerator.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
import {DatManager} from "./modules/datFile/datManager";
import {OtbManager} from "./modules/otbFile/otbManager";
import {SpriteManager} from "./modules/sprFile/spriteManager";
import {ImageGenerator} from "./modules/imageGenerator/imageGenerator";
import {DatThingCategory, GameFeature} from "./modules/constants/const";
import {WebsiteImageGeneratorBase} from "./websiteImageGeneratorBase";
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 itemSprites = null;
if (this.useDatItemIdsAsImageIds) {
itemSprites = imageGenerator.generateItemImagesByClientId(serverId);
} else {
itemSprites = imageGenerator.generateItemImagesByServerId(serverId);
}
if (!itemSprites || itemSprites.length == 0) {
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
return;
}
const firstItemSprite = itemSprites[0];
const canvas = <HTMLCanvasElement>document.createElement('canvas');
canvas.width = firstItemSprite.getWidth() * itemSprites.length;
canvas.height = firstItemSprite.getHeight();
document.getElementsByTagName('body')[0].appendChild(canvas);
const ctx = canvas.getContext("2d");
for (let animationFrame = 0; animationFrame < itemSprites.length; animationFrame++) {
const palette = ctx.getImageData(firstItemSprite.getWidth() * animationFrame, 0, firstItemSprite.getWidth(), firstItemSprite.getHeight());
const itemSprite = itemSprites[animationFrame];
palette.data.set(new Uint8ClampedArray(itemSprite.getPixels().m_buffer.buffer));
ctx.putImageData(palette, firstItemSprite.getWidth() * animationFrame, 0);
}
const callback = function (blob) {
canvas.remove();
zip.file('items/' + serverId + '_' + itemSprites.length + '.png', blob);
setTimeout(function () {
self.generateItemImage(imageGenerator, zip, serverId + 1);
}, 1);
};
canvas.toBlob(callback);
}
}
const itemImageGenerator = new ItemImageGenerator();
itemImageGenerator.init();