satdump/src-core/core/plugin.cpp

108 lines
3.5 KiB
C++
Raw Normal View History

#define SATDUMP_DLL_EXPORT 1
#include "plugin.h"
2021-08-19 01:56:57 +02:00
#ifdef _WIN32
2021-10-09 14:57:07 +02:00
#include "libs/dlfcn/dlfcn.h"
2021-08-19 01:56:57 +02:00
#else
#include <dlfcn.h>
2021-08-19 01:56:57 +02:00
#endif
#include "logger.h"
#include <filesystem>
#include "satdump_vars.h"
std::shared_ptr<satdump::Plugin> loadPlugin(std::string plugin)
{
logger->trace("Loading plugin " + plugin + "...");
void *dynlib = dlopen(plugin.c_str(), RTLD_LAZY);
if (!dynlib)
2022-07-02 13:01:39 +00:00
throw std::runtime_error("Error loading " + plugin + "! Error : " + std::string(dlerror()));
void *create = dlsym(dynlib, "loader");
const char *dlsym_error = dlerror();
if (dlsym_error != NULL)
logger->warn("Possible error loading symbols from plugin!");
satdump::Plugin *pluginObject = reinterpret_cast<satdump::Plugin *(*)()>(create)();
pluginObject->init();
logger->trace("Plugin " + pluginObject->getID() + " loaded!");
return std::shared_ptr<satdump::Plugin>(pluginObject);
}
namespace satdump
{
SATDUMP_DLL std::map<std::string, std::shared_ptr<satdump::Plugin>> loaded_plugins;
2022-05-13 23:40:35 +02:00
SATDUMP_DLL std::shared_ptr<EventBus> eventBus = std::make_shared<EventBus>();
};
2023-05-30 21:24:53 +02:00
#ifdef __ANDROID__
std::string android_plugins_dir = "";
#endif
2022-05-12 18:19:01 +02:00
void loadPlugins(std::map<std::string, std::shared_ptr<satdump::Plugin>> &loaded_plugins)
{
2023-11-26 18:21:53 +01:00
// Get possible paths
std::vector<std::string> plugins_paths;
2022-05-15 20:15:19 +02:00
#ifdef __ANDROID__
plugins_paths.push_back(android_plugins_dir + "/");
2022-05-15 20:15:19 +02:00
#else
if (std::filesystem::exists("plugins"))
2023-11-26 18:21:53 +01:00
plugins_paths.push_back("./plugins"); // Try local plugins directory first
plugins_paths.push_back(satdump::LIBPATH + "plugins"); // Followed by system
2022-05-15 20:15:19 +02:00
#endif
2023-11-26 18:21:53 +01:00
// Get platform file extensions
2022-02-22 14:24:18 +01:00
#if defined(_WIN32)
std::filesystem::path extension = ".dll";
2022-02-22 14:24:18 +01:00
#elif defined(__APPLE__)
std::filesystem::path extension = ".dylib";
2022-02-22 14:24:18 +01:00
#else
std::filesystem::path extension = ".so";
2022-02-22 14:24:18 +01:00
#endif
2023-11-26 18:21:53 +01:00
// Load all plugins
for (std::string &plugins_path : plugins_paths)
{
logger->info("Loading plugins from " + plugins_path);
std::filesystem::recursive_directory_iterator pluginIterator(plugins_path);
std::error_code iteratorError;
while (pluginIterator != std::filesystem::recursive_directory_iterator())
{
std::string path = pluginIterator->path().string();
if (!std::filesystem::is_regular_file(pluginIterator->path()) || pluginIterator->path().extension() != extension)
goto skip_this;
#ifdef __ANDROID__
if (path.find("libandroid_imgui.so") != std::string::npos)
goto skip_this;
if (path.find("libsatdump_core.so") != std::string::npos)
goto skip_this;
2023-06-01 20:16:54 +02:00
#endif
try
{
std::shared_ptr<satdump::Plugin> pl = loadPlugin(path);
2023-11-26 18:21:53 +01:00
loaded_plugins.insert({pl->getID(), pl});
}
2023-11-26 18:21:53 +01:00
catch (std::runtime_error &e)
{
logger->error(e.what());
}
2023-11-26 18:21:53 +01:00
skip_this:
pluginIterator.increment(iteratorError);
if (iteratorError)
logger->critical(iteratorError.message());
}
if (loaded_plugins.size() > 0)
{
logger->debug("Loaded plugins (" + std::to_string(loaded_plugins.size()) + ") : ");
2023-11-26 18:21:53 +01:00
for (std::pair<const std::string, std::shared_ptr<satdump::Plugin>> &it : loaded_plugins)
logger->debug(" - " + it.first);
break;
}
else
logger->warn("No Plugins in " + plugins_path + "!");
}
}