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