Files
Thomas MartinandClaude Opus 5.5 77096ee2bc Memory cards survive a save state; Archive::end checks before closing
A state restored the cards' contents but not their host folder, so a game's saves after
--load-state stayed in memory and were lost. Cards kept in a folder are now taken back from
the folder when a state loads (a console's memory card is not part of a save state); cards in
memory stay the state's.

Archive::end closed a section before checking its size: a reader trying an older layout (the
libraries' sif section) that failed there rewound into a nameless section and the load failed
further on. The check now comes first. Tests for both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 21:17:24 +01:00

138 lines
5.2 KiB
C++

// SPDX-License-Identifier: MIT
// Save state archives and files (bozo/state.h).
#include "bozo/state.h"
#include <cstdio>
#include <cstring>
namespace bozo::state
{
namespace
{
constexpr char kMagic[8] = {'B', 'O', 'Z', 'O', 'S', 'A', 'V', 'E'};
constexpr uint32_t kVersion = 1;
}
void Archive::raw(void *p, size_t n)
{
if (!n)
return;
if (!loading_)
{
const auto *b = static_cast<const uint8_t *>(p);
data_.insert(data_.end(), b, b + n);
return;
}
const size_t limit = open_.empty() ? data_.size() : open_.back().second;
if (n > limit - pos_)
throw Error("save state: section " + (open_.empty() ? std::string("<file>") : open_.back().first) +
" ends before its fields");
std::memcpy(p, data_.data() + pos_, n);
pos_ += n;
}
void Archive::begin(const char *name)
{
std::string n = name;
io(*this, n);
if (loading_ && n != name)
throw Error("save state: expected section " + std::string(name) + ", found " + n);
uint64_t size = 0;
const size_t at = loading_ ? pos_ : data_.size();
raw(&size, sizeof size);
if (!loading_)
open_.emplace_back(name, at);
else
{
if (size > data_.size() - pos_)
throw Error("save state: section " + n + " runs past the file");
open_.emplace_back(name, pos_ + size);
}
}
bool Archive::nextIs(const char *name) const
{
if (!loading_)
return false;
const size_t n = std::strlen(name);
uint64_t length = 0;
if (data_.size() - pos_ < sizeof length + n)
return false;
std::memcpy(&length, data_.data() + pos_, sizeof length); // a section begins with its name: count, then bytes
return length == n && std::memcmp(data_.data() + pos_ + sizeof length, name, n) == 0;
}
void Archive::end()
{
if (open_.empty())
throw Error("save state: a section ended twice");
const auto [name, at] = open_.back();
// Checked before the section is closed: a reader that tries an older layout and rewinds (Mark) finds the
// section still open (closed first, the rewind reopened it as a nameless one ending at 0).
if (loading_ && pos_ != at)
throw Error("save state: section " + name + " has " + std::to_string(at - pos_) +
" bytes its reader did not take (a different build wrote it?)");
open_.pop_back();
if (!loading_)
{
const uint64_t size = data_.size() - at - sizeof(uint64_t);
std::memcpy(data_.data() + at, &size, sizeof size);
}
}
void writeFile(const std::string &path, const std::string &game, const Archive &a)
{
const std::string temp = path + ".tmp";
std::FILE *f = std::fopen(temp.c_str(), "wb");
if (!f)
throw Error("save state: cannot write " + temp);
Archive header;
std::string tag = game;
uint32_t version = kVersion;
header.raw(const_cast<char *>(kMagic), sizeof kMagic);
io(header, version);
io(header, tag);
const bool ok = std::fwrite(header.data().data(), 1, header.data().size(), f) == header.data().size() &&
std::fwrite(a.data().data(), 1, a.data().size(), f) == a.data().size();
if (std::fclose(f) != 0 || !ok)
{
std::remove(temp.c_str());
throw Error("save state: cannot write " + temp);
}
std::remove(path.c_str());
if (std::rename(temp.c_str(), path.c_str()) != 0)
throw Error("save state: cannot rename " + temp + " to " + path);
}
Archive readFile(const std::string &path, const std::string &game)
{
std::FILE *f = std::fopen(path.c_str(), "rb");
if (!f)
throw Error("save state: cannot open " + path);
std::vector<uint8_t> bytes;
uint8_t buffer[1 << 16];
for (size_t n; (n = std::fread(buffer, 1, sizeof buffer, f)) != 0;)
bytes.insert(bytes.end(), buffer, buffer + n);
std::fclose(f);
Archive header(std::move(bytes));
char magic[sizeof kMagic];
header.raw(magic, sizeof magic);
if (std::memcmp(magic, kMagic, sizeof kMagic) != 0)
throw Error("save state: " + path + " is not a save state");
uint32_t version = 0;
std::string tag;
io(header, version);
io(header, tag);
if (version != kVersion)
throw Error("save state: " + path + " has format " + std::to_string(version) + ", this build reads " +
std::to_string(kVersion));
if (tag != game)
throw Error("save state: " + path + " is for " + tag + ", not " + game);
// The rest, as an archive of its own.
std::vector<uint8_t> rest;
const size_t used = sizeof kMagic + sizeof version + sizeof(uint64_t) + tag.size();
rest.assign(header.data().begin() + static_cast<std::ptrdiff_t>(used), header.data().end());
return Archive(std::move(rest));
}
}