Lua get_uncolored_string + bug fixes (#620)

This commit is contained in:
PeachyPeach 2025-01-09 05:59:16 +01:00 committed by GitHub
parent d1d36a1fa2
commit 08a2490954
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 49 additions and 37 deletions

View file

@ -11,6 +11,7 @@
#include "engine/math_util.h" #include "engine/math_util.h"
#include "engine/level_script.h" #include "engine/level_script.h"
#include "pc/djui/djui_hud_utils.h" #include "pc/djui/djui_hud_utils.h"
#include "pc/utils/misc.h"
#include "include/level_misc_macros.h" #include "include/level_misc_macros.h"
#include "include/macro_presets.h" #include "include/macro_presets.h"
#include "utils/smlua_anim_utils.h" #include "utils/smlua_anim_utils.h"
@ -1009,6 +1010,23 @@ int smlua_func_cast_graph_node(lua_State* L) {
return 1; 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 // // 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, "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, "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, "cast_graph_node", smlua_func_cast_graph_node);
smlua_bind_function(L, "get_uncolored_string", smlua_func_get_uncolored_string);
} }

View file

@ -18,6 +18,7 @@
#include "pc/djui/djui_lua_profiler.h" #include "pc/djui/djui_lua_profiler.h"
#include "pc/djui/djui_panel.h" #include "pc/djui/djui_panel.h"
#include "pc/configfile.h" #include "pc/configfile.h"
#include "pc/utils/misc.h"
#include "../mods/mods.h" #include "../mods/mods.h"
#include "game/print.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) { bool is_valid_subcommand(const char* start, const char* end) {
for (const char* ptr = start; ptr < end; ptr++) { for (const char* ptr = start; ptr < end; ptr++) {
if (isspace(*ptr) || *ptr == '\0') { 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++) { for (s32 i = 0; i < sHookedChatCommandsCount; i++) {
struct LuaHookedChatCommand* hook = &sHookedChatCommands[i]; struct LuaHookedChatCommand* hook = &sHookedChatCommands[i];
if (strcmp(hook->command, maincommand) == 0) { 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* startSubcommands = strstr(noColorsDesc, "[");
char* endSubcommands = strstr(noColorsDesc, "]"); char* endSubcommands = strstr(noColorsDesc, "]");

View file

@ -548,9 +548,13 @@ bool mod_load(struct Mods* mods, char* basePath, char* modName) {
} }
// set category // set category
if (mod->category == NULL && strstr(mod->name, "[CS] ")) { if (mod->category == NULL) {
char *modNameNoColor = str_remove_color_codes(mod->name);
if (strstr(modNameNoColor, "[CS]") == modNameNoColor) {
mod->category = strdup("cs"); mod->category = strdup("cs");
} }
free(modNameNoColor);
}
// print // print
// LOG_INFO(" %s", mod->name); // LOG_INFO(" %s", mod->name);

View file

@ -7,6 +7,7 @@
#include "pc/loading.h" #include "pc/loading.h"
#include "pc/fs/fmem.h" #include "pc/fs/fmem.h"
#include "pc/pc_main.h" #include "pc/pc_main.h"
#include "pc/utils/misc.h"
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#include <windows.h> #include <windows.h>
@ -60,9 +61,10 @@ u16 mods_get_character_select_count(void) {
for (u16 i = 0; i < gLocalMods.entryCount; i++) { for (u16 i = 0; i < gLocalMods.entryCount; i++) {
struct Mod* mod = gLocalMods.entries[i]; struct Mod* mod = gLocalMods.entries[i];
if (!mod->enabled || strcmp(mod->name, "[CS]")) { continue; } if (mod->enabled && mod->category && strcmp(mod->category, "cs") == 0) {
enabled++; enabled++;
} }
}
return enabled; return enabled;
} }
@ -176,21 +178,6 @@ void mods_activate(struct Mods* mods) {
mod_cache_save(); 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) { static void mods_sort(struct Mods* mods) {
if (mods->entryCount <= 1) { if (mods->entryCount <= 1) {
return; return;
@ -201,8 +188,8 @@ static void mods_sort(struct Mods* mods) {
struct Mod* mod = mods->entries[i]; struct Mod* mod = mods->entries[i];
for (s32 j = 0; j < i; ++j) { for (s32 j = 0; j < i; ++j) {
struct Mod* mod2 = mods->entries[j]; struct Mod* mod2 = mods->entries[j];
char* name = mods_remove_color_codes(mod->name); char* name = str_remove_color_codes(mod->name);
char* name2 = mods_remove_color_codes(mod2->name); char* name2 = str_remove_color_codes(mod2->name);
if (strcmp(name, name2) < 0) { if (strcmp(name, name2) < 0) {
mods->entries[i] = mod2; mods->entries[i] = mod2;
mods->entries[j] = mod; mods->entries[j] = mod;

View file

@ -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;
}

View file

@ -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 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); 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 #endif