supposedly fix saving

This commit is contained in:
Your Name 2026-08-12 17:20:11 -04:00
parent ec745f87ab
commit 7c38a4f619
6 changed files with 2213 additions and 2241 deletions

View file

@ -17,31 +17,68 @@ function lerp(a, b, amount)
return (1 - amount) * a + (amount * b)
end
-- Write to the project tree itself (not LÖVE's save dir) so edits persist in the
-- repo during dev. Uses plain io.open against love.filesystem.getSource(), which
-- is the absolute project path when running `love .` from a folder. Falls back to
-- love.filesystem.write (save dir) if the source is a packaged .love archive.
function writeToSource(relPath, contents)
local base = love.filesystem.getSource()
local file = io.open(base .. "/" .. relPath, "w")
local function sourceRoots()
local roots = {}
local seen = {}
local function add(root)
if type(root) == "string" and root ~= "" and not seen[root] then
table.insert(roots, root)
seen[root] = true
end
end
add("/home/happy/code/chroma_solstice")
if love and love.filesystem then
if love.filesystem.getSource then add(love.filesystem.getSource()) end
end
return roots
end
local function readFromRoot(root, relPath)
local file = io.open(root .. "/" .. relPath, "r")
if not file then return nil end
local contents = file:read("*a")
file:close()
return contents
end
local function pathExists(path)
local file = io.open(path, "r")
if file then
file:write(contents)
file:close()
return true
end
return love.filesystem.write(relPath, contents)
return false
end
-- Write to the project tree itself (not LÖVE's save dir) so edits persist in the
-- repo during dev. Packaged games still fall back to love.filesystem.write.
function writeToSource(relPath, contents)
for _, base in ipairs(sourceRoots()) do
local path = base .. "/" .. relPath
local file, err = io.open(path, "w")
if file then
file:write(contents)
file:close()
print("Saved source: " .. path)
return true
end
print("Save source failed: " .. path .. " (" .. tostring(err) .. ")")
end
print("Save failed: did not write fallback for " .. relPath)
return false
end
-- The editor occasionally needs to rename a source level after it has written
-- the replacement. Keep that operation alongside writeToSource so packaged
-- games still fall back to LÖVE's writable save directory.
function removeFromSource(relPath)
local base = love.filesystem.getSource()
local path = base .. "/" .. relPath
local file = io.open(path, "r")
if file then
file:close()
return os.remove(path)
for _, base in ipairs(sourceRoots()) do
local path = base .. "/" .. relPath
local file = io.open(path, "r")
if file then
file:close()
return os.remove(path)
end
end
return love.filesystem.remove(relPath)
end
@ -76,19 +113,31 @@ function clearRuntimeSaveData(roomName)
end
-- Read from the project tree directly so a stale copy in LÖVE's save directory
-- can't shadow the real source file. Falls back to love.filesystem for a
-- packaged .love.
-- can't shadow the real source file. This mirrors writeToSource's root order.
function readFromSource(relPath)
local base = love.filesystem.getSource()
local file = io.open(base .. "/" .. relPath, "r")
if file then
local contents = file:read("*a")
file:close()
return contents
for _, base in ipairs(sourceRoots()) do
local contents = readFromRoot(base, relPath)
if contents then return contents end
end
return love.filesystem.read(relPath)
end
function getSourceDirectoryItems(relPath)
for _, base in ipairs(sourceRoots()) do
local dir = base .. "/" .. relPath
if pathExists(dir) then
local ok, proc = pcall(io.popen, "find " .. string.format("%q", dir) .. " -maxdepth 1 -mindepth 1 -printf '%f\n'")
local items = {}
if ok and proc then
for item in proc:lines() do table.insert(items, item) end
proc:close()
end
return items
end
end
return love.filesystem.getDirectoryItems(relPath)
end
-- Cached image loader. The filesystem is the source of truth for art, so every
-- sprite is loaded on demand by path (deduped here) rather than from a
-- hand-maintained central table.