2020-12-22 18:10:01 +01:00
|
|
|
#include "helpers/plugin_handler.hpp"
|
|
|
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-02-07 13:40:47 +01:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
2021-02-07 14:57:13 +01:00
|
|
|
// hex::plugin::<pluginName>::internal::initializePlugin()
|
2021-02-19 13:22:12 +01:00
|
|
|
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin%d%s8internal16initializePluginEv";
|
|
|
|
constexpr auto GetPluginNameSymbol = "_ZN3hex6plugin%d%s8internal13getPluginNameEv";
|
|
|
|
constexpr auto GetPluginAuthorSymbol = "_ZN3hex6plugin%d%s8internal15getPluginAuthorEv";
|
|
|
|
constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin%d%s8internal20getPluginDescriptionEv";
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
Plugin::Plugin(std::string_view path) {
|
|
|
|
this->m_handle = dlopen(path.data(), RTLD_LAZY);
|
2021-02-19 13:22:12 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
if (this->m_handle == nullptr)
|
|
|
|
return;
|
|
|
|
|
2021-02-19 13:22:12 +01: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);
|
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;
|
2021-02-19 13:22:12 +01: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-02-07 14:57:13 +01:00
|
|
|
|
|
|
|
other.m_handle = nullptr;
|
2021-02-19 13:22:12 +01:00
|
|
|
other.m_initializePluginFunction = nullptr;
|
|
|
|
other.m_getPluginNameFunction = nullptr;
|
|
|
|
other.m_getPluginAuthorFunction = nullptr;
|
|
|
|
other.m_getPluginDescriptionFunction = 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
|
|
|
}
|
|
|
|
|
2021-01-12 23:28:41 +01:00
|
|
|
void Plugin::initializePlugin() const {
|
2021-02-19 13:22:12 +01:00
|
|
|
if (this->m_initializePluginFunction != nullptr)
|
2021-01-12 23:28:41 +01:00
|
|
|
this->m_initializePluginFunction();
|
2021-02-19 13:22:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Plugin::getPluginName() const {
|
|
|
|
if (this->m_getPluginNameFunction != nullptr)
|
|
|
|
return this->m_getPluginNameFunction();
|
|
|
|
else
|
|
|
|
return hex::format("Unknown Plugin @ 0x016llX", 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 "";
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginHandler::load(std::string_view pluginFolder) {
|
|
|
|
if (!std::filesystem::exists(pluginFolder))
|
|
|
|
throw std::runtime_error("Failed to find plugin folder");
|
|
|
|
|
|
|
|
PluginHandler::s_pluginFolder = pluginFolder;
|
|
|
|
|
2021-01-12 16:50:15 +01:00
|
|
|
for (auto& pluginPath : std::filesystem::directory_iterator(pluginFolder)) {
|
2021-02-07 13:40:47 +01:00
|
|
|
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug")
|
2021-02-07 14:57:13 +01:00
|
|
|
PluginHandler::s_plugins.emplace_back(pluginPath.path().string());
|
2021-01-12 16:50:15 +01:00
|
|
|
}
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginHandler::unload() {
|
|
|
|
PluginHandler::s_plugins.clear();
|
|
|
|
PluginHandler::s_pluginFolder.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginHandler::reload() {
|
2021-02-07 13:40:47 +01:00
|
|
|
PluginHandler::unload();
|
2020-12-22 18:10:01 +01:00
|
|
|
PluginHandler::load(PluginHandler::s_pluginFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|