Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
SavedGame.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_SAVEDGAME
19#define INCLUDED_SAVEDGAME
20
21#include "lib/status.h"
23
24#include <js/Value.h>
25#include <optional>
26#include <string>
27
28class CSimulation2;
29class CStrW;
30namespace Script { class Interface; }
31
32/**
33 * @file
34 * Contains functions for managing saved game archives.
35 *
36 * A saved game is simply a zip archive with the extension '0adsave'
37 * and containing two files:
38 * <ul>
39 * <li>metadata.json - JSON data file containing the game metadata</li>
40 * <li>simulation.dat - the serialized simulation state data</li>
41 * </ul>
42 */
43
44namespace SavedGames
45{
46 /**
47 * Create new saved game archive with given name and simulation data
48 *
49 * @param name Name to save the game with
50 * @param description A user-given description of the save
51 * @param simulation
52 * @param guiMetadataClone if not NULL, store some UI-related data with the saved game
53 * @return INFO::OK if successfully saved, else an error Status
54 */
55 Status Save(const CStrW& name, const CStrW& description, CSimulation2& simulation, const Script::StructuredClone& guiMetadataClone);
56
57 /**
58 * Create new saved game archive with given prefix and simulation data
59 *
60 * @param prefix Create new numbered file starting with this prefix
61 * @param description A user-given description of the save
62 * @param simulation
63 * @param guiMetadataClone if not NULL, store some UI-related data with the saved game
64 * @return INFO::OK if successfully saved, else an error Status
65 */
66 Status SavePrefix(const CStrW& prefix, const CStrW& description, CSimulation2& simulation, const Script::StructuredClone& guiMetadataClone);
67
69 {
70 // Object containing metadata associated with saved game,
71 // parsed from metadata.json inside the archive.
72 JS::Value metadata;
73 // Serialized simulation state stored as string of bytes,
74 // loaded from simulation.dat inside the archive.
75 std::string savedState;
76 };
77
78 /**
79 * Load saved game archive with the given name
80 *
81 * @param name filename of saved game (without path or extension)
82 * @param scriptInterface
83 * @return An empty `std::optional` if an error ocoured.
84 */
85 std::optional<LoadResult> Load(const Script::Interface& scriptInterface, const std::wstring& name);
86
87 /**
88 * Get list of saved games for GUI script usage
89 *
90 * @param scriptInterface the Script::Interface in which to create the return data.
91 * @return array of objects containing saved game data
92 */
93 JS::Value GetSavedGames(const Script::Interface& scriptInterface);
94
95 /**
96 * Permanently deletes the saved game archive with the given name
97 *
98 * @param name filename of saved game (without path or extension)
99 * @return true if deletion was successful, or false on error
100 */
101 bool DeleteSavedGame(const std::wstring& name);
102}
103
104#endif // INCLUDED_SAVEDGAME
Public API for simulation system.
Definition: Simulation2.h:57
Definition: Interface.h:93
Definition: SavedGame.h:45
JS::Value GetSavedGames(const Script::Interface &scriptInterface)
Get list of saved games for GUI script usage.
Definition: SavedGame.cpp:282
std::optional< LoadResult > Load(const Script::Interface &scriptInterface, const std::wstring &name)
Load saved game archive with the given name.
Definition: SavedGame.cpp:236
bool DeleteSavedGame(const std::wstring &name)
Permanently deletes the saved game archive with the given name.
Definition: SavedGame.cpp:336
Status Save(const CStrW &name, const CStrW &description, CSimulation2 &simulation, const Script::StructuredClone &guiMetadataClone)
Create new saved game archive with given name and simulation data.
Definition: SavedGame.cpp:80
Status SavePrefix(const CStrW &prefix, const CStrW &description, CSimulation2 &simulation, const Script::StructuredClone &guiMetadataClone)
Create new saved game archive with given prefix and simulation data.
Definition: SavedGame.cpp:65
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
std::shared_ptr< JSStructuredCloneData > StructuredClone
Structured clones are a way to serialize 'simple' JS::Values into a buffer that can safely be passed ...
Definition: StructuredClone.h:39
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: SavedGame.h:69
JS::Value metadata
Definition: SavedGame.h:72
std::string savedState
Definition: SavedGame.h:75