fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
4b06a2a
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 15local errno =ffi . errno 16 17local lshift =bit . lshift 18local rshift =bit . rshift 19local bswap =bit . bswap 20local band =bit . band 21local bor =bit . bor 22 23local libc =ffi . C 24 25cdef [[ 26char * strerror ( int errnum ); 27 28typedef signed int pid_t ; 29 30typedef struct { 31pid_t pid ; 32unsigned __int64 dolphin_base_addr ; 33} MEMORY_STRUCT ; 34 35typedef struct { 36void * iov_base ; /* pointer to start of buffer */ 37size_t iov_len ; /* size of buffer in bytes */ 38} iovec ; 39 40int process_vm_readv ( pid_t pid , 41iovec * local_iov , 42unsigned long liovcnt , 43iovec * remote_iov , 44unsigned long riovcnt , 45unsigned long flags ); 46 47int process_vm_writev ( pid_t pid , 48iovec * local_iov , 49unsigned long liovcnt , 50iovec * remote_iov , 51unsigned long riovcnt , 52unsigned long flags ); 53 54typedef unsigned short ino_t ; /* i-node number */ 55typedef unsigned long off_t ; /* offset within a file */ 56 57typedef struct { 58ino_t d_ino ; /* Inode number */ 59off_t d_off ; /* Not an offset; see below */ 60unsigned short d_reclen ; /* Length of this record */ 61unsigned char d_type ; /* Type of file; not supported 62by all filesystem types */ 63char name [ 256 ]; /* Null-terminated filename */ 64} dirent ; 65typedef struct __dirstream DIR ; 66 67int access ( const char * path , int amode ); 68DIR * opendir ( const char * name ); 69int closedir ( DIR * dirp ); 70dirent * readdir ( DIR * dirp ); 71]] 72 73local MEMORY ={} 74MEMORY . __index =MEMORY 75MEMORY . init =metatype ( "MEMORY_STRUCT" , MEMORY ) 76 77function MEMORY : findprocess ( name ) 78if self : hasProcess () then return false end 79 80local dir =libc . opendir ( "/proc/" ) 81 82if dir == nil then 83local message =string ( libc . strerror ( errno ())) 84error ( "error opening directory /proc/: " .. message ) 85else 86local entry 87while true do 88entry =libc . readdir ( dir ) 89if entry == nil then break end -- end of list 90local pid =string ( entry . name ) 91local f =io . open ( "/proc/" .. pid .. "/comm" ) 92if f then 93local line =f : read ( "*line" ) 94if line == "dolphin-emu" or line == "dolphin-emu-qt2" or line == "dolphin-emu-wx" then 95self . pid =tonumber ( pid ) 96end 97f : close () 98end 99end 100libc . closedir ( dir ) 101end 102 103return self : hasProcess () 104end 105 106function MEMORY : isProcessActive () 107if self . pid ~= 0 then 108return libc . access ( "/proc/" .. self . pid , 0 ) == 0 109end 110return false 111end 112 113function MEMORY : hasProcess () 114return self . pid ~= 0 115end 116 117function MEMORY : hasGamecubeRAMOffset () 118return self . dolphin_base_addr ~= 0 119end 120 121function MEMORY : close () 122if self : hasProcess () then 123self . pid =0 124self . dolphin_base_addr =0 125end 126end 127 128function MEMORY : __gc () 129self : close () 130end 131 132function MEMORY : findGamecubeRAMOffset () 133if self : hasProcess () then 134local f =io . open ( "/proc/" .. self . pid .. "/maps" ) 135if f then 136local line 137while true do 138line =f : read ( "*line" ) 139if not line then break end -- EOF 140if # line > 74 then 141if sub ( line , 74 , 74 + 18 ) == "/dev/shm/dolphinmem" or sub ( line , 74 , 74 + 19 ) == "/dev/shm/dolphin-emu" then 142local startAddr , endAddr =line : match ( "^(%x-)%-(%x-)%s" ) 143if startAddr and endAddr then 144-- Convert hex values to number 145startAddr =tonumber ( startAddr , 16 ) 146endAddr =tonumber ( endAddr , 16 ) 147if ( endAddr - startAddr ) == 0x2000000 then 148self . dolphin_base_addr =startAddr 149log . debug ( "Gamecube memory found: %08X" , tonumber ( self . dolphin_base_addr )) 150f : close () 151return true 152end 153end 154end 155end 156end 157f : close () 158end 159end 160 161return false 162end 163 164local function read ( mem , addr , output , size ) 165local localvec =new ( "iovec[1]" ) 166local remotevec =new ( "iovec[1]" ) 167 168local ramaddr =mem . dolphin_base_addr + ( addr % 0x80000000 ) 169 170localvec [ 0 ]. iov_base =output 171localvec [ 0 ]. iov_len =size 172 173remotevec [ 0 ]. iov_base =cast ( "void*" , ramaddr ) 174remotevec [ 0 ]. iov_len =size 175 176local read =libc . process_vm_readv ( mem . pid , localvec , 1 , remotevec , 1 , 0 ) 177 178if read == size then 179return true 180else 181mem : close () 182return false 183end 184end 185 186function MEMORY : readByte ( addr ) 187if not self : hasProcess () then return 0 end 188local output =new ( "int8_t[1]" ) 189read ( self , addr , output , sizeof ( output )) 190return output [ 0 ] 191end 192 193function MEMORY : readBool ( addr ) 194return self : readByte ( addr ) == 1 195end 196 197function MEMORY : readUByte ( addr ) 198if not self : hasProcess () then return 0 end 199local output =new ( "uint8_t[1]" ) 200read ( self , addr , output , sizeof ( output )) 201return output [ 0 ] 202end 203 204local function bswap16 ( n ) 205return bor ( rshift ( n , 8 ), lshift ( band ( n , 0xFF ), 8 )) 206end 207 208function MEMORY : readShort ( addr ) 209if not self : hasProcess () then return 0 end 210local output =new ( "int16_t[1]" ) 211read ( self , addr , output , sizeof ( output )) 212return bswap16 ( output [ 0 ]) 213end 214 215function MEMORY : readUShort ( addr ) 216if not self : hasProcess () then return 0 end 217local output =new ( "uint16_t[1]" ) 218read ( self , addr , output , sizeof ( output )) 219return bswap16 ( output [ 0 ]) 220end 221 222local flatconversion =ffi . new ( "union { uint32_t i; float f; }" ) 223 224function MEMORY : readFloat ( addr ) 225if not self : hasProcess () then return 0 end 226local output =new ( "uint32_t[1]" ) 227read ( self , addr , output , sizeof ( output )) 228flatconversion . i =bswap ( output [ 0 ]) 229return flatconversion . f 230end 231 232function MEMORY : readInt ( addr ) 233if not self : hasProcess () then return 0 end 234local output =new ( "int32_t[1]" ) 235read ( self , addr , output , sizeof ( output )) 236return bswap ( output [ 0 ]) 237end 238 239function MEMORY : readUInt ( addr ) 240if not self : hasProcess () then return 0 end 241local output =new ( "uint32_t[1]" ) 242read ( self , addr , output , sizeof ( output )) 243return bswap ( output [ 0 ]) 244end 245 246function MEMORY : read ( addr , len ) 247if not self : hasProcess () then return "" end 248local output =new ( "unsigned char[?]" , len ) 249read ( self , addr , output , sizeof ( output )) 250return string ( output , sizeof ( output )) 251end 252 253return MEMORY