Skip to content

Commit

Permalink
Savestate/snapshot frozen versioning (#110)
Browse files Browse the repository at this point in the history
* Add versioning to save states

* Switch to frozen version (changes when save state encoding changes)
  • Loading branch information
tiberiusbrown authored Aug 15, 2024
1 parent 85bbb94 commit b3d6932
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/absim_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <miniz.h>

static constexpr std::array<char, 8> SNAPSHOT_ID =
constexpr std::array<char, 8> SNAPSHOT_ID =
{
'_', 'A', 'B', 'S', 'I', 'M', '_', '\0',
};
Expand All @@ -37,20 +37,22 @@ struct version_t
}
};

static std::string version_str(version_t const& v)
{
char buf[32];
snprintf(buf, sizeof(buf), "v%u.%u.%u", v.major, v.minor, v.patch);
return buf;
}

constexpr version_t VERSION_INFO =
{
ARDENS_VERSION_MAJOR,
ARDENS_VERSION_MINOR,
ARDENS_VERSION_PATCH
};

constexpr version_t SNAPSHOT_VERSION = { 0, 23, 11 };

static std::string version_str(version_t const& v)
{
char buf[32];
snprintf(buf, sizeof(buf), "v%u.%u.%u", v.major, v.minor, v.patch);
return buf;
}

struct CustomUniquePtrExt
{
template<class Ser, class T, class F>
Expand Down Expand Up @@ -349,13 +351,26 @@ static std::string serdes_snapshot(Archive& ar, arduboy_t& a)
bool arduboy_t::save_savestate(std::ostream& f)
{
bitsery::Serializer<bitsery::OutputStreamAdapter> ar(f);
ar(SNAPSHOT_ID);
ar(SNAPSHOT_VERSION);
auto r = serdes_savestate(ar, *this);
return !r.empty();
}

std::string arduboy_t::load_savestate(std::istream& f)
{
bitsery::Deserializer<bitsery::InputStreamAdapter> ar(f);

std::array<char, 8> id;
ar(id);
if(id != SNAPSHOT_ID)
return "Snapshot: invalid identifier";

version_t version;
ar(version);
if(version != SNAPSHOT_VERSION)
return "Snapshot: incompatible version (created with " + version_str(version) + ")";

auto r = serdes_savestate(ar, *this);
return r;
}
Expand Down Expand Up @@ -389,7 +404,7 @@ bool arduboy_t::save_snapshot(std::ostream& f)
{
bitsery::Serializer<StreamAdapter> ar(f);
ar(SNAPSHOT_ID);
ar(VERSION_INFO);
ar(SNAPSHOT_VERSION);
uint32_t tsize = (uint32_t)data.size();
ar(tsize);
}
Expand Down Expand Up @@ -418,8 +433,8 @@ std::string arduboy_t::load_snapshot(std::istream& f)

version_t version;
ar(version);
if(version != VERSION_INFO)
return "Snapshot: requires " + version_str(version);
if(version != SNAPSHOT_VERSION)
return "Snapshot: incompatible version (created with " + version_str(version) + ")";

ar(dst_size);

Expand Down

0 comments on commit b3d6932

Please sign in to comment.