Skip to content

Commit

Permalink
Loading and saving maps is now more responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruor committed Oct 11, 2019
1 parent d49b165 commit b8358e9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/celeste_render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,8 @@ function celesteRender.drawFiller(filler, viewport)
end)
end

-- TODO - Move canvas rendering operations into a update function
-- Currently it visually breaks wipes, and shouldn't be in the render function regardless
function celesteRender.drawMap(state)
if state.map then
local map = state.map
Expand Down
57 changes: 38 additions & 19 deletions src/loaded_state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,57 @@ function state.loadFile(filename)

tasks.newTask(
(-> filename and mapcoder.decodeFile(filename)),
function(task)
if task.result then
celesteRender.invalidateRoomCache()
celesteRender.clearBatchingTasks()
function(binTask)
if binTask.result then
tasks.newTask(
(-> sideStruct.decodeTaskable(binTask.result)),
function(decodeTask)
celesteRender.invalidateRoomCache()
celesteRender.clearBatchingTasks()

state.filename = filename
state.side = decodeTask.result
state.map = state.side.map
state.selectedRoom = state.map and state.map.rooms[1]

state.filename = filename
state.side = sideStruct.decode(task.result)
state.map = state.side.map
state.selectedRoom = state.map and state.map.rooms[1]
sceneHandler.changeScene("Editor")
end
)

print("Loaded binary data")

else
-- TODO - Toast the user, failed to load
end

sceneHandler.changeScene("Editor")
end
)
end

-- TODO - Make and use a tasked version of sideStruct encode
function state.saveFile(filename)
tasks.newTask(
(-> filename and state.side and mapcoder.encodeFile(filename, sideStruct.encode(state.side))),
function(task)
if task.result then
state.filename = filename
if filename and state.side then
tasks.newTask(
(-> sideStruct.encodeTaskable(state.side)),
function(encodeTask)
if encodeTask.result then
tasks.newTask(
(-> mapcoder.encodeFile(filename, encodeTask.result)),
function(binTask)
if binTask.result then
state.filename = filename

else
-- TODO - Toast the user, failed to save
end
end
)

else
-- TODO - Toast the user, failed to save
else
-- TODO - Toast the user, failed to save
end
end
end
)
)
end
end

function state.selectRoom(room)
Expand Down
12 changes: 12 additions & 0 deletions src/structs/map.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local tasks = require("task")
local roomStruct = require("structs.room")
local fillerStruct = require("structs.filler")
local styleStruct = require("structs.style")
Expand All @@ -22,13 +23,16 @@ function mapStruct.decode(data)
if d.__name == "levels" then
for j, room in ipairs(d.__children or {}) do
table.insert(map.rooms, roomStruct.decode(room))
tasks.yield()
end

elseif d.__name == "Filler" then
for j, filler in ipairs(d.__children or {}) do
table.insert(map.fillers, fillerStruct.decode(filler))
end

tasks.yield()

elseif d.__name == "Style" then
for j, style in ipairs(d.__children or {}) do
if style.__name == "Foregrounds" then
Expand All @@ -38,8 +42,12 @@ function mapStruct.decode(data)
map.stylesBg = styleStruct.decode(style)
end
end

tasks.yield()
end
end

tasks.update(map)

return map
end
Expand Down Expand Up @@ -76,6 +84,8 @@ function mapStruct.encode(map)
__name = "levels",
__children = children
})

tasks.yield()
end

local style = {
Expand All @@ -95,6 +105,8 @@ function mapStruct.encode(map)

table.insert(res.__children, style)

tasks.update(res)

return res
end

Expand Down
53 changes: 52 additions & 1 deletion src/structs/side.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local tasks = require("task")
local utils = require("utils")
local mapStruct = require("structs.map")

Expand Down Expand Up @@ -60,7 +61,7 @@ end

function sideStruct.decode(data)
local side = {
_type = "map",
_type = "side",
_raw = data
}

Expand All @@ -74,9 +75,38 @@ function sideStruct.decode(data)

side.map = mapStruct.decode(data)

tasks.update(side)

return side
end

function sideStruct.decodeTaskable(data, tasksTarget)
local sideTask = tasks.newTask(
function(task)
local side = {}
local mapTask = tasks.newTask(-> mapStruct.decode(data), nil, tasksTarget)

task:waitFor(mapTask)
side.map = mapTask.result

for i, v in ipairs(data.__children or {}) do
local name = v.__name

if not decoderBlacklist[name] then
tableify(v, side)
end
end

tasks.update(side)
end,
nil,
tasksTarget
)

tasks.waitFor(sideTask)
tasks.update(sideTask.result)
end

function sideStruct.encode(side)
local res = mapStruct.encode(side.map)

Expand All @@ -89,4 +119,25 @@ function sideStruct.encode(side)
return res
end

function sideStruct.encodeTaskable(side, tasksTarget)
local sideTask = tasks.newTask(
function(task)
local mapTask = tasks.newTask(-> mapStruct.encode(side.map), nil, tasksTarget)

tasks.waitFor(mapTask)
local res = mapTask.result

for k, v in pairs(side) do
if not encoderBlacklist[k] then
table.insert(res.__children, binfileify(k, v))
end
end

tasks.update(res)
end,
nil,
tasksTarget
)
end

return sideStruct

0 comments on commit b8358e9

Please sign in to comment.