Skip to content

Commit

Permalink
ETag-based caching for SIF beatmap download
Browse files Browse the repository at this point in the history
Improved FFX to load from more libraries from more combination (mainly for Linux & macOS support).
  • Loading branch information
MikuAuahDark committed Mar 11, 2018
1 parent 2f9d33b commit 1c024b4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
16 changes: 16 additions & 0 deletions AquaShine/FFX2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ else
end

LOG("AquaShineFFmpeg", "Failed to load %s: %s", libname, out)
name = "lib"..libname..".so."..ver
s, out = pcall(ffi.load, name)

if s then
return out
end

LOG("AquaShineFFmpeg", "Failed to load %s: %s", name, out)
name = "lib"..libname..".dylib."..ver
s, out = pcall(ffi.load, name)

if s then
return out
end

LOG("AquaShineFFmpeg", "Failed to load %s: %s", name, out)
return nil
end
end
Expand Down
62 changes: 41 additions & 21 deletions download_beatmap_sif.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,44 @@ local address = "http://r.llsif.win/"
DLBeatmap.DLCB = {
Response = {},
RespLen = 0,
Error = function(this, msg)
Error = function(_, msg)
DLBeatmap.SetStatus("Cannot get beatmap list: "..msg)
DLBeatmap.DLCB.Response = {}
DLBeatmap.DLCB.RespLen = 0
end,
Receive = function(this, data)
DLBeatmap.DLCB.RespLen = DLBeatmap.DLCB.RespLen + #data
DLBeatmap.DLCB.Response[#DLBeatmap.DLCB.Response + 1] = data

-- If ContentLength is provided, show progress as percent
if this.ContentLength then
DLBeatmap.SetStatus(string.format("Downloading Beatmap List... (%d%%)", DLBeatmap.DLCB.RespLen / this.ContentLength * 100))
end
end,
Done = function(this)
-- Setup beatmap list, sort by live track ID
local str = table.concat(DLBeatmap.DLCB.Response)
local json = JSON:decode(str)
assert(love.filesystem.write("maps.json", love.data.compress("string", "zlib", str, 9)))
DLBeatmap.DLCB.RespLen = 0
-- If the response code is 304, that means our previous copy is good.
if this.StatusCode == 304 then
DLBeatmap.SetStatus("Local copy is used")
-- resplen will be 0 anyway
return
elseif this.StatusCode == 200 then
-- Setup beatmap list, sort by live track ID
local str = table.concat(DLBeatmap.DLCB.Response)
local json = JSON:decode(str)
local etag = this.HeaderData["ETag"] or this.HeaderData["etag"]
assert(love.filesystem.write("maps.json", love.data.compress("string", "zlib", str, 9)))
assert(love.filesystem.write("maps.json.etag", etag))
DLBeatmap.DLCB.RespLen = 0
DLBeatmap.DLCB.Response = {}
DLBeatmap.SetStatus("List refreshed!")
DLBeatmap.SetupList(json)
else
-- Error
DLBeatmap.SetStatus("Cannot get beatmap list: Status code "..this.StatusCode)
end

DLBeatmap.DLCB.Response = {}
DLBeatmap.SetStatus()
DLBeatmap.SetupList(json)
DLBeatmap.DLCB.RespLen = 0
end,
}

Expand Down Expand Up @@ -157,13 +172,12 @@ function DLBeatmap.MovePage(inc)
end

function DLBeatmap.Start()
local maps_info = love.filesystem.getInfo("maps.json")
local s_button_03 = AquaShine.LoadImage("assets/image/ui/s_button_03.png")
local s_button_03se = AquaShine.LoadImage("assets/image/ui/s_button_03se.png")
local maps_info = love.filesystem.getInfo("maps.json.etag")
local descFont = AquaShine.LoadFont("MTLmr3m.ttf", 22)
DLBeatmap.SwipeData = {nil, nil}
DLBeatmap.SwipeThreshold = 128
DLBeatmap.CurrentPage = 0
DLBeatmap.Download = AquaShine.GetCachedData("BeatmapDLHandle", AquaShine.Download)
DLBeatmap.MainNode = BackgroundImage(13)
:addChild(BackNavigation("SIF Download Beatmap", ":beatmap_select"))
:addChild(TextShadow(descFont, "", 52, 500)
Expand All @@ -173,21 +187,27 @@ function DLBeatmap.Start()
:setShadow(1, 1, true)
)

-- If there was previously cached maps data, use that
-- Otherwise, download it.
if not(maps_info) or (maps_info.modtime or 0) + 86400 < os.time() then
DLBeatmap.SetStatus("Downloading Beatmap List...")
DLBeatmap.Download = AquaShine.GetCachedData("BeatmapDLHandle", AquaShine.Download)
else
-- If maps.json.etag exist, that means we downloaded a previous copy
-- and we keep attempt to download it, but by specifying If-None-Match.
-- In case of failure, if previous copy exist, use it anyway.
if maps_info then
local json = JSON:decode(love.data.decompress("string", "zlib", love.filesystem.read("maps.json")))
DLBeatmap.LastCopy = love.filesystem.read("maps.json.etag")
DLBeatmap.SetupList(json)
end
end

function DLBeatmap.Update(deltaT)
if not(DLBeatmap.BeatmapListRaw) and DLBeatmap.Download and not(DLBeatmap.Download.downloading) then
function DLBeatmap.Update()
if not(DLBeatmap.Download.downloading) and not(DLBeatmap.IsDownloaded) then
local header
if DLBeatmap.LastCopy then
header = {["If-None-Match"] = DLBeatmap.LastCopy}
end

DLBeatmap.SetStatus("Downloading Beatmap List...")
DLBeatmap.Download:SetCallback(DLBeatmap.DLCB)
DLBeatmap.Download:Download(address.."maps.json")
DLBeatmap.Download:Download(address.."maps.json", header)
DLBeatmap.IsDownloaded = true
end
end

Expand Down

0 comments on commit 1c024b4

Please sign in to comment.