From 655e068b9b11ef6bc47d6ec3fb0415c368c829d4 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 31 Dec 2024 00:47:32 +0100 Subject: [PATCH] impr: Added changed data information to diff view table --- plugins/diffing/romfs/lang/en_US.json | 1 + .../source/content/views/view_diff.cpp | 77 ++++++++++++++++--- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/plugins/diffing/romfs/lang/en_US.json b/plugins/diffing/romfs/lang/en_US.json index f4e898e71..9007cdc15 100644 --- a/plugins/diffing/romfs/lang/en_US.json +++ b/plugins/diffing/romfs/lang/en_US.json @@ -13,6 +13,7 @@ "hex.diffing.view.diff.modified": "Modified", "hex.diffing.view.diff.provider_a": "Provider A", "hex.diffing.view.diff.provider_b": "Provider B", + "hex.diffing.view.diff.changes": "Changes", "hex.diffing.view.diff.removed": "Removed", "hex.diffing.view.diff.algorithm": "Diffing Algorithm", "hex.diffing.view.diff.settings": "No settings available", diff --git a/plugins/diffing/source/content/views/view_diff.cpp b/plugins/diffing/source/content/views/view_diff.cpp index 58cc3f666..ceff7d11c 100644 --- a/plugins/diffing/source/content/views/view_diff.cpp +++ b/plugins/diffing/source/content/views/view_diff.cpp @@ -149,6 +149,20 @@ namespace hex::plugin::diffing { }; } + static void drawByteString(const std::vector &bytes) { + for (u64 i = 0; i < bytes.size(); i += 1) { + if (i >= 16) { + ImGui::TextDisabled(ICON_VS_ELLIPSIS); + ImGui::SameLine(0, 0); + break; + } + + u8 byte = bytes[i]; + ImGuiExt::TextFormattedDisabled("{0:02X} ", byte); + ImGui::SameLine(0, (i % 4 == 3) ? 4_scaled : 0); + } + } + void ViewDiff::drawContent() { auto &[a, b] = m_columns; @@ -263,11 +277,12 @@ namespace hex::plugin::diffing { } // Draw the differences table - if (ImGui::BeginTable("##differences", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_ScrollY | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Resizable)) { + if (ImGui::BeginTable("##differences", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_ScrollY | ImGuiTableFlags_Reorderable | ImGuiTableFlags_SizingFixedFit)) { ImGui::TableSetupScrollFreeze(0, 1); - ImGui::TableSetupColumn("hex.ui.common.begin"_lang); - ImGui::TableSetupColumn("hex.ui.common.end"_lang); - ImGui::TableSetupColumn("hex.ui.common.type"_lang); + ImGui::TableSetupColumn("##Type", ImGuiTableColumnFlags_NoReorder); + ImGui::TableSetupColumn("hex.diffing.view.diff.provider_a"_lang); + ImGui::TableSetupColumn("hex.diffing.view.diff.provider_b"_lang); + ImGui::TableSetupColumn("hex.diffing.view.diff.changes"_lang); ImGui::TableHeadersRow(); // Draw the differences if the providers have been analyzed @@ -289,9 +304,28 @@ namespace hex::plugin::diffing { // Draw a clickable row for each difference that will select the difference in both hex editors + // Draw difference type + ImGui::TableNextColumn(); + switch (typeA) { + case DifferenceType::Mismatch: + ImGuiExt::TextFormattedColored(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_DiffChanged), ICON_VS_DIFF_MODIFIED); + ImGui::SetItemTooltip("hex.diffing.view.diff.modified"_lang); + break; + case DifferenceType::Insertion: + ImGuiExt::TextFormattedColored(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_DiffAdded), ICON_VS_DIFF_ADDED); + ImGui::SetItemTooltip("hex.diffing.view.diff.added"_lang); + break; + case DifferenceType::Deletion: + ImGuiExt::TextFormattedColored(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_DiffRemoved), ICON_VS_DIFF_REMOVED); + ImGui::SetItemTooltip("hex.diffing.view.diff.removed"_lang); + break; + default: + break; + } + // Draw start address ImGui::TableNextColumn(); - if (ImGui::Selectable(hex::format("0x{:02X}", regionA.start).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) { + if (ImGui::Selectable(hex::format("0x{:04X} - 0x{:04X}", regionA.start, regionA.end).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) { const Region selectionA = { regionA.start, ((regionA.end - regionA.start) + 1) }; const Region selectionB = { regionB.start, ((regionB.end - regionB.start) + 1) }; @@ -311,23 +345,42 @@ namespace hex::plugin::diffing { // Draw end address ImGui::TableNextColumn(); - ImGui::TextUnformatted(hex::format("0x{:02X}", regionA.end).c_str()); + ImGui::TextUnformatted(hex::format("0x{:04X} - 0x{:04X}", regionB.start, regionB.end).c_str()); - // Draw difference type + const auto &providers = ImHexApi::Provider::getProviders(); + std::vector data; + + // Draw changes ImGui::TableNextColumn(); + ImGui::Indent(); switch (typeA) { - case DifferenceType::Mismatch: - ImGuiExt::TextFormattedColored(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_DiffChanged), "hex.diffing.view.diff.modified"_lang); - break; case DifferenceType::Insertion: - ImGuiExt::TextFormattedColored(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_DiffAdded), "hex.diffing.view.diff.added"_lang); + data.resize(std::min(17, (regionA.end - regionA.start) + 1)); + providers[a.provider]->read(regionA.start, data.data(), data.size()); + drawByteString(data); + break; + case DifferenceType::Mismatch: + data.resize(std::min(17, (regionA.end - regionA.start) + 1)); + providers[a.provider]->read(regionA.start, data.data(), data.size()); + drawByteString(data); + + ImGui::SameLine(0, 0); + ImGuiExt::TextFormatted(" {} ", ICON_VS_ARROW_RIGHT); + ImGui::SameLine(0, 0); + + data.resize(std::min(17, (regionB.end - regionB.start) + 1)); + providers[b.provider]->read(regionB.start, data.data(), data.size()); + drawByteString(data); break; case DifferenceType::Deletion: - ImGuiExt::TextFormattedColored(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_DiffRemoved), "hex.diffing.view.diff.removed"_lang); + data.resize(std::min(17, (regionB.end - regionB.start) + 1)); + providers[b.provider]->read(regionB.start, data.data(), data.size()); + drawByteString(data); break; default: break; } + ImGui::Unindent(); ImGui::PopID(); }