Line data Source code
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" 22 : #include "scriptinterface/StructuredClone.h" 23 : 24 : #include <js/Value.h> 25 : #include <optional> 26 : #include <string> 27 : 28 : class CSimulation2; 29 : class CStrW; 30 : namespace 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 : 44 : namespace 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 : 68 0 : struct LoadResult 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