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 elexis, 9 years ago)

Adds a getter in ReplayLogger, so that we can retrieve the directory path that contains the commands.txt file after the game started. Also adds the timestamp, g_modsLoaded, and engine_version (#3359) when the game starts. This way we will be able to filter outdated / incompatible replays.

  • source/ps/Replay.cpp

     
    2424#include "lib/file/file_system.h"
    2525#include "lib/res/h_mgr.h"
    2626#include "lib/tex/tex.h"
    2727#include "ps/Game.h"
    2828#include "ps/Loader.h"
     29#include "ps/Mod.h"
    2930#include "ps/Profile.h"
    3031#include "ps/ProfileViewer.h"
     32#include "ps/Pyrogenesis.h"
    3133#include "scriptinterface/ScriptInterface.h"
    3234#include "scriptinterface/ScriptStats.h"
    3335#include "simulation2/Simulation2.h"
    3436#include "simulation2/helpers/SimulationCommand.h"
    3537
    CReplayLogger::~CReplayLogger()  
    6163    delete m_Stream;
    6264}
    6365
    6466void CReplayLogger::StartGame(JS::MutableHandleValue attribs)
    6567{
     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
    6675    // Construct the directory name based on the PID, to be relatively unique.
    6776    // Append "-1", "-2" etc if we run multiple matches in a single session,
    6877    // to avoid accidentally overwriting earlier logs.
    69 
    7078    std::wstringstream name;
    7179    name << getpid();
    7280
    7381    static int run = -1;
    7482    if (++run)
    7583        name << "-" << run;
    7684
    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);
    8087
     88    m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc);
    8189    *m_Stream << "start " << m_ScriptInterface.StringifyJSON(attribs, false) << "\n";
    8290}
    8391
    8492void CReplayLogger::Turn(u32 n, u32 turnLength, std::vector<SimulationCommand>& commands)
    8593{
    void CReplayLogger::Hash(const std::stri  
    101109        *m_Stream << "hash-quick " << Hexify(hash) << "\n";
    102110    else
    103111        *m_Stream << "hash " << Hexify(hash) << "\n";
    104112}
    105113
     114OsPath CReplayLogger::GetDirectory() const
     115{
     116    return m_Directory;
     117}
     118
    106119////////////////////////////////////////////////////////////////
    107120
    108121CReplayPlayer::CReplayPlayer() :
    109122    m_Stream(NULL)
    110123{
  • source/ps/Replay.h

    public:  
    4545
    4646    /**
    4747     * Optional hash of simulation state (for sync checking).
    4848     */
    4949    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;
    5055};
    5156
    5257/**
    5358 * Implementation of IReplayLogger that simply throws away all data.
    5459 */
    class CDummyReplayLogger : public IRepla  
    5661{
    5762public:
    5863    virtual void StartGame(JS::MutableHandleValue UNUSED(attribs)) { }
    5964    virtual void Turn(u32 UNUSED(n), u32 UNUSED(turnLength), std::vector<SimulationCommand>& UNUSED(commands)) { }
    6065    virtual void Hash(const std::string& UNUSED(hash), bool UNUSED(quick)) { }
     66    virtual OsPath GetDirectory() const { return OsPath(); }
    6167};
    6268
    6369/**
    6470 * Implementation of IReplayLogger that saves data to a file in the logs directory.
    6571 */
    public:  
    7177    ~CReplayLogger();
    7278
    7379    virtual void StartGame(JS::MutableHandleValue attribs);
    7480    virtual void Turn(u32 n, u32 turnLength, std::vector<SimulationCommand>& commands);
    7581    virtual void Hash(const std::string& hash, bool quick);
     82    virtual OsPath GetDirectory() const;
    7683
    7784private:
    7885    ScriptInterface& m_ScriptInterface;
    7986    std::ostream* m_Stream;
     87    OsPath m_Directory;
    8088};
    8189
    8290/**
    8391 * Replay log replayer. Runs the log with no graphics and dumps some info to stdout.
    8492 */