-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.js
197 lines (169 loc) · 4.08 KB
/
plugin.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
export default class ExtendableHeads {
prestart() {
this.initPrestart();
}
async initPrestart() {
// Image loaded successfully
const oldImage = await this.loadImage("media/gui/severed-heads.png");
const {headIdx} = await this.getHeadIdx();
let imgs = [oldImage];
// assume each image is 24x24
for (let index = 0; index < headIdx.length; ++index) {
const head = headIdx[index];
try {
imgs.push(await this.loadImage(head.img));
} catch (e) {
headIdx.splice(index, 1);
--index;
console.log(`Cound not load head of "${head.id}"`);
console.log(e);
}
}
const newImage = await this.mergeImages(imgs);
const dim = this.getTotalDim([newImage]);
const startIndex = this.calcStartIndex(dim, headIdx);
const customIdx = {};
let customNameMapping = {};
for (let index = 0; index < headIdx.length; ++index) {
const head = headIdx[index];
customIdx[head.id] = startIndex + index;
if (head.name) {
if (head.name in customNameMapping) {
console.warn(`Warning: duplicate name entry "${head.name}" for custom head of ${head.id}`);
} else {
customNameMapping[head.name] = startIndex + index;
}
}
}
sc.PlayerConfig.inject({
onload: function (config) {
if (!config.jsonTEMPLATES) {
const id = config.character;
if (id in customIdx) {
config.headIdx = customIdx[id];
}
}
this.parent(config);
},
});
sc.SaveSlotParty.inject({
setParty: function ({player}) {
try {
this.party[0] = sc.party.models[player.playerConfig].getHeadIdx();
} catch (e) {}
return this.parent.apply(this, arguments);
},
});
sc.CombatUpperHud.inject({
init: function () {
this.parent();
const pvp = this.sub.pvp;
const old = pvp._renderHeads;
pvp._renderHeads = function (_renderer, _x, flip, idxArr) {
if (flip) {
idxArr[0] = sc.model.player.config.headIdx;
}
return old.apply(this, arguments);
};
},
});
function customNameToHeadIdx(headIdx) {
if (typeof headIdx == "string" && headIdx in customNameMapping) {
return customNameMapping[headIdx];
}
return headIdx;
}
sc.PartyMemberModel.inject({
getHeadIdx() {
return customNameToHeadIdx(this.parent());
},
});
ig.ENTITY.Enemy.inject({
getHeadIdx() {
return customNameToHeadIdx(this.parent());
},
});
const img = new ig.Image("media/gui/severed-heads.png");
img.addLoadListener({
onLoadableComplete: function (loaded, image) {
// replace image
if (loaded) {
if (!image.failed) {
image.width = newImage.width;
image.height = newImage.height;
image.data = newImage;
}
}
},
});
}
async loadImage(src) {
let imgData = new Image();
await new Promise((resolve, reject) => {
imgData.onload = () => {
resolve();
};
imgData.onerror = () => {
reject();
};
imgData.src = src;
});
return imgData;
}
getTotalDim(imgs) {
let dim = {
x: 0,
y: 0,
};
for (const img of imgs) {
dim.x += img.width;
dim.y = Math.max(dim.y, img.height);
}
return dim;
}
async mergeImages(imgs) {
const canvas = document.createElement("canvas");
// preset canvas
// normalize the width
// calculate the true width
let width = 0;
const xPos = [];
for (const img of imgs) {
xPos.push(width);
const remainder = 24 - (img.width % 24);
if (remainder % 24 === 0) {
width += img.width;
} else {
// round up
width += img.width + remainder;
}
}
canvas.width = width;
canvas.height = 24;
const ctx = canvas.getContext("2d");
// want to go by a factor of 24
// go one by one
for (const img of imgs) {
ctx.drawImage(img, xPos.shift(), 0);
}
return await this.loadImage(canvas.toDataURL("image/png"));
}
calcStartIndex(dim, extraImgs) {
// assume each head is 24x24
return (dim.x - extraImgs.length * 24) / 24;
}
getHeadIdx() {
return new Promise((resolve, reject) => {
$.ajax({
dataType: "json",
url: "data/players/headIdx.json",
success: data => {
resolve(data);
},
error: () => {
reject();
},
});
});
}
}