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