Skip to content

Commit

Permalink
Some fixes and update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ev3nt committed Dec 18, 2021
1 parent 5aa93da commit 1c1d614
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
6 changes: 3 additions & 3 deletions Src/JassNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ DWORD JASSNATIVE::get_address() {
return _address;
}

DWORD JASSNATIVE::call(const std::vector<DWORD> 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]
Expand Down
2 changes: 1 addition & 1 deletion Src/JassNatives.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class JASSNATIVE {
const JASS_TYPE& get_rettype();
DWORD get_address();

DWORD call(const std::vector<DWORD> params);
DWORD call(DWORD* params, int size);
private:
DWORD _address;
std::vector<JASS_TYPE> _params;
Expand Down
37 changes: 24 additions & 13 deletions Src/LuaMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Mem.h"

lua_State* mainLuaState = NULL;
bool running = false;


//---------------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -311,6 +310,7 @@ void destroyMainLuaState()
if (mainLuaState) {
lua_close(mainLuaState);
mainLuaState = NULL;
running = false;
triggers.clear();
}

Expand Down Expand Up @@ -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() {
Expand All @@ -383,18 +394,26 @@ 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);
}

return 0;
}

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;
Expand All @@ -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);

Expand Down
45 changes: 24 additions & 21 deletions Src/LuaRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,76 @@
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<JASS_TYPE> 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<DWORD> temp_params;
std::vector<DWORD> 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;
}

i++;
}

DWORD result = native.call(params);
DWORD result = native.call(params, size);
delete[] temp_params;
delete[] params;

switch (native.get_rettype()) {
case TYPE_BOOLEAN:
Expand Down
2 changes: 1 addition & 1 deletion Src/Variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1c1d614

Please sign in to comment.