Fix cryptic level 0 Lua error built-in elements sometimes produce

If the built-in update function is allowed to run, it can change the particle's type. The code path assumes that there is a Lua update function to call on the particle, but this type change may break this assumption and cause the code to call the update function of an element that doesn't even have one, producing a weird error message with no line number.
This commit is contained in:
Tamás Bálint Misius
2022-03-31 16:46:24 +02:00
parent b2ddb39b42
commit 254b4a642a

View File

@@ -2948,8 +2948,6 @@ int LuaScriptInterface::elements_allocate(lua_State * l)
}
static int luaUpdateWrapper(UPDATE_FUNC_ARGS)
{
if (lua_el_func[parts[i].type])
{
auto *builtinUpdate = GetElements()[parts[i].type].Update;
if (builtinUpdate && lua_el_mode[parts[i].type] == 1)
@@ -2959,6 +2957,8 @@ static int luaUpdateWrapper(UPDATE_FUNC_ARGS)
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
}
if (lua_el_func[parts[i].type])
{
int retval = 0, callret;
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, lua_el_func[parts[i].type]);
lua_pushinteger(luacon_ci->l, i);
@@ -2979,6 +2979,7 @@ static int luaUpdateWrapper(UPDATE_FUNC_ARGS)
}
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
}
if (builtinUpdate && lua_el_mode[parts[i].type] == 3)
{
if (builtinUpdate(UPDATE_FUNC_SUBCALL_ARGS))
@@ -2986,7 +2987,6 @@ static int luaUpdateWrapper(UPDATE_FUNC_ARGS)
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
}
}
return 0;
}