fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
650c9b1
gui
1local bit =require ( "bit" ) 2local log =require ( "log" ) 3local memory =require ( "memory." .. jit . os : lower ()) 4 5require ( "extensions.string" ) 6 7local watcher ={ 8debug =false , 9process =memory . init (), 10hooks ={} , 11wildcard_hooks ={} , 12values_memory ={} , 13values_pointer ={} , 14watching_addr ={} , 15watching_str_addr ={} , 16watching_ptr_addr ={} , 17pointer_loc ={} , 18named ={} , 19map =require ( "smash.memory" ), -- The map of the game we want to use! 20} 21 22local TYPE_NULL =0 23local TYPE_BOOL =1 24local TYPE_BYTE =2 25local TYPE_SHORT =3 26local TYPE_INT =4 27local TYPE_FLOAT =5 28local TYPE_POINTER =6 29local TYPE_DATA =7 30 31-- Allow us to do things such as watcher.players.name without having to do watcher.named.player.name 32setmetatable ( watcher , { __index =watcher . named } ) 33 34local TYPE_NAME ={ 35[ "bool" ] =TYPE_BOOL , 36[ "byte" ] =TYPE_BYTE , 37[ "short" ] =TYPE_SHORT , 38[ "int" ] =TYPE_INT , 39[ "float" ] =TYPE_FLOAT , 40[ "pointer" ] =TYPE_POINTER , 41} 42 43local READ_TYPES ={ 44[ TYPE_BOOL ] ="readBool" , 45[ TYPE_BYTE ] ="readUByte" , 46[ TYPE_SHORT ] ="readShort" , 47[ TYPE_INT ] ="readInt" , 48[ TYPE_FLOAT ] ="readFloat" , 49} 50 51function watcher . init () 52for address , info in pairs ( watcher . map ) do 53if info . type == "pointer" and info . struct then 54watcher . pointer_loc [ address ] =0x00000000 55watcher . values_pointer [ address ] ={} 56watcher . watching_ptr_addr [ address ] ={} 57for offset , struct in pairs ( info . struct ) do 58watcher . watching_ptr_addr [ address ][ offset ] =TYPE_NAME [ struct . type ] 59local name =( "%s.%s" ): format ( info . name , struct . name ) 60watcher . setTableValue ( name , info . init ) 61end 62else 63watcher . values_memory [ address ] =info . init or 0 64watcher . watching_addr [ address ] =TYPE_NAME [ info . type ] 65watcher . setTableValue ( info . name , watcher . values_memory [ address ]) 66end 67end 68end 69 70function watcher . shutdown () 71watcher . values_memory ={} 72watcher . values_pointer ={} 73watcher . watching_addr ={} 74watcher . watching_ptr_addr ={} 75 76watcher . named ={} 77end 78 79-- Creates or updates a tree of values for easy indexing 80-- Example a path of "player.name" will become watcher.players = { name = value } 81function watcher . setTableValue ( path , value ) 82local last =watcher . named 83 84local keys ={} 85for key in string . gmatch ( path , "[^%.]+" ) do 86keys [ # keys + 1 ] =tonumber ( key ) or key 87end 88 89for i , key in ipairs ( keys ) do 90if i < # keys then 91last [ key ] =last [ key ] or {} 92last =last [ key ] 93else 94if type ( last ) ~= "table" then 95return error (( "Failed to index a %s value (%q)" ): format ( type ( last ), keys [ i - 1 ])) 96end 97last [ key ] =value 98end 99end 100end 101 102function watcher . hook ( name , desc , callback ) 103if string . find ( name , "*" , 1 , true ) then 104-- Convert '*' into capture patterns 105local pattern ='^' .. name : escape (): gsub ( "%%%*" , "([^.]-)" ) .. '$' 106watcher . wildcard_hooks [ pattern ] =watcher . wildcard_hooks [ pattern ] or {} 107watcher . wildcard_hooks [ pattern ][ desc ] =callback 108else 109watcher . hooks [ name ] =watcher . hooks [ name ] or {} 110watcher . hooks [ name ][ desc ] =callback 111end 112end 113 114function watcher . unhook ( name , desc ) 115watcher . hook ( name , desc , nil ) 116end 117 118local args ={} 119local matches ={} 120 121function watcher . hookRun ( name , ... ) 122-- Normal hooks 123if watcher . hooks [ name ] then 124for desc , callback in pairs ( watcher . hooks [ name ]) do 125local succ , err 126if type ( desc ) == "table" then 127-- Assume a table is an object, so call it as so 128succ , err =xpcall ( callback , debug . traceback , desc , ... ) 129else 130succ , err =xpcall ( callback , debug . traceback , ... ) 131end 132if not succ then 133log . error ( "watcher hook error: %s (%s)" , desc , err ) 134end 135end 136end 137 138local varargs ={ ... } 139 140-- Allow for wildcard hooks 141for pattern , hooks in pairs ( watcher . wildcard_hooks ) do 142if string . find ( name , pattern ) then 143args ={} 144matches ={ name : match ( pattern ) } 145 146for k , match in ipairs ( matches ) do 147table . insert ( args , tonumber ( match ) or match ) 148end 149for k , arg in ipairs ( varargs ) do 150table . insert ( args , arg ) 151end 152 153for desc , callback in pairs ( hooks ) do 154local succ , err 155if type ( desc ) == "table" then 156-- Assume a table is an object, so call it as so 157succ , err =xpcall ( callback , debug . traceback , desc , unpack ( args )) 158else 159succ , err =xpcall ( callback , debug . traceback , unpack ( args )) 160end 161if not succ then 162log . error ( "watcher wildcard hook error: %s (%s)" , desc , err ) 163end 164end 165end 166end 167end 168 169function watcher . toHex ( address ) 170return ( "%08X" ): format ( address ) 171end 172 173function watcher . readType ( type , address ) 174return watcher . process [ READ_TYPES [ type ]]( watcher . process , address ) 175end 176 177function watcher . isReady () 178return watcher . process : hasProcess () and watcher . process : isProcessActive () and watcher . process : hasGamecubeRAMOffset () 179end 180 181function watcher . update ( exe ) 182if not watcher . process : isProcessActive () and watcher . process : hasProcess () then 183watcher . process : close () 184love . window . setTitle ( "M'Overlay - Waiting for Dolphin.." ) 185log . info ( "closed: %s" , exe ) 186end 187 188if watcher . process : findprocess ( exe ) then 189log . info ( "hooked: %s" , exe ) 190love . window . setTitle ( "M'Overlay - Dolphin hooked" ) 191end 192 193if not watcher . process : hasGamecubeRAMOffset () and watcher . process : findGamecubeRAMOffset () then 194log . info ( "watching ram: %s" , exe ) 195watcher . init () 196elseif watcher . process : hasProcess () and watcher . process : hasGamecubeRAMOffset () then 197watcher . checkmemoryvalues () 198end 199end 200 201function watcher . checkmemoryvalues () 202local frame =watcher . frame 203 204for address , type in pairs ( watcher . watching_addr ) do 205local value =watcher . readType ( type , address ) 206if watcher . values_memory [ address ] ~= value then 207local info =watcher . map [ address ] 208local numValue =tonumber ( value ) or ( value and 1 or 0 ) 209 210if info . debug then 211log . debug ( "[%d][%08X = %08X] %s = %s" , frame , address , numValue , info . name , value ) 212end 213 214watcher . values_memory [ address ] =value 215watcher . setTableValue ( info . name , watcher . values_memory [ address ]) 216watcher . hookRun ( info . name , value ) 217end 218end 219 220for address , pointing in pairs ( watcher . pointer_loc ) do 221local ptr_addr =watcher . process : readInt ( address ) 222if watcher . pointer_loc [ address ] ~= ptr_addr then 223watcher . pointer_loc [ address ] =ptr_addr 224 225log . debug ( "[%d] pointer at %08X changed location to %08X" , frame , address , ptr_addr ) 226 227if ptr_addr == 0x00000000 then 228watcher . values_pointer [ address ] ={} 229end 230end 231end 232 233for address , offsets in pairs ( watcher . watching_ptr_addr ) do 234local ptr_addr =watcher . pointer_loc [ address ] 235if ptr_addr and ptr_addr ~= 0x00000000 then 236 237local info =watcher . map [ address ] 238 239for offset , type in pairs ( offsets ) do 240 241local value =watcher . readType ( type , ptr_addr + offset ) 242 243-- If the location of the pointer changed, or our value changed.. 244if watcher . values_pointer [ address ][ offset ] ~= value then 245 246if info and info . struct and info . struct [ offset ] then 247local name =string . format ( "%s.%s" , info . name , info . struct [ offset ]. name ) 248local numValue =tonumber ( value ) or ( value and 1 or 0 ) 249 250if info . debug or info . struct [ offset ]. debug then 251log . debug ( "[%d][%08X->%08X->%08X = %08X] %s = %s" , frame , address , ptr_addr , ptr_addr + offset , numValue , name , value ) 252end 253 254watcher . values_pointer [ address ][ offset ] =value 255watcher . setTableValue ( name , watcher . values_pointer [ address ][ offset ]) 256watcher . hookRun ( name , value ) 257end 258end 259end 260end 261end 262end 263 264return watcher