forked from RexiRexii/Remade-Lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.hpp
294 lines (235 loc) · 7.76 KB
/
functions.hpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#pragma once
#include <cstdint>
#include <emmintrin.h>
#include <dependencies/roblox/offsets.hpp>
/* struct */
struct r_TValue
{
union
{
std::uintptr_t gc;
void* p;
double n;
int b;
float v[2];
} value;
int extra[1];
int tt;
};
struct r_CallS
{
r_TValue* func;
int nresults;
};
// offsets
const auto luastate_top = 8;
const auto luastate_base = 16;
__inline std::uintptr_t r_incr_top(const std::uintptr_t a1)
{
return *reinterpret_cast<std::uintptr_t*>(a1 + luastate_top) += sizeof(r_TValue);
}
__inline std::uintptr_t r_decr_top(const std::uintptr_t a1)
{
return *reinterpret_cast<std::uintptr_t*>(a1 + luastate_top) -= sizeof(r_TValue);
}
__inline void r_setobj2s(r_TValue* obj1, r_TValue* obj2)
{
const r_TValue* o2 = (obj2);
r_TValue* o1 = (obj1);
*o1 = *o2;
}
// addresses
const auto r_luaO_nilobject = aslr(0x222D240);
const auto xorconst_address = aslr(0x2E01520);
const auto pseudo2_address = aslr(0x171E540);
const auto luad_precall_address = aslr(0x171FD60);
const auto luad_rawrunprotected_address = aslr(0x1725E00);
const auto r_pseudo2addr = reinterpret_cast<r_TValue*(__fastcall*)(std::uintptr_t, std::int32_t)>(pseudo2_address);
__inline std::double_t r_xor_double(const std::double_t* from)
{
__m128d xmm_key = _mm_load_pd(reinterpret_cast<const std::double_t*>(xorconst_address));
__m128d xmm_data = _mm_load_sd(from);
__m128d xmm_result = _mm_xor_pd(xmm_key, xmm_data);
return _mm_cvtsd_f64(xmm_result);
}
/*
** basic stack manipulation
*/
__inline r_TValue* r_index2addr(const std::uintptr_t rL, const std::int32_t idx)
{
if (idx > 0)
{
const auto o = *reinterpret_cast<r_TValue**>(rL + luastate_base) + ((idx - 1) * sizeof(r_TValue));
if (o >= *reinterpret_cast<r_TValue**>(rL + luastate_top))
*reinterpret_cast<r_TValue**>(r_luaO_nilobject);
else
return o;
}
else if (idx > -10000)
return *reinterpret_cast<r_TValue**>(rL + luastate_top) + idx;
else
return r_pseudo2addr(rL, idx);
}
__inline std::uintptr_t r_lua_gettop(const std::uintptr_t rL)
{
return (*reinterpret_cast<r_TValue**>(rL + luastate_top) - *reinterpret_cast<r_TValue**>(rL + luastate_base)) >> 4;
}
__inline void r_lua_settop(const std::uintptr_t rL, const std::int32_t idx)
{
auto top = *reinterpret_cast<r_TValue**>(rL + luastate_top);
auto base = *reinterpret_cast<r_TValue**>(rL + luastate_base);
if (idx >= 0)
{
while (top < base + idx)
top->tt = R_LUA_TNIL;
top = base + idx;
}
else
top += idx + 1; /* `subtract' index (index is negative) */
}
__inline void r_lua_pushvalue(const std::uintptr_t rL, const std::int32_t idx)
{
const auto o = r_index2addr(rL, idx);
const auto top = *reinterpret_cast<r_TValue**>(rL + luastate_top);
r_setobj2s(top, o);
r_incr_top(rL);
}
__inline void r_lua_pushnil(const std::uintptr_t rL)
{
const auto top = *reinterpret_cast<r_TValue**>(rL + luastate_top);
top->tt = R_LUA_TNIL;
r_incr_top(rL);
}
__inline void r_lua_pushnumber(const std::uintptr_t rL, const std::double_t n)
{
r_TValue* i_o = (*reinterpret_cast<r_TValue**>(rL + luastate_top));
i_o->value.n = r_xor_double(&(n));
i_o->tt = R_LUA_TNUMBER;
r_incr_top(rL);
}
__inline void r_lua_pushinteger(const std::uintptr_t rL, std::int32_t n)
{
r_TValue* i_o = (*reinterpret_cast<r_TValue**>(rL + luastate_top));
i_o->value.n = r_xor_double(&*reinterpret_cast<const std::double_t*>(&(n)));
i_o->tt = R_LUA_TNUMBER;
return;
}
__inline void r_lua_remove(const std::uintptr_t rL, const std::int32_t idx)
{
auto p = r_index2addr(rL, idx);
const auto top = *reinterpret_cast<r_TValue**>(rL + luastate_top);
while (++p < top)
r_setobj2s(p - 1, p);
r_decr_top(rL);
}
__inline void r_lua_insert(const std::uintptr_t rL, const std::int32_t idx)
{
const auto p = r_index2addr(rL, idx);
const auto top = *reinterpret_cast<r_TValue**>(rL + luastate_top);
for (auto q = top; q > p; q--)
r_setobj2s(q, q - 1);
r_setobj2s(p, top);
}
__inline void r_lua_replace(const std::uintptr_t rL, const std::int32_t idx)
{
const auto o = r_index2addr(rL, idx);
const auto top = *reinterpret_cast<r_TValue**>(rL + luastate_top);
if (idx == -10001)
throw std::exception("no calling environment");
r_setobj2s(o, top - 1);
r_decr_top(rL);
}
__inline void r_lua_xmove(const std::uintptr_t from, const std::uintptr_t to, std::int32_t n)
{
if (from == to)
return;
const auto ttop = *reinterpret_cast<r_TValue**>(to + luastate_top);
const auto ftop = *reinterpret_cast<r_TValue**>(from + luastate_top) - n;
for (auto i = 0; i < n; i++)
*reinterpret_cast<double*>(ttop + i) = *reinterpret_cast<const double*>(ftop + i);
*reinterpret_cast<r_TValue**>(from + luastate_top) = ftop;
*reinterpret_cast<r_TValue**>(to + luastate_top) = ttop + n;
}
/*
** access functions (stack -> C)
*/
__inline std::int32_t r_lua_type(const std::uintptr_t rL, const std::int32_t idx)
{
const auto o = r_index2addr(rL, idx);
return (o == *reinterpret_cast<r_TValue**>(r_luaO_nilobject)) ? R_LUA_TNONE : o->tt;
}
__inline std::double_t r_lua_tonumber(const std::uintptr_t rL, const std::int32_t idx)
{
auto top = r_index2addr(rL, idx);
const auto value = *reinterpret_cast<const std::double_t*>(&(reinterpret_cast<r_TValue*>(top)->value));
const auto result = r_xor_double(&(value));
if (result == 0)
throw std::exception("value is nil");
return result;
}
__inline std::int32_t r_lua_tointeger(const std::uintptr_t rL, const std::int32_t idx)
{
auto top = r_index2addr(rL, idx);
const auto value = *reinterpret_cast<const std::double_t*>(&(reinterpret_cast<r_TValue*>(top)->value));
const auto result = static_cast<const std::int32_t>(std::floor(r_xor_double(&(value))));
// std::floor = returns integer value, perfect for what we need it for!
if (result == 0)
throw std::exception("value is nil");
return result;
}
__inline bool r_lua_toboolean(const std::uintptr_t rL, const std::int32_t idx)
{
auto top = r_index2addr(rL, idx);
const auto result = reinterpret_cast<r_TValue*>(top)->value.b; // wow! even tho I could simply do this without the struct, I chose to use it! Little difference wouldnt hurt ;)
if (result == 0)
throw std::exception("value is nil");
return result;
}
__inline void* r_lua_touserdata(const std::uintptr_t rL, const std::int32_t idx)
{
const auto o = r_index2addr(rL, idx);
switch (o->tt)
{
case R_LUA_TLIGHTUSERDATA:
return *reinterpret_cast<void**>(o);
case R_LUA_TUSERDATA:
return reinterpret_cast<void*>(*reinterpret_cast<const std::uintptr_t*>(o) + 16u);
default:
return 0;
}
}
__inline std::int32_t r_luaD_call(const std::uintptr_t rL, r_TValue* func, const std::int32_t nresults)
{
const auto ptr = std::make_unique<r_CallS>();
const auto ud = ptr.get();
ud->func = func;
ud->nresults = nresults;
std::int32_t result = 0;
__asm
{
push edx;
push eax;
push ecx;
mov edx, [luad_precall_address];
mov ecx, [rL];
mov eax, [ud];
push eax;
call[luad_rawrunprotected_address];
mov[result], eax;
add esp, 4;
pop ecx;
pop eax;
pop edx;
}
return result;
}
__inline void r_lua_call(const std::uintptr_t rL, const std::int32_t nargs, const std::int32_t nresults)
{
auto func = *reinterpret_cast<r_TValue**>(rL + luastate_top) - (nargs + 1);
r_luaD_call(rL, func, nresults);
}
__inline void r_f_call(const std::uintptr_t rL, void* ud)
{
auto c = reinterpret_cast<r_CallS*>(ud);
r_luaD_call(rL, c->func, c->nresults);
}