fork/m-overlay

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

BkacjiosFinal update logic fixesf03565a

v2.1.2
3.2 KiB111 linesraw
1local updater = {}
2
3local log = require("log")
4local web = require("web")
5
6local downloader = require("downloader")
7
8local json = require("serializer.json")
9
10local DOWNLOAD_SIZE = 0
11local DOWNLOADED = 0
12
13local STATUS = ""
14
15local function versionNumber(tag)
16	-- Split into: major, minor, revision, hotfix
17	if not tag then
18		return -1
19	end
20	local maj, min, rev, hot = tag:lower():match("v?(%d+)%.(%d+)%.(%d+)(%a?)")
21	if not maj then
22		return -1
23	end
24	local hotfix = 0
25	if hot and #hot > 0 then
26		-- 97 = a
27		hotfix = string.byte(hot) - 96
28	end
29	return (tonumber(maj) * 10000) + (tonumber(min) * 1000) + (tonumber(rev) * 100) + hotfix
30end
31
32do
33	local icon = love.graphics.newImage("textures/icon.png")
34	local angleStart = math.pi * -0.5
35	local angleEnd = math.pi * 2
36	local font = love.graphics.newFont("fonts/melee.otf", 12)
37
38	function updater.draw(sw, sh)
39		local w  = icon:getWidth()
40		local h = icon:getHeight()
41
42		local cx, cy = sw/2, sh/2-(sh/16)
43
44		love.graphics.setColor(255, 255, 255, 255)
45		love.graphics.draw(icon, cx, cy, 0, 0.5, 0.5, w/2, h/2)
46
47		local downloadAng = angleStart + (angleEnd * (DOWNLOADED/DOWNLOAD_SIZE))
48
49		love.graphics.setColor(34, 177, 76, 255)
50		love.graphics.setLineWidth(8)
51		love.graphics.arc("line", "open", cx, cy, sw/3, angleStart, downloadAng)
52
53		local width	= font:getWidth(STATUS)
54		local sy = sh-font:getHeight()
55
56		love.graphics.setFont(font)
57		love.graphics.setColor(0, 0, 0, 255)
58		love.graphics.print(STATUS, cx+1, sy+1, 0, 1, 1, width/2)
59		love.graphics.setColor(255, 255, 255, 255)
60		love.graphics.print(STATUS, cx, sy, 0, 1, 1, width/2)
61	end
62end
63
64function updater.check()
65	local version = versionNumber(love.getMOverlayVersion())
66
67	if version <= 0 then return end
68
69	STATUS = "Checking for update.."
70
71	web.get("https://api.github.com/repos/bkacjios/m-overlay/releases/latest", function(event)
72		if event.success and event.code == 200 then
73			local response = json.decode(event.response)
74			if (response.tag_name) then
75				local updateVersion = versionNumber(response.tag_name)
76				if updateVersion > 0 and updateVersion > version then
77					log.warn("[UPDATER] Application update available")
78					for id, asset in pairs(response.assets) do
79						if asset.name == "application.love" then
80							log.info("[UPDATER] Download: %s", asset.browser_download_url)
81							DOWNLOAD_SIZE = 0
82							DOWNLOADED = 0
83							downloader.download(asset.browser_download_url, asset.name, function(event)
84								if event.size then
85									DOWNLOAD_SIZE = event.size
86								elseif event.chunk then
87									DOWNLOADED = DOWNLOADED + event.chunk
88									STATUS = string.format("Downloading: %d%%", DOWNLOADED/DOWNLOAD_SIZE*100)
89								elseif event.code and event.code == 200 then
90									log.error("[UPDATER] Updated to version %s", response.tag_name)
91									love.event.quit("restart")
92								elseif event.error then
93									log.error("[UPDATER] Update error: %s", event.error)
94								end
95							end)
96							return
97						end
98					end
99				else
100					log.info("[UPDATER] Application is up-to-date")
101					STATUS = "No update required"
102				end
103			end
104		else
105			log.error("[UPDATER] Failed to get latest release information: %s", event.code)
106			STATUS = string.format("Update error: %d", event.code)
107		end
108	end)
109end
110
111return updater