Skip to content

Commit 131b965

Browse files
committed
Add Lua apis to interact with DefaultProperties, Create, CreateAllowed, and ChangeType
1 parent bf7d182 commit 131b965

File tree

1 file changed

+207
-13
lines changed

1 file changed

+207
-13
lines changed

src/lua/LuaScriptInterface.cpp

Lines changed: 207 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ String *luacon_lastError;
7575
String lastCode;
7676

7777
int *lua_el_mode;
78-
LuaSmartRef *lua_el_func, *lua_gr_func, *lua_cd_func;
78+
LuaSmartRef *lua_el_func, *lua_gr_func;
79+
std::vector<LuaSmartRef> luaCtypeDrawHandlers, luaCreateHandlers, luaCreateAllowedHandlers, luaChangeTypeHandlers;
7980

8081
int getPartIndex_curIdx;
8182
int tptProperties; //Table for some TPT properties
@@ -333,15 +334,18 @@ tpt.partsdata = nil");
333334
}
334335
lua_setfield(l, tptProperties, "eltransition");
335336

336-
lua_cd_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
337-
lua_cd_func = &lua_cd_func_v[0];
338337
lua_gr_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
339338
lua_gr_func = &lua_gr_func_v[0];
340339
lua_el_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
341340
lua_el_func = &lua_el_func_v[0];
342341
lua_el_mode_v = std::vector<int>(PT_NUM, 0);
343342
lua_el_mode = &lua_el_mode_v[0];
344343

344+
luaCtypeDrawHandlers = std::vector<LuaSmartRef>(PT_NUM, l);
345+
luaCreateHandlers = std::vector<LuaSmartRef>(PT_NUM, l);
346+
luaCreateAllowedHandlers = std::vector<LuaSmartRef>(PT_NUM, l);
347+
luaChangeTypeHandlers = std::vector<LuaSmartRef>(PT_NUM, l);
348+
345349
//make tpt.* a metatable
346350
lua_newtable(l);
347351
lua_pushlightuserdata(l, this);
@@ -2685,12 +2689,73 @@ int LuaScriptInterface::elements_allocate(lua_State * l)
26852689
return 1;
26862690
}
26872691

2692+
void luaCreateWrapper(ELEMENT_CREATE_FUNC_ARGS)
2693+
{
2694+
if (luaCreateHandlers[sim->parts[i].type])
2695+
{
2696+
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, luaCreateHandlers[sim->parts[i].type]);
2697+
lua_pushinteger(luacon_ci->l, i);
2698+
lua_pushinteger(luacon_ci->l, x);
2699+
lua_pushinteger(luacon_ci->l, y);
2700+
lua_pushinteger(luacon_ci->l, t);
2701+
lua_pushinteger(luacon_ci->l, v);
2702+
if (lua_pcall(luacon_ci->l, 5, 0, 0))
2703+
{
2704+
luacon_ci->Log(CommandInterface::LogError, "In create func: " + luacon_geterror());
2705+
lua_pop(luacon_ci->l, 1);
2706+
}
2707+
}
2708+
}
2709+
2710+
bool luaCreateAllowedWrapper(ELEMENT_CREATE_ALLOWED_FUNC_ARGS)
2711+
{
2712+
bool ret = false;
2713+
if (luaCreateAllowedHandlers[t])
2714+
{
2715+
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, luaCreateAllowedHandlers[t]);
2716+
lua_pushinteger(luacon_ci->l, i);
2717+
lua_pushinteger(luacon_ci->l, x);
2718+
lua_pushinteger(luacon_ci->l, y);
2719+
lua_pushinteger(luacon_ci->l, t);
2720+
if (lua_pcall(luacon_ci->l, 4, 1, 0))
2721+
{
2722+
luacon_ci->Log(CommandInterface::LogError, "In create allowed: " + luacon_geterror());
2723+
lua_pop(luacon_ci->l, 1);
2724+
}
2725+
else
2726+
{
2727+
if (lua_isboolean(luacon_ci->l, -1))
2728+
ret = lua_toboolean(luacon_ci->l, -1);
2729+
lua_pop(luacon_ci->l, 1);
2730+
}
2731+
}
2732+
return ret;
2733+
}
2734+
2735+
void luaChangeTypeWrapper(ELEMENT_CHANGETYPE_FUNC_ARGS)
2736+
{
2737+
if (luaChangeTypeHandlers[sim->parts[i].type])
2738+
{
2739+
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, luaChangeTypeHandlers[sim->parts[i].type]);
2740+
lua_pushinteger(luacon_ci->l, i);
2741+
lua_pushinteger(luacon_ci->l, x);
2742+
lua_pushinteger(luacon_ci->l, y);
2743+
lua_pushinteger(luacon_ci->l, from);
2744+
lua_pushinteger(luacon_ci->l, to);
2745+
if (lua_pcall(luacon_ci->l, 5, 0, 0))
2746+
{
2747+
luacon_ci->Log(CommandInterface::LogError, "In change type: " + luacon_geterror());
2748+
lua_pop(luacon_ci->l, 1);
2749+
}
2750+
}
2751+
}
2752+
26882753
static bool luaCtypeDrawWrapper(CTYPEDRAW_FUNC_ARGS)
26892754
{
26902755
bool ret = false;
2691-
if (lua_cd_func[sim->parts[i].type])
2756+
if (luaCtypeDrawHandlers[sim->parts[i].type])
26922757
{
2693-
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, lua_cd_func[sim->parts[i].type]);
2758+
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, luaCtypeDrawHandlers[sim->parts[i].type]);
26942759
lua_pushinteger(luacon_ci->l, i);
26952760
lua_pushinteger(luacon_ci->l, t);
26962761
lua_pushinteger(luacon_ci->l, v);
@@ -2758,19 +2823,74 @@ int LuaScriptInterface::elements_element(lua_State * l)
27582823
}
27592824
lua_pop(l, 1);
27602825

