From 23268bf3e53abfe0667465631a4027d1f56cd49d Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Fri, 27 Oct 2023 16:29:47 +0200 Subject: [PATCH 1/8] Move input-related Lua variables into the library --- src/lua_inputlib.c | 60 ++++++++++++++++++++++++++++++++++++---------- src/lua_script.c | 2 ++ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/lua_inputlib.c b/src/lua_inputlib.c index 1f75ee6fe..0602b3db6 100644 --- a/src/lua_inputlib.c +++ b/src/lua_inputlib.c @@ -145,6 +145,30 @@ static luaL_Reg lib[] = { {NULL, NULL} }; +/////////////// +// VARIABLES // +/////////////// + +static int lib_get(lua_State *L) +{ + const char *field = luaL_checkstring(L, 2); + + if (fastcmp(field, "mouse")) + { + LUA_PushUserdata(L, &mouse, META_MOUSE); + return 1; + } + else if (fastcmp(field, "mouse2")) + { + LUA_PushUserdata(L, &mouse2, META_MOUSE); + return 1; + } + else + { + return 0; + } +} + /////////////////// // gamekeydown[] // /////////////////// @@ -239,19 +263,6 @@ static int mouse_num(lua_State *L) int LUA_InputLib(lua_State *L) { - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getGameKeyDown); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setGameKeyDown); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_lenGameKeyDown); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "gamekeydown"); - luaL_newmetatable(L, META_KEYEVENT); lua_pushcfunction(L, keyevent_get); lua_setfield(L, -2, "__index"); @@ -265,6 +276,29 @@ int LUA_InputLib(lua_State *L) lua_setfield(L, -2, "__len"); lua_pop(L, 1); + // Register the library, then add __index and __newindex + // metamethods to it to allow global variables luaL_register(L, "input", lib); + lua_createtable(L, 0, 2); + lua_pushcfunction(L, lib_get); + lua_setfield(L, -2, "__index"); + lua_setmetatable(L, -2); + + lua_newuserdata(L, 0); + lua_createtable(L, 0, 2); + lua_pushcfunction(L, lib_getGameKeyDown); + lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, lib_setGameKeyDown); + lua_setfield(L, -2, "__newindex"); + + lua_pushcfunction(L, lib_lenGameKeyDown); + lua_setfield(L, -2, "__len"); + lua_setmetatable(L, -2); + lua_pushvalue(L, -1); // TODO: 2.3: Delete (gamekeydown moved to input library) + lua_setglobal(L, "gamekeydown"); // Delete too + lua_setfield(L, -2, "gamekeydown"); + lua_pop(L, 1); + return 0; } diff --git a/src/lua_script.c b/src/lua_script.c index 392935b4f..9e106999d 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -415,9 +415,11 @@ int LUA_PushGlobals(lua_State *L, const char *word) } else if (fastcmp(word, "stagefailed")) { lua_pushboolean(L, stagefailed); return 1; + // TODO: 2.3: Deprecated (moved to the input library) } else if (fastcmp(word, "mouse")) { LUA_PushUserdata(L, &mouse, META_MOUSE); return 1; + // TODO: 2.3: Deprecated (moved to the input library) } else if (fastcmp(word, "mouse2")) { LUA_PushUserdata(L, &mouse2, META_MOUSE); return 1; From 4fa4d6e0766b2e427357f36a178cb797c4177ac9 Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Fri, 27 Oct 2023 17:34:29 +0200 Subject: [PATCH 2/8] Implement input.ignoregameinputs --- src/g_game.c | 3 ++- src/g_input.c | 11 ++++++++--- src/lua_inputlib.c | 25 +++++++++++++++++++++++++ src/lua_libs.h | 1 + 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/g_game.c b/src/g_game.c index fae311694..962c2fd14 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -51,6 +51,7 @@ #include "r_fps.h" // frame interpolation/uncapped #include "lua_hud.h" +#include "lua_libs.h" gameaction_t gameaction; gamestate_t gamestate = GS_NULL; @@ -1170,7 +1171,7 @@ void G_BuildTiccmd(ticcmd_t *cmd, INT32 realtics, UINT8 ssplayer) // why build a ticcmd if we're paused? // Or, for that matter, if we're being reborn. // ...OR if we're blindfolded. No looking into the floor. - if (paused || P_AutoPause() || (gamestate == GS_LEVEL && (player->playerstate == PST_REBORN || ((gametyperules & GTR_TAG) + if (ignoregameinputs || paused || P_AutoPause() || (gamestate == GS_LEVEL && (player->playerstate == PST_REBORN || ((gametyperules & GTR_TAG) && (leveltime < hidetime * TICRATE) && (player->pflags & PF_TAGIT))))) {//@TODO splitscreen player cmd->angleturn = ticcmd_oldangleturn[forplayer]; diff --git a/src/g_input.c b/src/g_input.c index fa30c1984..97c0505a3 100644 --- a/src/g_input.c +++ b/src/g_input.c @@ -18,6 +18,8 @@ #include "hu_stuff.h" // need HUFONT start & end #include "netcode/d_net.h" #include "console.h" +#include "lua_script.h" +#include "lua_libs.h" #define MAXMOUSESENSITIVITY 100 // sensitivity steps @@ -116,7 +118,10 @@ void G_MapEventsToControls(event_t *ev) { case ev_keydown: if (ev->key < NUMINPUTS) - gamekeydown[ev->key] = 1; + { + if (!ignoregameinputs) + gamekeydown[ev->key] = 1; + } #ifdef PARANOIA else { @@ -144,7 +149,7 @@ void G_MapEventsToControls(event_t *ev) case ev_joystick: // buttons are virtual keys i = ev->key; - if (i >= JOYAXISSET || menuactive || CON_Ready() || chat_on) + if (i >= JOYAXISSET || menuactive || CON_Ready() || chat_on || ignoregameinputs) break; if (ev->x != INT32_MAX) joyxmove[i] = ev->x; if (ev->y != INT32_MAX) joyymove[i] = ev->y; @@ -152,7 +157,7 @@ void G_MapEventsToControls(event_t *ev) case ev_joystick2: // buttons are virtual keys i = ev->key; - if (i >= JOYAXISSET || menuactive || CON_Ready() || chat_on) + if (i >= JOYAXISSET || menuactive || CON_Ready() || chat_on || ignoregameinputs) break; if (ev->x != INT32_MAX) joy2xmove[i] = ev->x; if (ev->y != INT32_MAX) joy2ymove[i] = ev->y; diff --git a/src/lua_inputlib.c b/src/lua_inputlib.c index 0602b3db6..eeb61fed7 100644 --- a/src/lua_inputlib.c +++ b/src/lua_inputlib.c @@ -20,6 +20,7 @@ #include "lua_libs.h" boolean mousegrabbedbylua = true; +boolean ignoregameinputs = false; /////////////// // FUNCTIONS // @@ -163,12 +164,33 @@ static int lib_get(lua_State *L) LUA_PushUserdata(L, &mouse2, META_MOUSE); return 1; } + else if (fastcmp(field, "ignoregameinputs")) + { + lua_pushboolean(L, ignoregameinputs); + return 1; + } else { return 0; } } +static int lib_set(lua_State *L) +{ + const char *field = luaL_checkstring(L, 2); + + if (fastcmp(field, "ignoregameinputs")) + { + ignoregameinputs = luaL_checkboolean(L, 3); + } + else + { + lua_rawset(L, 1); + } + + return 0; +} + /////////////////// // gamekeydown[] // /////////////////// @@ -282,6 +304,9 @@ int LUA_InputLib(lua_State *L) lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_get); lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, lib_set); + lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); lua_newuserdata(L, 0); diff --git a/src/lua_libs.h b/src/lua_libs.h index 7f8d21f38..2e3c70652 100644 --- a/src/lua_libs.h +++ b/src/lua_libs.h @@ -13,6 +13,7 @@ extern lua_State *gL; extern boolean mousegrabbedbylua; +extern boolean ignoregameinputs; #define MUTABLE_TAGS From bc9a378521f2d63dc46b75579b9cce63f3b7e0e5 Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Fri, 27 Oct 2023 20:17:27 +0200 Subject: [PATCH 3/8] Refactor userdata metatable registration --- src/lua_consolelib.c | 5 +- src/lua_hudlib.c | 40 ++--------- src/lua_infolib.c | 107 +++------------------------- src/lua_inputlib.c | 14 +--- src/lua_maplib.c | 163 ++++++------------------------------------- src/lua_mobjlib.c | 39 ++--------- src/lua_playerlib.c | 33 +-------- src/lua_polyobjlib.c | 29 +------- src/lua_script.c | 29 ++++++++ src/lua_script.h | 8 +++ src/lua_skinlib.c | 35 ++-------- 11 files changed, 89 insertions(+), 413 deletions(-) diff --git a/src/lua_consolelib.c b/src/lua_consolelib.c index 0ab8ad9c3..b34105004 100644 --- a/src/lua_consolelib.c +++ b/src/lua_consolelib.c @@ -626,10 +626,7 @@ static int cvar_get(lua_State *L) int LUA_ConsoleLib(lua_State *L) { // Metatable for consvar_t - luaL_newmetatable(L, META_CVAR); - lua_pushcfunction(L, cvar_get); - lua_setfield(L, -2, "__index"); - lua_pop(L,1); + LUA_RegisterUserdataMetatable(L, META_CVAR, cvar_get, NULL, NULL); cvar_fields_ref = Lua_CreateFieldTable(L, cvar_opt); diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c index 6eec91273..ed0ec86f8 100644 --- a/src/lua_hudlib.c +++ b/src/lua_hudlib.c @@ -1404,16 +1404,13 @@ int LUA_HudLib(lua_State *L) luaL_register(L, NULL, lib_draw); lib_draw_ref = luaL_ref(L, LUA_REGISTRYINDEX); - luaL_newmetatable(L, META_HUDINFO); - lua_pushcfunction(L, hudinfo_get); - lua_setfield(L, -2, "__index"); + LUA_RegisterUserdataMetatable(L, META_HUDINFO, hudinfo_get, hudinfo_set, hudinfo_num); + LUA_RegisterUserdataMetatable(L, META_COLORMAP, colormap_get, NULL, NULL); + LUA_RegisterUserdataMetatable(L, META_PATCH, patch_get, patch_set, NULL); + LUA_RegisterUserdataMetatable(L, META_CAMERA, camera_get, camera_set, NULL); - lua_pushcfunction(L, hudinfo_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, hudinfo_num); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); + patch_fields_ref = Lua_CreateFieldTable(L, patch_opt); + camera_fields_ref = Lua_CreateFieldTable(L, camera_opt); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); @@ -1425,31 +1422,6 @@ int LUA_HudLib(lua_State *L) lua_setmetatable(L, -2); lua_setglobal(L, "hudinfo"); - luaL_newmetatable(L, META_COLORMAP); - lua_pushcfunction(L, colormap_get); - lua_setfield(L, -2, "__index"); - lua_pop(L,1); - - luaL_newmetatable(L, META_PATCH); - lua_pushcfunction(L, patch_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, patch_set); - lua_setfield(L, -2, "__newindex"); - lua_pop(L,1); - - patch_fields_ref = Lua_CreateFieldTable(L, patch_opt); - - luaL_newmetatable(L, META_CAMERA); - lua_pushcfunction(L, camera_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, camera_set); - lua_setfield(L, -2, "__newindex"); - lua_pop(L,1); - - camera_fields_ref = Lua_CreateFieldTable(L, camera_opt); - luaL_register(L, "hud", lib_hud); return 0; } diff --git a/src/lua_infolib.c b/src/lua_infolib.c index 3764acf6a..6b15cc33f 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -1914,96 +1914,18 @@ int LUA_InfoLib(lua_State *L) lua_newtable(L); lua_setfield(L, LUA_REGISTRYINDEX, LREG_ACTIONS); - luaL_newmetatable(L, META_STATE); - lua_pushcfunction(L, state_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, state_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, state_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_MOBJINFO); - lua_pushcfunction(L, mobjinfo_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, mobjinfo_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, mobjinfo_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); + LUA_RegisterUserdataMetatable(L, META_STATE, state_get, state_set, state_num); + LUA_RegisterUserdataMetatable(L, META_MOBJINFO, mobjinfo_get, mobjinfo_set, mobjinfo_num); + LUA_RegisterUserdataMetatable(L, META_SKINCOLOR, skincolor_get, skincolor_set, skincolor_num); + LUA_RegisterUserdataMetatable(L, META_COLORRAMP, colorramp_get, colorramp_set, colorramp_len); + LUA_RegisterUserdataMetatable(L, META_SFXINFO, sfxinfo_get, sfxinfo_set, sfxinfo_num); + LUA_RegisterUserdataMetatable(L, META_SPRITEINFO, spriteinfo_get, spriteinfo_set, spriteinfo_num); + LUA_RegisterUserdataMetatable(L, META_PIVOTLIST, pivotlist_get, pivotlist_set, pivotlist_num); + LUA_RegisterUserdataMetatable(L, META_FRAMEPIVOT, framepivot_get, framepivot_set, framepivot_num); + LUA_RegisterUserdataMetatable(L, META_LUABANKS, lib_getluabanks, lib_setluabanks, lib_luabankslen); mobjinfo_fields_ref = Lua_CreateFieldTable(L, mobjinfo_opt); - luaL_newmetatable(L, META_SKINCOLOR); - lua_pushcfunction(L, skincolor_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, skincolor_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, skincolor_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_COLORRAMP); - lua_pushcfunction(L, colorramp_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, colorramp_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, colorramp_len); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); - - luaL_newmetatable(L, META_SFXINFO); - lua_pushcfunction(L, sfxinfo_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, sfxinfo_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, sfxinfo_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_SPRITEINFO); - lua_pushcfunction(L, spriteinfo_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, spriteinfo_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, spriteinfo_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_PIVOTLIST); - lua_pushcfunction(L, pivotlist_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, pivotlist_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, pivotlist_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_FRAMEPIVOT); - lua_pushcfunction(L, framepivot_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, framepivot_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, framepivot_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getSprname); @@ -2104,16 +2026,5 @@ int LUA_InfoLib(lua_State *L) lua_setmetatable(L, -2); lua_setglobal(L, "spriteinfo"); - luaL_newmetatable(L, META_LUABANKS); - lua_pushcfunction(L, lib_getluabanks); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setluabanks); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_luabankslen); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - return 0; } diff --git a/src/lua_inputlib.c b/src/lua_inputlib.c index eeb61fed7..34b44e3af 100644 --- a/src/lua_inputlib.c +++ b/src/lua_inputlib.c @@ -285,18 +285,8 @@ static int mouse_num(lua_State *L) int LUA_InputLib(lua_State *L) { - luaL_newmetatable(L, META_KEYEVENT); - lua_pushcfunction(L, keyevent_get); - lua_setfield(L, -2, "__index"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_MOUSE); - lua_pushcfunction(L, mouse_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, mouse_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); + LUA_RegisterUserdataMetatable(L, META_KEYEVENT, keyevent_get, NULL, NULL); + LUA_RegisterUserdataMetatable(L, META_MOUSE, mouse_get, NULL, mouse_num); // Register the library, then add __index and __newindex // metamethods to it to allow global variables diff --git a/src/lua_maplib.c b/src/lua_maplib.c index e34397993..998615850 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -2843,120 +2843,37 @@ static int mapheaderinfo_get(lua_State *L) int LUA_MapLib(lua_State *L) { - luaL_newmetatable(L, META_SECTORLINES); - lua_pushcfunction(L, sectorlines_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, sectorlines_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_SECTOR); - lua_pushcfunction(L, sector_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, sector_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, sector_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); + LUA_RegisterUserdataMetatable(L, META_SECTORLINES, sectorlines_get, NULL, sectorlines_num); + LUA_RegisterUserdataMetatable(L, META_SECTOR, sector_get, sector_set, sector_num); + LUA_RegisterUserdataMetatable(L, META_SUBSECTOR, subsector_get, NULL, subsector_num); + LUA_RegisterUserdataMetatable(L, META_LINE, line_get, NULL, line_num); + LUA_RegisterUserdataMetatable(L, META_LINEARGS, lineargs_get, NULL, lineargs_len); + LUA_RegisterUserdataMetatable(L, META_LINESTRINGARGS, linestringargs_get, NULL, linestringargs_len); + LUA_RegisterUserdataMetatable(L, META_SIDENUM, sidenum_get, NULL, NULL); + LUA_RegisterUserdataMetatable(L, META_SIDE, side_get, side_set, side_num); + LUA_RegisterUserdataMetatable(L, META_VERTEX, vertex_get, NULL, vertex_num); + LUA_RegisterUserdataMetatable(L, META_FFLOOR, ffloor_get, ffloor_set, NULL); + LUA_RegisterUserdataMetatable(L, META_BBOX, bbox_get, NULL, NULL); + LUA_RegisterUserdataMetatable(L, META_SLOPE, slope_get, slope_set, NULL); + LUA_RegisterUserdataMetatable(L, META_VECTOR2, vector2_get, NULL, NULL); + LUA_RegisterUserdataMetatable(L, META_VECTOR3, vector3_get, NULL, NULL); + LUA_RegisterUserdataMetatable(L, META_MAPHEADER, mapheaderinfo_get, NULL, NULL); sector_fields_ref = Lua_CreateFieldTable(L, sector_opt); - - luaL_newmetatable(L, META_SUBSECTOR); - lua_pushcfunction(L, subsector_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, subsector_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - subsector_fields_ref = Lua_CreateFieldTable(L, subsector_opt); - - luaL_newmetatable(L, META_LINE); - lua_pushcfunction(L, line_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, line_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - line_fields_ref = Lua_CreateFieldTable(L, line_opt); - - luaL_newmetatable(L, META_LINEARGS); - lua_pushcfunction(L, lineargs_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lineargs_len); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_LINESTRINGARGS); - lua_pushcfunction(L, linestringargs_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, linestringargs_len); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_SIDENUM); - lua_pushcfunction(L, sidenum_get); - lua_setfield(L, -2, "__index"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_SIDE); - lua_pushcfunction(L, side_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, side_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, side_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - side_fields_ref = Lua_CreateFieldTable(L, side_opt); - - luaL_newmetatable(L, META_VERTEX); - lua_pushcfunction(L, vertex_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, vertex_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - vertex_fields_ref = Lua_CreateFieldTable(L, vertex_opt); - - luaL_newmetatable(L, META_FFLOOR); - lua_pushcfunction(L, ffloor_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, ffloor_set); - lua_setfield(L, -2, "__newindex"); - lua_pop(L, 1); - ffloor_fields_ref = Lua_CreateFieldTable(L, ffloor_opt); + slope_fields_ref = Lua_CreateFieldTable(L, slope_opt); + mapheaderinfo_fields_ref = Lua_CreateFieldTable(L, mapheaderinfo_opt); #ifdef HAVE_LUA_SEGS - luaL_newmetatable(L, META_SEG); - lua_pushcfunction(L, seg_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, seg_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); + LUA_RegisterUserdataMetatable(L, META_SEG, seg_get, NULL, seg_num); + LUA_RegisterUserdataMetatable(L, META_NODE, node_get, NULL, node_num); + LUA_RegisterUserdataMetatable(L, META_NODECHILDREN, nodechildren_get, NULL, NULL); seg_fields_ref = Lua_CreateFieldTable(L, seg_opt); - - luaL_newmetatable(L, META_NODE); - lua_pushcfunction(L, node_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, node_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - node_fields_ref = Lua_CreateFieldTable(L, node_opt); luaL_newmetatable(L, META_NODEBBOX); @@ -2965,48 +2882,8 @@ int LUA_MapLib(lua_State *L) lua_pushcfunction(L, nodebbox_call); lua_setfield(L, -2, "__call"); lua_pop(L, 1); - - luaL_newmetatable(L, META_NODECHILDREN); - lua_pushcfunction(L, nodechildren_get); - lua_setfield(L, -2, "__index"); - lua_pop(L, 1); #endif - luaL_newmetatable(L, META_BBOX); - lua_pushcfunction(L, bbox_get); - lua_setfield(L, -2, "__index"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_SLOPE); - lua_pushcfunction(L, slope_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, slope_set); - lua_setfield(L, -2, "__newindex"); - lua_pop(L, 1); - - slope_fields_ref = Lua_CreateFieldTable(L, slope_opt); - - luaL_newmetatable(L, META_VECTOR2); - lua_pushcfunction(L, vector2_get); - lua_setfield(L, -2, "__index"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_VECTOR3); - lua_pushcfunction(L, vector3_get); - lua_setfield(L, -2, "__index"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_MAPHEADER); - lua_pushcfunction(L, mapheaderinfo_get); - lua_setfield(L, -2, "__index"); - - //lua_pushcfunction(L, mapheaderinfo_num); - //lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - mapheaderinfo_fields_ref = Lua_CreateFieldTable(L, mapheaderinfo_opt); - LUA_PushTaggableObjectArray(L, "sectors", lib_iterateSectors, lib_getSector, diff --git a/src/lua_mobjlib.c b/src/lua_mobjlib.c index fddf958be..19f30b70e 100644 --- a/src/lua_mobjlib.c +++ b/src/lua_mobjlib.c @@ -1163,43 +1163,12 @@ static int lib_nummapthings(lua_State *L) int LUA_MobjLib(lua_State *L) { - luaL_newmetatable(L, META_MOBJ); - lua_pushcfunction(L, mobj_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, mobj_set); - lua_setfield(L, -2, "__newindex"); - lua_pop(L,1); + LUA_RegisterUserdataMetatable(L, META_MOBJ, mobj_get, mobj_set, NULL); + LUA_RegisterUserdataMetatable(L, META_THINGARGS, thingargs_get, NULL, thingargs_len); + LUA_RegisterUserdataMetatable(L, META_THINGSTRINGARGS, thingstringargs_get, NULL, thingstringargs_len); + LUA_RegisterUserdataMetatable(L, META_MAPTHING, mapthing_get, mapthing_set, mapthing_num); mobj_fields_ref = Lua_CreateFieldTable(L, mobj_opt); - - luaL_newmetatable(L, META_THINGARGS); - lua_pushcfunction(L, thingargs_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, thingargs_len); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_THINGSTRINGARGS); - lua_pushcfunction(L, thingstringargs_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, thingstringargs_len); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_MAPTHING); - lua_pushcfunction(L, mapthing_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, mapthing_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, mapthing_num); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); - mapthing_fields_ref = Lua_CreateFieldTable(L, mapthing_opt); LUA_PushTaggableObjectArray(L, "mapthings", diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index 827e5a405..975025c44 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -1523,38 +1523,11 @@ static int ticcmd_set(lua_State *L) int LUA_PlayerLib(lua_State *L) { - luaL_newmetatable(L, META_PLAYER); - lua_pushcfunction(L, player_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, player_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, player_num); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); + LUA_RegisterUserdataMetatable(L, META_PLAYER, player_get, player_set, player_num); + LUA_RegisterUserdataMetatable(L, META_POWERS, power_get, power_set, power_len); + LUA_RegisterUserdataMetatable(L, META_TICCMD, ticcmd_get, ticcmd_set, NULL); player_fields_ref = Lua_CreateFieldTable(L, player_opt); - - luaL_newmetatable(L, META_POWERS); - lua_pushcfunction(L, power_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, power_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, power_len); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); - - luaL_newmetatable(L, META_TICCMD); - lua_pushcfunction(L, ticcmd_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, ticcmd_set); - lua_setfield(L, -2, "__newindex"); - lua_pop(L,1); - ticcmd_fields_ref = Lua_CreateFieldTable(L, ticcmd_opt); lua_newuserdata(L, 0); diff --git a/src/lua_polyobjlib.c b/src/lua_polyobjlib.c index c3d9d9d1a..d011ccfaa 100644 --- a/src/lua_polyobjlib.c +++ b/src/lua_polyobjlib.c @@ -447,32 +447,9 @@ static int lib_numPolyObjects(lua_State *L) int LUA_PolyObjLib(lua_State *L) { - luaL_newmetatable(L, META_POLYOBJVERTICES); - lua_pushcfunction(L, polyobjvertices_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, polyobjvertices_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_POLYOBJLINES); - lua_pushcfunction(L, polyobjlines_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, polyobjlines_num); - lua_setfield(L, -2, "__len"); - lua_pop(L, 1); - - luaL_newmetatable(L, META_POLYOBJ); - lua_pushcfunction(L, polyobj_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, polyobj_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, polyobj_num); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); + LUA_RegisterUserdataMetatable(L, META_POLYOBJVERTICES, polyobjvertices_get, NULL, polyobjvertices_num); + LUA_RegisterUserdataMetatable(L, META_POLYOBJLINES, polyobjlines_get, NULL, polyobjlines_num); + LUA_RegisterUserdataMetatable(L, META_POLYOBJ, polyobj_get, polyobj_set, polyobj_num); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); diff --git a/src/lua_script.c b/src/lua_script.c index 9e106999d..562114583 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -1832,3 +1832,32 @@ void LUA_PushTaggableObjectArray lua_setmetatable(L, -2); lua_setglobal(L, field); } + +void LUA_RegisterUserdataMetatable( + lua_State *L, + const char *name, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len +) +{ + luaL_newmetatable(L, name); + + if (get) + { + lua_pushcfunction(L, get); + lua_setfield(L, -2, "__index"); + } + if (set) + { + lua_pushcfunction(L, set); + lua_setfield(L, -2, "__newindex"); + } + if (len) + { + lua_pushcfunction(L, len); + lua_setfield(L, -2, "__len"); + } + + lua_pop(L, 1); +} diff --git a/src/lua_script.h b/src/lua_script.h index d0b06a719..8a4eb9f13 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -73,6 +73,14 @@ void LUA_PushTaggableObjectArray size_t sizeof_element, const char *meta); +void LUA_RegisterUserdataMetatable( + lua_State *L, + const char *name, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len +); + void LUA_InsertTaggroupIterator ( lua_State *L, taggroup_t *garray[], diff --git a/src/lua_skinlib.c b/src/lua_skinlib.c index 041c5d598..6c41758b1 100644 --- a/src/lua_skinlib.c +++ b/src/lua_skinlib.c @@ -373,40 +373,13 @@ static int sprite_get(lua_State *L) int LUA_SkinLib(lua_State *L) { - luaL_newmetatable(L, META_SKIN); - lua_pushcfunction(L, skin_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, skin_set); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, skin_num); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); + LUA_RegisterUserdataMetatable(L, META_SKIN, skin_get, skin_set, skin_num); + LUA_RegisterUserdataMetatable(L, META_SOUNDSID, soundsid_get, NULL, soundsid_num); + LUA_RegisterUserdataMetatable(L, META_SKINSPRITES, lib_getSkinSprite, NULL, lib_numSkinsSprites); + LUA_RegisterUserdataMetatable(L, META_SKINSPRITESLIST, sprite_get, NULL, NULL); skin_fields_ref = Lua_CreateFieldTable(L, skin_opt); - luaL_newmetatable(L, META_SOUNDSID); - lua_pushcfunction(L, soundsid_get); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, soundsid_num); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); - - luaL_newmetatable(L, META_SKINSPRITES); - lua_pushcfunction(L, lib_getSkinSprite); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numSkinsSprites); - lua_setfield(L, -2, "__len"); - lua_pop(L,1); - - luaL_newmetatable(L, META_SKINSPRITESLIST); - lua_pushcfunction(L, sprite_get); - lua_setfield(L, -2, "__index"); - lua_pop(L,1); - lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getSkin); From 7a7d4f832a3d1ad18949bc02bda04dea7296398f Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Fri, 27 Oct 2023 21:34:53 +0200 Subject: [PATCH 4/8] Remove stupid whitespace --- src/lua_maplib.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 998615850..a0a7120a2 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -35,7 +35,7 @@ enum sector_e { sector_floorpic, sector_floorxoffset, sector_flooryoffset, - sector_floorangle, + sector_floorangle, sector_ceilingpic, sector_ceilingxoffset, sector_ceilingyoffset, @@ -43,7 +43,7 @@ enum sector_e { sector_lightlevel, sector_floorlightlevel, sector_floorlightabsolute, - sector_floorlightsec, + sector_floorlightsec, sector_ceilinglightlevel, sector_ceilinglightabsolute, sector_ceilinglightsec, @@ -77,14 +77,14 @@ static const char *const sector_opt[] = { "ceilingpic", "ceilingxoffset", "ceilingyoffset", - "ceilingangle", + "ceilingangle", "lightlevel", "floorlightlevel", "floorlightabsolute", "floorlightsec", "ceilinglightlevel", "ceilinglightabsolute", - "ceilinglightsec", + "ceilinglightsec", "special", "tag", "taglist", @@ -663,11 +663,11 @@ static int sector_get(lua_State *L) lua_pushfixed(L, sector->flooryoffset); return 1; } - case sector_floorangle: + case sector_floorangle: { lua_pushangle(L, sector->floorangle); return 1; - } + } case sector_ceilingpic: // ceilingpic { levelflat_t *levelflat = &levelflats[sector->ceilingpic]; @@ -691,7 +691,7 @@ static int sector_get(lua_State *L) { lua_pushangle(L, sector->ceilingangle); return 1; - } + } case sector_lightlevel: lua_pushinteger(L, sector->lightlevel); return 1; @@ -703,7 +703,7 @@ static int sector_get(lua_State *L) return 1; case sector_floorlightsec: lua_pushinteger(L, sector->floorlightsec); - return 1; + return 1; case sector_ceilinglightlevel: lua_pushinteger(L, sector->ceilinglightlevel); return 1; @@ -712,7 +712,7 @@ static int sector_get(lua_State *L) return 1; case sector_ceilinglightsec: lua_pushinteger(L, sector->ceilinglightsec); - return 1; + return 1; case sector_special: lua_pushinteger(L, sector->special); return 1; @@ -842,7 +842,7 @@ static int sector_set(lua_State *L) break; case sector_floorangle: sector->floorangle = luaL_checkangle(L, 3); - break; + break; case sector_ceilingpic: sector->ceilingpic = P_AddLevelFlatRuntime(luaL_checkstring(L, 3)); break; @@ -866,7 +866,7 @@ static int sector_set(lua_State *L) break; case sector_floorlightsec: sector->floorlightsec = (INT32)luaL_checkinteger(L, 3); - break; + break; case sector_ceilinglightlevel: sector->ceilinglightlevel = (INT16)luaL_checkinteger(L, 3); break; @@ -875,7 +875,7 @@ static int sector_set(lua_State *L) break; case sector_ceilinglightsec: sector->ceilinglightsec = (INT32)luaL_checkinteger(L, 3); - break; + break; case sector_special: sector->special = (INT16)luaL_checkinteger(L, 3); break; From d1dbdee016631a62dd9883bc01eb777054d7942c Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Sat, 28 Oct 2023 00:24:04 +0200 Subject: [PATCH 5/8] Refactor global userdata registration --- src/lua_hudlib.c | 10 +--- src/lua_infolib.c | 109 ++++--------------------------------------- src/lua_inputlib.c | 25 ++-------- src/lua_maplib.c | 101 ++++++++++----------------------------- src/lua_playerlib.c | 10 +--- src/lua_polyobjlib.c | 10 +--- src/lua_script.c | 72 ++++++++++++++++++++++++++-- src/lua_script.h | 26 +++++++++++ src/lua_skinlib.c | 10 +--- src/lua_taglib.c | 17 +++---- 10 files changed, 142 insertions(+), 248 deletions(-) diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c index ed0ec86f8..666a11abc 100644 --- a/src/lua_hudlib.c +++ b/src/lua_hudlib.c @@ -1412,15 +1412,7 @@ int LUA_HudLib(lua_State *L) patch_fields_ref = Lua_CreateFieldTable(L, patch_opt); camera_fields_ref = Lua_CreateFieldTable(L, camera_opt); - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getHudInfo); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_hudinfolen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "hudinfo"); + LUA_RegisterGlobalUserdata(L, "hudinfo", lib_getHudInfo, NULL, lib_hudinfolen); luaL_register(L, "hud", lib_hud); return 0; diff --git a/src/lua_infolib.c b/src/lua_infolib.c index 6b15cc33f..020eba152 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -1926,105 +1926,16 @@ int LUA_InfoLib(lua_State *L) mobjinfo_fields_ref = Lua_CreateFieldTable(L, mobjinfo_opt); - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSprname); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_sprnamelen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "sprnames"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSpr2name); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_spr2namelen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "spr2names"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSpr2default); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setSpr2default); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_spr2namelen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "spr2defaults"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getState); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setState); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_statelen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "states"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getMobjInfo); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setMobjInfo); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_mobjinfolen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "mobjinfo"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSkinColor); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setSkinColor); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_skincolorslen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "skincolors"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSfxInfo); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setSfxInfo); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_sfxlen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_pushvalue(L, -1); - lua_setglobal(L, "S_sfx"); - lua_setglobal(L, "sfxinfo"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSpriteInfo); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setSpriteInfo); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_spriteinfolen); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "spriteinfo"); + LUA_RegisterGlobalUserdata(L, "sprnames", lib_getSprname, NULL, lib_sprnamelen); + LUA_RegisterGlobalUserdata(L, "spr2names", lib_getSpr2name, NULL, lib_spr2namelen); + LUA_RegisterGlobalUserdata(L, "spr2defaults", lib_getSpr2default, lib_setSpr2default, lib_spr2namelen); + LUA_RegisterGlobalUserdata(L, "states", lib_getState, lib_setState, lib_statelen); + LUA_RegisterGlobalUserdata(L, "mobjinfo", lib_getMobjInfo, lib_setMobjInfo, lib_mobjinfolen); + LUA_RegisterGlobalUserdata(L, "skincolors", lib_getSkinColor, lib_setSkinColor, lib_skincolorslen); + LUA_RegisterGlobalUserdata(L, "spriteinfo", lib_getSpriteInfo, lib_setSpriteInfo, lib_spriteinfolen); + LUA_RegisterGlobalUserdata(L, "sfxinfo", lib_getSfxInfo, lib_setSfxInfo, lib_sfxlen); + // TODO: 2.3: Delete this alias + LUA_RegisterGlobalUserdata(L, "S_sfx", lib_getSfxInfo, lib_setSfxInfo, lib_sfxlen); return 0; } diff --git a/src/lua_inputlib.c b/src/lua_inputlib.c index 34b44e3af..ef3a9011f 100644 --- a/src/lua_inputlib.c +++ b/src/lua_inputlib.c @@ -291,28 +291,11 @@ int LUA_InputLib(lua_State *L) // Register the library, then add __index and __newindex // metamethods to it to allow global variables luaL_register(L, "input", lib); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_get); - lua_setfield(L, -2, "__index"); + LUA_CreateAndSetMetatable(L, lib_get, lib_set, NULL, false); - lua_pushcfunction(L, lib_set); - lua_setfield(L, -2, "__newindex"); - lua_setmetatable(L, -2); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getGameKeyDown); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_setGameKeyDown); - lua_setfield(L, -2, "__newindex"); - - lua_pushcfunction(L, lib_lenGameKeyDown); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_pushvalue(L, -1); // TODO: 2.3: Delete (gamekeydown moved to input library) - lua_setglobal(L, "gamekeydown"); // Delete too - lua_setfield(L, -2, "gamekeydown"); + LUA_CreateAndSetUserdataField(L, -1, "gamekeydown", lib_getGameKeyDown, lib_setGameKeyDown, lib_lenGameKeyDown, false); + // TODO: 2.3: Delete this alias (moved to input library) + LUA_RegisterGlobalUserdata(L, "gamekeydown", lib_getGameKeyDown, lib_setGameKeyDown, lib_lenGameKeyDown); lua_pop(L, 1); return 0; diff --git a/src/lua_maplib.c b/src/lua_maplib.c index a0a7120a2..2e1aab3eb 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -2868,6 +2868,27 @@ int LUA_MapLib(lua_State *L) slope_fields_ref = Lua_CreateFieldTable(L, slope_opt); mapheaderinfo_fields_ref = Lua_CreateFieldTable(L, mapheaderinfo_opt); + LUA_RegisterGlobalUserdata(L, "subsectors", lib_getSubsector, NULL, lib_numsubsectors); + LUA_RegisterGlobalUserdata(L, "sides", lib_getSide, NULL, lib_numsides); + LUA_RegisterGlobalUserdata(L, "vertexes", lib_getVertex, NULL, lib_numvertexes); + LUA_RegisterGlobalUserdata(L, "mapheaderinfo", lib_getMapheaderinfo, NULL, lib_nummapheaders); + + LUA_PushTaggableObjectArray(L, "sectors", + lib_iterateSectors, + lib_getSector, + lib_numsectors, + tags_sectors, + &numsectors, §ors, + sizeof (sector_t), META_SECTOR); + + LUA_PushTaggableObjectArray(L, "lines", + lib_iterateLines, + lib_getLine, + lib_numlines, + tags_lines, + &numlines, &lines, + sizeof (line_t), META_LINE); + #ifdef HAVE_LUA_SEGS LUA_RegisterUserdataMetatable(L, META_SEG, seg_get, NULL, seg_num); LUA_RegisterUserdataMetatable(L, META_NODE, node_get, NULL, node_num); @@ -2882,84 +2903,10 @@ int LUA_MapLib(lua_State *L) lua_pushcfunction(L, nodebbox_call); lua_setfield(L, -2, "__call"); lua_pop(L, 1); + + LUA_RegisterGlobalUserdata(L, "segs", lib_getSeg, NULL, lib_numsegs); + LUA_RegisterGlobalUserdata(L, "nodes", lib_getNode, NULL, lib_numnodes); #endif - LUA_PushTaggableObjectArray(L, "sectors", - lib_iterateSectors, - lib_getSector, - lib_numsectors, - tags_sectors, - &numsectors, §ors, - sizeof (sector_t), META_SECTOR); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSubsector); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numsubsectors); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "subsectors"); - - LUA_PushTaggableObjectArray(L, "lines", - lib_iterateLines, - lib_getLine, - lib_numlines, - tags_lines, - &numlines, &lines, - sizeof (line_t), META_LINE); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSide); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numsides); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "sides"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getVertex); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numvertexes); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "vertexes"); - -#ifdef HAVE_LUA_SEGS - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSeg); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numsegs); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "segs"); - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getNode); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numnodes); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "nodes"); -#endif - - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getMapheaderinfo); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_nummapheaders); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "mapheaderinfo"); return 0; } diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index 975025c44..ac829d0c4 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -1530,14 +1530,6 @@ int LUA_PlayerLib(lua_State *L) player_fields_ref = Lua_CreateFieldTable(L, player_opt); ticcmd_fields_ref = Lua_CreateFieldTable(L, ticcmd_opt); - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getPlayer); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_lenPlayer); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "players"); + LUA_RegisterGlobalUserdata(L, "players", lib_getPlayer, NULL, lib_lenPlayer); return 0; } diff --git a/src/lua_polyobjlib.c b/src/lua_polyobjlib.c index d011ccfaa..6c016f8b2 100644 --- a/src/lua_polyobjlib.c +++ b/src/lua_polyobjlib.c @@ -451,14 +451,6 @@ int LUA_PolyObjLib(lua_State *L) LUA_RegisterUserdataMetatable(L, META_POLYOBJLINES, polyobjlines_get, NULL, polyobjlines_num); LUA_RegisterUserdataMetatable(L, META_POLYOBJ, polyobj_get, polyobj_set, polyobj_num); - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getPolyObject); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numPolyObjects); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "polyobjects"); + LUA_RegisterGlobalUserdata(L, "polyobjects", lib_getPolyObject, NULL, lib_numPolyObjects); return 0; } diff --git a/src/lua_script.c b/src/lua_script.c index 562114583..9aea96ac7 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -1833,16 +1833,13 @@ void LUA_PushTaggableObjectArray lua_setglobal(L, field); } -void LUA_RegisterUserdataMetatable( +static void SetBasicMetamethods( lua_State *L, - const char *name, lua_CFunction get, lua_CFunction set, lua_CFunction len ) { - luaL_newmetatable(L, name); - if (get) { lua_pushcfunction(L, get); @@ -1858,6 +1855,73 @@ void LUA_RegisterUserdataMetatable( lua_pushcfunction(L, len); lua_setfield(L, -2, "__len"); } +} +void LUA_RegisterUserdataMetatable( + lua_State *L, + const char *name, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len +) +{ + luaL_newmetatable(L, name); + SetBasicMetamethods(L, get, set, len); lua_pop(L, 1); } + +// If keep is true, leaves the metatable on the stack. +// Otherwise, the stack size remains unchanged. +void LUA_CreateAndSetMetatable( + lua_State *L, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len, + boolean keep +) +{ + lua_newtable(L); + SetBasicMetamethods(L, get, set, len); + + lua_pushvalue(L, -1); + lua_setmetatable(L, -3); + + if (!keep) + lua_pop(L, 1); +} + +// If keep is true, leaves the userdata and metatable on the stack. +// Otherwise, the stack size remains unchanged. +void LUA_CreateAndSetUserdataField( + lua_State *L, + int index, + const char *name, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len, + boolean keep +) +{ + if (index < 0 && index > LUA_REGISTRYINDEX) + index -= 3; + + lua_newuserdata(L, 0); + LUA_CreateAndSetMetatable(L, get, set, len, true); + + lua_pushvalue(L, -2); + lua_setfield(L, index, name); + + if (!keep) + lua_pop(L, 2); +} + +void LUA_RegisterGlobalUserdata( + lua_State *L, + const char *name, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len +) +{ + LUA_CreateAndSetUserdataField(L, LUA_GLOBALSINDEX, name, get, set, len, false); +} diff --git a/src/lua_script.h b/src/lua_script.h index 8a4eb9f13..dd9a2568e 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -81,6 +81,32 @@ void LUA_RegisterUserdataMetatable( lua_CFunction len ); +void LUA_CreateAndSetMetatable( + lua_State *L, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len, + boolean keep +); + +void LUA_CreateAndSetUserdataField( + lua_State *L, + int index, + const char *name, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len, + boolean keep +); + +void LUA_RegisterGlobalUserdata( + lua_State *L, + const char *name, + lua_CFunction get, + lua_CFunction set, + lua_CFunction len +); + void LUA_InsertTaggroupIterator ( lua_State *L, taggroup_t *garray[], diff --git a/src/lua_skinlib.c b/src/lua_skinlib.c index 6c41758b1..3debd3746 100644 --- a/src/lua_skinlib.c +++ b/src/lua_skinlib.c @@ -380,15 +380,7 @@ int LUA_SkinLib(lua_State *L) skin_fields_ref = Lua_CreateFieldTable(L, skin_opt); - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_pushcfunction(L, lib_getSkin); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numSkins); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "skins"); + LUA_RegisterGlobalUserdata(L, "skins", lib_getSkin, NULL, lib_numSkins); return 0; } diff --git a/src/lua_taglib.c b/src/lua_taglib.c index 2ba60df99..5ed457534 100644 --- a/src/lua_taglib.c +++ b/src/lua_taglib.c @@ -426,17 +426,12 @@ set_taglist_metatable(lua_State *L, const char *meta) int LUA_TagLib(lua_State *L) { - lua_newuserdata(L, 0); - lua_createtable(L, 0, 2); - lua_createtable(L, 0, 1); - lua_pushcfunction(L, lib_iterateTags); - lua_setfield(L, -2, "iterate"); - lua_setfield(L, -2, "__index"); - - lua_pushcfunction(L, lib_numTags); - lua_setfield(L, -2, "__len"); - lua_setmetatable(L, -2); - lua_setglobal(L, "tags"); + LUA_CreateAndSetUserdataField(L, LUA_GLOBALSINDEX, "tags", NULL, NULL, lib_numTags, true); + lua_createtable(L, 0, 1); + lua_pushcfunction(L, lib_iterateTags); + lua_setfield(L, -2, "iterate"); + lua_setfield(L, -2, "__index"); + lua_pop(L, 2); open_taglist(L); From c3f79a112f3ec5cff160f9040196d284ffd4f9d8 Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Sat, 28 Oct 2023 11:06:30 +0200 Subject: [PATCH 6/8] Add LUA_SetCFunctionField --- src/deh_lua.c | 3 +-- src/lua_baselib.c | 3 +-- src/lua_maplib.c | 6 ++---- src/lua_script.c | 33 +++++++++++++-------------------- src/lua_script.h | 2 ++ src/lua_taglib.c | 12 ++++-------- src/lua_thinkerlib.c | 3 +-- 7 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/deh_lua.c b/src/deh_lua.c index 0b789547b..7188d25e3 100644 --- a/src/deh_lua.c +++ b/src/deh_lua.c @@ -771,8 +771,7 @@ int LUA_SOCLib(lua_State *L) lua_register(L,"getActionName",lib_getActionName); luaL_newmetatable(L, META_ACTION); - lua_pushcfunction(L, action_call); - lua_setfield(L, -2, "__call"); + LUA_SetCFunctionField(L, "__call", action_call); lua_pop(L, 1); return 0; diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 1821550b8..f7ed763a9 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -4300,8 +4300,7 @@ int LUA_BaseLib(lua_State *L) // Set metatable for string lua_pushliteral(L, ""); // dummy string lua_getmetatable(L, -1); // get string metatable - lua_pushcfunction(L,lib_concat); // push concatination function - lua_setfield(L,-2,"__add"); // ... store it as mathematical addition + LUA_SetCFunctionField(L, "__add", lib_concat); lua_pop(L, 2); // pop metatable and dummy string lua_newtable(L); diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 2e1aab3eb..efa2edf20 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -2898,10 +2898,8 @@ int LUA_MapLib(lua_State *L) node_fields_ref = Lua_CreateFieldTable(L, node_opt); luaL_newmetatable(L, META_NODEBBOX); - //lua_pushcfunction(L, nodebbox_get); - //lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, nodebbox_call); - lua_setfield(L, -2, "__call"); + //LUA_SetCFunctionField(L, "__index", nodebbox_get); + LUA_SetCFunctionField(L, "__call", nodebbox_call); lua_pop(L, 1); LUA_RegisterGlobalUserdata(L, "segs", lib_getSeg, NULL, lib_numsegs); diff --git a/src/lua_script.c b/src/lua_script.c index 9aea96ac7..5cacc3f01 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -578,8 +578,7 @@ static void LUA_ClearState(void) // lock the global namespace lua_getmetatable(L, LUA_GLOBALSINDEX); - lua_pushcfunction(L, setglobals); - lua_setfield(L, -2, "__newindex"); + LUA_SetCFunctionField(L, "__newindex", setglobals); lua_newtable(L); lua_setfield(L, -2, "__metatable"); lua_pop(L, 1); @@ -1815,20 +1814,17 @@ void LUA_PushTaggableObjectArray lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_createtable(L, 0, 2); - lua_pushcfunction(L, iterator); - lua_setfield(L, -2, "iterate"); + LUA_SetCFunctionField(L, "iterate", iterator); LUA_InsertTaggroupIterator(L, garray, max_elements, element_array, sizeof_element, meta); lua_createtable(L, 0, 1); - lua_pushcfunction(L, indexer); - lua_setfield(L, -2, "__index"); + LUA_SetCFunctionField(L, "__index", indexer); lua_setmetatable(L, -2); lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, counter); - lua_setfield(L, -2, "__len"); + LUA_SetCFunctionField(L, "__len", counter); lua_setmetatable(L, -2); lua_setglobal(L, field); } @@ -1841,20 +1837,17 @@ static void SetBasicMetamethods( ) { if (get) - { - lua_pushcfunction(L, get); - lua_setfield(L, -2, "__index"); - } + LUA_SetCFunctionField(L, "__index", get); if (set) - { - lua_pushcfunction(L, set); - lua_setfield(L, -2, "__newindex"); - } + LUA_SetCFunctionField(L, "__newindex", set); if (len) - { - lua_pushcfunction(L, len); - lua_setfield(L, -2, "__len"); - } + LUA_SetCFunctionField(L, "__len", len); +} + +void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value) +{ + lua_pushcfunction(L, value); + lua_setfield(L, -2, name); } void LUA_RegisterUserdataMetatable( diff --git a/src/lua_script.h b/src/lua_script.h index dd9a2568e..53d848f8e 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -73,6 +73,8 @@ void LUA_PushTaggableObjectArray size_t sizeof_element, const char *meta); +void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value); + void LUA_RegisterUserdataMetatable( lua_State *L, const char *name, diff --git a/src/lua_taglib.c b/src/lua_taglib.c index 5ed457534..9e73a050c 100644 --- a/src/lua_taglib.c +++ b/src/lua_taglib.c @@ -372,8 +372,7 @@ void LUA_InsertTaggroupIterator lua_pushcclosure(L, lib_numTaggroupElements, 2); lua_setfield(L, -2, "__len"); - lua_pushcfunction(L, element_iterator); - lua_setfield(L, -2, "__call"); + LUA_SetCFunctionField(L, "__call", element_iterator); lua_pushcclosure(L, lib_getTaggroup, 1); lua_setfield(L, -2, "tagged"); } @@ -414,11 +413,9 @@ set_taglist_metatable(lua_State *L, const char *meta) lua_setfenv(L, -2); lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, taglist_len); - lua_setfield(L, -2, "__len"); + LUA_SetCFunctionField(L, "__len", taglist_len); - lua_pushcfunction(L, taglist_equal); - lua_setfield(L, -2, "__eq"); + LUA_SetCFunctionField(L, "__eq", taglist_equal); #ifdef MUTABLE_TAGS return luaL_ref(L, LUA_REGISTRYINDEX); #endif @@ -428,8 +425,7 @@ int LUA_TagLib(lua_State *L) { LUA_CreateAndSetUserdataField(L, LUA_GLOBALSINDEX, "tags", NULL, NULL, lib_numTags, true); lua_createtable(L, 0, 1); - lua_pushcfunction(L, lib_iterateTags); - lua_setfield(L, -2, "iterate"); + LUA_SetCFunctionField(L, "iterate", lib_iterateTags); lua_setfield(L, -2, "__index"); lua_pop(L, 2); diff --git a/src/lua_thinkerlib.c b/src/lua_thinkerlib.c index cff92f34d..f1be8c789 100644 --- a/src/lua_thinkerlib.c +++ b/src/lua_thinkerlib.c @@ -127,8 +127,7 @@ static int lib_startIterate(lua_State *L) int LUA_ThinkerLib(lua_State *L) { luaL_newmetatable(L, META_ITERATIONSTATE); - lua_pushcfunction(L, iterationState_gc); - lua_setfield(L, -2, "__gc"); + LUA_SetCFunctionField(L, "__gc", iterationState_gc); lua_pop(L, 1); lua_createtable(L, 0, 1); From c0146744b7af018a73c29c7df9d57c523841da89 Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Sat, 28 Oct 2023 14:46:10 +0200 Subject: [PATCH 7/8] Normalise 2.3-related TODO comments --- src/deh_lua.c | 6 ++++-- src/g_game.c | 2 +- src/g_input.c | 2 +- src/lua_maplib.c | 12 +----------- src/lua_playerlib.c | 2 +- src/p_mobj.c | 6 +++--- src/p_saveg.h | 2 +- 7 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/deh_lua.c b/src/deh_lua.c index 7188d25e3..e596e166f 100644 --- a/src/deh_lua.c +++ b/src/deh_lua.c @@ -297,7 +297,8 @@ static int ScanConstants(lua_State *L, boolean mathlib, const char *word) CacheAndPushConstant(L, word, (lua_Integer)PF_FULLSTASIS); return 1; } - else if (fastcmp(p, "USEDOWN")) // Remove case when 2.3 nears release... + // TODO: 2.3: Delete this alias + else if (fastcmp(p, "USEDOWN")) { CacheAndPushConstant(L, word, (lua_Integer)PF_SPINDOWN); return 1; @@ -583,7 +584,8 @@ static int ScanConstants(lua_State *L, boolean mathlib, const char *word) return 0; } - if (fastcmp(word, "BT_USE")) // Remove case when 2.3 nears release... + // TODO: 2.3: Delete this alias + if (fastcmp(word, "BT_USE")) { CacheAndPushConstant(L, word, (lua_Integer)BT_SPIN); return 1; diff --git a/src/g_game.c b/src/g_game.c index 962c2fd14..438428b90 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -4347,7 +4347,7 @@ void G_LoadGameSettings(void) } #define GAMEDATA_ID 0x86E4A27C // Change every major version, as usual -#define COMPAT_GAMEDATA_ID 0xFCAFE211 // Can be removed entirely for 2.3 +#define COMPAT_GAMEDATA_ID 0xFCAFE211 // TODO: 2.3: Delete // G_LoadGameData // Loads the main data file, which stores information such as emblems found, etc. diff --git a/src/g_input.c b/src/g_input.c index 97c0505a3..8b5056331 100644 --- a/src/g_input.c +++ b/src/g_input.c @@ -1002,7 +1002,7 @@ static void setcontrol(INT32 (*gc)[2]) INT32 player = ((void*)gc == (void*)&gamecontrolbis ? 1 : 0); boolean nestedoverride = false; - // Update me for 2.3 + // TODO: 2.3: Delete the "use" alias namectrl = (stricmp(COM_Argv(1), "use")) ? COM_Argv(1) : "spin"; for (numctrl = 0; numctrl < NUM_GAMECONTROLS && stricmp(namectrl, gamecontrolname[numctrl]); diff --git a/src/lua_maplib.c b/src/lua_maplib.c index efa2edf20..588a6b4e6 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -1043,17 +1043,7 @@ static int line_get(lua_State *L) lua_pushinteger(L, line->special); return 1; case line_tag: - // HELLO - // THIS IS LJ SONIC - // HOW IS YOUR DAY? - // BY THE WAY WHEN 2.3 OR 3.0 OR 4.0 OR SRB3 OR SRB4 OR WHATEVER IS OUT - // YOU SHOULD REMEMBER TO CHANGE THIS SO IT ALWAYS RETURNS A UNSIGNED VALUE - // HAVE A NICE DAY - // - // - // - // - // you are ugly + // TODO: 2.3: Always return a unsigned value lua_pushinteger(L, Tag_FGet(&line->tags)); return 1; case line_taglist: diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index ac829d0c4..66d9c009b 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -407,7 +407,7 @@ static int player_get(lua_State *L) case player_realmo: LUA_PushUserdata(L, plr->mo, META_MOBJ); break; - // Kept for backward-compatibility + // TODO: 2.3: Kept for backward-compatibility // Should be fixed to work like "realmo" later case player_mo: if (plr->spectator) diff --git a/src/p_mobj.c b/src/p_mobj.c index a81845918..1ec46d528 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -10571,14 +10571,14 @@ static fixed_t P_DefaultMobjShadowScale (mobj_t *thing) case MT_EXPLOSIONRING: case MT_SCATTERRING: case MT_GRENADERING: - + case MT_BOUNCEPICKUP: case MT_RAILPICKUP: case MT_AUTOPICKUP: case MT_EXPLODEPICKUP: case MT_SCATTERPICKUP: case MT_GRENADEPICKUP: - + case MT_REDRING: case MT_THROWNBOUNCE: case MT_THROWNINFINITY: @@ -13319,7 +13319,7 @@ static boolean P_SetupSpawnedMapThing(mapthing_t *mthing, mobj_t *mobj, boolean return true; } -// Pre-UDMF backwards compatibility stuff. Remove for 2.3 +// TODO: 2.3: Delete (Pre-UDMF backwards compatibility stuff) static void P_SetAmbush(mapthing_t *mthing, mobj_t *mobj) { if (mobj->type == MT_NIGHTSBUMPER diff --git a/src/p_saveg.h b/src/p_saveg.h index 73fcfd583..545008e7e 100644 --- a/src/p_saveg.h +++ b/src/p_saveg.h @@ -18,7 +18,7 @@ #pragma interface #endif -#define NEWSKINSAVES (INT16_MAX) // Purely for backwards compatibility, remove this for 2.3 +#define NEWSKINSAVES (INT16_MAX) // TODO: 2.3: Delete (Purely for backwards compatibility) // Persistent storage/archiving. // These are the load / save game routines. From 8608ded317c46e435dcfa2fb1936bfd963e1ec18 Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Sat, 28 Oct 2023 15:19:35 +0200 Subject: [PATCH 8/8] Add deprecation TODOs --- src/deh_soc.c | 2 ++ src/lua_baselib.c | 2 ++ src/lua_consolelib.c | 1 + src/lua_infolib.c | 3 +++ src/lua_maplib.c | 2 ++ src/lua_mathlib.c | 1 + src/p_setup.c | 9 +++++++-- src/p_spec.c | 4 +++- src/p_user.c | 13 +++++++------ 9 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/deh_soc.c b/src/deh_soc.c index 59eb0a9bd..6162034de 100644 --- a/src/deh_soc.c +++ b/src/deh_soc.c @@ -911,6 +911,7 @@ static void readspriteframe(MYFILE *f, spriteinfo_t *sprinfo, UINT8 frame) sprinfo->pivot[frame].x = value; else if (fastcmp(word, "YPIVOT")) sprinfo->pivot[frame].y = value; + // TODO: 2.3: Delete else if (fastcmp(word, "ROTAXIS")) deh_warning("SpriteInfo: ROTAXIS is deprecated and will be removed."); else @@ -1617,6 +1618,7 @@ void readlevelheader(MYFILE *f, INT32 num) sizeof(mapheaderinfo[num-1]->musname), va("Level header %d: music", num)); } } + // TODO: 2.3: Delete else if (fastcmp(word, "MUSICSLOT")) deh_warning("Level header %d: MusicSlot parameter is deprecated and will be removed.\nUse \"Music\" instead.", num); else if (fastcmp(word, "MUSICTRACK")) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index f7ed763a9..c5ed4ddb6 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -1788,6 +1788,7 @@ static int lib_pMove(lua_State *L) return 2; } +// TODO: 2.3: Delete static int lib_pTeleportMove(lua_State *L) { mobj_t *ptmthing = tmthing; @@ -2266,6 +2267,7 @@ static int lib_pMobjTouchingSectorSpecial(lua_State *L) return 1; } +// TODO: 2.3: Delete static int lib_pThingOnSpecial3DFloor(lua_State *L) { mobj_t *mo = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ)); diff --git a/src/lua_consolelib.c b/src/lua_consolelib.c index b34105004..c281b23b3 100644 --- a/src/lua_consolelib.c +++ b/src/lua_consolelib.c @@ -194,6 +194,7 @@ static int lib_comAddCommand(lua_State *L) if (lua_gettop(L) >= 3) { // For the third argument, only take a boolean or a number. lua_settop(L, 3); + // TODO: 2.3: Remove boolean option if (lua_type(L, 3) == LUA_TBOOLEAN) { CONS_Alert(CONS_WARNING, diff --git a/src/lua_infolib.c b/src/lua_infolib.c index 020eba152..ed78811ce 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -318,6 +318,7 @@ static int PopPivotSubTable(spriteframepivot_t *pivot, lua_State *L, int stk, in pivot[idx].x = (INT32)value; else if (ikey == 2 || (key && fastcmp(key, "y"))) pivot[idx].y = (INT32)value; + // TODO: 2.3: Delete else if (ikey == 3 || (key && fastcmp(key, "rotaxis"))) LUA_UsageWarning(L, "\"rotaxis\" is deprecated and will be removed.") else if (ikey == -1 && (key != NULL)) @@ -571,6 +572,7 @@ static int framepivot_get(lua_State *L) lua_pushinteger(L, framepivot->x); else if (fastcmp("y", field)) lua_pushinteger(L, framepivot->y); + // TODO: 2.3: Delete else if (fastcmp("rotaxis", field)) { LUA_UsageWarning(L, "\"rotaxis\" is deprecated and will be removed."); @@ -600,6 +602,7 @@ static int framepivot_set(lua_State *L) framepivot->x = luaL_checkinteger(L, 3); else if (fastcmp("y", field)) framepivot->y = luaL_checkinteger(L, 3); + // TODO: 2.3: delete else if (fastcmp("rotaxis", field)) LUA_UsageWarning(L, "\"rotaxis\" is deprecated and will be removed.") else diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 588a6b4e6..df06e9721 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -1098,6 +1098,7 @@ static int line_get(lua_State *L) case line_polyobj: LUA_PushUserdata(L, line->polyobj, META_POLYOBJ); return 1; + // TODO: 2.3: Delete case line_text: { if (udmf) @@ -1231,6 +1232,7 @@ static int side_get(lua_State *L) case side_repeatcnt: lua_pushinteger(L, side->repeatcnt); return 1; + // TODO: 2.3: Delete case side_text: { if (udmf) diff --git a/src/lua_mathlib.c b/src/lua_mathlib.c index d0fe6863f..1bc6019de 100644 --- a/src/lua_mathlib.c +++ b/src/lua_mathlib.c @@ -125,6 +125,7 @@ static int lib_fixeddiv(lua_State *L) return 1; } +// TODO: 2.3: Delete static int lib_fixedrem(lua_State *L) { LUA_Deprecated(L, "FixedRem(a, b)", "a % b"); diff --git a/src/p_setup.c b/src/p_setup.c index 0390761b6..7f6fcd36c 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -1679,7 +1679,7 @@ static void ParseTextmapSectorParameter(UINT32 i, const char *param, const char if ((id = strchr(id, ' '))) id++; } - } + } else if (fastcmp(param, "xpanningfloor")) sectors[i].floorxoffset = FLOAT_TO_FIXED(atof(val)); else if (fastcmp(param, "ypanningfloor")) @@ -6132,6 +6132,7 @@ static void P_ConvertBinarySectorTypes(void) case 14: //Non-ramp sector sectors[i].specialflags |= SSF_NOSTEPDOWN; break; + // TODO: 2.3: Delete case 15: //Bouncy FOF CONS_Alert(CONS_WARNING, M_GetText("Deprecated bouncy FOF sector type detected. Please use linedef type 76 instead.\n")); break; @@ -6166,12 +6167,14 @@ static void P_ConvertBinarySectorTypes(void) sectors[i].flags |= MSF_TRIGGERLINE_PLANE; sectors[i].triggerer = TO_PLAYER; break; + // TODO: 2.3: Delete case 6: //Trigger linedef executor (Emerald check) CONS_Alert(CONS_WARNING, M_GetText("Deprecated emerald check sector type detected. Please use linedef types 337-339 instead.\n")); sectors[i].triggertag = tag; sectors[i].flags &= ~MSF_TRIGGERLINE_PLANE; sectors[i].triggerer = TO_PLAYEREMERALDS; break; + // TODO: 2.3: Delete case 7: //Trigger linedef executor (NiGHTS mare) CONS_Alert(CONS_WARNING, M_GetText("Deprecated NiGHTS mare sector type detected. Please use linedef types 340-342 instead.\n")); sectors[i].triggertag = tag; @@ -6181,9 +6184,11 @@ static void P_ConvertBinarySectorTypes(void) case 8: //Check for linedef executor on FOFs sectors[i].flags |= MSF_TRIGGERLINE_MOBJ; break; + // TODO: 2.3: Delete case 10: //Special stage time/spheres requirements CONS_Alert(CONS_WARNING, M_GetText("Deprecated sector type for special stage requirements detected. Please use the SpecialStageTime and SpecialStageSpheres level header options instead.\n")); break; + // TODO: 2.3: Delete case 11: //Custom global gravity CONS_Alert(CONS_WARNING, M_GetText("Deprecated sector type for global gravity detected. Please use the Gravity level header option instead.\n")); break; @@ -6822,7 +6827,7 @@ static void P_ConvertBinaryThingTypes(void) default: break; } - + // Clear binary thing height hacks, to prevent interfering with UDMF-only flags mapthings[i].options &= 0xF; } diff --git a/src/p_spec.c b/src/p_spec.c index 28ecc60f4..aa4ee37cf 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -2661,7 +2661,7 @@ static void P_ProcessLineSpecial(line_t *line, mobj_t *mo, sector_t *callsec) titlemapcameraref = altview; else if (!mo->player->awayviewtics || mo->player->awayviewmobj != altview) { P_SetTarget(&mo->player->awayviewmobj, altview); - + if (mo->player == &players[displayplayer]) P_ResetCamera(mo->player, &camera); // reset p1 camera on p1 getting an awayviewmobj else if (splitscreen && mo->player == &players[secondarydisplayplayer]) @@ -4178,6 +4178,7 @@ sector_t *P_MobjTouchingSectorSpecial(mobj_t *mo, INT32 section, INT32 number) return NULL; } +// TODO: 2.3: Delete // Deprecated in favor of P_MobjTouchingSectorSpecial // Kept for Lua backwards compatibility only sector_t *P_ThingOnSpecial3DFloor(mobj_t *mo) @@ -6229,6 +6230,7 @@ void P_SpawnSpecials(boolean fromnetsave) sector->flags |= MSF_TRIGGERSPECIAL_TOUCH; } + // TODO: 2.3: Delete everything below // Process deprecated binary sector specials if (udmf || !sector->special) continue; diff --git a/src/p_user.c b/src/p_user.c index 5730d476d..453a10b5b 100644 --- a/src/p_user.c +++ b/src/p_user.c @@ -2775,6 +2775,7 @@ static void P_CheckBouncySectors(player_t *player) if (!(rover->fofflags & FOF_EXISTS)) continue; // FOFs should not be bouncy if they don't even "exist" + // TODO: 2.3: Delete // Handle deprecated bouncy FOF sector type if (!udmf && GETSECSPECIAL(rover->master->frontsector->special, 1) == 15) { @@ -11390,7 +11391,7 @@ static void P_DoTailsOverlay(player_t *player, mobj_t *tails) tails->y = player->mo->y + P_ReturnThrustY(tails, tails->angle, FixedMul(backwards, tails->scale)); tails->z = player->mo->z + zoffs; P_SetThingPosition(tails); - + if (player->mo->flags2 & MF2_SHADOW) tails->flags2 |= MF2_SHADOW; else @@ -11606,7 +11607,7 @@ void P_PlayerThink(player_t *player) P_SetTarget(&player->awayviewmobj, NULL); // remove awayviewmobj asap if invalid player->awayviewtics = 1; // reset to one, the below code will immediately set it to zero } - + if (player->awayviewtics && player->awayviewtics != -1) { player->awayviewtics--; @@ -12278,11 +12279,11 @@ void P_PlayerThink(player_t *player) if (!(player->stronganim)) player->stronganim = player->panim; else if (player->panim != player->stronganim) - player->powers[pw_strong] = STR_NONE; - } + player->powers[pw_strong] = STR_NONE; + } else if (player->stronganim) player->stronganim = 0; - + //pw_super acts as a timer now if (player->powers[pw_super] && (player->mo->state < &states[S_PLAY_SUPER_TRANS1] @@ -12389,7 +12390,7 @@ void P_PlayerThink(player_t *player) if (player->jumpfactor < FixedMul(skins[player->skin].jumpfactor, 5*FRACUNIT/4)) // Boost jump height. player->jumpfactor += FRACUNIT/300; - if ((player->charflags & SF_MACHINE) && (!(player->powers[pw_strong] == STR_METAL))) + if ((player->charflags & SF_MACHINE) && (!(player->powers[pw_strong] == STR_METAL))) player->powers[pw_strong] = STR_METAL; }