From 6198c0bac5c643b415b43ffb8e3e68823a7805c8 Mon Sep 17 00:00:00 2001 From: Edo Date: Sat, 8 Apr 2023 12:22:50 +0200 Subject: [PATCH] [IO]: Support fs_game mod folder (#913) --- src/Components/Modules/GSC/IO.cpp | 45 +++++++++++++++++++------------ src/Components/Modules/GSC/IO.hpp | 3 ++- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Components/Modules/GSC/IO.cpp b/src/Components/Modules/GSC/IO.cpp index fd9176a2..662bc079 100644 --- a/src/Components/Modules/GSC/IO.cpp +++ b/src/Components/Modules/GSC/IO.cpp @@ -8,7 +8,7 @@ namespace Components::GSC FILE* IO::openScriptIOFileHandle; - std::filesystem::path IO::Path; + std::filesystem::path IO::DefaultDestPath; bool IO::ValidatePath(const char* function, const char* path) { @@ -24,6 +24,17 @@ namespace Components::GSC return true; } + std::filesystem::path IO::BuildPath(const char* path) + { + const std::filesystem::path fsGame = (*Game::fs_gameDirVar)->current.string; + if (!fsGame.empty()) + { + return fsGame / "scriptdata"s / path; + } + + return DefaultDestPath / "scriptdata"s / path; + } + void IO::GScr_OpenFile() { const auto* filepath = Game::Scr_GetString(0); @@ -49,10 +60,10 @@ namespace Components::GSC return; } - const auto scriptData = Path / "scriptdata"s / filepath; + const auto dest = BuildPath(filepath); _set_errno(0); - const auto result = fopen_s(&openScriptIOFileHandle, scriptData.string().data(), "r"); + const auto result = fopen_s(&openScriptIOFileHandle, dest.string().data(), "r"); if (result || !openScriptIOFileHandle) { Logger::PrintError(Game::CON_CHANNEL_PARSERSCRIPT, "OpenFile failed. '{}'", result); @@ -119,8 +130,8 @@ namespace Components::GSC } const auto append = mode == "append"s; - const auto scriptData = Path / "scriptdata"s / filepath; - Utils::IO::WriteFile(scriptData.string(), text, append); + const auto dest = BuildPath(filepath); + Utils::IO::WriteFile(dest.string(), text, append); }); Script::AddFunction("FileRead", [] // gsc: FileRead() @@ -131,12 +142,12 @@ namespace Components::GSC return; } - const auto scriptData = Path / "scriptdata"s / filepath; + const auto dest = BuildPath(filepath); std::string file; - if (!Utils::IO::ReadFile(scriptData.string(), &file)) + if (!Utils::IO::ReadFile(dest.string(), &file)) { - Logger::PrintError(Game::CON_CHANNEL_PARSERSCRIPT, "FileRead: file '{}' not found!\n", scriptData.string()); + Logger::PrintError(Game::CON_CHANNEL_PARSERSCRIPT, "FileRead: file '{}' not found!\n", dest.string()); return; } @@ -152,8 +163,8 @@ namespace Components::GSC return; } - const auto scriptData = Path / "scriptdata"s / filepath; - Game::Scr_AddBool(Utils::IO::FileExists(scriptData.string())); + const auto dest = BuildPath(filepath); + Game::Scr_AddBool(Utils::IO::FileExists(dest.string())); }); Script::AddFunction("FileRemove", [] // gsc: FileRemove() @@ -164,8 +175,8 @@ namespace Components::GSC return; } - const auto scriptData = Path / "scriptdata"s / filepath; - Game::Scr_AddBool(Utils::IO::RemoveFile(scriptData.string())); + const auto dest = BuildPath(filepath); + Game::Scr_AddBool(Utils::IO::RemoveFile(dest.string())); }); Script::AddFunction("FileRename", [] // gsc: FileRename(, ) @@ -177,8 +188,8 @@ namespace Components::GSC return; } - const auto from = Path / "scriptdata"s / filepath; - const auto to = Path / "scriptdata"s / destpath; + const auto from = BuildPath(filepath); + const auto to = BuildPath(destpath); std::error_code err; std::filesystem::rename(from, to, err); @@ -201,8 +212,8 @@ namespace Components::GSC return; } - const auto from = Path / "scriptdata"s / filepath; - const auto to = Path / "scriptdata"s / destpath; + const auto from = BuildPath(filepath); + const auto to = BuildPath(destpath); std::error_code err; std::filesystem::copy(from, to, err); @@ -222,7 +233,7 @@ namespace Components::GSC IO::IO() { openScriptIOFileHandle = nullptr; - Path = "userraw"s; + DefaultDestPath = "userraw"s; AddScriptFunctions(); diff --git a/src/Components/Modules/GSC/IO.hpp b/src/Components/Modules/GSC/IO.hpp index f77ddc65..45a9a0b8 100644 --- a/src/Components/Modules/GSC/IO.hpp +++ b/src/Components/Modules/GSC/IO.hpp @@ -12,9 +12,10 @@ namespace Components::GSC static FILE* openScriptIOFileHandle; - static std::filesystem::path Path; + static std::filesystem::path DefaultDestPath; static bool ValidatePath(const char* function, const char* path); + static std::filesystem::path BuildPath(const char* path); static void GScr_OpenFile(); static void GScr_ReadStream();