1
0
mirror of synced 2024-12-12 16:01:06 +01:00
ImHex/lib/libimhex/source/api/plugin_manager.cpp

301 lines
10 KiB
C++
Raw Normal View History

#include <hex/api/plugin_manager.hpp>
#include <hex/api/imhex_api.hpp>
2021-08-29 22:15:18 +02:00
#include <hex/helpers/logger.hpp>
2023-11-10 20:47:08 +01:00
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/utils.hpp>
2021-08-29 22:15:18 +02:00
2023-03-12 18:43:05 +01:00
#include <wolv/utils/string.hpp>
#include <filesystem>
2023-11-30 10:22:15 +01:00
#if defined(OS_WINDOWS)
#include <windows.h>
#else
#include <dlfcn.h>
#endif
namespace hex {
Plugin::Plugin(const std::fs::path &path) : m_path(path) {
log::info("Loading plugin '{}'", wolv::util::toUTF8String(path.filename()));
#if defined(OS_WINDOWS)
2023-12-19 13:10:25 +01:00
m_handle = uintptr_t(LoadLibraryW(path.c_str()));
2023-12-19 13:10:25 +01:00
if (m_handle == uintptr_t(INVALID_HANDLE_VALUE) || m_handle == 0) {
log::error("Loading plugin '{}' failed: {} {}!", wolv::util::toUTF8String(path.filename()), ::GetLastError(), std::system_category().message(::GetLastError()));
return;
}
#else
2023-12-19 13:10:25 +01:00
m_handle = uintptr_t(dlopen(wolv::util::toUTF8String(path).c_str(), RTLD_LAZY));
2023-12-19 13:10:25 +01:00
if (m_handle == 0) {
log::error("Loading plugin '{}' failed: {}!", wolv::util::toUTF8String(path.filename()), dlerror());
return;
}
#endif
2023-12-19 13:10:25 +01:00
m_functions.initializePluginFunction = getPluginFunction<PluginFunctions::InitializePluginFunc>("initializePlugin");
m_functions.initializeLibraryFunction = getPluginFunction<PluginFunctions::InitializePluginFunc>("initializeLibrary");
2023-12-19 13:10:25 +01:00
m_functions.getPluginNameFunction = getPluginFunction<PluginFunctions::GetPluginNameFunc>("getPluginName");
m_functions.getLibraryNameFunction = getPluginFunction<PluginFunctions::GetLibraryNameFunc>("getLibraryName");
2023-12-19 13:10:25 +01:00
m_functions.getPluginAuthorFunction = getPluginFunction<PluginFunctions::GetPluginAuthorFunc>("getPluginAuthor");
m_functions.getPluginDescriptionFunction = getPluginFunction<PluginFunctions::GetPluginDescriptionFunc>("getPluginDescription");
m_functions.getCompatibleVersionFunction = getPluginFunction<PluginFunctions::GetCompatibleVersionFunc>("getCompatibleVersion");
m_functions.setImGuiContextFunction = getPluginFunction<PluginFunctions::SetImGuiContextFunc>("setImGuiContext");
m_functions.getSubCommandsFunction = getPluginFunction<PluginFunctions::GetSubCommandsFunc>("getSubCommands");
m_functions.getFeaturesFunction = getPluginFunction<PluginFunctions::GetSubCommandsFunc>("getFeatures");
}
Plugin::Plugin(const std::string &name, const hex::PluginFunctions &functions) {
m_handle = 0;
m_functions = functions;
m_path = name;
}
Plugin::Plugin(Plugin &&other) noexcept {
2023-12-19 13:10:25 +01:00
m_handle = other.m_handle;
2023-11-30 10:22:15 +01:00
other.m_handle = 0;
m_path = std::move(other.m_path);
m_functions = other.m_functions;
other.m_functions = {};
}
Plugin& Plugin::operator=(Plugin &&other) noexcept {
m_handle = other.m_handle;
other.m_handle = 0;
m_path = std::move(other.m_path);
2022-02-01 22:09:44 +01:00
2023-12-19 13:10:25 +01:00
m_functions = other.m_functions;
other.m_functions = {};
return *this;
}
Plugin::~Plugin() {
#if defined(OS_WINDOWS)
2023-12-19 13:10:25 +01:00
if (m_handle != 0)
if (FreeLibrary(HMODULE(m_handle)) == FALSE) {
log::error("Error when unloading plugin '{}': {}!", wolv::util::toUTF8String(m_path.filename()), std::system_category().message(::GetLastError()));
}
#else
2023-12-19 13:10:25 +01:00
if (m_handle != 0)
dlclose(reinterpret_cast<void*>(m_handle));
#endif
}
bool Plugin::initializePlugin() const {
2023-12-19 13:10:25 +01:00
const auto pluginName = wolv::util::toUTF8String(m_path.filename());
if (this->isLibraryPlugin()) {
m_functions.initializeLibraryFunction();
log::info("Library '{}' initialized successfully", pluginName);
m_initialized = true;
return true;
}
const auto requestedVersion = getCompatibleVersion();
if (requestedVersion != ImHexApi::System::getImHexVersion()) {
if (requestedVersion.empty()) {
2023-12-19 13:10:25 +01:00
log::warn("Plugin '{}' did not specify a compatible version, assuming it is compatible with the current version of ImHex.", wolv::util::toUTF8String(m_path.filename()));
} else {
2023-12-19 13:10:25 +01:00
log::error("Refused to load plugin '{}' which was built for a different version of ImHex: '{}'", wolv::util::toUTF8String(m_path.filename()), requestedVersion);
return false;
}
}
2023-12-19 13:10:25 +01:00
if (m_functions.initializePluginFunction != nullptr) {
try {
2023-12-19 13:10:25 +01:00
m_functions.initializePluginFunction();
} catch (const std::exception &e) {
log::error("Plugin '{}' threw an exception on init: {}", pluginName, e.what());
return false;
} catch (...) {
log::error("Plugin '{}' threw an exception on init", pluginName);
return false;
}
} else {
log::error("Plugin '{}' does not have a proper entrypoint", pluginName);
return false;
}
2023-12-01 14:07:10 +01:00
log::info("Plugin '{}' initialized successfully", pluginName);
2023-12-19 13:10:25 +01:00
m_initialized = true;
return true;
}
std::string Plugin::getPluginName() const {
2023-12-27 16:33:49 +01:00
if (m_functions.getPluginNameFunction != nullptr) {
2023-12-19 13:10:25 +01:00
return m_functions.getPluginNameFunction();
2023-12-27 16:33:49 +01:00
} else {
if (this->isLibraryPlugin())
return m_functions.getLibraryNameFunction();
else
return hex::format("Unknown Plugin @ 0x{0:016X}", m_handle);
}
}
std::string Plugin::getPluginAuthor() const {
2023-12-19 13:10:25 +01:00
if (m_functions.getPluginAuthorFunction != nullptr)
return m_functions.getPluginAuthorFunction();
else
return "Unknown";
}
std::string Plugin::getPluginDescription() const {
2023-12-19 13:10:25 +01:00
if (m_functions.getPluginDescriptionFunction != nullptr)
return m_functions.getPluginDescriptionFunction();
else
return "";
}
std::string Plugin::getCompatibleVersion() const {
2023-12-19 13:10:25 +01:00
if (m_functions.getCompatibleVersionFunction != nullptr)
return m_functions.getCompatibleVersionFunction();
else
return "";
}
2021-08-21 00:52:11 +02:00
void Plugin::setImGuiContext(ImGuiContext *ctx) const {
2023-12-19 13:10:25 +01:00
if (m_functions.setImGuiContextFunction != nullptr)
m_functions.setImGuiContextFunction(ctx);
2021-08-21 00:52:11 +02:00
}
const std::fs::path &Plugin::getPath() const {
2023-12-19 13:10:25 +01:00
return m_path;
}
bool Plugin::isValid() const {
2023-12-24 13:14:51 +01:00
return m_handle != 0 || m_functions.initializeLibraryFunction != nullptr || m_functions.initializePluginFunction != nullptr;
}
bool Plugin::isLoaded() const {
2023-12-19 13:10:25 +01:00
return m_initialized;
}
std::span<SubCommand> Plugin::getSubCommands() const {
2023-12-19 13:10:25 +01:00
if (m_functions.getSubCommandsFunction != nullptr) {
const auto result = m_functions.getSubCommandsFunction();
if (result == nullptr)
return { };
2023-11-10 20:47:08 +01:00
return *static_cast<std::vector<SubCommand>*>(result);
2023-12-27 16:33:49 +01:00
} else {
return { };
2023-12-27 16:33:49 +01:00
}
}
std::span<Feature> Plugin::getFeatures() const {
if (m_functions.getFeaturesFunction != nullptr) {
const auto result = m_functions.getFeaturesFunction();
if (result == nullptr)
return { };
return *static_cast<std::vector<Feature>*>(result);
} else {
return { };
}
}
bool Plugin::isLibraryPlugin() const {
return m_functions.initializeLibraryFunction != nullptr &&
m_functions.initializePluginFunction == nullptr;
}
2023-11-10 20:47:08 +01:00
void *Plugin::getPluginFunction(const std::string &symbol) const {
#if defined(OS_WINDOWS)
2023-12-19 13:10:25 +01:00
return reinterpret_cast<void *>(GetProcAddress(HMODULE(m_handle), symbol.c_str()));
#else
2023-12-19 13:10:25 +01:00
return dlsym(reinterpret_cast<void*>(m_handle), symbol.c_str());
#endif
}
bool PluginManager::load(const std::fs::path &pluginFolder) {
if (!wolv::io::fs::exists(pluginFolder))
return false;
getPluginPaths().push_back(pluginFolder);
// Load library plugins first
for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexpluglib") {
if (!isPluginLoaded(pluginPath.path())) {
getPlugins().emplace_back(pluginPath.path());
}
}
}
// Load regular plugins afterwards
for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug") {
if (!isPluginLoaded(pluginPath.path())) {
getPlugins().emplace_back(pluginPath.path());
}
}
2021-01-12 16:50:15 +01:00
}
std::erase_if(getPlugins(), [](const Plugin &plugin) {
return !plugin.isValid();
});
if (getPlugins().empty())
return false;
return true;
}
void PluginManager::unload() {
getPluginPaths().clear();
// Unload plugins in reverse order
auto &plugins = getPlugins();
const auto pluginCount = plugins.size();
for (size_t i = 0; i < pluginCount; i++) {
2024-01-09 10:54:53 +01:00
auto &plugin = plugins[pluginCount - 1 - i];
if (plugin.isLoaded()) {
log::info("Trying to unload plugin '{}'", plugin.getPluginName());
}
plugins.pop_back();
}
}
void PluginManager::addPlugin(const std::string &name, hex::PluginFunctions functions) {
getPlugins().emplace_back(name, functions);
}
std::vector<Plugin> &PluginManager::getPlugins() {
static std::vector<Plugin> plugins;
return plugins;
}
std::vector<std::fs::path> &PluginManager::getPluginPaths() {
static std::vector<std::fs::path> pluginPaths;
return pluginPaths;
}
bool PluginManager::isPluginLoaded(const std::fs::path &path) {
return std::ranges::any_of(getPlugins(), [&path](const Plugin &plugin) {
return plugin.getPath().filename() == path.filename();
});
}
}