build/plugin: Make plugins no longer depend on their file name

This commit is contained in:
WerWolv
2022-01-23 23:28:56 +01:00
parent b3a8d02d19
commit 541c0d7547
6 changed files with 85 additions and 53 deletions

View File

@@ -7,13 +7,6 @@
namespace hex {
// 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";
constexpr auto SetImGuiContextSymbol = "_ZN3hex6plugin{0}{1}8internal15setImGuiContextEP12ImGuiContext";
Plugin::Plugin(const fs::path &path) : m_path(path) {
this->m_handle = dlopen(path.string().c_str(), RTLD_LAZY);
@@ -24,11 +17,12 @@ namespace hex {
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);
this->m_setImGuiContextFunction = getPluginFunction<SetImGuiContextFunc>(pluginName, SetImGuiContextSymbol);
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");
}
Plugin::Plugin(Plugin &&other) noexcept {
@@ -39,6 +33,7 @@ namespace hex {
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;
other.m_handle = nullptr;
@@ -46,6 +41,7 @@ namespace hex {
other.m_getPluginNameFunction = nullptr;
other.m_getPluginAuthorFunction = nullptr;
other.m_getPluginDescriptionFunction = nullptr;
other.m_getCompatibleVersionFunction = nullptr;
other.m_setImGuiContextFunction = nullptr;
}
@@ -55,12 +51,20 @@ namespace hex {
}
bool Plugin::initializePlugin() const {
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;
}
if (this->m_initializePluginFunction != nullptr) {
this->m_initializePluginFunction();
return true;
} else {
return false;
}
this->m_initialized = true;
return true;
}
std::string Plugin::getPluginName() const {
@@ -84,6 +88,13 @@ namespace hex {
return "";
}
std::string Plugin::getCompatibleVersion() const {
if (this->m_getCompatibleVersionFunction != nullptr)
return this->m_getCompatibleVersionFunction();
else
return "";
}
void Plugin::setImGuiContext(ImGuiContext *ctx) const {
if (this->m_setImGuiContextFunction != nullptr)
this->m_setImGuiContextFunction(ctx);
@@ -93,12 +104,15 @@ namespace hex {
return this->m_path;
}
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());
bool Plugin::isLoaded() const {
return this->m_initialized;
}
void* Plugin::getPluginFunction(const std::string &symbol) {
return dlsym(this->m_handle, symbol.c_str());
}
bool PluginManager::load(const fs::path &pluginFolder) {
if (!fs::exists(pluginFolder))