From ccb1d3bc80aa8b6a198535cb916ab28edb17c281 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:34:49 +0300 Subject: [PATCH 01/20] add SHARED macro --- include/global.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/global.h b/include/global.h index 6ec84fe..eff5d78 100644 --- a/include/global.h +++ b/include/global.h @@ -4,6 +4,8 @@ #include "../workflow.cpp" #include +#define SHARED extern "C" + #define GPtr(addr, type) \ (*(type*)addr) From fe2159188afb02c360a7712facc55f90c33fc4f4 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:39:17 +0300 Subject: [PATCH 02/20] remove clone and build by clang --- section/Lua/{TableFuncs.cpp => TableFuncs.cxx} | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) rename section/Lua/{TableFuncs.cpp => TableFuncs.cxx} (93%) diff --git a/section/Lua/TableFuncs.cpp b/section/Lua/TableFuncs.cxx similarity index 93% rename from section/Lua/TableFuncs.cpp rename to section/Lua/TableFuncs.cxx index e923aa2..1b6898d 100644 --- a/section/Lua/TableFuncs.cpp +++ b/section/Lua/TableFuncs.cxx @@ -126,15 +126,6 @@ int lua_tableempty(lua_State *L) return 1; } -int TableClone(lua_State *L) -{ - LuaObject obj{L->LuaState, 1}; - LuaObject cloned{}; - obj.Clone(&cloned); - cloned.PushStack(L); - return 1; -} - // UI_Lua reprsl({table.unpack2({1,2,3,4},2,3)}) // UI_Lua reprsl({table.unpack2({1,2,3,4},2)}) // UI_Lua reprsl({table.unpack2({1,2,3,4})}) @@ -162,13 +153,12 @@ int lua_unpack(lua_State *l) const luaL_reg RegTableFuncsDesc[] = {{"getsize", &lua_tablesize}, {"empty", &lua_tableempty}, - {"clone", &TableClone}, {"unpack", &lua_unpack}, {nullptr, nullptr}}; extern const luaL_reg original_table_funcs[] asm("0x00D47418"); -int __cdecl lua_openlibtable(lua_State *L) +SHARED int __cdecl lua_openlibtable(lua_State *L) { luaL_openlib(L, "table", original_table_funcs, 0); luaL_openlib(L, "table", RegTableFuncsDesc, 0); From 045a06c8e454a1ac30c06d0cdfac5b188457cd28 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:47:48 +0300 Subject: [PATCH 03/20] update LuaAPI header --- include/LuaAPI.h | 841 ++++++++++++++++++++++++++--------------------- 1 file changed, 471 insertions(+), 370 deletions(-) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 7de799e..c195821 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -2,30 +2,33 @@ #include "global.h" -#define LUA_IDSIZE 60 +#define LUA_IDSIZE 60 struct lua_Debug { int event; - const char *name; // (n) - const char *namewhat; // (n) 'global', 'local', 'field', 'method' - const char *what; // (S) 'Lua', 'C', 'main', 'tail' - const char *source; // (S) - int currentline; // (l) - int nups; // (u) number of upvalues - int linedefined; // (S) - char short_src[LUA_IDSIZE]; // (S) + const char *name; // (n) + const char *namewhat; // (n) 'global', 'local', 'field', 'method' + const char *what; // (S) 'Lua', 'C', 'main', 'tail' + const char *source; // (S) + int currentline; // (l) + int nups; // (u) number of upvalues + int linedefined; // (S) + char short_src[LUA_IDSIZE]; // (S) /* private part */ - int i_ci; // active function + int i_ci; // active function }; class LuaState; -//lua.org/source/5.0/lstate.h.html#lua_State -typedef struct {uint8_t pad[0x44]; LuaState* LuaState;} lua_State; +// lua.org/source/5.0/lstate.h.html#lua_State +typedef struct { + uint8_t pad[0x44]; + LuaState *LuaState; +} lua_State; typedef struct luaL_Buffer luaL_Buffer; -typedef const char* (*lua_Chunkreader)(lua_State *L, void *data, size_t *size); -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); -typedef void (*userGCFunction)(void*); +typedef const char *(*lua_Chunkreader)(lua_State *L, void *data, size_t *size); +typedef void (*lua_Hook)(lua_State *L, lua_Debug *ar); +typedef void (*userGCFunction)(void *); typedef int (*lua_CFunction)(lua_State *L); typedef float lua_Number; @@ -34,86 +37,89 @@ typedef struct luaL_reg { lua_CFunction func; } luaL_reg; -#define LUA_MULTRET (-1) - -#define LUA_REGISTRYINDEX (-10000) -#define LUA_GLOBALSINDEX (-10001) -#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) - -#define LUA_OK 0 -#define LUA_ERRRUN 1 -#define LUA_ERRFILE 2 -#define LUA_ERRSYNTAX 3 -#define LUA_ERRMEM 4 -#define LUA_ERRERR 5 - -#define LUA_TNONE (-1) -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_CFUNCTION 6 -#define LUA_TFUNCTION 7 -#define LUA_TUSERDATA 8 -#define LUA_TTHREAD 9 -#define LUA_TPROTO 10 -#define LUA_TUPVALUE 11 - -#define LUA_HOOKCALL 0 -#define LUA_HOOKRET 1 -#define LUA_HOOKLINE 2 -#define LUA_HOOKCOUNT 3 -#define LUA_HOOKTAILRET 4 - -#define LUA_MASKCALL (1 << LUA_HOOKCALL) -#define LUA_MASKRET (1 << LUA_HOOKRET) -#define LUA_MASKLINE (1 << LUA_HOOKLINE) -#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) - -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) - -#define lua_boxpointer(L,u) \ +#define LUA_MULTRET (-1) + +#define LUA_REGISTRYINDEX (-10000) +#define LUA_GLOBALSINDEX (-10001) +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX - (i)) + +#define LUA_OK 0 +#define LUA_ERRRUN 1 +#define LUA_ERRFILE 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRERR 5 + +#define LUA_TNONE (-1) +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_CFUNCTION 6 +#define LUA_TFUNCTION 7 +#define LUA_TUSERDATA 8 +#define LUA_TTHREAD 9 +#define LUA_TPROTO 10 +#define LUA_TUPVALUE 11 + +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILRET 4 + +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +#define lua_boxpointer(L, u) \ (*(void **)(lua_newuserdata(L, sizeof(void *))) = (u)) -#define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i))) - -#define lua_pop(L,n) lua_settop(L, -(n)-1) - -#define lua_register(L,n,f) \ - (lua_pushstring(L, n), \ - lua_pushcfunction(L, f), \ - lua_settable(L, LUA_GLOBALSINDEX)) - -#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0) - -#define lua_isnoneornil(L,n) (lua_type(L,n) <= 0) -#define lua_isnone(L,n) (lua_type(L,n) == LUA_TNONE) -#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL) -#define lua_isboolean(L,n) (lua_type(L,n) == LUA_TBOOLEAN) -#define lua_islightuserdata(L,n)(lua_type(L,n) == LUA_TLIGHTUSERDATA) -#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE) -#define lua_iscfunction(L,n) (lua_type(L,n) == LUA_CFUNCTION) -#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION) -#define lua_isuserdata(L,n) (lua_type(L,n) == LUA_TUSERDATA) -#define lua_isthread(L,n) (lua_type(L,n) == LUA_TTHREAD) -#define lua_isproto(L,n) (lua_type(L,n) == LUA_TPROTO) -#define lua_isupvalue(L,n) (lua_type(L,n) == LUA_TUPVALUE) - -#define lua_pushliteral(L, s) \ - lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) - -#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) -#define lua_setglobal(L,s) \ +#define lua_unboxpointer(L, i) (*(void **)(lua_touserdata(L, i))) + +#define lua_pop(L, n) lua_settop(L, -(n) - 1) + +#define lua_register(L, n, f) \ + (lua_pushstring(L, n), lua_pushcfunction(L, f), \ + lua_settable(L, LUA_GLOBALSINDEX)) + +#define lua_pushcfunction(L, f) lua_pushcclosure(L, f, 0) + +#define lua_isnoneornil(L, n) (lua_type(L, n) <= 0) +#define lua_isnone(L, n) (lua_type(L, n) == LUA_TNONE) +#define lua_isnil(L, n) (lua_type(L, n) == LUA_TNIL) +#define lua_isboolean(L, n) (lua_type(L, n) == LUA_TBOOLEAN) +#define lua_islightuserdata(L, n) (lua_type(L, n) == LUA_TLIGHTUSERDATA) +#define lua_istable(L, n) (lua_type(L, n) == LUA_TTABLE) +#define lua_iscfunction(L, n) (lua_type(L, n) == LUA_CFUNCTION) +#define lua_isfunction(L, n) (lua_type(L, n) == LUA_TFUNCTION) +#define lua_isuserdata(L, n) (lua_type(L, n) == LUA_TUSERDATA) +#define lua_isthread(L, n) (lua_type(L, n) == LUA_TTHREAD) +#define lua_isproto(L, n) (lua_type(L, n) == LUA_TPROTO) +#define lua_isupvalue(L, n) (lua_type(L, n) == LUA_TUPVALUE) + +#define lua_pushliteral(L, s) \ + lua_pushlstring(L, "" s, (sizeof(s) / sizeof(char)) - 1) + +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) +#define lua_setglobal(L, s) \ (lua_pushstring(L, s), lua_insert(L, -2), lua_settable(L, LUA_GLOBALSINDEX)) -#define lua_getglobal(L,s) \ +#define lua_getglobal(L, s) \ (lua_pushstring(L, s), lua_gettable(L, LUA_GLOBALSINDEX)) -#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ - (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) -#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) -#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref) +#define lua_ref(L, lock) \ + ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) \ + : (lua_pushstring(L, "unlocked references are obsolete"), \ + lua_error(L), 0)) +#define lua_unref(L, ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) +#define lua_getref(L, ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref) +#define abs_index(L, i) \ + ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1) typedef union { void *p; @@ -121,289 +127,384 @@ typedef union { int b; } Value; -//lua.org/source/5.0/lobject.h.html#TObject -typedef struct { +void GetTableArrAndHash(const Value *t, int &narr, unsigned int &nhash) { + narr = *(int *)(t->b + 32); + nhash = *(unsigned char *)(t->b + 9); +} + +// lua.org/source/5.0/lobject.h.html#TObject +struct TObject { int tt; Value value; -} TObject; + + inline TObject(int value) : tt{LUA_TNUMBER} { this->value.n = value; } + inline TObject(float value) : tt{LUA_TNUMBER} { this->value.n = value; } + inline TObject(bool value) : tt{LUA_TBOOLEAN} { this->value.b = value; } + inline TObject() : tt{LUA_TNIL} {} +}; + VALIDATE_SIZE(TObject, 8) -//namespace gpg - class RRef {public: void* d; void* t;}; - class RType {}; - -//namespace LuaPlus - class LuaState; - - class LuaStackObject { - public: - LuaState* m_state; - int m_stackIndex; - }; - VALIDATE_SIZE(LuaStackObject, 8) - - class LuaObject - {// 0x14 bytes - void __LuaObject() asm("0x9072a0"); - void __LuaObject(LuaState* state) asm("0x908970"); - void __LuaObject(LuaState* state, int index) asm("0x9089c0"); - void __LuaObject(const LuaObject* obj) asm("0x908a40"); - void __LuaObject(const LuaStackObject* stack) asm("0x908a70"); - void __LuaObject(LuaState* state, const TObject* obj) asm("0x9089f0"); - void __DLuaObject() asm("0x9075d0"); - void __Index(LuaObject *out, int key) asm("0x9091e0"); - void __Index(LuaObject *out, const char* key) asm("0x908f60"); - - public: - LuaObject() {__LuaObject();} - LuaObject(LuaState* state) {__LuaObject(state);} - LuaObject(LuaState* state, int index) {__LuaObject(state, index);} - LuaObject(const LuaObject* obj) {__LuaObject(obj);} - LuaObject(const LuaStackObject* stack) {__LuaObject(stack);} - LuaObject(LuaState* state, const TObject* obj) {__LuaObject(state, obj);} - ~LuaObject() {__DLuaObject();} - - LuaObject* operator=(const LuaObject* obj) asm("0x908ab0"); - LuaObject* operator=(const LuaStackObject* stack) asm("0x908b00"); - - LuaObject operator[](int key) { - LuaObject out; __Index(&out, key); return out; - } - LuaObject operator[](const char* key) { - LuaObject out; __Index(&out, key); return out; - } - - bool GetBoolean() asm("0x907c90"); - bool IsBoolean() asm("0x9078d0"); - bool IsConvertibleToString() asm("0x9077c0"); - bool IsFunction() asm("0x907810"); - bool IsInteger() asm("0x907350"); - bool IsNil() asm("0x9072f0"); - bool IsNumber() asm("0x907360"); - bool IsString() asm("0x907370"); - bool IsTable() asm("0x907310"); - bool IsUserData() asm("0x907320"); - void Clone(LuaObject* out) asm("0x90a180"); - void CreateTable(LuaObject* out, const char* key, int narray, int lnhash) asm("0x908c10"); - void CreateTable(LuaObject* out, int key, int narray, int lnhash) asm("0x908ca0"); - void GetByIndex(LuaObject* out, int index) asm("0x908df0"); - void GetByName(LuaObject* out, const char* name) asm("0x90a160"); - void GetByObject(LuaObject* out, const LuaObject* obj) asm("0x908e70"); - void GetMetaTable(LuaObject* out) asm("0x908ba0"); - void Lookup(LuaObject* out, const char* key) asm("0x9093b0"); - void PushStack(LuaStackObject* out, LuaState* state) asm("0x907d80"); - void PushStack(lua_State* L) asm("0x907d10"); - LuaState* GetActiveState() asm("0x9072b0"); - const char* GetString() asm("0x907a90"); - const char* ToString() asm("0x9073e0"); - const char* TypeName() asm("0x908b50"); - lua_Number GetNumber() asm("0x907970"); - lua_Number GetDouble() asm("0x907a30"); - lua_Number ToNumber() asm("0x9073b0"); - void AssignNewUserData(RRef* out, LuaState* state, const RRef* rRef) asm("0x909840"); - void AssignNewUserData(RRef* out, LuaState* state, const RType* rType) asm("0x9097d0"); - void GetUserData(RRef* out) asm("0x907bc0"); - int GetCount() asm("0x907f50"); - int GetInteger() asm("0x907910"); - int GetN() asm("0x907e50"); - int GetTableCount() asm("0x90a410"); - int IsPassed() asm("0x907440"); - int Type() asm("0x9076d0"); - lua_State* GetActiveCState() asm("0x9072c0"); - void AssignBoolean(LuaState* state, bool value) asm("0x909600"); - void AssignInteger(LuaState* state, int value) asm("0x909650"); - void AssignNewTable(LuaState* state, int narray, int lnhash) asm("0x909940"); - void AssignNil(LuaState* state) asm("0x9095c0"); - void AssignNumber(LuaState* state, float value) asm("0x9096a0"); - void AssignString(LuaState* state, const char* value) asm("0x909750"); - void AssignTObject(LuaState* state, const TObject* value) asm("0x9099b0"); - void AssignThread(LuaState* state) asm("0x9096f0"); - void Insert(LuaObject* value) asm("0x909af0"); - void Insert(int key, LuaObject* value) asm("0x909ce0"); - void Register(const char* name, lua_CFunction func, int nupvalues) asm("0x907630"); - void Reset() asm("0x9075f0"); - void SetBoolean(const char* key, bool value) asm("0x9080c0"); - void SetInteger(const char* key, int value) asm("0x9081f0"); - void SetInteger(int key, int value) asm("0x908240"); - void SetMetaTable(const LuaObject* value) asm("0x907e00"); - void SetN(int n) asm("0x907ed0"); - void SetNil(const char* key) asm("0x907fa0"); - void SetNil(int key) asm("0x907ff0"); - void SetNumber(const char* key, float value) asm("0x908320"); - void SetNumber(int key, float value) asm("0x908370"); - void SetObject(const LuaObject* key, const LuaObject* value) asm("0x908810"); - void SetObject(const char* key, const LuaObject* value) asm("0x908760"); - void SetObject(int key, const LuaObject* value) asm("0x9087a0"); - void SetString(const char* key, const char* value) asm("0x908450"); - void SetString(int key, const char* value) asm("0x9084e0"); - void TypeError(const char* msg) asm("0x9072d0"); - - //private - void AddToUsedList(LuaState* state) asm("0x908890"); - void AddToUsedList(LuaState* state, const TObject* obj) asm("0x9088e0"); - void SetTableHelper(const char* key, const TObject* value) asm("0x9074b0"); - - LuaObject* m_next; - LuaObject* m_prev; - LuaState* m_state; - TObject m_object; - }; - VALIDATE_SIZE(LuaObject, 0x14) - - enum StandardLibraries {LIB_NONE, LIB_BASE, LIB_OSIO}; - class LuaState - {// 0x34 bytes - void __LuaState(StandardLibraries libs) asm("0x90ac10"); - void __LuaState(LuaState* parentState) asm("0x90a520"); - void __LuaState(int Unused) asm("0x90a5d0"); - void __DLuaState() asm("0x90a600"); - public: - LuaState(StandardLibraries libs) {__LuaState(libs);}; - LuaState(LuaState* parentState) {__LuaState(parentState);}; - LuaState(int Unused) {__LuaState(Unused);}; - ~LuaState() {__DLuaState();}; - void GetGlobal(LuaObject* out, const char* key) asm("0x4579d0"); - void GetGlobals(LuaObject* out) asm("0x90a690"); - LuaState* GetActiveState() asm("0x90bee0"); - const char* CheckString(int narg) asm("0x912d10"); - int ArgError(int narg, const char* msg) asm("0x90bf70"); - __cdecl int Error(const char* fmt, ...) asm("0x90c1d0"); - lua_State* GetActiveCState() asm("0x90bef0"); - void CheckAny(int narg) asm("0x923f20"); - - //private - void Init(enum StandardLibraries libs) asm("0x90aad0"); - - lua_State* m_state; - void* ForMultipleThreads; - bool m_ownState; - LuaObject m_threadObj; - LuaState* m_rootState; - struct MiniLuaObject { - LuaObject* m_next; // only valid when in free list - LuaObject* m_prev; // only valid when in used list - } m_headObject, m_tailObject; - }; - VALIDATE_SIZE(LuaState, 0x34) - -bool LuaPlusH_next(LuaState*, const LuaObject*, LuaObject*, LuaObject*) asm("0x90a6b0"); -bool lua_toboolean(lua_State*, int) asm("0x90ca40"); -TObject* negindex(lua_State*, int) asm("0x90c340"); -char* luaL_prepbuffer(luaL_Buffer*) asm("0x90e260"); -const char* luaL_checklstring(lua_State*, int, unsigned int*) asm("0x90eaa0"); -const char* luaL_optlstring(lua_State*, int, const char*, unsigned int*) asm("0x90eb10"); -const char* lua_getlocal(lua_State*, const lua_Debug*, int) asm("0x912680"); -const char* lua_getupvalue(lua_State*, int, int) asm("0x90d9a0"); -const char* lua_pushfstring(lua_State*, const char*, ...) asm("0x90ce90"); -const char* lua_pushvfstring(lua_State*, const char*, char*) asm("0x90ce50"); -const char* lua_setlocal(lua_State*, const lua_Debug*, int) asm("0x9126f0"); -const char* lua_setupvalue(lua_State*, int, int) asm("0x90da00"); -const char* lua_tostring(lua_State*, int) asm("0x90ca90"); -const char* lua_typename(lua_State*, int) asm("0x90c780"); -float luaL_checknumber(lua_State*, int) asm("0x90eb70"); -float luaL_optnumber(lua_State*, int, float) asm("0x90ebf0"); -float lua_tonumber(lua_State*, int) asm("0x90c9f0"); -void lua_newuserdata(RRef *ret, lua_State*, const RType*) asm("0x90d7e0"); -void lua_touserdata(RRef *ret, lua_State*, int) asm("0x90cbb0"); -int luaL_argerror(lua_State*, int, const char*) asm("0x90e900"); -int luaL_callmeta(lua_State*, int, const char*) asm("0x90dda0"); -int luaL_error(lua_State*, const char*, ...) asm("0x90dbf0"); -int luaL_findstring(const char*, const char* const list[]) asm("0x90dc20"); -int luaL_getmetafield(lua_State*, int, const char*) asm("0x90dd40"); -int luaL_getn(lua_State*, int) asm("0x90e090"); -int luaL_loadbuffer(lua_State*, const char*, unsigned int, const char*) asm("0x90e760"); -int luaL_loadfile(lua_State*, const char*) asm("0x90e5d0"); -int luaL_newmetatable(lua_State*, const char*) asm("0x90dc70"); -int luaL_typerror(lua_State*, int, const char*) asm("0x90e9a0"); -int lua_checkstack(lua_State*, int) asm("0x90c460"); -int lua_dobuffer(lua_State*, const char*, unsigned int, const char*) asm("0x90e870"); -int lua_dostring(lua_State*, const char*) asm("0x90e8d0"); -int lua_error(lua_State*) asm("0x90d6c0"); -int lua_getgccount(lua_State*) asm("0x90d660"); -int lua_getgcthreshold(lua_State*) asm("0x90d650"); -int lua_gethookcount(lua_State*) asm("0x9125d0"); -int lua_gethookmask(lua_State*) asm("0x9125c0"); -int lua_getinfo(lua_State*, const char*, lua_Debug*) asm("0x9132f0"); -int lua_getmetatable(lua_State*, int) asm("0x90d180"); -int lua_getn(lua_State*, int) asm("0x90ad30"); -int lua_getstack(lua_State*, int, lua_Debug*) asm("0x9125e0"); -int lua_gettop(lua_State*) asm("0x90c590"); -int lua_isnumber(lua_State*, int) asm("0x90c7a0"); -int lua_isstring(lua_State*, int) asm("0x90c800"); -int lua_lessthan(lua_State*, int, int) asm("0x90c980"); -int lua_load(lua_State*, lua_Chunkreader, void*, const char*) asm("0x90d5c0"); -int lua_next(lua_State*, int) asm("0x90d6d0"); -int lua_pcall(lua_State*, int, int) asm("0x90d430"); -int lua_rawequal(lua_State*, int, int) asm("0x90c890"); -int lua_resume(lua_State*, int) asm("0x914610"); -int lua_setfenv(lua_State*, int) asm("0x90d3b0"); -int lua_sethook(lua_State*, lua_Hook, int, int) asm("0x912560"); -int lua_setmetatable(lua_State*, int) asm("0x90d340"); -int lua_traceback(lua_State*, const char*, int) asm("0x911ea0"); -int lua_type(lua_State*, int) asm("0x90c740"); -int lua_yield(lua_State*, int) asm("0x913e40"); -int luaopen_base(lua_State*) asm("0x90fd90"); -int luaopen_debug(lua_State*) asm("0x9124c0"); -int luaopen_loadlib(lua_State*) asm("0x91a4b0"); -int luaopen_math(lua_State*) asm("0x91a110"); -int luaopen_serialize(lua_State*) asm("0x923690"); -int luaopen_string(lua_State*) asm("0x926ef0"); -lua_State* lua_newthread(lua_State*) asm("0x90c530"); -lua_State* lua_open() asm("0x9246d0"); -lua_State* lua_tothread(lua_State*, int) asm("0x90cc50"); -unsigned int lua_strlen(lua_State*, int) asm("0x90cb10"); -lua_Hook lua_gethook(lua_State*) asm("0x9125b0"); -void luaL_addlstring(luaL_Buffer*, const char*, unsigned int) asm("0x90e2a0"); -void luaL_addstring(luaL_Buffer*, const char*) asm("0x90e300"); -void luaL_addvalue(luaL_Buffer*) asm("0x90e370"); -void luaL_buffinit(lua_State*, luaL_Buffer*) asm("0x90e400"); -void luaL_checkany(lua_State*, int) asm("0x90ea70"); -void luaL_checkstack(lua_State*, int, const char*) asm("0x90dd10"); -void luaL_checktype(lua_State*, int, int) asm("0x90ea20"); -void luaL_getmetatable(lua_State*, const char*) asm("0x90dcf0"); -void luaL_openlib(lua_State*, const char*, const luaL_reg*, int) asm("0x90de00"); -void luaL_pushresult(luaL_Buffer*) asm("0x90e330"); -void luaL_setn(lua_State*, int, int) asm("0x90dfb0"); -void luaL_where(lua_State*, int) asm("0x90db80"); -void lua_call(lua_State*, int, int) asm("0x90d400"); -void lua_close(lua_State*) asm("0x9243e0"); -void lua_concat(lua_State*, int) asm("0x90d740"); -void lua_getfenv(lua_State*, int) asm("0x90d1f0"); -void lua_gettable(lua_State*, int) asm("0x90d000"); -void lua_insert(lua_State*, int) asm("0x90c640"); -void lua_newtable(lua_State*) asm("0x90d110"); -void lua_pushboolean(lua_State*, int) asm("0x90cf80"); -void lua_pushcclosure(lua_State*, lua_CFunction, int) asm("0x90ced0"); -void lua_pushlightuserdata(lua_State*, void*) asm("0x90cfc0"); -void lua_pushlstring(lua_State*, const char*, unsigned int) asm("0x90cd80"); -void lua_pushnil(lua_State*) asm("0x90cd00"); -void lua_pushnumber(lua_State*, float) asm("0x90cd40"); -void lua_pushstring(lua_State*, const char*) asm("0x90cdf0"); -void lua_pushvalue(lua_State*, int) asm("0x90c6e0"); -void lua_rawget(lua_State*, int) asm("0x90d050"); -void lua_rawgeti(lua_State*, int, int) asm("0x90d0a0"); -void lua_rawset(lua_State*, int) asm("0x90d2a0"); -void lua_rawseti(lua_State*, int, int) asm("0x90d2f0"); -void lua_remove(lua_State*, int) asm("0x90c5f0"); -void lua_replace(lua_State*, int) asm("0x90c690"); -void lua_setdefaultmetatable(lua_State*, int) asm("0x90ad00"); -void lua_setgcthreshold(lua_State*, int) asm("0x90d670"); -void lua_setglobaluserdata(lua_State*, void*) asm("0x924060"); -void lua_setstateuserdata(lua_State*, void*) asm("0x9240b0"); -void lua_settable(lua_State*, int) asm("0x90d260"); -void lua_settop(lua_State*, int) asm("0x90c5a0"); -void lua_setusergcfunction(lua_State*, userGCFunction) asm("0x924080"); -void lua_xmove(lua_State*, lua_State*, int) asm("0x90c4c0"); -void const* lua_topointer(lua_State*, int) asm("0x90cc90"); -void* lua_getglobaluserdata(lua_State*) asm("0x924050"); -void* lua_getstateuserdata(lua_State*) asm("0x9240a0"); -void* lua_tolightuserdata(lua_State*, int) asm("0x90cc10"); - -void GetTableAH(const void *t, uint32_t *asize, uint8_t *hbits) { - *asize = *(int*)(t + 32); - *hbits = *(uint8_t*)(t + 9); +namespace lua +{ + struct TObject + { + int tt; + void *value; + }; + + struct Node + { + TObject i_key; + TObject i_val; + Node *next; + }; + + struct Table + { + /* lua::GCObject*/ void *next; + uint8_t tt; + uint8_t marked; + uint16_t gap; + uint8_t flags; + uint8_t lsizenode; + // padding byte + // padding byte + Table *metatable; + TObject *array; + lua::Node *node; + lua::Node *firstfree; + /*lua::GCObject*/ void *gclist; + int sizearray; + }; + +} // namespace lua + +// namespace gpg +class RRef { +public: + void *d; + void *t; +}; +class RType {}; + +// namespace LuaPlus +class LuaState; + +class LuaStackObject { +public: + LuaState *m_state; + int m_stackIndex; +}; +VALIDATE_SIZE(LuaStackObject, 8) + +extern const char *luaT_typenames[] asm("0x00D474D8"); + +class LuaObject { // 0x14 bytes + void __Index(LuaObject *out, int key) const asm("0x9091e0"); + void __Index(LuaObject *out, const char *key) const asm("0x908f60"); + void __Clone(LuaObject *out) const asm("0x90a180"); + +public: + LuaObject(); + LuaObject(LuaState *state); + LuaObject(LuaState *state, int index); + LuaObject(const LuaObject &obj); + LuaObject(const LuaStackObject &stack); + LuaObject(LuaState *state, const TObject *obj); + ~LuaObject(); + + LuaObject &operator=(const LuaObject &obj); + LuaObject &operator=(const LuaStackObject &stack); + + LuaObject operator[](int key) const; + LuaObject operator[](const char *key) const; + + bool operator==(const LuaObject& right) const; + bool operator!=(const LuaObject& right) const; + + bool GetBoolean() asm("0x907c90"); + bool IsConvertibleToString() asm("0x9077c0"); + bool IsFunction() asm("0x907810"); + + inline int Type() const; + inline bool IsNil() const; + inline bool IsBoolean() const; + inline bool IsInteger() const; + inline bool IsNumber() const; + inline bool IsString() const; + inline bool IsTable() const; + inline bool IsUserData() const; + + LuaObject GetMetaTable()const; + LuaObject Clone() const; + LuaObject DeepCopy() const; + void Insert(const LuaObject &obj) const; + void Insert(int index, const LuaObject &obj) const; + + LuaObject GetObject(const LuaObject &key) const; + LuaObject GetObject(int key) const; + + lua_Number GetNumber(int key) const; + int GetInteger(int key) const; + // LuaObject GetObject(const char* key) const; + inline const TObject *GetTableHelper(const TObject *key) const; + + void SetNil(const LuaObject& key) const; + void SetObject(const LuaObject &key, const LuaObject &value) const; + // void SetObject(const char *key, const const LuaObject &value) const; + void SetObject(int key, const LuaObject &value) const; + void SetTableHelper(const TObject *key, const TObject *value) const; + + void CreateTable(LuaObject *out, const char *key, int narray, + int lnhash) asm("0x908c10"); + void CreateTable(LuaObject *out, int key, int narray, + int lnhash) asm("0x908ca0"); + void GetByName(LuaObject *out, const char *name) asm("0x90a160"); + LuaObject *__GetMetaTable(LuaObject *out) const asm("0x908ba0"); + void Lookup(LuaObject *out, const char *key) asm("0x9093b0"); + void PushStack(LuaStackObject *out, LuaState *state) const asm("0x907d80"); + void PushStack(lua_State *L) const asm("0x907d10"); + LuaState *GetActiveState() asm("0x9072b0"); + const char *GetString() asm("0x907a90"); + const char *ToString() const asm("0x9073e0"); + const char *TypeName() const; + lua_Number GetNumber() asm("0x907970"); + lua_Number GetDouble() asm("0x907a30"); + lua_Number ToNumber() asm("0x9073b0"); + void AssignNewUserData(RRef *out, LuaState *state, + const RRef *rRef) asm("0x909840"); + void AssignNewUserData(RRef *out, LuaState *state, + const RType *rType) asm("0x9097d0"); + void GetUserData(RRef *out) asm("0x907bc0"); + int GetCount() asm("0x907f50"); + int GetInteger() const asm("0x907910"); + int GetN() const asm("0x907e50"); + int GetTableCount() asm("0x90a410"); + int IsPassed() asm("0x907440"); + lua_State *GetActiveCState() const asm("0x9072c0"); + void AssignBoolean(LuaState *state, bool value) asm("0x909600"); + void AssignInteger(LuaState *state, int value) asm("0x909650"); + void AssignNewTable(LuaState *state, size_t narray, int lnhash) asm("0x909940"); + void AssignNil(LuaState *state) asm("0x9095c0"); + void AssignNumber(LuaState *state, float value) asm("0x9096a0"); + void AssignString(LuaState *state, const char *value) asm("0x909750"); + void AssignTObject(LuaState *state, const TObject *value) asm("0x9099b0"); + void AssignThread(LuaState *state) asm("0x9096f0"); + void Insert(LuaObject *value) asm("0x909af0"); + void Insert(int key, LuaObject *value) asm("0x909ce0"); + void Register(const char *name, lua_CFunction func, + int nupvalues) asm("0x907630"); + void Reset(); + void SetBoolean(const char *key, bool value) asm("0x9080c0"); + void SetInteger(const char *key, int value) asm("0x9081f0"); + void SetInteger(int key, int value) asm("0x908240"); + void SetMetaTable(const LuaObject &value) asm("0x907e00"); + void SetN(int n) asm("0x907ed0"); + void SetNil(const char *key) asm("0x907fa0"); + void SetNil(int key) asm("0x907ff0"); + void SetNumber(const char *key, float value) asm("0x908320"); + void SetNumber(int key, float value) asm("0x908370"); + void SetObject(const LuaObject *key, const LuaObject *value) asm("0x908810"); + void SetObject(const char *key, const LuaObject *value) asm("0x908760"); + void SetObject(int key, const LuaObject *value) asm("0x9087a0"); + void SetString(const char *key, const char *value) asm("0x908450"); + void SetString(int key, const char *value) asm("0x9084e0"); + void TypeError(const char *msg) asm("0x9072d0"); + + // private + void AddToUsedList(LuaState *state) asm("0x908890"); + void AddToUsedList(LuaState *state, const TObject *obj) asm("0x9088e0"); + + LuaObject *m_next; + LuaObject *m_prev; + LuaState *m_state; + TObject m_object; + +private: + LuaObject __Clone(LuaObject &backref) const; + void RemoveFromUsedList(); +}; +VALIDATE_SIZE(LuaObject, 0x14) + +enum StandardLibraries { LIB_NONE, LIB_BASE, LIB_OSIO }; +class LuaState { // 0x34 bytes + void __LuaState(StandardLibraries libs) asm("0x90ac10"); + void __LuaState(LuaState *parentState) asm("0x90a520"); + void __LuaState(int Unused) asm("0x90a5d0"); + void __DLuaState() asm("0x90a600"); + +public: + LuaState(StandardLibraries libs) { __LuaState(libs); }; + LuaState(LuaState *parentState) { __LuaState(parentState); }; + LuaState(int Unused) { __LuaState(Unused); }; + ~LuaState() { __DLuaState(); }; + void GetGlobal(LuaObject *out, const char *key) asm("0x4579d0"); + void GetGlobals(LuaObject *out) asm("0x90a690"); + LuaState *GetActiveState() asm("0x90bee0"); + const char *CheckString(int narg) asm("0x912d10"); + int ArgError(int narg, const char *msg) asm("0x90bf70"); + __cdecl int Error(const char *fmt, ...) asm("0x90c1d0"); + lua_State *GetActiveCState() asm("0x90bef0"); + void CheckAny(int narg) asm("0x923f20"); + + // private + void Init(enum StandardLibraries libs) asm("0x90aad0"); + + lua_State *m_state; + void *ForMultipleThreads; + bool m_ownState; + LuaObject m_threadObj; + LuaState *m_rootState; + struct MiniLuaObject { + LuaObject *m_next; // only valid when in free list + LuaObject *m_prev; // only valid when in used list + } m_headObject, m_tailObject; +}; +VALIDATE_SIZE(LuaState, 0x34) + +lua_State *__cdecl luaV_settable(lua_State *L, const TObject *t, + const TObject *key, + const TObject *value) asm("0x00929450"); +const TObject *__cdecl luaV_gettable(lua_State *L, const TObject *t, + const TObject *key, + int loop) asm("0x009293E0"); +bool LuaPlusH_next(LuaState *, const LuaObject *, LuaObject *, + LuaObject *) asm("0x90a6b0"); +bool lua_toboolean(lua_State *, int) asm("0x90ca40"); +TObject *negindex(lua_State *, int) asm("0x90c340"); +char *luaL_prepbuffer(luaL_Buffer *) asm("0x90e260"); +const char *luaL_checklstring(lua_State *, int, unsigned int *) asm("0x90eaa0"); +const char *luaL_optlstring(lua_State *, int, const char *, + unsigned int *) asm("0x90eb10"); +const char *lua_getlocal(lua_State *, const lua_Debug *, int) asm("0x912680"); +const char *lua_getupvalue(lua_State *, int, int) asm("0x90d9a0"); +const char *lua_pushfstring(lua_State *, const char *, ...) asm("0x90ce90"); +const char *lua_pushvfstring(lua_State *, const char *, char *) asm("0x90ce50"); +const char *lua_setlocal(lua_State *, const lua_Debug *, int) asm("0x9126f0"); +const char *lua_setupvalue(lua_State *, int, int) asm("0x90da00"); +const char *lua_tostring(lua_State *, int) asm("0x90ca90"); +const char *lua_typename(lua_State *, int) asm("0x90c780"); +float luaL_checknumber(lua_State *, int) asm("0x90eb70"); +float luaL_optnumber(lua_State *, int, float) asm("0x90ebf0"); +float lua_tonumber(lua_State *, int) asm("0x90c9f0"); +void lua_newuserdata(RRef *ret, lua_State *, const RType *) asm("0x90d7e0"); +void lua_touserdata(RRef *ret, lua_State *, int) asm("0x90cbb0"); +int luaL_argerror(lua_State *, int, const char *) asm("0x90e900"); +int luaL_callmeta(lua_State *, int, const char *) asm("0x90dda0"); +int luaL_error(lua_State *, const char *, ...) asm("0x90dbf0"); +int luaL_findstring(const char *, const char *const list[]) asm("0x90dc20"); +int luaL_getmetafield(lua_State *, int, const char *) asm("0x90dd40"); +int luaL_getn(lua_State *, int) asm("0x90e090"); +int luaL_loadbuffer(lua_State *, const char *, unsigned int, + const char *) asm("0x90e760"); +int luaL_loadfile(lua_State *, const char *) asm("0x90e5d0"); +int luaL_newmetatable(lua_State *, const char *) asm("0x90dc70"); +int luaL_typerror(lua_State *, int, const char *) asm("0x90e9a0"); +int lua_checkstack(lua_State *, int) asm("0x90c460"); +int lua_dobuffer(lua_State *, const char *, unsigned int, + const char *) asm("0x90e870"); +int lua_dostring(lua_State *, const char *) asm("0x90e8d0"); +int lua_error(lua_State *) noexcept(false) asm("0x90d6c0"); +int lua_getgccount(lua_State *) asm("0x90d660"); +int lua_getgcthreshold(lua_State *) asm("0x90d650"); +int lua_gethookcount(lua_State *) asm("0x9125d0"); +int lua_gethookmask(lua_State *) asm("0x9125c0"); +int lua_getinfo(lua_State *, const char *, lua_Debug *) asm("0x9132f0"); +int lua_getmetatable(lua_State *, int) asm("0x90d180"); +int lua_getn(lua_State *, int) asm("0x90ad30"); +int lua_getstack(lua_State *, int, lua_Debug *) asm("0x9125e0"); +int lua_gettop(lua_State *) asm("0x90c590"); +int lua_isnumber(lua_State *, int) asm("0x90c7a0"); +int lua_isstring(lua_State *, int) asm("0x90c800"); +int lua_lessthan(lua_State *, int, int) asm("0x90c980"); +int lua_load(lua_State *, lua_Chunkreader, void *, + const char *) asm("0x90d5c0"); +int lua_next(lua_State *, int) asm("0x90d6d0"); +int lua_pcall(lua_State *, int, int) asm("0x90d430"); +int lua_rawequal(lua_State *, int, int) asm("0x90c890"); +int lua_resume(lua_State *, int) asm("0x914610"); +int lua_setfenv(lua_State *, int) asm("0x90d3b0"); +int lua_sethook(lua_State *, lua_Hook, int, int) asm("0x912560"); +int lua_setmetatable(lua_State *, int) asm("0x90d340"); +int lua_traceback(lua_State *, const char *, int) asm("0x911ea0"); +int lua_type(lua_State *, int) asm("0x90c740"); +int lua_yield(lua_State *, int) asm("0x913e40"); +int luaopen_base(lua_State *) asm("0x90fd90"); +int luaopen_debug(lua_State *) asm("0x9124c0"); +int luaopen_loadlib(lua_State *) asm("0x91a4b0"); +int luaopen_math(lua_State *) asm("0x91a110"); +int luaopen_serialize(lua_State *) asm("0x923690"); +int luaopen_string(lua_State *) asm("0x926ef0"); +lua_State *lua_newthread(lua_State *) asm("0x90c530"); +lua_State *lua_open() asm("0x9246d0"); +lua_State *lua_tothread(lua_State *, int) asm("0x90cc50"); +unsigned int lua_strlen(lua_State *, int) asm("0x90cb10"); +lua_Hook lua_gethook(lua_State *) asm("0x9125b0"); +void luaL_addlstring(luaL_Buffer *, const char *, unsigned int) asm("0x90e2a0"); +void luaL_addstring(luaL_Buffer *, const char *) asm("0x90e300"); +void luaL_addvalue(luaL_Buffer *) asm("0x90e370"); +void luaL_buffinit(lua_State *, luaL_Buffer *) asm("0x90e400"); +void luaL_checkany(lua_State *, int) asm("0x90ea70"); +void luaL_checkstack(lua_State *, int, const char *) asm("0x90dd10"); +void luaL_checktype(lua_State *, int, int) asm("0x90ea20"); +void luaL_getmetatable(lua_State *, const char *) asm("0x90dcf0"); +void luaL_openlib(lua_State *, const char *, const luaL_reg *, int) asm("0x90de00"); +int __cdecl luaV_equalval(lua_State *L, const TObject *l, const TObject *r) asm("0x00929810"); +void luaL_pushresult(luaL_Buffer *) asm("0x90e330"); +void luaL_setn(lua_State *, int, int) asm("0x90dfb0"); +void luaL_where(lua_State *, int) asm("0x90db80"); +void lua_call(lua_State *, int, int) asm("0x90d400"); +void lua_close(lua_State *) asm("0x9243e0"); +void lua_concat(lua_State *, int) asm("0x90d740"); +void lua_getfenv(lua_State *, int) asm("0x90d1f0"); +void lua_gettable(lua_State *, int) asm("0x90d000"); +void lua_insert(lua_State *, int) asm("0x90c640"); +void lua_newtable(lua_State *) asm("0x90d110"); +void lua_pushboolean(lua_State *, int) asm("0x90cf80"); +void lua_pushcclosure(lua_State *, lua_CFunction, int) asm("0x90ced0"); +void lua_pushlightuserdata(lua_State *, void *) asm("0x90cfc0"); +void lua_pushlstring(lua_State *, const char *, unsigned int) asm("0x90cd80"); +void lua_pushnil(lua_State *) asm("0x90cd00"); +void lua_pushnumber(lua_State *, float) asm("0x90cd40"); +void lua_pushstring(lua_State *, const char *) asm("0x90cdf0"); +void lua_pushvalue(lua_State *, int) asm("0x90c6e0"); +void lua_rawget(lua_State *, int) asm("0x90d050"); +void lua_rawgeti(lua_State *, int, int) asm("0x90d0a0"); +void lua_rawset(lua_State *, int) asm("0x90d2a0"); +void lua_rawseti(lua_State *, int, int) asm("0x90d2f0"); +void lua_remove(lua_State *, int) asm("0x90c5f0"); +void lua_replace(lua_State *, int) asm("0x90c690"); +void lua_setdefaultmetatable(lua_State *, int) asm("0x90ad00"); +void lua_setgcthreshold(lua_State *, int) asm("0x90d670"); +void lua_setglobaluserdata(lua_State *, void *) asm("0x924060"); +void lua_setstateuserdata(lua_State *, void *) asm("0x9240b0"); +void lua_settable(lua_State *, int) asm("0x90d260"); +void lua_settop(lua_State *, int) asm("0x90c5a0"); +void lua_setusergcfunction(lua_State *, userGCFunction) asm("0x924080"); +void lua_xmove(lua_State *, lua_State *, int) asm("0x90c4c0"); +void const *lua_topointer(lua_State *, int) asm("0x90cc90"); +void *lua_getglobaluserdata(lua_State *) asm("0x924050"); +void *lua_getstateuserdata(lua_State *) asm("0x9240a0"); +void *lua_tolightuserdata(lua_State *, int) asm("0x90cc10"); +TObject *luaA_index(lua_State *L, int index) asm("0x90C3D0"); +extern "C" { +void lua_createtable(lua_State *l, int narr, int nhash); +} + +void *__thiscall luaplusassert(void *except, const char *msg) asm("0x00457880"); +void __stdcall _CXXThrowException(void *except, + void *throwInfo) asm("0x00A89950"); + +void ThrowLuaException(const char *message) { + int info[10]; + luaplusassert((void *)info, message); + _CXXThrowException((void *)info, (void *)0x00EC23F0); } -void lua_createtable(lua_State *L, int narray, int nrec); +// #define luaplus_assert(e) if (!(e)) throw std::exception(#e) +#define luaplus_assert(e) \ + if (!(e)) \ + ThrowLuaException(#e) -// void* FAJit = GetModuleHandleA("FAExt.dll"); \ No newline at end of file +LuaObject SCR_Import(LuaState *state, const char *name); \ No newline at end of file From 31ed6727688ec6fe36542a5b74045087fe8c406d Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:48:01 +0300 Subject: [PATCH 04/20] build by clang --- section/ForkThreadFix.cpp | 6 ------ section/ForkThreadFix.cxx | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) delete mode 100644 section/ForkThreadFix.cpp create mode 100644 section/ForkThreadFix.cxx diff --git a/section/ForkThreadFix.cpp b/section/ForkThreadFix.cpp deleted file mode 100644 index dd99e89..0000000 --- a/section/ForkThreadFix.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "moho.h" - -bool __thiscall IsLuaFunction(LuaObject *obj) -{ - return obj->Type() == LUA_TFUNCTION; -} \ No newline at end of file diff --git a/section/ForkThreadFix.cxx b/section/ForkThreadFix.cxx new file mode 100644 index 0000000..832e41c --- /dev/null +++ b/section/ForkThreadFix.cxx @@ -0,0 +1,6 @@ +#include "LuaAPI.h" + +SHARED bool __thiscall IsLuaFunction(LuaObject *obj) +{ + return obj->Type() == LUA_TFUNCTION; +} \ No newline at end of file From 8a0078529c008789fcd94612ff4daad1fc3c3ac8 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:48:17 +0300 Subject: [PATCH 05/20] add iterators --- section/Lua/Iterators/IPairs.cxx | 33 +++++++++++++++++++++++++ section/Lua/Iterators/IPairs.h | 42 ++++++++++++++++++++++++++++++++ section/Lua/Iterators/Pairs.cxx | 34 ++++++++++++++++++++++++++ section/Lua/Iterators/Pairs.h | 33 +++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 section/Lua/Iterators/IPairs.cxx create mode 100644 section/Lua/Iterators/IPairs.h create mode 100644 section/Lua/Iterators/Pairs.cxx create mode 100644 section/Lua/Iterators/Pairs.h diff --git a/section/Lua/Iterators/IPairs.cxx b/section/Lua/Iterators/IPairs.cxx new file mode 100644 index 0000000..db1ac13 --- /dev/null +++ b/section/Lua/Iterators/IPairs.cxx @@ -0,0 +1,33 @@ +#include "IPairs.h" + +IPairsEndIterator::IPairsEndIterator(const LuaObject &table) + : n{table.GetN()} {} + +int IPairsEndIterator::GetN() const { return n; } + +IPairsIterator::IPairsIterator(const LuaObject &table) + : table{table}, index{0}, value{table.m_state} {} + +IPairsIterator &IPairsIterator::operator++() { + LuaState *state = table.m_state; + ++index; + value = table[index]; + return *this; +} + +const std::pair IPairsIterator::operator*() const { + return {index, value}; +} + +std::pair IPairsIterator::operator*() { return {index, value}; } + +bool IPairsIterator::operator!=(const IPairsEndIterator &end_it) const { + return index <= end_it.GetN(); +} + +IPairs::IPairs(const LuaObject &table) : table{table} { + luaplus_assert(table.IsTable()); +} + +IPairsIterator IPairs::begin() { return ++IPairsIterator(table); } +IPairsEndIterator IPairs::end() { return IPairsEndIterator{table}; } diff --git a/section/Lua/Iterators/IPairs.h b/section/Lua/Iterators/IPairs.h new file mode 100644 index 0000000..2482968 --- /dev/null +++ b/section/Lua/Iterators/IPairs.h @@ -0,0 +1,42 @@ +#pragma once +#include "LuaAPI.h" +#include + +class IPairsEndIterator { +public: + IPairsEndIterator(const LuaObject &table); + + int GetN() const; + +private: + int n; +}; + +class IPairsIterator { +public: + IPairsIterator(const LuaObject &table); + + IPairsIterator &operator++(); + + const std::pair operator*() const; + + std::pair operator*(); + + bool operator!=(const IPairsEndIterator &end_it) const; + +private: + const LuaObject &table; + int index; + LuaObject value; +}; + +class IPairs { +public: + IPairs(const LuaObject &table); + + IPairsIterator begin(); + IPairsEndIterator end(); + +private: + const LuaObject &table; +}; \ No newline at end of file diff --git a/section/Lua/Iterators/Pairs.cxx b/section/Lua/Iterators/Pairs.cxx new file mode 100644 index 0000000..6f0be9a --- /dev/null +++ b/section/Lua/Iterators/Pairs.cxx @@ -0,0 +1,34 @@ +#include "Pairs.h" + +PairsIterator::PairsIterator(const LuaObject &table) + : table{table}, key{table.m_state}, value{table.m_state}, done{false} {} + +PairsIterator &PairsIterator::operator++() +{ + LuaState *state = table.m_state; + if (!done && !LuaPlusH_next(state, &table, &key, &value)) + { + done = true; + } + return *this; +} + +const std::pair PairsIterator::operator*() const +{ + return {key, value}; +} + +std::pair PairsIterator::operator*() +{ + return {key, value}; +} + +bool PairsIterator::operator!=(const EndIterator &) const { return !done; } + +Pairs::Pairs(const LuaObject &table) : table{table} +{ + luaplus_assert(table.IsTable()); +} + +PairsIterator Pairs::begin() { return ++PairsIterator(table); } +EndIterator Pairs::end() { return EndIterator{}; } diff --git a/section/Lua/Iterators/Pairs.h b/section/Lua/Iterators/Pairs.h new file mode 100644 index 0000000..b0a7d6f --- /dev/null +++ b/section/Lua/Iterators/Pairs.h @@ -0,0 +1,33 @@ +#pragma once +#include "LuaAPI.h" +#include + +class EndIterator {}; +class PairsIterator { +public: + PairsIterator(const LuaObject &table); + + PairsIterator &operator++(); + + const std::pair operator*() const; + + std::pair operator*(); + + bool operator!=(const EndIterator &) const; + +private: + const LuaObject &table; + LuaObject key; + LuaObject value; + bool done; +}; + +class Pairs { +public: + Pairs(const LuaObject &table); + PairsIterator begin(); + EndIterator end(); + +private: + const LuaObject &table; +}; \ No newline at end of file From 3227fc4ed03b754bf4fe522e10af725c73d64393 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:48:44 +0300 Subject: [PATCH 06/20] LuaObject replicated methods --- section/Lua/LuaObject.cxx | 287 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 287 insertions(+) create mode 100644 section/Lua/LuaObject.cxx diff --git a/section/Lua/LuaObject.cxx b/section/Lua/LuaObject.cxx new file mode 100644 index 0000000..723ad97 --- /dev/null +++ b/section/Lua/LuaObject.cxx @@ -0,0 +1,287 @@ +#include "Iterators/Pairs.h" +#include "LuaAPI.h" + +LuaObject::LuaObject() + : m_next{nullptr}, m_prev{nullptr}, m_state{nullptr}, m_object{} {} + +LuaObject::LuaObject(LuaState *state, int index) : m_object{} +{ + AddToUsedList(state, luaA_index(state->m_state, index)); +} + +LuaObject::LuaObject(LuaState *state) : m_object{} +{ + AddToUsedList(state); +} + +LuaObject::LuaObject(LuaState *state, const TObject *obj) : m_object{} +{ + AddToUsedList(state, obj); +} + +LuaObject::LuaObject(const LuaObject &obj) : LuaObject{} +{ + if (obj.m_state) + { + AddToUsedList(obj.m_state, &obj.m_object); + } +} + +LuaObject::LuaObject(const LuaStackObject &stack) + : LuaObject{stack.m_state, stack.m_stackIndex} {} + +LuaObject LuaObject::operator[](int key) const +{ + LuaObject out; + __Index(&out, key); + return out; +} + +LuaObject LuaObject::operator[](const char *key) const +{ + LuaObject out; + __Index(&out, key); + return out; +} + +LuaObject LuaObject::Clone() const +{ + LuaObject obj; + __Clone(&obj); + return obj; +} + +LuaObject &LuaObject::operator=(const LuaObject &obj) +{ + if (this != &obj) + { + RemoveFromUsedList(); + + LuaState *m_state = obj.m_state; + if (m_state) + { + AddToUsedList(m_state, &obj.m_object); + return *this; + } + + m_state = nullptr; + m_next = nullptr; + m_prev = nullptr; + } + return *this; +} + +LuaObject &LuaObject::operator=(const LuaStackObject &stack) +{ + RemoveFromUsedList(); + AddToUsedList(stack.m_state, + luaA_index(stack.m_state->m_state, stack.m_stackIndex)); + return *this; +} + +LuaObject::~LuaObject() { RemoveFromUsedList(); } + +LuaObject LuaObject::DeepCopy() const +{ + if (!IsTable()) + return *this; + LuaObject backref; + backref.AssignNewTable(m_state, 0, 0); + return __Clone(backref); +} + +LuaObject LuaObject::__Clone(LuaObject &backref) const +{ + luaplus_assert(IsTable()); + LuaObject result; + int narr; + unsigned nhash; + GetTableArrAndHash(&m_object.value, narr, nhash); + + result.AssignNewTable(m_state, narr, 1 << nhash); + backref.SetObject(*this, result); + + for (const auto &[key, value] : Pairs(*this)) + { + LuaObject self_ref = backref.GetObject(value); + + if (self_ref.IsNil()) + { + LuaObject clonedValue = value.IsTable() ? value.__Clone(backref) : value; + result.SetObject(key, clonedValue); + } + else + { + result.SetObject(key, self_ref); + } + } + return result; +} + +inline int LuaObject::Type() const +{ + luaplus_assert(m_state); + return m_object.tt; +} + +inline bool LuaObject::IsBoolean() const { return Type() == LUA_TBOOLEAN; } +inline bool LuaObject::IsInteger() const { return Type() == LUA_TNUMBER; } +inline bool LuaObject::IsNumber() const { return Type() == LUA_TNUMBER; } +inline bool LuaObject::IsString() const { return Type() == LUA_TSTRING; } +inline bool LuaObject::IsTable() const { return Type() == LUA_TTABLE; } +inline bool LuaObject::IsNil() const { return m_state && Type() == LUA_TNIL; } +inline bool LuaObject::IsUserData() const +{ + return Type() == LUA_TUSERDATA || Type() == LUA_TLIGHTUSERDATA; +} + +void LuaObject::RemoveFromUsedList() +{ + if (m_state) + { + m_prev->m_next = m_next; + m_next->m_prev = m_prev; + m_object.tt = LUA_TNIL; + } +} + +void LuaObject::Reset() +{ + RemoveFromUsedList(); + m_state = nullptr; +} + +const char *LuaObject::TypeName() const +{ + int tt = Type(); + if (tt == LUA_TNONE) + { + return "no value"; + } + return luaT_typenames[tt]; +} + +void LuaObject::SetTableHelper(const TObject *key, const TObject *value) const +{ + luaV_settable(GetActiveCState(), &m_object, key, value); +} + +void LuaObject::SetNil(const LuaObject &key) const +{ + luaplus_assert(m_state == key.m_state); + TObject value{}; + SetTableHelper(&key.m_object, &value); +} + +void LuaObject::SetObject(const LuaObject &key, const LuaObject &value) const +{ + luaplus_assert(m_state == key.m_state); + luaplus_assert(m_state == value.m_state); + SetTableHelper(&key.m_object, &value.m_object); +} + +// void LuaObject::SetObject(const char *key, const LuaObject &value) const { +// TObject key_obj{}; +// key_obj.tt = LUA_TSTRING; +// } + +void LuaObject::SetObject(int key, const LuaObject &value) const +{ + TObject key_obj{key}; + SetTableHelper(&key_obj, &value.m_object); +} + +inline const TObject *LuaObject::GetTableHelper(const TObject *key) const +{ + return luaV_gettable(GetActiveCState(), &m_object, key, 0); +} + +LuaObject LuaObject::GetObject(const LuaObject &key) const +{ + luaplus_assert(m_state == key.m_state); + return LuaObject{m_state, GetTableHelper(&key.m_object)}; +} + +LuaObject LuaObject::GetObject(int key) const +{ + TObject key_obj{key}; + return LuaObject{m_state, GetTableHelper(&key_obj)}; +} + +lua_Number LuaObject::GetNumber(int key) const +{ + TObject key_obj{key}; + const TObject *obj = GetTableHelper(&key_obj); + + if (obj->tt != LUA_TNUMBER) + { + luaL_typerror(GetActiveCState(), key, + "expected number"); //! needs proper error message + } + return obj->value.n; +} + +int LuaObject::GetInteger(int key) const { return GetNumber(key); } + +void LuaObject::Insert(const LuaObject &obj) const +{ + luaplus_assert(m_state == obj.m_state); + auto L = m_state->m_state; + + LuaStackObject tbl_stack_obj; + PushStack(&tbl_stack_obj, m_state); + int tbl_index = tbl_stack_obj.m_stackIndex; + + obj.PushStack(L); + // low level insert + luaL_checktype(L, tbl_index, LUA_TTABLE); + int cur_n = luaL_getn(L, tbl_index); + int n = cur_n + 1; + luaL_setn(L, tbl_index, n); + lua_rawseti(L, tbl_index, n); + lua_pop(L, 1); +} + +void LuaObject::Insert(int index, const LuaObject &obj) const +{ + luaplus_assert(m_state == obj.m_state); + auto L = m_state->m_state; + + LuaStackObject tbl_stack_obj; + PushStack(&tbl_stack_obj, m_state); + int tbl_index = tbl_stack_obj.m_stackIndex; + + obj.PushStack(L); + // low level insert + luaL_checktype(L, tbl_index, LUA_TTABLE); + int v2 = luaL_getn(L, tbl_index); + int n = v2 + 1; + int pos = index; + if (pos > n) + n = pos; + luaL_setn(L, tbl_index, n); + for (int i = n - 1; i >= pos; --i) + { + lua_rawgeti(L, tbl_index, i); + lua_rawseti(L, tbl_index, i + 1); + } + lua_rawseti(L, tbl_index, pos); + lua_pop(L, 1); +} + +LuaObject LuaObject::GetMetaTable() const +{ + LuaObject mt; + __GetMetaTable(&mt); + return mt; +} + +bool LuaObject::operator==(const LuaObject &right) const +{ + return m_object.tt == right.m_object.tt && luaV_equalval(m_state->m_state, &m_object, &right.m_object); +} + +bool LuaObject::operator!=(const LuaObject &right) const +{ + return !(*this == right); +} \ No newline at end of file From 9b960b799a6e63be012653884581f99592ebf737 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:48:54 +0300 Subject: [PATCH 07/20] update patches --- section/Lua/TableFuncs.cxx | 38 +------------------------------------- section/Lua/TableFuncs.h | 7 +++++++ section/WorldView.cpp | 7 ++----- 3 files changed, 10 insertions(+), 42 deletions(-) create mode 100644 section/Lua/TableFuncs.h diff --git a/section/Lua/TableFuncs.cxx b/section/Lua/TableFuncs.cxx index 1b6898d..958450b 100644 --- a/section/Lua/TableFuncs.cxx +++ b/section/Lua/TableFuncs.cxx @@ -1,40 +1,4 @@ -#include -namespace lua -{ - struct TObject - { - int tt; - void *value; - }; - - struct Node - { - TObject i_key; - TObject i_val; - Node *next; - }; - - struct Table - { - /* lua::GCObject*/ void *next; - uint8_t tt; - uint8_t marked; - uint16_t gap; - uint8_t flags; - uint8_t lsizenode; - // padding byte - // padding byte - Table *metatable; - TObject *array; - lua::Node *node; - lua::Node *firstfree; - /*lua::GCObject*/ void *gclist; - int sizearray; - }; - -} // namespace lua - -VALIDATE_SIZE(lua::Table, 0x24); +#include "TableFuncs.h" int lua_tablesize(lua_State *L) { diff --git a/section/Lua/TableFuncs.h b/section/Lua/TableFuncs.h new file mode 100644 index 0000000..ddca552 --- /dev/null +++ b/section/Lua/TableFuncs.h @@ -0,0 +1,7 @@ +#include + +VALIDATE_SIZE(lua::Table, 0x24); + +extern const luaL_reg original_table_funcs[] asm("0x00D47418"); + +SHARED int __cdecl lua_openlibtable(lua_State *L); \ No newline at end of file diff --git a/section/WorldView.cpp b/section/WorldView.cpp index 5fbbf4a..86d5c3c 100644 --- a/section/WorldView.cpp +++ b/section/WorldView.cpp @@ -24,11 +24,8 @@ Vector2f ProjectVec(const Vector3f &v, float *camera) void ProjectVectors(lua_State *l, int index, float *camera) { - const void *t = lua_topointer(l, index); - uint32_t asize; - uint8_t hbits; - GetTableAH(t, &asize, &hbits); - lua_createtable(l, asize, hbits); // result table + auto t = (const lua::Table*)lua_topointer(l, index); + lua_createtable(l, t->sizearray, t->lsizenode); // result table lua_pushvalue(l, index); // input vectors lua_pushnil(l); while (lua_next(l, -2)) // -1 = value, -2 = key, -3 = table, -4 = result table From cf403454bd32e6b034babeef876ace33cb631892 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:55:30 +0300 Subject: [PATCH 08/20] use defined type --- include/LuaAPI.h | 5 ----- section/Lua/LuaObject.cxx | 6 ++---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index c195821..de2eed2 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -127,11 +127,6 @@ typedef union { int b; } Value; -void GetTableArrAndHash(const Value *t, int &narr, unsigned int &nhash) { - narr = *(int *)(t->b + 32); - nhash = *(unsigned char *)(t->b + 9); -} - // lua.org/source/5.0/lobject.h.html#TObject struct TObject { int tt; diff --git a/section/Lua/LuaObject.cxx b/section/Lua/LuaObject.cxx index 723ad97..ee3fae5 100644 --- a/section/Lua/LuaObject.cxx +++ b/section/Lua/LuaObject.cxx @@ -94,11 +94,9 @@ LuaObject LuaObject::__Clone(LuaObject &backref) const { luaplus_assert(IsTable()); LuaObject result; - int narr; - unsigned nhash; - GetTableArrAndHash(&m_object.value, narr, nhash); - result.AssignNewTable(m_state, narr, 1 << nhash); + auto t = reinterpret_cast(m_object.value.p); + result.AssignNewTable(m_state, t->sizearray, 1 << t->lsizenode); backref.SetObject(*this, result); for (const auto &[key, value] : Pairs(*this)) From 595866a1b229e3ad96f4d4beae0447f102b55358 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Thu, 21 Nov 2024 13:58:08 +0300 Subject: [PATCH 09/20] introduce new table.clone --- section/Lua/TableFuncs.cxx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/section/Lua/TableFuncs.cxx b/section/Lua/TableFuncs.cxx index 958450b..29668bc 100644 --- a/section/Lua/TableFuncs.cxx +++ b/section/Lua/TableFuncs.cxx @@ -115,9 +115,22 @@ int lua_unpack(lua_State *l) return n_stack; } +// UI_Lua local a = {} reprsl(table.clone(a)) +// UI_Lua local a = {1,2,3} reprsl(table.clone(a)) +// UI_Lua local a = (function() local t = {} for i=1,1000 do t[i]={1,2,3} end +// return t end)() reprsl(table.clone(a)) UI_Lua local a = {a=1,b=3,c=4,1,3,4} +// reprsl(table.clone(a)) UI_Lua local a = {} a[1] = a reprsl(table.clone(a)) +// UI_Lua local a = {} a.a = a reprsl(table.clone(a)) +int TableClone(lua_State *L) noexcept(false) +{ + LuaObject(L->LuaState, 1).DeepCopy().PushStack(L); + return 1; +} + const luaL_reg RegTableFuncsDesc[] = {{"getsize", &lua_tablesize}, {"empty", &lua_tableempty}, {"unpack", &lua_unpack}, + {"clone", &TableClone}, {nullptr, nullptr}}; extern const luaL_reg original_table_funcs[] asm("0x00D47418"); From ff4c59b35dfc57d14c83534e6586017cbed72df4 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sat, 30 Nov 2024 12:43:50 +0300 Subject: [PATCH 10/20] define as gpg::RType --- include/LuaAPI.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index de2eed2..3b011ad 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -176,10 +176,17 @@ namespace lua } // namespace lua // namespace gpg +namespace gpg +{ + struct RType + { + }; + +} class RRef { public: void *d; - void *t; + gpg::RType *t; }; class RType {}; From 4482d5a2e94a481e5b8bd092af7da4ed7ac3fb01 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sat, 30 Nov 2024 12:48:03 +0300 Subject: [PATCH 11/20] correct CheckString --- include/LuaAPI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 3b011ad..14e0980 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -344,7 +344,7 @@ class LuaState { // 0x34 bytes void GetGlobal(LuaObject *out, const char *key) asm("0x4579d0"); void GetGlobals(LuaObject *out) asm("0x90a690"); LuaState *GetActiveState() asm("0x90bee0"); - const char *CheckString(int narg) asm("0x912d10"); + const char *CheckString(int narg, size_t* len) asm("0x90BFB0"); int ArgError(int narg, const char *msg) asm("0x90bf70"); __cdecl int Error(const char *fmt, ...) asm("0x90c1d0"); lua_State *GetActiveCState() asm("0x90bef0"); From f62d7ef85770b5ef49ccb20de3a2a1980aae258d Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sat, 30 Nov 2024 12:48:55 +0300 Subject: [PATCH 12/20] correct CheckAny --- include/LuaAPI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 14e0980..13743f5 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -348,7 +348,7 @@ class LuaState { // 0x34 bytes int ArgError(int narg, const char *msg) asm("0x90bf70"); __cdecl int Error(const char *fmt, ...) asm("0x90c1d0"); lua_State *GetActiveCState() asm("0x90bef0"); - void CheckAny(int narg) asm("0x923f20"); + void CheckAny(int narg) asm("0x90C170"); // private void Init(enum StandardLibraries libs) asm("0x90aad0"); From a27239ef60725c32cfd6f26503a9b5d8ffef85e7 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sat, 30 Nov 2024 12:50:46 +0300 Subject: [PATCH 13/20] correct lua_pushvfstring --- include/LuaAPI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 13743f5..1bee4a0 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -382,7 +382,7 @@ const char *luaL_optlstring(lua_State *, int, const char *, const char *lua_getlocal(lua_State *, const lua_Debug *, int) asm("0x912680"); const char *lua_getupvalue(lua_State *, int, int) asm("0x90d9a0"); const char *lua_pushfstring(lua_State *, const char *, ...) asm("0x90ce90"); -const char *lua_pushvfstring(lua_State *, const char *, char *) asm("0x90ce50"); +const char *lua_pushvfstring(lua_State *, const char *, va_list args) asm("0x90ce50"); const char *lua_setlocal(lua_State *, const lua_Debug *, int) asm("0x9126f0"); const char *lua_setupvalue(lua_State *, int, int) asm("0x90da00"); const char *lua_tostring(lua_State *, int) asm("0x90ca90"); From ed6ea92c94bb3ff1b7d8d243293e0111b730aa99 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sat, 30 Nov 2024 12:51:28 +0300 Subject: [PATCH 14/20] correct lua_traceback --- include/LuaAPI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 1bee4a0..d41e837 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -429,7 +429,7 @@ int lua_resume(lua_State *, int) asm("0x914610"); int lua_setfenv(lua_State *, int) asm("0x90d3b0"); int lua_sethook(lua_State *, lua_Hook, int, int) asm("0x912560"); int lua_setmetatable(lua_State *, int) asm("0x90d340"); -int lua_traceback(lua_State *, const char *, int) asm("0x911ea0"); +int lua_traceback(lua_State *, const char *) asm("0x911ea0"); int lua_type(lua_State *, int) asm("0x90c740"); int lua_yield(lua_State *, int) asm("0x913e40"); int luaopen_base(lua_State *) asm("0x90fd90"); From 60fda9c8a7f968ee872a3587206bda6773d8b576 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sat, 30 Nov 2024 12:55:59 +0300 Subject: [PATCH 15/20] replicate operator bool --- include/LuaAPI.h | 2 ++ section/Lua/LuaObject.cxx | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index d41e837..ca3704d 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -238,6 +238,8 @@ class LuaObject { // 0x14 bytes inline bool IsTable() const; inline bool IsUserData() const; + operator bool() const; + LuaObject GetMetaTable()const; LuaObject Clone() const; LuaObject DeepCopy() const; diff --git a/section/Lua/LuaObject.cxx b/section/Lua/LuaObject.cxx index ee3fae5..7d23341 100644 --- a/section/Lua/LuaObject.cxx +++ b/section/Lua/LuaObject.cxx @@ -282,4 +282,18 @@ bool LuaObject::operator==(const LuaObject &right) const bool LuaObject::operator!=(const LuaObject &right) const { return !(*this == right); +} + +LuaObject::operator bool() const +{ + if (this->m_state) + { + auto tt = this->m_object.tt; + if (tt) + { + if (tt != LUA_TBOOLEAN || this->m_object.value.b) + return true; + } + } + return false; } \ No newline at end of file From 2b77e2e67ccb44f18c3c5a545f48f147a7bcd9f9 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sat, 30 Nov 2024 13:10:43 +0300 Subject: [PATCH 16/20] proper IPairs iterator implementation --- include/LuaAPI.h | 1 - section/Lua/Iterators/IPairs.cxx | 9 ++------- section/Lua/Iterators/IPairs.h | 7 ------- 3 files changed, 2 insertions(+), 15 deletions(-) diff --git a/include/LuaAPI.h b/include/LuaAPI.h index ca3704d..be88c64 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -285,7 +285,6 @@ class LuaObject { // 0x14 bytes int GetInteger() const asm("0x907910"); int GetN() const asm("0x907e50"); int GetTableCount() asm("0x90a410"); - int IsPassed() asm("0x907440"); lua_State *GetActiveCState() const asm("0x9072c0"); void AssignBoolean(LuaState *state, bool value) asm("0x909600"); void AssignInteger(LuaState *state, int value) asm("0x909650"); diff --git a/section/Lua/Iterators/IPairs.cxx b/section/Lua/Iterators/IPairs.cxx index db1ac13..760345d 100644 --- a/section/Lua/Iterators/IPairs.cxx +++ b/section/Lua/Iterators/IPairs.cxx @@ -1,10 +1,5 @@ #include "IPairs.h" -IPairsEndIterator::IPairsEndIterator(const LuaObject &table) - : n{table.GetN()} {} - -int IPairsEndIterator::GetN() const { return n; } - IPairsIterator::IPairsIterator(const LuaObject &table) : table{table}, index{0}, value{table.m_state} {} @@ -22,7 +17,7 @@ const std::pair IPairsIterator::operator*() const { std::pair IPairsIterator::operator*() { return {index, value}; } bool IPairsIterator::operator!=(const IPairsEndIterator &end_it) const { - return index <= end_it.GetN(); + return !value.IsNil(); } IPairs::IPairs(const LuaObject &table) : table{table} { @@ -30,4 +25,4 @@ IPairs::IPairs(const LuaObject &table) : table{table} { } IPairsIterator IPairs::begin() { return ++IPairsIterator(table); } -IPairsEndIterator IPairs::end() { return IPairsEndIterator{table}; } +IPairsEndIterator IPairs::end() { return IPairsEndIterator{}; } diff --git a/section/Lua/Iterators/IPairs.h b/section/Lua/Iterators/IPairs.h index 2482968..0bfa06d 100644 --- a/section/Lua/Iterators/IPairs.h +++ b/section/Lua/Iterators/IPairs.h @@ -3,13 +3,6 @@ #include class IPairsEndIterator { -public: - IPairsEndIterator(const LuaObject &table); - - int GetN() const; - -private: - int n; }; class IPairsIterator { From d58b01b658354cb03d26d455174b11c200d6380f Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sat, 30 Nov 2024 14:46:53 +0300 Subject: [PATCH 17/20] remove arg name --- section/Lua/Iterators/IPairs.cxx | 2 +- section/Lua/Iterators/IPairs.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/section/Lua/Iterators/IPairs.cxx b/section/Lua/Iterators/IPairs.cxx index 760345d..fad7dbc 100644 --- a/section/Lua/Iterators/IPairs.cxx +++ b/section/Lua/Iterators/IPairs.cxx @@ -16,7 +16,7 @@ const std::pair IPairsIterator::operator*() const { std::pair IPairsIterator::operator*() { return {index, value}; } -bool IPairsIterator::operator!=(const IPairsEndIterator &end_it) const { +bool IPairsIterator::operator!=(const IPairsEndIterator &) const { return !value.IsNil(); } diff --git a/section/Lua/Iterators/IPairs.h b/section/Lua/Iterators/IPairs.h index 0bfa06d..d61001d 100644 --- a/section/Lua/Iterators/IPairs.h +++ b/section/Lua/Iterators/IPairs.h @@ -15,7 +15,7 @@ class IPairsIterator { std::pair operator*(); - bool operator!=(const IPairsEndIterator &end_it) const; + bool operator!=(const IPairsEndIterator &) const; private: const LuaObject &table; From 4d2168857092d9ac401df99b9c529dd90e87e506 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sun, 1 Dec 2024 18:34:57 +0300 Subject: [PATCH 18/20] remove redundant line --- section/Lua/Iterators/IPairs.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/section/Lua/Iterators/IPairs.cxx b/section/Lua/Iterators/IPairs.cxx index fad7dbc..6083070 100644 --- a/section/Lua/Iterators/IPairs.cxx +++ b/section/Lua/Iterators/IPairs.cxx @@ -4,7 +4,6 @@ IPairsIterator::IPairsIterator(const LuaObject &table) : table{table}, index{0}, value{table.m_state} {} IPairsIterator &IPairsIterator::operator++() { - LuaState *state = table.m_state; ++index; value = table[index]; return *this; From b649c4fd95424e3b0554c2a5ffa3f851d3f05fb8 Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sun, 1 Dec 2024 18:35:49 +0300 Subject: [PATCH 19/20] proper formatting --- section/Lua/Iterators/IPairs.cxx | 24 ++++++++++++--------- section/Lua/Iterators/IPairs.h | 33 ++++++++++++++++------------- section/Lua/Iterators/Pairs.h | 36 ++++++++++++++++++-------------- 3 files changed, 52 insertions(+), 41 deletions(-) diff --git a/section/Lua/Iterators/IPairs.cxx b/section/Lua/Iterators/IPairs.cxx index 6083070..54c6074 100644 --- a/section/Lua/Iterators/IPairs.cxx +++ b/section/Lua/Iterators/IPairs.cxx @@ -3,24 +3,28 @@ IPairsIterator::IPairsIterator(const LuaObject &table) : table{table}, index{0}, value{table.m_state} {} -IPairsIterator &IPairsIterator::operator++() { - ++index; - value = table[index]; - return *this; +IPairsIterator &IPairsIterator::operator++() +{ + ++index; + value = table[index]; + return *this; } -const std::pair IPairsIterator::operator*() const { - return {index, value}; +const std::pair IPairsIterator::operator*() const +{ + return {index, value}; } std::pair IPairsIterator::operator*() { return {index, value}; } -bool IPairsIterator::operator!=(const IPairsEndIterator &) const { - return !value.IsNil(); +bool IPairsIterator::operator!=(const IPairsEndIterator &) const +{ + return !value.IsNil(); } -IPairs::IPairs(const LuaObject &table) : table{table} { - luaplus_assert(table.IsTable()); +IPairs::IPairs(const LuaObject &table) : table{table} +{ + luaplus_assert(table.IsTable()); } IPairsIterator IPairs::begin() { return ++IPairsIterator(table); } diff --git a/section/Lua/Iterators/IPairs.h b/section/Lua/Iterators/IPairs.h index d61001d..e9bd704 100644 --- a/section/Lua/Iterators/IPairs.h +++ b/section/Lua/Iterators/IPairs.h @@ -2,34 +2,37 @@ #include "LuaAPI.h" #include -class IPairsEndIterator { +class IPairsEndIterator +{ }; -class IPairsIterator { +class IPairsIterator +{ public: - IPairsIterator(const LuaObject &table); + IPairsIterator(const LuaObject &table); - IPairsIterator &operator++(); + IPairsIterator &operator++(); - const std::pair operator*() const; + const std::pair operator*() const; - std::pair operator*(); + std::pair operator*(); - bool operator!=(const IPairsEndIterator &) const; + bool operator!=(const IPairsEndIterator &) const; private: - const LuaObject &table; - int index; - LuaObject value; + const LuaObject &table; + int index; + LuaObject value; }; -class IPairs { +class IPairs +{ public: - IPairs(const LuaObject &table); + IPairs(const LuaObject &table); - IPairsIterator begin(); - IPairsEndIterator end(); + IPairsIterator begin(); + IPairsEndIterator end(); private: - const LuaObject &table; + const LuaObject &table; }; \ No newline at end of file diff --git a/section/Lua/Iterators/Pairs.h b/section/Lua/Iterators/Pairs.h index b0a7d6f..e05f056 100644 --- a/section/Lua/Iterators/Pairs.h +++ b/section/Lua/Iterators/Pairs.h @@ -2,32 +2,36 @@ #include "LuaAPI.h" #include -class EndIterator {}; -class PairsIterator { +class EndIterator +{ +}; +class PairsIterator +{ public: - PairsIterator(const LuaObject &table); + PairsIterator(const LuaObject &table); - PairsIterator &operator++(); + PairsIterator &operator++(); - const std::pair operator*() const; + const std::pair operator*() const; - std::pair operator*(); + std::pair operator*(); - bool operator!=(const EndIterator &) const; + bool operator!=(const EndIterator &) const; private: - const LuaObject &table; - LuaObject key; - LuaObject value; - bool done; + const LuaObject &table; + LuaObject key; + LuaObject value; + bool done; }; -class Pairs { +class Pairs +{ public: - Pairs(const LuaObject &table); - PairsIterator begin(); - EndIterator end(); + Pairs(const LuaObject &table); + PairsIterator begin(); + EndIterator end(); private: - const LuaObject &table; + const LuaObject &table; }; \ No newline at end of file From afac055e5c2c51e79761564a5ad0c864bc868afd Mon Sep 17 00:00:00 2001 From: 4z0t Date: Sun, 1 Dec 2024 19:10:37 +0300 Subject: [PATCH 20/20] remove already defined one --- section/Lua/TableFuncs.cxx | 2 -- 1 file changed, 2 deletions(-) diff --git a/section/Lua/TableFuncs.cxx b/section/Lua/TableFuncs.cxx index 29668bc..458f7ed 100644 --- a/section/Lua/TableFuncs.cxx +++ b/section/Lua/TableFuncs.cxx @@ -133,8 +133,6 @@ const luaL_reg RegTableFuncsDesc[] = {{"getsize", &lua_tablesize}, {"clone", &TableClone}, {nullptr, nullptr}}; -extern const luaL_reg original_table_funcs[] asm("0x00D47418"); - SHARED int __cdecl lua_openlibtable(lua_State *L) { luaL_openlib(L, "table", original_table_funcs, 0);