From 07b0d52f908b818ec21c236ccc93991353ccfc68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Mon, 3 Jun 2019 19:27:06 +0200 Subject: [PATCH] Add more transition type checks and fix tpt.eltransition Though I'm quite sure nobody actually cares about that table. It's been broken for a very long time. The PROP tool now properly calls part_change_type when type is being set. --- src/gui/game/PropertyTool.cpp | 8 ++++++ src/gui/game/Tool.h | 1 + src/lua/LegacyLuaAPI.cpp | 49 ++++++++++++++++++++++++---------- src/lua/LuaScriptInterface.cpp | 4 +-- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/gui/game/PropertyTool.cpp b/src/gui/game/PropertyTool.cpp index 72b6b42462..c8b9ef2785 100644 --- a/src/gui/game/PropertyTool.cpp +++ b/src/gui/game/PropertyTool.cpp @@ -200,6 +200,7 @@ void PropertyWindow::SetProperty() } tool->propOffset = properties[property->GetOption().second].Offset; tool->propType = properties[property->GetOption().second].Type; + tool->changeType = properties[property->GetOption().second].Name == "type"; } catch (const std::exception& ex) { new ErrorMessage("Could not set property", "Invalid value provided"); return; @@ -245,6 +246,13 @@ void PropertyTool::SetProperty(Simulation *sim, ui::Point position) i = sim->photons[position.Y][position.X]; if(!i) return; + + if (changeType) + { + sim->part_change_type(ID(i), sim->parts[ID(i)].x+0.5f, sim->parts[ID(i)].y+0.5f, propValue.Integer); + return; + } + switch (propType) { case StructProperty::Float: diff --git a/src/gui/game/Tool.h b/src/gui/game/Tool.h index 771d294e74..a48ef0979d 100644 --- a/src/gui/game/Tool.h +++ b/src/gui/game/Tool.h @@ -87,6 +87,7 @@ class PropertyTool: public Tool } StructProperty::PropertyType propType; PropertyValue propValue; + bool changeType; size_t propOffset; void OpenWindow(Simulation *sim); diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index 26d6c68d2b..ed5b7ba077 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -26,6 +26,7 @@ #include "simulation/Simulation.h" #include "simulation/Gravity.h" #include "simulation/SimulationData.h" +#include "simulation/ElementCommon.h" #include "graphics/Graphics.h" #include "graphics/Renderer.h" @@ -165,11 +166,11 @@ int luacon_transitionread(lua_State* l) StructProperty prop = legacyTransitionNames[key]; //Get Raw Index value for element - lua_pushstring(l, "value"); + lua_pushstring(l, "id"); lua_rawget(l, 1); int i = lua_tointeger (l, lua_gettop(l)); lua_pop(l, 1); - if (i < 0 || i >= PT_NUM) + if (!luacon_sim->IsValidElement(i)) { return luaL_error(l, "Invalid index"); } @@ -192,11 +193,20 @@ int luacon_transitionwrite(lua_State* l) lua_rawget(l, 1); int i = lua_tointeger (l, lua_gettop(l)); lua_pop(l, 1); - if (i < 0 || i >= PT_NUM) + if (!luacon_sim->IsValidElement(i)) { return luaL_error(l, "Invalid index"); } + if (prop.Type == StructProperty::TransitionType) + { + int type = luaL_checkinteger(l, 3); + if (!luacon_sim->IsValidElement(type) && type != NT && type != ST) + { + return luaL_error(l, "Invalid element"); + } + } + intptr_t propertyAddress = (intptr_t)(((unsigned char*)&luacon_sim->elements[i]) + prop.Offset); LuaScriptInterface::LuaSetProperty(l, prop, propertyAddress, 3); @@ -215,7 +225,7 @@ int luacon_elementread(lua_State* l) lua_rawget(l, 1); int i = lua_tointeger (l, lua_gettop(l)); lua_pop(l, 1); - if (i < 0 || i >= PT_NUM) + if (!luacon_sim->IsValidElement(i)) { return luaL_error(l, "Invalid index"); } @@ -238,13 +248,20 @@ int luacon_elementwrite(lua_State* l) lua_rawget(l, 1); int i = lua_tointeger (l, lua_gettop(l)); lua_pop(l, 1); - if (i < 0 || i >= PT_NUM) + if (!luacon_sim->IsValidElement(i)) { return luaL_error(l, "Invalid index"); } - intptr_t propertyAddress = (intptr_t)(((unsigned char*)&luacon_sim->elements[i]) + prop.Offset); - LuaScriptInterface::LuaSetProperty(l, prop, propertyAddress, 3); + if (prop.Name == "type") // i.e. it's .type + { + luacon_sim->part_change_type(i, luacon_sim->parts[i].x+0.5f, luacon_sim->parts[i].y+0.5f, luaL_checkinteger(l, 3)); + } + else + { + intptr_t propertyAddress = (intptr_t)(((unsigned char*)&luacon_sim->elements[i]) + prop.Offset); + LuaScriptInterface::LuaSetProperty(l, prop, propertyAddress, 3); + } luacon_model->BuildMenus(); luacon_sim->init_can_move(); @@ -284,8 +301,10 @@ int luatpt_getelement(lua_State *l) if (lua_isnumber(l, 1)) { t = luaL_optint(l, 1, 1); - if (t<0 || t>=PT_NUM) + if (!luacon_sim->IsValidElement(t)) + { return luaL_error(l, "Unrecognised element number '%d'", t); + } lua_pushstring(l, luacon_sim->elements[t].Name.ToUtf8().c_str()); } else @@ -327,7 +346,7 @@ int luatpt_element_func(lua_State *l) { int element = luaL_optint(l, 2, 0); int replace = luaL_optint(l, 3, 0); - if(element > 0 && element < PT_NUM) + if (luacon_sim->IsValidElement(element)) { lua_el_func[element].Assign(1); if (replace == 2) @@ -346,7 +365,7 @@ int luatpt_element_func(lua_State *l) else if(lua_isnil(l, 1)) { int element = luaL_optint(l, 2, 0); - if(element > 0 && element < PT_NUM) + if (luacon_sim->IsValidElement(element)) { lua_el_func[element].Clear(); lua_el_mode[element] = 0; @@ -397,7 +416,7 @@ int luatpt_graphics_func(lua_State *l) if(lua_isfunction(l, 1)) { int element = luaL_optint(l, 2, 0); - if (element > 0 && element < PT_NUM) + if (luacon_sim->IsValidElement(element)) { lua_gr_func[element].Assign(1); luacon_ren->graphicscache[element].isready = 0; @@ -411,7 +430,7 @@ int luatpt_graphics_func(lua_State *l) else if (lua_isnil(l, 1)) { int element = luaL_optint(l, 2, 0); - if (element > 0 && element < PT_NUM) + if (luacon_sim->IsValidElement(element)) { lua_gr_func[element].Clear(); luacon_ren->graphicscache[element].isready = 0; @@ -470,8 +489,10 @@ int luatpt_create(lua_State* l) if(lua_isnumber(l, 3)) { t = luaL_optint(l, 3, 0); - if (t<0 || t >= PT_NUM || !luacon_sim->elements[t].Enabled) + if (!luacon_sim->IsValidElement(t)) + { return luaL_error(l, "Unrecognised element number '%d'", t); + } } else { const char* name = luaL_optstring(l, 3, "dust"); if ((t = luacon_sim->GetParticleType(ByteString(name))) == -1) @@ -698,7 +719,7 @@ int luatpt_set_property(lua_State* l) else t = luaL_optint(l, 2, 0); - if (!strcmp(prop, "type") && (t<0 || t>=PT_NUM || !luacon_sim->elements[t].Enabled)) + if (!strcmp(prop, "type") && !luacon_sim->IsValidElement(t)) return luaL_error(l, "Unrecognised element number '%d'", t); } else if (lua_isstring(l, 2)) diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index b05cf4f621..0abddb3996 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -321,7 +321,7 @@ tpt.partsdata = nil"); lua_newtable(l); currentElementMeta = lua_gettop(l); lua_pushinteger(l, i); - lua_setfield(l, currentElement, "value"); + lua_setfield(l, currentElement, "id"); lua_pushcfunction(l, luacon_transitionwrite); lua_setfield(l, currentElementMeta, "__newindex"); lua_pushcfunction(l, luacon_transitionread); @@ -2775,7 +2775,7 @@ int LuaScriptInterface::elements_property(lua_State * l) int type = luaL_checkinteger(l, 3); if (!luacon_sim->IsValidElement(type) && type != NT && type != ST) { - luaL_error(l, "Invalid element"); + return luaL_error(l, "Invalid element"); } }