/
anm.c
234 lines (205 loc) · 5.57 KB
/
anm.c
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
/**
* Touhou Community Reliant Automatic Patcher
* Team Shanghai Alice support plugin
*
* ----
*
* On-the-fly ANM patcher.
*
* Portions adapted from xarnonymous' Touhou Toolkit
* http://code.google.com/p/thtk/
*/
#include <thcrap.h>
#include "thcrap_tsa.h"
#include <png.h>
/// Enums
/// -----
// All of the 16-bit formats are little-endian.
typedef enum {
FORMAT_BGRA8888 = 1,
FORMAT_RGB565 = 3,
// 0xGB 0xAR
FORMAT_ARGB4444 = 5,
FORMAT_GRAY8 = 7
} format_t;
/// -----
/// Structures
/// ----------
typedef struct {
#ifdef PACK_PRAGMA
#pragma pack(push,1)
#endif
char magic[4];
WORD zero;
WORD format;
/* These may be different from the parent entry. */
WORD w, h;
DWORD size;
#ifdef PACK_PRAGMA
#pragma pack(pop)
#endif
unsigned char data[];
} PACK_ATTRIBUTE thtx_header_t;
/// ----------
/// JSON-based structure data access
/// --------------------------------
int struct_get(void *dest, size_t dest_size, void *src, json_t *spec)
{
if(!dest || !dest_size || !src || !spec || !spec) {
return -1;
}
{
json_t *spec_offset = json_object_get(spec, "offset");
json_t *spec_size = json_object_get(spec, "size");
size_t offset = json_hex_value(spec_offset);
// Default to architecture word size
size_t size = sizeof(size_t);
if(spec_size) {
size = json_hex_value(spec_size);
};
if(size > dest_size) {
return -2;
}
ZeroMemory(dest, dest_size);
memcpy(dest, (char*)src + offset, size);
}
return 0;
}
#define STRUCT_GET(type, val, src, spec_obj) \
struct_get(&(val), sizeof(type), anm_entry_out, json_object_get(spec_obj, #val))
/// --------------------------------
/// Formats
/// -------
unsigned int format_Bpp(format_t format)
{
switch(format) {
case FORMAT_BGRA8888:
return 4;
case FORMAT_ARGB4444:
case FORMAT_RGB565:
return 2;
case FORMAT_GRAY8:
return 1;
default:
log_printf("unknown format: %u\n", format);
return 0;
}
}
// Converts a number of BGRA8888 [pixels] in [data] to the given [format] in-place.
void format_from_bgra(png_bytep data, unsigned int pixels, format_t format)
{
unsigned int i = 0;
png_bytep in = data;
if(format == FORMAT_ARGB4444) {
png_bytep out = data;
for(i = 0; i < pixels; ++i, in += 4, out += 2) {
// I don't see the point in doing any "rounding" here. Let's rather focus on
// writing understandable code independent of endianness assumptions.
const unsigned char b = in[0] >> 4;
const unsigned char g = in[1] >> 4;
const unsigned char r = in[2] >> 4;
const unsigned char a = in[3] >> 4;
out[1] = (a << 4) | r;
out[0] = (g << 4) | b;
}
}
}
/// -------
int patch_thtx(thtx_header_t *thtx, size_t x, size_t y, void *rep_buffer, size_t rep_size)
{
png_image rep_png;
png_bytep rep_png_buffer = NULL;
if(!thtx || !rep_buffer) {
return -1;
}
if(strncmp(thtx->magic, "THTX", sizeof(thtx->magic))) {
return -2;
}
// Only support for single-texture images right now
if(
thtx->format != FORMAT_BGRA8888 &&
thtx->format != FORMAT_ARGB4444
) {
log_printf("No patching support for texture format %d at this point.\n", thtx->format);
return 1;
} else if(x || y) {
log_printf("No patching support for multi-texture images at this point.\n");
return 1;
}
ZeroMemory(&rep_png, sizeof(png_image));
rep_png.version = PNG_IMAGE_VERSION;
if(png_image_begin_read_from_memory(&rep_png, rep_buffer, rep_size)) {
size_t rep_png_size;
rep_png.format = PNG_FORMAT_BGRA;
rep_png_size = PNG_IMAGE_SIZE(rep_png);
if(rep_png.width * rep_png.height * format_Bpp(thtx->format) == thtx->size) {
rep_png_buffer = (png_bytep)malloc(rep_png_size);
if(rep_png_buffer) {
png_image_finish_read(&rep_png, 0, rep_png_buffer, 0, NULL);
}
} else {
log_printf(
"Image size mismatch, patching not supported yet!\n"
"\t(format: %d, original: %d, replacement: %d)\n",
thtx->format, thtx->size, rep_png_size
);
}
}
if(rep_png_buffer) {
format_from_bgra(rep_png_buffer, rep_png.width * rep_png.height, thtx->format);
memcpy(thtx->data, rep_png_buffer, thtx->size);
SAFE_FREE(rep_png_buffer);
}
png_image_free(&rep_png);
return 0;
}
int patch_anm(BYTE *file_inout, size_t size_out, size_t size_in, json_t *patch, json_t *run_cfg)
{
json_t *format;
// Some ANMs reference the same file name multiple times in a row
char *name_prev = NULL;
void *rep_buffer = NULL;
size_t rep_size;
BYTE *anm_entry_out = file_inout;
format = json_object_get(json_object_get(run_cfg, "formats"), "anm");
log_printf("---- ANM ----\n");
while(anm_entry_out < file_inout + size_in) {
size_t x;
size_t y;
size_t nameoffset;
size_t thtxoffset;
size_t hasdata;
size_t nextoffset;
if(
STRUCT_GET(size_t, x, anm_entry_out, format) ||
STRUCT_GET(size_t, y, anm_entry_out, format) ||
STRUCT_GET(size_t, nameoffset, anm_entry_out, format) ||
STRUCT_GET(size_t, thtxoffset, anm_entry_out, format) ||
STRUCT_GET(size_t, hasdata, anm_entry_out, format) ||
STRUCT_GET(size_t, nextoffset, anm_entry_out, format)
) {
log_printf("Corrupt ANM file or format definition, aborting ...\n");
return -2;
}
if(hasdata && thtxoffset) {
char *name = (char*)(anm_entry_out + nameoffset);
thtx_header_t *thtx = (thtx_header_t*)(anm_entry_out + thtxoffset);
if(!name_prev || strcmp(name, name_prev)) {
SAFE_FREE(rep_buffer);
rep_buffer = stack_game_file_resolve(name, &rep_size);
name_prev = name;
}
if(rep_buffer) {
patch_thtx(thtx, x, y, rep_buffer, rep_size);
}
}
if(!nextoffset) {
break;
} else {
anm_entry_out += nextoffset;
}
}
SAFE_FREE(rep_buffer);
log_printf("-------------\n");
return 1;
}