1
0
mirror of synced 2024-09-25 20:18:26 +02:00

fix: Path handling and plugin loading breaking with non-ASCII paths

This commit is contained in:
WerWolv 2022-06-29 21:34:17 +02:00
parent 11c2f240a1
commit ac964dc5ec
3 changed files with 54 additions and 26 deletions

View File

@ -7,6 +7,12 @@
#include <string> #include <string>
#if defined(OS_WINDOWS)
#include <windows.h>
#else
#include <dlfcn.h>
#endif
struct ImGuiContext; struct ImGuiContext;
namespace hex { namespace hex {
@ -39,7 +45,11 @@ namespace hex {
using SetImGuiContextFunc = void (*)(ImGuiContext *); using SetImGuiContextFunc = void (*)(ImGuiContext *);
using IsBuiltinPluginFunc = bool (*)(); using IsBuiltinPluginFunc = bool (*)();
void *m_handle = nullptr; #if defined(OS_WINDOWS)
HMODULE m_handle = nullptr;
#else
void *m_handle = nullptr;
#endif
std::fs::path m_path; std::fs::path m_path;
mutable bool m_initialized = false; mutable bool m_initialized = false;

View File

@ -3,17 +3,26 @@
#include <hex/helpers/logger.hpp> #include <hex/helpers/logger.hpp>
#include <filesystem> #include <filesystem>
#include <dlfcn.h> #include <system_error>
namespace hex { namespace hex {
Plugin::Plugin(const std::fs::path &path) : m_path(path) { Plugin::Plugin(const std::fs::path &path) : m_path(path) {
this->m_handle = dlopen(path.string().c_str(), RTLD_LAZY); #if defined(OS_WINDOWS)
this->m_handle = LoadLibraryW(path.c_str());
if (this->m_handle == nullptr) { if (this->m_handle == nullptr) {
log::error("dlopen failed: {}", dlerror()); log::error("LoadLibraryW failed: {}!", std::system_category().message(::GetLastError()));
return; return;
} }
#else
this->m_handle = dlopen(path.string().c_str(), RTLD_LAZY);
if (this->m_handle == nullptr) {
log::error("dlopen failed: {}!", dlerror());
return;
}
#endif
auto pluginName = std::fs::path(path).stem().string(); auto pluginName = std::fs::path(path).stem().string();
@ -49,8 +58,13 @@ namespace hex {
} }
Plugin::~Plugin() { Plugin::~Plugin() {
if (this->m_handle != nullptr) #if defined(OS_WINDOWS)
dlclose(this->m_handle); if (this->m_handle != nullptr)
FreeLibrary(this->m_handle);
#else
if (this->m_handle != nullptr)
dlclose(this->m_handle);
#endif
} }
bool Plugin::initializePlugin() const { bool Plugin::initializePlugin() const {
@ -120,7 +134,11 @@ namespace hex {
void *Plugin::getPluginFunction(const std::string &symbol) { void *Plugin::getPluginFunction(const std::string &symbol) {
return dlsym(this->m_handle, symbol.c_str()); #if defined(OS_WINDOWS)
return reinterpret_cast<void *>(GetProcAddress(this->m_handle, symbol.c_str()));
#else
return dlsym(this->m_handle, symbol.c_str());
#endif
} }
@ -135,7 +153,7 @@ namespace hex {
for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) { for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) {
if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug") if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug")
PluginManager::s_plugins.emplace_back(pluginPath.path().string()); PluginManager::s_plugins.emplace_back(pluginPath.path());
} }
if (PluginManager::s_plugins.empty()) if (PluginManager::s_plugins.empty())

View File

@ -22,8 +22,8 @@ namespace hex::fs {
std::optional<std::fs::path> getExecutablePath() { std::optional<std::fs::path> getExecutablePath() {
#if defined(OS_WINDOWS) #if defined(OS_WINDOWS)
std::string exePath(MAX_PATH, '\0'); std::wstring exePath(MAX_PATH, '\0');
if (GetModuleFileName(nullptr, exePath.data(), exePath.length()) == 0) if (GetModuleFileNameW(nullptr, exePath.data(), exePath.length()) == 0)
return std::nullopt; return std::nullopt;
return exePath; return exePath;
@ -113,11 +113,11 @@ namespace hex::fs {
#if defined(OS_WINDOWS) #if defined(OS_WINDOWS)
std::fs::path appDataDir; std::fs::path appDataDir;
{ {
LPWSTR wAppDataPath = nullptr; PWSTR wAppDataPath = nullptr;
if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &wAppDataPath))) if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &wAppDataPath)))
throw std::runtime_error("Failed to get APPDATA folder path"); throw std::runtime_error("Failed to get APPDATA folder path");
appDataDir = wAppDataPath; appDataDir = std::wstring(wAppDataPath);
CoTaskMemFree(wAppDataPath); CoTaskMemFree(wAppDataPath);
} }
@ -130,60 +130,60 @@ namespace hex::fs {
case ImHexPath::Patterns: case ImHexPath::Patterns:
addUserDirs(paths); addUserDirs(paths);
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "patterns").string(); return path / "patterns";
}); });
break; break;
case ImHexPath::PatternsInclude: case ImHexPath::PatternsInclude:
addUserDirs(paths); addUserDirs(paths);
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "includes").string(); return path / "includes";
}); });
break; break;
case ImHexPath::Magic: case ImHexPath::Magic:
addUserDirs(paths); addUserDirs(paths);
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "magic").string(); return path / "magic";
}); });
break; break;
case ImHexPath::Python: case ImHexPath::Python:
addUserDirs(paths); addUserDirs(paths);
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "python").string(); return path / "python";
}); });
break; break;
case ImHexPath::Plugins: case ImHexPath::Plugins:
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "plugins").string(); return path / "plugins";
}); });
break; break;
case ImHexPath::Yara: case ImHexPath::Yara:
addUserDirs(paths); addUserDirs(paths);
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "yara").string(); return path / "yara";
}); });
break; break;
case ImHexPath::Config: case ImHexPath::Config:
return { (appDataDir / "imhex" / "config").string() }; return { appDataDir / "imhex" / "config" };
case ImHexPath::Resources: case ImHexPath::Resources:
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "resources").string(); return path / "resources";
}); });
break; break;
case ImHexPath::Constants: case ImHexPath::Constants:
addUserDirs(paths); addUserDirs(paths);
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "constants").string(); return path / "constants";
}); });
break; break;
case ImHexPath::Encodings: case ImHexPath::Encodings:
addUserDirs(paths); addUserDirs(paths);
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "encodings").string(); return path / "encodings";
}); });
break; break;
case ImHexPath::Logs: case ImHexPath::Logs:
std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) {
return (path / "logs").string(); return path / "logs";
}); });
break; break;
default: default: