1
0
mirror of synced 2024-11-13 18:50:53 +01:00

Fixed plugin loading and closing issues mainly on Windows

This fixes #87
This commit is contained in:
WerWolv 2021-02-07 13:40:47 +01:00
parent 4d7d449cea
commit 5b38c43b7e
8 changed files with 49 additions and 25 deletions

View File

@ -38,7 +38,7 @@ namespace hex {
private: private:
static inline std::string s_pluginFolder; static inline std::string s_pluginFolder;
static inline std::vector<Plugin> s_plugins; static inline std::vector<Plugin*> s_plugins;
}; };
} }

View File

@ -8,6 +8,7 @@
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <string_view>
#include <thread> #include <thread>
#include <vector> #include <vector>
@ -33,7 +34,7 @@ namespace hex {
TextEditor m_textEditor; TextEditor m_textEditor;
std::vector<std::pair<lang::LogConsole::Level, std::string>> m_console; std::vector<std::pair<lang::LogConsole::Level, std::string>> m_console;
void loadPatternFile(std::string path); void loadPatternFile(std::string_view path);
void clearPatternData(); void clearPatternData();
void parsePattern(char *buffer); void parsePattern(char *buffer);
}; };

View File

@ -26,12 +26,15 @@ target_link_libraries(${PROJECT_NAME} PRIVATE libimhex LLVMDemangle)
# ---- No need to change anything from here downwards unless you know what you're doing ---- # # ---- No need to change anything from here downwards unless you know what you're doing ---- #
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_SHARED_LIBRARY_PREFIX "plugin") set(CMAKE_SHARED_LIBRARY_PREFIX "")
set(CMAKE_SHARED_LIBRARY_SUFFIX ".hexplug")
if (WIN32) if (WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -Wl,--allow-multiple-definition") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -Wl,--allow-multiple-definition -fvisibility=hidden")
endif() endif()
add_compile_definitions(IMHEX_PLUGIN_NAME=${PROJECT_NAME})
if (NOT TARGET libimhex) if (NOT TARGET libimhex)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libimhex ${CMAKE_CURRENT_BINARY_DIR}/plugins/libimhex) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libimhex ${CMAKE_CURRENT_BINARY_DIR}/plugins/libimhex)
endif() endif()

View File

@ -18,12 +18,15 @@ target_link_libraries(${PROJECT_NAME} PRIVATE libimhex)
# ---- No need to change anything from here downwards unless you know what you're doing ---- # # ---- No need to change anything from here downwards unless you know what you're doing ---- #
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_SHARED_LIBRARY_PREFIX "plugin") set(CMAKE_SHARED_LIBRARY_PREFIX "")
set(CMAKE_SHARED_LIBRARY_SUFFIX ".hexplug")
if (WIN32) if (WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -Wl,--allow-multiple-definition") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -Wl,--allow-multiple-definition -fvisibility=hidden")
endif() endif()
add_compile_definitions(IMHEX_PLUGIN_NAME=${PROJECT_NAME})
if (NOT TARGET libimhex) if (NOT TARGET libimhex)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libimhex ${CMAKE_CURRENT_BINARY_DIR}/plugins/libimhex) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libimhex ${CMAKE_CURRENT_BINARY_DIR}/plugins/libimhex)
endif() endif()

View File

@ -11,7 +11,9 @@
#include <hex/helpers/shared_data.hpp> #include <hex/helpers/shared_data.hpp>
#include <hex/data_processor/node.hpp> #include <hex/data_processor/node.hpp>
#define IMHEX_PLUGIN_SETUP namespace hex::plugin::internal { \ #define IMHEX_PLUGIN_SETUP IMHEX_PLUGIN_SETUP_IMPL(IMHEX_PLUGIN_NAME)
void initializePlugin(); \
} \ #define IMHEX_PLUGIN_SETUP_IMPL(name) namespace hex::plugin::name::internal { \
void hex::plugin::internal::initializePlugin() [[gnu::visibility("default")]] void initializePlugin(); \
} \
void hex::plugin::name::internal::initializePlugin()

View File

