Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Replay.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_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
31class CSimulation2;
32namespace Script { class Interface; }
34
35/**
36 * Replay log recorder interface.
37 * Call its methods at appropriate times during the game.
38 */
40{
41public:
43 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 */
75{
76public:
77 virtual void StartGame(JS::MutableHandleValue /*attribs*/) { }
78 virtual void Turn(std::uint32_t /*n*/, std::uint32_t /*turnLength*/, std::vector<SimulationCommand>&)
79 {}
80 virtual void Hash(const std::string& /*hash*/, bool /*quick*/) { }
81 virtual void SaveMetadata(const CSimulation2&) { };
82 virtual OsPath GetDirectory() const { return OsPath(); }
83};
84
85/**
86 * Implementation of IReplayLogger that saves data to a file in the logs directory.
87 */
89{
91public:
92 CReplayLogger(const Script::Interface& scriptInterface);
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
101private:
103 std::ostream* m_Stream;
105};
106
107/**
108 * Replay log replayer. Runs the log with no graphics and dumps some info to stdout.
109 */
111{
112public:
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
120private:
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
Implementation of IReplayLogger that simply throws away all data.
Definition: Replay.h:75
virtual void Hash(const std::string &, bool)
Optional hash of simulation state (for sync checking).
Definition: Replay.h:80
virtual void Turn(std::uint32_t, std::uint32_t, std::vector< SimulationCommand > &)
Run the given turn with the given collection of player commands.
Definition: Replay.h:78
virtual OsPath GetDirectory() const
Remember the directory containing the commands.txt file, so that we can save additional files to it.
Definition: Replay.h:82
virtual void StartGame(JS::MutableHandleValue)
Started the game with the given game attributes.
Definition: Replay.h:77
virtual void SaveMetadata(const CSimulation2 &)
Saves metadata.json containing part of the simulation state used for the summary screen.
Definition: Replay.h:81
Implementation of IReplayLogger that saves data to a file in the logs directory.
Definition: Replay.h:89
virtual void SaveMetadata(const CSimulation2 &simulation)
Saves metadata.json containing part of the simulation state used for the summary screen.
Definition: Replay.cpp:116
std::ostream * m_Stream
Definition: Replay.h:103
OsPath m_Directory
Definition: Replay.h:104
NONCOPYABLE(CReplayLogger)
virtual void Hash(const std::string &hash, bool quick)
Optional hash of simulation state (for sync checking).
Definition: Replay.cpp:108
CReplayLogger(const Script::Interface &scriptInterface)
Definition: Replay.cpp:65
~CReplayLogger()
Definition: Replay.cpp:70
virtual void Turn(std::uint32_t n, std::uint32_t turnLength, std::vector< SimulationCommand > &commands)
Run the given turn with the given collection of player commands.
Definition: Replay.cpp:95
const Script::Interface & m_ScriptInterface
Definition: Replay.h:102
virtual void StartGame(JS::MutableHandleValue attribs)
Started the game with the given game attributes.
Definition: Replay.cpp:75
virtual OsPath GetDirectory() const
Remember the directory containing the commands.txt file, so that we can save additional files to it.
Definition: Replay.cpp:147
Replay log replayer.
Definition: Replay.h:111
void Load(const OsPath &path)
Definition: Replay.cpp:164
void Replay(const int serializationtestturn, const int rejointestturn, const bool ooslog, const bool testHashFull, const bool testHashQuick)
Definition: Replay.cpp:197
~CReplayPlayer()
Definition: Replay.cpp:159
std::istream * m_Stream
Definition: Replay.h:121
void TestHash(const std::string &hashType, const std::string &replayHash, const bool testHashFull, const bool testHashQuick)
Definition: Replay.cpp:349
CReplayPlayer()
Definition: Replay.cpp:154
Public API for simulation system.
Definition: Simulation2.h:57
Replay log recorder interface.
Definition: Replay.h:40
IReplayLogger()
Definition: Replay.h:42
virtual void Turn(std::uint32_t n, std::uint32_t turnLength, std::vector< SimulationCommand > &commands)=0
Run the given turn with the given collection of player commands.
virtual void StartGame(JS::MutableHandleValue attribs)=0
Started the game with the given game attributes.
virtual OsPath GetDirectory() const =0
Remember the directory containing the commands.txt file, so that we can save additional files to it.
virtual void Hash(const std::string &hash, bool quick)=0
Optional hash of simulation state (for sync checking).
virtual void SaveMetadata(const CSimulation2 &simulation)=0
Saves metadata.json containing part of the simulation state used for the summary screen.
virtual ~IReplayLogger()
Definition: Replay.h:43
Definition: path.h:84
Definition: Interface.h:93
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
Path OsPath
Definition: os_path.h:37
Simulation command, typically received over the network in multiplayer games.
Definition: SimulationCommand.h:39