Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Replay.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 #include "precompiled.h"
19 
20 #include "Replay.h"
21 
23 #include "lib/timer.h"
24 #include "lib/file/file_system.h"
25 #include "lib/res/h_mgr.h"
26 #include "lib/tex/tex.h"
27 #include "ps/Game.h"
28 #include "ps/Loader.h"
29 #include "ps/Profile.h"
30 #include "ps/ProfileViewer.h"
35 
36 #include <sstream>
37 #include <fstream>
38 #include <iomanip>
39 
40 #if MSC_VERSION
41 #include <process.h>
42 #define getpid _getpid // use the non-deprecated function name
43 #endif
44 
45 static std::string Hexify(const std::string& s)
46 {
47  std::stringstream str;
48  str << std::hex;
49  for (size_t i = 0; i < s.size(); ++i)
50  str << std::setfill('0') << std::setw(2) << (int)(unsigned char)s[i];
51  return str.str();
52 }
53 
55  m_ScriptInterface(scriptInterface)
56 {
57  // Construct the directory name based on the PID, to be relatively unique.
58  // Append "-1", "-2" etc if we run multiple matches in a single session,
59  // to avoid accidentally overwriting earlier logs.
60 
61  std::wstringstream name;
62  name << getpid();
63 
64  static int run = -1;
65  if (++run)
66  name << "-" << run;
67 
68  OsPath path = psLogDir() / L"sim_log" / name.str() / L"commands.txt";
69  CreateDirectories(path.Parent(), 0700);
70  m_Stream = new std::ofstream(OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc);
71 }
72 
74 {
75  delete m_Stream;
76 }
77 
79 {
80  *m_Stream << "start " << m_ScriptInterface.StringifyJSON(attribs.get(), false) << "\n";
81 }
82 
83 void CReplayLogger::Turn(u32 n, u32 turnLength, const std::vector<SimulationCommand>& commands)
84 {
85  *m_Stream << "turn " << n << " " << turnLength << "\n";
86  for (size_t i = 0; i < commands.size(); ++i)
87  {
88  *m_Stream << "cmd " << commands[i].player << " " << m_ScriptInterface.StringifyJSON(commands[i].data.get(), false) << "\n";
89  }
90  *m_Stream << "end\n";
91  m_Stream->flush();
92 }
93 
94 void CReplayLogger::Hash(const std::string& hash, bool quick)
95 {
96  if (quick)
97  *m_Stream << "hash-quick " << Hexify(hash) << "\n";
98  else
99  *m_Stream << "hash " << Hexify(hash) << "\n";
100 }
101 
102 ////////////////////////////////////////////////////////////////
103 
105  m_Stream(NULL)
106 {
107 }
108 
110 {
111  delete m_Stream;
112 }
113 
114 void CReplayPlayer::Load(const std::string& path)
115 {
116  ENSURE(!m_Stream);
117 
118  m_Stream = new std::ifstream(path.c_str());
119  ENSURE(m_Stream->good());
120 }
121 
123 {
124  ENSURE(m_Stream);
125 
126  new CProfileViewer;
127  new CProfileManager;
129  g_ProfileViewer.AddRootTable(g_ScriptStatsTable);
130 
131  CGame game(true);
132  g_Game = &game;
133 
134  // Need some stuff for terrain movement costs:
135  // (TODO: this ought to be independent of any graphics code)
138  g_TexMan.LoadTerrainTextures();
139 
140  // Initialise h_mgr so it doesn't crash when emitting sounds
141  h_mgr_init();
142 
143  std::vector<SimulationCommand> commands;
144  u32 turn = 0;
145  u32 turnLength = 0;
146 
147  std::string type;
148  while ((*m_Stream >> type).good())
149  {
150 // if (turn >= 1400) break;
151 
152  if (type == "start")
153  {
154  std::string line;
155  std::getline(*m_Stream, line);
157 
158  game.StartGame(attribs, "");
159 
160  // TODO: Non progressive load can fail - need a decent way to handle this
162 
163  PSRETURN ret = game.ReallyStartGame();
164  ENSURE(ret == PSRETURN_OK);
165  }
166  else if (type == "turn")
167  {
168  *m_Stream >> turn >> turnLength;
169  debug_printf(L"Turn %u (%u)... ", turn, turnLength);
170  }
171  else if (type == "cmd")
172  {
173  player_id_t player;
174  *m_Stream >> player;
175 
176  std::string line;
177  std::getline(*m_Stream, line);
179 
180  SimulationCommand cmd = { player, data };
181  commands.push_back(cmd);
182  }
183  else if (type == "hash" || type == "hash-quick")
184  {
185  std::string replayHash;
186  *m_Stream >> replayHash;
187 
188  bool quick = (type == "hash-quick");
189 
190 // if (turn >= 1300)
191 // if (turn >= 0)
192  if (turn % 100 == 0)
193  {
194  std::string hash;
195  bool ok = game.GetSimulation2()->ComputeStateHash(hash, quick);
196  ENSURE(ok);
197  std::string hexHash = Hexify(hash);
198  if (hexHash == replayHash)
199  debug_printf(L"hash ok (%hs)", hexHash.c_str());
200  else
201  debug_printf(L"HASH MISMATCH (%hs != %hs)", hexHash.c_str(), replayHash.c_str());
202  }
203  }
204  else if (type == "end")
205  {
206  {
208  PROFILE2("frame");
211 
212  game.GetSimulation2()->Update(turnLength, commands);
213  commands.clear();
214  }
215 
216 // std::string hash;
217 // bool ok = game.GetSimulation2()->ComputeStateHash(hash, true);
218 // ENSURE(ok);
219 // debug_printf(L"%hs", Hexify(hash).c_str());
220 
221  debug_printf(L"\n");
222 
223  g_Profiler.Frame();
224 
225 // if (turn % 1000 == 0)
226 // JS_GC(game.GetSimulation2()->GetScriptInterface().GetContext());
227 
228  if (turn % 20 == 0)
229  g_ProfileViewer.SaveToFile();
230  }
231  else
232  {
233  debug_printf(L"Unrecognised replay token %hs\n", type.c_str());
234  }
235  }
236 
238 
239  std::string hash;
240  bool ok = game.GetSimulation2()->ComputeStateHash(hash, false);
241  ENSURE(ok);
242  debug_printf(L"# Final state: %hs\n", Hexify(hash).c_str());
243 
245 
246  // Clean up
247  delete &g_TexMan;
249 
250  delete &g_Profiler;
251  delete &g_ProfileViewer;
252 
253  g_Game = NULL;
254 }
The container that holds the rules, resources and attributes of the game.
Definition: Game.h:39
virtual void StartGame(const CScriptValRooted &attribs)
Started the game with the given game attributes.
Definition: Replay.cpp:78
#define g_TexMan
CScriptStatsTable * g_ScriptStatsTable
Definition: ScriptStats.cpp:26
const OsPath & psLogDir()
Definition: Pyrogenesis.cpp:96
Status LDR_NonprogressiveLoad()
Definition: Loader.cpp:308
const PSRETURN PSRETURN_OK
Definition: Errors.h:103
void RecordFrameStart()
Call in main thread at the start of a frame.
Definition: Profiler2.h:304
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
void h_mgr_init()
Definition: h_mgr.cpp:811
~CReplayLogger()
Definition: Replay.cpp:73
Path Parent() const
Definition: path.h:150
std::ostream * m_Stream
Definition: Replay.h:78
void Replay()
Definition: Replay.cpp:122
Class CProfileViewer: Manage and display profiling tables.
virtual void Hash(const std::string &hash, bool quick)
Optional hash of simulation state (for sync checking).
Definition: Replay.cpp:94
std::string StringifyJSON(jsval obj, bool indent=true)
Stringify to a JSON string, UTF-8 encoded.
CScriptValRooted ParseJSON(const std::string &string_utf8)
Parse a UTF-8-encoded JSON string.
CReplayLogger(ScriptInterface &scriptInterface)
Definition: Replay.cpp:54
void Update(int turnLength)
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
void Load(const std::string &path)
Definition: Replay.cpp:114
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
#define PROFILE2(region)
Starts timing from now until the end of the current scope.
Definition: Profiler2.h:446
void IncrementFrameNumber()
Definition: Profiler2.h:367
#define g_Profiler
Definition: Profile.h:147
CProfiler2 g_Profiler2
Definition: Profiler2.cpp:35
u32 PSRETURN
Definition: Errors.h:75
Definition: path.h:75
void SaveToFile()
Call in any thread to save a JSONP representation of the buffers for all threads, to a file named pro...
Definition: Profiler2.cpp:540
Simulation command, typically received over the network in multiplayer games.
void StartGame(const CScriptValRooted &attribs, const std::string &savedState)
Definition: Game.cpp:261
ScriptInterface & GetScriptInterface() const
CGame * g_Game
Globally accessible pointer to the CGame object.
Definition: Game.cpp:56
bool ComputeStateHash(std::string &outHash, bool quick)
virtual void Turn(u32 n, u32 turnLength, const std::vector< SimulationCommand > &commands)
Run the given turn with the given collection of player commands.
Definition: Replay.cpp:83
CSimulation2 * GetSimulation2()
Get the pointer to the simulation2 object.
Definition: Game.h:136
#define PROFILE2_ATTR
Associates a string (with printf-style formatting) with the current region or event.
Definition: Profiler2.h:461
std::istream * m_Stream
Definition: Replay.h:94
ScriptInterface & m_ScriptInterface
Definition: Replay.h:77
#define u32
Definition: types.h:41
#define g_ProfileViewer
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Definition: ScriptVal.cpp:45
Abstraction around a SpiderMonkey JSContext.
void tex_codec_register_all()
Manually register codecs.
Definition: tex_codec.cpp:134
PSRETURN ReallyStartGame()
Game initialization has been completed.
Definition: Game.cpp:198
Status CreateDirectories(const OsPath &path, mode_t mode)
void tex_codec_unregister_all()
remove all codecs that have been registered.
Definition: tex_codec.cpp:56
static std::string Hexify(const std::string &s)
Definition: Replay.cpp:45
int GetFrameNumber()
Definition: Profiler2.h:362
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
Definition: debug.cpp:142
static std::string OsString(const OsPath &path)
Definition: os_path.h:42
void timer_DisplayClientTotals()
display all clients&#39; totals; does not reset them.
Definition: timer.cpp:181