From 23268bf3e53abfe0667465631a4027d1f56cd49d Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Fri, 27 Oct 2023 16:29:47 +0200 Subject: [PATCH] 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;