Index: source/gui/scripting/ScriptFunctions.cpp =================================================================== --- source/gui/scripting/ScriptFunctions.cpp +++ source/gui/scripting/ScriptFunctions.cpp @@ -202,15 +202,15 @@ return g_Game->IsVisualReplay(); } -std::wstring GetCurrentReplayDirectory(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +std::string GetCurrentReplayDirectory(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) { if (!g_Game) - return std::wstring(); + return std::string(); if (g_Game->IsVisualReplay()) - return OsPath(g_Game->GetReplayPath()).Parent().Filename().string(); + return OsString(g_Game->GetReplayPath().Parent().Filename()); - return g_Game->GetReplayLogger().GetDirectory().Filename().string(); + return OsString(g_Game->GetReplayLogger().GetDirectory().Filename()); } int GetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) @@ -1059,7 +1059,7 @@ // Misc functions scriptInterface.RegisterFunction("SetCursor"); scriptInterface.RegisterFunction("IsVisualReplay"); - scriptInterface.RegisterFunction("GetCurrentReplayDirectory"); + scriptInterface.RegisterFunction("GetCurrentReplayDirectory"); scriptInterface.RegisterFunction("GetPlayerID"); scriptInterface.RegisterFunction("SetPlayerID"); scriptInterface.RegisterFunction("SetViewedPlayer"); Index: source/main.cpp =================================================================== --- source/main.cpp +++ source/main.cpp @@ -441,20 +441,20 @@ const bool isVisualReplay = args.Has("replay-visual"); const bool isNonVisualReplay = args.Has("replay"); - const CStr replayFile = + const OsPath replayFile( isVisualReplay ? args.Get("replay-visual") : - isNonVisualReplay ? args.Get("replay") : ""; + isNonVisualReplay ? args.Get("replay") : ""); if (isVisualReplay || isNonVisualReplay) { - if (!FileExists(OsPath(replayFile))) + if (!FileExists(replayFile)) { - debug_printf("ERROR: The requested replay file '%s' does not exist!\n", replayFile.c_str()); + debug_printf("ERROR: The requested replay file '%s' does not exist!\n", OsString(replayFile).c_str()); return; } - if (DirectoryExists(OsPath(replayFile))) + if (DirectoryExists(replayFile)) { - debug_printf("ERROR: The requested replay file '%s' is a directory!\n", replayFile.c_str()); + debug_printf("ERROR: The requested replay file '%s' is a directory!\n", OsString(replayFile).c_str()); return; } } Index: source/ps/Game.h =================================================================== --- source/ps/Game.h +++ source/ps/Game.h @@ -18,9 +18,10 @@ #ifndef INCLUDED_GAME #define INCLUDED_GAME -#include "ps/Errors.h" #include +#include "ps/Errors.h" +#include "ps/Filesystem.h" #include "scriptinterface/ScriptVal.h" #include "simulation2/helpers/Player.h" @@ -91,7 +92,7 @@ void StartGame(JS::MutableHandleValue attribs, const std::string& savedState); PSRETURN ReallyStartGame(); - bool StartVisualReplay(const std::string& replayPath); + bool StartVisualReplay(const OsPath& replayPath); /** * Periodic heartbeat that controls the process. performs all per-frame updates. @@ -178,7 +179,7 @@ inline float GetSimRate() const { return m_SimRate; } - inline std::string GetReplayPath() const + inline OsPath GetReplayPath() const { return m_ReplayPath; } /** @@ -204,7 +205,7 @@ bool m_IsSavedGame; // true if loading a saved game; false for a new game int LoadVisualReplayData(); - std::string m_ReplayPath; + OsPath m_ReplayPath; bool m_IsVisualReplay; std::istream* m_ReplayStream; u32 m_FinalReplayTurn; Index: source/ps/Game.cpp =================================================================== --- source/ps/Game.cpp +++ source/ps/Game.cpp @@ -168,9 +168,9 @@ return 0; } -bool CGame::StartVisualReplay(const std::string& replayPath) +bool CGame::StartVisualReplay(const OsPath& replayPath) { - debug_printf("Starting to replay %s\n", replayPath.c_str()); + debug_printf("Starting to replay %s\n", OsString(replayPath).c_str()); m_IsVisualReplay = true; ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface(); @@ -178,7 +178,7 @@ SetTurnManager(new CReplayTurnManager(*m_Simulation2, GetReplayLogger())); m_ReplayPath = replayPath; - m_ReplayStream = new std::ifstream(m_ReplayPath.c_str()); + m_ReplayStream = new std::ifstream(OsString(replayPath).c_str()); std::string type; ENSURE((*m_ReplayStream >> type).good() && type == "start"); Index: source/ps/Replay.h =================================================================== --- source/ps/Replay.h +++ source/ps/Replay.h @@ -96,7 +96,7 @@ CReplayPlayer(); ~CReplayPlayer(); - void Load(const std::string& path); + void Load(const OsPath& path); void Replay(bool serializationtest, int rejointestturn, bool ooslog); private: Index: source/ps/Replay.cpp =================================================================== --- source/ps/Replay.cpp +++ source/ps/Replay.cpp @@ -61,7 +61,7 @@ m_ScriptInterface.SetProperty(attribs, "mods", g_modsLoaded); m_Directory = createDateIndexSubdirectory(VisualReplay::GetDirectoryName()); - debug_printf("Writing replay to %s\n", m_Directory.string8().c_str()); + debug_printf("Writing replay to %s\n", OsString(m_Directory).c_str()); m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc); *m_Stream << "start " << m_ScriptInterface.StringifyJSON(attribs, false) << "\n"; @@ -106,11 +106,11 @@ delete m_Stream; } -void CReplayPlayer::Load(const std::string& path) +void CReplayPlayer::Load(const OsPath& path) { ENSURE(!m_Stream); - m_Stream = new std::ifstream(path.c_str()); + m_Stream = new std::ifstream(OsString(path).c_str()); ENSURE(m_Stream->good()); } Index: source/ps/VisualReplay.h =================================================================== --- source/ps/VisualReplay.h +++ source/ps/VisualReplay.h @@ -39,7 +39,7 @@ /** * Replays the commands.txt file in the given subdirectory visually. */ -void StartVisualReplay(const CStrW& directory); +void StartVisualReplay(const OsPath& directory); /** * Get a list of replays to display in the GUI. @@ -61,22 +61,22 @@ * @param replayFile path to commands.txt, whose parent directory will be deleted * @return true if deletion was successful, false on error */ -bool DeleteReplay(const CStrW& replayFile); +bool DeleteReplay(const OsPath& replayFile); /** * Returns the parsed header of the replay file (commands.txt). */ -JS::Value GetReplayAttributes(ScriptInterface::CxPrivate* pCxPrivate, const CStrW& directoryName); +JS::Value GetReplayAttributes(ScriptInterface::CxPrivate* pCxPrivate, const OsPath& directoryName); /** * Returns whether or not the metadata / summary screen data has been saved properly when the game ended. */ -bool HasReplayMetadata(const CStrW& directoryName); +bool HasReplayMetadata(const OsPath& directoryName); /** * Returns the metadata of a replay. */ -JS::Value GetReplayMetadata(ScriptInterface::CxPrivate* pCxPrivate, const CStrW& directoryName); +JS::Value GetReplayMetadata(ScriptInterface::CxPrivate* pCxPrivate, const OsPath& directoryName); /** * Saves the metadata from the session to metadata.json Index: source/ps/VisualReplay.cpp =================================================================== --- source/ps/VisualReplay.cpp +++ source/ps/VisualReplay.cpp @@ -46,19 +46,19 @@ return OsPath(paths.UserData() / "replays" / engine_version); } -void VisualReplay::StartVisualReplay(const CStrW& directory) +void VisualReplay::StartVisualReplay(const OsPath& directory) { ENSURE(!g_NetServer); ENSURE(!g_NetClient); ENSURE(!g_Game); - const OsPath replayFile = VisualReplay::GetDirectoryName() / directory / L"commands.txt"; + const OsPath replayFile(VisualReplay::GetDirectoryName() / directory / L"commands.txt"); if (!FileExists(replayFile)) return; g_Game = new CGame(false, false); - g_Game->StartVisualReplay(replayFile.string8()); + g_Game->StartVisualReplay(replayFile); } /** @@ -96,7 +96,7 @@ * * @return The current cursor position or -1 on error. */ -inline int goBackToLineBeginning(std::istream* replayStream, const CStr& fileName, const u64& fileSize) +inline off_t goBackToLineBeginning(std::istream* replayStream, const OsPath& fileName, off_t fileSize) { int currentPos; char character; @@ -110,7 +110,7 @@ if (!replayStream->good()) { - LOGERROR("Unknown error when returning to the last line (%i of %lu) of %s", currentPos, fileSize, fileName.c_str()); + LOGERROR("Unknown error when returning to the last line (%i of %lu) of %s", currentPos, fileSize, OsString(fileName).c_str()); return -1; } @@ -124,7 +124,7 @@ replayStream->seekg(-2, std::ios_base::cur); } - LOGERROR("Infinite loop when going back to a line beginning in %s", fileName.c_str()); + LOGERROR("Infinite loop when going back to a line beginning in %s", OsString(fileName).c_str()); return -1; } @@ -134,7 +134,7 @@ * * @return seconds or -1 on error */ -inline int getReplayDuration(std::istream* replayStream, const CStr& fileName, const u64& fileSize) +inline int getReplayDuration(std::istream* replayStream, const OsPath& fileName, off_t fileSize) { CStr type; @@ -145,7 +145,7 @@ // There should be about 5 lines to read until a turn is found. for (int linesRead = 1; linesRead < 1000; ++linesRead) { - int currentPosition = goBackToLineBeginning(replayStream, fileName, fileSize); + off_t currentPosition = goBackToLineBeginning(replayStream, fileName, fileSize); // Read error or reached file beginning. No turns exist. if (currentPosition < 1) @@ -153,12 +153,12 @@ if (!replayStream->good()) { - LOGERROR("Read error when determining replay duration at %i of %llu in %s", currentPosition - 2, fileSize, fileName.c_str()); + LOGERROR("Read error when determining replay duration at %i of %llu in %s", currentPosition - 2, fileSize, OsString(fileName).c_str()); return -1; } // Found last turn, compute duration. - if ((u64) currentPosition + 4 < fileSize && (*replayStream >> type).good() && type == "turn") + if (currentPosition + 4 < fileSize && (*replayStream >> type).good() && type == "turn") { u32 turn = 0, turnLength = 0; *replayStream >> turn >> turnLength; @@ -169,14 +169,14 @@ replayStream->seekg(currentPosition - 2, std::ios_base::beg); } - LOGERROR("Infinite loop when determining replay duration for %s", fileName.c_str()); + LOGERROR("Infinite loop when determining replay duration for %s", OsString(fileName).c_str()); return -1; } JS::Value VisualReplay::LoadReplayData(ScriptInterface& scriptInterface, OsPath& directory) { // The directory argument must not be constant, otherwise concatenating will fail - const OsPath replayFile = GetDirectoryName() / directory / L"commands.txt"; + const OsPath replayFile(GetDirectoryName() / directory / L"commands.txt"); if (!FileExists(replayFile)) return JSVAL_NULL; @@ -184,26 +184,17 @@ // Get file size and modification date CFileInfo fileInfo; GetFileInfo(replayFile, &fileInfo); - const u64 fileSize = (u64)fileInfo.Size(); + const off_t fileSize = fileInfo.Size(); if (fileSize == 0) return JSVAL_NULL; - // Open file - const CStr fileName = replayFile.string8(); - std::ifstream* replayStream = new std::ifstream(fileName.c_str()); + std::ifstream* replayStream = new std::ifstream(OsString(replayFile).c_str()); - // File must begin with "start" CStr type; - if (!(*replayStream >> type).good()) - { - LOGERROR("Couldn't open %s. Non-latin characters are not supported yet.", fileName.c_str()); - SAFE_DELETE(replayStream); - return JSVAL_NULL; - } - if (type != "start") + if (!(*replayStream >> type).good() || type != "start") { - LOGWARNING("The replay %s is broken!", fileName.c_str()); + LOGERROR("Could not open %s.", OsString(replayFile).c_str()); SAFE_DELETE(replayStream); return JSVAL_NULL; } @@ -216,7 +207,7 @@ JS::RootedValue attribs(cx); if (!scriptInterface.ParseJSON(header, &attribs)) { - LOGERROR("Couldn't parse replay header of %s", fileName.c_str()); + LOGERROR("Couldn't parse replay header of %s", OsString(replayFile).c_str()); SAFE_DELETE(replayStream); return JSVAL_NULL; } @@ -237,7 +228,7 @@ return JSVAL_NULL; } - int duration = getReplayDuration(replayStream, fileName, fileSize); + int duration = getReplayDuration(replayStream, replayFile, fileSize); SAFE_DELETE(replayStream); @@ -248,14 +239,14 @@ // Return the actual data JS::RootedValue replayData(cx); scriptInterface.Eval("({})", &replayData); - scriptInterface.SetProperty(replayData, "file", replayFile); + scriptInterface.SetProperty(replayData, "file", OsString(replayFile)); scriptInterface.SetProperty(replayData, "directory", directory); scriptInterface.SetProperty(replayData, "attribs", attribs); scriptInterface.SetProperty(replayData, "duration", duration); return replayData; } -bool VisualReplay::DeleteReplay(const CStrW& replayDirectory) +bool VisualReplay::DeleteReplay(const OsPath& replayDirectory) { if (replayDirectory.empty()) return false; @@ -265,7 +256,7 @@ } -JS::Value VisualReplay::GetReplayAttributes(ScriptInterface::CxPrivate* pCxPrivate, const CStrW& directoryName) +JS::Value VisualReplay::GetReplayAttributes(ScriptInterface::CxPrivate* pCxPrivate, const OsPath& directoryName) { // Create empty JS object JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); @@ -279,7 +270,7 @@ return attribs; // Open file - std::istream* replayStream = new std::ifstream(replayFile.string8().c_str()); + std::istream* replayStream = new std::ifstream(OsString(replayFile).c_str()); CStr type, line; ENSURE((*replayStream >> type).good() && type == "start"); @@ -305,16 +296,16 @@ } // Get the directory of the currently active replay - const OsPath fileName = g_Game->GetReplayLogger().GetDirectory() / L"metadata.json"; + const OsPath fileName(g_Game->GetReplayLogger().GetDirectory() / L"metadata.json"); CreateDirectories(fileName.Parent(), 0700); - std::ofstream stream (fileName.string8().c_str(), std::ofstream::out | std::ofstream::trunc); + std::ofstream stream (OsString(fileName).c_str(), std::ofstream::out | std::ofstream::trunc); stream << scriptInterface->StringifyJSON(&metadata, false); stream.close(); - debug_printf("Saved replay metadata to %s\n", fileName.string8().c_str()); + debug_printf("Saved replay metadata to %s\n", OsString(fileName).c_str()); } -bool VisualReplay::HasReplayMetadata(const CStrW& directoryName) +bool VisualReplay::HasReplayMetadata(const OsPath& directoryName) { const OsPath filePath(GetDirectoryName() / directoryName / L"metadata.json"); @@ -327,7 +318,7 @@ return fileInfo.Size() > 0; } -JS::Value VisualReplay::GetReplayMetadata(ScriptInterface::CxPrivate* pCxPrivate, const CStrW& directoryName) +JS::Value VisualReplay::GetReplayMetadata(ScriptInterface::CxPrivate* pCxPrivate, const OsPath& directoryName) { if (!HasReplayMetadata(directoryName)) return JSVAL_NULL; @@ -336,7 +327,7 @@ JSAutoRequest rq(cx); JS::RootedValue metadata(cx); - std::ifstream* stream = new std::ifstream(OsPath(GetDirectoryName() / directoryName / L"metadata.json").string8()); + std::ifstream* stream = new std::ifstream(OsString(GetDirectoryName() / directoryName / L"metadata.json").c_str()); ENSURE(stream->good()); CStr line; std::getline(*stream, line);