Player12
An open source football management simulator
Loading...
Searching...
No Matches
SaveManager Class Reference

#include <save_manager.h>

Public Types

enum class  FaultPoint : std::uint8_t { MidFlush , TempOpened , SnapshotWritten , Rotated }
 
using FaultHook = std::function< void(FaultPoint, sqlite3 *temp_db)>
 

Static Public Member Functions

static void setFaultHook (FaultHook hook)
 
static void fault (FaultPoint point, sqlite3 *temp_db=nullptr)
 
static SaveInspection inspect (const std::filesystem::path &file, SaveCheck check=SaveCheck::Full)
 
static std::shared_ptr< DatabaseConnection > openWorkingCopy (const std::filesystem::path &file)
 
static std::shared_ptr< DatabaseConnection > createWorkingCopy ()
 
static void stampMetadata (const DatabaseConnection &working, std::uint64_t world_seed, const GameDateValue &game_date, std::int64_t playtime_seconds)
 
static SaveTimings persist (const DatabaseConnection &working, const std::filesystem::path &file, int backups)
 
static void preserveBeforeMigration (const std::filesystem::path &file, int schema_version)
 
static int countBackups (const std::filesystem::path &file)
 
static std::vector< SaveBackup > listBackups (const std::filesystem::path &file)
 
static const SaveBackup * newestUsable (const std::vector< SaveBackup > &backups)
 
static void restoreBackup (const std::filesystem::path &file, const std::filesystem::path &backup)
 
static void deleteSave (const std::filesystem::path &file)
 
static bool isAutosaveDue (AutosaveFrequency frequency, const GameDateValue &last_autosave, int last_autosave_season, const GameDateValue &today, int season, bool managed_match_yesterday)
 
static std::filesystem::path backupPath (const std::filesystem::path &file, int index)
 
static std::filesystem::path tempPath (const std::filesystem::path &file)
 

Detailed Description

Crash-safe persistence of career saves.

A career is played on an in-memory working database; the slot file is only replaced by a complete, verified snapshot (online backup to <slot>.tmp, quick_check, fsync, rotate backups, atomic rename, fsync of the directory). An interrupted save therefore never damages the previous one. See src/database/migrations/README.md, "Save protocol".

Member Typedef Documentation

◆ FaultHook

using SaveManager::FaultHook = std::function<void(FaultPoint, sqlite3* temp_db)>

Member Enumeration Documentation

◆ FaultPoint

enum class SaveManager::FaultPoint : std::uint8_t
strong

Places where tests inject faults (kill, disk full, I/O errors).

Enumerator
MidFlush 

Inside Game::saveGame's transaction.

TempOpened 

Temporary file opened, before the backup.

SnapshotWritten 

Backup done, before verification.

Rotated 

Backups rotated, before the rename.

Member Function Documentation

◆ backupPath()

fs::path SaveManager::backupPath ( const std::filesystem::path &  file,
int  index 
)
static

◆ countBackups()

int SaveManager::countBackups ( const std::filesystem::path &  file)
static

Number of rotation backups of a slot (no file is opened).

◆ createWorkingCopy()

std::shared_ptr< DatabaseConnection > SaveManager::createWorkingCopy ( )
static

A new, empty in-memory working database.

◆ deleteSave()

void SaveManager::deleteSave ( const std::filesystem::path &  file)
static

Removes a save with its sidecars, temporary file and backups.

◆ fault()

void SaveManager::fault ( FaultPoint  point,
sqlite3 *  temp_db = nullptr 
)
static

◆ inspect()

SaveInspection SaveManager::inspect ( const std::filesystem::path &  file,
SaveCheck  check = SaveCheck::Full 
)
static

Read-only inspection; never modifies the file.

◆ isAutosaveDue()

bool SaveManager::isAutosaveDue ( AutosaveFrequency  frequency,
const GameDateValue &  last_autosave,
int  last_autosave_season,
const GameDateValue &  today,
int  season,
bool  managed_match_yesterday 
)
static

Whether an autosave is due today.

◆ listBackups()

std::vector< SaveBackup > SaveManager::listBackups ( const std::filesystem::path &  file)
static

Rotation and pre-migration backups of a slot, newest first.

◆ newestUsable()

const SaveBackup * SaveManager::newestUsable ( const std::vector< SaveBackup > &  backups)
static

The newest backup of backups (as listed by listBackups) that loads, or nullptr: what a recovery dialog offers first.

◆ openWorkingCopy()

std::shared_ptr< DatabaseConnection > SaveManager::openWorkingCopy ( const std::filesystem::path &  file)
static

Copies a save into a new in-memory working database (online backup; content of a leftover -wal file from a crash is included).

Exceptions
SaveFailurewhen the file cannot be read.

◆ persist()

SaveTimings SaveManager::persist ( const DatabaseConnection &  working,
const std::filesystem::path &  file,
int  backups 
)
static

Atomically replaces file with a snapshot of working, keeping backups previous versions. On failure the previous file and its backups are unchanged.

Exceptions
SaveFailure(DiskFull, PermissionDenied, Io).

◆ preserveBeforeMigration()

void SaveManager::preserveBeforeMigration ( const std::filesystem::path &  file,
int  schema_version 
)
static

Keeps a copy of a save before its schema is upgraded (once).

◆ restoreBackup()

void SaveManager::restoreBackup ( const std::filesystem::path &  file,
const std::filesystem::path &  backup 
)
static

Replaces file with backup (verified first). The replaced file is kept as <file>.corrupt-<utc> or <file>.replaced-<utc>, never deleted.

Exceptions
SaveFailure.

◆ setFaultHook()

void SaveManager::setFaultHook ( FaultHook  hook)
static

Test hook, called at every FaultPoint (nullptr to disable).

◆ stampMetadata()

void SaveManager::stampMetadata ( const DatabaseConnection &  working,
std::uint64_t  world_seed,
const GameDateValue &  game_date,
std::int64_t  playtime_seconds 
)
static

Writes the provenance of this save into save_meta of working, including the game, engine, RNG and simulation versions of this build.

◆ tempPath()

fs::path SaveManager::tempPath ( const std::filesystem::path &  file)
static

The documentation for this class was generated from the following files: