#include "lua/api.h" #include "classes.h" // Pushes field name of the table at index without metamethods. static int field_raw_get(lua_State* state, int index, const char* name) { lua_pushstring(state, name); return lua_rawget(state, index < 0 ? index - 1 : index); } // (0085bf90, 0085da20) Sets field name of the table at index to the value on top, popping it, without metamethods. void lua_table_set(lua_State* state, int table, const char* name) { lua_pushstring(state, name); lua_insert(state, -2); lua_rawset(state, table < 0 ? table - 1 : table); } // Sets a key of the begun class's __propget or __propset to the function on top, popping it. void lua_property_set(lua_State* state, const char* table, const char* name) { field_raw_get(state, -2, table); lua_insert(state, -2); lua_table_set(state, -2, name); lua_pop(state, 1); } // (0085c810, 0085cd40) __index of objects, classes and namespaces: a method or field of the metatable, else a property // getter, else the same in each parent class, else nil. static int index_get(lua_State* state) { lua_getmetatable(state, 1); while (true) { lua_pushvalue(state, 2); if (lua_rawget(state, -2) != LUA_TNIL) return 1; lua_pop(state, 1); if (field_raw_get(state, -1, "__propget") == LUA_TTABLE) { lua_pushvalue(state, 2); if (lua_rawget(state, -2) == LUA_TFUNCTION) { lua_pushvalue(state, 1); lua_call(state, 1, 1); return 1; } lua_pop(state, 1); } lua_pop(state, 1); if (field_raw_get(state, -1, "__parent") == LUA_TNIL) return 1; lua_remove(state, -2); } } // (0085c970, 0085cf30, 0085cad0) __newindex of objects, classes and namespaces: a property setter of the class or a // parent. Anything else raises an error, so scripts add to namespaces with rawset. static int index_set(lua_State* state) { lua_getmetatable(state, 1); while (true) { if (field_raw_get(state, -1, "__propset") == LUA_TTABLE) { lua_pushvalue(state, 2); if (lua_rawget(state, -2) == LUA_TFUNCTION) { lua_pushvalue(state, 1); lua_pushvalue(state, 3); lua_call(state, 2, 0); return 0; } lua_pop(state, 1); } lua_pop(state, 1); if (field_raw_get(state, -1, "__parent") == LUA_TNIL) return luaL_error(state, "no writable variable '%s'", lua_tostring(state, 2)); lua_remove(state, -2); } } // (0085c7e0) Pushes an object as a userdata holding its pointer, with its class's metatable, or nil for none. void lua_object_push(lua_State* state, void* object, LuaClass luaClass) { if (!object) return lua_pushnil(state); *(void**)lua_newuserdata(state, sizeof(void*)) = object; lua_rawgetp(state, LUA_REGISTRYINDEX, lua_class_key(luaClass)); lua_setmetatable(state, -2); } // (0085c590, 0085c3e0) The object argument index holds when it is of the class or one derived from it. Raises an // argument error if not. void* lua_object_check(lua_State* state, int index, LuaClass luaClass) { void** block = lua_type(state, index) == LUA_TUSERDATA ? (void**)lua_touserdata(state, index) : nullptr; const char* actual = luaL_typename(state, index); lua_rawgetp(state, LUA_REGISTRYINDEX, lua_class_key(luaClass)); if (!block || !lua_getmetatable(state, index)) lua_pushnil(state); while (!lua_isnil(state, -1)) { if (lua_rawequal(state, -1, -2)) { lua_pop(state, 2); return *block; } field_raw_get(state, -1, "__parent"); lua_remove(state, -2); } field_raw_get(state, -2, "__name"); const char* expected = lua_pushfstring(state, "%s expected, got %s", lua_tostring(state, -1), actual); luaL_argerror(state, index, expected); return nullptr; } // Gives the table on top the fields every class, static table and namespace has. static void table_init(lua_State* state, const char* name) { lua_pushcfunction(state, index_get); lua_table_set(state, -2, "__index"); lua_pushcfunction(state, index_set); lua_table_set(state, -2, "__newindex"); lua_newtable(state); lua_table_set(state, -2, "__propget"); lua_newtable(state); lua_table_set(state, -2, "__propset"); lua_pushstring(state, name); lua_table_set(state, -2, "__name"); } // (0085d040, 0085d2c0, 0085d500) Begins a class as LuaBridge lays one out: a static table that is its own metatable, holding the constructor and // static functions with __class pointing at the class table, the metatable its objects get. The class table keeps the // methods and properties, with __parent pointing at the parent class's. Leaves the static and class tables on top. void lua_class_begin(lua_State* state, const char* name, LuaClass luaClass, LuaClass parent) { lua_newtable(state); lua_pushvalue(state, -1); lua_setmetatable(state, -2); table_init(state, name); lua_newtable(state); table_init(state, name); if (parent != LUA_CLASS_COUNT) { lua_rawgetp(state, LUA_REGISTRYINDEX, lua_class_key(parent)); lua_table_set(state, -2, "__parent"); } lua_pushvalue(state, -1); lua_table_set(state, -3, "__class"); lua_pushvalue(state, -1); lua_rawsetp(state, LUA_REGISTRYINDEX, lua_class_key(luaClass)); } // (0085d730, 0085da00) Begins a namespace: a table that is its own metatable, left on top twice so functions and properties are added to // it as to a class. void lua_namespace_begin(lua_State* state, const char* name) { lua_newtable(state); lua_pushvalue(state, -1); lua_setmetatable(state, -2); table_init(state, name); lua_pushvalue(state, -1); } // (0085d910, 0085d980, 0085cce0, 00872510) Ends the class or namespace begun last, naming it as a global or in the namespace below it. void lua_class_end(lua_State* state, LuaScope scope) { field_raw_get(state, -1, "__name"); std::string name = lua_tostring(state, -1); lua_pop(state, 2); if (scope == LUA_SCOPE_GLOBAL) lua_setglobal(state, name.c_str()); else lua_table_set(state, -2, name.c_str()); }