From 4159b7c5f5c76cb0ee4779a1557a39597d8db612 Mon Sep 17 00:00:00 2001 From: MysterD Date: Mon, 18 Apr 2022 23:02:40 -0700 Subject: [PATCH] Allow custom levels to use any active mod's variables --- src/engine/level_script.c | 17 ++++++++++++++++- src/pc/lua/smlua_utils.c | 23 +++++++++++++++++++++++ src/pc/lua/smlua_utils.h | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/engine/level_script.c b/src/engine/level_script.c index 2f0d96d8..45412c8f 100644 --- a/src/engine/level_script.c +++ b/src/engine/level_script.c @@ -861,8 +861,13 @@ static void level_cmd_place_object_ext(void) { gSmLuaConvertSuccess = true; enum BehaviorId behId = smlua_get_mod_variable(modIndex, behStr); + if (!gSmLuaConvertSuccess) { + gSmLuaConvertSuccess = true; + behId = smlua_get_any_mod_variable(behStr); + } + if ((gLevelScriptModIndex == -1) || !gSmLuaConvertSuccess) { - LOG_ERROR("Failed to place custom object: %u", behId); + LOG_ERROR("Failed to place custom object: %u :: %s", behId, behStr); sCurrentCmd = CMD_NEXT; return; } @@ -904,7 +909,17 @@ static void level_cmd_place_object_ext2(void) { gSmLuaConvertSuccess = true; enum ModelExtendedId modelId = smlua_get_mod_variable(modIndex, modelStr); + if (!gSmLuaConvertSuccess) { + gSmLuaConvertSuccess = true; + modelId = smlua_get_any_mod_variable(modelStr); + } + + gSmLuaConvertSuccess = true; enum BehaviorId behId = smlua_get_mod_variable(modIndex, behStr); + if (!gSmLuaConvertSuccess) { + gSmLuaConvertSuccess = true; + behId = smlua_get_any_mod_variable(behStr); + } if ((gLevelScriptModIndex == -1) || !gSmLuaConvertSuccess) { LOG_ERROR("Failed to place custom object: %u, %u", modelId, behId); diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index 0eaa970f..97cd4ef1 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -395,6 +395,29 @@ s64 smlua_get_mod_variable(u16 modIndex, const char* variable) { return value; } +s64 smlua_get_any_mod_variable(const char* variable) { + lua_State* L = gLuaState; + + s64 value = 0; + for (s32 i = 0; i < gActiveMods.entryCount; i++) { + // figure out entry + struct Mod* mod = gActiveMods.entries[i]; + + int prevTop = lua_gettop(L); + lua_getglobal(L, "_G"); // get global table + lua_getfield(L, LUA_REGISTRYINDEX, mod->relativePath); // get the file's "global" table + value = smlua_get_integer_field(-1, (char*)variable); + lua_settop(L, prevTop); + + if (gSmLuaConvertSuccess) { + return value; + } + } + + // return variable + return value; +} + /////////////////////////////////////////////////////////////////////////////////////////// char* smlua_lnt_to_str(struct LSTNetworkType* lnt) { diff --git a/src/pc/lua/smlua_utils.h b/src/pc/lua/smlua_utils.h index 80259a28..136fe5c9 100644 --- a/src/pc/lua/smlua_utils.h +++ b/src/pc/lua/smlua_utils.h @@ -35,6 +35,7 @@ lua_Number smlua_get_number_field(int index, char* name); char* smlua_lnt_to_str(struct LSTNetworkType* lnt); s64 smlua_get_mod_variable(u16 modIndex, const char* variable) ; +s64 smlua_get_any_mod_variable(const char* variable) ; void smlua_logline(void); void smlua_dump_stack(void);