2020-12-22 18:10:01 +01:00
|
|
|
#include "helpers/plugin_handler.hpp"
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-01-12 23:28:41 +01:00
|
|
|
// hex::plugin::internal::initializePlugin()
|
|
|
|
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin8internal16initializePluginEv";
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
Plugin::Plugin(std::string_view path) {
|
|
|
|
this->m_handle = dlopen(path.data(), RTLD_LAZY);
|
|
|
|
|
|
|
|
if (this->m_handle == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
this->m_initializePluginFunction = reinterpret_cast<InitializePluginFunc>(dlsym(this->m_handle, InitializePluginSymbol));
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Plugin::~Plugin() {
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 23:28:41 +01:00
|
|
|
void Plugin::initializePlugin() const {
|
2020-12-27 15:39:06 +01:00
|
|
|
if (this->m_initializePluginFunction != nullptr)
|
2021-01-12 23:28:41 +01:00
|
|
|
this->m_initializePluginFunction();
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginHandler::load(std::string_view pluginFolder) {
|
|
|
|
PluginHandler::unload();
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
if (pluginPath.is_regular_file())
|
|
|
|
PluginHandler::s_plugins.emplace_back(pluginPath.path().string());
|
|
|
|
}
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginHandler::unload() {
|
|
|
|
PluginHandler::s_plugins.clear();
|
|
|
|
PluginHandler::s_pluginFolder.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginHandler::reload() {
|
|
|
|
PluginHandler::load(PluginHandler::s_pluginFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|