Ticket #3255: t3255_prevent_replay_overwrites_v6.patch
File t3255_prevent_replay_overwrites_v6.patch, 8.4 KB (added by , 8 years ago) |
---|
-
source/ps/Replay.cpp
28 28 #include "ps/Loader.h" 29 29 #include "ps/Mod.h" 30 30 #include "ps/Profile.h" 31 31 #include "ps/ProfileViewer.h" 32 32 #include "ps/Pyrogenesis.h" 33 #include "ps/Util.h" 34 #include "ps/VisualReplay.h" 33 35 #include "scriptinterface/ScriptInterface.h" 34 36 #include "scriptinterface/ScriptStats.h" 35 37 #include "simulation2/Simulation2.h" 36 38 #include "simulation2/helpers/SimulationCommand.h" 37 39 40 #include <ctime> 38 41 #include <sstream> 39 42 #include <fstream> 40 43 #include <iomanip> 41 44 42 #if MSC_VERSION43 #include <process.h>44 #define getpid _getpid // use the non-deprecated function name45 #endif46 47 45 static std::string Hexify(const std::string& s) 48 46 { 49 47 std::stringstream str; 50 48 str << std::hex; 51 49 for (size_t i = 0; i < s.size(); ++i) … … void CReplayLogger::StartGame(JS::Mutabl 70 68 71 69 // Add engine version and currently loaded mods for sanity checks when replaying 72 70 m_ScriptInterface.SetProperty(attribs, "engine_version", CStr(engine_version)); 73 71 m_ScriptInterface.SetProperty(attribs, "mods", g_modsLoaded); 74 72 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 static int run = -1; 79 do 80 { 81 std::wstringstream name; 82 name << getpid(); 83 if (++run) 84 name << "-" << run; 85 86 m_Directory = psLogDir() / L"sim_log" / name.str(); 87 } while (DirectoryExists(m_Directory)); 88 73 m_Directory = getDateIndexSubdirectory(VisualReplay::GetDirectoryName()); 74 debug_printf("Writing replay to %s\n", m_Directory.string8().c_str()); 89 75 CreateDirectories(m_Directory, 0700); 90 76 91 77 m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc); 92 78 *m_Stream << "start " << m_ScriptInterface.StringifyJSON(attribs, false) << "\n"; 93 79 } -
source/ps/Util.cpp
1 /* Copyright (C) 201 4Wildfire Games.1 /* Copyright (C) 2016 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * 4 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 5 * it under the terms of the GNU General Public License as published by 6 6 * the Free Software Foundation, either version 2 of the License, or … … Status tex_write(Tex* t, const VfsPath& 191 191 192 192 (void)da_free(&da); 193 193 return ret; 194 194 } 195 195 196 /** 197 * Return an unused directory, based on date and index (for example 2016-02-09_0001) 198 */ 199 OsPath getDateIndexSubdirectory(const OsPath& parentDir) 200 { 201 const std::time_t timestamp = std::time(nullptr); 202 const struct std::tm* now = std::localtime(×tamp); 203 204 int i = 0; 205 OsPath path; 206 char directory[256]; 207 208 do 209 { 210 sprintf(directory, "%04d-%02d-%02d_%04d", now->tm_year+1900, now->tm_mon+1, now->tm_mday, ++i); 211 path = parentDir / CStr(directory); 212 } while (DirectoryExists(path) || FileExists(path)); 213 214 return path; 215 } 196 216 197 217 static size_t s_nextScreenshotNumber; 198 218 199 219 // <extension> identifies the file format that is to be written 200 220 // (case-insensitive). examples: "bmp", "png", "jpg". -
source/ps/Util.h
1 /* Copyright (C) 20 09Wildfire Games.1 /* Copyright (C) 2016 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * 4 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 5 * it under the terms of the GNU General Public License as published by 6 6 * the Free Software Foundation, either version 2 of the License, or … … struct Tex; 24 24 25 25 extern void WriteSystemInfo(); 26 26 27 27 extern const wchar_t* ErrorString(int err); 28 28 29 extern OsPath getDateIndexSubdirectory(const OsPath& parentDir); 30 29 31 extern void WriteScreenshot(const VfsPath& extension); 30 32 extern void WriteBigScreenshot(const VfsPath& extension, int tiles); 31 33 32 34 extern Status tex_write(Tex* t, const VfsPath& filename); 33 35 -
source/ps/VisualReplay.cpp
1 /* Copyright (C) 201 5Wildfire Games.1 /* Copyright (C) 2016 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * 4 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 5 * it under the terms of the GNU General Public License as published by 6 6 * the Free Software Foundation, either version 2 of the License, or … … 25 25 #include "network/NetClient.h" 26 26 #include "network/NetServer.h" 27 27 #include "ps/CLogger.h" 28 28 #include "ps/Filesystem.h" 29 29 #include "ps/Game.h" 30 #include "ps/GameSetup/Paths.h" 31 #include "ps/Mod.h" 30 32 #include "ps/Pyrogenesis.h" 31 33 #include "ps/Replay.h" 34 #include "ps/Util.h" 32 35 #include "scriptinterface/ScriptInterface.h" 33 36 34 37 /** 35 38 * Filter too short replays (value in seconds). 36 39 */ 37 40 const u8 minimumReplayDuration = 3; 38 41 39 42 OsPath VisualReplay::GetDirectoryName() 40 43 { 41 return OsPath(psLogDir() / L"sim_log"); 44 const Paths paths(g_args); 45 return OsPath(paths.UserData() / "replays" / engine_version); 42 46 } 43 47 44 48 void VisualReplay::StartVisualReplay(const CStrW& directory) 45 49 { 46 50 ENSURE(!g_NetServer); -
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/Util.h" 44 45 #include "ps/XML/Xeromyces.h" 45 46 46 47 #include <iomanip> 47 48 48 #if MSC_VERSION49 #include <process.h>50 #define getpid _getpid // use the non-deprecated function name51 #endif52 53 49 static std::string Hexify(const std::string& s) // TODO: shouldn't duplicate this function in so many places 54 50 { 55 51 std::stringstream str; 56 52 str << std::hex; 57 53 for (size_t i = 0; i < s.size(); ++i) … … public: 77 73 if (CConfigDB::IsInitialised()) 78 74 { 79 75 CFG_GET_VAL("ooslog", m_EnableOOSLog); 80 76 CFG_GET_VAL("serializationtest", m_EnableSerializationTest); 81 77 } 78 79 m_OOSLogPath = getDateIndexSubdirectory(psLogDir() / "oos_logs"); 80 81 if (m_EnableOOSLog) 82 debug_printf("Writing ooslogs to %s\n", m_OOSLogPath.string8().c_str()); 82 83 } 83 84 84 85 ~CSimulation2Impl() 85 86 { 86 87 UnregisterFileReloadFunc(ReloadChangedFileCB, this); … … public: 130 131 std::set<VfsPath> m_LoadedScripts; 131 132 132 133 uint32_t m_TurnNumber; 133 134 134 135 bool m_EnableOOSLog; 135 136 OsPath m_OOSLogPath; 136 137 137 138 // Functions and data for the serialization test mode: (see Update() for relevant comments) 138 139 139 140 bool m_EnableSerializationTest; 140 141 … … void CSimulation2Impl::DumpSerialization 293 294 294 295 void CSimulation2Impl::ReportSerializationFailure( 295 296 SerializationTestState* primaryStateBefore, SerializationTestState* primaryStateAfter, 296 297 SerializationTestState* secondaryStateBefore, SerializationTestState* secondaryStateAfter) 297 298 { 298 OsPath path = psLogDir() / "oos_log"; 299 const OsPath path = getDateIndexSubdirectory(psLogDir() / "serializationtest"); 300 debug_printf("Writing serializationtest-data to %s\n", path.string8().c_str()); 299 301 CreateDirectories(path, 0700); 300 302 301 303 // Clean up obsolete files from previous runs 302 304 wunlink(path / "hash.before.a"); 303 305 wunlink(path / "hash.before.b"); … … void CSimulation2Impl::Interpolate(float 547 549 548 550 void CSimulation2Impl::DumpState() 549 551 { 550 552 PROFILE("DumpState"); 551 553 552 std::stringstream pid;553 pid << getpid();554 554 std::stringstream name;\ 555 555 name << std::setw(5) << std::setfill('0') << m_TurnNumber << ".txt"; 556 OsPath path = psLogDir() / "sim_log" / pid.str()/ name.str();556 const OsPath path = m_OOSLogPath / name.str(); 557 557 CreateDirectories(path.Parent(), 0700); 558 558 std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); 559 559 560 560 file << "State hash: " << std::hex; 561 561 std::string hashRaw; … … CSimulation2::~CSimulation2() 587 587 // Forward all method calls to the appropriate CSimulation2Impl/CComponentManager methods: 588 588 589 589 void CSimulation2::EnableOOSLog() 590 590 { 591 591 m->m_EnableOOSLog = true; 592 debug_printf("Writing ooslogs to %s\n", m->m_OOSLogPath.string8().c_str()); 592 593 } 593 594 594 595 void CSimulation2::EnableSerializationTest() 595 596 { 596 597 m->m_EnableSerializationTest = true;