fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
2efdb91
settings-overhaul
1local bit =require ( "bit" ) 2local ffi =require ( "ffi" ) 3local log =require ( "log" ) 4local cloneloader =require ( "memory.cloneloader" ) 5local process =require ( "memory." .. jit . os : lower ()) 6local notification =require ( "notification" ) 7 8require ( "extensions.string" ) 9require ( "extensions.table" ) 10 11local NULL =0x00000000 12 13local GAME_ID_ADDR =0x80000000 14local GAME_VER_ADDR =0x80000007 15local GAME_ID_LEN =0x06 16local GAME_NONE ="\0\0\0\0\0\0" 17 18local VC_ID_ADDR =0x80003180 19local VC_ID_LEN =0x04 20local VC_NONE ="\0\0\0\0" 21 22local memory ={ 23clones =require ( "games.clones" ), 24vcclones =require ( "games.vcclones" ), 25 26gameid =GAME_NONE , 27vcid =VC_NONE , 28process =process , 29permissions =process : hasPermissions (), 30 31hooked =false , 32initialized =false , 33ingame =false , 34supportedgame =false , 35 36map ={} , 37values ={} , 38 39hooks ={} , 40wildcard_hooks ={} , 41 42hook_queue ={} , 43} 44 45cloneloader . loadFile ( "clones.lua" , memory . clones ) 46 47setmetatable ( memory , { __index =memory . values } ) 48 49local bswap =bit . bswap 50local function bswap16 ( n ) 51return bit . bor ( bit . rshift ( n , 8 ), bit . lshift ( bit . band ( n , 0xFF ), 8 )) 52end 53 54function memory . readGameID () 55return memory . read ( GAME_ID_ADDR , GAME_ID_LEN ) 56end 57 58function memory . readVirtualConsoleID () 59return memory . read ( VC_ID_ADDR , VC_ID_LEN ) 60end 61 62function memory . readGameVersion () 63return memory . readUByte ( GAME_VER_ADDR ) 64end 65 66local typecache ={} 67 68local function cache ( typ , len ) 69-- This stops us from constantly calling ffi.new by recycling old variables 70len =len or 1 71if not typecache [ typ ] then 72-- Create new cache for this type 73typecache [ typ ] ={} 74elseif typecache [ typ ][ len ] then 75-- Set the cached value to all 0's so it can be reused safely 76ffi . fill ( typecache [ typ ][ len ], len ) 77-- Return the cached value 78return typecache [ typ ][ len ] 79end 80-- Create a new variable 81local cached =ffi . new ( string . format ( "%s[?]" , typ ), len ) 82-- Store it in the cache 83typecache [ typ ][ len ] =cached 84return cached 85end 86 87function memory . read ( addr , len ) 88local output =cache ( "char" , len ) 89local size =ffi . sizeof ( output ) 90process : read ( addr , output , size ) 91return ffi . string ( output , size ) 92end 93 94function memory . readByte ( addr ) 95if not process : hasProcess () then return 0 end 96local output =cache ( "int8_t" ) 97process : read ( addr , output , ffi . sizeof ( output )) 98return output [ 0 ] 99end 100 101function memory . writeByte ( addr , value ) 102if not process : hasProcess () then return end 103local input =cache ( "int8_t" ) 104return process : write ( addr , input , ffi . sizeof ( input )) 105end 106 107function memory . readBool ( addr ) 108if not process : hasProcess () then return false end 109return memory . readByte ( addr ) == 1 110end 111 112function memory . writeBool ( addr , value ) 113if not self : hasProcess () then return end 114self : writeByte ( addr , value == true and 1 or 0 ) 115end 116 117function memory . readUByte ( addr ) 118if not process : hasProcess () then return 0 end 119local output =cache ( "uint8_t" ) 120process : read ( addr , output , ffi . sizeof ( output )) 121return output [ 0 ] 122end 123 124function memory . writeUByte ( addr , value ) 125if not process : hasProcess () then return end 126local input =cache ( "uint8_t" ) 127return process : write ( addr , input , ffi . sizeof ( input )) 128end 129 130function memory . readShort ( addr ) 131if not process : hasProcess () then return 0 end 132local output =cache ( "int16_t" ) 133process : read ( addr , output , ffi . sizeof ( output )) 134return bswap16 ( output [ 0 ]) 135end 136 137function memory . writeShort ( addr , value ) 138if not process : hasProcess () then return end 139local input =cache ( "int16_t" ) 140return process : write ( addr , input , ffi . sizeof ( input )) 141end 142 143function memory . readUShort ( addr ) 144if not process : hasProcess () then return 0 end 145local output =cache ( "uint16_t" ) 146process : read ( addr , output , ffi . sizeof ( output )) 147return bswap16 ( output [ 0 ]) 148end 149 150function memory . writeUShort ( addr , value ) 151if not process : hasProcess () then return end 152local input =cache ( "uint16_t" ) 153return process : write ( addr , input , ffi . sizeof ( input )) 154end 155 156do 157local floatconversion =ffi . new ( "union { uint32_t i; float f; }" ) 158 159function memory . readFloat ( addr ) 160if not process : hasProcess () then return 0 end 161local output =cache ( "uint32_t" ) 162process : read ( addr , output , ffi . sizeof ( output )) 163floatconversion . i =bswap ( output [ 0 ]) 164return floatconversion . f , floatconversion . i 165end 166end 167 168function memory . writeFloat ( addr , value ) 169if not process : hasProcess () then return end 170local input =cache ( "float" ) 171return process : write ( addr , input , ffi . sizeof ( input )) 172end 173 174function memory . readInt ( addr ) 175if not process : hasProcess () then return 0 end 176local output =cache ( "int32_t" ) 177process : read ( addr , output , ffi . sizeof ( output )) 178return bswap ( output [ 0 ]) 179end 180 181function memory . writeInt ( addr , value ) 182if not process : hasProcess () then return end 183local input =cache ( "int32_t" ) 184return process : write ( addr , input , ffi . sizeof ( input )) 185end 186 187function memory . readUInt ( addr ) 188if not process : hasProcess () then return 0 end 189local output =cache ( "uint32_t" ) 190process : read ( addr , output , ffi . sizeof ( output )) 191return bswap ( output [ 0 ]) 192end 193 194function memory . writeUInt ( addr , value ) 195if not process : hasProcess () then return end 196local input =cache ( "uint32_t" ) 197return process : write ( addr , input , ffi . sizeof ( input )) 198end 199 200local TYPES_READ ={ 201[ "bool" ] =memory . readBool , 202 203[ "sbyte" ] =memory . readByte , 204[ "byte" ] =memory . readUByte , 205[ "short" ] =memory . readShort , 206[ "int" ] =memory . readInt , 207 208[ "u8" ] =memory . readUByte , 209[ "s8" ] =memory . readByte , 210[ "u16" ] =memory . readUShort , 211[ "s16" ] =memory . readShort , 212[ "u32" ] =memory . readUInt , 213[ "s32" ] =memory . readInt , 214 215[ "float" ] =memory . readFloat , 216 217[ "data" ] =memory . read , 218} 219 220-- Creates or updates a tree of values for easy indexing 221-- Example a path of "player.name" will become memory.players = { name = value } 222function memory . cacheValue ( table , path , value ) 223local last =table 224local last_key =nil 225 226local keys ={} 227for key in string . gmatch ( path , "[^%.]+" ) do 228keys [ # keys + 1 ] =tonumber ( key ) or key 229end 230 231for i , key in ipairs ( keys ) do 232if i < # keys then 233last [ key ] =last [ key ] or {} 234last =last [ key ] 235else 236if type ( last ) ~= "table" then 237return error (( "Failed to index a %s value (%q)" ): format ( type ( last ), keys [ i - 1 ])) 238end 239last [ key ] =value 240last_key =key 241end 242end 243 244-- Return the table that the value is being stored in, and the key name 245return last , last_key 246end 247 248local ADDRESS ={} 249ADDRESS . __index =ADDRESS 250 251function memory . newvalue ( addr , offset , struct , name ) 252assert ( type ( addr ) == "number" , "argument #1 'address' must be a number" ) 253assert ( TYPES_READ [ struct . type ] ~= nil , "unhandled type: " .. struct . type ) 254 255name =name or struct . name or addr 256 257local init =struct . init or NULL 258 259-- create/get a new value cache based off of the value name 260local tbl , key =memory . cacheValue ( memory . values , name , init ) 261 262--log.debug("[MEMORY] VALUE CREATED %08X + %X %q", addr, offset, name) 263 264return setmetatable ( { 265name =name , 266 267address =addr , -- Where in memory this value is located 268offset =offset , -- How far past the address value we should get the value from 269 270type =struct . type , 271-- Assign the function we will be using to read from memory 272read =TYPES_READ [ struct . type ], 273len =struct . len , 274 275-- Setup the cache 276cache =tbl , 277cache_key =key , 278cache_value =init , 279 280debug =struct . debug , 281} , ADDRESS ) 282end 283 284function ADDRESS : update () 285if self . address == NULL then return end 286 287-- value = converted value 288-- orig = Non-converted value (Only available for floats) 289local value , orig =self . read ( self . address + self . offset , self . len ) 290 291-- Check if there has been a value change 292if self . cache_value ~= value then 293self . cache_value =value 294self . cache [ self . cache_key ] =self . cache_value 295 296if self . debug then 297if self . type == "data" then 298value =value : match ( "(.-)%z" ) -- Strip trailing 0's 299log . debug ( "[MEMORY] [0x%08X = 0x%s] %s = %q" , self . address + self . offset , string . tohex ( value ), self . name , value ) 300else 301local numValue =tonumber ( orig ) or tonumber ( value ) or ( value and 1 or 0 ) 302log . debug ( "[MEMORY] [0x%08X = 0x%08X] %s = %s" , self . address + self . offset , numValue , self . name , value ) 303end 304end 305 306-- Queue up a hook event 307table . insert ( memory . hook_queue , { name =self . name , value =value , debug =self . debug } ) 308end 309end 310 311local POINTER ={} 312POINTER . __index =POINTER 313 314function memory . newpointer ( addr , offset , pointer , name ) 315local pstruct ={} 316 317name =name or pointer . name 318 319--log.debug("[MEMORY] POINTER CREATED %08X + %X %q", addr, offset, name or "nil") 320 321-- Loop through the pointers children 322for poffset , struct in pairs ( pointer . struct ) do 323local sname =name 324if sname and struct . name then 325sname =sname .. "." .. struct . name 326end 327if struct . type == "pointer" then 328pstruct [ poffset ] =memory . newpointer ( NULL , poffset , struct , sname ) 329else 330pstruct [ poffset ] =memory . newvalue ( NULL , poffset , struct , sname ) 331end 332end 333 334return setmetatable ( { 335parent =addr ~= NULL and pointer or nil , 336name =name , 337address =addr , 338offset =offset , 339location =NULL , 340struct =pstruct , 341} , POINTER ) 342end 343 344function POINTER : getAddress () 345return ( "0x%08X" ): format ( self . address + self . offset ) 346end 347 348function POINTER : getPointerPath () 349local parent =self 350while parent do 351local path =parent : getAddress () 352path =parent : getAddress () .. " -> " .. path 353parent =parent . parent 354end 355return path 356end 357 358do 359local cast =ffi . cast 360local GC_RAM_START =cast ( "uint32_t" , 0x80000000 ) 361local GC_RAM_END =cast ( "uint32_t" , 0x81800000 ) 362local WII_RAM_START =cast ( "uint32_t" , 0x90000000 ) 363local WII_RAM_END =cast ( "uint32_t" , 0x94000000 ) 364 365local function isValidPointer ( ptr ) 366ptr =cast ( "uint32_t" , ptr ) 367return ptr ~= NULL and (( ptr >= GC_RAM_START and ptr <= GC_RAM_END ) or ( ptr >= WII_RAM_START and ptr <= WII_RAM_END )) 368end 369 370function POINTER : update () 371local addr =self . address + self . offset 372local ploc =memory . readUInt ( addr ) 373local valid =isValidPointer ( ploc ) 374 375if self . location ~= ploc then 376self . location =ploc 377 378if valid then 379log . debug ( "[MEMORY] [0x%08X -> 0x%08X] %s -> 0x%08X" , addr , ploc , self . name , ploc ) 380else 381log . debug ( "[MEMORY] [0x%08X -> 0x0 (NULL)] %s -> 0x%08X" , addr , self . name , ploc ) 382end 383end 384 385if valid then 386for offset , struct in pairs ( self . struct ) do 387-- Set the address space to be where the containing pointer is pointing to 388struct . address =ploc 389-- Update the value/pointer recursively 390struct : update () 391end 392end 393end 394end 395 396function memory . loadmap ( map ) 397for address , struct in pairs ( map ) do 398if struct . type == "pointer" then 399memory . map [ address ] =memory . newpointer ( address , NULL , struct ) 400else 401memory . map [ address ] =memory . newvalue ( address , NULL , struct ) 402end 403end 404end 405 406function memory . hasPermissions () 407return memory . permissions 408end 409 410function memory . isSupportedGame () 411return memory . supportedgame 412end 413 414function memory . isInGame () 415local gid =memory . gameid 416local vcid =memory . vcid 417return gid ~= GAME_NONE or vcid ~= VC_NONE 418end 419 420function memory . isMelee () 421local gid =memory . gameid 422local version =memory . version 423 424-- Force the GAMEID and VERSION to be Melee 1.02, since Fizzi seems to be using the gameid address space for something.. 425if gid ~= GAME_NONE and PANEL_SETTINGS : IsSlippiNetplay () then 426gid ="GALE01" 427version =0x02 428end 429 430-- See if this GameID is a clone of another 431local clone =memory . clones [ gid ] and memory . clones [ gid ][ version ] or nil 432 433if gid == "GTME01" then 434version =0x02 435gid ="GALE01" 436elseif clone then 437version =clone . version 438gid =clone . id 439end 440 441return gid == "GALE01" and version == 0x02 442end 443 444local timer =love . timer . getTime () 445 446function memory . loadGameScript ( path ) 447-- Try to load the game table 448local status , game =xpcall ( require , debug . traceback , ( "games.%s" ): format ( path )) 449 450if status then 451memory . supportedgame =true 452memory . game =game 453log . info ( "[DOLPHIN] Loaded game config: %s" , path ) 454notification . info (( "Game %q detected" ): format ( path )) 455memory . init ( game . memorymap ) 456else 457memory . supportedgame =false 458notification . error (( "Unsupported game %s" ): format ( path )) 459notification . error ( "Playing slippi netplay? Press 'escape' and enable Rollback/Netplay mode" ) 460log . error ( "[DOLPHIN] %s" , game ) -- game variable is an error string 461end 462end 463 464function memory . findGame () 465local gid =memory . readGameID () 466local version =memory . readGameVersion () 467 468local vcid =memory . readVirtualConsoleID () 469 470-- Force the GAMEID and VERSION to be Melee 1.02, since Fizzi seems to be using the gameid address space for something.. 471if gid ~= GAME_NONE and PANEL_SETTINGS : IsSlippiNetplay () then 472gid ="GALE01" 473version =0x02 474end 475 476-- When playing Slippi netplay.. the game ID can and will change.. 477-- This would normally break things, but if you enable Rollback/Netplay mode it will force the gameid to always be GALE01 v1.02 478local meleeMode =( memory . isMelee () and memory . gameid ~= gid ) 479 480if not memory . ingame and gid == GAME_NONE and vcid ~= VC_NONE then 481memory . reset () 482memory . ingame =true 483memory . vcid =vcid 484 485log . info ( "[DOLPHIN] Game: %s" , vcid ) 486love . updateTitle (( "M'Overlay - Dolphin hooked (%s)" ): format ( vcid )) 487 488-- Check for VC clones 489vcid =memory . vcclones [ vcid ] or vcid 490 491memory . loadGameScript ( vcid ) 492memory . runhook ( "OnGameOpen" , vcid ) 493elseif ( not memory . ingame or meleeMode ) and gid ~= GAME_NONE then 494memory . reset () 495memory . ingame =true 496memory . gameid =gid 497memory . version =version 498 499log . info ( "[DOLPHIN] Game: %s revision %i" , gid , version ) 500love . updateTitle (( "M'Overlay - Dolphin hooked (%s-%i)" ): format ( gid , version )) 501 502-- See if this GameID is a clone of another 503local clone =memory . clones [ gid ] and memory . clones [ gid ][ version ] or nil 504 505if gid == "GTME01" then 506version =0x02 507gid ="GALE01" 508elseif clone then 509version =clone . version 510gid =clone . id 511end 512 513memory . loadGameScript (( "%s-%d" ): format ( gid , version )) 514memory . runhook ( "OnGameOpen" , gid , version ) 515elseif ( memory . ingame or meleeMode ) and ( gid == GAME_NONE and vcid == VC_NONE ) then 516memory . reset () 517memory . ingame =false 518memory . gameid =gid 519memory . vcid =vcid 520memory . version =version 521 522love . updateTitle ( "M'Overlay - Dolphin hooked" ) 523memory . runhook ( "OnGameClosed" ) 524memory . process : clearGamecubeRAMOffset () -- Clear the memory address space location (When a new game is opened, we recheck this) 525log . info ( "[DOLPHIN] Game closed.." ) 526end 527end 528 529function memory . update () 530if not memory . hasPermissions () then return end 531 532if not process : isProcessActive () and process : hasProcess () then 533process : close () 534love . updateTitle ( "M'Overlay - Waiting for Dolphin.." ) 535log . info ( "[DOLPHIN] Unhooked" ) 536memory . hooked =false 537end 538 539local t =love . timer . getTime () 540 541-- Only check for the dolphin process once per second to reduce CPU load 542if not process : hasProcess () or not process : hasGamecubeRAMOffset () then 543if timer <= t then 544timer =t + 0.5 545if process : findprocess () then 546log . info ( "[DOLPHIN] Hooked" ) 547love . updateTitle ( "M'Overlay - Dolphin hooked" ) 548memory . hooked =true 549elseif not process : hasGamecubeRAMOffset () and process : findGamecubeRAMOffset () then 550log . debug ( "[DOLPHIN] Watching ram: %X [%X]" , process : getGamecubeRAMOffset (), process : getGamecubeRAMSize ()) 551end 552end 553else 554memory . updatememory () 555 556local frame =memory . frame or 0 557if frame == 0 or memory . game_frame ~= frame then 558memory . game_frame =frame 559memory . runhooks () 560end 561end 562end 563 564function memory . init ( map ) 565memory . initialized =true 566memory . loadmap ( map ) 567log . info ( "[MEMORY] Mapped game memory structure" ) 568end 569 570function memory . reset () 571memory . initialized =false 572memory . ingame =false 573memory . map ={} 574memory . values ={} 575memory . gameid =GAME_NONE 576memory . version =0 577memory . game =nil 578setmetatable ( memory , { __index =memory . values } ) 579end 580 581function memory . updatememory () 582memory . findGame () 583 584for addr , value in pairs ( memory . map ) do 585value : update () 586end 587end 588 589local function hookPattern ( name ) 590-- Convert '*' into capture patterns 591if string . find ( name , "*" , 1 , true ) then 592return true , '^' .. name : escape (): gsub ( "%%%*" , "([^.]-)" ) .. '$' 593end 594return false , name 595end 596 597function memory . hook ( name , desc , callback ) 598local wildcard , name =hookPattern ( name ) 599if wildcard then 600memory . wildcard_hooks [ name ] =memory . wildcard_hooks [ name ] or {} 601memory . wildcard_hooks [ name ][ desc ] =callback 602else 603memory . hooks [ name ] =memory . hooks [ name ] or {} 604memory . hooks [ name ][ desc ] =callback 605end 606end 607 608function memory . unhook ( name , desc ) 609memory . hook ( name , desc , nil ) 610end 611 612function memory . runhooks () 613local pop 614while true do 615pop =table . remove ( memory . hook_queue , 1 ) 616if not pop then break end 617memory . runhook ( pop . name , pop . value ) 618end 619end 620 621do 622local args ={} 623local matches ={} 624 625function memory . runhook ( name , ... ) 626-- Normal hooks 627if memory . hooks [ name ] then 628for desc , callback in pairs ( memory . hooks [ name ]) do 629local succ , err 630if type ( desc ) == "table" then 631-- Assume a table is an object, so call it as so 632succ , err =xpcall ( callback , debug . traceback , desc , ... ) 633else 634succ , err =xpcall ( callback , debug . traceback , ... ) 635end 636if not succ then 637log . error ( "[MEMORY] hook error: %s (%s)" , desc , err ) 638end 639end 640end 641 642local varargs ={ ... } 643 644-- Allow for wildcard hooks 645for pattern , hooks in pairs ( memory . wildcard_hooks ) do 646if string . find ( name , pattern ) then 647args ={} 648matches ={ name : match ( pattern ) } 649 650for k , match in ipairs ( matches ) do 651table . insert ( args , tonumber ( match ) or match ) 652end 653for k , arg in ipairs ( varargs ) do 654table . insert ( args , arg ) 655end 656 657for desc , callback in pairs ( hooks ) do 658local succ , err 659if type ( desc ) == "table" then 660-- Assume a table is an object, so call it as so 661succ , err =xpcall ( callback , debug . traceback , desc , unpack ( args )) 662else 663succ , err =xpcall ( callback , debug . traceback , unpack ( args )) 664end 665if not succ then 666log . error ( "[MEMORY] wildcard hook error: %s (%s)" , desc , err ) 667end 668end 669end 670end 671end 672end 673 674return memory