1
0
mirror of synced 2024-09-25 03:58:27 +02:00

windows: Added context menu entry option

Closes #333
This commit is contained in:
WerWolv 2021-10-31 16:28:10 +01:00
parent 1f250e87a3
commit c88053a575
7 changed files with 109 additions and 8 deletions

View File

@ -17,6 +17,8 @@ namespace hex {
Constants
};
std::string getExecutablePath();
std::vector<std::string> getPath(ImHexPath path);
}

View File

@ -16,11 +16,28 @@
namespace hex {
std::vector<std::string> getPath(ImHexPath path) {
std::string getExecutablePath() {
#if defined(OS_WINDOWS)
std::string exePath(MAX_PATH, '\0');
GetModuleFileName(nullptr, exePath.data(), exePath.length());
auto parentDir = std::filesystem::path(exePath).parent_path();
return exePath;
#elif defined(OS_LINUX)
std::string exePath(PATH_MAX, '\0');
readlink("/proc/self/exe", executablePath.data(), PATH_MAX);
return exePath;
#elif defined(OS_MACOS)
return getMacExecutableDirectoryPath();
#else
return "";
#endif
}
std::vector<std::string> getPath(ImHexPath path) {
#if defined(OS_WINDOWS)
const auto exePath = getExecutablePath();
const auto parentDir = std::filesystem::path(exePath).parent_path();
std::filesystem::path appDataDir;
{
@ -84,10 +101,10 @@ namespace hex {
return results;
#elif defined(OS_MACOS)
// Get path to special directories
const std::filesystem::path exeDir(getMacExecutableDirectoryPath());
const auto exePath = getExecutablePath();
const std::filesystem::path applicationSupportDir(getMacApplicationSupportDirectoryPath());
std::vector<std::filesystem::path> paths = { exeDir, applicationSupportDir };
std::vector<std::filesystem::path> paths = { exePath, applicationSupportDir };
std::vector<std::string> results;
switch (path) {
@ -126,8 +143,9 @@ namespace hex {
for (auto &dir : dataDirs)
dir = dir / "imhex";
std::array<char, PATH_MAX> executablePath = { 0 };
if (readlink("/proc/self/exe", executablePath.data(), PATH_MAX) != -1)
const auto exePath = getExecutablePath();
if (!exePath.empty())
dataDirs.emplace(dataDirs.begin(), std::filesystem::path(executablePath.data()).parent_path());
std::vector<std::string> result;

View File

@ -14,6 +14,7 @@ if (WIN32)
source/lang/zh_CN.cpp
source/content/ui_items.cpp
source/content/settings_entries.cpp
)
# Add additional include directories here #

View File

@ -0,0 +1,76 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/lang.hpp>
using namespace hex::lang_literals;
#include <imgui.h>
#include <nlohmann/json.hpp>
#include <windows.h>
namespace hex::plugin::windows {
namespace {
constexpr auto ImHexContextMenuKey = R"(Software\Classes\*\shell\ImHex)";
void addImHexContextMenuEntry() {
// Create ImHex Root Key
HKEY imHexRootKey;
RegCreateKeyExA(HKEY_CURRENT_USER, ImHexContextMenuKey, 0x00, nullptr, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE , nullptr, &imHexRootKey, nullptr);
RegSetValueA(imHexRootKey, nullptr, REG_SZ, "Open with ImHex", 0x00);
// Add Icon key to use first icon embedded in exe
std::array<char, MAX_PATH> imHexPath = { 0 };
GetModuleFileNameA(nullptr, imHexPath.data(), imHexPath.size());
auto iconValue = hex::format(R"("{}",0)", imHexPath.data());
RegSetKeyValueA(imHexRootKey, nullptr, "Icon", REG_SZ, iconValue.c_str(), iconValue.size() + 1);
// Add command key to pass file path as first argument to ImHex
auto commandValue = hex::format(R"("{}" "%1")", imHexPath.data());
RegSetValueA(imHexRootKey, "command", REG_SZ, commandValue.c_str(), commandValue.size() + 1);
RegCloseKey(imHexRootKey);
}
void removeImHexContextMenuEntry() {
RegDeleteTreeA(HKEY_CURRENT_USER, ImHexContextMenuKey);
}
bool hasImHexContextMenuEntry() {
HKEY key;
bool keyExists = (RegOpenKeyExA(HKEY_CURRENT_USER, ImHexContextMenuKey, 0x00, KEY_SET_VALUE, &key) == ERROR_SUCCESS);
RegCloseKey(key);
return keyExists;
}
}
void registerSettings() {
/* General */
ContentRegistry::Settings::add("hex.builtin.setting.general", "hex.builtin.setting.general.context_menu_entry", 0, [](auto name, nlohmann::json &setting) {
static bool enabled = hasImHexContextMenuEntry();
if (ImGui::Checkbox(name.data(), &enabled)) {
if (enabled)
addImHexContextMenuEntry();
else
removeImHexContextMenuEntry();
enabled = hasImHexContextMenuEntry();
setting = enabled;
return true;
}
return false;
});
}
}

View File

@ -23,8 +23,9 @@ namespace hex::plugin::windows {
{ "hex.windows.view.tty_console.console", "Console" },
{ "hex.windows.view.tty_console.send_etx", "Send ETX" },
{ "hex.windows.view.tty_console.send_eot", "Send EOT" },
{ "hex.windows.view.tty_console.send_sub", "Send SUB" }
{ "hex.windows.view.tty_console.send_sub", "Send SUB" },
{ "hex.builtin.setting.general.context_menu_entry", "Windows context menu entry" },
});
}

View File

@ -23,8 +23,9 @@ namespace hex::plugin::windows {
{ "hex.windows.view.tty_console.console", "控制台" },
{ "hex.windows.view.tty_console.send_etx", "发送ETX" },
{ "hex.windows.view.tty_console.send_eot", "发送EOT" },
{ "hex.windows.view.tty_console.send_sub", "发送SUB" }
{ "hex.windows.view.tty_console.send_sub", "发送SUB" },
//{ "hex.builtin.setting.general.context_menu_entry", "Windows context menu entry" },
});
}

View File

@ -8,6 +8,7 @@ namespace hex::plugin::windows {
void registerLanguageZhCN();
void addFooterItems();
void registerSettings();
}
@ -20,6 +21,7 @@ IMHEX_PLUGIN_SETUP("Windows", "WerWolv", "Windows-only features") {
registerLanguageZhCN();
addFooterItems();
registerSettings();
}