From 36ddffd20fb4445a574cc011bd9ed14ef42aacfb Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Fri, 21 Jul 2023 22:22:54 +0200 Subject: [PATCH 1/4] Add support for loading plugins from Thunderstore packages --- NorthstarDLL/plugins/plugins.cpp | 51 +++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/NorthstarDLL/plugins/plugins.cpp b/NorthstarDLL/plugins/plugins.cpp index 1c426f09d..89a2ecc3b 100644 --- a/NorthstarDLL/plugins/plugins.cpp +++ b/NorthstarDLL/plugins/plugins.cpp @@ -190,6 +190,12 @@ std::optional PluginManager::LoadPlugin(fs::path path, PluginInitFuncs* bool PluginManager::LoadPlugins() { + if (strstr(GetCommandLineA(), "-noplugins") != NULL) + { + NS::log::PLUGINSYS->warn("-noplugins detected; skipping loading plugins"); + return false; + } + std::vector paths; pluginPath = GetNorthstarPrefix() + "/plugins"; @@ -207,28 +213,45 @@ bool PluginManager::LoadPlugins() data.version = ns_version.c_str(); data.northstarModule = g_NorthstarModule; - if (strstr(GetCommandLineA(), "-noplugins") != NULL) + if (fs::exists(pluginPath) && fs::is_directory(pluginPath)) { - NS::log::PLUGINSYS->warn("-noplugins detected; skipping loading plugins"); - return false; + // ensure dirs exist + fs::recursive_directory_iterator iterator(pluginPath); + if (std::filesystem::begin(iterator) != std::filesystem::end(iterator)) + { + for (auto const& entry : iterator) + { + if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") + paths.emplace_back(entry.path()); + } + } } - if (!fs::exists(pluginPath)) + + // Special case for Thunderstore plugin dirs + + for (fs::directory_entry dir : fs::directory_iterator(GetThunderstoreModFolderPath())) { - NS::log::PLUGINSYS->warn("Could not find a plugins directory. Skipped loading plugins"); - return false; + fs::path pluginDir = dir.path() / "plugins"; + if (fs::exists(pluginDir) && fs::is_directory(pluginDir)) + { + fs::recursive_directory_iterator iterator(pluginDir); + if (std::filesystem::begin(iterator) != std::filesystem::end(iterator)) + { + for (auto const& entry : iterator) + { + if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") + paths.emplace_back(entry.path()); + } + } + } } - // ensure dirs exist - fs::recursive_directory_iterator iterator(pluginPath); - if (std::filesystem::begin(iterator) == std::filesystem::end(iterator)) + + if (paths.empty()) { NS::log::PLUGINSYS->warn("Could not find any plugins. Skipped loading plugins"); return false; } - for (auto const& entry : iterator) - { - if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") - paths.emplace_back(entry.path()); - } + for (fs::path path : paths) { if (LoadPlugin(path, &funcs, &data)) From c97f3c5033cbdd2d89a3aafb90e9fa73e35607f3 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Mon, 24 Jul 2023 00:01:20 +0200 Subject: [PATCH 2/4] move duplicate code into inline helper function --- NorthstarDLL/plugins/plugins.cpp | 44 ++++++++++++++------------------ 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/NorthstarDLL/plugins/plugins.cpp b/NorthstarDLL/plugins/plugins.cpp index 89a2ecc3b..d170d33db 100644 --- a/NorthstarDLL/plugins/plugins.cpp +++ b/NorthstarDLL/plugins/plugins.cpp @@ -188,6 +188,23 @@ std::optional PluginManager::LoadPlugin(fs::path path, PluginInitFuncs* return plugin; } +inline void findPlugins(fs::path pluginPath, std::vector& paths) +{ + if (fs::exists(pluginPath) && fs::is_directory(pluginPath)) + { + // ensure dirs exist + fs::recursive_directory_iterator iterator(pluginPath); + if (std::filesystem::begin(iterator) != std::filesystem::end(iterator)) + { + for (auto const& entry : iterator) + { + if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") + paths.emplace_back(entry.path()); + } + } + } +} + bool PluginManager::LoadPlugins() { if (strstr(GetCommandLineA(), "-noplugins") != NULL) @@ -213,37 +230,14 @@ bool PluginManager::LoadPlugins() data.version = ns_version.c_str(); data.northstarModule = g_NorthstarModule; - if (fs::exists(pluginPath) && fs::is_directory(pluginPath)) - { - // ensure dirs exist - fs::recursive_directory_iterator iterator(pluginPath); - if (std::filesystem::begin(iterator) != std::filesystem::end(iterator)) - { - for (auto const& entry : iterator) - { - if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") - paths.emplace_back(entry.path()); - } - } - } + findPlugins(pluginPath, paths); // Special case for Thunderstore plugin dirs for (fs::directory_entry dir : fs::directory_iterator(GetThunderstoreModFolderPath())) { fs::path pluginDir = dir.path() / "plugins"; - if (fs::exists(pluginDir) && fs::is_directory(pluginDir)) - { - fs::recursive_directory_iterator iterator(pluginDir); - if (std::filesystem::begin(iterator) != std::filesystem::end(iterator)) - { - for (auto const& entry : iterator) - { - if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") - paths.emplace_back(entry.path()); - } - } - } + findPlugins(pluginDir, paths); } if (paths.empty()) From 09bb27c0590fc3ad0a5a35230acaf4586aa1b466 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 26 Jul 2023 11:29:34 +0200 Subject: [PATCH 3/4] reduce nesting in inline plugin finding function --- NorthstarDLL/plugins/plugins.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/NorthstarDLL/plugins/plugins.cpp b/NorthstarDLL/plugins/plugins.cpp index d170d33db..14d289f94 100644 --- a/NorthstarDLL/plugins/plugins.cpp +++ b/NorthstarDLL/plugins/plugins.cpp @@ -190,18 +190,23 @@ std::optional PluginManager::LoadPlugin(fs::path path, PluginInitFuncs* inline void findPlugins(fs::path pluginPath, std::vector& paths) { - if (fs::exists(pluginPath) && fs::is_directory(pluginPath)) + // ensure dirs exist + if (!fs::exists(pluginPath) || !fs::is_directory(pluginPath)) { - // ensure dirs exist - fs::recursive_directory_iterator iterator(pluginPath); - if (std::filesystem::begin(iterator) != std::filesystem::end(iterator)) - { - for (auto const& entry : iterator) - { - if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") - paths.emplace_back(entry.path()); - } - } + return; + } + + fs::recursive_directory_iterator iterator(pluginPath); + // ensure iterator is not empty + if (std::filesystem::begin(iterator) != std::filesystem::end(iterator)) + { + return; + } + + for (auto const& entry : iterator) + { + if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") + paths.emplace_back(entry.path()); } } From 6e40b6e22edd87b891c79b02891d8b4473729680 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 26 Jul 2023 20:17:42 +0200 Subject: [PATCH 4/4] rename inline method --- NorthstarDLL/plugins/plugins.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NorthstarDLL/plugins/plugins.cpp b/NorthstarDLL/plugins/plugins.cpp index 14d289f94..9ba4707a9 100644 --- a/NorthstarDLL/plugins/plugins.cpp +++ b/NorthstarDLL/plugins/plugins.cpp @@ -188,7 +188,7 @@ std::optional PluginManager::LoadPlugin(fs::path path, PluginInitFuncs* return plugin; } -inline void findPlugins(fs::path pluginPath, std::vector& paths) +inline void FindPlugins(fs::path pluginPath, std::vector& paths) { // ensure dirs exist if (!fs::exists(pluginPath) || !fs::is_directory(pluginPath)) @@ -235,14 +235,14 @@ bool PluginManager::LoadPlugins() data.version = ns_version.c_str(); data.northstarModule = g_NorthstarModule; - findPlugins(pluginPath, paths); + FindPlugins(pluginPath, paths); // Special case for Thunderstore plugin dirs for (fs::directory_entry dir : fs::directory_iterator(GetThunderstoreModFolderPath())) { fs::path pluginDir = dir.path() / "plugins"; - findPlugins(pluginDir, paths); + FindPlugins(pluginDir, paths); } if (paths.empty())