fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
7537829
v2.1.6
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 [[ 26static const int MAXCOMLEN = 16 ; 27 28char * strerror ( int errnum ); 29 30typedef struct task * task_t ; 31typedef task_t task_port_t ; 32 33typedef signed int pid_t ; 34typedef unsigned int uid_t ; 35typedef unsigned int gid_t ; 36typedef unsigned long uintptr_t ; 37typedef unsigned long long u_int64_t ; 38 39typedef uint32_t natural_t ; 40typedef uint32_t mach_port_t ; 41typedef uint64_t mach_vm_address_t ; 42typedef uint64_t mach_vm_size_t ; 43typedef mach_port_t vm_map_read_t ; 44typedef natural_t mach_msg_type_number_t ; 45typedef natural_t mach_port_t ; 46typedef int vm_region_flavor_t ; 47typedef int * vm_region_info_t ; 48 49typedef struct { 50pid_t pid ; 51task_t task ; 52unsigned __int64 dolphin_base_addr ; 53unsigned __int64 dolphin_addr_size ; 54} MEMORY_STRUCT ; 55 56typedef struct proc_bsdinfo { 57uint32_t pbi_flags ; /* 64bit; emulated etc */ 58uint32_t pbi_status ; 59uint32_t pbi_xstatus ; 60uint32_t pbi_pid ; 61uint32_t pbi_ppid ; 62uid_t pbi_uid ; 63gid_t pbi_gid ; 64uid_t pbi_ruid ; 65gid_t pbi_rgid ; 66uid_t pbi_svuid ; 67gid_t pbi_svgid ; 68uint32_t rfu_1 ; /* reserved */ 69char pbi_comm [ MAXCOMLEN ]; 70char pbi_name [ 2 * MAXCOMLEN ]; /* empty if no name is registered */ 71uint32_t pbi_nfiles ; 72uint32_t pbi_pgid ; 73uint32_t pbi_pjobc ; 74uint32_t e_tdev ; /* controlling tty dev */ 75uint32_t e_tpgid ; /* tty process group id */ 76int32_t pbi_nice ; 77uint64_t pbi_start_tvsec ; 78uint64_t pbi_start_tvusec ; 79} proc_bsdinfo ; 80 81typedef struct proc_bsdshortinfo { 82uint32_t pbsi_pid ; /* process id */ 83uint32_t pbsi_ppid ; /* process parent id */ 84uint32_t pbsi_pgid ; /* process perp id */ 85uint32_t pbsi_status ; /* p_stat value, SZOMB, SRUN, etc */ 86char pbsi_comm [ MAXCOMLEN ]; /* upto 16 characters of process name */ 87uint32_t pbsi_flags ; /* 64bit; emulated etc */ 88uid_t pbsi_uid ; /* current uid on process */ 89gid_t pbsi_gid ; /* current gid on process */ 90uid_t pbsi_ruid ; /* current ruid on process */ 91gid_t pbsi_rgid ; /* current tgid on process */ 92uid_t pbsi_svuid ; /* current svuid on process */ 93gid_t pbsi_svgid ; /* current svgid on process */ 94uint32_t pbsi_rfu ; /* reserved for future use*/ 95} proc_bsdshortinfo ; 96 97int proc_listpids ( uint32_t type , uint32_t typeinfo , void * buffer , int buffersize ); // , int32_t * retval 98int proc_pidinfo ( int pid , int flavor , uint64_t arg , void * buffer , uint32_t buffersize ); // , int32_t * retval 99int vm_read ( uint32_t target_task , uintptr_t address , uintptr_t size , uintptr_t * data , uint32_t * dataCnt ); 100 101pid_t getpgid ( pid_t pid ); 102 103int task_for_pid ( mach_port_t task , pid_t pid , task_port_t * target ); 104mach_port_t mach_task_self ( void ); 105 106int mach_vm_region ( vm_map_read_t target_task , mach_vm_address_t * address , mach_vm_size_t * size , vm_region_flavor_t flavor , vm_region_info_t info , mach_msg_type_number_t * infoCnt , mach_port_t * object_name ); 107]] 108 109local PROC_ALL_PIDS =1 110local PROC_PIDTBSDINFO =3 111 112local MEMORY ={} 113MEMORY . __index =MEMORY 114MEMORY . init =metatype ( "MEMORY_STRUCT" , MEMORY ) 115MEMORY . task =new ( "task_t[1]" ) 116 117function MEMORY : hasPermissions () 118return true 119end 120 121local valid_process_names ={ 122[ "Dolphin" ] =true , 123[ "Slippi Dolphin" ] =true , 124[ "DolphinWx" ] =true , 125[ "DolphinQt2" ] =true , 126} 127 128local PROC_BSDINFO_PTR =typeof ( "proc_bsdinfo[1]" ) 129 130function MEMORY : findprocess () 131if self : hasProcess () then return false end 132 133local num_pids =libc . proc_listpids ( PROC_ALL_PIDS , 0 , nil , 0 ) 134local pids =new ( "pid_t[?]" , num_pids ) 135libc . proc_listpids ( PROC_ALL_PIDS , 0 , pids , sizeof ( pids )) 136 137local proc =PROC_BSDINFO_PTR ()[ 0 ] 138 139for i =0 , num_pids do 140local pid =pids [ i ] 141if pid ~= 0 then 142local st =libc . proc_pidinfo ( pid , PROC_PIDTBSDINFO , 0 , proc , sizeof ( PROC_BSDINFO_PTR )) 143if valid_process_names [ string ( proc . pbi_name )] then 144self . pid =pid 145log . debug ( "Found dolphin-emu process: /proc/%d" , self . pid ) 146break 147end 148end 149end 150 151return self : hasProcess () 152end 153 154function MEMORY : isProcessActive () 155if self . pid then 156return libc . getpgid ( self . pid ) >= 0 157end 158return false 159end 160 161function MEMORY : hasProcess () 162return self . pid ~= 0 163end 164 165function MEMORY : clearGamecubeRAMOffset () 166self . dolphin_base_addr =0 167end 168 169function MEMORY : hasGamecubeRAMOffset () 170return self . dolphin_base_addr ~= 0 171end 172 173function MEMORY : getGamecubeRAMOffset () 174return tonumber ( self . dolphin_base_addr ) 175end 176 177function MEMORY : getGamecubeRAMSize () 178return tonumber ( self . dolphin_addr_size ) 179end 180 181function MEMORY : close () 182if self : hasProcess () then 183self . pid =0 184self . dolphin_base_addr =0 185end 186end 187 188function MEMORY : __gc () 189self : close () 190end 191 192function MEMORY : findGamecubeRAMOffset () 193if self : hasProcess () then 194local err =libc . task_for_pid ( libc . mach_task_self (), self . pid , self . task ) 195 196if error == 0 then 197log . info ( "task_for_pid SUCCESS: valid permissions" ) 198else 199log . warn ( "task_for_pid FAILED: invalid permissions" ) 200end 201 202if error ~= 0 then 203return false 204end 205 206-- while libc.mach_vm_region(self.task, regionAddr, size, VM_REGION_EXTENDED_INFO, regInfo, cnt, obj) == 0 do 207-- TODO: Scan all memory regions.. 208-- end 209end 210 211return false 212end 213 214function MEMORY : read ( addr , output , size ) 215return false 216end 217 218function MEMORY : write ( addr , input , size ) 219return false 220end 221 222return MEMORY