-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.lua
107 lines (92 loc) · 2.72 KB
/
Memory.lua
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
local Debug = {}
local getupvalue = debug.getupvalue
local setupvalue = debug.setupvalue
local getupvalues = debug.getupvalues
local getconstants = debug.getconstants
function Debug:ReadVal(Func, Offset)
assert(Func, "No function was provided.")
return getupvalue(Func, tonumber(Offset))
end
function Debug:WriteVal(Func, Offset, Valve)
assert(Func, "No function was provided.")
return setupvalue(Func, tonumber(Offset), Valve)
end
function Debug:GetBase(Script: Instance)
assert(Script, "No script provided.")
return self:Scan(Script, "base")
end
function GenerateInfo(Func, Offset: number, Script)
local Base = {
["base"] = Func,
["script"] = Script,
["constants"] = getconstants(Func),
}
if Offset then
Base["offset"] = Offset
end
function Base:WriteMem(NewValue, _Offset)
assert(_Offset, "No offset was provided.")
return Debug:WriteVal(Func, _Offset or Offset, NewValue)
end
function Base:ReadMem(_Offset)
assert(_Offset, "No offset was provided.")
return Debug:ReadVal(Func, _Offset or Offset)
end
function Base:GetConstants()
return getconstants(Func)
end
function Base:TableSearch(Contains)
assert(Contains, "No search scope was provided.")
-- Returns both the sharedTable and the Value
-- of the searched key within the table
return Debug:Scan(nil, "table", Contains)
end
function Base:Scan(SearchType: string, Target)
return Debug:Scan(Script, SearchType, Target)
end
return Base
end
local find = table.find
function Debug:Scan(Script: Instance, SearchType: string, Contains: SharedTable)
for _, thread in next, getgc(SearchType=="table") do
-- Search the collected tables for keys
if SearchType=="table" then
local Table = thread
assert(Contains, "No search scope was provided.")
if type(Table) ~= "table" then continue end
for _, Key in Contains do
local suc, Value = pcall(function() -- Table search can error
-- rawget can bypass a metatable index hook
-- replace with Table[Key] to fix!
return find(Table, Key) or rawget(Table, Key)
end)
if suc and Value then
return Table, Value
end
end
return
end
-- Thread upvalue search:
if type(thread)~="function" or not islclosure(thread) then
continue
end
-- Check the script enviroment for the target
if Script then
local _Env = getfenv(thread)
if _Env.script and _Env.script ~= Script then
continue
end
end
-- getscriptbase replacement
if SearchType=="base" then
return GenerateInfo(thread, nil, Script)
end
local Values = SearchType=="upvalue"and getupvalues(thread)or getconstants(thread)
for Offset, Value in Values do
if Value == Contains then
return GenerateInfo(thread, Offset, Script)
end
end
end
end
return Debug