From 9fcf994b357ee1e710f446b497cdc82776adcaad Mon Sep 17 00:00:00 2001 From: m Date: Fri, 9 Sep 2022 20:38:04 -0500 Subject: [PATCH] register fs game path on fs startup [skip ci] --- src/client/component/command.cpp | 73 ++++++++++++----------------- src/client/component/command.hpp | 2 + src/client/component/fastfiles.cpp | 4 +- src/client/component/filesystem.cpp | 3 ++ 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/client/component/command.cpp b/src/client/component/command.cpp index 32ac5e9c..5b7c6d84 100644 --- a/src/client/component/command.cpp +++ b/src/client/component/command.cpp @@ -108,28 +108,6 @@ namespace command parsed = true; } - void register_fs_game_path() - { - static const auto* fs_game = game::Dvar_FindVar("fs_game"); - const auto new_mod_path = fs_game->current.string; - - // check if the last saved fs_game value isn't empty and if it doesn't equal the new fs_game - if (!saved_fs_game.empty() && saved_fs_game != new_mod_path) - { - // unregister path to be used as a fs directory - filesystem::unregister_path(saved_fs_game); - } - - if (new_mod_path && !new_mod_path[0]) - { - return; - } - - // register fs_game value as a fs directory used for many things - filesystem::register_path(new_mod_path); - saved_fs_game = new_mod_path; - } - void parse_startup_variables() { auto& com_num_console_lines = *reinterpret_cast(0x35634B8_b); @@ -417,24 +395,6 @@ namespace command ? "^2on" : "^1off")); } - - void monitor_fs_game_values() - { - dvars::callback::on_register("fs_game", []() - { - register_fs_game_path(); - }); - - // it might be overdone to change the filesystem path on every new value change, but to be fair, - // for the mods that don't need full restarts, this is good because it'll adjust and work like so - // in my opinion, this is fine. if a user tries to modify the dvar themselves, they'll have problems - // but i seriously doubt it'll be bad. - dvars::callback::on_new_value("fs_game", []() - { - console::warn("fs_game value changed, filesystem paths will be adjusted to new dvar value."); - register_fs_game_path(); - }); - } } void read_startup_variable(const std::string& dvar) @@ -597,13 +557,42 @@ namespace command } } + void register_fs_game_path() + { + const auto* fs_game = game::Dvar_FindVar("fs_game"); + const auto new_mod_path = fs_game->current.string; + + // check if the last saved fs_game value isn't empty and if it doesn't equal the new fs_game + if (!saved_fs_game.empty() && saved_fs_game != new_mod_path) + { + // unregister path to be used as a fs directory + filesystem::unregister_path(saved_fs_game); + } + + if (new_mod_path && !new_mod_path[0]) + { + return; + } + + // register fs_game value as a fs directory used for many things + filesystem::register_path(new_mod_path); + saved_fs_game = new_mod_path; + } + class component final : public component_interface { public: void post_unpack() override { - // monitor fs_game register and new value changes to adjust our paths for searching - monitor_fs_game_values(); + // it might be overdone to change the filesystem path on every new value change, but to be fair, + // for the mods that don't need full restarts, this is good because it'll adjust and work like so + // in my opinion, this is fine. if a user tries to modify the dvar themselves, they'll have problems + // but i seriously doubt it'll be bad. + dvars::callback::on_new_value("fs_game", []() + { + console::warn("fs_game value changed, filesystem paths will be adjusted to new dvar value."); + register_fs_game_path(); + }); if (game::environment::is_sp()) { diff --git a/src/client/component/command.hpp b/src/client/component/command.hpp index 06228c9d..a891b802 100644 --- a/src/client/component/command.hpp +++ b/src/client/component/command.hpp @@ -49,4 +49,6 @@ namespace command void add_sv(const char* name, std::function callback); void execute(std::string command, bool sync = false); + + void register_fs_game_path(); } \ No newline at end of file diff --git a/src/client/component/fastfiles.cpp b/src/client/component/fastfiles.cpp index 4a7aba99..8ad93908 100644 --- a/src/client/component/fastfiles.cpp +++ b/src/client/component/fastfiles.cpp @@ -171,8 +171,8 @@ namespace fastfiles utils::hook::detour sys_createfile_hook; HANDLE sys_create_file_stub(game::Sys_Folder folder, const char* base_filename) { - static const auto* fs_basepath = game::Dvar_FindVar("fs_basepath"); - static const auto* fs_game = game::Dvar_FindVar("fs_game"); + const auto* fs_basepath = game::Dvar_FindVar("fs_basepath"); + const auto* fs_game = game::Dvar_FindVar("fs_game"); std::string dir = fs_basepath ? fs_basepath->current.string : ""; std::string mod_dir = fs_game ? fs_game->current.string : ""; diff --git a/src/client/component/filesystem.cpp b/src/client/component/filesystem.cpp index 954d511c..1d4868c6 100644 --- a/src/client/component/filesystem.cpp +++ b/src/client/component/filesystem.cpp @@ -1,6 +1,7 @@ #include #include "loader/component_loader.hpp" +#include "command.hpp" #include "console.hpp" #include "filesystem.hpp" #include "localized_strings.hpp" @@ -46,6 +47,8 @@ namespace filesystem filesystem::register_path(L"h1-mod"); fs_startup_hook.invoke(name); + + command::register_fs_game_path(); } std::vector get_paths(const std::filesystem::path& path)