fork/m-overlay

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

blackops7799Added build files and stuffeefe2ce

GUI
3.0 KiB130 linesraw
1local utf8 = require("utf8")
2 
3local function error_printer(msg, layer)
4	print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
5end
6
7function love.errorhandler(msg)
8	msg = tostring(msg)
9 
10	error_printer(msg, 2)
11 
12	if not love.window or not love.graphics or not love.event then
13		return
14	end
15 
16	if not love.graphics.isCreated() or not love.window.isOpen() then
17		local success, status = pcall(love.window.setMode, 800, 600)
18		if not success or not status then
19			return
20		end
21	end
22 
23	-- Reset state.
24	if love.mouse then
25		love.mouse.setVisible(true)
26		love.mouse.setGrabbed(false)
27		love.mouse.setRelativeMode(false)
28		if love.mouse.isCursorSupported() then
29			love.mouse.setCursor()
30		end
31	end
32	if love.joystick then
33		-- Stop all joystick vibrations.
34		for i,v in ipairs(love.joystick.getJoysticks()) do
35			v:setVibration()
36		end
37	end
38	if love.audio then love.audio.stop() end
39 
40	love.graphics.reset()
41	local font = love.graphics.setNewFont(14)
42 
43	love.graphics.setColor(255, 255, 255, 255)
44 
45	local trace = debug.traceback()
46 
47	love.graphics.origin()
48 
49	local sanitizedmsg = {}
50	for char in msg:gmatch(utf8.charpattern) do
51		table.insert(sanitizedmsg, char)
52	end
53	sanitizedmsg = table.concat(sanitizedmsg)
54 
55	local err = {}
56 
57	table.insert(err, "Error\n")
58	table.insert(err, sanitizedmsg)
59 
60	if #sanitizedmsg ~= #msg then
61		table.insert(err, "Invalid UTF-8 string in error message.")
62	end
63 
64	table.insert(err, "\n")
65 
66	for l in trace:gmatch("(.-)\n") do
67		if not l:match("boot.lua") then
68			l = l:gsub("stack traceback:", "Traceback\n")
69			table.insert(err, l)
70		end
71	end
72 
73	local p = table.concat(err, "\n")
74 
75	p = p:gsub("\t", "")
76	p = p:gsub("%[string \"(.-)\"%]", "%1")
77 
78	local function draw()
79		local pos = 8
80		love.graphics.clear(89/255, 157/255, 220/255)
81		love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos)
82		love.graphics.present()
83	end
84 
85	local fullErrorText = p
86	local function copyToClipboard()
87		if not love.system then return end
88		love.system.setClipboardText(fullErrorText)
89		p = p .. "\nCopied to clipboard!"
90		draw()
91	end
92 
93	if love.system and love.keyboard then
94		p = p .. "\n\nPress Ctrl+C or tap to copy this error"
95	end
96 
97	return function()
98		love.event.pump()
99 
100		for e, a, b, c in love.event.poll() do
101			if e == "quit" then
102				return 1
103			elseif e == "keypressed" and a == "escape" then
104				return 1
105			elseif e == "keypressed" and a == "c" and love.keyboard and love.keyboard.isDown("lctrl", "rctrl") then
106				copyToClipboard()
107			elseif e == "touchpressed" then
108				local name = love.window.getTitle()
109				if #name == 0 or name == "Untitled" then name = "Game" end
110				local buttons = {"OK", "Cancel"}
111				if love.system then
112					buttons[3] = "Copy to clipboard"
113				end
114				local pressed = love.window.showMessageBox("Quit "..name.."?", "", buttons)
115				if pressed == 1 then
116					return 1
117				elseif pressed == 3 then
118					copyToClipboard()
119				end
120			end
121		end
122 
123		draw()
124 
125		if love.timer then
126			love.timer.sleep(0.1)
127		end
128	end
129 
130end