2826+
lua_getfield(l, -1, "Create");
2827+
if (lua_type(l, -1) == LUA_TFUNCTION)
2828+
{
2829+
luaCreateHandlers[id].Assign(-1);
2830+
luacon_sim->elements[id].Create = luaCreateWrapper;
2831+
}
2832+
else if (lua_type(l, -1) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
2833+
{
2834+
luaCreateHandlers[id].Clear();
2835+
luacon_sim->elements[id].Create = nullptr;
2836+
}
2837+
lua_pop(l, 1);
2838+
2839+
lua_getfield(l, -1, "CreateAllowed");
2840+
if (lua_type(l, -1) == LUA_TFUNCTION)
2841+
{
2842+
luaCreateAllowedHandlers[id].Assign(-1);
2843+
luacon_sim->elements[id].CreateAllowed = luaCreateAllowedWrapper;
2844+
}
2845+
else if (lua_type(l, -1) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
2846+
{
2847+
luaCreateAllowedHandlers[id].Clear();
2848+
luacon_sim->elements[id].CreateAllowed = nullptr;
2849+
}
2850+
lua_pop(l, 1);
2851+
2852+
lua_getfield(l, -1, "ChangeType");
2853+
if (lua_type(l, -1) == LUA_TFUNCTION)
2854+
{
2855+
luaChangeTypeHandlers[id].Assign(-1);
2856+
luacon_sim->elements[id].ChangeType = luaChangeTypeWrapper;
2857+
}
2858+
else if (lua_type(l, -1) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
2859+
{
2860+
luaChangeTypeHandlers[id].Clear();
2861+
luacon_sim->elements[id].ChangeType = nullptr;
2862+
}
2863+
lua_pop(l, 1);
2864+
27612865
lua_getfield(l, -1, "CtypeDraw");
27622866
if (lua_type(l, -1) == LUA_TFUNCTION)
27632867
{
2764-
lua_cd_func[id].Assign(-1);
2868+
luaCtypeDrawHandlers[id].Assign(-1);
27652869
luacon_sim->elements[id].CtypeDraw = luaCtypeDrawWrapper;
27662870
}
27672871
else if (lua_type(l, -1) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
27682872
{
2769-
lua_cd_func[id].Clear();
2873+
luaCtypeDrawHandlers[id].Clear();
27702874
luacon_sim->elements[id].CtypeDraw = nullptr;
27712875
}
27722876
lua_pop(l, 1);
27732877

2878+
lua_getfield(l, -1, "DefaultProperties");
2879+
if (lua_type(l, -1) == LUA_TTABLE)
2880+
{
2881+
for (auto &prop : Particle::GetProperties())
2882+
{
2883+
lua_getfield(l, -1, prop.Name.c_str());
2884+
if (lua_type(l, -1) != LUA_TNIL)
2885+
{
2886+
auto propertyAddress = reinterpret_cast<intptr_t>((reinterpret_cast<unsigned char*>(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset);
2887+
LuaSetProperty(l, prop, propertyAddress, -1);
2888+
}
2889+
lua_pop(l, 1);
2890+
}
2891+
}
2892+
lua_pop(l, 1);
2893+
27742894
luacon_model->BuildMenus();
27752895
luacon_sim->init_can_move();
27762896
luacon_ren->graphicscache[id].isready = 0;
@@ -2787,8 +2907,20 @@ int LuaScriptInterface::elements_element(lua_State * l)
27872907
LuaGetProperty(l, prop, propertyAddress);
27882908
lua_setfield(l, -2, prop.Name.c_str());
27892909
}
2910+
27902911
lua_pushstring(l, luacon_sim->elements[id].Identifier.c_str());
27912912
lua_setfield(l, -2, "Identifier");
2913+
2914+
lua_newtable(l);
2915+
int tableIdx = lua_gettop(l);
2916+
for (auto &prop : Particle::GetProperties())
2917+
{
2918+
auto propertyAddress = reinterpret_cast<intptr_t>((reinterpret_cast<unsigned char*>(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset);
2919+
LuaGetProperty(l, prop, propertyAddress);
2920+
lua_setfield(l, tableIdx, prop.Name.c_str());
2921+
}
2922+
lua_setfield(l, -2, "DefaultProperties");
2923+
27922924
return 1;
27932925
}
27942926
}
@@ -2829,7 +2961,6 @@ int LuaScriptInterface::elements_property(lua_State * l)
28292961
luacon_model->BuildMenus();
28302962
luacon_sim->init_can_move();
28312963
luacon_ren->graphicscache[id].isready = 0;
2832-
return 0;
28332964
}
28342965
else if (propertyName == "Update")
28352966
{
@@ -2857,7 +2988,6 @@ int LuaScriptInterface::elements_property(lua_State * l)
28572988
lua_el_mode[id] = 0;
28582989
luacon_sim->elements[id].Update = NULL;
28592990
}
2860-
return 0;
28612991
}
28622992
else if (propertyName == "Graphics")
28632993
{
@@ -2871,26 +3001,78 @@ int LuaScriptInterface::elements_property(lua_State * l)
28713001
luacon_sim->elements[id].Graphics = NULL;
28723002
}
28733003
luacon_ren->graphicscache[id].isready = 0;
2874-
return 0;
3004+
}
3005+
else if (propertyName == "Create")
3006+
{
3007+
if (lua_type(l, 3) == LUA_TFUNCTION)
3008+
{
3009+
luaCreateHandlers[id].Assign(3);
3010+
luacon_sim->elements[id].Create = luaCreateWrapper;
3011+
}
3012+
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3))
3013+
{
3014+
luaCreateHandlers[id].Clear();
3015+
luacon_sim->elements[id].Create = nullptr;
3016+
}
3017+
}
3018+
else if (propertyName == "CreateAllowed")
3019+
{
3020+
if (lua_type(l, 3) == LUA_TFUNCTION)
3021+
{
3022+
luaCreateAllowedHandlers[id].Assign(3);
3023+
luacon_sim->elements[id].CreateAllowed = luaCreateAllowedWrapper;
3024+
}
3025+
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3))
3026+
{
3027+
luaCreateAllowedHandlers[id].Clear();
3028+
luacon_sim->elements[id].CreateAllowed = nullptr;
3029+
}
3030+
}
3031+
else if (propertyName == "ChangeType")
3032+
{
3033+
if (lua_type(l, 3) == LUA_TFUNCTION)
3034+
{
3035+
luaChangeTypeHandlers[id].Assign(3);
3036+
luacon_sim->elements[id].ChangeType = luaChangeTypeWrapper;
3037+
}
3038+
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3))
3039+
{
3040+
luaChangeTypeHandlers[id].Clear();
3041+
luacon_sim->elements[id].ChangeType = nullptr;
3042+
}
28753043
}
28763044
else if (propertyName == "CtypeDraw")
28773045
{
28783046
if (lua_type(l, 3) == LUA_TFUNCTION)
28793047
{
2880-
lua_cd_func[id].Assign(3);
3048+
luaCtypeDrawHandlers[id].Assign(3);
28813049
luacon_sim->elements[id].CtypeDraw = luaCtypeDrawWrapper;
28823050
}
28833051
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3))
28843052
{
2885-
lua_cd_func[id].Clear();
3053+
luaCtypeDrawHandlers[id].Clear();
28863054
luacon_sim->elements[id].CtypeDraw = nullptr;
28873055
}
2888-
return 0;
3056+
}
3057+
else if (propertyName == "DefaultProperties")
3058+
{
3059+
luaL_checktype(l, 3, LUA_TTABLE);
3060+
for (auto &prop : Particle::GetProperties())
3061+
{
3062+
lua_getfield(l, -1, prop.Name.c_str());
3063+
if (lua_type(l, -1) != LUA_TNIL)
3064+
{
3065+
auto propertyAddress = reinterpret_cast<intptr_t>((reinterpret_cast<unsigned char*>(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset);
3066+
LuaSetProperty(l, prop, propertyAddress, -1);
3067+
}
3068+
lua_pop(l, 1);
3069+
}
28893070
}
28903071
else
28913072
{
28923073
return luaL_error(l, "Invalid element property");
28933074
}
3075+
return 0;
28943076
}
28953077
else
28963078
{
@@ -2905,6 +3087,18 @@ int LuaScriptInterface::elements_property(lua_State * l)
29053087
lua_pushstring(l, luacon_sim->elements[id].Identifier.c_str());
29063088
return 1;
29073089
}
3090+
else if (propertyName == "DefaultProperties")
3091+
{
3092+
lua_newtable(l);
3093+
int tableIdx = lua_gettop(l);
3094+
for (auto &prop : Particle::GetProperties())
3095+
{
3096+
auto propertyAddress = reinterpret_cast<intptr_t>((reinterpret_cast<unsigned char*>(&luacon_sim->elements[id].DefaultProperties)) + prop.Offset);
3097+
LuaGetProperty(l, prop, propertyAddress);
3098+
lua_setfield(l, tableIdx, prop.Name.c_str());
3099+
}
3100+
return 1;
3101+
}
29083102
else
29093103
{
29103104
return luaL_error(l, "Invalid element property");

0 commit comments

Comments
 (0)