fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosMore class changes27686d7

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