fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

blackops7799Linux portb58a563

GUI
7.1 KiB260 linesraw
1local bit = require("bit")
2local log = require("log")
3local memory = require("memory." .. jit.os:lower())
4
5require("extensions.string")
6
7local watcher = {
8	debug = false,
9	process = memory.init(),
10	hooks = {},
11	wildcard_hooks = {},
12	values_memory = {},
13	values_pointer = {},
14	watching_addr = {},
15	watching_str_addr = {},
16	watching_ptr_addr = {},
17	pointer_loc = {},
18	named = {},
19	map = require("smash.memory"), -- The map of the game we want to use!
20}
21
22local TYPE_NULL = 0
23local TYPE_BOOL = 1
24local TYPE_BYTE = 2
25local TYPE_SHORT = 3
26local TYPE_INT = 4
27local TYPE_FLOAT = 5
28local TYPE_POINTER = 6
29local TYPE_DATA = 7
30
31-- Allow us to do things such as watcher.players.name without having to do watcher.named.player.name
32setmetatable(watcher, {__index = watcher.named})
33
34local TYPE_NAME = {
35	["bool"] = TYPE_BOOL,
36	["byte"] = TYPE_BYTE,
37	["short"] = TYPE_SHORT,
38	["int"] = TYPE_INT,
39	["float"] = TYPE_FLOAT,
40	["pointer"] = TYPE_POINTER,
41}
42
43local READ_TYPES = {
44	[TYPE_BOOL] = "readBool",
45	[TYPE_BYTE] = "readUByte",
46	[TYPE_SHORT] = "readShort",
47	[TYPE_INT] = "readInt",
48	[TYPE_FLOAT] = "readFloat",
49}
50
51function watcher.init()
52	for address, info in pairs(watcher.map) do
53		if info.type == "pointer" and info.struct then
54			watcher.pointer_loc[address] = 0x00000000
55			watcher.values_pointer[address] = {}
56			watcher.watching_ptr_addr[address] = {}
57			for offset, struct in pairs(info.struct) do
58				watcher.watching_ptr_addr[address][offset] = TYPE_NAME[struct.type]
59				local name = ("%s.%s"):format(info.name, struct.name)
60				watcher.setTableValue(name, info.init)
61			end
62		else
63			watcher.values_memory[address] = info.init or 0
64			watcher.watching_addr[address] = TYPE_NAME[info.type]
65			watcher.setTableValue(info.name, watcher.values_memory[address])
66		end
67	end
68end
69
70function watcher.shutdown()
71	watcher.values_memory = {}
72	watcher.values_pointer = {}
73	watcher.watching_addr = {}
74	watcher.watching_ptr_addr = {}
75
76	watcher.named = {}
77end
78
79-- Creates or updates a tree of values for easy indexing
80-- Example a path of "player.name" will become watcher.players = { name = value }
81function watcher.setTableValue(path, value)
82	local last = watcher.named
83
84	local keys = {}
85	for key in string.gmatch(path, "[^%.]+") do
86		keys[#keys + 1] = tonumber(key) or key
87	end
88
89	for i, key in ipairs(keys) do
90		if i < #keys then
91			last[key] = last[key] or {}
92			last = last[key]
93		else
94			if type(last) ~= "table" then
95				return error(("Failed to index a %s value (%q)"):format(type(last), keys[i-1]))
96			end
97			last[key] = value
98		end
99	end
100end
101
102function watcher.hook(name, desc, callback)
103	if string.find(name, "*", 1, true) then
104		-- Convert '*' into capture patterns
105		local pattern = '^' .. name:escape():gsub("%%%*", "([^.]-)") .. '$'
106		watcher.wildcard_hooks[pattern] = watcher.wildcard_hooks[pattern] or {}
107		watcher.wildcard_hooks[pattern][desc] = callback
108	else
109		watcher.hooks[name] = watcher.hooks[name] or {}
110		watcher.hooks[name][desc] = callback
111	end
112end
113
114local args = {}
115local matches = {}
116
117function watcher.hookRun(name, ...)
118	-- Normal hooks
119	if watcher.hooks[name] then
120		for desc, callback in pairs(watcher.hooks[name]) do
121			local succ, err
122			if type(desc) == "table" then
123				-- Assume a table is an object, so call it as so
124				succ, err = xpcall(callback, debug.traceback, desc, ...)
125			else
126				succ, err = xpcall(callback, debug.traceback, ...)
127			end
128			if not succ then
129				log.error("watcher hook error: %s (%s)", desc, err)
130			end
131		end
132	end
133
134	local varargs = {...}
135
136	-- Allow for wildcard hooks
137	for pattern, hooks in pairs(watcher.wildcard_hooks) do
138		if string.find(name, pattern) then
139			args = {}
140			matches = {name:match(pattern)}
141
142			for k, match in ipairs(matches) do
143				table.insert(args, tonumber(match) or match)
144			end
145			for k, arg in ipairs(varargs) do
146				table.insert(args, arg)
147			end
148
149			for desc, callback in pairs(hooks) do
150				local succ, err
151				if type(desc) == "table" then
152					-- Assume a table is an object, so call it as so
153					succ, err = xpcall(callback, debug.traceback, desc, unpack(args))
154				else
155					succ, err = xpcall(callback, debug.traceback, unpack(args))
156				end
157				if not succ then
158					log.error("watcher wildcard hook error: %s (%s)", desc, err)
159				end
160			end
161		end
162	end
163end
164
165function watcher.toHex(address)
166	return ("%08X"):format(address)
167end
168
169function watcher.readType(type, address)
170	return watcher.process[READ_TYPES[type]](watcher.process, address)
171end
172
173function watcher.isReady()
174	return watcher.process:hasProcess() and watcher.process:isProcessActive() and watcher.process:hasGamecubeRAMOffset()
175end
176
177function watcher.update(exe)
178	if not watcher.process:isProcessActive() and watcher.process:hasProcess() then
179		watcher.process:close()
180		love.window.setTitle("M'Overlay - Waiting for Dolphin..")
181		log.info("closed: %s", exe)
182	end
183
184	if watcher.process:findprocess(exe) then
185		log.info("hooked: %s", exe)
186		love.window.setTitle("M'Overlay - Dolphin hooked")
187	end
188
189	if not watcher.process:hasGamecubeRAMOffset() and watcher.process:findGamecubeRAMOffset() then
190		log.info("watching ram: %s", exe)
191		watcher.init()
192	elseif watcher.process:hasProcess() and watcher.process:hasGamecubeRAMOffset() then
193		watcher.checkmemoryvalues()
194	end
195end
196
197function watcher.checkmemoryvalues()
198	local frame = watcher.frame
199
200	for address, type in pairs(watcher.watching_addr) do
201		local value = watcher.readType(type, address)
202		if watcher.values_memory[address] ~= value then
203			local info = watcher.map[address]
204			local numValue = tonumber(value) or (value and 1 or 0)
205
206			if info.debug then
207				log.debug("[%d][%08X = %08X] %s = %s", frame, address, numValue, info.name, value)
208			end
209
210			watcher.values_memory[address] = value
211			watcher.setTableValue(info.name, watcher.values_memory[address])
212			watcher.hookRun(info.name, value)
213		end
214	end
215
216	for address, pointing in pairs(watcher.pointer_loc) do
217		local ptr_addr = watcher.process:readInt(address)
218		if watcher.pointer_loc[address] ~= ptr_addr then
219			watcher.pointer_loc[address] = ptr_addr
220
221			log.debug("[%d] pointer at %08X changed location to %08X", frame, address, ptr_addr)
222
223			if ptr_addr == 0x00000000 then
224				watcher.values_pointer[address] = {}
225			end
226		end
227	end
228
229	for address, offsets in pairs(watcher.watching_ptr_addr) do
230		local ptr_addr = watcher.pointer_loc[address]
231		if ptr_addr and ptr_addr ~= 0x00000000 then
232
233			local info = watcher.map[address]
234
235			for offset, type in pairs(offsets) do
236
237				local value = watcher.readType(type, ptr_addr + offset)
238
239				-- If the location of the pointer changed, or our value changed..
240				if watcher.values_pointer[address][offset] ~= value then
241				
242					if info and info.struct and info.struct[offset] then
243						local name = string.format("%s.%s", info.name, info.struct[offset].name)
244						local numValue = tonumber(value) or (value and 1 or 0)
245
246						if info.debug or info.struct[offset].debug then
247							log.debug("[%d][%08X->%08X->%08X = %08X] %s = %s", frame, address, ptr_addr, ptr_addr + offset, numValue, name, value)
248						end
249
250						watcher.values_pointer[address][offset] = value
251						watcher.setTableValue(name, watcher.values_pointer[address][offset])
252						watcher.hookRun(name, value)
253					end
254				end
255			end
256		end
257	end
258end
259
260return watcher