diff --git a/thcrap_tsa/src/spells.c b/thcrap_tsa/src/spells.c index 825735f9..9e9fdc39 100644 --- a/thcrap_tsa/src/spells.c +++ b/thcrap_tsa/src/spells.c @@ -12,6 +12,7 @@ // Lookup cache static json_t *spells = NULL; +static json_t *spellcomments = NULL; static size_t cache_spell_id; static size_t cache_spell_id_real; @@ -66,6 +67,54 @@ int BP_spell_name(x86_reg_t *regs, json_t *bp_info) return 1; } +int BP_spell_comment_line(x86_reg_t *regs, json_t *bp_info) +{ + // Parameters + // ---------- + const char **str = (const char**)json_object_get_register(bp_info, regs, "str"); + size_t comment_num = json_object_get_hex(bp_info, "comment_num"); + size_t line_num = json_object_get_hex(bp_info, "line_num"); + json_t *cave_exec = json_object_get(bp_info, "cave_exec"); + // ---------- + + // Other breakpoints + // ----------------- + BP_spell_id(regs, bp_info); + // ----------------- + + if(str && comment_num) { + json_t *json_cmt = NULL; + size_t i = cache_spell_id_real; + + size_t cmt_key_str_len = strlen("comment_") + 16 + 1; + VLA(char, cmt_key_str, cmt_key_str_len); + sprintf(cmt_key_str, "comment_%d", comment_num); + + // Count down from the real number to the given number + // until we find something + do { + char key_str[16]; + _itoa(i, key_str, 10); + + json_cmt = json_object_get(spellcomments, key_str); + json_cmt = json_object_get(json_cmt, cmt_key_str); + } while( (i-- > cache_spell_id) && !json_is_array(json_cmt) ); + + if(json_is_array(json_cmt)) { + const char *new_str; + + if(line_num >= json_array_size(json_cmt)) { + new_str = ""; + } else { + new_str = json_array_get_string(json_cmt, line_num); + } + *str = new_str; + return !json_is_false(cave_exec); + } + } + return 1; +} + int patch_std(BYTE *msg_out, size_t size_out, size_t size_in, json_t *patch, json_t *format) { // TODO: This could be much nicer once issue #5 has been dealt with. @@ -77,9 +126,11 @@ int patch_std(BYTE *msg_out, size_t size_out, size_t size_in, json_t *patch, jso void spells_init() { spells = stack_game_json_resolve("spells.js", NULL); + spellcomments = stack_game_json_resolve("spellcomments.js", NULL); } void spells_exit() { json_decref(spells); + json_decref(spellcomments); } diff --git a/thcrap_tsa/src/thcrap_tsa.h b/thcrap_tsa/src/thcrap_tsa.h index 9dee770b..db44c9d2 100644 --- a/thcrap_tsa/src/thcrap_tsa.h +++ b/thcrap_tsa/src/thcrap_tsa.h @@ -80,6 +80,35 @@ int BP_spell_id(x86_reg_t *regs, json_t *bp_info); */ int BP_spell_name(x86_reg_t *regs, json_t *bp_info); +/** + * Writes a single translated spell comment line. + * + * Own JSON parameters + * ------------------- + * [str] + * Register to write to. + * Type: register + * + * [comment_num] + * Comment number. Gets prefixed with "comment_" to make up the JSON key. + * Must be at least 1. + * Type: hex + * + * [line_num] + * Line number. Array element in the JSON array of [comment_num]. + * Type: hex + * + * [cave_exec] + * Set to false to disable the execution of the code cave + * if the spell name is replaced. + * Type: bool + * + * Other breakpoints called + * ------------------------ + * BP_spell_id + */ +int BP_spell_comment_line(x86_reg_t *regs, json_t *bp_info); + /** * For now, this is merely a dummy hook to make sure that the spell files are * reloaded at the beginning of a stage. diff --git a/thcrap_tsa/thcrap_tsa.def b/thcrap_tsa/thcrap_tsa.def index 6385ae7f..0c7237d7 100644 --- a/thcrap_tsa/thcrap_tsa.def +++ b/thcrap_tsa/thcrap_tsa.def @@ -11,6 +11,7 @@ EXPORTS ; ------ BP_spell_id BP_spell_name + BP_spell_comment_line spells_init spells_exit