From 8f54d16f5fdc41cc8dd3a894df6f81ec2ed328e7 Mon Sep 17 00:00:00 2001 From: markmon Date: Tue, 12 Mar 2024 11:14:45 -0700 Subject: [PATCH] added global plugins and profiles (#223) * added global plugins and profiles * Fixed inconsistent tabs * Plugins: Check if the folder exists before loading * Plugins: Reduce code repetition --------- Co-authored-by: praydog --- src/mods/PluginLoader.cpp | 39 ++++++++++++++++++++------------- src/mods/vr/runtimes/OpenXR.cpp | 13 ++++++++--- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/mods/PluginLoader.cpp b/src/mods/PluginLoader.cpp index 742c31a9..fa5d14aa 100644 --- a/src/mods/PluginLoader.cpp +++ b/src/mods/PluginLoader.cpp @@ -1261,7 +1261,8 @@ void PluginLoader::early_init() try { spdlog::info("[PluginLoader] Module path {}", utility::narrow(module_path)); const auto plugin_path = Framework::get_persistent_dir() / "plugins"; - + const auto global_plugins_path = Framework::get_persistent_dir() / ".." / "UEVR" / "plugins"; + spdlog::info("[PluginLoader] Creating directories {}", plugin_path.string()); if (!fs::create_directories(plugin_path) && !fs::exists(plugin_path)) { @@ -1272,23 +1273,31 @@ void PluginLoader::early_init() try { spdlog::info("[PluginLoader] Loading plugins..."); - // Load all dlls in the plugins directory. - for (auto&& entry : fs::directory_iterator{plugin_path}) { - auto&& path = entry.path(); + auto load_plugins_from_dir = [this](std::filesystem::path path) { + if (!fs::exists(path) || !fs::is_directory(path)) { + return; + } - if (path.has_extension() && path.extension() == ".dll") { - auto module = LoadLibrary(path.string().c_str()); + for (auto&& entry : fs::directory_iterator{path}) { + auto&& path = entry.path(); - if (module == nullptr) { - spdlog::error("[PluginLoader] Failed to load {}", path.string()); - m_plugin_load_errors.emplace(path.stem().string(), "Failed to load"); - continue; - } + if (path.has_extension() && path.extension() == ".dll") { + auto module = LoadLibrary(path.string().c_str()); - spdlog::info("[PluginLoader] Loaded {}", path.string()); - m_plugins.emplace(path.stem().string(), module); + if (module == nullptr) { + spdlog::error("[PluginLoader] Failed to load {}", path.string()); + m_plugin_load_errors.emplace(path.stem().string(), "Failed to load"); + continue; + } + + spdlog::info("[PluginLoader] Loaded {}", path.string()); + m_plugins.emplace(path.stem().string(), module); + } } - } + }; + + load_plugins_from_dir(global_plugins_path); + load_plugins_from_dir(plugin_path); } catch(const std::exception& e) { spdlog::error("[PluginLoader] Exception during early init {}", e.what()); } catch(...) { @@ -1777,4 +1786,4 @@ bool PluginLoader::add_on_post_viewport_client_draw(UEVR_ViewportClient_DrawCb c m_on_post_viewport_client_draw_cbs.push_back(cb); return true; -} \ No newline at end of file +} diff --git a/src/mods/vr/runtimes/OpenXR.cpp b/src/mods/vr/runtimes/OpenXR.cpp index 6afb8e61..99a0996f 100644 --- a/src/mods/vr/runtimes/OpenXR.cpp +++ b/src/mods/vr/runtimes/OpenXR.cpp @@ -983,12 +983,19 @@ std::optional OpenXR::initialize_actions(const std::string& json_st } } - auto filename = controller + ".json"; + auto profile_file = controller + ".json"; // replace the slashes with underscores - std::replace(filename.begin(), filename.end(), '/', '_'); + std::replace(profile_file.begin(), profile_file.end(), '/', '_'); + + // If the json exists in the game's profile dir, use that. + auto filename = (Framework::get_persistent_dir() / profile_file).string(); - filename = (Framework::get_persistent_dir() / filename).string(); + // If not, check for global profile in UEVR\Profiles dir + if (!std::filesystem::exists(filename)) { + filename = (Framework::get_persistent_dir() / ".." / "UEVR" / "Profiles" / profile_file).string(); + spdlog::info("[VR] Setting bindings file to {}", filename); + } // check if the file exists if (std::filesystem::exists(filename)) {