fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
b58a563
GUI
1local ffi =require ( "ffi" ) 2local log =require ( "log" ) 3 4local format =string . format 5local sub =string . sub 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 libc =load ( "libc.so" ) 23 24cdef [[ 25typedef signed int pid_t ; 26 27typedef struct { 28pid_t pid ; 29unsigned __int64 dolphin_base_addr ; 30} MEMORY_STRUCT ; 31 32typedef struct { 33void * iov_base ; /* pointer to start of buffer */ 34size_t iov_len ; /* size of buffer in bytes */ 35} iovec ; 36 37ssize_t process_vm_readv ( pid_t pid , 38iovec * local_iov , 39unsigned long liovcnt , 40iovec * remote_iov , 41unsigned long riovcnt , 42unsigned long flags ); 43 44ssize_t process_vm_writev ( pid_t pid , 45iovec * local_iov , 46unsigned long liovcnt , 47iovec * remote_iov , 48unsigned long riovcnt , 49unsigned long flags ); 50]] 51 52local MEMORY ={} 53MEMORY . __index =MEMORY 54MEMORY . init =metatype ( "MEMORY_STRUCT" , MEMORY ) 55 56local lfs =require ( "lfs" ) 57 58local function NOTWITHINMEMRANGE ( addr ) 59return addr < 0x80000000 or addr > 0x81800000 60end 61 62function MEMORY : findprocess ( name ) 63if self : hasProcess () then return false end 64 65for pid in lfs . dir ( "/proc" ) do 66if pid ~= "." and pid ~= ".." then 67local attr =lfs . attributes ( "/proc/" .. pid ) 68if attr and attr . mode == "directory" then 69local f =io . open ( "/proc/" .. pid .. "/comm" ) 70if f then 71local line =f : read ( "*line" ) 72if line == "dolphin-emu" or line == "dolphin-emu-qt2" or line == "dolphin-emu-wx" then 73self . pid =tonumber ( pid ) 74end 75f : close () 76end 77end 78end 79end 80 81return self : hasProcess () 82end 83 84function MEMORY : isProcessActive () 85if self . pid ~= 0 then 86--local status = new("DWORD[1]") 87--kernel.GetExitCodeProcess(self.pid, status) 88--return status[0] == STILL_ACTIVE 89return true 90end 91return false 92end 93 94function MEMORY : hasProcess () 95return self . pid ~= 0 96end 97 98function MEMORY : hasGamecubeRAMOffset () 99return self . dolphin_base_addr ~= 0 100end 101 102function MEMORY : close () 103if self : hasProcess () then 104self . pid =0 105self . dolphin_base_addr =0 106end 107end 108 109function MEMORY : __gc () 110self : close () 111end 112 113function MEMORY : findGamecubeRAMOffset () 114if self : hasProcess () then 115local maps ="/proc/" .. self . pid .. "/maps" 116local attr =lfs . attributes ( maps ) 117if attr . mode == "file" then 118local f =io . open ( maps ) 119if f then 120local line 121repeat 122line =f : read ( "*line" ) 123if not line then break end 124if # line > 74 then 125if sub ( line , 74 , 74 + 18 ) == "/dev/shm/dolphinmem" or sub ( line , 74 , 74 + 19 ) == "/dev/shm/dolphin-emu" then 126local startAddr =tonumber ( sub ( line , 1 , 12 ), 16 ) 127local endAddr =tonumber ( sub ( line , 14 , 14 + 12 ), 16 ) 128local size =endAddr - startAddr 129if size == 0x2000000 then 130self . dolphin_base_addr =startAddr 131return true 132end 133end 134end 135until false 136f : close () 137end 138end 139end 140 141return false 142end 143 144local function read ( mem , addr , output , size ) 145local localvec =new ( "iovec[1]" ) 146local remotevec =new ( "iovec[1]" ) 147 148local ramaddr =mem . dolphin_base_addr + ( addr % 0x80000000 ) 149 150localvec [ 0 ]. iov_base =output 151localvec [ 0 ]. iov_len =size 152 153remotevec [ 0 ]. iov_base =ffi . cast ( "void*" , ramaddr ) 154remotevec [ 0 ]. iov_len =size 155 156local read =libc . process_vm_readv ( mem . pid , localvec , 1 , remotevec , 1 , 0 ) 157 158if read == size then 159return true 160else 161mem : close () 162return false 163end 164end 165 166function MEMORY : readByte ( addr ) 167if not self : hasProcess () then return 0 end 168local output =new ( "int8_t[1]" ) 169read ( self , addr , output , sizeof ( output )) 170return output [ 0 ] 171end 172 173function MEMORY : readBool ( addr ) 174return self : readByte ( addr ) == 1 175end 176 177function MEMORY : readUByte ( addr ) 178if not self : hasProcess () then return 0 end 179local output =new ( "uint8_t[1]" ) 180read ( self , addr , output , sizeof ( output )) 181return output [ 0 ] 182end 183 184local function bswap16 ( n ) 185return bor ( rshift ( n , 8 ), lshift ( band ( n , 0xFF ), 8 )) 186end 187 188function MEMORY : readShort ( addr ) 189if not self : hasProcess () then return 0 end 190local output =new ( "int16_t[1]" ) 191read ( self , addr , output , sizeof ( output )) 192return bswap16 ( output [ 0 ]) 193end 194 195function MEMORY : readUShort ( addr ) 196if not self : hasProcess () then return 0 end 197local output =new ( "uint16_t[1]" ) 198read ( self , addr , output , sizeof ( output )) 199return bswap16 ( output [ 0 ]) 200end 201 202local flatconversion =ffi . new ( "union { uint32_t i; float f; }" ) 203 204function MEMORY : readFloat ( addr ) 205if not self : hasProcess () then return 0 end 206local output =new ( "uint32_t[1]" ) 207read ( self , addr , output , sizeof ( output )) 208flatconversion . i =bswap ( output [ 0 ]) 209return flatconversion . f 210end 211 212function MEMORY : readInt ( addr ) 213if not self : hasProcess () then return 0 end 214local output =new ( "int32_t[1]" ) 215read ( self , addr , output , sizeof ( output )) 216return bswap ( output [ 0 ]) 217end 218 219function MEMORY : readUInt ( addr ) 220if not self : hasProcess () then return 0 end 221local output =new ( "uint32_t[1]" ) 222read ( self , addr , output , sizeof ( output )) 223return bswap ( output [ 0 ]) 224end 225 226function MEMORY : read ( addr , len ) 227if not self : hasProcess () then return "" end 228local output =new ( "unsigned char[?]" , len ) 229read ( self , addr , output , sizeof ( output )) 230return string ( output , sizeof ( output )) 231end 232 233return MEMORY