feat(packages): ludic.save - versioned save files generic over their content: a version in the file, the steps a game declares into an open registry, a torn write told from a whole one, a backup of only a whole file, a write read back, a newer file refused and read-only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-25 08:28:30 +03:00
parent b24c523fa4
commit 47ba3d9028
11 changed files with 450 additions and 0 deletions

View file

@ -0,0 +1,16 @@
# write.ludic - a write that is known to be on disk: the old file to the backup first (only when it
# is whole - a torn file must never replace a good backup), then the new text, read straight back
# and compared, because a caller about to delete something on the strength of it needs to know
export function save_write(path: string, backup: string, text: string) -> bool {
let old = Fs.read_text(path)
if old != null and len(backup) > 0 and save_text_intact(old) { Fs.write_text(backup, old) }
Fs.write_text(path, text)
let back = Fs.read_text(path)
return back != null and back == text
}
# a SaveRead that must not be written over says so; a write through it refuses
export function save_write_to(r: SaveRead, path: string, backup: string, text: string) -> bool {
if r != null and r.readonly { return false }
return save_write(path, backup, text)
}