Skip to content

Commit

Permalink
Add spell comment patching.
Browse files Browse the repository at this point in the history
Currently without owner support, as that requires #15 to be visible anyway.
Closes #11.
  • Loading branch information
nmlgc committed Jul 14, 2013
1 parent 3dae42d commit 205d9de
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
51 changes: 51 additions & 0 deletions thcrap_tsa/src/spells.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
29 changes: 29 additions & 0 deletions thcrap_tsa/src/thcrap_tsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions thcrap_tsa/thcrap_tsa.def
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ EXPORTS
; ------
BP_spell_id
BP_spell_name
BP_spell_comment_line
spells_init
spells_exit

Expand Down

0 comments on commit 205d9de

Please sign in to comment.