fork/m-overlay

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

BkacjiosApplication update checker7e0ab3b

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