2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/plugin_manager.hpp>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
#include <filesystem>
|
2022-01-18 00:10:10 +01:00
|
|
|
#include <dlfcn.h>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
Plugin::Plugin(const fs::path &path) : m_path(path) {
|
2022-01-16 01:51:31 +01:00
|
|
|
this->m_handle = dlopen(path.string().c_str(), RTLD_LAZY);
|
2021-02-19 13:22:12 +01:00
|
|
|
|
2021-06-18 20:09:36 +02:00
|
|
|
if (this->m_handle == nullptr) {
|
2021-08-29 22:15:18 +02:00
|
|
|
log::error("dlopen failed: {}", dlerror());
|
2020-12-22 18:10:01 +01:00
|
|
|
return;
|
2021-06-18 20:09:36 +02:00
|
|
|
}
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2021-02-19 13:22:12 +01:00
|
|
|
auto pluginName = fs::path(path).stem().string();
|
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
this->m_initializePluginFunction = getPluginFunction<InitializePluginFunc>("initializePlugin");
|
|
|
|
this->m_getPluginNameFunction = getPluginFunction<GetPluginNameFunc>("getPluginName");
|
|
|
|
this->m_getPluginAuthorFunction = getPluginFunction<GetPluginAuthorFunc>("getPluginAuthor");
|
|
|
|
this->m_getPluginDescriptionFunction = getPluginFunction<GetPluginDescriptionFunc>("getPluginDescription");
|
|
|
|
this->m_getCompatibleVersionFunction = getPluginFunction<GetCompatibleVersionFunc>("getCompatibleVersion");
|
|
|
|
this->m_setImGuiContextFunction = getPluginFunction<SetImGuiContextFunc>("setImGuiContext");
|
|
|
|
this->m_isBuiltinPluginFunction = getPluginFunction<IsBuiltinPluginFunc>("isBuiltinPlugin");
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 13:22:12 +01:00
|
|
|
Plugin::Plugin(Plugin &&other) noexcept {
|
2021-02-07 14:57:13 +01:00
|
|
|
this->m_handle = other.m_handle;
|
2022-02-01 22:09:44 +01:00
|
|
|
this->m_path = std::move(other.m_path);
|
|
|
|
|
|
|
|
this->m_initializePluginFunction = other.m_initializePluginFunction;
|
|
|
|
this->m_getPluginNameFunction = other.m_getPluginNameFunction;
|
|
|
|
this->m_getPluginAuthorFunction = other.m_getPluginAuthorFunction;
|
|
|
|
this->m_getPluginDescriptionFunction = other.m_getPluginDescriptionFunction;
|
|
|
|
this->m_getCompatibleVersionFunction = other.m_getCompatibleVersionFunction;
|
|
|
|
this->m_setImGuiContextFunction = other.m_setImGuiContextFunction;
|
|
|
|
this->m_isBuiltinPluginFunction = other.m_isBuiltinPluginFunction;
|
|
|
|
|
|
|
|
other.m_handle = nullptr;
|
|
|
|
other.m_initializePluginFunction = nullptr;
|
|
|
|
other.m_getPluginNameFunction = nullptr;
|
|
|
|
other.m_getPluginAuthorFunction = nullptr;
|
|
|
|
other.m_getPluginDescriptionFunction = nullptr;
|
|
|
|
other.m_getCompatibleVersionFunction = nullptr;
|
|
|
|
other.m_setImGuiContextFunction = nullptr;
|
|
|
|
other.m_isBuiltinPluginFunction = nullptr;
|
2021-02-07 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
Plugin::~Plugin() {
|
2021-02-26 12:49:33 +01:00
|
|
|
if (this->m_handle != nullptr)
|
|
|
|
dlclose(this->m_handle);
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
bool Plugin::initializePlugin() const {
|
2022-01-23 23:28:56 +01:00
|
|
|
const auto requestedVersion = getCompatibleVersion();
|
|
|
|
if (requestedVersion != IMHEX_VERSION) {
|
|
|
|
log::error("Refused to load plugin '{}' which was built for a different version of ImHex: '{}'", this->m_path.filename().string(), requestedVersion);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
if (this->m_initializePluginFunction != nullptr) {
|
2021-01-12 23:28:41 +01:00
|
|
|
this->m_initializePluginFunction();
|
2022-01-17 20:06:00 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-23 23:28:56 +01:00
|
|
|
|
|
|
|
this->m_initialized = true;
|
|
|
|
return true;
|
2021-02-19 13:22:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Plugin::getPluginName() const {
|
|
|
|
if (this->m_getPluginNameFunction != nullptr)
|
|
|
|
return this->m_getPluginNameFunction();
|
|
|
|
else
|
2021-05-29 21:51:00 +02:00
|
|
|
return hex::format("Unknown Plugin @ 0x{0:016X}", reinterpret_cast<intptr_t>(this->m_handle));
|
2021-02-19 13:22:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 "";
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 23:28:56 +01:00
|
|
|
std::string Plugin::getCompatibleVersion() const {
|
|
|
|
if (this->m_getCompatibleVersionFunction != nullptr)
|
|
|
|
return this->m_getCompatibleVersionFunction();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
[[nodiscard]] bool Plugin::isBuiltinPlugin() const {
|
|
|
|
if (this->m_isBuiltinPluginFunction != nullptr)
|
|
|
|
return this->m_isBuiltinPluginFunction();
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const fs::path &Plugin::getPath() const {
|
2022-01-17 20:06:00 +01:00
|
|
|
return this->m_path;
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:28:56 +01:00
|
|
|
bool Plugin::isLoaded() const {
|
|
|
|
return this->m_initialized;
|
2022-01-18 00:10:10 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
void *Plugin::getPluginFunction(const std::string &symbol) {
|
2022-01-23 23:28:56 +01:00
|
|
|
return dlsym(this->m_handle, symbol.c_str());
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
fs::path PluginManager::s_pluginFolder;
|
|
|
|
std::vector<Plugin> PluginManager::s_plugins;
|
|
|
|
|
2022-01-13 14:33:30 +01:00
|
|
|
bool PluginManager::load(const fs::path &pluginFolder) {
|
|
|
|
if (!fs::exists(pluginFolder))
|
2021-04-20 21:46:48 +02:00
|
|
|
return false;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
PluginManager::s_pluginFolder = pluginFolder;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
for (auto &pluginPath : fs::directory_iterator(pluginFolder)) {
|
2021-02-07 13:40:47 +01:00
|
|
|
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug")
|
2021-04-20 21:46:48 +02:00
|
|
|
PluginManager::s_plugins.emplace_back(pluginPath.path().string());
|
2021-01-12 16:50:15 +01:00
|
|
|
}
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
if (PluginManager::s_plugins.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
void PluginManager::unload() {
|
|
|
|
PluginManager::s_plugins.clear();
|
|
|
|
PluginManager::s_pluginFolder.clear();
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
void PluginManager::reload() {
|
|
|
|
PluginManager::unload();
|
|
|
|
PluginManager::load(PluginManager::s_pluginFolder);
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|