Line data Source code
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"
23 : #include "scriptinterface/ScriptTypes.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 : */
35 : class IReplayLogger
36 : {
37 : public:
38 0 : IReplayLogger() { }
39 0 : 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 : */
70 0 : class CDummyReplayLogger : public IReplayLogger
71 : {
72 : public:
73 0 : virtual void StartGame(JS::MutableHandleValue UNUSED(attribs)) { }
74 0 : virtual void Turn(u32 UNUSED(n), u32 UNUSED(turnLength), std::vector<SimulationCommand>& UNUSED(commands)) { }
75 0 : virtual void Hash(const std::string& UNUSED(hash), bool UNUSED(quick)) { }
76 0 : virtual void SaveMetadata(const CSimulation2& UNUSED(simulation)) { };
77 0 : virtual OsPath GetDirectory() const { return OsPath(); }
78 : };
79 :
80 : /**
81 : * Implementation of IReplayLogger that saves data to a file in the logs directory.
82 : */
83 : class CReplayLogger : public IReplayLogger
84 : {
85 : NONCOPYABLE(CReplayLogger);
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:
97 : const ScriptInterface& m_ScriptInterface;
98 : std::ostream* m_Stream;
99 : OsPath m_Directory;
100 : };
101 :
102 : /**
103 : * Replay log replayer. Runs the log with no graphics and dumps some info to stdout.
104 : */
105 : class CReplayPlayer
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
|