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 
27 struct SimulationCommand;
28 class CSimulation2;
29 class ScriptInterface;
30 
31 /**
32  * Replay log recorder interface.
33  * Call its methods at appropriate times during the game.
34  */
36 {
37 public:
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 {
72 public:
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 {
86 public:
87  CReplayLogger(const ScriptInterface& scriptInterface);
88  ~CReplayLogger();
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 
96 private:
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 {
107 public:
108  CReplayPlayer();
109  ~CReplayPlayer();
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 
114 private:
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
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
const ScriptInterface & m_ScriptInterface
Definition: Replay.h:97
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 ~IReplayLogger()
Definition: Replay.h:39
std::ostream * m_Stream
Definition: Replay.h:98
virtual void Turn(u32 n, u32 turnLength, std::vector< SimulationCommand > &commands)=0
Run the given turn with the given collection of player commands.
Replay log recorder interface.
Definition: Replay.h:35
virtual void SaveMetadata(const CSimulation2 &simulation)
Saves metadata.json containing part of the simulation state used for the summary screen.
Definition: Replay.h:76
Public API for simulation system.
Definition: Simulation2.h:46
virtual void SaveMetadata(const CSimulation2 &simulation)=0
Saves metadata.json containing part of the simulation state used for the summary screen.
uint32_t u32
Definition: types.h:39
Definition: path.h:79
virtual void StartGame(JS::MutableHandleValue attribs)=0
Started the game with the given game attributes.
Replay log replayer.
Definition: Replay.h:105
Path OsPath
Definition: os_path.h:31
Simulation command, typically received over the network in multiplayer games.
Definition: SimulationCommand.h:27
virtual void Hash(const std::string &hash, bool quick)=0
Optional hash of simulation state (for sync checking).
static Status Load(const OsPath &pathname, void *buf, size_t size, const Parameters &p=Parameters(), const CompletedHook &completedHook=CompletedHook(), const IssueHook &issueHook=IssueHook())
Definition: io.h:347
OsPath m_Directory
Definition: Replay.h:99
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
IReplayLogger()
Definition: Replay.h:38
std::istream * m_Stream
Definition: Replay.h:115
virtual void Hash(const std::string &hash, bool quick)
Optional hash of simulation state (for sync checking).
Definition: Replay.h:75
virtual OsPath GetDirectory() const =0
Remember the directory containing the commands.txt file, so that we can save additional files to it...
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
Implementation of IReplayLogger that saves data to a file in the logs directory.
Definition: Replay.h:83
Implementation of IReplayLogger that simply throws away all data.
Definition: Replay.h:70
virtual void StartGame(JS::MutableHandleValue attribs)
Started the game with the given game attributes.
Definition: Replay.h:73