fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
eefe2ce
gui
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 { 49HANDLE process_handle ; 50ULONG_PTR dolphin_base_addr ; 51} MEMORY_STRUCT ; 52 53typedef struct tagPROCESSENTRY32 { 54DWORD dwSize ; 55DWORD cntUsage ; 56DWORD th32ProcessID ; 57ULONG_PTR th32DefaultHeapID ; // DWORD on 32-bit 58DWORD th32ModuleID ; 59DWORD cntThreads ; 60DWORD th32ParentProcessID ; 61LONG pcPriClassBase ; 62DWORD dwFlags ; 63CHAR szExeFile [ 260 ]; 64} PROCESSENTRY32 , * PPROCESSENTRY32 , * LPPROCESSENTRY32 ; 65 66HANDLE CreateToolhelp32Snapshot ( 67DWORD dwFlags , 68DWORD th32ProcessID 69); 70 71BOOL Process32First ( 72HANDLE hSnapshot , 73LPPROCESSENTRY32 lppe 74); 75 76BOOL Process32Next ( 77HANDLE hSnapshot , 78LPPROCESSENTRY32 lppe 79); 80 81HANDLE OpenProcess ( 82DWORD dwDesiredAccess , 83BOOL bInheritHandle , 84DWORD dwProcessId 85); 86 87BOOL ReadProcessMemory ( 88HANDLE hProcess , 89LPCVOID lpBaseAddress , 90LPVOID lpBuffer , 91SIZE_T nSize , 92SIZE_T * lpNumberOfBytesRead 93); 94 95BOOL CloseHandle ( 96HANDLE hObject 97); 98 99typedef struct _MEMORY_BASIC_INFORMATION { 100PVOID BaseAddress ; 101PVOID AllocationBase ; 102DWORD AllocationProtect ; 103SIZE_T RegionSize ; 104DWORD State ; 105DWORD Protect ; 106DWORD Type ; 107} MEMORY_BASIC_INFORMATION , * PMEMORY_BASIC_INFORMATION ; 108 109typedef union _PSAPI_WORKING_SET_EX_BLOCK { 110ULONG_PTR Flags ; 111/*union { 112struct { 113ULONG_PTR Valid : 1; 114ULONG_PTR ShareCount : 3; 115ULONG_PTR Win32Protection : 11; 116ULONG_PTR Shared : 1; 117ULONG_PTR Node : 6; 118ULONG_PTR Locked : 1; 119ULONG_PTR LargePage : 1; 120ULONG_PTR Reserved : 7; 121ULONG_PTR Bad : 1; 122ULONG_PTR ReservedUlong : 32; 123}; 124struct { 125ULONG_PTR Valid : 1; 126ULONG_PTR Reserved0 : 14; 127ULONG_PTR Shared : 1; 128ULONG_PTR Reserved1 : 15; 129ULONG_PTR Bad : 1; 130ULONG_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 { 136PVOID VirtualAddress ; 137PSAPI_WORKING_SET_EX_BLOCK VirtualAttributes ; 138} PSAPI_WORKING_SET_EX_INFORMATION , * PPSAPI_WORKING_SET_EX_INFORMATION ; 139 140SIZE_T VirtualQueryEx ( 141HANDLE hProcess , 142LPCVOID lpAddress , 143PMEMORY_BASIC_INFORMATION lpBuffer , 144SIZE_T dwLength 145); 146 147BOOL QueryWorkingSetEx ( 148HANDLE hProcess , 149PVOID pv , 150DWORD cb 151); 152 153BOOL GetExitCodeProcess ( 154HANDLE hProcess , 155LPDWORD 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 ) 175return 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 ) 183if self : hasProcess () then return false end 184 185local processes =kernel . CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS , 0 ) -- Create the snapshot 186 187local pe32 =PROCESSENTRY32_PTR ()[ 0 ] -- Will store the info about the process 188pe32 . dwSize =sizeof ( pe32 ) 189 190repeat 191if string ( pe32 . szExeFile ) == name then 192self . process_handle =kernel . OpenProcess ( PROCESS_VM_OPERATION + PROCESS_VM_READ + PROCESS_QUERY_INFORMATION , false , pe32 . th32ProcessID ); 193break 194end 195until kernel . Process32Next ( processes , pe32 ) == 0 196 197kernel . CloseHandle ( processes ) 198 199return self : hasProcess () 200end 201 202function MEMORY : isProcessActive () 203if self . process_handle ~= nil then 204local status =new ( "DWORD[1]" ) 205kernel . GetExitCodeProcess ( self . process_handle , status ) 206return status [ 0 ] == STILL_ACTIVE 207end 208return false 209end 210 211function MEMORY : hasProcess () 212return self . process_handle ~= nil 213end 214 215function MEMORY : hasGamecubeRAMOffset () 216return self . dolphin_base_addr ~= 0 217end 218 219function MEMORY : close () 220if self : hasProcess () then 221kernel . CloseHandle ( self . process_handle ) 222self . process_handle =nil 223self . dolphin_base_addr =0 224end 225end 226 227function MEMORY : __gc () 228self : close () 229end 230 231function MEMORY : findGamecubeRAMOffset () 232local info =MEMORY_BASIC_INFORMATION_PTR ()[ 0 ] 233 234local p =new ( "unsigned char*[1]" , nil )[ 0 ] 235 236while kernel . VirtualQueryEx ( self . process_handle , p , info , sizeof ( info )) == sizeof ( info ) do 237p =p + info . RegionSize 238 239if ( info . RegionSize == 0x2000000 and info . Type == MEM_MAPPED ) then 240local wsinfo =PSAPI_WORKING_SET_EX_INFORMATION_PTR ()[ 0 ] 241wsinfo . VirtualAddress =info . BaseAddress 242 243if psapi . QueryWorkingSetEx ( self . process_handle , wsinfo , sizeof ( wsinfo )) == 1 then 244local flags =tonumber ( wsinfo . VirtualAttributes . Flags ) 245if band ( flags , lshift ( 1 , 0 )) == 1 then -- Check if the Valid flag is set 246self . dolphin_base_addr =cast ( "ULONG_PTR" , info . BaseAddress ) 247log . debug ( "Gamecube memory found: %08X" , tonumber ( self . dolphin_base_addr )) 248return true 249end 250end 251end 252end 253 254return false 255end 256 257local function read ( mem , addr , output , size ) 258local read =new ( "SIZE_T[1]" ) 259local success =kernel . ReadProcessMemory ( mem . process_handle , ffi . cast ( "LPCVOID" , mem . dolphin_base_addr + ( addr % 0x80000000 )), output , size , read ) 260 261if success and read [ 0 ] == size then 262return true 263else 264mem : close () 265return false 266end 267end 268 269function MEMORY : readByte ( addr ) 270if not self : hasProcess () then return 0 end 271local output =new ( "int8_t[1]" ) 272read ( self , addr , output , sizeof ( output )) 273return output [ 0 ] 274end 275 276function MEMORY : readBool ( addr ) 277return self : readByte ( addr ) == 1 278end 279 280function MEMORY : readUByte ( addr ) 281if not self : hasProcess () then return 0 end 282local output =new ( "uint8_t[1]" ) 283read ( self , addr , output , sizeof ( output )) 284return output [ 0 ] 285end 286 287local function bswap16 ( n ) 288return bor ( rshift ( n , 8 ), lshift ( band ( n , 0xFF ), 8 )) 289end 290 291function MEMORY : readShort ( addr ) 292if not self : hasProcess () then return 0 end 293local output =new ( "int16_t[1]" ) 294read ( self , addr , output , sizeof ( output )) 295return bswap16 ( output [ 0 ]) 296end 297 298function MEMORY : readUShort ( addr ) 299if not self : hasProcess () then return 0 end 300local output =new ( "uint16_t[1]" ) 301read ( self , addr , output , sizeof ( output )) 302return bswap16 ( output [ 0 ]) 303end 304 305local flatconversion =ffi . new ( "union { uint32_t i; float f; }" ) 306 307function MEMORY : readFloat ( addr ) 308if not self : hasProcess () then return 0 end 309local output =new ( "uint32_t[1]" ) 310read ( self , addr , output , sizeof ( output )) 311flatconversion . i =bswap ( output [ 0 ]) 312return flatconversion . f 313end 314 315function MEMORY : readInt ( addr ) 316if not self : hasProcess () then return 0 end 317local output =new ( "int32_t[1]" ) 318read ( self , addr , output , sizeof ( output )) 319return bswap ( output [ 0 ]) 320end 321 322function MEMORY : readUInt ( addr ) 323if not self : hasProcess () then return 0 end 324local output =new ( "uint32_t[1]" ) 325read ( self , addr , output , sizeof ( output )) 326return bswap ( output [ 0 ]) 327end 328 329function MEMORY : read ( addr , len ) 330if not self : hasProcess () then return "" end 331local output =new ( "unsigned char[?]" , len ) 332read ( self , addr , output , sizeof ( output )) 333return string ( output , sizeof ( output )) 334end 335 336return MEMORY