@ -1,51 +1,66 @@
#include "helpers/plugin_handler.hpp" #include "helpers/plugin_handler.hpp"
#include <dlfcn.h> #include <dlfcn.h>
#include <filesystem> #include <filesystem>
namespace hex { namespace hex {
namespace fs = std::filesystem;
// hex::plugin::internal::initializePlugin() // hex::plugin::internal::initializePlugin()
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin8internal16initializePluginEv"; constexpr auto InitializePluginSymbol = "_ZN3hex6plugin%d%s8internal16initializePluginEv";
Plugin::Plugin(std::string_view path) { 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); this->m_handle = dlopen(path.data(), RTLD_LAZY);
if (this->m_handle == nullptr) if (this->m_handle == nullptr)
return; return;
this->m_initializePluginFunction = reinterpret_cast<InitializePluginFunc>(dlsym(this->m_handle, InitializePluginSymbol)); printf("Loaded plugin %s\n", path.data());
this->m_initializePluginFunction = reinterpret_cast<InitializePluginFunc>(dlsym(this->m_handle, symbolName.c_str()));
printf("Symbol %s at %p\n", symbolName.c_str(), this->m_initializePluginFunction);
} }
Plugin::~Plugin() { Plugin::~Plugin() {
printf("Plugin unloaded\n");
dlclose(this->m_handle); dlclose(this->m_handle);
} }
void Plugin::initializePlugin() const { void Plugin::initializePlugin() const {
if (this->m_initializePluginFunction != nullptr) if (this->m_initializePluginFunction != nullptr) {
printf("Initializing plugin\n");
this->m_initializePluginFunction(); this->m_initializePluginFunction();
printf("Initialized plugin\n");
}
} }
void PluginHandler::load(std::string_view pluginFolder) { void PluginHandler::load(std::string_view pluginFolder) {
PluginHandler::unload();
if (!std::filesystem::exists(pluginFolder)) if (!std::filesystem::exists(pluginFolder))
throw std::runtime_error("Failed to find plugin folder"); throw std::runtime_error("Failed to find plugin folder");
PluginHandler::s_pluginFolder = pluginFolder; PluginHandler::s_pluginFolder = pluginFolder;
for (auto& pluginPath : std::filesystem::directory_iterator(pluginFolder)) { for (auto& pluginPath : std::filesystem::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file()) if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug")
PluginHandler::s_plugins.emplace_back(pluginPath.path().string()); PluginHandler::s_plugins.push_back(new Plugin(pluginPath.path().string()));
} }
} }
void PluginHandler::unload() { void PluginHandler::unload() {
for (auto &plugin : PluginHandler::s_plugins)
delete plugin;
PluginHandler::s_plugins.clear(); PluginHandler::s_plugins.clear();
PluginHandler::s_pluginFolder.clear(); PluginHandler::s_pluginFolder.clear();
} }
void PluginHandler::reload() { void PluginHandler::reload() {
PluginHandler::unload();
PluginHandler::load(PluginHandler::s_pluginFolder); PluginHandler::load(PluginHandler::s_pluginFolder);
} }

View File

@ -153,11 +153,11 @@ namespace hex {
size_t size = ftell(file); size_t size = ftell(file);
rewind(file); rewind(file);
std::vector<char> buffer( size + 1, 0x00); std::vector<char> patternBuffer( size + 1, 0x00);
fread(buffer.data(), 1, size, file); fread(patternBuffer.data(), 1, size, file);
fclose(file); fclose(file);
preprocessor.preprocess(buffer.data()); preprocessor.preprocess(patternBuffer.data());
if (foundCorrectType) if (foundCorrectType)
this->m_possiblePatternFiles.push_back(entry.path().filename().string()); this->m_possiblePatternFiles.push_back(entry.path().filename().string());
@ -289,8 +289,8 @@ namespace hex {
} }
void ViewPattern::loadPatternFile(std::string path) { void ViewPattern::loadPatternFile(std::string_view path) {
FILE *file = fopen(path.c_str(), "rb"); FILE *file = fopen(path.data(), "rb");
if (file != nullptr) { if (file != nullptr) {
char *buffer; char *buffer;
@ -325,7 +325,7 @@ namespace hex {
this->clearPatternData(); this->clearPatternData();
this->m_textEditor.SetErrorMarkers({ }); this->m_textEditor.SetErrorMarkers({ });
this->m_console.clear(); this->m_console.clear();
this->postEvent(Events::PatternChanged); View::postEvent(Events::PatternChanged);
auto result = this->m_patternLanguageRuntime->executeString(SharedData::currentProvider, buffer); auto result = this->m_patternLanguageRuntime->executeString(SharedData::currentProvider, buffer);

View File

@ -552,7 +552,7 @@ namespace hex {
} catch (std::runtime_error &e) { return; } } catch (std::runtime_error &e) { return; }
for (const auto &plugin : PluginHandler::getPlugins()) { for (const auto &plugin : PluginHandler::getPlugins()) {
plugin.initializePlugin(); plugin->initializePlugin();
} }
} }