refactor: Rework features that use external libraries into optional plugins (#1470)

This commit is contained in:
Nik
2023-12-23 21:09:41 +01:00
committed by GitHub
parent 84bfd10416
commit 61bfe10bc2
149 changed files with 2940 additions and 2390 deletions

View File

@@ -16,25 +16,27 @@
namespace hex {
Plugin::Plugin(const std::fs::path &path) : m_path(path) {
Plugin::Plugin(const std::fs::path &path, bool libraryPlugin) : m_path(path), m_libraryPlugin(libraryPlugin) {
#if defined(OS_WINDOWS)
m_handle = uintptr_t(LoadLibraryW(path.c_str()));
if (m_handle == uintptr_t(INVALID_HANDLE_VALUE) || m_handle == 0) {
log::error("LoadLibraryW failed: {}!", std::system_category().message(::GetLastError()));
log::error("Loading plugin '{}' failed: {} {}!", wolv::util::toUTF8String(path.filename()), ::GetLastError(), std::system_category().message(::GetLastError()));
return;
}
#else
m_handle = uintptr_t(dlopen(wolv::util::toUTF8String(path).c_str(), RTLD_LAZY));
if (m_handle == 0) {
log::error("dlopen failed: {}!", dlerror());
log::error("Loading plugin '{}' failed: {}!", wolv::util::toUTF8String(path.filename()), dlerror());
return;
}
#endif
log::info("Loaded plugin '{}'", wolv::util::toUTF8String(path.filename()));
m_functions.initializePluginFunction = getPluginFunction<PluginFunctions::InitializePluginFunc>("initializePlugin");
m_functions.initializeLibraryFunction = getPluginFunction<PluginFunctions::InitializePluginFunc>("initializeLibrary");
m_functions.getPluginNameFunction = getPluginFunction<PluginFunctions::GetPluginNameFunc>("getPluginName");
m_functions.getPluginAuthorFunction = getPluginFunction<PluginFunctions::GetPluginAuthorFunc>("getPluginAuthor");
m_functions.getPluginDescriptionFunction = getPluginFunction<PluginFunctions::GetPluginDescriptionFunc>("getPluginDescription");
@@ -44,9 +46,10 @@ namespace hex {
m_functions.getSubCommandsFunction = getPluginFunction<PluginFunctions::GetSubCommandsFunc>("getSubCommands");
}
Plugin::Plugin(hex::PluginFunctions functions) {
m_handle = 0;
m_functions = functions;
Plugin::Plugin(hex::PluginFunctions functions, bool libraryPlugin) {
m_handle = 0;
m_functions = functions;
m_libraryPlugin = libraryPlugin;
}
@@ -54,6 +57,8 @@ namespace hex {
m_handle = other.m_handle;
other.m_handle = 0;
m_libraryPlugin = other.m_libraryPlugin;
m_path = std::move(other.m_path);
m_functions = other.m_functions;
@@ -73,6 +78,12 @@ namespace hex {
bool Plugin::initializePlugin() const {
const auto pluginName = wolv::util::toUTF8String(m_path.filename());
if (m_functions.initializeLibraryFunction != nullptr) {
m_functions.initializeLibraryFunction();
log::info("Library plugin '{}' initialized successfully", pluginName);
return true;
}
const auto requestedVersion = getCompatibleVersion();
if (requestedVersion != ImHexApi::System::getImHexVersion()) {
if (requestedVersion.empty()) {
@@ -107,8 +118,12 @@ namespace hex {
std::string Plugin::getPluginName() const {
if (m_functions.getPluginNameFunction != nullptr)
return m_functions.getPluginNameFunction();
else
return hex::format("Unknown Plugin @ 0x{0:016X}", m_handle);
else {
if (this->isLibraryPlugin())
return "Library Plugin";
else
return hex::format("Unknown Plugin @ 0x{0:016X}", m_handle);
}
}
std::string Plugin::getPluginAuthor() const {
@@ -132,6 +147,7 @@ namespace hex {
return "";
}
void Plugin::setImGuiContext(ImGuiContext *ctx) const {
if (m_functions.setImGuiContextFunction != nullptr)
m_functions.setImGuiContextFunction(ctx);
@@ -175,9 +191,16 @@ namespace hex {
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")
getPlugins().emplace_back(pluginPath.path(), true);
}
// Load regular plugins afterwards
for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug")
getPlugins().emplace_back(pluginPath.path());
getPlugins().emplace_back(pluginPath.path(), false);
}
if (getPlugins().empty())
@@ -200,7 +223,7 @@ namespace hex {
}
void PluginManager::addPlugin(hex::PluginFunctions functions) {
getPlugins().emplace_back(functions);
getPlugins().emplace_back(functions, false);
}
std::vector<Plugin> &PluginManager::getPlugins() {