Skip to content

Commit

Permalink
Updater updated update
Browse files Browse the repository at this point in the history
  • Loading branch information
C1XTZ committed Feb 9, 2024
1 parent 719b219 commit 8c9d127
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
2 changes: 1 addition & 1 deletion mobilephone/manifest.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[ABOUT]
NAME = Mobilephone
AUTHOR = C1XTZ, CheesyManiac
VERSION = 1.30
VERSION = 1.3
DESCRIPTION = A phone that displays ingame chat, time and the current song.

[WINDOW_...]
Expand Down
100 changes: 70 additions & 30 deletions mobilephone/mobilephone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,50 +161,89 @@ local updateStatusColor = {
local appFolder = ac.getFolder(ac.FolderID.ACApps) .. '/lua/mobilephone/'
local manifest = ac.INIConfig.load(appFolder .. '/manifest.ini', ac.INIFormat.Extended)
local appVersion = manifest:get('ABOUT', 'VERSION', 0.01)
local oneDayInSeconds = 60 * 60 * 24
local releaseURL = 'https://api.github.com/repos/C1XTZ/ac-mobilephone/releases/latest'
local doUpdate = (os.time() - settings.lastCheck) / 86400 > settings.updateInterval


--xtz: The following function was mostly taken from tuttertep's comfy map app
function getUpdate()
--xtz: The ingame updater idea was taken from tuttertep's comfy map app and rewritten to work with my github releases instead of pulling from the entire repository
function updateCheckVersion()
settings.lastCheck = os.time()

local url = 'https://github.com/C1XTZ/ac-mobilephone/archive/refs/heads/master.zip'
web.get(url, function(err, response)
web.get(releaseURL, function(err, response)
if err then
error(err)
settings.updateStatus = 4
error(err)
return
end

local responseBody = response.body
local manifest = io.loadFromZip(responseBody, 'ac-mobilephone-master/mobilephone/manifest.ini')
if not manifest then
local latestRelease = JSON.parse(response.body)
if not (latestRelease.tag_name and latestRelease.tag_name:match("^v%d%d?%.%d%d?$")) then
error('URL unavailable or no Version recognized, aborted update')
settings.updateStatus = 4
return print('Couldnt find manifest.ini, aborting update.')
return
end
local version = tonumber(latestRelease.tag_name:sub(2))

local version = ac.INIConfig.parse(manifest, ac.INIFormat.Extended):get('ABOUT', 'VERSION', 0)
if appVersion > version then
settings.updateStatus = 3
return
elseif appVersion == version then
settings.updateStatus = 2
return
else
updateApplyUpdate()
end
end)
end

for _, file in ipairs(io.scanZip(responseBody)) do
local content = io.loadFromZip(responseBody, file)
if content then
local filePath = file:match('/mobilephone/(.*)')
if filePath then
assert(appFolder, 'appFolder is nil')
if io.save(appFolder .. filePath, content) then print(file) end
else
print('Skipping file: ' .. file)
end
function updateApplyUpdate()
web.get(releaseURL, function(err, response)
if err then
settings.updateStatus = 4
error(err)
return
end

local releaseInfo = JSON.parse(response.body)
if not releaseInfo.assets or #releaseInfo.assets == 0 then
error('Release has no assets, aborted update')
settings.updateStatus = 4
return
end

local downloadUrl
for _, asset in ipairs(releaseInfo.assets) do
if asset.name == "mobilephone.zip" then
downloadUrl = asset.browser_download_url
break
end
end

settings.updateStatus = 1
if not downloadUrl then
error('No matching asset found, aborted update')
settings.updateStatus = 4
return
end

web.get(downloadUrl, function(downloadErr, downloadResponse)
if downloadErr then
settings.updateStatus = 4
error(downloadErr)
return
end

for _, file in ipairs(io.scanZip(downloadResponse.body)) do
local content = io.loadFromZip(downloadResponse.body, file)
if content then
local filePath = file:match('(.*)')
if filePath then
filePath = filePath:gsub("mobilephone/", "")
if io.save(appFolder .. filePath, content) then print('Updating: ' .. file) end
end
end
end

settings.updateStatus = 1
end)
end)
end

Expand Down Expand Up @@ -405,8 +444,8 @@ function onShowWindow()
UpdateTime()
RunUpdate()

if settings.autoUpdate and ((os.time() - settings.lastCheck) / oneDayInSeconds > settings.updateInterval) then
getUpdate()
if settings.autoUpdate and doUpdate then
updateCheckVersion()
end
end

Expand All @@ -416,24 +455,25 @@ function script.windowMainSettings(dt)
ui.textColored('You are using a version of CSP older than 0.2.0!\nIf anything breaks update to the latest version\n ', rgbm.colors.red)
end
ui.tabItem('Update', function()
ui.text('Running Mobilephone version ' .. appVersion)
if ui.checkbox('Enable Automatic Updates', settings.autoUpdate) then
ui.text('Currrently running version ' .. appVersion)
if ui.checkbox('Enable Automatic Update Checks', settings.autoUpdate) then
settings.autoUpdate = not settings.autoUpdate
if settings.autoUpdate then getUpdate() end
if settings.autoUpdate and doUpdate then updateCheckVersion() end
end
if settings.autoUpdate then
ui.text('\t')
ui.sameLine()
settings.updateInterval = ui.slider('##UpdateInterval', settings.updateInterval, 1, 60, 'Check for Update every ' .. '%.0f days')
end
if ui.button('Manual Update') then
getUpdate()
updateCheckVersion()
end
if settings.updateStatus > 0 then
ui.textColored(updateStatusTable[settings.updateStatus], updateStatusColor[settings.updateStatus])

local diff = os.time() - settings.lastCheck
local units = { 'second(s)', 'minute(s)', 'hour(s)', 'day(s)' }
if diff > 600 then settings.updateStatus = 0 end
local units = { 'seconds', 'minutes', 'hours', 'days' }
local values = { 1, 60, 3600, 86400 }

local i = #values
Expand Down

0 comments on commit 8c9d127

Please sign in to comment.