Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented smlua_text_utils_dialog_get #321

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autogen/convert_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"src/pc/lua/utils/smlua_audio_utils.h": [ "smlua_audio_utils_override", "audio_custom_shutdown", "smlua_audio_custom_init", "smlua_audio_custom_deinit", "audio_sample_destroy_pending_copies", "audio_custom_update_volume" ],
"src/pc/djui/djui_hud_utils.h": [ "djui_hud_render_texture", "djui_hud_render_texture_raw", "djui_hud_render_texture_tile", "djui_hud_render_texture_tile_raw" ],
"src/pc/lua/utils/smlua_level_utils.h": [ "smlua_level_util_reset" ],
"src/pc/lua/utils/smlua_text_utils.h": [ "smlua_text_utils_init", "smlua_text_utils_shutdown", "smlua_text_utils_reset_all" ],
"src/pc/lua/utils/smlua_text_utils.h": [ "smlua_text_utils_init", "smlua_text_utils_shutdown", "smlua_text_utils_reset_all", "smlua_text_utils_dialog_get" ],
"src/pc/lua/utils/smlua_anim_utils.h": [ "smlua_anim_util_reset", "smlua_anim_util_register_animation" ],
"src/pc/network/lag_compensation.h": [ "lag_compensation_clear", "lag_compensation_store" ],
"src/game/first_person_cam.h": [ "first_person_update" ],
Expand Down
54 changes: 54 additions & 0 deletions src/pc/lua/smlua_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "include/macro_presets.h"
#include "utils/smlua_anim_utils.h"
#include "utils/smlua_collision_utils.h"
#include "utils/smlua_text_utils.h"

bool smlua_functions_valid_param_count(lua_State* L, int expected) {
int top = lua_gettop(L);
Expand Down Expand Up @@ -814,6 +815,58 @@ int smlua_func_log_to_console(lua_State* L) {
return 1;
}

/////////////
// dialogs //
/////////////

int smlua_func_smlua_text_utils_dialog_get(lua_State* L) {
if (L == NULL) { return 0; }

int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "smlua_text_utils_dialog_get", 1, top);
return 0;
}

int dialogId = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "smlua_text_utils_dialog_get"); return 0; }

struct DialogEntry *dialog = smlua_text_utils_dialog_get(dialogId);

if (!dialog) { return 0; }

static char output[DIALOG_MAXIMUM_LENGTH];
convert_string_sm64_to_ascii(output, segmented_to_virtual(dialog->str));

lua_newtable(L);

lua_pushstring(L, "str");
lua_pushstring(L, output);
lua_settable(L, -3);

lua_pushstring(L, "dialogId");
lua_pushinteger(L, dialogId);
lua_settable(L, -3);

lua_pushstring(L, "unused");
lua_pushinteger(L, dialog->unused);
lua_settable(L, -3);

lua_pushstring(L, "linesPerBox");
lua_pushinteger(L, dialog->linesPerBox);
lua_settable(L, -3);

lua_pushstring(L, "leftOffset");
lua_pushinteger(L, dialog->leftOffset);
lua_settable(L, -3);

lua_pushstring(L, "width");
lua_pushinteger(L, dialog->width);
lua_settable(L, -3);

return 1;
}

////////////////////
// scroll targets //
////////////////////
Expand Down Expand Up @@ -902,4 +955,5 @@ void smlua_bind_functions(void) {
smlua_bind_function(L, "log_to_console", smlua_func_log_to_console);
smlua_bind_function(L, "add_scroll_target", smlua_func_add_scroll_target);
smlua_bind_function(L, "collision_find_surface_on_ray", smlua_func_collision_find_surface_on_ray);
smlua_bind_function(L, "smlua_text_utils_dialog_get", smlua_func_smlua_text_utils_dialog_get);
}
41 changes: 38 additions & 3 deletions src/pc/lua/utils/smlua_text_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ static bool sReplacedActName[(COURSE_RR+2)*6] = { 0 };

#define INVALID_COURSE_NUM(courseNum) (smlua_level_util_get_info_from_course_num(courseNum) == NULL && !COURSE_IS_VALID_COURSE(courseNum))

void convert_string_sm64_to_ascii(char *strAscii, const u8 *str64);

struct CourseName *gReplacedActNameTable[COURSE_END];

static bool sSmluaTextUtilsInited = false;
Expand Down Expand Up @@ -160,8 +158,45 @@ void smlua_text_utils_reset_all(void) {
}
}

struct DialogEntry* smlua_text_utils_dialog_get(enum DialogId dialogId) {
if (dialogId >= DIALOG_COUNT) {
LOG_LUA("smlua_text_utils_dialog_get: Invalid dialogId: %d, expected 1-%d", dialogId, DIALOG_COUNT);
return NULL;
}

void **dialogTable = NULL;

#ifdef VERSION_EU
switch (gInGameLanguage) {
case LANGUAGE_ENGLISH:
dialogTable = segmented_to_virtual(dialog_table_eu_en);
break;
case LANGUAGE_FRENCH:
dialogTable = segmented_to_virtual(dialog_table_eu_fr);
break;
case LANGUAGE_GERMAN:
dialogTable = segmented_to_virtual(dialog_table_eu_de);
break;
}
#else
dialogTable = segmented_to_virtual(seg2_dialog_table);
#endif

struct DialogEntry *dialog = segmented_to_virtual(dialogTable[dialogId]);

return dialog;
}

void smlua_text_utils_dialog_replace(enum DialogId dialogId, UNUSED u32 unused, s8 linesPerBox, s16 leftOffset, s16 width, const char* str) {
if (dialogId >= DIALOG_COUNT) { return; }
if (dialogId >= DIALOG_COUNT) {
LOG_LUA("smlua_text_utils_dialog_replace: Invalid dialogId: %d, expected 1-%d", dialogId, DIALOG_COUNT);
return;
}

if (strlen(str) > DIALOG_MAXIMUM_LENGTH) {
LOG_LUA("smlua_text_utils_dialog_replace: Dialog string too long, max length is %d", DIALOG_MAXIMUM_LENGTH);
return;
}

void **dialogTable = NULL;

Expand Down
5 changes: 5 additions & 0 deletions src/pc/lua/utils/smlua_text_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "types.h"
#include "dialog_ids.h"

#define DIALOG_MAXIMUM_LENGTH 1024

#define MAX_ACTS 6
#define MAX_ACTS_AND_100_COINS 7

Expand All @@ -25,6 +27,7 @@ extern struct CourseName *gReplacedActNameTable[];
void smlua_text_utils_init(void);
void smlua_text_utils_shutdown(void);
void smlua_text_utils_reset_all(void);
struct DialogEntry* smlua_text_utils_dialog_get(enum DialogId dialogId);
void smlua_text_utils_dialog_replace(enum DialogId dialogId, u32 unused, s8 linesPerBox, s16 leftOffset, s16 width, const char* str);
void smlua_text_utils_course_acts_replace(s16 courseNum, const char* courseName, const char* act1, const char* act2, const char* act3, const char* act4, const char* act5, const char* act6);
void smlua_text_utils_secret_star_replace(s16 courseNum, const char* courseName);
Expand All @@ -40,4 +43,6 @@ void smlua_text_utils_castle_secret_stars_replace(const char* name);
void smlua_text_utils_extra_text_replace(s16 index, const char* text);
const char* smlua_text_utils_get_language(void);

void convert_string_sm64_to_ascii(char *strAscii, const u8 *str64);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convert_string_sm64_to_ascii does not belong to smlua_text_utils.c
it is defined in level_info.c

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can correct this 👍


#endif