From 60af9970c130a18e78efb438dac0ad720fb36a11 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 16 Feb 2022 10:04:05 +0100 Subject: [PATCH] fix: Opening files with unicode characters in their path --- lib/libimhex/source/helpers/utils.cpp | 16 +++++++++- main/source/window/window.cpp | 9 ++++-- .../content/providers/file_provider.hpp | 2 +- .../source/content/views/view_hex_editor.cpp | 30 ++++++++----------- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index ba40b15c2..2d5dabc82 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -22,6 +22,8 @@ #include #endif +#include + namespace hex { long double operator""_scaled(long double value) { @@ -111,6 +113,18 @@ namespace hex { return result; } + std::string makeStringPrintable(const std::string &string) { + std::string result; + for (char c : string) { + if (std::isprint(c)) + result += c; + else + result += hex::format("\\x{0:02X}", u8(c)); + } + + return result; + } + std::string makePrintable(u8 c) { switch (c) { case 0: @@ -282,7 +296,7 @@ namespace hex { } if (result == NFD_OKAY) { - callback(outPath); + callback(reinterpret_cast(outPath)); NFD::FreePath(outPath); } diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index 0db3fe0b2..f1a7db83f 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -99,7 +99,7 @@ namespace hex { EventManager::post(""); }); - EventManager::subscribe(this, [this](std::string windowTitle) { + EventManager::subscribe(this, [this](const std::string &windowTitle) { std::string title = "ImHex"; if (ImHexApi::Provider::isValid()) { @@ -108,6 +108,9 @@ namespace hex { if (ProjectFile::hasUnsavedChanges()) title += " (*)"; + + if (!ImHexApi::Provider::get()->isWritable()) + title += " (Read Only)"; } this->m_windowTitle = title; @@ -600,7 +603,7 @@ namespace hex { return; for (u32 i = 0; i < count; i++) { - auto path = std::filesystem::path(paths[i]); + auto path = std::filesystem::path(reinterpret_cast(paths[i])); bool handled = false; for (const auto &[extensions, handler] : ContentRegistry::FileHandler::getEntries()) { @@ -616,7 +619,7 @@ namespace hex { } if (!handled) - EventManager::post(path.string()); + EventManager::post(path); } }); diff --git a/plugins/builtin/include/content/providers/file_provider.hpp b/plugins/builtin/include/content/providers/file_provider.hpp index 15d2c107c..62ef90a81 100644 --- a/plugins/builtin/include/content/providers/file_provider.hpp +++ b/plugins/builtin/include/content/providers/file_provider.hpp @@ -60,9 +60,9 @@ namespace hex::plugin::builtin::prv { void *m_mappedFile = nullptr; size_t m_fileSize = 0; + struct stat m_fileStats = { 0 }; bool m_fileStatsValid = false; bool m_emptyFile = false; - struct stat m_fileStats = { 0 }; bool m_readable = false, m_writable = false; }; diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 0ae31b615..0c7efe732 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -28,7 +28,7 @@ namespace hex::plugin::builtin { this->m_searchHexBuffer.resize(0xFFF, 0x00); ContentRegistry::FileHandler::add({ ".hexproj" }, [](const auto &path) { - return ProjectFile::load(path.string()); + return ProjectFile::load(path); }); ContentRegistry::FileHandler::add({ ".tbl" }, [this](const auto &path) { @@ -566,8 +566,7 @@ namespace hex::plugin::builtin { this->m_searchRequested = false; } - if (ImGui::InputText("##nolabel", currBuffer->data(), currBuffer->size(), - flags, ViewHexEditor::inputCallback, this)) { + if (ImGui::InputText("##nolabel", currBuffer->data(), currBuffer->size(), flags, ViewHexEditor::inputCallback, this)) { this->m_searchRequested = true; if (this->m_lastSearchBuffer == nullptr || this->m_lastSearchBuffer->empty()) performSearch(currBuffer->data()); @@ -637,8 +636,7 @@ namespace hex::plugin::builtin { this->m_lastSearchBuffer = &this->m_lastStringSearch; currBuffer = &this->m_searchStringBuffer; - drawSearchInput(currBuffer, ImGuiInputTextFlags_CallbackCompletion | - ImGuiInputTextFlags_EnterReturnsTrue); + drawSearchInput(currBuffer, ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_EnterReturnsTrue); } if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.search.hex"_lang)) { @@ -646,9 +644,7 @@ namespace hex::plugin::builtin { this->m_lastSearchBuffer = &this->m_lastHexSearch; currBuffer = &this->m_searchHexBuffer; - drawSearchInput(currBuffer, ImGuiInputTextFlags_CharsHexadecimal | - ImGuiInputTextFlags_CallbackCompletion | - ImGuiInputTextFlags_EnterReturnsTrue); + drawSearchInput(currBuffer, ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_EnterReturnsTrue); } if (currBuffer != nullptr) { @@ -989,13 +985,13 @@ namespace hex::plugin::builtin { ShortcutManager::addShortcut(this, CTRL + Keys::F, [this] { this->m_searchRequested = true; ImGui::OpenPopupInWindow(View::toWindowName("hex.builtin.view.hex_editor.name").c_str(), - "hex.builtin.view.hex_editor.menu.file.search"_lang); + "hex.builtin.view.hex_editor.menu.file.search"_lang); }); ShortcutManager::addShortcut(this, CTRL + Keys::G, [this] { this->m_gotoRequested = true; ImGui::OpenPopupInWindow(View::toWindowName("hex.builtin.view.hex_editor.name").c_str(), - "hex.builtin.view.hex_editor.menu.file.goto"_lang); + "hex.builtin.view.hex_editor.menu.file.goto"_lang); }); ShortcutManager::addShortcut(this, CTRL + Keys::O, [] { @@ -1071,12 +1067,12 @@ namespace hex::plugin::builtin { hex::openFileBrowser("hex.builtin.view.hex_editor.save_project"_lang, DialogMode::Save, { {"Project File", "hexproj"} }, - [](const auto &path) { - if (path.extension() == ".hexproj") { - ProjectFile::store(path); - } else { - ProjectFile::store(path.string() + ".hexproj"); + [](fs::path path) { + if (path.extension() != ".hexproj") { + path.replace_extension(".hexproj"); } + + ProjectFile::store(path); }); } else ProjectFile::store(); @@ -1270,13 +1266,13 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 1400, [&, this] { if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.search"_lang, "CTRL + F")) { this->getWindowOpenState() = true; - this->m_searchRequested = true; + this->m_searchRequested = true; ImGui::OpenPopupInWindow(View::toWindowName("hex.builtin.view.hex_editor.name").c_str(), "hex.builtin.view.hex_editor.menu.file.search"_lang); } if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.goto"_lang, "CTRL + G")) { this->getWindowOpenState() = true; - this->m_gotoRequested = true; + this->m_gotoRequested = true; ImGui::OpenPopupInWindow(View::toWindowName("hex.builtin.view.hex_editor.name").c_str(), "hex.builtin.view.hex_editor.menu.file.goto"_lang); } });