2021-04-20 19:46:48 +00:00
|
|
|
#include "helpers/plugin_manager.hpp"
|
2020-12-22 17:10:01 +00:00
|
|
|
|
2021-08-29 20:15:18 +00:00
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
|
2020-12-22 17:10:01 +00:00
|
|
|
#include <filesystem>
|
2022-01-17 23:10:10 +00:00
|
|
|
#include <dlfcn.h>
|
2020-12-22 17:10:01 +00:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-02-07 13:57:13 +00:00
|
|
|
// hex::plugin::<pluginName>::internal::initializePlugin()
|
2021-03-03 18:58:22 +00:00
|
|
|
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-20 22:52:11 +00:00
|
|
|
constexpr auto SetImGuiContextSymbol = "_ZN3hex6plugin{0}{1}8internal15setImGuiContextEP12ImGuiContext";
|
2020-12-22 17:10:01 +00:00
|
|
|
|
2022-01-17 19:06:00 +00:00
|
|
|
Plugin::Plugin(const fs::path &path) : m_path(path) {
|
2022-01-16 00:51:31 +00:00
|
|
|
this->m_handle = dlopen(path.string().c_str(), RTLD_LAZY);
|
2021-02-19 12:22:12 +00:00
|
|
|
|
2021-06-18 18:09:36 +00:00
|
|
|
if (this->m_handle == nullptr) {
|
2021-08-29 20:15:18 +00:00
|
|
|
log::error("dlopen failed: {}", dlerror());
|
2020-12-22 17:10:01 +00:00
|
|
|
return;
|
2021-06-18 18:09:36 +00:00
|
|
|
}
|
2020-12-22 17:10:01 +00:00
|
|
|
|
2021-02-19 12:22:12 +00:00
|
|
|
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-20 22:52:11 +00:00
|
|
|
this->m_setImGuiContextFunction = getPluginFunction<SetImGuiContextFunc>(pluginName, SetImGuiContextSymbol);
|
2020-12-22 17:10:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 12:22:12 +00:00
|
|
|
Plugin::Plugin(Plugin &&other) noexcept {
|
2021-02-07 13:57:13 +00:00
|
|
|
this->m_handle = other.m_handle;
|
2022-01-17 19:06:00 +00:00
|
|
|
this->m_path = std::move(other.m_path);
|
|
|
|
|
2021-02-19 12:22:12 +00:00
|
|
|
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-20 22:52:11 +00:00
|
|
|
this->m_setImGuiContextFunction = other.m_setImGuiContextFunction;
|
2021-02-07 13:57:13 +00:00
|
|
|
|
|
|
|
other.m_handle = nullptr;
|
2021-02-19 12:22:12 +00:00
|
|
|
other.m_initializePluginFunction = nullptr;
|
|
|
|
other.m_getPluginNameFunction = nullptr;
|
|
|
|
other.m_getPluginAuthorFunction = nullptr;
|
|
|
|
other.m_getPluginDescriptionFunction = nullptr;
|
2021-08-20 22:52:11 +00:00
|
|
|
other.m_setImGuiContextFunction = nullptr;
|
2021-02-07 13:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 17:10:01 +00:00
|
|
|
Plugin::~Plugin() {
|
2021-02-26 11:49:33 +00:00
|
|
|
if (this->m_handle != nullptr)
|
|
|
|
dlclose(this->m_handle);
|
2020-12-22 17:10:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 19:06:00 +00:00
|
|
|
bool Plugin::initializePlugin() const {
|
|
|
|
if (this->m_initializePluginFunction != nullptr) {
|
2021-01-12 22:28:41 +00:00
|
|
|
this->m_initializePluginFunction();
|
2022-01-17 19:06:00 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-19 12:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Plugin::getPluginName() const {
|
|
|
|
if (this->m_getPluginNameFunction != nullptr)
|
|
|
|
return this->m_getPluginNameFunction();
|
|
|
|
else
|
2021-05-29 19:51:00 +00:00
|
|
|
return hex::format("Unknown Plugin @ 0x{0:016X}", reinterpret_cast<intptr_t>(this->m_handle));
|
2021-02-19 12:22:12 +00: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 17:10:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 22:52:11 +00:00
|
|
|
void Plugin::setImGuiContext(ImGuiContext *ctx) const {
|
|
|
|
if (this->m_setImGuiContextFunction != nullptr)
|
|
|
|
this->m_setImGuiContextFunction(ctx);
|
|
|
|
}
|
|
|
|
|
2022-01-17 23:10:10 +00:00
|
|
|
const fs::path& Plugin::getPath() const {
|
2022-01-17 19:06:00 +00:00
|
|
|
return this->m_path;
|
|
|
|
}
|
|
|
|
|
2022-01-17 23:10:10 +00:00
|
|
|
void* Plugin::getPluginFunction(const std::string &pluginName, const std::string &symbol) {
|
|
|
|
auto symbolName = hex::format(symbol.data(), pluginName.length(), pluginName.data());
|
|
|
|
return dlsym(this->m_handle, symbolName.c_str());
|
|
|
|
}
|
|
|
|
|
2022-01-17 19:06:00 +00:00
|
|
|
|
|
|
|
|
2022-01-13 13:33:30 +00:00
|
|
|
bool PluginManager::load(const fs::path &pluginFolder) {
|
|
|
|
if (!fs::exists(pluginFolder))
|
2021-04-20 19:46:48 +00:00
|
|
|
return false;
|
2020-12-22 17:10:01 +00:00
|
|
|
|
2021-04-20 19:46:48 +00:00
|
|
|
PluginManager::s_pluginFolder = pluginFolder;
|
2020-12-22 17:10:01 +00:00
|
|
|
|
2022-01-13 13:33:30 +00:00
|
|
|
for (auto& pluginPath : fs::directory_iterator(pluginFolder)) {
|
2021-02-07 12:40:47 +00:00
|
|
|
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug")
|
2021-04-20 19:46:48 +00:00
|
|
|
PluginManager::s_plugins.emplace_back(pluginPath.path().string());
|
2021-01-12 15:50:15 +00:00
|
|
|
}
|
2021-04-20 19:46:48 +00:00
|
|
|
|
|
|
|
if (PluginManager::s_plugins.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2020-12-22 17:10:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 19:46:48 +00:00
|
|
|
void PluginManager::unload() {
|
|
|
|
PluginManager::s_plugins.clear();
|
|
|
|
PluginManager::s_pluginFolder.clear();
|
2020-12-22 17:10:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 19:46:48 +00:00
|
|
|
void PluginManager::reload() {
|
|
|
|
PluginManager::unload();
|
|
|
|
PluginManager::load(PluginManager::s_pluginFolder);
|
2020-12-22 17:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|