From 08a2490954a956521a30ccf460a20eb79f2ead28 Mon Sep 17 00:00:00 2001 From: PeachyPeach <72323920+PeachyPeachSM64@users.noreply.github.com> Date: Thu, 9 Jan 2025 05:59:16 +0100 Subject: [PATCH] Lua get_uncolored_string + bug fixes (#620) --- src/pc/lua/smlua_functions.c | 19 +++++++++++++++++++ src/pc/lua/smlua_hooks.c | 18 ++---------------- src/pc/mods/mod.c | 8 ++++++-- src/pc/mods/mods.c | 25 ++++++------------------- src/pc/utils/misc.c | 15 +++++++++++++++ src/pc/utils/misc.h | 1 + 6 files changed, 49 insertions(+), 37 deletions(-) diff --git a/src/pc/lua/smlua_functions.c b/src/pc/lua/smlua_functions.c index 0db48a484..ebc66ed93 100644 --- a/src/pc/lua/smlua_functions.c +++ b/src/pc/lua/smlua_functions.c @@ -11,6 +11,7 @@ #include "engine/math_util.h" #include "engine/level_script.h" #include "pc/djui/djui_hud_utils.h" +#include "pc/utils/misc.h" #include "include/level_misc_macros.h" #include "include/macro_presets.h" #include "utils/smlua_anim_utils.h" @@ -1009,6 +1010,23 @@ int smlua_func_cast_graph_node(lua_State* L) { return 1; } + ///////////// + // strings // +///////////// + +int smlua_func_get_uncolored_string(lua_State* L) { + if (!smlua_functions_valid_param_count(L, 1)) { return 0; } + + const char *str = smlua_to_string(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("get_uncolored_string: Failed to convert parameter 1"); return 0; } + + char *strNoColor = str_remove_color_codes(str); + lua_pushstring(L, strNoColor); + free(strNoColor); + + return 1; +} + ////////// // bind // ////////// @@ -1039,4 +1057,5 @@ void smlua_bind_functions(void) { 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, "cast_graph_node", smlua_func_cast_graph_node); + smlua_bind_function(L, "get_uncolored_string", smlua_func_get_uncolored_string); } diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 1db3a0ead..47b217a75 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -18,6 +18,7 @@ #include "pc/djui/djui_lua_profiler.h" #include "pc/djui/djui_panel.h" #include "pc/configfile.h" +#include "pc/utils/misc.h" #include "../mods/mods.h" #include "game/print.h" @@ -1737,21 +1738,6 @@ void smlua_display_chat_commands(void) { } } -char* remove_color_codes(const char* str) { - char* result = strdup(str); - char* startColor; - while ((startColor = strstr(result, "\\#"))) { - char* endColor = strstr(startColor + 2, "\\"); - if (endColor) { - memmove(startColor, endColor + 1, strlen(endColor + 1) + 1); - } else { - *startColor = '\0'; - break; - } - } - return result; -} - bool is_valid_subcommand(const char* start, const char* end) { for (const char* ptr = start; ptr < end; ptr++) { if (isspace(*ptr) || *ptr == '\0') { @@ -1851,7 +1837,7 @@ char** smlua_get_chat_subcommands_list(const char* maincommand) { for (s32 i = 0; i < sHookedChatCommandsCount; i++) { struct LuaHookedChatCommand* hook = &sHookedChatCommands[i]; if (strcmp(hook->command, maincommand) == 0) { - char* noColorsDesc = remove_color_codes(hook->description); + char* noColorsDesc = str_remove_color_codes(hook->description); char* startSubcommands = strstr(noColorsDesc, "["); char* endSubcommands = strstr(noColorsDesc, "]"); diff --git a/src/pc/mods/mod.c b/src/pc/mods/mod.c index 3b057f9a7..dbc97c93e 100644 --- a/src/pc/mods/mod.c +++ b/src/pc/mods/mod.c @@ -548,8 +548,12 @@ bool mod_load(struct Mods* mods, char* basePath, char* modName) { } // set category - if (mod->category == NULL && strstr(mod->name, "[CS] ")) { - mod->category = strdup("cs"); + if (mod->category == NULL) { + char *modNameNoColor = str_remove_color_codes(mod->name); + if (strstr(modNameNoColor, "[CS]") == modNameNoColor) { + mod->category = strdup("cs"); + } + free(modNameNoColor); } // print diff --git a/src/pc/mods/mods.c b/src/pc/mods/mods.c index 8907d9d2a..9af7ab2b3 100644 --- a/src/pc/mods/mods.c +++ b/src/pc/mods/mods.c @@ -7,6 +7,7 @@ #include "pc/loading.h" #include "pc/fs/fmem.h" #include "pc/pc_main.h" +#include "pc/utils/misc.h" #if defined(_WIN32) || defined(_WIN64) #include @@ -60,8 +61,9 @@ u16 mods_get_character_select_count(void) { for (u16 i = 0; i < gLocalMods.entryCount; i++) { struct Mod* mod = gLocalMods.entries[i]; - if (!mod->enabled || strcmp(mod->name, "[CS]")) { continue; } - enabled++; + if (mod->enabled && mod->category && strcmp(mod->category, "cs") == 0) { + enabled++; + } } return enabled; @@ -176,21 +178,6 @@ void mods_activate(struct Mods* mods) { mod_cache_save(); } -static char* mods_remove_color_codes(const char* str) { - char* result = strdup(str); - char* startColor; - while ((startColor = strstr(result, "\\#"))) { - char* endColor = strstr(startColor + 2, "\\"); - if (endColor) { - memmove(startColor, endColor + 1, strlen(endColor + 1) + 1); - } else { - *startColor = '\0'; - break; - } - } - return result; -} - static void mods_sort(struct Mods* mods) { if (mods->entryCount <= 1) { return; @@ -201,8 +188,8 @@ static void mods_sort(struct Mods* mods) { struct Mod* mod = mods->entries[i]; for (s32 j = 0; j < i; ++j) { struct Mod* mod2 = mods->entries[j]; - char* name = mods_remove_color_codes(mod->name); - char* name2 = mods_remove_color_codes(mod2->name); + char* name = str_remove_color_codes(mod->name); + char* name2 = str_remove_color_codes(mod2->name); if (strcmp(name, name2) < 0) { mods->entries[i] = mod2; mods->entries[j] = mod; diff --git a/src/pc/utils/misc.c b/src/pc/utils/misc.c index 906a1593a..d669e92d6 100644 --- a/src/pc/utils/misc.c +++ b/src/pc/utils/misc.c @@ -568,3 +568,18 @@ void str_seperator_concat(char *output_buffer, int buffer_size, char** strings, } } } + +char *str_remove_color_codes(const char *str) { + char *output = strdup(str); + char *startColor; + while ((startColor = strstr(output, "\\#"))) { + char *endColor = strchr(startColor + 2, '\\'); + if (endColor) { + memmove(startColor, endColor + 1, strlen(endColor + 1) + 1); + } else { + *startColor = 0; + break; + } + } + return output; +} diff --git a/src/pc/utils/misc.h b/src/pc/utils/misc.h index 165333576..066b1132b 100644 --- a/src/pc/utils/misc.h +++ b/src/pc/utils/misc.h @@ -25,5 +25,6 @@ void delta_interpolate_mtx(Mtx* out, Mtx* a, Mtx* b, f32 delta); void detect_and_skip_mtx_interpolation(Mtx** mtxPrev, Mtx** mtx); void str_seperator_concat(char *output_buffer, int buffer_size, char** strings, int num_strings, char* seperator); +char *str_remove_color_codes(const char *str); #endif \ No newline at end of file