From 4d91e7f34765c095d76e1a43712123d1d9895c80 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 23 Feb 2024 17:59:37 +0100 Subject: [PATCH] impr: Handle read-only files more gracefully --- lib/libimhex/source/providers/provider.cpp | 6 ++++++ plugins/builtin/source/content/events.cpp | 6 +++++- .../builtin/source/content/views/view_data_inspector.cpp | 5 ++--- plugins/builtin/source/content/views/view_pattern_data.cpp | 3 ++- plugins/ui/include/ui/pattern_drawer.hpp | 2 ++ plugins/ui/source/ui/pattern_drawer.cpp | 2 +- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index d45702256..31e68177d 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -43,11 +43,17 @@ namespace hex::prv { } void Provider::write(u64 offset, const void *buffer, size_t size) { + if (!this->isWritable()) + return; + EventProviderDataModified::post(this, offset, size, static_cast(buffer)); this->markDirty(); } void Provider::save() { + if (!this->isWritable()) + return; + EventProviderSaved::post(this); } void Provider::saveAs(const std::fs::path &path) { diff --git a/plugins/builtin/source/content/events.cpp b/plugins/builtin/source/content/events.cpp index be9e9f739..0283081fb 100644 --- a/plugins/builtin/source/content/events.cpp +++ b/plugins/builtin/source/content/events.cpp @@ -89,8 +89,12 @@ namespace hex::plugin::builtin { }); EventProviderOpened::subscribe([](hex::prv::Provider *provider) { - if (provider != nullptr && ImHexApi::Provider::get() == provider) + if (provider != nullptr && ImHexApi::Provider::get() == provider) { RequestUpdateWindowTitle::post(); + + if (!provider->isWritable()) + ui::ToastInfo::open("hex.builtin.popup.error.read_only"_lang); + } }); RequestOpenFile::subscribe(openFile); diff --git a/plugins/builtin/source/content/views/view_data_inspector.cpp b/plugins/builtin/source/content/views/view_data_inspector.cpp index 187bf3ca6..ff15f5aef 100644 --- a/plugins/builtin/source/content/views/view_data_inspector.cpp +++ b/plugins/builtin/source/content/views/view_data_inspector.cpp @@ -264,11 +264,10 @@ namespace hex::plugin::builtin { } // Enter editing mode when double-clicking the row - if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && editingFunction.has_value()) { - editing = true; + if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && editingFunction.has_value() && m_selectedProvider->isWritable()) { + editing = true; m_editingValue = copyValue; } - } else { // Handle editing mode diff --git a/plugins/builtin/source/content/views/view_pattern_data.cpp b/plugins/builtin/source/content/views/view_pattern_data.cpp index 763fa7e9c..c6d502606 100644 --- a/plugins/builtin/source/content/views/view_pattern_data.cpp +++ b/plugins/builtin/source/content/views/view_pattern_data.cpp @@ -37,7 +37,7 @@ namespace hex::plugin::builtin { (*m_patternDrawer)->jumpToPattern(pattern); }); - m_patternDrawer.setOnCreateCallback([this](prv::Provider *, auto &drawer) { + m_patternDrawer.setOnCreateCallback([this](prv::Provider *provider, auto &drawer) { drawer = std::make_unique(); drawer->setSelectionCallback([](const pl::ptrn::Pattern *pattern) { @@ -47,6 +47,7 @@ namespace hex::plugin::builtin { drawer->setTreeStyle(m_treeStyle); drawer->enableRowColoring(m_rowColoring); + drawer->enablePatternEditing(provider->isWritable()); }); } diff --git a/plugins/ui/include/ui/pattern_drawer.hpp b/plugins/ui/include/ui/pattern_drawer.hpp index 4bdf4999f..97b24c9fe 100644 --- a/plugins/ui/include/ui/pattern_drawer.hpp +++ b/plugins/ui/include/ui/pattern_drawer.hpp @@ -33,6 +33,7 @@ namespace hex::ui { void setTreeStyle(TreeStyle style) { m_treeStyle = style; } void setSelectionCallback(std::function callback) { m_selectionCallback = std::move(callback); } void enableRowColoring(bool enabled) { m_rowColoring = enabled; } + void enablePatternEditing(bool enabled) { m_editingEnabled = enabled; } void reset(); void jumpToPattern(const pl::ptrn::Pattern *pattern) { m_jumpToPattern = pattern; } @@ -103,6 +104,7 @@ namespace hex::ui { TreeStyle m_treeStyle = TreeStyle::Default; bool m_rowColoring = false; + bool m_editingEnabled = false; pl::ptrn::Pattern *m_currVisualizedPattern = nullptr; const pl::ptrn::Pattern *m_jumpToPattern = nullptr; diff --git a/plugins/ui/source/ui/pattern_drawer.cpp b/plugins/ui/source/ui/pattern_drawer.cpp index 102a8587d..9ede093ab 100644 --- a/plugins/ui/source/ui/pattern_drawer.cpp +++ b/plugins/ui/source/ui/pattern_drawer.cpp @@ -411,7 +411,7 @@ namespace hex::ui { } } - if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { + if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && m_editingEnabled) { m_editingPattern = &pattern; m_editingPatternOffset = pattern.getOffset(); AchievementManager::unlockAchievement("hex.builtin.achievement.patterns", "hex.builtin.achievement.patterns.modify_data.name");