From bece6ab2b6890ab538f5685f3d1cdcce233ff0a0 Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 27 Jul 2023 22:55:42 +0200 Subject: Add support for loading plugins from Thunderstore packages (#513) Adds support for loading plugins from `packages` directory which was missing from the original PR that introduced the `packages` directory. (cherry picked from commit a65cbeacfbaed17f0526fb513f49338f1151d247) --- NorthstarDLL/plugins/plugins.cpp | 54 ++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/NorthstarDLL/plugins/plugins.cpp b/NorthstarDLL/plugins/plugins.cpp index 1c426f09..9ba4707a 100644 --- a/NorthstarDLL/plugins/plugins.cpp +++ b/NorthstarDLL/plugins/plugins.cpp @@ -188,8 +188,36 @@ std::optional PluginManager::LoadPlugin(fs::path path, PluginInitFuncs* return plugin; } +inline void FindPlugins(fs::path pluginPath, std::vector& paths) +{ + // ensure dirs exist + if (!fs::exists(pluginPath) || !fs::is_directory(pluginPath)) + { + 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()); + } +} + 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 +235,22 @@ bool PluginManager::LoadPlugins() data.version = ns_version.c_str(); data.northstarModule = g_NorthstarModule; - if (strstr(GetCommandLineA(), "-noplugins") != NULL) - { - NS::log::PLUGINSYS->warn("-noplugins detected; skipping loading plugins"); - return false; - } - if (!fs::exists(pluginPath)) + FindPlugins(pluginPath, paths); + + // 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"; + FindPlugins(pluginDir, paths); } - // 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)) -- cgit v1.2.3