Ticket #3255: t3255_prevent_replay_overwrites_v3.patch
File t3255_prevent_replay_overwrites_v3.patch, 3.5 KB (added by , 9 years ago) |
---|
-
source/ps/Replay.cpp
33 33 #include "scriptinterface/ScriptInterface.h" 34 34 #include "scriptinterface/ScriptStats.h" 35 35 #include "simulation2/Simulation2.h" 36 36 #include "simulation2/helpers/SimulationCommand.h" 37 37 38 #include <ctime> 38 39 #include <sstream> 39 40 #include <fstream> 40 41 #include <iomanip> 41 42 42 #if MSC_VERSION43 #include <process.h>44 #define getpid _getpid // use the non-deprecated function name45 #endif46 47 43 static std::string Hexify(const std::string& s) 48 44 { 49 45 std::stringstream str; 50 46 str << std::hex; 51 47 for (size_t i = 0; i < s.size(); ++i) … … CReplayLogger::~CReplayLogger() 63 59 delete m_Stream; 64 60 } 65 61 66 62 void CReplayLogger::StartGame(JS::MutableHandleValue attribs) 67 63 { 68 // Add timestamp , since the file-modification-date can change64 // Add timestamp to the header, since the file-modification-date can change 69 65 m_ScriptInterface.SetProperty(attribs, "timestamp", std::to_string(std::time(nullptr))); 70 66 71 // Add engine version and currently loaded mods for sanity checkswhen replaying67 // Add engine version and currently loaded mods to the header for a compatibility check when replaying 72 68 m_ScriptInterface.SetProperty(attribs, "engine_version", CStr(engine_version)); 73 69 m_ScriptInterface.SetProperty(attribs, "mods", g_modsLoaded); 74 70 75 // Construct the directory name based on the PID, to be relatively unique. 76 // Append "-1", "-2" etc if we run multiple matches in a single session, 77 // to avoid accidentally overwriting earlier logs. 78 std::wstringstream name; 79 name << getpid(); 80 81 static int run = -1; 82 if (++run) 83 name << "-" << run; 71 // Get current date 72 const time_t t = time(nullptr); 73 const struct tm* now = localtime(&t); 74 75 // Construct directory name based on date and sequential number per date 76 const CStr subDirectoryFormat = "%04d-%02d-%02d_replay_%03d"; 77 char subDirectory[subDirectoryFormat.length()]; 78 OsPath fullPath; 79 int i = 1; 80 do { 81 sprintf(subDirectory, subDirectoryFormat.c_str(), 1900+now->tm_year, 1+now->tm_mon, now->tm_mday, i++); 82 fullPath = psLogDir() / L"sim_log" / wstring_from_utf8(CStr(subDirectory)); 83 } while (DirectoryExists(fullPath) || FileExists(fullPath)); 84 84 85 m_Directory = psLogDir() / L"sim_log" / name.str(); 85 // Create directory 86 m_Directory = fullPath; 86 87 CreateDirectories(m_Directory, 0700); 87 88 89 // Open commands.txt 88 90 m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc); 91 92 // Write header 89 93 *m_Stream << "start " << m_ScriptInterface.StringifyJSON(attribs, false) << "\n"; 90 94 } 91 95 92 96 void CReplayLogger::Turn(u32 n, u32 turnLength, std::vector<SimulationCommand>& commands) 93 97 { -
source/simulation2/Simulation2.cpp
void CSimulation2Impl::DumpState() 553 553 554 554 std::stringstream pid; 555 555 pid << getpid(); 556 556 std::stringstream name;\ 557 557 name << std::setw(5) << std::setfill('0') << m_TurnNumber << ".txt"; 558 OsPath path = psLogDir() / " sim_log" / pid.str()/ name.str();558 OsPath path = psLogDir() / "oos_log" / name.str(); 559 559 CreateDirectories(path.Parent(), 0700); 560 560 std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); 561 561 562 562 file << "State hash: " << std::hex; 563 563 std::string hashRaw;