fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
b58a563
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 114local args ={} 115local matches ={} 116 117function watcher . hookRun ( name , ... ) 118-- Normal hooks 119if watcher . hooks [ name ] then 120for desc , callback in pairs ( watcher . hooks [ name ]) do 121local succ , err 122if type ( desc ) == "table" then 123-- Assume a table is an object, so call it as so 124succ , err =xpcall ( callback , debug . traceback , desc , ... ) 125else 126succ , err =xpcall ( callback , debug . traceback , ... ) 127end 128if not succ then 129log . error ( "watcher hook error: %s (%s)" , desc , err ) 130end 131end 132end 133 134local varargs ={ ... } 135 136-- Allow for wildcard hooks 137for pattern , hooks in pairs ( watcher . wildcard_hooks ) do 138if string . find ( name , pattern ) then 139args ={} 140matches ={ name : match ( pattern ) } 141 142for k , match in ipairs ( matches ) do 143table . insert ( args , tonumber ( match ) or match ) 144end 145for k , arg in ipairs ( varargs ) do 146table . insert ( args , arg ) 147end 148 149for desc , callback in pairs ( hooks ) do 150local succ , err 151if type ( desc ) == "table" then 152-- Assume a table is an object, so call it as so 153succ , err =xpcall ( callback , debug . traceback , desc , unpack ( args )) 154else 155succ , err =xpcall ( callback , debug . traceback , unpack ( args )) 156end 157if not succ then 158log . error ( "watcher wildcard hook error: %s (%s)" , desc , err ) 159end 160end 161end 162end 163end 164 165function watcher . toHex ( address ) 166return ( "%08X" ): format ( address ) 167end 168 169function watcher . readType ( type , address ) 170return watcher . process [ READ_TYPES [ type ]]( watcher . process , address ) 171end 172 173function watcher . isReady () 174return watcher . process : hasProcess () and watcher . process : isProcessActive () and watcher . process : hasGamecubeRAMOffset () 175end 176 177function watcher . update ( exe ) 178if not watcher . process : isProcessActive () and watcher . process : hasProcess () then 179watcher . process : close () 180love . window . setTitle ( "M'Overlay - Waiting for Dolphin.." ) 181log . info ( "closed: %s" , exe ) 182end 183 184if watcher . process : findprocess ( exe ) then 185log . info ( "hooked: %s" , exe ) 186love . window . setTitle ( "M'Overlay - Dolphin hooked" ) 187end 188 189if not watcher . process : hasGamecubeRAMOffset () and watcher . process : findGamecubeRAMOffset () then 190log . info ( "watching ram: %s" , exe ) 191watcher . init () 192elseif watcher . process : hasProcess () and watcher . process : hasGamecubeRAMOffset () then 193watcher . checkmemoryvalues () 194end 195end 196 197function watcher . checkmemoryvalues () 198local frame =watcher . frame 199 200for address , type in pairs ( watcher . watching_addr ) do 201local value =watcher . readType ( type , address ) 202if watcher . values_memory [ address ] ~= value then 203local info =watcher . map [ address ] 204local numValue =tonumber ( value ) or ( value and 1 or 0 ) 205 206if info . debug then 207log . debug ( "[%d][%08X = %08X] %s = %s" , frame , address , numValue , info . name , value ) 208end 209 210watcher . values_memory [ address ] =value 211watcher . setTableValue ( info . name , watcher . values_memory [ address ]) 212watcher . hookRun ( info . name , value ) 213end 214end 215 216for address , pointing in pairs ( watcher . pointer_loc ) do 217local ptr_addr =watcher . process : readInt ( address ) 218if watcher . pointer_loc [ address ] ~= ptr_addr then 219watcher . pointer_loc [ address ] =ptr_addr 220 221log . debug ( "[%d] pointer at %08X changed location to %08X" , frame , address , ptr_addr ) 222 223if ptr_addr == 0x00000000 then 224watcher . values_pointer [ address ] ={} 225end 226end 227end 228 229for address , offsets in pairs ( watcher . watching_ptr_addr ) do 230local ptr_addr =watcher . pointer_loc [ address ] 231if ptr_addr and ptr_addr ~= 0x00000000 then 232 233local info =watcher . map [ address ] 234 235for offset , type in pairs ( offsets ) do 236 237local value =watcher . readType ( type , ptr_addr + offset ) 238 239-- If the location of the pointer changed, or our value changed.. 240if watcher . values_pointer [ address ][ offset ] ~= value then 241 242if info and info . struct and info . struct [ offset ] then 243local name =string . format ( "%s.%s" , info . name , info . struct [ offset ]. name ) 244local numValue =tonumber ( value ) or ( value and 1 or 0 ) 245 246if info . debug or info . struct [ offset ]. debug then 247log . debug ( "[%d][%08X->%08X->%08X = %08X] %s = %s" , frame , address , ptr_addr , ptr_addr + offset , numValue , name , value ) 248end 249 250watcher . values_pointer [ address ][ offset ] =value 251watcher . setTableValue ( name , watcher . values_pointer [ address ][ offset ]) 252watcher . hookRun ( name , value ) 253end 254end 255end 256end 257end 258end 259 260return watcher