diff --git a/include/PluginManager.h b/include/PluginManager.h index 3c44488..254ccb6 100644 --- a/include/PluginManager.h +++ b/include/PluginManager.h @@ -2,14 +2,14 @@ #include #include -#include +#include "dynlib.h" #include "IPackageManager.h" struct PluginInfo { std::string path; std::string name; IPackageManager* instance = nullptr; - void* handle = nullptr; + dynlib_handle handle = nullptr; bool loaded = false; }; diff --git a/src/PluginManager.cpp b/src/PluginManager.cpp index ddb0e13..a6cd613 100644 --- a/src/PluginManager.cpp +++ b/src/PluginManager.cpp @@ -7,14 +7,20 @@ namespace fs = std::filesystem; PluginManager::PluginManager() { // Default search paths +#ifdef _WIN32 + char* appdata = getenv("APPDATA"); + if (appdata) { + m_searchPaths.push_back(std::string(appdata) + "\\PackageMapper\\plugins"); + } + m_searchPaths.push_back(".\\plugins"); +#else char* home = getenv("HOME"); if (home) { m_searchPaths.push_back(std::string(home) + "/.local/lib/PackageMapper/plugins"); } m_searchPaths.push_back("/usr/lib/PackageMapper/plugins"); m_searchPaths.push_back("/usr/local/lib/PackageMapper/plugins"); - - // Also check relative to executable +#endif m_searchPaths.push_back("./plugins"); // Load from all search paths @@ -34,7 +40,11 @@ void PluginManager::loadPluginsFromDirectory(const std::string& directory) { for (const auto& entry : fs::directory_iterator(directory)) { if (entry.is_regular_file()) { auto ext = entry.path().extension().string(); +#ifdef _WIN32 + if (ext == ".dll") { +#else if (ext == ".so" || ext == ".dylib") { +#endif loadPlugin(entry.path().string()); } } @@ -45,28 +55,28 @@ void PluginManager::loadPluginsFromDirectory(const std::string& directory) { } bool PluginManager::loadPlugin(const std::string& path) { - void* handle = dlopen(path.c_str(), RTLD_NOW); + dynlib_handle handle = dynlib_open(path.c_str()); if (!handle) { - std::cerr << "Failed to load plugin: " << path << " - " << dlerror() << std::endl; + std::cerr << "Failed to load plugin: " << path << " - " << dynlib_error() << std::endl; return false; } using CreateFunc = IPackageManager* (*)(); using DestroyFunc = void (*)(IPackageManager*); - auto createFunc = reinterpret_cast(dlsym(handle, "createPlugin")); - auto destroyFunc = reinterpret_cast(dlsym(handle, "destroyPlugin")); + auto createFunc = reinterpret_cast(dynlib_sym(handle, "createPlugin")); + auto destroyFunc = reinterpret_cast(dynlib_sym(handle, "destroyPlugin")); if (!createFunc || !destroyFunc) { std::cerr << "Invalid plugin: " << path << " (missing entry points)" << std::endl; - dlclose(handle); + dynlib_close(handle); return false; } IPackageManager* instance = createFunc(); if (!instance) { std::cerr << "Plugin returned null: " << path << std::endl; - dlclose(handle); + dynlib_close(handle); return false; } @@ -86,11 +96,11 @@ void PluginManager::unloadAll() { for (auto& plugin : m_plugins) { if (plugin.instance && plugin.handle) { using DestroyFunc = void (*)(IPackageManager*); - auto destroyFunc = reinterpret_cast(dlsym(plugin.handle, "destroyPlugin")); + auto destroyFunc = reinterpret_cast(dynlib_sym(plugin.handle, "destroyPlugin")); if (destroyFunc) { destroyFunc(plugin.instance); } - dlclose(plugin.handle); + dynlib_close(plugin.handle); plugin.instance = nullptr; plugin.handle = nullptr; plugin.loaded = false;