1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "DemoRecorder.h"
#include "System/FileSystem/DataDirsAccess.h"
#include "System/FileSystem/FileSystem.h"
#include "System/FileSystem/FileQueryFlags.h"
#include "System/FileSystem/FileHandler.h"
#include "Game/GameVersion.h"
#include "Sim/Misc/TeamStatistics.h"
#include "System/Util.h"
#include "System/TimeUtil.h"
#include "System/Log/ILog.h"
#include <cassert>
#include <cerrno>
#include <cstring>
CDemoRecorder::CDemoRecorder(const std::string& mapName, const std::string& modName, bool serverDemo):
demoStream(std::ios::binary | std::ios::out)
{
SetName(mapName, modName, serverDemo);
SetFileHeader();
file = gzopen(demoName.c_str(), "wb9");
}
CDemoRecorder::~CDemoRecorder()
{
LOG("Writing demo: %s", GetName().c_str());
WriteWinnerList();
WritePlayerStats();
WriteTeamStats();
WriteFileHeader(true);
WriteDemoFile();
}
void CDemoRecorder::SetFileHeader()
{
memset(&fileHeader, 0, sizeof(DemoFileHeader));
strcpy(fileHeader.magic, DEMOFILE_MAGIC);
fileHeader.version = DEMOFILE_VERSION;
fileHeader.headerSize = sizeof(DemoFileHeader);
STRNCPY(fileHeader.versionString, (SpringVersion::GetSync()).c_str(), sizeof(fileHeader.versionString) - 1);
fileHeader.unixTime = CTimeUtil::GetCurrentTime();
fileHeader.playerStatElemSize = sizeof(PlayerStatistics);
fileHeader.teamStatElemSize = sizeof(TeamStatistics);
fileHeader.teamStatPeriod = TeamStatistics::statsPeriod;
fileHeader.winningAllyTeamsSize = 0;
demoStream.seekp(WriteFileHeader(false) + sizeof(DemoFileHeader));
}
void CDemoRecorder::WriteDemoFile()
{
// using operator<<(basic_stringbuf*) requires the stream to be opened with std::ios::in
// stringbuf::{eback(), egptr(), gptr()} are protected so we cannot access them directly
// (plus data is not guaranteed to be stored contiguously) ==> the only clean OO solution
// that avoids str()'s copy would be to supply our own stringbuffer backend to demoStream
// which is slightly overdoing it
const std::string data = demoStream.str();
gzwrite(file, data.c_str(), data.size());
gzflush(file, Z_FINISH);
gzclose(file);
}
void CDemoRecorder::WriteSetupText(const std::string& text)
{
int length = text.length();
while (text[length - 1] == '\0') {
--length;
}
fileHeader.scriptSize = length;
demoStream.write(text.c_str(), length);
}
void CDemoRecorder::SaveToDemo(const unsigned char* buf, const unsigned length, const float modGameTime)
{
DemoStreamChunkHeader chunkHeader;
chunkHeader.modGameTime = modGameTime;
chunkHeader.length = length;
chunkHeader.swab();
demoStream.write((char*) &chunkHeader, sizeof(chunkHeader));
demoStream.write((char*) buf, length);
fileHeader.demoStreamSize += length + sizeof(chunkHeader);
}
void CDemoRecorder::SetName(const std::string& mapName, const std::string& modName, bool serverDemo)
{
// Returns the current local time as "JJJJMMDD_HHmmSS", eg: "20091231_115959"
const std::string curTime = CTimeUtil::GetCurrentTimeStr();
const std::string demoDir = serverDemo? "demos-server/": "demos/";
// We want this folder to exist
if (!FileSystem::CreateDirectory(demoDir))
return;
std::ostringstream oss;
std::ostringstream buf;
oss << demoDir << curTime << "_";
oss << FileSystem::GetBasename(mapName);
oss << "_";
// FIXME: why is this not included?
// oss << FileSystem::GetBasename(modName);
// oss << "_";
oss << SpringVersion::GetSync();
buf << oss.str() << ".sdfz";
int n = 0;
while (FileSystem::FileExists(buf.str()) && (n < 99)) {
buf.str(""); // clears content
buf << oss.str() << "_" << n++ << ".sdfz";
}
demoName = dataDirsAccess.LocateFile(buf.str(), FileQueryFlags::WRITE);
}
void CDemoRecorder::SetGameID(const unsigned char* buf)
{
memcpy(&fileHeader.gameID, buf, sizeof(fileHeader.gameID));
WriteFileHeader(false);
}
void CDemoRecorder::SetTime(int gameTime, int wallclockTime)
{
fileHeader.gameTime = gameTime;
fileHeader.wallclockTime = wallclockTime;
}
void CDemoRecorder::InitializeStats(int numPlayers, int numTeams)
{
playerStats.resize(numPlayers);
teamStats.resize(numTeams);
}
void CDemoRecorder::AddNewPlayer(const std::string& name, int playerNum)
{
if (playerNum >= playerStats.size()) {
playerStats.resize(playerNum + 1);
}
}
/** @brief Set (overwrite) the CPlayer::Statistics for player playerNum */
void CDemoRecorder::SetPlayerStats(int playerNum, const PlayerStatistics& stats)
{
if (playerNum >= playerStats.size()) {
playerStats.resize(playerNum + 1);
}
playerStats[playerNum] = stats;
}
/** @brief Set (overwrite) the TeamStatistics history for team teamNum */
void CDemoRecorder::SetTeamStats(int teamNum, const std::list< TeamStatistics >& stats)
{
assert((unsigned)teamNum < teamStats.size()); //FIXME
teamStats[teamNum].clear();
teamStats[teamNum].reserve(stats.size());
for (std::list< TeamStatistics >::const_iterator it = stats.begin(); it != stats.end(); ++it)
teamStats[teamNum].push_back(*it);
}
/** @brief Set (overwrite) the list of winning allyTeams */
void CDemoRecorder::SetWinningAllyTeams(const std::vector<unsigned char>& winningAllyTeamIDs)
{
fileHeader.winningAllyTeamsSize = (winningAllyTeamIDs.size() * sizeof(unsigned char));
winningAllyTeams = winningAllyTeamIDs;
}
/** @brief Write DemoFileHeader
Write the DemoFileHeader at the start of the file and restores the original
position in the file afterwards. */
unsigned int CDemoRecorder::WriteFileHeader(bool updateStreamLength)
{
#ifdef _MSC_VER // MSVC8 behaves strange if tell/seek is called before anything has been written
const bool empty = (demoStream.str() == "");
const unsigned int pos = empty? 0 : demoStream.tellp();
#else
const unsigned int pos = demoStream.tellp();
#endif
DemoFileHeader tmpHeader;
memcpy(&tmpHeader, &fileHeader, sizeof(fileHeader));
if (!updateStreamLength)
tmpHeader.demoStreamSize = 0;
tmpHeader.swab(); // to little endian
#ifdef _MSC_VER
if (!empty)
#endif
{
demoStream.seekp(0);
}
demoStream.write((char*) &tmpHeader, sizeof(tmpHeader));
demoStream.seekp(pos);
return pos;
}
/** @brief Write the CPlayer::Statistics at the current position in the file. */
void CDemoRecorder::WritePlayerStats()
{
int pos = demoStream.tellp();
for (PlayerStatistics& stats: playerStats) {
stats.swab();
demoStream.write(reinterpret_cast<char*>(&stats), sizeof(PlayerStatistics));
}
fileHeader.numPlayers = playerStats.size();
fileHeader.playerStatSize = (int)demoStream.tellp() - pos;
playerStats.clear();
}
/** @brief Write the winningAllyTeams at the current position in the file. */
void CDemoRecorder::WriteWinnerList()
{
if (fileHeader.numTeams == 0)
return;
const int pos = demoStream.tellp();
// Write the array of winningAllyTeams.
for (std::vector<unsigned char>::const_iterator it = winningAllyTeams.begin(); it != winningAllyTeams.end(); ++it) {
demoStream.write((char*) &(*it), sizeof(unsigned char));
}
winningAllyTeams.clear();
fileHeader.winningAllyTeamsSize = int(demoStream.tellp()) - pos;
}
/** @brief Write the TeamStatistics at the current position in the file. */
void CDemoRecorder::WriteTeamStats()
{
int pos = demoStream.tellp();
// Write array of dwords indicating number of TeamStatistics per team.
for (std::vector<TeamStatistics>& history: teamStats) {
unsigned int c = swabDWord(history.size());
demoStream.write((char*)&c, sizeof(unsigned int));
}
// Write big array of TeamStatistics.
for (std::vector<TeamStatistics>& history: teamStats) {
for (TeamStatistics& stats: history) {
stats.swab();
demoStream.write(reinterpret_cast<char*>(&stats), sizeof(TeamStatistics));
}
}
fileHeader.numTeams = teamStats.size();
fileHeader.teamStatSize = (int)demoStream.tellp() - pos;
teamStats.clear();
}
|