Skip to content

Commit

Permalink
Add more transition type checks and fix tpt.eltransition
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LBPHacker committed Jun 3, 2019
1 parent 97bde11 commit 07b0d52
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/gui/game/PropertyTool.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/gui/game/Tool.h
Expand Up @@ -87,6 +87,7 @@ class PropertyTool: public Tool
}
StructProperty::PropertyType propType;
PropertyValue propValue;
bool changeType;
size_t propOffset;

void OpenWindow(Simulation *sim);
Expand Down
49 changes: 35 additions & 14 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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");
}
Expand All @@ -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);

Expand All @@ -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");
}
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 07b0d52

Please sign in to comment.