-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHavokScript.cpp
115 lines (104 loc) · 5.4 KB
/
HavokScript.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <Windows.h>
#include <string>
#include "HavokScript.h"
namespace hks {
hks_pushnamedcclosureType pushnamedcclosure;
luaL_checkintegerType checkinteger;
luaL_checknumberType checknumber;
hksi_lua_setfieldType setfield;
GetTopType gettop;
DoStringType dostring;
hksi_lua_tobooleanType toboolean;
hksi_lua_pushnumberType pushnumber;
hksi_lua_pushintegerType pushinteger;
hksi_luaL_errorType error;
hksi_lua_pushfstringType pushfstring;
CheckLStringType checklstring;
hksi_lua_touserdataType touserdata;
hksi_lua_getfieldType getfield;
PopType pop;
hksi_lua_createtableType createtable;
lua_tocfunctionType tocfunction;
hksL_checktableType checktable;
hks_lua_objlenType objlen;
hks_lua_gettableType gettable;
hksi_lua_cpcallType cpcall;
hski_lua_pushvalueType pushvalue;
RefType ref;
hksi_lua_pcallType pcall;
hksi_lua_rawgetiType rawgeti;
hksi_lua_unrefType unref;
hksi_lua_isnumberType isnumber;
hksi_lua_tonumberType tonumber;
hksi_lua_isuserdataType isuserdata;
hksi_lua_settableType settable;
namespace {
static void InitHavokScriptImports(HMODULE hksDll) {
pushnamedcclosure = (hks_pushnamedcclosureType)GetProcAddress(hksDll, "?hks_pushnamedcclosure@@YAXPEAUlua_State@@P6AH0@ZHPEBDH@Z");
setfield = (hksi_lua_setfieldType)GetProcAddress(hksDll, "?hksi_lua_setfield@@YAXPEAUlua_State@@HPEBD@Z");
checkinteger = (luaL_checkintegerType)GetProcAddress(hksDll, "?luaL_checkinteger@@YAHPEAUlua_State@@H@Z");
gettop = (GetTopType)GetProcAddress(hksDll, "?GetTop@LuaState@LuaPlus@@QEBAHXZ");
dostring = (DoStringType)GetProcAddress(hksDll, "?DoString@LuaState@LuaPlus@@QEAAHPEBD@Z");
toboolean = (hksi_lua_tobooleanType)GetProcAddress(hksDll, "?hksi_lua_toboolean@@YAHPEAUlua_State@@H@Z");
pushinteger = (hksi_lua_pushintegerType)GetProcAddress(hksDll, "?hksi_lua_pushinteger@@YAXPEAUlua_State@@H@Z");
pushnumber = (hksi_lua_pushnumberType)GetProcAddress(hksDll, "?hksi_lua_pushnumber@@YAXPEAUlua_State@@N@Z");
error = (hksi_luaL_errorType)GetProcAddress(hksDll, "?hksi_luaL_error@@YAHPEAUlua_State@@PEBDZZ");
checknumber = (luaL_checknumberType)GetProcAddress(hksDll, "?luaL_checknumber@@YANPEAUlua_State@@H@Z");
pushfstring = (hksi_lua_pushfstringType)GetProcAddress(hksDll, "?hksi_lua_pushfstring@@YAPEBDPEAUlua_State@@PEBDZZ");
checklstring = (CheckLStringType)GetProcAddress(hksDll, "?hksi_luaL_checklstring@@YAPEBDPEAUlua_State@@HPEA_K@Z");
pop = (PopType)GetProcAddress(hksDll, "?Pop@LuaState@LuaPlus@@QEAAXH@Z");
touserdata = (hksi_lua_touserdataType)GetProcAddress(hksDll, "?hksi_lua_touserdata@@YAPEAXPEAUlua_State@@H@Z");
getfield = (hksi_lua_getfieldType)GetProcAddress(hksDll, "?hksi_lua_getfield@@YAXPEAUlua_State@@HPEBD@Z");
createtable = (hksi_lua_createtableType)GetProcAddress(hksDll, "?lua_createtable@@YAXPEAUlua_State@@HH@Z");
tocfunction = (lua_tocfunctionType)GetProcAddress(hksDll, "?lua_tocfunction@@YAP6AHPEAUlua_State@@@Z0H@Z");
checktable = (hksL_checktableType)GetProcAddress(hksDll, "?hksL_checktable@@YAXPEAUlua_State@@H@Z");
objlen = (hks_lua_objlenType)GetProcAddress(hksDll, "?hksi_lua_objlen@@YA_KPEAUlua_State@@H@Z");
gettable = (hks_lua_gettableType)GetProcAddress(hksDll, "?hksi_lua_gettable@@YAXPEAUlua_State@@H@Z");
cpcall = (hksi_lua_cpcallType)GetProcAddress(hksDll, "?hksi_lua_cpcall@@YAHPEAUlua_State@@P6AH0@ZPEAX@Z");
ref = (RefType)GetProcAddress(hksDll, "?luaL_ref@@YAHPEAUlua_State@@H@Z");
pcall = (hksi_lua_pcallType)GetProcAddress(hksDll, "?hksi_lua_pcall@@YAHPEAUlua_State@@HHH@Z");
pushvalue = (hski_lua_pushvalueType)GetProcAddress(hksDll, "?hksi_lua_pushvalue@@YAXPEAUlua_State@@H@Z");
rawgeti = (hksi_lua_rawgetiType)GetProcAddress(hksDll, "?hksi_lua_rawgeti@@YAXPEAUlua_State@@HH@Z");
unref = (hksi_lua_unrefType)GetProcAddress(hksDll, "?hksi_luaL_unref@@YAXPEAUlua_State@@HH@Z");
settable = (hksi_lua_settableType)GetProcAddress(hksDll, "?SetTable@LuaState@LuaPlus@@QEAAXH@Z");
isnumber = (hksi_lua_isnumberType)GetProcAddress(hksDll, "?hksi_lua_isnumber@@YAHPEAUlua_State@@H@Z");
tonumber = (hksi_lua_tonumberType)GetProcAddress(hksDll, "?hksi_lua_tonumber@@YANPEAUlua_State@@H@Z");
isuserdata = (hksi_lua_isuserdataType)GetProcAddress(hksDll, "?hksi_lua_isuserdata@@YAHPEAUlua_State@@H@Z");
}
}
int checkplayerid(lua_State* L, int position) {
int playerId = hks::checkinteger(L, position);
if (0 <= playerId && playerId < 64) {
return playerId;
}
std::string errorMessage = "Invalid playerId: ";
hks::error(L, errorMessage.append(std::to_string(playerId)).c_str());
return -1;
}
void pushboolean(lua_State* L, bool value) {
// Access the Lua stack top pointer
int* stackTop = *(int**)((uintptr_t)L + 0x48);
// Push a Lua boolean value onto the stack
*stackTop = 1;
stackTop[2] = static_cast<unsigned int>(value);
// Increment the stack top pointer by 4 bytes
*(int**)((uintptr_t)L + 0x48) = stackTop + 0x4;
}
// GameCore: 0x16a10
void pushlightuserdata(lua_State* L, void* ptr) {
int* luaStack = *(int**)((uintptr_t)L + 0x48);
*(void**)(luaStack + 2) = ptr;
*luaStack = 2;
*(int**)((uintptr_t)L + 0x48) = luaStack + 0x4;
}
// Should only ever be called once.
void InitHavokScript() {
HMODULE hksDll = GetModuleHandle(TEXT("HavokScript_FinalRelease.dll"));
if (hksDll == NULL) {
std::cout << "Failed to load HavokScript! " << GetLastError() << '\n';
return;
}
InitHavokScriptImports(hksDll);
}
}