From cab329556c65368dad872d21228bc224b4cd92e7 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 7 Jan 2024 18:45:17 +0100 Subject: [PATCH] impr: Make sure plugins are only loaded once --- .../include/hex/api/plugin_manager.hpp | 2 ++ lib/libimhex/source/api/plugin_manager.cpp | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/libimhex/include/hex/api/plugin_manager.hpp b/lib/libimhex/include/hex/api/plugin_manager.hpp index fafa71271..d9b6d5e3c 100644 --- a/lib/libimhex/include/hex/api/plugin_manager.hpp +++ b/lib/libimhex/include/hex/api/plugin_manager.hpp @@ -104,6 +104,8 @@ namespace hex { static std::vector &getPlugins(); static std::vector &getPluginPaths(); + + static bool isPluginLoaded(const std::fs::path &path); }; } \ No newline at end of file diff --git a/lib/libimhex/source/api/plugin_manager.cpp b/lib/libimhex/source/api/plugin_manager.cpp index c2c3ec098..6a7bd76bc 100644 --- a/lib/libimhex/source/api/plugin_manager.cpp +++ b/lib/libimhex/source/api/plugin_manager.cpp @@ -217,6 +217,8 @@ namespace hex { #endif } + + bool PluginManager::load(const std::fs::path &pluginFolder) { if (!wolv::io::fs::exists(pluginFolder)) return false; @@ -225,14 +227,20 @@ namespace hex { // Load library plugins first for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) { - if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexpluglib") - getPlugins().emplace_back(pluginPath.path()); + if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexpluglib") { + if (!isPluginLoaded(pluginPath.path())) { + getPlugins().emplace_back(pluginPath.path()); + } + } } // Load regular plugins afterwards for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) { - if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug") - getPlugins().emplace_back(pluginPath.path()); + if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug") { + if (!isPluginLoaded(pluginPath.path())) { + getPlugins().emplace_back(pluginPath.path()); + } + } } std::erase_if(getPlugins(), [](const Plugin &plugin) { @@ -285,4 +293,11 @@ namespace hex { return pluginPaths; } + bool PluginManager::isPluginLoaded(const std::fs::path &path) { + return std::ranges::any_of(getPlugins(), [&path](const Plugin &plugin) { + return plugin.getPath().filename() == path.filename(); + }); + } + + }