fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
bc297e6
settings-overhaul
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 24local libc =ffi . C 25 26cdef [[ 27// Stupid Windows typenames.. 28 29//#define MAX_PATH 260 30 31typedef int BOOL ; 32typedef unsigned long DWORD ; 33typedef long LONG ; 34 35typedef void * PVOID ; 36typedef PVOID HANDLE ; 37 38typedef void * LPVOID ; 39typedef const void * LPCVOID ; 40 41typedef DWORD * LPDWORD ; 42 43typedef char CHAR ; 44 45typedef unsigned __int64 ULONG_PTR ; // 64-bit 46// typedef unsigned long ULONG_PTR; // 32-bit 47 48typedef ULONG_PTR SIZE_T ; 49 50typedef struct { 51HANDLE process_handle ; 52ULONG_PTR dolphin_base_addr ; 53ULONG_PTR dolphin_addr_size ; 54} MEMORY_STRUCT ; 55 56typedef struct tagPROCESSENTRY32 { 57DWORD dwSize ; 58DWORD cntUsage ; 59DWORD th32ProcessID ; 60ULONG_PTR th32DefaultHeapID ; // DWORD on 32-bit 61DWORD th32ModuleID ; 62DWORD cntThreads ; 63DWORD th32ParentProcessID ; 64LONG pcPriClassBase ; 65DWORD dwFlags ; 66CHAR szExeFile [ 260 ]; 67} PROCESSENTRY32 , * PPROCESSENTRY32 , * LPPROCESSENTRY32 ; 68 69HANDLE CreateToolhelp32Snapshot ( 70DWORD dwFlags , 71DWORD th32ProcessID 72); 73 74BOOL Process32First ( 75HANDLE hSnapshot , 76LPPROCESSENTRY32 lppe 77); 78 79BOOL Process32Next ( 80HANDLE hSnapshot , 81LPPROCESSENTRY32 lppe 82); 83 84HANDLE OpenProcess ( 85DWORD dwDesiredAccess , 86BOOL bInheritHandle , 87DWORD dwProcessId 88); 89 90BOOL ReadProcessMemory ( 91HANDLE hProcess , 92LPCVOID lpBaseAddress , 93LPVOID lpBuffer , 94SIZE_T nSize , 95SIZE_T * lpNumberOfBytesRead 96); 97 98BOOL WriteProcessMemory ( 99HANDLE hProcess , 100LPVOID lpBaseAddress , 101LPCVOID lpBuffer , 102SIZE_T nSize , 103SIZE_T * lpNumberOfBytesWritten 104); 105 106BOOL CloseHandle ( 107HANDLE hObject 108); 109 110typedef struct _MEMORY_BASIC_INFORMATION { 111PVOID BaseAddress ; 112PVOID AllocationBase ; 113DWORD AllocationProtect ; 114SIZE_T RegionSize ; 115DWORD State ; 116DWORD Protect ; 117DWORD Type ; 118} MEMORY_BASIC_INFORMATION , * PMEMORY_BASIC_INFORMATION ; 119 120typedef union _PSAPI_WORKING_SET_EX_BLOCK { 121ULONG_PTR Flags ; 122/*union { 123struct { 124ULONG_PTR Valid : 1; 125ULONG_PTR ShareCount : 3; 126ULONG_PTR Win32Protection : 11; 127ULONG_PTR Shared : 1; 128ULONG_PTR Node : 6; 129ULONG_PTR Locked : 1; 130ULONG_PTR LargePage : 1; 131ULONG_PTR Reserved : 7; 132ULONG_PTR Bad : 1; 133ULONG_PTR ReservedUlong : 32; 134}; 135struct { 136ULONG_PTR Valid : 1; 137ULONG_PTR Reserved0 : 14; 138ULONG_PTR Shared : 1; 139ULONG_PTR Reserved1 : 15; 140ULONG_PTR Bad : 1; 141ULONG_PTR ReservedUlong : 32; 142} Invalid; 143};*/ 144} PSAPI_WORKING_SET_EX_BLOCK , * PPSAPI_WORKING_SET_EX_BLOCK ; 145 146typedef struct _PSAPI_WORKING_SET_EX_INFORMATION { 147PVOID VirtualAddress ; 148PSAPI_WORKING_SET_EX_BLOCK VirtualAttributes ; 149} PSAPI_WORKING_SET_EX_INFORMATION , * PPSAPI_WORKING_SET_EX_INFORMATION ; 150 151SIZE_T VirtualQueryEx ( 152HANDLE hProcess , 153LPCVOID lpAddress , 154PMEMORY_BASIC_INFORMATION lpBuffer , 155SIZE_T dwLength 156); 157 158BOOL QueryWorkingSetEx ( 159HANDLE hProcess , 160PVOID pv , 161DWORD cb 162); 163 164BOOL GetExitCodeProcess ( 165HANDLE hProcess , 166LPDWORD lpExitCode 167); 168 169DWORD WaitForSingleObject ( 170HANDLE hHandle , 171DWORD dwMilliseconds 172); 173 174DWORD GetLastError (); 175]] 176 177local MEMORY ={} 178MEMORY . __index =MEMORY 179MEMORY . init =metatype ( "MEMORY_STRUCT" , MEMORY ) 180 181local TH32CS_SNAPPROCESS =0x02 182 183local PROCESS_QUERY_INFORMATION =0x0400 184local PROCESS_VM_OPERATION =0x0008 185local PROCESS_VM_READ =0x0010 186local PROCESS_VM_WRITE =0x0020 187 188local MEM_MAPPED =0x40000 189 190local STILL_ACTIVE =259 191 192local function NOTWITHINMEMRANGE ( addr ) 193return addr < 0x80000000 or addr > 0x81800000 194end 195 196local PROCESSENTRY32_PTR =typeof ( "PROCESSENTRY32[1]" ) 197local MEMORY_BASIC_INFORMATION_PTR =typeof ( "MEMORY_BASIC_INFORMATION[1]" ) 198local PSAPI_WORKING_SET_EX_INFORMATION_PTR =typeof ( "PSAPI_WORKING_SET_EX_INFORMATION[1]" ) 199 200function MEMORY : hasPermissions () 201-- Windows doesn't need any special permissions to read memory from another process 202return true 203end 204 205local valid_process_names ={ 206[ "Dolphin.exe" ] =true , 207[ "Slippi Dolphin.exe" ] =true , 208[ "DolphinWx.exe" ] =true , 209[ "DolphinQt2.exe" ] =true , 210} 211 212do 213local status =new ( "DWORD[1]" ) 214 215function MEMORY : findprocess () 216if self : hasProcess () then return false end 217 218local handle 219local snapshot =kernel . CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS , 0 ) -- Create the snapshot 220 221local pe32 =PROCESSENTRY32_PTR ()[ 0 ] -- Will store the info about the process 222pe32 . dwSize =sizeof ( pe32 ) 223 224repeat 225local name =string ( pe32 . szExeFile ) 226if valid_process_names [ name ] then 227local handle =kernel . OpenProcess ( PROCESS_VM_OPERATION + PROCESS_VM_READ + PROCESS_QUERY_INFORMATION , false , pe32 . th32ProcessID ) 228 229-- Check if the process is active, we don't want to rehook the closing application 230if handle ~= nil and kernel . GetExitCodeProcess ( handle , status ) and status [ 0 ] == STILL_ACTIVE then 231-- We have an active process that matches, let's use it 232self . process_handle =handle 233break 234elseif handle then 235-- Dolphin is closing.. ignore and close the handle.. 236kernel . CloseHandle ( handle ) 237end 238end 239until kernel . Process32Next ( snapshot , pe32 ) == 0 240 241kernel . CloseHandle ( snapshot ) 242 243return self : hasProcess () 244end 245end 246 247do 248local status =new ( "DWORD[1]" ) 249 250function MEMORY : isProcessActive () 251return self . process_handle ~= nil and kernel . GetExitCodeProcess ( self . process_handle , status ) and status [ 0 ] == STILL_ACTIVE 252end 253end 254 255function MEMORY : hasProcess () 256return self . process_handle ~= nil 257end 258 259function MEMORY : clearGamecubeRAMOffset () 260self . dolphin_base_addr =nil 261end 262 263function MEMORY : hasGamecubeRAMOffset () 264return self . dolphin_base_addr ~= nil 265end 266 267function MEMORY : getGamecubeRAMOffset () 268return tonumber ( self . dolphin_base_addr ) 269end 270 271function MEMORY : getGamecubeRAMSize () 272return tonumber ( self . dolphin_addr_size ) 273end 274 275function MEMORY : close () 276if self : hasProcess () then 277kernel . CloseHandle ( self . process_handle ) 278self . process_handle =nil 279self . dolphin_base_addr =nil 280end 281end 282 283function MEMORY : __gc () 284self : close () 285end 286 287local regionptr =new ( "unsigned char*[1]" , nil ) 288 289local NULL =cast ( "uint32_t" , 0x00000000 ) 290 291function MEMORY : findGamecubeRAMOffset () 292local info =MEMORY_BASIC_INFORMATION_PTR ()[ 0 ] 293 294local p =regionptr [ 0 ] 295 296while kernel . VirtualQueryEx ( self . process_handle , p , info , sizeof ( info )) == sizeof ( info ) do 297p =p + info . RegionSize 298 299if ( info . RegionSize >= 0x2000000 and info . Type == MEM_MAPPED ) then 300local wsinfo =PSAPI_WORKING_SET_EX_INFORMATION_PTR ()[ 0 ] 301wsinfo . VirtualAddress =info . BaseAddress 302 303if psapi . QueryWorkingSetEx ( self . process_handle , wsinfo , sizeof ( wsinfo )) == 1 then 304local flags =tonumber ( wsinfo . VirtualAttributes . Flags ) 305 306local base =cast ( "ULONG_PTR" , info . BaseAddress ) 307local region =cast ( "ULONG_PTR" , info . RegionSize ) 308 309if band ( flags , lshift ( 1 , 0 )) == 1 and cast ( "uint32_t" , info . BaseAddress ) ~= NULL then -- Check if the Valid flag is set 310--log.debug("%08X %x", tonumber(base), tonumber(region)) 311self . dolphin_base_addr =base 312self . dolphin_addr_size =region 313return true 314end 315end 316end 317end 318 319return false 320end 321 322local GC_RAM_START =cast ( "uint32_t" , 0x80000000 ) 323local GC_RAM_END =cast ( "uint32_t" , 0x81800000 ) 324local WII_RAM_START =cast ( "uint32_t" , 0x90000000 ) 325local WII_RAM_END =cast ( "uint32_t" , 0x94000000 ) 326 327local WII_RAM_LOCAL_START =cast ( "uint32_t" , 0x02000000 ) 328 329local CAST_ADDR =ffi . new ( "uint32_t" , 0x00000000 ) 330local memread =ffi . new ( "SIZE_T[1]" ) -- How many bytes are read from memory 331 332function MEMORY : read ( addr , output , size ) 333if not self : hasProcess () or not self : hasGamecubeRAMOffset () then return false end 334 335CAST_ADDR =cast ( "uint32_t" , addr ) 336 337if CAST_ADDR >= WII_RAM_START and CAST_ADDR <= WII_RAM_END then 338CAST_ADDR =cast ( "uint32_t" , WII_RAM_LOCAL_START + ( CAST_ADDR % WII_RAM_START )) 339elseif CAST_ADDR >= GC_RAM_START and CAST_ADDR <= GC_RAM_END then 340CAST_ADDR =cast ( "uint32_t" , CAST_ADDR % GC_RAM_START ) 341else 342log . warn ( "[MEMORY] Attempt to read from invalid address %08X" , tonumber ( CAST_ADDR )) 343return false 344end 345 346local raddr =cast ( "LPCVOID" , self . dolphin_base_addr + CAST_ADDR ) 347local success =kernel . ReadProcessMemory ( self . process_handle , raddr , output , size , memread ) 348if not success then 349log . debug ( "[MEMORY] Failed reading from address [%08X] ERROR #%d" , CAST_ADDR , tonumber ( kernel . GetLastError ())) 350--else 351-- log.debug("[MEMORY] read 0x%X size 0x%X bytes from 0x%08X = %q", tonumber(memread[0]), size, tonumber(CAST_ADDR), tohex(ffi.string(output, memread[0]))) 352end 353return success and memread [ 0 ] == size 354end 355 356local memwritten =ffi . new ( "SIZE_T[1]" ) 357 358function MEMORY : write ( addr , input , size ) 359if not self : hasProcess () or not self : hasGamecubeRAMOffset () then return false end 360local waddr =cast ( "LPVOID" , self . dolphin_base_addr + ( addr % 0x80000000 )) 361local success =kernel . WriteProcessMemory ( self . process_handle , waddr , input , size , memwritten ) 362if not success then 363log . debug ( "[MEMORY] Failed writing to address [%08X = %08X] ERROR #%d" , addr , tonumber ( input ), tonumber ( kernel . GetLastError ())) 364end 365return success and memwritten [ 0 ] == size 366end 367 368return MEMORY