fork/m-overlay

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

blackops7799Merge APM counter into controller display Lots of small fixes and stuff650c9b1

gui
7.1 KiB264 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
114function watcher.unhook(name, desc)
115	watcher.hook(name, desc, nil)
116end
117
118local args = {}
119local matches = {}
120
121function watcher.hookRun(name, ...)
122	-- Normal hooks
123	if watcher.hooks[name] then
124		for desc, callback in pairs(watcher.hooks[name]) do
125			local succ, err
126			if type(desc) == "table" then
127				-- Assume a table is an object, so call it as so
128				succ, err = xpcall(callback, debug.traceback, desc, ...)
129			else
130				succ, err = xpcall(callback, debug.traceback, ...)
131			end
132			if not succ then
133				log.error("watcher hook error: %s (%s)", desc, err)
134			end
135		end
136	end
137
138	local varargs = {...}
139
140	-- Allow for wildcard hooks
141	for pattern, hooks in pairs(watcher.wildcard_hooks) do
142		if string.find(name, pattern) then
143			args = {}
144			matches = {name:match(pattern)}
145
146			for k, match in ipairs(matches) do
147				table.insert(args, tonumber(match) or match)
148			end
149			for k, arg in ipairs(varargs) do
150				table.insert(args, arg)
151			end
152
153			for desc, callback in pairs(hooks) do
154				local succ, err
155				if type(desc) == "table" then
156					-- Assume a table is an object, so call it as so
157					succ, err = xpcall(callback, debug.traceback, desc, unpack(args))
158				else
159					succ, err = xpcall(callback, debug.traceback, unpack(args))
160				end
161				if not succ then
162					log.error("watcher wildcard hook error: %s (%s)", desc, err)
163				end
164			end
165		end
166	end
167end
168
169function watcher.toHex(address)
170	return ("%08X"):format(address)
171end
172
173function watcher.readType(type, address)
174	return watcher.process[READ_TYPES[type]](watcher.process, address)
175end
176
177function watcher.isReady()
178	return watcher.process:hasProcess() and watcher.process:isProcessActive() and watcher.process:hasGamecubeRAMOffset()
179end
180
181function watcher.update(exe)
182	if not watcher.process:isProcessActive() and watcher.process:hasProcess() then
183		watcher.process:close()
184		love.window.setTitle("M'Overlay - Waiting for Dolphin..")
185		log.info("closed: %s", exe)
186	end
187
188	if watcher.process:findprocess(exe) then
189		log.info("hooked: %s", exe)
190		love.window.setTitle("M'Overlay - Dolphin hooked")
191	end
192
193	if not watcher.process:hasGamecubeRAMOffset() and watcher.process:findGamecubeRAMOffset() then
194		log.info("watching ram: %s", exe)
195		watcher.init()
196	elseif watcher.process:hasProcess() and watcher.process:hasGamecubeRAMOffset() then
197		watcher.checkmemoryvalues()
198	end
199end
200
201function watcher.checkmemoryvalues()
202	local frame = watcher.frame
203
204	for address, type in pairs(watcher.watching_addr) do
205		local value = watcher.readType(type, address)
206		if watcher.values_memory[address] ~= value then
207			local info = watcher.map[address]
208			local numValue = tonumber(value) or (value and 1 or 0)
209
210			if info.debug then
211				log.debug("[%d][%08X = %08X] %s = %s", frame, address, numValue, info.name, value)
212			end
213
214			watcher.values_memory[address] = value
215			watcher.setTableValue(info.name, watcher.values_memory[address])
216			watcher.hookRun(info.name, value)
217		end
218	end
219
220	for address, pointing in pairs(watcher.pointer_loc) do
221		local ptr_addr = watcher.process:readInt(address)
222		if watcher.pointer_loc[address] ~= ptr_addr then
223			watcher.pointer_loc[address] = ptr_addr
224
225			log.debug("[%d] pointer at %08X changed location to %08X", frame, address, ptr_addr)
226
227			if ptr_addr == 0x00000000 then
228				watcher.values_pointer[address] = {}
229			end
230		end
231	end
232
233	for address, offsets in pairs(watcher.watching_ptr_addr) do
234		local ptr_addr = watcher.pointer_loc[address]
235		if ptr_addr and ptr_addr ~= 0x00000000 then
236
237			local info = watcher.map[address]
238
239			for offset, type in pairs(offsets) do
240
241				local value = watcher.readType(type, ptr_addr + offset)
242
243				-- If the location of the pointer changed, or our value changed..
244				if watcher.values_pointer[address][offset] ~= value then
245				
246					if info and info.struct and info.struct[offset] then
247						local name = string.format("%s.%s", info.name, info.struct[offset].name)
248						local numValue = tonumber(value) or (value and 1 or 0)
249
250						if info.debug or info.struct[offset].debug then
251							log.debug("[%d][%08X->%08X->%08X = %08X] %s = %s", frame, address, ptr_addr, ptr_addr + offset, numValue, name, value)
252						end
253
254						watcher.values_pointer[address][offset] = value
255						watcher.setTableValue(name, watcher.values_pointer[address][offset])
256						watcher.hookRun(name, value)
257					end
258				end
259			end
260		end
261	end
262end
263
264return watcher