forked from igroglaz/a2mgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itempng.cpp
436 lines (380 loc) · 8.61 KB
/
itempng.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "Image.h"
#include "File.h"
#include "utils.h"
#include <windows.h>
void* ipng_out_primary = NULL;
void* ipng_out_secondary = NULL;
void _stdcall IImage_CopyTo(int arg1, int arg2, int arg3, int arg4)
{
char* c = NULL;
__asm mov c, ecx;
Image* image = *(Image**)(c+4);
if(!image) return;
if(image->GetWidth() != 160 ||
image->GetHeight() != 240) return;
uint8_t* upx = *(uint8_t**)(0x0062571C);
uint32_t* px = image->GetPixels();
for(uint32_t i = 0; i < 160*240; i++)
{
uint32_t src_color = px[i];
uint8_t src_a = (src_color & 0x000000FF);
if(src_a < 127) continue;
upx[i] = arg4;
}
}
void _stdcall IImage_Display(int arg1, int arg2, int arg3, int arg4, int arg5)
{
char* c = NULL;
__asm mov c, ecx;
Image* image = *(Image**)(c+4);
if(!image) return;
if(image->GetWidth() != 160 ||
image->GetHeight() != 240) return;
uint16_t* upx = *(uint16_t**)(0x0062571C);
uint32_t* px = image->GetPixels();
bool no_alpha = false;
for(uint32_t i = 0; i < 160*240; i++)
{
uint32_t src_color = px[i];
uint8_t src_r = (src_color & 0xFF000000) >> 27;
uint8_t src_g = (src_color & 0x00FF0000) >> 18;
uint8_t src_b = (src_color & 0x0000FF00) >> 11;
uint8_t src_a = (src_color & 0x000000FF);
if(no_alpha)
{
if(src_a > 127) src_a = 255;
else src_a = 0;
}
if(!src_a) continue;
uint16_t dst_color = upx[i];
uint8_t dst_r = (dst_color & 0xF800) >> 11;
uint8_t dst_g = (dst_color & 0x07E0) >> 5;
uint8_t dst_b = (dst_color & 0x001F);
// max for blue&red = 31.0
// max for green = 63.0
uint8_t out_r;
uint8_t out_g;
uint8_t out_b;
if(src_a != 255)
{
float alpha = (float)(src_a) / 255.0;
float Fout_r = (src_r * alpha) + (dst_r * (1.0-alpha));
float Fout_g = (src_g * alpha) + (dst_g * (1.0-alpha));
float Fout_b = (src_b * alpha) + (dst_b * (1.0-alpha));
out_r = max(min((uint8_t)Fout_r, 0x1F), 1);
out_g = max(min((uint8_t)Fout_g, 0x3F), 1);
out_b = max(min((uint8_t)Fout_b, 0x1F), 1);
}
else
{
out_r = max(src_r, 1);
out_g = max(src_g, 1);
out_b = max(src_b, 1);
}
dst_color = (out_r & 0x1F) << 11;
dst_color |= (out_g & 0x3F) << 5;
dst_color |= (out_b);
upx[i] = dst_color;
}
}
struct IImageUse
{
void* ptr;
int ref;
std::string filename;
};
std::vector<IImageUse> IImages;
void _stdcall IImage_Destruct(unsigned long final)
{
return;
/*
char* c = NULL;
__asm mov c, ecx;
Image* image = *(Image**)(c+4);
if (image) delete image;
delete[] c;*/
}
void* _stdcall IImage_Construct(std::string filename = "")
{
for (std::vector<IImageUse>::iterator it = IImages.begin();
it != IImages.end(); ++it)
{
IImageUse& use = (*it);
if (use.filename == filename)
{
use.ref++;
return use.ptr;
}
}
static unsigned long* vtable = NULL;
if (!vtable)
{
vtable = new unsigned long[17];
memset(vtable, 0, sizeof(unsigned long)*17);
vtable[1] = (unsigned long)&IImage_Destruct;
vtable[16] = (unsigned long)&IImage_CopyTo;
vtable[6] = (unsigned long)&IImage_Display;
}
char* c = new char[8];
memset(c, 0, 8);
*(unsigned long**)(c) = vtable;
Image* image = (filename.length() > 0) ? new Image(filename) : NULL;
*(Image**)(c+4) = image;
IImageUse use;
use.ptr = c;
use.ref = 1;
use.filename = filename;
IImages.push_back(use);
return c;
}
void* _stdcall ITEMPNG_doLoadPngChar(const char* location, const char* character)
{
int chrN = *(int*)(character+0x28);
std::string img = Format("data\\%s%d.png", location, chrN);
std::string oimg = Format("data\\%s%d.256", location, chrN);
void* v = NULL;
if(FileExistsEx(img))
{
v = IImage_Construct(img);
}
else
{
for (std::vector<IImageUse>::iterator it = IImages.begin();
it != IImages.end(); ++it)
{
IImageUse& use = (*it);
if (use.filename == oimg)
{
use.ref++;
return use.ptr;
}
}
const char* coimg = oimg.c_str();
__asm
{
push 0x24
mov edx, 0x00401840
call edx
push [coimg]
mov ecx, eax
mov edx, 0x00426DA9
call edx
mov [v], eax
push 0
push 1
push 1
mov ecx, eax
mov edx, 0x00426BE0
call edx
}
IImageUse use;
use.ptr = v;
use.ref = 1;
use.filename = oimg;
IImages.push_back(use);
}
return v;
}
void _stdcall ITEMPNG_doLoadPng(const char* character, int i, const char* str_primary, const char* str_secondary)
{
std::string i_pr = "data\\" + std::string(str_primary) + ".png";
std::string i_sc = "data\\" + std::string(str_secondary) + ".png";
std::string oi_pr = "data\\" + std::string(str_primary) + ".256";
std::string oi_sc = "data\\" + std::string(str_secondary) + ".256";
ipng_out_primary = NULL;
ipng_out_secondary = NULL;
if(FileExistsEx(i_pr))
{
//log_format("loading replacement for \"%s\"...\n", i_pr.c_str());
ipng_out_primary = IImage_Construct(i_pr);
}
else
{
for (std::vector<IImageUse>::iterator it = IImages.begin();
it != IImages.end(); ++it)
{
IImageUse& use = (*it);
if (use.filename == oi_pr)
{
use.ref++;
ipng_out_primary = use.ptr;
break;
}
}
if (!ipng_out_primary)
{
const char* coi_pr = oi_pr.c_str();
//log_format("loading original for \"%s\"...\n", coi_pr);
__asm
{
push 0x24
mov edx, 0x00401840
call edx
push [coi_pr]
mov ecx, eax
mov edx, 0x00426DA9
call edx
mov [ipng_out_primary], eax
push 0
push 1
push 1
mov ecx, eax
mov edx, 0x00426BE0
call edx
}
IImageUse use;
use.ptr = ipng_out_primary;
use.ref = 1;
use.filename = oi_pr;
IImages.push_back(use);
}
}
if(i == 3 || i == 8 || i == 9 || (i == 7 && (*(uint32_t*)(character+0x1B8) & 2)))
{
if(FileExistsEx(i_sc))
{
//log_format("loading replacement for \"%s\"...\n", i_sc.c_str());
ipng_out_secondary = IImage_Construct(i_sc);
}
else
{
for (std::vector<IImageUse>::iterator it = IImages.begin();
it != IImages.end(); ++it)
{
IImageUse& use = (*it);
if (use.filename == oi_sc)
{
use.ref++;
ipng_out_secondary = use.ptr;
break;
}
}
if (!ipng_out_secondary)
{
const char* coi_sc = oi_sc.c_str();
//log_format("loading original for \"%s\"...\n", coi_sc);
__asm
{
push 0x24
mov edx, 0x00401840
call edx
push [coi_sc]
mov ecx, eax
mov edx, 0x00426DA9
call edx
mov [ipng_out_secondary], eax
push 0
push 1
push 1
mov ecx, eax
mov edx, 0x00426BE0
call edx
}
IImageUse use;
use.ptr = ipng_out_secondary;
use.ref = 1;
use.filename = oi_sc;
IImages.push_back(use);
}
}
}
static void* iimg_null = NULL;
if (!iimg_null) iimg_null = IImage_Construct();
if(ipng_out_primary == NULL) ipng_out_primary = iimg_null;
if(ipng_out_secondary == NULL) ipng_out_secondary = iimg_null;
//log_format("spawned item: %08X %08X\n", ipng_out_primary, ipng_out_secondary);
}
// -> 46E18B
void __declspec(naked) ITEMPNG_loadPng()
{
__asm
{
push eax
push ebx
push ecx
push edx
push edi
push esi
lea edx, [ebp-0x77C]
lea ecx, [ebp-0x87C]
push ecx // secondary
push edx // primary
push [ebp-0x27C]
push [ebp-0x8C8]
call ITEMPNG_doLoadPng
pop edx
pop ecx
pop ebx
pop eax
pop esi
pop edi
mov eax, [ebp-0x27C]
mov ebx, [ipng_out_primary]
mov [ebp-0x244+eax*4], ebx
mov ebx, [ipng_out_secondary]
mov [ebp-0x274+eax*4], ebx
mov edx, 0x0046DE9E
jmp edx
}
}
// -> 46E1B0
void __declspec(naked) ITEMPNG_loadPngChar()
{
__asm
{
push eax
push ebx
push ecx
push edx
push edi
push esi
push [ebp-0x8C8]
lea ecx, [ebp-0x214]
push ecx
call ITEMPNG_doLoadPngChar
mov [ebp-0x14], eax
pop edx
pop ecx
pop ebx
pop eax
pop esi
pop edi
mov edx, 0x0046E21B
jmp edx
}
}
bool _stdcall ITEMPNG_doCachePng(void* ptr)
{
for (std::vector<IImageUse>::iterator it = IImages.begin();
it != IImages.end(); ++it)
{
IImageUse& use = (*it);
if (use.ptr == ptr)
return true;
}
return false;
}
// -> 42AAC0
void __declspec(naked) ITEMPNG_cachePng()
{
__asm
{
push ebp
mov ebp, esp
push ecx
mov [ebp-0x04], ecx
push ecx
call ITEMPNG_doCachePng
test eax, eax
jnz do_nothing
mov ecx, [ebp-0x04]
mov edx, 0x0042AAF0
call edx
mov edx, 0x0042AACF
jmp edx
do_nothing:
mov esp, ebp
pop ebp
retn 0x0004
}
}