1
0
mirror of synced 2024-11-28 09:30:51 +01:00

ui/api: Added loaded plugin information to welcome screen

This commit is contained in:
WerWolv 2021-02-19 13:22:12 +01:00
parent 89643d1538
commit 0da508594b
8 changed files with 125 additions and 22 deletions

View File

@ -4,8 +4,10 @@
#include <hex/views/view.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/utils.hpp>
#include <string_view>
#include <dlfcn.h>
namespace hex {
@ -13,17 +15,33 @@ namespace hex {
public:
Plugin(std::string_view path);
Plugin(const Plugin&) = delete;
Plugin(Plugin &&other);
Plugin(Plugin &&other) noexcept;
~Plugin();
void initializePlugin() const;
std::string getPluginName() const;
std::string getPluginAuthor() const;
std::string getPluginDescription() const;
private:
using InitializePluginFunc = void(*)();
using InitializePluginFunc = void(*)();
using GetPluginNameFunc = const char*(*)();
using GetPluginAuthorFunc = const char*(*)();
using GetPluginDescriptionFunc = const char*(*)();
void *m_handle = nullptr;
InitializePluginFunc m_initializePluginFunction = nullptr;
InitializePluginFunc m_initializePluginFunction = nullptr;
GetPluginNameFunc m_getPluginNameFunction = nullptr;
GetPluginAuthorFunc m_getPluginAuthorFunction = nullptr;
GetPluginDescriptionFunc m_getPluginDescriptionFunction = nullptr;
template<typename T>
auto getPluginFunction(std::string_view pluginName, std::string_view symbol) {
auto symbolName = hex::format(symbol.data(), pluginName.length(), pluginName.data());
return reinterpret_cast<T>(dlsym(this->m_handle, symbolName.c_str()));
};
};
class PluginHandler {

View File

@ -24,6 +24,10 @@ namespace hex::plugin::builtin {
{ "hex.welcome.help.repo.link", "https://github.com/WerWolv/ImHex" },
{ "hex.welcome.help.gethelp", "Get Help" },
{ "hex.welcome.help.gethelp.link", "https://github.com/WerWolv/ImHex/discussions/categories/get-help" },
{ "hex.welcome.header.plugins", "Loaded Plugins" },
{ "hex.welcome.plugins.plugin", "Plugin" },
{ "hex.welcome.plugins.author", "Author" },
{ "hex.welcome.plugins.desc", "Description" },
{ "hex.welcome.header.customize", "Customize" },
{ "hex.welcome.customize.settings.title", "Settings" },
{ "hex.welcome.customize.settings.desc", "Change preferences of ImHex" },

View File

@ -13,7 +13,7 @@ namespace hex::plugin::builtin {
}
IMHEX_PLUGIN_SETUP {
IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {
using namespace hex::plugin::builtin;

View File

@ -15,7 +15,7 @@ public:
}
};
IMHEX_PLUGIN_SETUP {
IMHEX_PLUGIN_SETUP("Example", "WerWolv", "Example plugin used as template for plugin devs") {
ContentRegistry::Views::add<ViewExample>();

View File

@ -11,9 +11,14 @@
#include <hex/helpers/shared_data.hpp>
#include <hex/data_processor/node.hpp>
#define IMHEX_PLUGIN_SETUP IMHEX_PLUGIN_SETUP_IMPL(IMHEX_PLUGIN_NAME)
#define IMHEX_PLUGIN_SETUP(name, author, description) IMHEX_PLUGIN_SETUP_IMPL(IMHEX_PLUGIN_NAME, name, author, description)
#define IMHEX_PLUGIN_SETUP_IMPL(name) namespace hex::plugin::name::internal { \
[[gnu::visibility("default")]] void initializePlugin(); \
} \
void hex::plugin::name::internal::initializePlugin()
#define IMHEX_PLUGIN_SETUP_IMPL(namespaceName, name, author, description) \
namespace hex::plugin::namespaceName::internal { \
[[gnu::visibility("default")]] void initializePlugin(); \
\
[[gnu::visibility("default")]] const char* getPluginName() { return name; } \
[[gnu::visibility("default")]] const char* getPluginAuthor() { return author; } \
[[gnu::visibility("default")]] const char* getPluginDescription() { return description; } \
} \
void hex::plugin::namespaceName::internal::initializePlugin()

View File

@ -1,7 +1,5 @@
#include "helpers/plugin_handler.hpp"
#include <dlfcn.h>
#include <filesystem>
namespace hex {
@ -9,24 +7,37 @@ namespace hex {
namespace fs = std::filesystem;
// hex::plugin::<pluginName>::internal::initializePlugin()
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin%d%s8internal16initializePluginEv";
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin%d%s8internal16initializePluginEv";
constexpr auto GetPluginNameSymbol = "_ZN3hex6plugin%d%s8internal13getPluginNameEv";
constexpr auto GetPluginAuthorSymbol = "_ZN3hex6plugin%d%s8internal15getPluginAuthorEv";
constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin%d%s8internal20getPluginDescriptionEv";
Plugin::Plugin(std::string_view path) {
auto fileName = fs::path(path).stem();
auto symbolName = hex::format(InitializePluginSymbol, fileName.string().length(), fileName.string().c_str());
this->m_handle = dlopen(path.data(), RTLD_LAZY);
if (this->m_handle == nullptr)
return;
this->m_initializePluginFunction = reinterpret_cast<InitializePluginFunc>(dlsym(this->m_handle, symbolName.c_str()));
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);
}
Plugin::Plugin(Plugin &&other) {
Plugin::Plugin(Plugin &&other) noexcept {
this->m_handle = other.m_handle;
this->m_initializePluginFunction = other.m_initializePluginFunction;
this->m_initializePluginFunction = other.m_initializePluginFunction;
this->m_getPluginNameFunction = other.m_getPluginNameFunction;
this->m_getPluginAuthorFunction = other.m_getPluginAuthorFunction;
this->m_getPluginDescriptionFunction = other.m_getPluginDescriptionFunction;
other.m_handle = nullptr;
other.m_initializePluginFunction = nullptr;
other.m_initializePluginFunction = nullptr;
other.m_getPluginNameFunction = nullptr;
other.m_getPluginAuthorFunction = nullptr;
other.m_getPluginDescriptionFunction = nullptr;
}
Plugin::~Plugin() {
@ -34,9 +45,29 @@ namespace hex {
}
void Plugin::initializePlugin() const {
if (this->m_initializePluginFunction != nullptr) {
if (this->m_initializePluginFunction != nullptr)
this->m_initializePluginFunction();
}
}
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 "";
}
void PluginHandler::load(std::string_view pluginFolder) {

View File

@ -177,7 +177,7 @@ namespace hex {
if (this->m_averageEntropy > 0.83 && this->m_highestBlockEntropy > 0.9) {
ImGui::NewLine();
ImGui::TextColored(ImVec4(0.92F, 0.25F, 0.2F, 1.0F),"hex.view.information.encrypted"_lang);
ImGui::TextColored(ImVec4(0.92F, 0.25F, 0.2F, 1.0F), "hex.view.information.encrypted"_lang);
}
}

View File

@ -364,6 +364,7 @@ namespace hex {
if (ImGui::BulletHyperlink("hex.welcome.start.open_project"_lang))
EventManager::post(Events::OpenWindow, "Open Project");
}
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
ImGui::TableNextColumn();
ImGui::TextUnformatted("hex.welcome.start.recent"_lang);
@ -377,6 +378,7 @@ namespace hex {
}
}
}
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
ImGui::TableNextColumn();
ImGui::TextUnformatted("hex.welcome.header.help"_lang);
@ -385,6 +387,49 @@ namespace hex {
if (ImGui::BulletHyperlink("hex.welcome.help.gethelp"_lang)) hex::openWebpage("hex.welcome.help.gethelp.link"_lang);
}
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
ImGui::TableNextColumn();
ImGui::TextUnformatted("hex.welcome.header.plugins"_lang);
{
const auto &plugins = PluginHandler::getPlugins();
if (plugins.empty()) {
// Intentionally left untranslated so it will be readable even if no plugin with translations is loaded
ImGui::TextColored(ImVec4(0.92F, 0.25F, 0.2F, 1.0F),
"No plugins loaded! To use ImHex properly, "
"make sure at least the builtin plugin is in the /plugins folder next to the executable");
} else {
if (ImGui::BeginTable("plugins", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY | ImGuiTableFlags_SizingFixedFit, ImVec2((ImGui::GetContentRegionAvail().x * 5) / 6, ImGui::GetTextLineHeightWithSpacing() * 4))) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("hex.welcome.plugins.plugin"_lang);
ImGui::TableSetupColumn("hex.welcome.plugins.author"_lang);
ImGui::TableSetupColumn("hex.welcome.plugins.desc"_lang);
ImGui::TableHeadersRow();
ImGuiListClipper clipper;
clipper.Begin(plugins.size());
while (clipper.Step()) {
for (u64 i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted((plugins[i].getPluginName() + " ").c_str());
ImGui::TableNextColumn();
ImGui::TextUnformatted((plugins[i].getPluginAuthor() + " ").c_str());
ImGui::TableNextColumn();
ImGui::TextUnformatted(plugins[i].getPluginDescription().c_str());
}
}
clipper.End();
ImGui::EndTable();
}
}
}
ImGui::EndTable();
}
ImGui::SameLine();