fix: make PluginManager compatible with Windows and Linux
This commit is contained in:
1 parent
de9fbdc7c3
commit
a7f8893bf2
2 files changed
+22
-12
No files matched your search
+20
-10
@@ -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<CreateFunc>(dlsym(handle, "createPlugin"));
|
||||
auto destroyFunc = reinterpret_cast<DestroyFunc>(dlsym(handle, "destroyPlugin"));
|
||||
auto createFunc = reinterpret_cast<CreateFunc>(dynlib_sym(handle, "createPlugin"));
|
||||
auto destroyFunc = reinterpret_cast<DestroyFunc>(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<DestroyFunc>(dlsym(plugin.handle, "destroyPlugin"));
|
||||
auto destroyFunc = reinterpret_cast<DestroyFunc>(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;
|
||||
|
||||
Reference in new issue
Block a user