-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.h
402 lines (367 loc) · 9.93 KB
/
Converter.h
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <vector>
#include <sstream>
#include <fstream>
#include <istream>
#include <iterator>
#include <algorithm>
#include <string>
#include <windows.h>
#include <iostream>
#include <process.h>
#include <Shlwapi.h>
#include <ctime>
#include <cstdlib>
#include <chrono>
#include <unordered_map>
#include "Stuffs.h"
#include "Rlua.h"
extern "C" {
#include "Lua\lua.h"
#include "Lua\lualib.h"
#include "Lua\lauxlib.h"
#include "Lua\luaconf.h"
#include "Lua\lapi.h"
#include "Lua\lualib.h"
#include "Lua\lobject.h"
#include "Lua\lstate.h"
#include "Lua\lgc.h"
#include "Tools.h"
}
std::vector<int> Breakpoints;
std::unordered_map<void*, void*> robloxInstanceMap;
std::unordered_map<void*, void*> vanillaInstanceMap;
static int WrapCallHandler(lua_State* L);
static int UnWrapCallHandler(int rL);
int Registry;
lua_State* Vanilla;
void wrap(lua_State* L, int rL, int idx);
void unwrap(lua_State* L, int rL, int idx);
struct UserDataCachingStruct {
int ReferenceToUserData;
};
typedef int UserDataHandle;
int DataPointer;
struct UserDataCache {
int* UserDataSize;
};
static TValue* idx2adr(lua_State* L, int idx) {
if (idx > 0) {
TValue* o = L->base + (idx - 1);
api_check(L, idx <= L->ci->top - L->base);
if (o >= L->top) return cast(TValue*, luaO_nilobject);
else return o;
}
else if (idx > LUA_REGISTRYINDEX) {
api_check(L, idx != 0 && -idx <= L->top - L->base);
return L->top + idx;
}
else switch (idx) { /* pseudo-indices */
case LUA_REGISTRYINDEX: return registry(L);
case LUA_ENVIRONINDEX: {
Closure* func = curr_func(L);
sethvalue(L, &L->env, func->c.env);
return &L->env;
}
case LUA_GLOBALSINDEX: return gt(L);
default: {
Closure* func = curr_func(L);
idx = LUA_GLOBALSINDEX - idx;
return (idx <= func->c.nupvalues)
? &func->c.upvalue[idx - 1]
: cast(TValue*, luaO_nilobject);
}
}
}
static int GarbageCollector(lua_State* L) {
void* NUD = (void*)rlua_touserdata(luaState, -1);
lua_pushvalue(L, 1);
lua_gettable(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)) {
int Reference = lua_tointeger(L, -1);
rlua_pushnil(luaState);
rlua_rawseti(luaState, LUA_REGISTRYINDEX, Reference);
rlua_settop(luaState, 0);
//writerf(1, "Garbage Collected!");
return 1;
}
lua_pop(L, 1);
return 0;
}
static int GetThreadPointer(lua_State* L) {
lua_getfield(L, -10000, "__thread");
int Thread = lua_tointeger(L, -1);
lua_pop(L, 1);
return Thread;
}
static int DownloadString(lua_State* L) {
if (lua_gettop(L) == 2) {
lua_pushstring(L, DownloadURL(lua_tostring(L, -2)).c_str());
}
else {
lua_pushstring(L, DownloadURL(lua_tostring(L, -1)).c_str());
}
return 1;
}
static int GetObjects(lua_State* L) {
int State = GetThreadPointer(L);
const char* id = lua_tostring(L, 1);
lua_newtable(L);
lua_pushnumber(L, 1);
r_lua_getglobal(State, "game");
rlua_getfield(State, -1, "GetService");
rlua_pushvalue(State, -2);
rlua_pushstring(State, "InsertService");
rlua_pcall(State, 2, 1, 0);
rlua_getfield(State, -1, "LoadLocalAsset");
rlua_pushvalue(State, -2);
rlua_pushstring(State, id);
rlua_pcall(State, 2, 1, 0);
wrap(L, State, -1);
lua_settable(L, -3);
return 1;
}
int Index(lua_State* L) {
const char* key = lua_tostring(L, -1);
unwrap(L, luaState, 1);
rlua_getfield(luaState, -1, key);
wrap(L, luaState, -1);
return 1;
}
const char* typestr(int type) {
if (type == RLUA_TBOOLEAN) {
return "boolean";
}
else if (type == RLUA_TFUNCTION) {
return "function";
}
else if (type == RLUA_TLIGHTUSERDATA) {
return "lightuserdata";
}
else if (type == RLUA_TNIL) {
return "nil";
}
else if (type == -1) {
return "none";
}
else if (type == RLUA_TNUMBER) {
return "number";
}
else if (type == RLUA_TSTRING) {
return "string";
}
else if (type == RLUA_TTABLE) {
return "table";
}
else if (type == RLUA_TTHREAD) {
return "thread";
}
else if (type == RLUA_TUSERDATA) {
return "userdata";
}
else
{
return "Bruh fucking instance please.";
}
}
void DisplayStack(int ls) {
int size = r_lua_gettop(ls);
printf(" -- Stack Start --\n");
for (int at = -11; at < size; at++) {
printf(" --- at %d - %s\n", at + 1, typestr(rlua_type(ls, at + 1)));
}
printf(" -- Stack End --\n");
}
void wrap(lua_State* L, int rL, int idx) {
switch (rlua_type(rL, idx))
{
case RLUA_TLIGHTUSERDATA:
lua_pushlightuserdata(L, (void*)rlua_touserdata(rL, idx));
break;
case RLUA_TNIL:
lua_pushnil(L);
break;
case RLUA_TNUMBER:
lua_pushnumber(L, rlua_tonumber(rL, idx));
break;
case RLUA_TBOOLEAN:
lua_pushboolean(L, rlua_toboolean(rL, idx));
break;
case RLUA_TTHREAD:
lua_newthread(L);
break;
case RLUA_TSTRING:
lua_pushstring(L, rlua_tolstring(rL, idx, 0));
break;
case RLUA_TFUNCTION:
{
rlua_pushvalue(rL, idx);
lua_pushinteger(L, rluaL_ref(rL, LUA_REGISTRYINDEX));
lua_pushcclosure(L, WrapCallHandler, 1);
break;
}
case RLUA_TTABLE:
{
rlua_pushvalue(rL, idx);
lua_newtable(L);
rlua_pushnil(rL);
while (rlua_next(rL, -2))
{
wrap(L, rL, -2);
wrap(L, rL, -1);
lua_settable(L, -3);
r_lua_pop(rL, 1);
}
r_lua_pop(rL, 1);
break;
}
case RLUA_TUSERDATA: {
const auto robloxPtr = (void*)rlua_touserdata(rL, idx);/*grabs a userdata from it's index*/
const auto iterator = vanillaInstanceMap.find((void*)robloxPtr);/*find's the roblox's userdata from it's current mapped*/
if (iterator == vanillaInstanceMap.cend()) { /*uses cend to cache all the used stuff*/
rlua_pushvalue(rL, idx);
reinterpret_cast<Userdata*>(lua_newuserdata(L, sizeof(Userdata)))->reference = rluaL_ref(rL, LUA_REGISTRYINDEX);/*calls roblox's ref by using newuserdata*/
rlua_getmetatable(rL, idx);/*grabs the metatable to it's index*/
wrap(L, rL, -1);/*push to vanilla*/
lua_pushcfunction(L, GarbageCollector);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, Index);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);/*set the metatable*/
lua_pushvalue(L, -1);/*push the value and set it to -1 (index)*/
lua_rawseti(L, LUA_REGISTRYINDEX, ++Registry);/*rawseti the registryindex aka LUA_REGISTRYINDEX (-10000) but it's set as a var*/
vanillaInstanceMap[robloxPtr] = (void*)Registry;/*used vanilla cache map*/
r_lua_pop(rL, 1);/*pop the stack lol*/
}
else
{
lua_rawgeti(L, LUA_REGISTRYINDEX, (int)iterator->second);
}
break;
}
default:
rlua_pushnil(rL);
}
}
void unwrap(lua_State* L, int rL, int idx) {
switch (lua_type(L, idx))
{
case LUA_TLIGHTUSERDATA:
rlua_pushlightuserdata(rL, lua_touserdata(L, idx));
break;
case LUA_TNIL:
rlua_pushnil(rL);
break;
case LUA_TNUMBER:
rlua_pushnumber(rL, lua_tonumber(L, idx));
break;
case LUA_TBOOLEAN:
rlua_pushboolean(rL, lua_toboolean(L, idx));
break;
case LUA_TTHREAD:
rlua_newthread(rL);
break;
case LUA_TSTRING:
rlua_pushstring(rL, lua_tostring(L, idx));
break;
case LUA_TFUNCTION:
{
lua_pushvalue(L, idx);
rlua_pushnumber(rL, luaL_ref(L, LUA_REGISTRYINDEX));
rlua_pushccfunction(rL, (DWORD)UnWrapCallHandler, 1);
break;
}
case LUA_TTABLE:
{
lua_pushvalue(L, idx);
lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
lua_pushvalue(L, -2);
lua_pcall(L, 1, 1, 0);
const char* a = lua_tolstring(L, -1, NULL);
lua_settop(L, -2);
rlua_createtable(rL, 0, 0);
lua_pushnil(L);
while (lua_next(L, -2)) {
lua_pushvalue(L, -1);
lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
lua_pushvalue(L, -2);
lua_pcall(L, 1, 1, 0);
const char* b = lua_tolstring(L, -1, NULL);
lua_settop(L, -3);
if (strcmp(a, b) == 0) {
lua_settop(L, -2);
continue;
}
unwrap(L, rL, -2);
unwrap(L, rL, -1);
rlua_settable(rL, -3);
lua_settop(L, -2);
}
lua_settop(L, -2);
break;
}
case LUA_TUSERDATA:
{
rlua_rawgeti(rL, LUA_REGISTRYINDEX, reinterpret_cast<Userdata*>(lua_touserdata(L, idx))->reference);
break;
}
default:
lua_pushnil(L);
}
}
static int resume(lua_State* rL)
{
lua_State* To = lua_tothread(rL, lua_upvalueindex(1));
const int nargs = lua_gettop(rL);
lua_xmove(rL, To, nargs);
return lua_resume(To, nargs);
lua_newtable(To);
lua_pushstring(To, "This metatable is locked");
lua_setfield(To, -2, "__metatable");
}
static int WrapCallHandler(lua_State* L) {
DWORD rL = rlua_newthread(luaState);
rlua_setfield(luaState, LUA_REGISTRYINDEX, to_string(++Registry).c_str());
int Args = lua_gettop(L);
rlua_rawgeti(rL, LUA_REGISTRYINDEX, lua_tointeger(L, lua_upvalueindex(1)));
for (int i = 1; i <= Args; ++i)
unwrap(L, rL, i);
if (rlua_pcall(rL, Args, LUA_MULTRET, 0))
{
string Message = rlua_tolstring(rL, -1, 0);
if (Message.empty()) Message = "Error occured, no output from Lua\n";
if (Message == "attempt to yield across metamethod/C-call boundary")
{
r_lua_pop(rL, 1);
lua_pushthread(L);
lua_pushcclosure(L, &resume, 1);
unwrap(L, rL, -1);
return lua_yield(L, 0);
}
return luaL_error(L, Message.c_str());
}
int Return = r_lua_gettop(rL);
for (int i = 1; i <= Return; ++i)
wrap(L, rL, i);
rlua_settop(rL, 0);
rlua_pushnil(luaState);
rlua_setfield(luaState, LUA_REGISTRYINDEX, to_string(Registry).c_str());
return Return;
}
static int UnWrapCallHandler(int rL) {
lua_State* L = lua_newthread(Vanilla);
lua_setfield(Vanilla, LUA_REGISTRYINDEX, to_string(++Registry).c_str());
int Args = r_lua_gettop(rL);
lua_rawgeti(L, LUA_REGISTRYINDEX, cast_int(rlua_tonumber(rL, lua_upvalueindex(1))));
for (int i = 1; i <= Args; ++i)
wrap(L, rL, i);
if (lua_pcall(L, Args, LUA_MULTRET, 0)) {
return rluaL_error(rL, luaL_checkstring(L, -1));
}
int Return = lua_gettop(L);
for (int i = 1; i <= Return; ++i)
unwrap(L, rL, i);
lua_settop(L, 0);
lua_pushnil(Vanilla);
lua_setfield(Vanilla, LUA_REGISTRYINDEX, to_string(Registry).c_str());
return Return;
}