From 09e2b7e070ef41228fe7a26d85ac2e08a30e9ad0 Mon Sep 17 00:00:00 2001 From: brliron Date: Sun, 5 Feb 2017 20:07:16 +0100 Subject: [PATCH] thcrap_tasofro: add endings support [V] --- thcrap_tasofro/src/pl.c | 99 ++++++++++++++++++++--------- thcrap_tasofro/src/pl.h | 1 + thcrap_tasofro/src/thcrap_tasofro.c | 1 + 3 files changed, 72 insertions(+), 29 deletions(-) diff --git a/thcrap_tasofro/src/pl.c b/thcrap_tasofro/src/pl.c index 7deb8997..5c15e895 100644 --- a/thcrap_tasofro/src/pl.c +++ b/thcrap_tasofro/src/pl.c @@ -111,37 +111,12 @@ static int balloon_gen_name(balloon_t *balloon, int cur_line, json_t *lines) return 1; } -static void replace_line(BYTE *file_in, size_t size_in, BYTE **file_out, size_t *size_out, unsigned int balloon_nb, json_t *lines) +static void put_line_story(BYTE *file_in, size_t size_in, BYTE **file_out, size_t *size_out, json_t *lines, balloon_t *balloon) { - balloon_t *balloon = balloon_get(); - unsigned int i; - - // Skip the original text - if (*file_in != '"') { - while (size_in > 0 && *file_in != '\n' && *file_in != ',') { - MOVE_BUFF(file_in, size_in, 1); - } - balloon->has_pause = file_in[-1] == '\\'; - } - else { - MOVE_BUFF(file_in, size_in, 1); - while (size_in > 0 && *file_in != '\n' && *file_in != '"') { - MOVE_BUFF(file_in, size_in, 1); - } - balloon->has_pause = file_in[-1] == '\\'; - MOVE_BUFF(file_in, size_in, 1); - } - MOVE_BUFF(file_in, size_in, 1); - - // Copy the balloon name from the original file - for (i = 0; size_in > 0 && *file_in != '\n' && *file_in != ',' && i < 10; i++, file_in++, size_in--) { - balloon->name[i] = *file_in; - } - - // Write to the output file unsigned int cur_line = 0; unsigned int nb_lines = json_array_size(lines); int ignore_clear_balloon = 1; + unsigned int i; for (; cur_line < nb_lines; cur_line++, balloon->cur_line++) { if (balloon_gen_name(balloon, cur_line, lines) == 0) @@ -162,10 +137,11 @@ static void replace_line(BYTE *file_in, size_t size_in, BYTE **file_out, size_t // Writing the replacement text PUT_CHAR(*file_out, *size_out, '"'); PUT_STR(*file_out, *size_out, json_line); - if (balloon->has_pause && balloon->cur_line == balloon->nb_lines) { + if (balloon->has_pause && balloon->cur_line == balloon->nb_lines) { PUT_CHAR(*file_out, *size_out, '\\'); } PUT_CHAR(*file_out, *size_out, '"'); + PUT_CHAR(*file_out, *size_out, ','); // Writing the balloon name @@ -184,6 +160,66 @@ static void replace_line(BYTE *file_in, size_t size_in, BYTE **file_out, size_t } } +static void put_line_ending(BYTE *file_in, size_t size_in, BYTE **file_out, size_t *size_out, json_t *lines, balloon_t *balloon) +{ + unsigned int cur_line = 0; + unsigned int nb_lines = json_array_size(lines); + + PUT_CHAR(*file_out, *size_out, '"'); + for (; cur_line < nb_lines; cur_line++) { + const char *json_line = json_array_get_string(lines, cur_line); + if (cur_line != 0) { + PUT_STR(*file_out, *size_out, "\\n"); + } + PUT_STR(*file_out, *size_out, json_line); + } + if (balloon->has_pause) { + PUT_CHAR(*file_out, *size_out, '\\'); + } + PUT_STR(*file_out, *size_out, "\"\n"); +} + +static void replace_line(BYTE *file_in, size_t size_in, BYTE **file_out, size_t *size_out, unsigned int balloon_nb, json_t *lines) +{ + balloon_t *balloon = balloon_get(); + unsigned int i; + + // Skip the original text + if (*file_in != '"') { + while (size_in > 0 && *file_in != '\r' && *file_in != '\n' && *file_in != ',') { + MOVE_BUFF(file_in, size_in, 1); + } + balloon->has_pause = file_in[-1] == '\\'; + } + else { + MOVE_BUFF(file_in, size_in, 1); + while (size_in > 0 && *file_in != '\r' && *file_in != '\n' && *file_in != '"') { + MOVE_BUFF(file_in, size_in, 1); + } + balloon->has_pause = file_in[-1] == '\\'; + MOVE_BUFF(file_in, size_in, 1); + } + + if (*file_in == ',') { + balloon->is_ending = 1; + MOVE_BUFF(file_in, size_in, 1); + // Copy the balloon name from the original file + for (i = 0; size_in > 0 && *file_in != '\n' && *file_in != ',' && i < 10; i++, file_in++, size_in--) { + balloon->name[i] = *file_in; + } + } + else { + balloon->is_ending = 0; + } + + if (balloon->is_ending) { + put_line_story(file_in, size_in, file_out, size_out, lines, balloon); + } + else { + put_line_ending(file_in, size_in, file_out, size_out, lines, balloon); + } +} + int patch_pl(BYTE *file_inout, size_t size_out, size_t size_in, json_t *patch) { BYTE* buffer_in; @@ -204,7 +240,7 @@ int patch_pl(BYTE *file_inout, size_t size_out, size_t size_in, json_t *patch) balloon = 1; - for (; size_in > 0; next_line(&file_in, &size_in)) { + while (size_in > 0) { need_to_copy_line = TRUE; if (!ignore_line(file_in, size_in)) { @@ -221,6 +257,10 @@ int patch_pl(BYTE *file_inout, size_t size_out, size_t size_in, json_t *patch) replace_line(file_in, size_in, &file_inout, &size_out, balloon, lines); need_to_copy_line = FALSE; + // If the text is on more than 1 line (like in the endings or in the english patch), skip the other lines. + while (ignore_line(file_in, size_in) == FALSE) { + next_line(&file_in, &size_in); + } } json_decref_safe(lines); } @@ -229,6 +269,7 @@ int patch_pl(BYTE *file_inout, size_t size_out, size_t size_in, json_t *patch) if (need_to_copy_line) { copy_line(file_in, size_in, file_inout, size_out); + next_line(&file_in, &size_in); next_line(&file_inout, &size_out); } } diff --git a/thcrap_tasofro/src/pl.h b/thcrap_tasofro/src/pl.h index 97762233..5183b780 100644 --- a/thcrap_tasofro/src/pl.h +++ b/thcrap_tasofro/src/pl.h @@ -16,6 +16,7 @@ typedef struct { char name[10]; int has_pause; + int is_ending; int cur_line; int nb_lines; diff --git a/thcrap_tasofro/src/thcrap_tasofro.c b/thcrap_tasofro/src/thcrap_tasofro.c index 61833c95..8f919576 100644 --- a/thcrap_tasofro/src/thcrap_tasofro.c +++ b/thcrap_tasofro/src/thcrap_tasofro.c @@ -19,6 +19,7 @@ int __stdcall thcrap_plugin_init() size_t filenames_list_size; patchhook_register("*/stage*.pl", patch_pl); + patchhook_register("*/ed_*.pl", patch_pl); patchhook_register("*.csv", patch_tfcs); filenames_list = stack_game_file_resolve("fileslist.txt", &filenames_list_size); LoadFileNameListFromMemory(filenames_list, filenames_list_size);