fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
27686d7
settings-overhaul
1love . filesystem . setRequirePath ( "?.lua;?/init.lua;modules/?.lua;modules/?/init.lua" ) 2 3math . randomseed ( love . timer . getTime ()) 4 5local VERSION 6 7if love . filesystem . getInfo ( "version.txt" ) then 8-- Read version file and strip any trailing whitespace 9VERSION =love . filesystem . read ( "version.txt" ): match ( "^(%S+)" ) 10end 11 12function love . getMOverlayVersion () 13return VERSION or "0.0.0" 14end 15 16require ( "console" ) 17require ( "errorhandler" ) 18require ( "extensions.love" ) 19 20local log =require ( "log" ) 21local melee =require ( "melee" ) 22local zce =require ( "zce" ) 23local memory =require ( "memory" ) 24local notification =require ( "notification" ) 25 26local overlay =require ( "overlay" ) 27local music =require ( "music" ) 28 29local color =require ( "util.color" ) 30local gui =require ( "gui" ) 31 32local ease =require ( "ease" ) 33 34local graphics =love . graphics 35local newImage =graphics . newImage 36 37local PORT_FONT =graphics . newFont ( "fonts/melee-bold.otf" , 42 ) 38local WAITING_FONT =graphics . newFont ( "fonts/melee-bold.otf" , 24 ) 39local DEBUG_FONT =graphics . newFont ( "fonts/melee-bold.otf" , 12 ) 40 41local GRADIENT =newImage ( "textures/gui/gradient.png" ) 42local DOLPHIN =newImage ( "textures/dolphin.png" ) 43local GAME =newImage ( "textures/game.png" ) 44local MELEE =newImage ( "textures/meleedisk.png" ) 45local MELEELABEL =newImage ( "textures/meleedisklabel.png" ) 46local SHADOW =newImage ( "textures/shadow.png" ) 47local KEY =newImage ( "textures/key.png" ) 48 49local ESCAPE_MENU_HINT =love . timer . getTime () + 5 50local ESCAPE_MENU_HINT_FADE =ESCAPE_MENU_HINT + 1 51 52MAX_PORTS =4 53 54local PORT_DISPLAY_OVERRIDE =nil 55 56local PORT_TEXTURES ={ 57[ 1 ] =newImage ( "textures/player1_color.png" ), 58[ 2 ] =newImage ( "textures/player2_color.png" ), 59[ 3 ] =newImage ( "textures/player3_color.png" ), 60[ 4 ] =newImage ( "textures/player4_color.png" ) 61} 62 63local CODE_ENTERED =false 64 65local CODE_POSITION ={ 66[ 1 ] =1 , 67[ 2 ] =1 , 68[ 3 ] =1 , 69[ 4 ] =1 70} 71 72local CODE ={ -- Konami code 730x0008 , 0x0008 , 740x0004 , 0x0004 , 750x0001 , 0x0002 , 760x0001 , 0x0002 , 770x0200 , 0x0100 , 0x1000 78} 79 80--[[local CODE2 = { -- Konami code using joysticks 810x10000, 0x10000, 820x20000, 0x20000, 830x40000, 0x80000, 840x40000, 0x80000, 850x0200, 0x0100, 0x1000 86}]] 87 88local portless_title ="" 89function love . updateTitle ( str ) 90local title =str 91portless_title =str 92if PANEL_SETTINGS : IsPortTitleEnabled () then 93title =string . format ( "%s (Port %d)" , str , overlay . getPort ()) 94end 95love . window . setTitle ( title ) 96end 97 98function love . getTitleNoPort () 99return portless_title 100end 101 102memory . hook ( "romstring.akaneia" , "Load Game Specific Textures" , function ( romname , romvalue ) 103melee . loadRomSpecificTextures () 104end ) 105 106function love . load ( args , unfilteredArg ) 107love . keyboard . setKeyRepeat ( true ) 108 109melee . loadTextures () 110gui . init () 111music . init () 112overlay . init () 113 114if memory . hasPermissions () then 115love . updateTitle ( "M'Overlay - Waiting for Dolphin..." ) 116else 117love . updateTitle ( "M'Overlay - Invalid permissions..." ) 118--notification.error() 119end 120 121-- Loop through all the commandline arguments 122for n , arg in pairs ( args ) do 123-- Check for '--port=N' first.. 124local portn =tonumber ( string . match ( arg , "%-%-port=(%d+)" )) 125 126if arg == "--port" then -- Alternative '--port N' 127portn =tonumber ( args [ n + 1 ]) -- Convert the next argument in the commandline to a number 128end 129 130if portn then -- A port number was specified.. 131overlay . setPort ( portn ) 132break -- Done 133end 134end 135end 136 137memory . hook ( "menu.player_one_port" , "Controller port that is acting as player 1" , function ( port ) 138if melee . isSinglePlayerGame () or ( memory . menu . major == MENU_VS_UNKNOWN and PANEL_SETTINGS : IsSlippiNetplay ()) then 139overlay . setPort ( port + 1 ) 140log . debug ( "[AUTOPORT] Player \"one\" port changed %d" , overlay . getPort ()) 141end 142end ) 143 144memory . hook ( "menu.major" , "Slippi Auto Port Switcher" , function ( major ) 145if melee . isSinglePlayerGame () or ( major == MENU_VS_UNKNOWN and PANEL_SETTINGS : IsSlippiNetplay ()) then 146-- Switch back to whatever controller is controlling port 1, when not in a match 147overlay . setPort ( memory . menu . player_one_port + 1 ) 148log . debug ( "[AUTOPORT] Forcing port %d in menus" , overlay . getPort ()) 149end 150end ) 151 152memory . hook ( "menu.minor" , "Slippi Auto Port Switcher" , function ( minor ) 153-- MENU_VS_UNKNOWN = Slippi online 154if memory . menu . major == MENU_VS_UNKNOWN and PANEL_SETTINGS : IsSlippiNetplay () then 155if minor == MENU_VS_UNKNOWN_CSS or menu == MENU_VS_UNKNOWN_SSS then 156-- Switch back to whatever controller is controlling port 1, when not in a match 157overlay . setPort ( memory . menu . player_one_port + 1 ) 158log . debug ( "[AUTOPORT] Forcing port %d in menus" , overlay . getPort ()) 159if minor == MENU_VS_UNKNOWN_CSS then 160-- Display the port info only when swiching back to CSS 161overlay . showPort ( 1.5 ) -- Show the port display number for 1.5 seconds 162end 163elseif minor == MENU_VS_UNKNOWN_VERSUS then 164PORT_DISPLAY_OVERRIDE =( memory . slippi . local_player . index % MAX_PORTS ) + 1 165overlay . showPort ( 3 ) -- Show the port display number for 3 seconds 166log . debug ( "[AUTOPORT] Switching display icon to use port %d" , overlay . getPort ()) 167elseif minor == MENU_VS_UNKNOWN_INGAME then 168-- Switch to the local player index whenever else 169PORT_DISPLAY_OVERRIDE =nil 170overlay . setPort ( memory . slippi . local_player . index + 1 ) 171log . debug ( "[AUTOPORT] Switching to slippi local player index %d" , overlay . getPort ()) 172end 173end 174end ) 175 176memory . hook ( "player.*.character" , "Show port on character select" , function ( port , character ) 177if memory . menu . minor == MENU_VS_CSS and port == overlay . getPort () then 178overlay . showPort ( 1.5 ) -- Show the port display number for 1.5 seconds 179end 180end ) 181 182local snd =love . audio . newSource ( "sounds/main7b.wav" , "static" ) 183 184memory . hook ( "controller.*.buttons.pressed" , "Konami code check" , function ( port , pressed ) 185if pressed == 0x0 or port ~= overlay . getPort () then return end 186local pos =CODE_POSITION [ port ] 187if CODE [ pos ] == pressed then 188CODE_POSITION [ port ] =pos + 1 189if pos >= # CODE then 190CODE_ENTERED =not CODE_ENTERED 191snd : setVolume ( 0.25 ) 192snd : setPitch ( CODE_ENTERED and 1 or 0.75 ) 193snd : play () 194log . warn ( "[KONAMI] %s developer stats.." , CODE_ENTERED and "Showing" or "Hiding" ) 195end 196else 197CODE_POSITION [ port ] =1 198end 199end ) 200 201function love . update ( dt ) 202music . update () 203memory . update () -- Look for Dolphin.exe 204notification . update ( 8 , 0 ) 205gui . update ( dt ) 206end 207 208function love . resize ( w , h ) 209gui . resize ( w , h ) 210end 211 212function love . joystickpressed ( joy , but ) 213gui . joyPressed ( joy , but ) 214end 215 216function love . joystickreleased ( joy , but ) 217gui . joyReleased ( joy , but ) 218end 219 220function love . keypressed ( key , scancode , isrepeat ) 221if key == "escape" and not isrepeat then 222PANEL_SETTINGS : Toggle () 223ESCAPE_MENU_HINT =nil 224ESCAPE_MENU_HINT_FADE =nil 225end 226 227gui . keyPressed ( key , scancode , isrepeat ) 228 229local num =tonumber ( string . match ( key , "kp(%d)" ) or key ) 230 231if not PANEL_SETTINGS : IsVisible () and num and num >= 1 and num <= 4 then 232overlay . setPort ( num ) 233PORT_DISPLAY_OVERRIDE =nil 234overlay . showPort ( 1.5 ) -- Show the port display number for 1.5 seconds 235end 236end 237 238function love . keyreleased ( key ) 239gui . keyReleased ( key ) 240end 241 242function love . textinput ( text ) 243gui . textInput ( text ) 244end 245 246function love . mousemoved ( x , y , dx , dy , istouch ) 247gui . mouseMoved ( x , y , dx , dy , istouch ) 248end 249 250function love . mousepressed ( x , y , button , istouch , presses ) 251gui . mousePressed ( x , y , button , istouch , presses ) 252end 253 254function love . mousereleased ( x , y , button , istouch , presses ) 255gui . mouseReleased ( x , y , button , istouch , presses ) 256end 257 258function love . wheelmoved ( x , y ) 259if PANEL_SETTINGS : IsVisible () then 260gui . mouseWheeled ( x , y ) 261return 262end 263 264local port =overlay . getPort () 265 266if y > 0 then 267port =port - 1 268elseif y < 0 then 269port =port + 1 270end 271overlay . setPort ( port ) 272overlay . showPort ( 1.5 ) 273PORT_DISPLAY_OVERRIDE =nil 274end 275 276function love . drawDeveloperInfo () 277local stats =love . graphics . getStats () 278stats . memory =collectgarbage ( "count" ) 279 280graphics . setFont ( DEBUG_FONT ) 281local i =0 282for stat , val in pairs ( stats ) do 283local str 284 285if stat == "memory" or stat == "texturememory" then 286str =string . format ( "%s: %s" , stat , string . toSize ( val )) 287else 288str =string . format ( "%s: %d" , stat , val ) 289end 290 291graphics . setColor ( 0 , 0 , 0 , 255 ) 292graphics . textOutline ( str , 2 , 8 , 8 + 14 * i ) 293graphics . setColor ( 0 , 255 , 0 , 255 ) 294graphics . ( str , 8 , 8 + 14 * i ) 295 296i =i + 1 297end 298end 299 300local DC_CON =newImage ( "textures/buttons/disconnected.png" ) 301 302function love . drawControllerOverlay () 303local controller 304local port =overlay . getPort () 305 306-- Check if ZCE (dynamic controller address swapping) 307if zce . isZce () then 308if zce . isOot () then 309controller =memory . controller . oot [ port ] 310elseif zce . isMajora () then 311controller =memory . controller . mm [ port ] 312elseif zce . isZ1 () then 313controller =memory . controller . z1 [ port ] 314elseif zce . isZ2 () then 315controller =memory . controller . z2 [ port ] 316else 317controller =memory . controller . menu [ port ] 318end 319else 320controller =memory . controller [ port ] 321end 322 323if PANEL_SETTINGS : IsSlippiReplay () and melee . isInGame () then 324local player =memory . player [ port ] 325 326if not player then return end 327 328local entity 329 330if player . transformed == 256 then 331-- If the player has the "transformed" flag set, assume they are now controlling the "partner" entity 332entity =player . partner 333else 334entity =player . entity 335end 336 337controller =entity . controller 338end 339 340if controller then 341if controller . plugged and controller . plugged ~= ( memory . game . pluggedValue or 0x00 ) then 342local sin =128 + math . sin ( love . timer . getTime () * 2 ) * 128 343graphics . setColor ( 255 , 0 , 0 , sin ) 344graphics . easyDraw ( DC_CON , 512 - 42 - 16 , 256 - 42 - 16 , 0 , 42 , 42 ) 345end 346 347overlay . draw ( controller ) 348end 349end 350 351do 352local icon_time_start 353local icon_time_show 354local icon_time_next 355 356local icon_rotate =0 357 358local canvas =love . graphics . newCanvas () 359 360function love . drawTrobber ( game ) 361local t =love . timer . getTime () 362local dt =love . timer . getDelta () 363 364local lx =0 365local ly =math . sin ( t * 3 ) * 4 366local rx =icon_rotate 367 368local rotate_speed =0 369 370if not game then 371if not icon_time_start or icon_time_next < t then 372icon_time_start =t 373icon_time_show =t + 1 374icon_time_next =t + 2 375end 376 377local anim =0 378if icon_time_show > t then 379anim =ease . sigmoid ( math . min ( 1 , ( t - icon_time_start ) / 1 )) 380lx =ease . lerp ( 0 , - 160 , anim ) 381ly =ease . lerp ( 0 , 64 , anim ) 382rx =ease . lerp ( 0 , - 90 , anim ) 383else 384anim =ease . outback ( math . min ( 1 , ( t - icon_time_show ) / 1 )) 385lx =ease . lerp ( 160 , 0 , anim ) 386ly =ease . lerp ( 64 , 0 , anim ) 387rx =ease . lerp ( 90 , 0 , anim ) 388end 389else 390rotate_speed =math . sinlerp ( 0 , 360 * 4 * dt / 2 , t / 2 ) 391icon_rotate =( icon_rotate + rotate_speed ) % 360 392end 393 394graphics . setColor ( 255 , 255 , 255 , 255 ) 395 396graphics . setCanvas ( canvas ) 397 398graphics . clear ( 0 , 0 , 0 , 0 ) 399if not game then 400graphics . setBlendMode ( "replace" , "premultiplied" ) 401end 402 403graphics . setScissor ( 256 - 80 - 20 , 0 , 160 + 40 , 256 ) 404 405local slippi =PANEL_SETTINGS : IsSlippiNetplay () or PANEL_SETTINGS : IsSlippiReplay () 406local icon =game and ( slippi and MELEE or GAME ) or DOLPHIN 407 408graphics . setColor ( 255 , 255 , 255 , 255 ) 409graphics . easyDraw ( icon , 256 + lx , 64 + 40 + ly , math . rad ( rx ), 80 , 80 , 0.5 , 0.5 ) 410 411if game then 412local p =rotate_speed / 13 413 414for i =0 , 16 do 415local j =rotate_speed - i 416graphics . setColor ( 255 , 255 , 255 , rotate_speed * 4 ) 417graphics . easyDraw ( slippi and MELEELABEL or icon , 256 + lx , 64 + 40 + ly , math . rad ( rx - ( i * p * 4 )), 80 , 80 , 0.5 , 0.5 ) 418end 419end 420 421graphics . setScissor () 422 423if not game then 424graphics . setBlendMode ( "multiply" , "premultiplied" ) 425 426graphics . easyDraw ( GRADIENT , 256 - 80 - 20 , 0 , 0 , 80 , 256 ) 427graphics . easyDraw ( GRADIENT , 256 + 80 + 20 , 0 , math . rad ( 180 ), 80 , 256 , 0 , 1 ) 428end 429 430graphics . setCanvas () 431 432graphics . setBlendMode ( "alpha" , "alphamultiply" ) 433 434if game then 435local sw =math . sinlerp ( 0.5 , 1 , t * 3 ) 436graphics . setColor ( 125 , 125 , 125 , 150 ) 437graphics . easyDraw ( SHADOW , 256 , 154 , 0 , 64 * sw , 6 * sw , 0.5 , 0.5 ) 438end 439 440graphics . setColor ( 255 , 255 , 255 , 255 ) 441graphics . draw ( canvas ) 442end 443end 444 445do 446local ellipses ={ "." , ".." , "..." } 447 448function love . drawNotificationText ( msg ) 449local t =love . timer . getTime () 450 451local w =WAITING_FONT : getWidth ( msg ) 452local h =WAITING_FONT : getHeight () 453local x =256 - ( w / 2 ) 454local y =128 + 32 455 456local i =math . floor ( t % # ellipses ) + 1 457 458msg =msg .. ellipses [ i ] 459 460graphics . setFont ( WAITING_FONT ) 461graphics . setColor ( 0 , 0 , 0 , 255 ) 462graphics . textOutline ( msg , 3 , x , y ) 463graphics . setColor ( 255 , 255 , 255 , 255 ) 464graphics . ( msg , x , y ) 465end 466end 467 468function love . isWindows () 469return jit . os : lower () == "windows" 470end 471 472function love . supportsGameCapture () 473return love . isWindows () 474end 475 476function love . supportsAttachableConsole () 477return love . isWindows () 478end 479 480--[=[local canvas = love.graphics.newCanvas() 481 482local stenciledCanvas = { 483canvas, 484stencil = true, 485} 486 487local blur = graphics.newShader[[ 488extern number radius; 489extern vec2 imageSize; 490 491vec4 effect(vec4 color, Image tex, vec2 tc, vec2 pc) 492{ 493color = vec4(0); 494vec2 st; 495 496for (float x = -radius; x <= radius; x++) { 497for (float y = -radius; y <= radius; y++) { 498// to texture coordinates 499st.xy = vec2(x,y) / imageSize; 500color += Texel(tex, tc + st); 501} 502} 503return color / ((2.0 * radius + 1.0) * (2.0 * radius + 1.0)); 504} 505]] 506 507blur:send("radius", 6) 508blur:send("imageSize", {canvas:getPixelDimensions()})]=] 509 510function love . draw () 511if not love . supportsGameCapture () then 512graphics . setBackgroundColor ( PANEL_SETTINGS : GetBackgroundColor ()) 513else 514-- Default to completely transparent, makes the overlay completely invisible when not in a game! 515local alpha =0 516 517if ( memory . initialized and memory . game ) or PANEL_SETTINGS : IsVisible () then 518-- Only apply transparency when we are watching a games memory. 519alpha =255 - (( PANEL_SETTINGS : GetTransparency () / 100 ) * 255 ) 520end 521 522-- Transparent background for OBS 523graphics . setBackgroundColor ( 0 , 0 , 0 , alpha ) 524 525-- Show a preview for transparency 526if PANEL_SETTINGS : IsVisible () then 527graphics . setBackgroundColor ( 255 , 255 , 255 , alpha ) 528 529for x =0 , 512 / 32 do 530for y =0 , 256 / 32 do 531graphics . setColor ( 230 , 230 , 230 , 255 ) 532graphics . rectangle ( "fill" , 32 * ( x + ( y % 2 )), 32 * ( y + ( x % 2 )), 32 , 32 ) 533end 534end 535 536graphics . setColor ( 0 , 0 , 0 , alpha ) 537graphics . rectangle ( "fill" , 0 , 0 , 512 , 256 ) 538 539--[[graphics.setColor(0, 0, 0, 100) 540graphics.rectangle("fill", 512 - 20, 0, 20, 256) 541 542local rad = math.rad(90) 543 544graphics.setFont(DEBUG_FONT) 545graphics.setColor(0, 0, 0, 255) 546graphics.print(VERSION, 512 - 5, 5, rad) 547graphics.setColor(255, 255, 255, 255) 548graphics.print(VERSION, 512 - 4, 4, rad)]] 549end 550end 551 552--[[if PANEL_SETTINGS:IsVisible() then 553graphics.setCanvas(stenciledCanvas) 554graphics.clear(0, 0, 0, 0) 555end]] 556 557if memory . initialized and memory . game and memory . controller then 558love . drawControllerOverlay () 559else 560if memory . hooked then 561love . drawTrobber ( true ) 562local slippi =PANEL_SETTINGS : IsSlippiNetplay () or PANEL_SETTINGS : IsSlippiReplay () 563love . drawNotificationText ( slippi and "Waiting for melee" or "Waiting for game" ) 564else 565love . drawTrobber () 566love . drawNotificationText ( "Waiting for dolphin" ) 567end 568end 569 570-- Draw a temporary number to show that the user changed controller port 571if (( PANEL_SETTINGS : AlwaysShowPort () and memory . isInGame ()) or overlay . isPortShowing ()) then 572local port =overlay . getPort () 573if memory . isMelee () then 574local port =PORT_DISPLAY_OVERRIDE or port 575local portColor 576if melee . isTeams () then 577portColor =melee . getPlayerTeamColor ( port ) 578else 579portColor =melee . getPlayerColor ( port ) 580end 581portColor . a =200 582graphics . setColor ( portColor ) 583melee . drawSeries ( port , 8 , 256 - 72 - 4 , 0 , 72 , 72 ) 584graphics . setColor ( 255 , 255 , 255 , 255 ) 585melee . drawStock ( port , 40 , 256 - 42 , 0 , 24 , 24 ) 586graphics . easyDraw ( PORT_TEXTURES [ port ], 12 , 256 - 42 - 24 , 0 , 33 , 14 ) 587else 588graphics . setFont ( PORT_FONT ) 589graphics . setColor ( 0 , 0 , 0 , 255 ) 590graphics . textOutline ( port , 3 , 16 , 256 - 42 - 16 ) 591graphics . setColor ( 255 , 255 , 255 , 255 ) 592graphics . ( port , 16 , 256 - 42 - 16 ) 593end 594end 595 596--[[if PANEL_SETTINGS:IsVisible() then 597graphics.setCanvas() 598 599graphics.setShader(blur) 600graphics.easyDraw(canvas, 0, 0, 0, 512, 256) 601graphics.setShader() 602end]] 603 604local time =love . timer . getTime () 605 606if ESCAPE_MENU_HINT_FADE and ESCAPE_MENU_HINT_FADE >= time then 607local msg ="Press 'ESC' to access the settings!" 608 609local w =DEBUG_FONT : getWidth ( msg ) + 40 610local h =32 611 612local x =512 / 2 - w / 2 613local y =256 - h - 16 614 615y =y + math . sin ( time * 5 ) * 4 616 617local alpha =255 618 619if ESCAPE_MENU_HINT < time then 620local fadeLen =ESCAPE_MENU_HINT_FADE - ESCAPE_MENU_HINT 621local timeRem =ESCAPE_MENU_HINT_FADE - time 622alpha =timeRem / fadeLen * 255 623end 624 625graphics . setColor ( 255 , 255 , 255 , alpha ) 626graphics . roundRect ( x , y , w , h , 4 ) 627graphics . easyDraw ( KEY , x , y , 0 , 32 , 32 ) 628 629graphics . setFont ( DEBUG_FONT ) 630graphics . setColor ( 0 , 0 , 0 , alpha ) 631graphics . ( msg , x + 36 , y + ( h / 2 ) - 6 ) 632end 633 634gui . render () 635notification . draw () 636if CODE_ENTERED then 637love . drawDeveloperInfo () 638end 639end 640 641local FPS_LIMIT =60 642 643function love . run () 644if love . load then love . load ( love . arg . parseGameArguments ( arg ), arg ) end 645 646-- We don't want the first frame's dt to include time taken by love.load. 647if love . timer then love . timer . step () end 648 649local dt =0 650 651-- Main loop time. 652return function () 653local frame_start =love . timer . getTime () 654 655-- Process events. 656if love . event then 657love . event . pump () 658for name , a , b , c , d , e , f in love . event . poll () do 659if name == "quit" then 660if not love . quit or not love . quit () then 661return a or 0 662end 663end 664love . handlers [ name ]( a , b , c , d , e , f ) 665end 666end 667 668-- Update dt, as we'll be passing it to update 669if love . timer then dt =love . timer . step () end 670 671-- Call update and draw 672if love . update then love . update ( dt ) end -- will pass 0 if love.timer is disabled 673 674if love . graphics and love . graphics . isActive () then 675love . graphics . origin () 676love . graphics . clear ( love . graphics . getBackgroundColor ()) 677 678if love . draw then love . draw () end 679 680love . graphics . present () 681end 682 683if love . timer then 684local frame_time =love . timer . getTime () - frame_start 685love . timer . sleep ( 1 / FPS_LIMIT - frame_time ) 686end 687end 688end 689 690function love . quit () 691PANEL_SETTINGS : SaveSettings () 692gui . shutdown () 693end