Skip to content

Commit

Permalink
Added the ability to download, update, and delete maps.
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Balthazar committed Dec 16, 2024
1 parent ab881ef commit c93d439
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ui/elements/button.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ local buttonCore = {
love.graphics.rectangle('fill', self.cornerX, self.cornerY, self.width, self.height, sType.rx, sType.ry, sType.segments)
love.graphics.setColor(self.inactive and colours.midgrey or colours.white)
if self.text and self.icon then
love.graphics.draw(self.icon, self.cornerX+self.height/2, self.midY, 0, w.scale, w.scale, self.icon:getWidth()/2, self.icon:getHeight()/2)
love.graphics.draw(self.icon, self.cornerX+self.height/2, self.midY, self.iconAngle or 0, w.scale, w.scale, self.icon:getWidth()/2, self.icon:getHeight()/2)
love.graphics.printf(self.text, self.cornerX+self.height, self.midY-8*w.scale, self.widthBase-self.heightBase, 'left', 0, w.scale)
elseif self.text then
love.graphics.printf(self.text, self.cornerX, self.midY-8*w.scale, self.widthBase, 'center', 0, w.scale)
elseif self.icon then
love.graphics.draw(self.icon, self.midX, self.midY, 0, w.scale, w.scale, self.icon:getWidth()/2, self.icon:getHeight()/2)
love.graphics.draw(self.icon, self.midX, self.midY, self.iconAngle or 0, w.scale, w.scale, self.icon:getWidth()/2, self.icon:getHeight()/2)
end
love.graphics.setColor(1,1,1)
end,
Expand Down
73 changes: 73 additions & 0 deletions src/ui/mapView.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
local selected
local textHeadings, textValues, textDesc
local white, grey = {1,1,1}, {0.5, 0.5, 0.5}
local throbber = love.graphics.newImage'graphics/throbber.png'
local writePath = love.filesystem.isFused() and 'SCFA/LOUD/' or ''
local downloading = {}
local outOfDate, localPath, scenarioInfoPath

return {
set = function(self, data)
Expand Down Expand Up @@ -48,6 +52,13 @@ return {
:gsub('\\t', '\t')
or '<error: no description>',
}, 480, 'left' )
localPath = ('%susermaps/%s'):format(writePath, selected.identifier)
scenarioInfoPath = ('%susermaps/%s/%s_scenario.lua'):format(writePath, selected.identifier, selected.identifier)
if love.filesystem.getInfo(scenarioInfoPath) then
scenarioInfo = love.filesystem.read(scenarioInfoPath)
local localVersion = (scenarioInfo:match('map_version%s*%=%s*([^,%s]*)%s*,') or '')
outOfDate = tostring(selected.version):gsub('["\']', '')~=localVersion:gsub('["\']', '')
end

return true
end
Expand Down Expand Up @@ -101,6 +112,68 @@ return {
UI:goBack()
end,
},
require'ui.elements.button'{
text = 'Download',
posXN = 0,
posYN = 1,
offsetXN = 0.5,
offsetXP = 10,
offsetYN = -1.5,
offsetYP = -20,
type = 'bigicon',
onPress = function(self, UI)
if self.inactive then return end
love.filesystem.remove(scenarioInfoPath)
love.thread.getChannel'getMap':push(selected)
love.thread.newThread'utils/threads/getMap.lua':start()
downloading[selected.identifier] = true
selected.downloads = selected.downloads+1
self.inactive = true
outOfDate = nil
end,
update = function(self, UI, delta)
if outOfDate then
self.inactive = nil
self.icon = nil
self.text = 'Update'
elseif love.filesystem.getInfo(scenarioInfoPath) then
self.inactive = true
self.icon = nil
self.text = 'Installed'
else
self.inactive = downloading[selected.identifier]
self.icon = downloading[selected.identifier] and throbber or nil
self.text = (not self.icon) and 'Download' or nil
end
self.iconAngle = love.timer.getTime()
end,
},
require'ui.elements.button'{
text = 'Uninstall',
posXN = 0,
posYN = 1,
offsetXN = 0.5,
offsetXP = 10,
offsetYN = -2.5,
offsetYP = -30,
type = 'bigicon',
onPress = function(self, UI)
if self.inactive then return end
downloading[selected.identifier] = nil
outOfDate = nil
require'utils.filesystem'
forEachFile(localPath, function(path, name)
love.filesystem.remove(path..'/'..(name or ''))
end)
love.filesystem.remove(localPath)
end,
update = function(self, UI, delta)
self.inactive = not love.filesystem.getInfo(scenarioInfoPath)
end,
showIf = function(self, UI)
return love.filesystem.getInfo(scenarioInfoPath)
end,
},
},
goBack = function(self)
setUIMode(require'ui.mapLib')
Expand Down
4 changes: 2 additions & 2 deletions src/ui/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ return {
end,
update = function(self, UI, delta)
if exeFound then
self.inactive = updating
self.inactive = updating or ( (#files+todo+done)>0 and todo~=done )
end
end,
},
Expand Down Expand Up @@ -469,7 +469,7 @@ return {
love.graphics.printf(text, 576*scale, (337+(i-1)*20)*scale, 556, 'right', 0, scale, scale)
end
]]
if updating~=nil then
if updating~=nil or (#files+todo+done)>0 then
love.graphics.printf(('Files downloading: %d Queued: %d Finished: %d'):format(#files, todo, done), 20*scale, 337*scale, 556, 'right', 0, scale, scale)
end
for i, text in ipairs(files) do
Expand Down
35 changes: 35 additions & 0 deletions src/utils/network.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,39 @@ function network.getMapLibFile(file)
return file, body
end

local writePath = love.filesystem.isFused() and 'SCFA/LOUD/' or ''

function network.getMap(data)
feedback:push(1)
feedback:push{data.name, 'downloading'}
local code, body, headers = require'https'.request('https://theloudproject.org:8081/'..data.file)
if code~=200 then
feedback:push{data.name, 'done'}
feedback:push(('%s map download failed: code %s: %s'):format(data.name, tostring(code), body))
return
end
feedback:push{data.name, 'writing'}
local path, filename = data.file:match'(.*)(/[^/]*)'
love.filesystem.createDirectory('temp/'..path)
local SCDWritePath = 'temp/'..data.file
love.filesystem.write(SCDWritePath, body)
local mountPath = data.file:gsub('/', '-')
love.filesystem.mount(SCDWritePath, mountPath)

require'utils.filesystem'
forEachFile(mountPath, function(path, name)
local inputPath = path..'/'..(name or '')
local outputPath = writePath..'usermaps'..path:sub(#mountPath+1)..'/'..(name or '')
if not name then
love.filesystem.createDirectory(outputPath)
return
end
love.filesystem.write(outputPath, (love.filesystem.read(inputPath)))
end)
love.filesystem.unmount(SCDWritePath)
love.filesystem.remove(SCDWritePath)
feedback:push{data.name, 'done'}
feedback:push(('Map "%s" downloaded.'):format(data.name))
end

return network
4 changes: 4 additions & 0 deletions src/utils/threads/getMap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local channel = love.thread.getChannel'getMap'
local network = require'utils.network'

network.getMap(channel:demand())

0 comments on commit c93d439

Please sign in to comment.