Ticket #3255: t3255_prevent_replay_overwrites_v4.patch
File t3255_prevent_replay_overwrites_v4.patch, 4.4 KB (added by , 8 years ago) |
---|
-
source/ps/Replay.cpp
22 22 #include "graphics/TerrainTextureManager.h" 23 23 #include "lib/timer.h" 24 24 #include "lib/file/file_system.h" 25 25 #include "lib/res/h_mgr.h" 26 26 #include "lib/tex/tex.h" 27 #include "ps/CLogger.h" 27 28 #include "ps/Game.h" 28 29 #include "ps/Loader.h" 29 30 #include "ps/Mod.h" 30 31 #include "ps/Profile.h" 31 32 #include "ps/ProfileViewer.h" 32 #include "ps/ Pyrogenesis.h"33 #include "ps/VisualReplay.h" 33 34 #include "scriptinterface/ScriptInterface.h" 34 35 #include "scriptinterface/ScriptStats.h" 35 36 #include "simulation2/Simulation2.h" 36 37 #include "simulation2/helpers/SimulationCommand.h" 37 38 39 #include <ctime> 38 40 #include <sstream> 39 41 #include <fstream> 40 42 #include <iomanip> 41 43 42 #if MSC_VERSION43 #include <process.h>44 #define getpid _getpid // use the non-deprecated function name45 #endif46 47 44 static std::string Hexify(const std::string& s) 48 45 { 49 46 std::stringstream str; 50 47 str << std::hex; 51 48 for (size_t i = 0; i < s.size(); ++i) … … CReplayLogger::~CReplayLogger() 63 60 delete m_Stream; 64 61 } 65 62 66 63 void CReplayLogger::StartGame(JS::MutableHandleValue attribs) 67 64 { 65 const time_t timestamp = std::time(nullptr); 66 68 67 // Add timestamp, since the file-modification-date can change 69 m_ScriptInterface.SetProperty(attribs, "timestamp", std::to_string( std::time(nullptr)));68 m_ScriptInterface.SetProperty(attribs, "timestamp", std::to_string(timestamp)); 70 69 71 70 // Add engine version and currently loaded mods for sanity checks when replaying 72 71 m_ScriptInterface.SetProperty(attribs, "engine_version", CStr(engine_version)); 73 72 m_ScriptInterface.SetProperty(attribs, "mods", g_modsLoaded); 74 73 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; 74 // Construct directory name (for example 2015-11-02_001) 75 const struct tm* now = localtime(×tamp); 76 const CStr directoryFormat = "%04d-%02d-%02d_%03d"; 77 char directory[directoryFormat.length()]; 78 for (int i=1; i<=1000; ++i) 79 { 80 sprintf(directory, directoryFormat.c_str(), now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, i); 81 82 m_Directory = VisualReplay::GetDirectoryName() / wstring_from_utf8(CStr(directory)); 83 84 if (!DirectoryExists(m_Directory) && !FileExists(m_Directory)) 85 break; 86 87 if (i==1000) 88 LOGERROR("Overwriting %s", m_Directory.string8()); 89 } 84 90 85 m_Directory = psLogDir() / L"sim_log" / name.str();86 91 CreateDirectories(m_Directory, 0700); 87 92 88 93 m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc); 89 94 *m_Stream << "start " << m_ScriptInterface.StringifyJSON(attribs, false) << "\n"; 90 95 } -
source/ps/VisualReplay.cpp
const u8 minimumReplayDuration = 3; 39 39 */ 40 40 const bool debugParser = false; 41 41 42 42 OsPath VisualReplay::GetDirectoryName() 43 43 { 44 return OsPath(psLogDir() / L"sim_log");44 return OsPath(psLogDir() / (L"replays_v" + wstring_from_utf8(engine_version))); 45 45 } 46 46 47 47 JS::Value VisualReplay::GetReplays(ScriptInterface& scriptInterface) 48 48 { 49 49 TIMER(L"GetReplays"); -
source/simulation2/Simulation2.cpp
39 39 #include "ps/ConfigDB.h" 40 40 #include "ps/Filesystem.h" 41 41 #include "ps/Loader.h" 42 42 #include "ps/Profile.h" 43 43 #include "ps/Pyrogenesis.h" 44 #include "ps/VisualReplay.h" 44 45 #include "ps/XML/Xeromyces.h" 45 46 46 47 #include <iomanip> 47 48 48 49 #if MSC_VERSION … … void CSimulation2Impl::DumpState() 553 554 554 555 std::stringstream pid; 555 556 pid << getpid(); 556 557 std::stringstream name;\ 557 558 name << std::setw(5) << std::setfill('0') << m_TurnNumber << ".txt"; 558 OsPath path = psLogDir() / "sim_log" / pid.str()/ name.str();559 OsPath path = VisualReplay::GetDirectoryName() / "oos_log" / name.str(); 559 560 CreateDirectories(path.Parent(), 0700); 560 561 std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); 561 562 562 563 file << "State hash: " << std::hex; 563 564 std::string hashRaw;