diff --git a/src/lua/CommandInterface.cpp b/src/lua/CommandInterface.cpp index 07e2f9318..890c1b872 100644 --- a/src/lua/CommandInterface.cpp +++ b/src/lua/CommandInterface.cpp @@ -39,70 +39,37 @@ void CommandInterface::Log(LogType type, String message) int CommandInterface::GetPropertyOffset(ByteString key, FormatType & format) { int offset = -1; - if (!key.compare("type")) + for (auto &alias : Particle::GetPropertyAliases()) { - offset = offsetof(Particle, type); - format = FormatElement; + if (key == alias.from) + { + key = alias.to; + } } - else if (!key.compare("life")) + for (auto &prop : Particle::GetProperties()) { - offset = offsetof(Particle, life); - format = FormatInt; - } - else if (!key.compare("ctype")) - { - offset = offsetof(Particle, ctype); - format = FormatInt; - } - else if (!key.compare("temp")) - { - offset = offsetof(Particle, temp); - format = FormatFloat; - } - else if (!key.compare("tmp2")) - { - offset = offsetof(Particle, tmp2); - format = FormatInt; - } - else if (!key.compare("tmp")) - { - offset = offsetof(Particle, tmp); - format = FormatInt; - } - else if (!key.compare("vy")) - { - offset = offsetof(Particle, vy); - format = FormatFloat; - } - else if (!key.compare("vx")) - { - offset = offsetof(Particle, vx); - format = FormatFloat; - } - else if (!key.compare("x")) - { - offset = offsetof(Particle, x); - format = FormatFloat; - } - else if (!key.compare("y")) - { - offset = offsetof(Particle, y); - format = FormatFloat; - } - else if (!key.compare("dcolor") || !key.compare("dcolour")) - { - offset = offsetof(Particle, dcolour); - format = FormatInt; - } - else if (!key.compare("tmp3")) - { - offset = offsetof(Particle, tmp3); - format = FormatInt; - } - else if (!key.compare("tmp4")) - { - offset = offsetof(Particle, tmp4); - format = FormatInt; + if (key == prop.Name) + { + offset = prop.Offset; + switch (prop.Type) + { + case StructProperty::ParticleType: + format = (key == "type") ? FormatElement : FormatInt; // FormatElement is tightly coupled with "type" + break; + + case StructProperty::Integer: + case StructProperty::UInteger: + format = FormatInt; + break; + + case StructProperty::Float: + format = FormatFloat; + break; + + default: + break; + } + } } return offset; } diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 820084775..74c15e1e8 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -916,6 +916,11 @@ void LuaScriptInterface::initSimulationAPI() lua_pushinteger(l, particlePropertiesCount++); lua_setfield(l, -2, ("FIELD_" + prop.Name.ToUpper()).c_str()); } + for (auto &alias : Particle::GetPropertyAliases()) + { + lua_getfield(l, -1, ("FIELD_" + alias.to.ToUpper()).c_str()); + lua_setfield(l, -2, ("FIELD_" + alias.from.ToUpper()).c_str()); + } } lua_newtable(l); @@ -1129,6 +1134,13 @@ int LuaScriptInterface::simulation_partProperty(lua_State * l) else if (lua_type(l, 2) == LUA_TSTRING) { ByteString fieldName = lua_tostring(l, 2); + for (auto &alias : Particle::GetPropertyAliases()) + { + if (fieldName == alias.from) + { + fieldName = alias.to; + } + } prop = std::find_if(properties.begin(), properties.end(), [&fieldName](StructProperty const &p) { return p.Name == fieldName; }); @@ -3259,19 +3271,7 @@ int LuaScriptInterface::elements_element(lua_State * l) lua_pop(l, 1); lua_getfield(l, -1, "DefaultProperties"); - if (lua_type(l, -1) == LUA_TTABLE) - { - for (auto &prop : Particle::GetProperties()) - { - lua_getfield(l, -1, prop.Name.c_str()); - if (lua_type(l, -1) != LUA_TNIL) - { - auto propertyAddress = reinterpret_cast((reinterpret_cast(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset); - LuaSetProperty(l, prop, propertyAddress, -1); - } - lua_pop(l, 1); - } - } + SetDefaultProperties(l, id, -1); lua_pop(l, 1); luacon_model->BuildMenus(); @@ -3295,20 +3295,57 @@ int LuaScriptInterface::elements_element(lua_State * l) lua_pushstring(l, luacon_sim->elements[id].Identifier.c_str()); lua_setfield(l, -2, "Identifier"); - lua_newtable(l); - int tableIdx = lua_gettop(l); - for (auto &prop : Particle::GetProperties()) - { - auto propertyAddress = reinterpret_cast((reinterpret_cast(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset); - LuaGetProperty(l, prop, propertyAddress); - lua_setfield(l, tableIdx, prop.Name.c_str()); - } + GetDefaultProperties(l, id); lua_setfield(l, -2, "DefaultProperties"); return 1; } } +void LuaScriptInterface::GetDefaultProperties(lua_State * l, int id) +{ + lua_newtable(l); + for (auto &prop : Particle::GetProperties()) + { + auto propertyAddress = reinterpret_cast((reinterpret_cast(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset); + LuaGetProperty(l, prop, propertyAddress); + lua_setfield(l, -2, prop.Name.c_str()); + } + for (auto &alias : Particle::GetPropertyAliases()) + { + lua_getfield(l, -1, alias.to.c_str()); + lua_setfield(l, -2, alias.from.c_str()); + } +} + +void LuaScriptInterface::SetDefaultProperties(lua_State * l, int id, int stackPos) +{ + if (lua_type(l, stackPos) == LUA_TTABLE) + { + for (auto &prop : Particle::GetProperties()) + { + lua_getfield(l, stackPos, prop.Name.c_str()); + if (lua_type(l, -1) == LUA_TNIL) + { + for (auto &alias : Particle::GetPropertyAliases()) + { + if (alias.to == prop.Name) + { + lua_pop(l, 1); + lua_getfield(l, stackPos, alias.from.c_str()); + } + } + } + if (lua_type(l, -1) != LUA_TNIL) + { + auto propertyAddress = reinterpret_cast((reinterpret_cast(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset); + LuaSetProperty(l, prop, propertyAddress, -1); + } + lua_pop(l, 1); + } + } +} + int LuaScriptInterface::elements_property(lua_State * l) { int id = luaL_checkinteger(l, 1); @@ -3444,17 +3481,7 @@ int LuaScriptInterface::elements_property(lua_State * l) } else if (propertyName == "DefaultProperties") { - luaL_checktype(l, 3, LUA_TTABLE); - for (auto &prop : Particle::GetProperties()) - { - lua_getfield(l, -1, prop.Name.c_str()); - if (lua_type(l, -1) != LUA_TNIL) - { - auto propertyAddress = reinterpret_cast((reinterpret_cast(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset); - LuaSetProperty(l, prop, propertyAddress, -1); - } - lua_pop(l, 1); - } + SetDefaultProperties(l, id, 3); } else { @@ -3477,14 +3504,7 @@ int LuaScriptInterface::elements_property(lua_State * l) } else if (propertyName == "DefaultProperties") { - lua_newtable(l); - int tableIdx = lua_gettop(l); - for (auto &prop : Particle::GetProperties()) - { - auto propertyAddress = reinterpret_cast((reinterpret_cast(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset); - LuaGetProperty(l, prop, propertyAddress); - lua_setfield(l, tableIdx, prop.Name.c_str()); - } + GetDefaultProperties(l, id); return 1; } else diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index 733196d00..1b21dfbd5 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -144,6 +144,9 @@ class LuaScriptInterface: public CommandInterface static int elements_free(lua_State * l); static int elements_exists(lua_State * l); + static void GetDefaultProperties(lua_State * l, int id); + static void SetDefaultProperties(lua_State * l, int id, int stackPos); + //Interface void initInterfaceAPI(); static int interface_showWindow(lua_State * l); diff --git a/src/simulation/Particle.cpp b/src/simulation/Particle.cpp index 987cfecf0..c9bc46ee8 100644 --- a/src/simulation/Particle.cpp +++ b/src/simulation/Particle.cpp @@ -21,3 +21,13 @@ std::vector const &Particle::GetProperties() }; return properties; } + +std::vector const &Particle::GetPropertyAliases() +{ + static std::vector aliases = { + { "pavg0" , "tmp3" }, + { "pavg1" , "tmp4" }, + { "dcolor", "dcolour" }, + }; + return aliases; +} diff --git a/src/simulation/Particle.h b/src/simulation/Particle.h index 0c1ace9e0..8a327838c 100644 --- a/src/simulation/Particle.h +++ b/src/simulation/Particle.h @@ -20,6 +20,7 @@ struct Particle /** Returns a list of properties, their type and offset within the structure that can be changed by higher-level processes referring to them by name such as Lua or the property tool **/ static std::vector const &GetProperties(); + static std::vector const &GetPropertyAliases(); }; #endif diff --git a/src/simulation/StructProperty.h b/src/simulation/StructProperty.h index 4659ca782..a2c5bc8ad 100644 --- a/src/simulation/StructProperty.h +++ b/src/simulation/StructProperty.h @@ -46,4 +46,9 @@ union PropertyValue { float Float; }; +struct StructPropertyAlias +{ + ByteString from, to; +}; + #endif