1
0
mirror of synced 2024-11-12 02:00:52 +01:00

impr: Make sure plugins are only loaded once

This commit is contained in:
WerWolv 2024-01-07 18:45:17 +01:00
parent 1c5d6cbe94
commit cab329556c
2 changed files with 21 additions and 4 deletions

View File

@ -104,6 +104,8 @@ namespace hex {
static std::vector<Plugin> &getPlugins();
static std::vector<std::fs::path> &getPluginPaths();
static bool isPluginLoaded(const std::fs::path &path);
};
}

View File

@ -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();
});
}
}