Skip to content

Commit

Permalink
Add Music Room title patching.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmlgc committed Aug 20, 2013
1 parent 2b7c110 commit cc50311
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 21 deletions.
69 changes: 55 additions & 14 deletions thcrap_tsa/src/music.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,74 @@
#include <thcrap.h>
#include "thcrap_tsa.h"

static json_t *themes = NULL;
static json_t *musiccmt = NULL;
static size_t cache_track = 0;
static size_t cache_cmt_line = 0;

// Helper for generic parameters
int breakpoint_cache_param(size_t *target, const char *param, x86_reg_t *regs, json_t *bp_info)
const char* music_title_get(size_t track)
{
size_t *val;
if(!regs || !bp_info || !param) {
return -1;
const char *game = json_object_get_string(runconfig_get(), "game");
if(!game || !json_is_object(themes)) {
return NULL;
}
val = (size_t*)json_object_get_register(bp_info, regs, param);
if(val) {
*target = *val;
{
size_t key_len = strlen(game) + 1 + 2 + str_num_digits(track) + 1;
VLA(char, key, key_len);
sprintf(key, "%s_%02u", game, track);
return json_object_get_string(themes, key);
}
}

void music_title_print(const char **str, const char *format_id, size_t track)
{
if(str) {
const char *format = strings_get(format_id);
const char *title = music_title_get(track);
if(title) {
if(format) {
*str = strings_sprintf(0, format, track, title);
} else {
*str = title;
}
}
}
}

const char** BP_music_params(x86_reg_t *regs, json_t *bp_info)
{
// Parameters
// ----------
size_t *track = (size_t*)json_object_get_register(bp_info, regs, "track");
// ----------
if(track) {
cache_track = (*track) + 1;
}
return (const char**)json_object_get_register(bp_info, regs, "str");
}

int BP_music_title(x86_reg_t *regs, json_t *bp_info)
{
const char **str = BP_music_params(regs, bp_info);
const char *format_id = json_object_get_string(bp_info, "format_id");
music_title_print(str, format_id, cache_track);
return 1;
}

int BP_music_cmt(x86_reg_t *regs, json_t *bp_info)
{
static size_t cache_cmt_line = 0;

// Parameters
// ----------
const char **str = (const char**)json_object_get_register(bp_info, regs, "str");
const char **str = BP_music_params(regs, bp_info);
const char *format_id = json_object_get_string(bp_info, "format_id");
size_t *line_num = json_object_get_register(bp_info, regs, "line_num");
// ----------
breakpoint_cache_param(&cache_track, "track", regs, bp_info);
breakpoint_cache_param(&cache_cmt_line, "line_num", regs, bp_info);

if(line_num) {
cache_cmt_line = *line_num;
}
if(str && *str) {
json_t *cmt = json_object_get_numkey(musiccmt, cache_track + 1);
json_t *cmt = json_object_get_numkey(musiccmt, cache_track);
if(json_is_array(cmt)) {
*str = json_array_get_string_safe(cmt, cache_cmt_line);
}
Expand All @@ -48,10 +87,12 @@ int BP_music_cmt(x86_reg_t *regs, json_t *bp_info)

void music_init()
{
themes = stack_json_resolve("themes.js", NULL);
musiccmt = stack_game_json_resolve("musiccmt.js", NULL);
}

void music_exit()
{
json_decref(themes);
json_decref(musiccmt);
}
73 changes: 66 additions & 7 deletions thcrap_tsa/src/thcrap_tsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int BP_spell_name(x86_reg_t *regs, json_t *bp_info);
* Type: hex
*
* [line_num]
* Line number. Array element in the JSON array of [comment_num].
* Line number. Array index in the JSON array of [comment_num].
* Type: hex
*
* [cave_exec]
Expand Down Expand Up @@ -126,16 +126,75 @@ void spells_init();
void spells_exit();
/// ------

/// Music Room
/// ----------
// Returns the translated title for track #[track] of the current game.
const char* music_title_get(size_t track);

// Prints the translated title of track #[track] to [str], according to
// [format_id] in the string definition table. This format string receives
// the track number and its translated title, in this order.
void music_title_print(const char **str, const char *format_id, size_t track);

/**
* Pseudo breakpoint taking parameters cached across the other
* Music Room breakpoints. *Not* exported.
*
* Own JSON parameters
* -------------------
* [track]
* Music Room track number, 0-based. Gets cached 1-based.
* Type: register
*
* [str]
* Register containing the address of the target string.
* Also the return value of this function.
* Type: register
*/
const char** BP_music_params(x86_reg_t *regs, json_t *bp_info);

/**
* Music title patching.
*
* Own JSON parameters
* -------------------
* [format_id]
* Music title format string to use for the title.
* Type: string (ID in the string definition table)
*
* Other breakpoints called
* ------------------------
* BP_music_params
*/
int BP_music_cmt(x86_reg_t *regs, json_t *bp_info);

/**
* Music comment patching.
*
* Own JSON parameters
* -------------------
* [format_id]
* Music title format string to replace lines consisting of a single "@".
* Type: string (ID in the string definition table)
*
* [line_num]
* Line number of the comment.
* Type: register
*
* Other breakpoints called
* ------------------------
* BP_music_params
*/
int BP_music_cmt(x86_reg_t *regs, json_t *bp_info);

void music_init();
void music_exit();
/// ----------

/// Format patchers
/// ---------------
int patch_msg(BYTE *msg_out, size_t size_out, size_t size_in, json_t *patch, json_t *format);
int patch_msg_dlg(BYTE *msg_out, size_t size_out, size_t size_in, json_t *patch, json_t *run_cfg);
int patch_msg_end(BYTE *msg_out, size_t size_out, size_t size_in, json_t *patch, json_t *run_cfg);
int patch_anm(BYTE *msg_out, size_t size_out, size_t size_in, json_t *patch, json_t *run_cfg);
/// ---------------

/// Music Room
/// ----------
void music_init();
void music_exit();
/// ----------
4 changes: 4 additions & 0 deletions thcrap_tsa/thcrap_tsa.def
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ EXPORTS

; Music
; -----
music_title_get
music_title_print

BP_music_title
BP_music_cmt

music_init
Expand Down

0 comments on commit cc50311

Please sign in to comment.