2011-02-11 00:57:19 +01:00
|
|
|
/* Copyright (C) 2011 Wildfire Games.
|
2010-08-07 00:16:05 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "Replay.h"
|
|
|
|
|
2010-09-17 19:45:50 +02:00
|
|
|
#include "graphics/TerrainTextureManager.h"
|
2011-03-03 01:16:14 +01:00
|
|
|
#include "lib/timer.h"
|
2010-08-07 00:16:05 +02:00
|
|
|
#include "lib/file/file_system.h"
|
2011-10-27 23:22:41 +02:00
|
|
|
#include "lib/res/h_mgr.h"
|
2010-09-17 19:45:50 +02:00
|
|
|
#include "lib/tex/tex.h"
|
2010-08-07 00:16:05 +02:00
|
|
|
#include "ps/Game.h"
|
|
|
|
#include "ps/Loader.h"
|
2010-09-17 19:45:50 +02:00
|
|
|
#include "ps/Profile.h"
|
|
|
|
#include "ps/ProfileViewer.h"
|
2010-08-07 00:16:05 +02:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2010-09-17 19:45:50 +02:00
|
|
|
#include "scriptinterface/ScriptStats.h"
|
2010-08-07 00:16:05 +02:00
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
#include "simulation2/helpers/SimulationCommand.h"
|
|
|
|
|
2010-08-10 21:48:35 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2010-08-08 22:11:57 +02:00
|
|
|
#if MSC_VERSION
|
|
|
|
#include <process.h>
|
|
|
|
#define getpid _getpid // use the non-deprecated function name
|
|
|
|
#endif
|
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
static std::string Hexify(const std::string& s)
|
|
|
|
{
|
|
|
|
std::stringstream str;
|
|
|
|
str << std::hex;
|
|
|
|
for (size_t i = 0; i < s.size(); ++i)
|
|
|
|
str << std::setfill('0') << std::setw(2) << (int)(unsigned char)s[i];
|
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
CReplayLogger::CReplayLogger(ScriptInterface& scriptInterface) :
|
|
|
|
m_ScriptInterface(scriptInterface)
|
|
|
|
{
|
2011-02-11 00:57:19 +01:00
|
|
|
// Construct the directory name based on the PID, to be relatively unique.
|
|
|
|
// Append "-1", "-2" etc if we run multiple matches in a single session,
|
|
|
|
// to avoid accidentally overwriting earlier logs.
|
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
std::wstringstream name;
|
2011-05-04 14:16:51 +02:00
|
|
|
name << getpid();
|
2011-02-11 00:57:19 +01:00
|
|
|
|
|
|
|
static int run = -1;
|
|
|
|
if (++run)
|
|
|
|
name << "-" << run;
|
|
|
|
|
2011-05-04 14:16:51 +02:00
|
|
|
OsPath path = psLogDir() / L"sim_log" / name.str() / L"commands.txt";
|
2011-03-23 14:36:20 +01:00
|
|
|
CreateDirectories(path.Parent(), 0700);
|
|
|
|
m_Stream = new std::ofstream(OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc);
|
2010-08-07 00:16:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CReplayLogger::~CReplayLogger()
|
|
|
|
{
|
|
|
|
delete m_Stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CReplayLogger::StartGame(const CScriptValRooted& attribs)
|
|
|
|
{
|
|
|
|
*m_Stream << "start " << m_ScriptInterface.StringifyJSON(attribs.get(), false) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CReplayLogger::Turn(u32 n, u32 turnLength, const std::vector<SimulationCommand>& commands)
|
|
|
|
{
|
|
|
|
*m_Stream << "turn " << n << " " << turnLength << "\n";
|
|
|
|
for (size_t i = 0; i < commands.size(); ++i)
|
|
|
|
{
|
|
|
|
*m_Stream << "cmd " << commands[i].player << " " << m_ScriptInterface.StringifyJSON(commands[i].data.get(), false) << "\n";
|
|
|
|
}
|
|
|
|
*m_Stream << "end\n";
|
|
|
|
m_Stream->flush();
|
|
|
|
}
|
|
|
|
|
2011-03-05 23:30:32 +01:00
|
|
|
void CReplayLogger::Hash(const std::string& hash, bool quick)
|
2010-08-07 00:16:05 +02:00
|
|
|
{
|
2011-03-05 23:30:32 +01:00
|
|
|
if (quick)
|
|
|
|
*m_Stream << "hash-quick " << Hexify(hash) << "\n";
|
|
|
|
else
|
|
|
|
*m_Stream << "hash " << Hexify(hash) << "\n";
|
2010-08-07 00:16:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CReplayPlayer::CReplayPlayer() :
|
|
|
|
m_Stream(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CReplayPlayer::~CReplayPlayer()
|
|
|
|
{
|
|
|
|
delete m_Stream;
|
|
|
|
}
|
|
|
|
|
2011-03-21 18:53:13 +01:00
|
|
|
void CReplayPlayer::Load(const std::string& path)
|
2010-08-07 00:16:05 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(!m_Stream);
|
2010-08-07 00:16:05 +02:00
|
|
|
|
2011-03-21 18:53:13 +01:00
|
|
|
m_Stream = new std::ifstream(path.c_str());
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(m_Stream->good());
|
2010-08-07 00:16:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CReplayPlayer::Replay()
|
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(m_Stream);
|
2010-08-07 00:16:05 +02:00
|
|
|
|
2010-09-17 19:45:50 +02:00
|
|
|
new CProfileViewer;
|
|
|
|
new CProfileManager;
|
|
|
|
g_ScriptStatsTable = new CScriptStatsTable;
|
|
|
|
g_ProfileViewer.AddRootTable(g_ScriptStatsTable);
|
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
CGame game(true);
|
|
|
|
g_Game = &game;
|
|
|
|
|
2010-09-17 19:45:50 +02:00
|
|
|
// Need some stuff for terrain movement costs:
|
|
|
|
// (TODO: this ought to be independent of any graphics code)
|
|
|
|
tex_codec_register_all();
|
|
|
|
new CTerrainTextureManager;
|
|
|
|
g_TexMan.LoadTerrainTextures();
|
|
|
|
|
2011-10-27 23:22:41 +02:00
|
|
|
// Initialise h_mgr so it doesn't crash when emitting sounds
|
|
|
|
h_mgr_init();
|
2010-09-17 19:45:50 +02:00
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
std::vector<SimulationCommand> commands;
|
2010-09-17 19:45:50 +02:00
|
|
|
u32 turn = 0;
|
2010-08-09 11:49:24 +02:00
|
|
|
u32 turnLength = 0;
|
2010-08-07 00:16:05 +02:00
|
|
|
|
|
|
|
std::string type;
|
|
|
|
while ((*m_Stream >> type).good())
|
|
|
|
{
|
2010-09-17 19:45:50 +02:00
|
|
|
// if (turn >= 1400) break;
|
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
if (type == "start")
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
std::getline(*m_Stream, line);
|
2010-10-31 23:00:28 +01:00
|
|
|
CScriptValRooted attribs = game.GetSimulation2()->GetScriptInterface().ParseJSON(line);
|
2010-08-07 00:16:05 +02:00
|
|
|
|
2011-10-30 02:07:28 +02:00
|
|
|
game.StartGame(attribs, "");
|
2011-04-07 04:32:16 +02:00
|
|
|
|
|
|
|
// TODO: Non progressive load can fail - need a decent way to handle this
|
2010-08-07 00:16:05 +02:00
|
|
|
LDR_NonprogressiveLoad();
|
2011-04-07 04:32:16 +02:00
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
PSRETURN ret = game.ReallyStartGame();
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(ret == PSRETURN_OK);
|
2010-08-07 00:16:05 +02:00
|
|
|
}
|
|
|
|
else if (type == "turn")
|
|
|
|
{
|
|
|
|
*m_Stream >> turn >> turnLength;
|
2010-09-17 19:45:50 +02:00
|
|
|
debug_printf(L"Turn %d (%d)... ", turn, turnLength);
|
2010-08-07 00:16:05 +02:00
|
|
|
}
|
|
|
|
else if (type == "cmd")
|
|
|
|
{
|
|
|
|
u32 player;
|
|
|
|
*m_Stream >> player;
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
std::getline(*m_Stream, line);
|
2010-10-31 23:00:28 +01:00
|
|
|
CScriptValRooted data = game.GetSimulation2()->GetScriptInterface().ParseJSON(line);
|
2010-08-07 00:16:05 +02:00
|
|
|
|
|
|
|
SimulationCommand cmd = { player, data };
|
|
|
|
commands.push_back(cmd);
|
|
|
|
}
|
2011-03-05 23:30:32 +01:00
|
|
|
else if (type == "hash" || type == "hash-quick")
|
2010-09-17 19:45:50 +02:00
|
|
|
{
|
|
|
|
std::string replayHash;
|
|
|
|
*m_Stream >> replayHash;
|
|
|
|
|
2011-03-05 23:30:32 +01:00
|
|
|
bool quick = (type == "hash-quick");
|
|
|
|
|
2010-09-17 19:45:50 +02:00
|
|
|
// if (turn >= 1300)
|
|
|
|
// if (turn >= 0)
|
|
|
|
if (turn % 100 == 0)
|
|
|
|
{
|
|
|
|
std::string hash;
|
2011-03-05 23:30:32 +01:00
|
|
|
bool ok = game.GetSimulation2()->ComputeStateHash(hash, quick);
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(ok);
|
2010-09-17 19:45:50 +02:00
|
|
|
std::string hexHash = Hexify(hash);
|
|
|
|
if (hexHash == replayHash)
|
|
|
|
debug_printf(L"hash ok (%hs)", hexHash.c_str());
|
|
|
|
else
|
|
|
|
debug_printf(L"HASH MISMATCH (%hs != %hs)", hexHash.c_str(), replayHash.c_str());
|
|
|
|
}
|
|
|
|
}
|
2010-08-07 00:16:05 +02:00
|
|
|
else if (type == "end")
|
|
|
|
{
|
2011-11-29 21:32:43 +01:00
|
|
|
{
|
|
|
|
g_Profiler2.RecordFrameStart();
|
|
|
|
PROFILE2("frame");
|
|
|
|
g_Profiler2.IncrementFrameNumber();
|
|
|
|
PROFILE2_ATTR("%d", g_Profiler2.GetFrameNumber());
|
|
|
|
|
|
|
|
game.GetSimulation2()->Update(turnLength, commands);
|
|
|
|
commands.clear();
|
|
|
|
}
|
2010-08-07 00:16:05 +02:00
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
// std::string hash;
|
2011-03-05 23:30:32 +01:00
|
|
|
// bool ok = game.GetSimulation2()->ComputeStateHash(hash, true);
|
2011-04-30 15:01:45 +02:00
|
|
|
// ENSURE(ok);
|
2011-01-16 00:35:20 +01:00
|
|
|
// debug_printf(L"%hs", Hexify(hash).c_str());
|
2010-09-17 19:45:50 +02:00
|
|
|
|
|
|
|
debug_printf(L"\n");
|
|
|
|
|
|
|
|
g_Profiler.Frame();
|
|
|
|
|
|
|
|
// if (turn % 1000 == 0)
|
|
|
|
// JS_GC(game.GetSimulation2()->GetScriptInterface().GetContext());
|
|
|
|
|
|
|
|
if (turn % 20 == 0)
|
|
|
|
g_ProfileViewer.SaveToFile();
|
2010-08-07 00:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-17 19:45:50 +02:00
|
|
|
debug_printf(L"Unrecognised replay token %hs\n", type.c_str());
|
2010-08-07 00:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-03 11:55:14 +02:00
|
|
|
|
2011-11-29 21:32:43 +01:00
|
|
|
g_Profiler2.SaveToFile();
|
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
std::string hash;
|
2011-03-05 23:30:32 +01:00
|
|
|
bool ok = game.GetSimulation2()->ComputeStateHash(hash, false);
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(ok);
|
2010-09-17 19:45:50 +02:00
|
|
|
debug_printf(L"# Final state: %hs\n", Hexify(hash).c_str());
|
|
|
|
|
2011-03-03 01:16:14 +01:00
|
|
|
timer_DisplayClientTotals();
|
|
|
|
|
2010-09-17 19:45:50 +02:00
|
|
|
// Clean up
|
|
|
|
delete &g_TexMan;
|
|
|
|
tex_codec_unregister_all();
|
|
|
|
|
|
|
|
delete &g_Profiler;
|
|
|
|
delete &g_ProfileViewer;
|
2010-08-07 00:16:05 +02:00
|
|
|
}
|