Pyrogenesis trunk
Replay.h
Go to the documentation of this file.
1/* Copyright (C) 2021 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/os_path.h"
22#include "ps/CStr.h"
24
25#include <vector>
26
28class CSimulation2;
29class ScriptInterface;
30
31/**
32 * Replay log recorder interface.
33 * Call its methods at appropriate times during the game.
34 */
36{
37public:
39 virtual ~IReplayLogger() { }
40
41 /**
42 * Started the game with the given game attributes.
43 */
44 virtual void StartGame(JS::MutableHandleValue attribs) = 0;
45
46 /**
47 * Run the given turn with the given collection of player commands.
48 */
49 virtual void Turn(u32 n, u32 turnLength, std::vector<SimulationCommand>& commands) = 0;
50
51 /**
52 * Optional hash of simulation state (for sync checking).
53 */
54 virtual void Hash(const std::string& hash, bool quick) = 0;
55
56 /**
57 * Saves metadata.json containing part of the simulation state used for the summary screen.
58 */
59 virtual void SaveMetadata(const CSimulation2& simulation) = 0;
60
61 /**
62 * Remember the directory containing the commands.txt file, so that we can save additional files to it.
63 */
64 virtual OsPath GetDirectory() const = 0;
65};
66
67/**
68 * Implementation of IReplayLogger that simply throws away all data.
69 */
71{
72public:
73 virtual void StartGame(JS::MutableHandleValue UNUSED(attribs)) { }
74 virtual void Turn(u32 UNUSED(n), u32 UNUSED(turnLength), std::vector<SimulationCommand>& UNUSED(commands)) { }
75 virtual void Hash(const std::string& UNUSED(hash), bool UNUSED(quick)) { }
76 virtual void SaveMetadata(const CSimulation2& UNUSED(simulation)) { };
77 virtual OsPath GetDirectory() const { return OsPath(); }
78};
79
80/**
81 * Implementation of IReplayLogger that saves data to a file in the logs directory.
82 */
84{
86public:
87 CReplayLogger(const ScriptInterface& scriptInterface);
89
90 virtual void StartGame(JS::MutableHandleValue attribs);
91 virtual void Turn(u32 n, u32 turnLength, std::vector<SimulationCommand>& commands);
92 virtual void Hash(const std::string& hash, bool quick);
93 virtual void SaveMetadata(const CSimulation2& simulation);
94 virtual OsPath GetDirectory() const;
95
96private:
98 std::ostream* m_Stream;
100};
101
102/**
103 * Replay log replayer. Runs the log with no graphics and dumps some info to stdout.
104 */
106{
107public:
110
111 void Load(const OsPath& path);
112 void Replay(const bool serializationtest, const int rejointestturn, const bool ooslog, const bool testHashFull, const bool testHashQuick);
113
114private:
115 std::istream* m_Stream;
116 void TestHash(const std::string& hashType, const std::string& replayHash, const bool testHashFull, const bool testHashQuick);
117};
118
119#endif // INCLUDED_REPLAY
Implementation of IReplayLogger that simply throws away all data.
Definition: Replay.h:71
virtual void Hash(const std::string &hash, bool quick)
Optional hash of simulation state (for sync checking).
Definition: Replay.h:75
virtual void StartGame(JS::MutableHandleValue attribs)
Started the game with the given game attributes.
Definition: Replay.h:73
virtual OsPath GetDirectory() const
Remember the directory containing the commands.txt file, so that we can save additional files to it.
Definition: Replay.h:77
virtual void SaveMetadata(const CSimulation2 &simulation)
Saves metadata.json containing part of the simulation state used for the summary screen.
Definition: Replay.h:76
virtual void Turn(u32 n, u32 turnLength, std::vector< SimulationCommand > &commands)
Run the given turn with the given collection of player commands.
Definition: Replay.h:74
Implementation of IReplayLogger that saves data to a file in the logs directory.
Definition: Replay.h:84
virtual void SaveMetadata(const CSimulation2 &simulation)
Saves metadata.json containing part of the simulation state used for the summary screen.
Definition: Replay.cpp:112
std::ostream * m_Stream
Definition: Replay.h:98
OsPath m_Directory
Definition: Replay.h:99
NONCOPYABLE(CReplayLogger)
const ScriptInterface & m_ScriptInterface
Definition: Replay.h:97
virtual void Hash(const std::string &hash, bool quick)
Optional hash of simulation state (for sync checking).
Definition: Replay.cpp:104
virtual void Turn(u32 n, u32 turnLength, std::vector< SimulationCommand > &commands)
Run the given turn with the given collection of player commands.
Definition: Replay.cpp:91
~CReplayLogger()
Definition: Replay.cpp:66
CReplayLogger(const ScriptInterface &scriptInterface)
Definition: Replay.cpp:61
virtual void StartGame(JS::MutableHandleValue attribs)
Started the game with the given game attributes.
Definition: Replay.cpp:71
virtual OsPath GetDirectory() const
Remember the directory containing the commands.txt file, so that we can save additional files to it.
Definition: Replay.cpp:143
Replay log replayer.
Definition: Replay.h:106
void Load(const OsPath &path)
Definition: Replay.cpp:160
void Replay(const bool serializationtest, const int rejointestturn, const bool ooslog, const bool testHashFull, const bool testHashQuick)
Definition: Replay.cpp:190
~CReplayPlayer()
Definition: Replay.cpp:155
std::istream * m_Stream
Definition: Replay.h:115
void TestHash(const std::string &hashType, const std::string &replayHash, const bool testHashFull, const bool testHashQuick)
Definition: Replay.cpp:334
CReplayPlayer()
Definition: Replay.cpp:150
Public API for simulation system.
Definition: Simulation2.h:47
Replay log recorder interface.
Definition: Replay.h:36
IReplayLogger()
Definition: Replay.h:38
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 Turn(u32 n, u32 turnLength, std::vector< SimulationCommand > &commands)=0
Run the given turn with the given collection of player commands.
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:39
Definition: path.h:80
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:72
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning.
Definition: code_annotation.h:40
Path OsPath
Definition: os_path.h:31
Simulation command, typically received over the network in multiplayer games.
Definition: SimulationCommand.h:28
uint32_t u32
Definition: types.h:39