fork/m-overlay

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

blackops7799Added build files and stuffeefe2ce

gui
7.7 KiB336 linesraw
1local ffi = require("ffi")
2local log = require("log")
3
4local format = string.format
5
6local cast = ffi.cast
7local cdef = ffi.cdef
8local load = ffi.load
9local metatype = ffi.metatype
10local new = ffi.new
11local typeof = ffi.typeof
12local sizeof = ffi.sizeof
13local string = ffi.string
14
15local lshift = bit.lshift
16local rshift = bit.rshift
17local bswap = bit.bswap
18local band = bit.band
19local bor = bit.bor
20
21local kernel = load("Kernel32.dll")
22local psapi = load("Psapi.dll")
23
24cdef [[
25// Stupid Windows typenames..
26
27//#define MAX_PATH 260
28
29typedef int BOOL;
30typedef unsigned long DWORD;
31typedef long LONG;
32
33typedef void *PVOID;
34typedef PVOID HANDLE;
35
36typedef void *LPVOID;
37typedef const void *LPCVOID;
38
39typedef DWORD *LPDWORD;
40
41typedef char CHAR;
42
43typedef unsigned __int64 ULONG_PTR; // 64-bit
44// typedef unsigned long ULONG_PTR; // 32-bit
45
46typedef ULONG_PTR SIZE_T;
47
48typedef struct {
49	HANDLE process_handle;
50	ULONG_PTR dolphin_base_addr;
51} MEMORY_STRUCT;
52
53typedef struct tagPROCESSENTRY32 {
54	DWORD dwSize;
55	DWORD cntUsage;
56	DWORD th32ProcessID;
57	ULONG_PTR th32DefaultHeapID; // DWORD on 32-bit
58	DWORD th32ModuleID;
59	DWORD cntThreads;
60	DWORD th32ParentProcessID;
61	LONG pcPriClassBase;
62	DWORD dwFlags;
63	CHAR  szExeFile[260];
64} PROCESSENTRY32,*PPROCESSENTRY32,*LPPROCESSENTRY32;
65
66HANDLE CreateToolhelp32Snapshot(
67	DWORD dwFlags,
68	DWORD th32ProcessID
69);
70
71BOOL Process32First(
72	HANDLE           hSnapshot,
73	LPPROCESSENTRY32 lppe
74);
75
76BOOL Process32Next(
77	HANDLE           hSnapshot,
78	LPPROCESSENTRY32 lppe
79);
80
81HANDLE OpenProcess(
82	DWORD dwDesiredAccess,
83	BOOL  bInheritHandle,
84	DWORD dwProcessId
85);
86
87BOOL ReadProcessMemory(
88	HANDLE  hProcess,
89	LPCVOID lpBaseAddress,
90	LPVOID  lpBuffer,
91	SIZE_T  nSize,
92	SIZE_T  *lpNumberOfBytesRead
93);
94
95BOOL CloseHandle(
96	HANDLE hObject
97);
98
99typedef struct _MEMORY_BASIC_INFORMATION {
100	PVOID  BaseAddress;
101	PVOID  AllocationBase;
102	DWORD  AllocationProtect;
103	SIZE_T RegionSize;
104	DWORD  State;
105	DWORD  Protect;
106	DWORD  Type;
107} MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;
108
109typedef union _PSAPI_WORKING_SET_EX_BLOCK {
110	ULONG_PTR Flags;
111	/*union {
112		struct {
113			ULONG_PTR Valid : 1;
114			ULONG_PTR ShareCount : 3;
115			ULONG_PTR Win32Protection : 11;
116			ULONG_PTR Shared : 1;
117			ULONG_PTR Node : 6;
118			ULONG_PTR Locked : 1;
119			ULONG_PTR LargePage : 1;
120			ULONG_PTR Reserved : 7;
121			ULONG_PTR Bad : 1;
122			ULONG_PTR ReservedUlong : 32;
123		};
124		struct {
125			ULONG_PTR Valid : 1;
126			ULONG_PTR Reserved0 : 14;
127			ULONG_PTR Shared : 1;
128			ULONG_PTR Reserved1 : 15;
129			ULONG_PTR Bad : 1;
130			ULONG_PTR ReservedUlong : 32;
131		} Invalid;
132	};*/
133} PSAPI_WORKING_SET_EX_BLOCK, *PPSAPI_WORKING_SET_EX_BLOCK;
134
135typedef struct _PSAPI_WORKING_SET_EX_INFORMATION {
136	PVOID                      VirtualAddress;
137	PSAPI_WORKING_SET_EX_BLOCK VirtualAttributes;
138} PSAPI_WORKING_SET_EX_INFORMATION, *PPSAPI_WORKING_SET_EX_INFORMATION;
139
140SIZE_T VirtualQueryEx(
141	HANDLE                    hProcess,
142	LPCVOID                   lpAddress,
143	PMEMORY_BASIC_INFORMATION lpBuffer,
144	SIZE_T                    dwLength
145);
146
147BOOL QueryWorkingSetEx(
148	HANDLE hProcess,
149	PVOID  pv,
150	DWORD  cb
151);
152
153BOOL GetExitCodeProcess(
154	HANDLE  hProcess,
155	LPDWORD lpExitCode
156);
157]]
158
159local MEMORY = {}
160MEMORY.__index = MEMORY
161MEMORY.init = metatype("MEMORY_STRUCT", MEMORY)
162
163local TH32CS_SNAPPROCESS = 0x02
164
165local PROCESS_QUERY_INFORMATION = 0x0400
166local PROCESS_VM_OPERATION = 0x0008
167local PROCESS_VM_READ = 0x0010
168local PROCESS_VM_WRITE = 0x0020
169
170local MEM_MAPPED = 0x40000
171
172local STILL_ACTIVE = 259
173
174local function NOTWITHINMEMRANGE(addr)
175	return addr < 0x80000000 or addr > 0x81800000
176end
177
178local PROCESSENTRY32_PTR = typeof("PROCESSENTRY32[1]")
179local MEMORY_BASIC_INFORMATION_PTR = typeof("MEMORY_BASIC_INFORMATION[1]")
180local PSAPI_WORKING_SET_EX_INFORMATION_PTR = typeof("PSAPI_WORKING_SET_EX_INFORMATION[1]")
181
182function MEMORY:findprocess(name)
183	if self:hasProcess() then return false end
184
185	local processes = kernel.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) -- Create the snapshot
186
187	local pe32 = PROCESSENTRY32_PTR()[0] -- Will store the info about the process
188	pe32.dwSize = sizeof(pe32)
189
190	repeat
191		if string(pe32.szExeFile) == name then
192			self.process_handle = kernel.OpenProcess(PROCESS_VM_OPERATION + PROCESS_VM_READ + PROCESS_QUERY_INFORMATION, false, pe32.th32ProcessID);
193			break
194		end
195	until kernel.Process32Next(processes, pe32) == 0
196	
197	kernel.CloseHandle(processes)
198
199	return self:hasProcess()
200end
201
202function MEMORY:isProcessActive()
203	if self.process_handle ~= nil then
204		local status = new("DWORD[1]")
205		kernel.GetExitCodeProcess(self.process_handle, status)
206		return status[0] == STILL_ACTIVE
207	end
208	return false
209end
210
211function MEMORY:hasProcess()
212	return self.process_handle ~= nil
213end
214
215function MEMORY:hasGamecubeRAMOffset()
216	return self.dolphin_base_addr ~= 0
217end
218
219function MEMORY:close()
220	if self:hasProcess() then
221		kernel.CloseHandle(self.process_handle)
222		self.process_handle = nil
223		self.dolphin_base_addr = 0
224	end
225end
226
227function MEMORY:__gc()
228	self:close()
229end
230
231function MEMORY:findGamecubeRAMOffset()
232	local info = MEMORY_BASIC_INFORMATION_PTR()[0]
233
234	local p = new("unsigned char*[1]", nil)[0]
235
236	while kernel.VirtualQueryEx(self.process_handle, p, info, sizeof(info)) == sizeof(info) do
237		p = p + info.RegionSize
238
239		if (info.RegionSize == 0x2000000 and info.Type == MEM_MAPPED) then
240			local wsinfo = PSAPI_WORKING_SET_EX_INFORMATION_PTR()[0]
241			wsinfo.VirtualAddress = info.BaseAddress
242
243			if psapi.QueryWorkingSetEx(self.process_handle, wsinfo, sizeof(wsinfo)) == 1 then
244				local flags = tonumber(wsinfo.VirtualAttributes.Flags)
245				if band(flags, lshift(1, 0)) == 1 then -- Check if the Valid flag is set
246					self.dolphin_base_addr = cast("ULONG_PTR", info.BaseAddress)
247					log.debug("Gamecube memory found: %08X", tonumber(self.dolphin_base_addr))
248					return true
249				end
250			end
251		end
252	end
253
254	return false
255end
256
257local function read(mem, addr, output, size)
258	local read = new("SIZE_T[1]")
259	local success = kernel.ReadProcessMemory(mem.process_handle, ffi.cast("LPCVOID", mem.dolphin_base_addr + (addr % 0x80000000)), output, size, read)
260
261	if success and read[0] == size then
262		return true
263	else
264		mem:close()
265		return false
266	end
267end
268
269function MEMORY:readByte(addr)
270	if not self:hasProcess() then return 0 end
271	local output = new("int8_t[1]")
272	read(self, addr, output, sizeof(output))
273	return output[0]
274end
275
276function MEMORY:readBool(addr)
277	return self:readByte(addr) == 1
278end
279
280function MEMORY:readUByte(addr)
281	if not self:hasProcess() then return 0 end
282	local output = new("uint8_t[1]")
283	read(self, addr, output, sizeof(output))
284	return output[0]
285end
286
287local function bswap16(n)
288	return bor(rshift(n, 8), lshift(band(n, 0xFF), 8))
289end
290
291function MEMORY:readShort(addr)
292	if not self:hasProcess() then return 0 end
293	local output = new("int16_t[1]")
294	read(self, addr, output, sizeof(output))
295	return bswap16(output[0])
296end
297
298function MEMORY:readUShort(addr)
299	if not self:hasProcess() then return 0 end
300	local output = new("uint16_t[1]")
301	read(self, addr, output, sizeof(output))
302	return bswap16(output[0])
303end
304
305local flatconversion = ffi.new("union { uint32_t i; float f; }")
306
307function MEMORY:readFloat(addr)
308	if not self:hasProcess() then return 0 end
309	local output = new("uint32_t[1]")
310	read(self, addr, output, sizeof(output))
311	flatconversion.i = bswap(output[0])
312	return flatconversion.f
313end
314
315function MEMORY:readInt(addr)
316	if not self:hasProcess() then return 0 end
317	local output = new("int32_t[1]")
318	read(self, addr, output, sizeof(output))
319	return bswap(output[0])
320end
321
322function MEMORY:readUInt(addr)
323	if not self:hasProcess() then return 0 end
324	local output = new("uint32_t[1]")
325	read(self, addr, output, sizeof(output))
326	return bswap(output[0])
327end
328
329function MEMORY:read(addr, len)
330	if not self:hasProcess() then return "" end
331	local output = new("unsigned char[?]", len)
332	read(self, addr, output, sizeof(output))
333	return string(output, sizeof(output))
334end
335
336return MEMORY