Skip to content

Commit

Permalink
thcrap_tasofro: th105: adjust the width/height in dat files to fit the
Browse files Browse the repository at this point in the history
related PNGs.
  • Loading branch information
brliron committed Nov 28, 2017
1 parent 3bf8a58 commit c78ff8d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions thcrap_tasofro/src/nsml.cpp
Expand Up @@ -14,6 +14,7 @@

size_t get_cv2_size_for_th123(const char *fn, json_t*, size_t);
int patch_cv2_for_th123(void *file_inout, size_t size_out, size_t size_in, const char *fn, json_t*);
int patch_dat_for_png_for_th123(void *file_inout, size_t size_out, size_t size_in, const char *fn, json_t *patch);
static CRITICAL_SECTION cs;
static std::set<const char*, bool(*)(const char*, const char*)> game_fallback_ignore_list([](const char *a, const char *b){ return strcmp(a, b) < 0; });

Expand All @@ -28,9 +29,11 @@ int nsml_init()
}
else if (game_id == TH105) {
patchhook_register("*.cv2", patch_cv2, get_cv2_size);
patchhook_register("*.dat", patch_dat_for_png, [](const char*, json_t*, size_t) -> size_t { return 0; });
}
else if (game_id == TH123) {
patchhook_register("*.cv2", patch_cv2_for_th123, get_cv2_size_for_th123);
patchhook_register("*.dat", patch_dat_for_png_for_th123, [](const char*, json_t*, size_t) -> size_t { return 0; });
json_t *list = stack_game_json_resolve("game_fallback_ignore_list.js", nullptr);
size_t i;
json_t *value;
Expand Down Expand Up @@ -116,6 +119,16 @@ int patch_cv2_for_th123(void *file_inout, size_t size_out, size_t size_in, const
return ret;
}

int patch_dat_for_png_for_th123(void *file_inout, size_t size_out, size_t size_in, const char *fn, json_t *patch)
{
int ret = 0;
run_with_game_fallback("th105", nullptr, fn, [file_inout, size_out, size_in, fn, patch, &ret]() {
ret = patch_dat_for_png(file_inout, size_out, size_in, fn, patch);
});
int ret2 = patch_dat_for_png(file_inout, size_out, size_in, fn, patch);
return max(ret, ret2);
}

int BP_nsml_file_header(x86_reg_t *regs, json_t *bp_info)
{
// Parameters
Expand Down
51 changes: 51 additions & 0 deletions thcrap_tasofro/src/nsml_images.cpp
Expand Up @@ -90,6 +90,57 @@ int patch_cv2(void *file_inout, size_t size_out, size_t size_in, const char *fn,
return 1;
}

int patch_dat_for_png(void *file_inout, size_t, size_t size_in, const char *fn, json_t*)
{
if (size_in < 8) {
return 0;
}

BYTE* file_in = (BYTE*)file_inout;
DWORD magic = *(DWORD*)file_in;
if (magic != 4) {
return 0;
}

char path[MAX_PATH];
strcpy(path, fn);
char *path_fn = strrchr(path, '/') + 1;

DWORD nb_files = *(DWORD*)(file_in + 4);
file_in += 8; size_in -= 8;

for (DWORD i = 0; i < nb_files; i++) {
if (size_in < 4) {
return 0;
}
DWORD fn_size = *(DWORD*)file_in;
file_in += 4; size_in -= 4;
if (size_in < fn_size + 16) {
return 0;
}

memcpy(path_fn, file_in, fn_size);
path_fn[fn_size] = '\0';
file_in += fn_size; size_in -= fn_size;

// Retrieve the PNG width/height
strcpy(PathFindExtensionA(path_fn), ".png");
uint32_t width, height;
bool IHDR_read = png_image_get_IHDR(path, &width, &height, nullptr);
if (!IHDR_read) {
file_in += 16; size_in -= 16;
continue;
}

DWORD x = *(DWORD*)file_in;
DWORD y = *(DWORD*)(file_in + 4);
*(DWORD*)(file_in + 8) = width;
*(DWORD*)(file_in + 12) = height;
file_in += 16; size_in -= 16;
}
return 1;
}

size_t get_bmp_size(const char *fn, json_t*, size_t)
{
return sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + get_image_data_size(fn, false);
Expand Down
2 changes: 2 additions & 0 deletions thcrap_tasofro/src/nsml_images.h
Expand Up @@ -18,3 +18,5 @@ size_t get_bmp_size(const char *fn, json_t*, size_t);

int patch_cv2(void *file_inout, size_t size_out, size_t size_in, const char *fn, json_t*);
int patch_bmp(void *file_inout, size_t size_out, size_t size_in, const char *fn, json_t*);

int patch_dat_for_png(void *file_inout, size_t, size_t size_in, const char *fn, json_t*);

0 comments on commit c78ff8d

Please sign in to comment.