Alias dcolor, pavg0, pavg1 to dcolour, tmp3, tmp4

This commit is contained in:
Tamás Bálint Misius 2022-08-08 08:55:32 +02:00
parent 140531aa88
commit 059697aba0
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
6 changed files with 107 additions and 101 deletions

View File

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

View File

@ -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<intptr_t>((reinterpret_cast<unsigned char*>(&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<intptr_t>((reinterpret_cast<unsigned char*>(&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<intptr_t>((reinterpret_cast<unsigned char*>(&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<intptr_t>((reinterpret_cast<unsigned char*>(&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<intptr_t>((reinterpret_cast<unsigned char*>(&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<intptr_t>((reinterpret_cast<unsigned char*>(&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

View File

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

View File

@ -21,3 +21,13 @@ std::vector<StructProperty> const &Particle::GetProperties()
};
return properties;
}
std::vector<StructPropertyAlias> const &Particle::GetPropertyAliases()
{
static std::vector<StructPropertyAlias> aliases = {
{ "pavg0" , "tmp3" },
{ "pavg1" , "tmp4" },
{ "dcolor", "dcolour" },
};
return aliases;
}

View File

@ -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<StructProperty> const &GetProperties();
static std::vector<StructPropertyAlias> const &GetPropertyAliases();
};
#endif

View File

@ -46,4 +46,9 @@ union PropertyValue {
float Float;
};
struct StructPropertyAlias
{
ByteString from, to;
};
#endif