Allow custom levels to use any active mod's variables

This commit is contained in:
MysterD 2022-04-18 23:02:40 -07:00
parent 456ec2a2cc
commit 97f1182498
3 changed files with 40 additions and 1 deletions

View file

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

View file

@ -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) {

View file

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