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_REPLAY 19 : #define INCLUDED_REPLAY 20 : 21 : #include "lib/code_annotation.h" 22 : #include "lib/os_path.h" 23 : 24 : #include <iosfwd> 25 : #include <js/RootingAPI.h> 26 : #include <js/TypeDecls.h> 27 : #include <js/Value.h> 28 : #include <string> 29 : #include <vector> 30 : 31 : class CSimulation2; 32 : namespace Script { class Interface; } 33 : struct SimulationCommand; 34 : 35 : /** 36 : * Replay log recorder interface. 37 : * Call its methods at appropriate times during the game. 38 : */ 39 : class IReplayLogger 40 : { 41 : public: 42 3 : IReplayLogger() { } 43 0 : virtual ~IReplayLogger() { } 44 : 45 : /** 46 : * Started the game with the given game attributes. 47 : */ 48 : virtual void StartGame(JS::MutableHandleValue attribs) = 0; 49 : 50 : /** 51 : * Run the given turn with the given collection of player commands. 52 : */ 53 : virtual void Turn(std::uint32_t n, std::uint32_t turnLength, std::vector<SimulationCommand>& commands) = 0; 54 : 55 : /** 56 : * Optional hash of simulation state (for sync checking). 57 : */ 58 : virtual void Hash(const std::string& hash, bool quick) = 0; 59 : 60 : /** 61 : * Saves metadata.json containing part of the simulation state used for the summary screen. 62 : */ 63 : virtual void SaveMetadata(const CSimulation2& simulation) = 0; 64 : 65 : /** 66 : * Remember the directory containing the commands.txt file, so that we can save additional files to it. 67 : */ 68 : virtual OsPath GetDirectory() const = 0; 69 : }; 70 : 71 : /** 72 : * Implementation of IReplayLogger that simply throws away all data. 73 : */ 74 3 : class CDummyReplayLogger : public IReplayLogger 75 : { 76 : public: 77 3 : virtual void StartGame(JS::MutableHandleValue /*attribs*/) { } 78 180 : virtual void Turn(std::uint32_t /*n*/, std::uint32_t /*turnLength*/, std::vector<SimulationCommand>&) 79 180 : {} 80 0 : virtual void Hash(const std::string& /*hash*/, bool /*quick*/) { } 81 3 : virtual void SaveMetadata(const CSimulation2&) { }; 82 0 : virtual OsPath GetDirectory() const { return OsPath(); } 83 : }; 84 : 85 : /** 86 : * Implementation of IReplayLogger that saves data to a file in the logs directory. 87 : */ 88 : class CReplayLogger : public IReplayLogger 89 : { 90 : NONCOPYABLE(CReplayLogger); 91 : public: 92 : CReplayLogger(const Script::Interface& scriptInterface); 93 : ~CReplayLogger(); 94 : 95 : virtual void StartGame(JS::MutableHandleValue attribs); 96 : virtual void Turn(std::uint32_t n, std::uint32_t turnLength, std::vector<SimulationCommand>& commands); 97 : virtual void Hash(const std::string& hash, bool quick); 98 : virtual void SaveMetadata(const CSimulation2& simulation); 99 : virtual OsPath GetDirectory() const; 100 : 101 : private: 102 : const Script::Interface& m_ScriptInterface; 103 : std::ostream* m_Stream; 104 : OsPath m_Directory; 105 : }; 106 : 107 : /** 108 : * Replay log replayer. Runs the log with no graphics and dumps some info to stdout. 109 : */ 110 : class CReplayPlayer 111 : { 112 : public: 113 : CReplayPlayer(); 114 : ~CReplayPlayer(); 115 : 116 : void Load(const OsPath& path); 117 : void Replay(const int serializationtestturn, const int rejointestturn, const bool ooslog, 118 : const bool testHashFull, const bool testHashQuick); 119 : 120 : private: 121 : std::istream* m_Stream; 122 : void TestHash(const std::string& hashType, const std::string& replayHash, const bool testHashFull, const bool testHashQuick); 123 : }; 124 : 125 : #endif // INCLUDED_REPLAY