Merge branch SRB2:next into custom-2.2.10

This commit is contained in:
Logan Aerl Arias 2023-12-31 20:17:32 +00:00
commit 8dcc609144

View file

@ -600,14 +600,9 @@ static int ScanConstants(lua_State *L, boolean mathlib, const char *word)
return 0;
}
static inline int lib_getenum(lua_State *L)
static inline int getEnum(lua_State *L, boolean mathlib, const char *word)
{
const char *word;
fixed_t i;
boolean mathlib = lua_toboolean(L, lua_upvalueindex(1));
if (lua_type(L,2) != LUA_TSTRING)
return 0;
word = lua_tostring(L,2);
// check actions, super and globals first, as they don't have _G caching implemented
// so they benefit from being checked first
@ -674,6 +669,46 @@ static inline int lib_getenum(lua_State *L)
else if ((!mathlib && LUA_PushGlobals(L, word)) || ScanConstants(L, mathlib, word))
return 1;
return -1;
}
static int constants_get(lua_State *L)
{
const char *key;
int ret;
if (!lua_isstring(L, 2))
return 0;
key = luaL_checkstring(L, 2);
// In Lua, mathlib is never there
ret = getEnum(L, false, key);
if (ret != -1)
// Don't allow A_* or super.
// All userdata is meant to be considered global variables,
// so no need to get more specific than "is it userdata?"
if (!lua_isuserdata(L, -1) && !lua_isfunction(L, -1))
return ret;
return 0;
}
static inline int lib_getenum(lua_State *L)
{
const char *word;
int ret;
boolean mathlib = lua_toboolean(L, lua_upvalueindex(1));
if (lua_type(L,2) != LUA_TSTRING)
return 0;
word = lua_tostring(L,2);
ret = getEnum(L, mathlib, word);
if (ret != -1)
return ret;
if (mathlib) return luaL_error(L, "constant '%s' could not be parsed.\n", word);
return 0;
@ -776,6 +811,15 @@ int LUA_SOCLib(lua_State *L)
LUA_SetCFunctionField(L, "__call", action_call);
lua_pop(L, 1);
// Allow access to constants without forcing the use of name comparison checks Lua-side
// This table will not access global variables, only constants
lua_newuserdata(L, 0);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, constants_get);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_setglobal(L, "constants");
return 0;
}