Ticket #3258: t3258_remember_replay_directory_and_save_metadata_v1.patch
File t3258_remember_replay_directory_and_save_metadata_v1.patch, 3.9 KB (added by , 9 years ago) |
---|
-
source/ps/Replay.cpp
24 24 #include "lib/file/file_system.h" 25 25 #include "lib/res/h_mgr.h" 26 26 #include "lib/tex/tex.h" 27 27 #include "ps/Game.h" 28 28 #include "ps/Loader.h" 29 #include "ps/Mod.h" 29 30 #include "ps/Profile.h" 30 31 #include "ps/ProfileViewer.h" 32 #include "ps/Pyrogenesis.h" 31 33 #include "scriptinterface/ScriptInterface.h" 32 34 #include "scriptinterface/ScriptStats.h" 33 35 #include "simulation2/Simulation2.h" 34 36 #include "simulation2/helpers/SimulationCommand.h" 35 37 … … CReplayLogger::~CReplayLogger() 61 63 delete m_Stream; 62 64 } 63 65 64 66 void CReplayLogger::StartGame(JS::MutableHandleValue attribs) 65 67 { 68 // Add timestamp, since the file-modification-date can change 69 m_ScriptInterface.SetProperty(attribs, "timestamp", std::time(nullptr)); 70 71 // Add engine version and currently loaded mods for sanity checks when replaying 72 m_ScriptInterface.SetProperty(attribs, "engine_version", CStr(engine_version)); 73 m_ScriptInterface.SetProperty(attribs, "mods", g_modsLoaded); 74 66 75 // Construct the directory name based on the PID, to be relatively unique. 67 76 // Append "-1", "-2" etc if we run multiple matches in a single session, 68 77 // to avoid accidentally overwriting earlier logs. 69 70 78 std::wstringstream name; 71 79 name << getpid(); 72 80 73 81 static int run = -1; 74 82 if (++run) 75 83 name << "-" << run; 76 84 77 OsPath path = psLogDir() / L"sim_log" / name.str() / L"commands.txt"; 78 CreateDirectories(path.Parent(), 0700); 79 m_Stream = new std::ofstream(OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); 85 m_Directory = psLogDir() / L"sim_log" / name.str(); 86 CreateDirectories(m_Directory, 0700); 80 87 88 m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc); 81 89 *m_Stream << "start " << m_ScriptInterface.StringifyJSON(attribs, false) << "\n"; 82 90 } 83 91 84 92 void CReplayLogger::Turn(u32 n, u32 turnLength, std::vector<SimulationCommand>& commands) 85 93 { … … void CReplayLogger::Hash(const std::stri 101 109 *m_Stream << "hash-quick " << Hexify(hash) << "\n"; 102 110 else 103 111 *m_Stream << "hash " << Hexify(hash) << "\n"; 104 112 } 105 113 114 OsPath CReplayLogger::GetDirectory() const 115 { 116 return m_Directory; 117 } 118 106 119 //////////////////////////////////////////////////////////////// 107 120 108 121 CReplayPlayer::CReplayPlayer() : 109 122 m_Stream(NULL) 110 123 { -
source/ps/Replay.h
public: 45 45 46 46 /** 47 47 * Optional hash of simulation state (for sync checking). 48 48 */ 49 49 virtual void Hash(const std::string& hash, bool quick) = 0; 50 51 /** 52 * Remember the directory containing the commands.txt file, so that we can save additional files to it. 53 */ 54 virtual OsPath GetDirectory() const = 0; 50 55 }; 51 56 52 57 /** 53 58 * Implementation of IReplayLogger that simply throws away all data. 54 59 */ … … class CDummyReplayLogger : public IRepla 56 61 { 57 62 public: 58 63 virtual void StartGame(JS::MutableHandleValue UNUSED(attribs)) { } 59 64 virtual void Turn(u32 UNUSED(n), u32 UNUSED(turnLength), std::vector<SimulationCommand>& UNUSED(commands)) { } 60 65 virtual void Hash(const std::string& UNUSED(hash), bool UNUSED(quick)) { } 66 virtual OsPath GetDirectory() const { return OsPath(); } 61 67 }; 62 68 63 69 /** 64 70 * Implementation of IReplayLogger that saves data to a file in the logs directory. 65 71 */ … … public: 71 77 ~CReplayLogger(); 72 78 73 79 virtual void StartGame(JS::MutableHandleValue attribs); 74 80 virtual void Turn(u32 n, u32 turnLength, std::vector<SimulationCommand>& commands); 75 81 virtual void Hash(const std::string& hash, bool quick); 82 virtual OsPath GetDirectory() const; 76 83 77 84 private: 78 85 ScriptInterface& m_ScriptInterface; 79 86 std::ostream* m_Stream; 87 OsPath m_Directory; 80 88 }; 81 89 82 90 /** 83 91 * Replay log replayer. Runs the log with no graphics and dumps some info to stdout. 84 92 */