1
0
mirror of synced 2024-11-27 17:10:51 +01:00
ImHex/source/helpers/plugin_manager.cpp

115 lines
4.4 KiB
C++
Raw Normal View History

#include "helpers/plugin_manager.hpp"
2021-08-29 22:15:18 +02:00
#include <hex/helpers/logger.hpp>
#include <filesystem>
namespace hex {
namespace fs = std::filesystem;
// hex::plugin::<pluginName>::internal::initializePlugin()
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin{0}{1}8internal16initializePluginEv";
constexpr auto GetPluginNameSymbol = "_ZN3hex6plugin{0}{1}8internal13getPluginNameEv";
constexpr auto GetPluginAuthorSymbol = "_ZN3hex6plugin{0}{1}8internal15getPluginAuthorEv";
constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin{0}{1}8internal20getPluginDescriptionEv";
2021-08-21 00:52:11 +02:00
constexpr auto SetImGuiContextSymbol = "_ZN3hex6plugin{0}{1}8internal15setImGuiContextEP12ImGuiContext";
Plugin::Plugin(const std::string &path) {
this->m_handle = dlopen(path.data(), RTLD_LAZY);
if (this->m_handle == nullptr) {
2021-08-29 22:15:18 +02:00
log::error("dlopen failed: {}", dlerror());
return;
}
auto pluginName = fs::path(path).stem().string();
this->m_initializePluginFunction = getPluginFunction<InitializePluginFunc>(pluginName, InitializePluginSymbol);
this->m_getPluginNameFunction = getPluginFunction<GetPluginNameFunc>(pluginName, GetPluginNameSymbol);
this->m_getPluginAuthorFunction = getPluginFunction<GetPluginAuthorFunc>(pluginName, GetPluginAuthorSymbol);
this->m_getPluginDescriptionFunction = getPluginFunction<GetPluginDescriptionFunc>(pluginName, GetPluginDescriptionSymbol);
2021-08-21 00:52:11 +02:00
this->m_setImGuiContextFunction = getPluginFunction<SetImGuiContextFunc>(pluginName, SetImGuiContextSymbol);
}
Plugin::Plugin(Plugin &&other) noexcept {
this->m_handle = other.m_handle;
this->m_initializePluginFunction = other.m_initializePluginFunction;
this->m_getPluginNameFunction = other.m_getPluginNameFunction;
this->m_getPluginAuthorFunction = other.m_getPluginAuthorFunction;
this->m_getPluginDescriptionFunction = other.m_getPluginDescriptionFunction;
2021-08-21 00:52:11 +02:00
this->m_setImGuiContextFunction = other.m_setImGuiContextFunction;
other.m_handle = nullptr;
other.m_initializePluginFunction = nullptr;
other.m_getPluginNameFunction = nullptr;
other.m_getPluginAuthorFunction = nullptr;
other.m_getPluginDescriptionFunction = nullptr;
2021-08-21 00:52:11 +02:00
other.m_setImGuiContextFunction = nullptr;
}
Plugin::~Plugin() {
if (this->m_handle != nullptr)
dlclose(this->m_handle);
}
void Plugin::initializePlugin() const {
if (this->m_initializePluginFunction != nullptr)
this->m_initializePluginFunction();
}
std::string Plugin::getPluginName() const {
if (this->m_getPluginNameFunction != nullptr)
return this->m_getPluginNameFunction();
else
return hex::format("Unknown Plugin @ 0x{0:016X}", reinterpret_cast<intptr_t>(this->m_handle));
}
std::string Plugin::getPluginAuthor() const {
if (this->m_getPluginAuthorFunction != nullptr)
return this->m_getPluginAuthorFunction();
else
return "Unknown";
}
std::string Plugin::getPluginDescription() const {
if (this->m_getPluginDescriptionFunction != nullptr)
return this->m_getPluginDescriptionFunction();
else
return "";
}
2021-08-21 00:52:11 +02:00
void Plugin::setImGuiContext(ImGuiContext *ctx) const {
if (this->m_setImGuiContextFunction != nullptr)
this->m_setImGuiContextFunction(ctx);
}
bool PluginManager::load(const std::string &pluginFolder) {
if (!std::filesystem::exists(pluginFolder))
return false;
PluginManager::s_pluginFolder = pluginFolder;
2021-01-12 16:50:15 +01:00
for (auto& pluginPath : std::filesystem::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug")
PluginManager::s_plugins.emplace_back(pluginPath.path().string());
2021-01-12 16:50:15 +01:00
}
if (PluginManager::s_plugins.empty())
return false;
return true;
}
void PluginManager::unload() {
PluginManager::s_plugins.clear();
PluginManager::s_pluginFolder.clear();
}
void PluginManager::reload() {
PluginManager::unload();
PluginManager::load(PluginManager::s_pluginFolder);
}
}