From 1c1d614627ab20efe58ac35c4895798628c50376 Mon Sep 17 00:00:00 2001 From: Ev3nt Date: Sun, 19 Dec 2021 02:51:20 +0300 Subject: [PATCH] Some fixes and update. --- README.md | 2 +- Src/JassNatives.cpp | 6 +++--- Src/JassNatives.h | 2 +- Src/LuaMachine.cpp | 37 ++++++++++++++++++++++++------------- Src/LuaRegister.cpp | 45 ++++++++++++++++++++++++--------------------- Src/Variables.h | 2 +- 6 files changed, 54 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index e414829..7b6d37d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# War3 Lua - 1.1.2 (Beta) +# War3 Lua - 1.1.3 (Beta) [![build](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml/badge.svg)](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml) diff --git a/Src/JassNatives.cpp b/Src/JassNatives.cpp index 4baaf4d..e7d946f 100644 --- a/Src/JassNatives.cpp +++ b/Src/JassNatives.cpp @@ -113,18 +113,18 @@ DWORD JASSNATIVE::get_address() { return _address; } -DWORD JASSNATIVE::call(const std::vector params) { +DWORD JASSNATIVE::call(DWORD* params, int size) { uintptr_t func_address = _address; DWORD retval; uintptr_t esp_ptr; - size_t params_size = params.size() * sizeof DWORD; + size_t params_size = size * sizeof DWORD; _asm { sub esp, params_size mov esp_ptr, esp } - memcpy((LPVOID)esp_ptr, (LPVOID)params.data(), params_size); + memcpy((LPVOID)esp_ptr, params, params_size); _asm { call[func_address] diff --git a/Src/JassNatives.h b/Src/JassNatives.h index 8190a6f..7d70df0 100644 --- a/Src/JassNatives.h +++ b/Src/JassNatives.h @@ -173,7 +173,7 @@ class JASSNATIVE { const JASS_TYPE& get_rettype(); DWORD get_address(); - DWORD call(const std::vector params); + DWORD call(DWORD* params, int size); private: DWORD _address; std::vector _params; diff --git a/Src/LuaMachine.cpp b/Src/LuaMachine.cpp index 5328317..c58777f 100644 --- a/Src/LuaMachine.cpp +++ b/Src/LuaMachine.cpp @@ -10,6 +10,7 @@ #include "Mem.h" lua_State* mainLuaState = NULL; +bool running = false; //--------------------------------------------------------------------------------- @@ -250,8 +251,6 @@ lua_State* getMainLuaState() { lua_open_warcraftfunctions(l); lua_replaceSearchers(l); lua_replaceFileStreamFunctions(l); - - // get_native("TriggerSleepAction").set_sleep(true); } return mainLuaState; @@ -261,7 +260,7 @@ lua_State* createThread(lua_State* l, int index) { lua_pushvalue(l, index); getGlobalTable(l, "_LUA_THREADS", false); lua_pushvalue(l, -2); - + if (lua_rawget(l, -2) != LUA_TTHREAD) { lua_pop(l, 1); lua_newthread(l); @@ -311,6 +310,7 @@ void destroyMainLuaState() if (mainLuaState) { lua_close(mainLuaState); mainLuaState = NULL; + running = false; triggers.clear(); } @@ -370,7 +370,18 @@ void getFunctionByRef(lua_State* l, int ref) { } void lua_throwerr(lua_State* l) { - printf("--------------------Lua Error--------------------\n%s\n-------------------------------------------------\n\n", lua_tostring(l, -1)); + running = false; + + LPCSTR error = lua_tostring(l, -1); + printf("--------------------Lua Error--------------------\n%s\n-------------------------------------------------\n\n", error); + MessageBox(FindWindow("Warcraft III", NULL), error, "Lua Error", MB_ICONHAND | MB_TOPMOST ); +} + +LUA stacktrace(lua_State* L) +{ + luaL_traceback(L, L, lua_tostring(L, -1), 0); + + return 1; } DWORD startLua() { @@ -383,11 +394,15 @@ DWORD startLua() { if (SFileOpenFileEx(*(HANDLE*)MakePtr(gameBase, _mapMPQ), "war3map.lua", NULL, &war3luaScript)) { SFileCloseFile(war3luaScript); + running = true; + + lua_pushcfunction(l, stacktrace); lua_getglobal(l, "require"); lua_pushstring(l, "war3map"); - if (lua_pcall(l, 1, LUA_MULTRET, 0) != LUA_OK) { + if (lua_pcall(l, 1, LUA_MULTRET, -3) != LUA_OK) { lua_throwerr(l); } + lua_pop(l, 1); } @@ -395,6 +410,10 @@ DWORD startLua() { } BOOL __stdcall startLuaThread(DWORD esi, DWORD edi) { + if (!running) { + return FALSE; + } + PJASS_STACK stack = (PJASS_STACK) * (DWORD*)(esi + 0x2868); lua_State* l = (lua_State*)stack->pop()->value; @@ -408,14 +427,6 @@ BOOL __stdcall startLuaThread(DWORD esi, DWORD edi) { ((PJASS_DATA_SLOT)(esi + 80))->set(lua_toboolean(thread, 1), OPCODE_VARIABLE_BOOLEAN); break; - // case LUA_YIELD: - // if (res == lua_gettop(l)) { - // //printf("%s = %d\n", lua_tostring(l, -1), lua_gettop(l)); - // } - // - // printf("%d = %d\n", res, lua_gettop(l)); - // - // break; case LUA_ERRRUN: lua_throwerr(thread); diff --git a/Src/LuaRegister.cpp b/Src/LuaRegister.cpp index 4ad32f5..e43e2ba 100644 --- a/Src/LuaRegister.cpp +++ b/Src/LuaRegister.cpp @@ -10,65 +10,66 @@ LUA lua_jCall(lua_State* l) { LPCSTR name = lua_tostring(l, lua_upvalueindex(1)); JASSNATIVE native = get_native(name); - + if (!native.is_valid()) { return 0; } { std::vector params = native.get_params(); - if ((int)params.size() > lua_gettop(l)) { - return 0; + int size = params.size(); + if (size > lua_gettop(l)) { + return luaL_error(l, "function '%s' must have %d %s", name, size, size > 1 ? "arguments" : "argument"); } - - // if (native.is_sleep()) { - // lua_yield(l, NULL); - // } } - std::vector temp_params; - std::vector params; + int size = lua_gettop(l); + float* temp_params = new float[size]; + DWORD* params = new DWORD[size]; + ZeroMemory(temp_params, size); + ZeroMemory(params, size); UINT i = 1; for (const auto& type : native.get_params()) { switch (type) { case TYPE_CODE: if (lua_isinteger(l, i)) { - params.push_back((DWORD)lua_tointeger(l, i)); + params[i - 1] = (DWORD)lua_tointeger(l, i); } else if (lua_isfunction(l, i)) { - params.push_back(to_Code(l, i)); + params[i - 1] = to_Code(l, i); } break; case TYPE_BOOLEAN: - params.push_back((DWORD)lua_toboolean(l, i)); + params[i - 1] = (DWORD)lua_toboolean(l, i); break; case TYPE_HANDLE: - params.push_back((DWORD)lua_tointeger(l, i)); + params[i - 1] = (DWORD)lua_tointeger(l, i); break; case TYPE_INTEGER: if (lua_isinteger(l, i)) { - params.push_back((DWORD)lua_tointeger(l, i)); + params[i - 1] = (DWORD)lua_tointeger(l, i); } else { - params.push_back(to_ID(lua_tostring(l, i))); + params[i - 1] = to_ID(lua_tostring(l, i)); } break; - case TYPE_REAL: - temp_params.push_back(to_jReal((float)lua_tonumber(l, i))); - params.push_back((DWORD) & (temp_params[temp_params.size() - 1])); + case TYPE_REAL: { + temp_params[i - 1] = (float)lua_tonumber(l, i); + params[i - 1] = (DWORD)&(temp_params[i - 1]); break; + } case TYPE_STRING: - params.push_back(to_jString(lua_tostring(l, i))); + params[i - 1] = to_jString(lua_tostring(l, i)); break; default: - params.push_back(NULL); + params[i - 1] = NULL; break; } @@ -76,7 +77,9 @@ LUA lua_jCall(lua_State* l) { i++; } - DWORD result = native.call(params); + DWORD result = native.call(params, size); + delete[] temp_params; + delete[] params; switch (native.get_rettype()) { case TYPE_BOOLEAN: diff --git a/Src/Variables.h b/Src/Variables.h index edc6c03..9199edd 100644 --- a/Src/Variables.h +++ b/Src/Variables.h @@ -9,7 +9,7 @@ #define WAR3_LUA_MAJOR "1" #define WAR3_LUA_MINOR "1" -#define WAR3_LUA_RELEASE "2 (Beta)" +#define WAR3_LUA_RELEASE "3 (Beta)" #define WAR3_LUA_VERSION WAR3_LUA_MAJOR "." WAR3_LUA_MINOR "." WAR3_LUA_RELEASE #define WAR3_LUA "War3 Lua " WAR3_LUA_VERSION