-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
view_diff.cpp
341 lines (267 loc) 路 13.5 KB
/
view_diff.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "content/views/view_diff.hpp"
#include <hex/api/imhex_api.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/providers/buffered_reader.hpp>
#include <fonts/codicons_font.h>
#include <wolv/utils/guards.hpp>
namespace hex::plugin::diffing {
using DifferenceType = ContentRegistry::Diffing::DifferenceType;
ViewDiff::ViewDiff() : View::Window("hex.diffing.view.diff.name", ICON_VS_DIFF_SIDEBYSIDE) {
// Clear the selected diff providers when a provider is closed
EventProviderClosed::subscribe(this, [this](prv::Provider *) {
this->reset();
});
EventDataChanged::subscribe(this, [this](prv::Provider *) {
m_analyzed = false;
});
// Set the background highlight callbacks for the two hex editor columns
m_columns[0].hexEditor.setBackgroundHighlightCallback(this->createCompareFunction(1));
m_columns[1].hexEditor.setBackgroundHighlightCallback(this->createCompareFunction(0));
}
ViewDiff::~ViewDiff() {
EventProviderClosed::unsubscribe(this);
EventDataChanged::unsubscribe(this);
}
namespace {
bool drawDiffColumn(ViewDiff::Column &column, float height) {
if (height < 0)
return false;
bool scrolled = false;
ImGui::PushID(&column);
ON_SCOPE_EXIT { ImGui::PopID(); };
// Draw the hex editor
float prevScroll = column.hexEditor.getScrollPosition();
column.hexEditor.draw(height);
float currScroll = column.hexEditor.getScrollPosition();
// Check if the user scrolled the hex editor
if (prevScroll != currScroll) {
scrolled = true;
column.scrollLock = 5;
}
return scrolled;
}
bool drawProviderSelector(ViewDiff::Column &column) {
bool shouldReanalyze = false;
ImGui::PushID(&column);
auto providers = ImHexApi::Provider::getProviders();
auto &providerIndex = column.provider;
// Get the name of the currently selected provider
std::string preview;
if (ImHexApi::Provider::isValid() && providerIndex >= 0)
preview = providers[providerIndex]->getName();
// Draw combobox with all available providers
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
if (ImGui::BeginCombo("", preview.c_str())) {
for (size_t i = 0; i < providers.size(); i++) {
ImGui::PushID(i + 1);
if (ImGui::Selectable(providers[i]->getName().c_str())) {
providerIndex = i;
shouldReanalyze = true;
}
ImGui::PopID();
}
ImGui::EndCombo();
}
ImGui::PopID();
return shouldReanalyze;
}
}
void ViewDiff::analyze(prv::Provider *providerA, prv::Provider *providerB) {
auto commonSize = std::max(providerA->getActualSize(), providerB->getActualSize());
m_diffTask = TaskManager::createTask("Diffing...", commonSize, [this, providerA, providerB](Task &) {
auto differences = m_algorithm->analyze(providerA, providerB);
auto providers = ImHexApi::Provider::getProviders();
// Move the calculated differences over so they can be displayed
for (size_t i = 0; i < m_columns.size(); i++) {
auto &column = m_columns[i];
auto &provider = providers[column.provider];
column.differences = differences[i].overlapping({ provider->getBaseAddress(), provider->getBaseAddress() + provider->getActualSize() });
std::ranges::sort(
column.differences,
std::less(),
[](const auto &a) { return a.interval; }
);
column.diffTree = std::move(differences[i]);
}
m_analyzed = true;
});
}
void ViewDiff::reset() {
for (auto &column : m_columns) {
column.provider = -1;
column.hexEditor.setSelectionUnchecked(std::nullopt, std::nullopt);
column.diffTree.clear();
}
}
std::function<std::optional<color_t>(u64, const u8*, size_t)> ViewDiff::createCompareFunction(size_t otherIndex) const {
const auto currIndex = otherIndex == 0 ? 1 : 0;
return [=, this](u64 address, const u8 *, size_t size) -> std::optional<color_t> {
if (!m_analyzed)
return std::nullopt;
const auto matches = m_columns[currIndex].diffTree.overlapping({ address, (address + size) - 1 });
if (matches.empty())
return std::nullopt;
const auto type = matches[0].value;
if (type == DifferenceType::Mismatch) {
return ImGuiExt::GetCustomColorU32(ImGuiCustomCol_DiffChanged);
} else if (type == DifferenceType::Insertion && currIndex == 0) {
return ImGuiExt::GetCustomColorU32(ImGuiCustomCol_DiffAdded);
} else if (type == DifferenceType::Deletion && currIndex == 1) {
return ImGuiExt::GetCustomColorU32(ImGuiCustomCol_DiffRemoved);
}
return std::nullopt;
};
}
void ViewDiff::drawContent() {
auto &[a, b] = m_columns;
a.hexEditor.enableSyncScrolling(false);
b.hexEditor.enableSyncScrolling(false);
if (a.scrollLock > 0) a.scrollLock--;
if (b.scrollLock > 0) b.scrollLock--;
// Change the hex editor providers if the user selected a new provider
{
const auto &providers = ImHexApi::Provider::getProviders();
if (a.provider >= 0 && size_t(a.provider) < providers.size())
a.hexEditor.setProvider(providers[a.provider]);
else
a.hexEditor.setProvider(nullptr);
if (b.provider >= 0 && size_t(b.provider) < providers.size())
b.hexEditor.setProvider(providers[b.provider]);
else
b.hexEditor.setProvider(nullptr);
}
// Analyze the providers if they are valid and the user selected a new provider
if (!m_analyzed && a.provider != -1 && b.provider != -1 && !m_diffTask.isRunning() && m_algorithm != nullptr) {
const auto &providers = ImHexApi::Provider::getProviders();
auto providerA = providers[a.provider];
auto providerB = providers[b.provider];
this->analyze(providerA, providerB);
}
if (auto &algorithms = ContentRegistry::Diffing::impl::getAlgorithms(); m_algorithm == nullptr && !algorithms.empty())
m_algorithm = algorithms.front().get();
const auto height = ImGui::GetContentRegionAvail().y;
// Draw the two hex editor columns side by side
if (ImGui::BeginTable("##binary_diff", 2, ImGuiTableFlags_None, ImVec2(0, height - 250_scaled))) {
ImGui::TableSetupColumn("hex.diffing.view.diff.provider_a"_lang);
ImGui::TableSetupColumn("hex.diffing.view.diff.provider_b"_lang);
ImGui::TableHeadersRow();
ImGui::BeginDisabled(m_diffTask.isRunning());
{
// Draw settings button
ImGui::TableNextColumn();
if (ImGuiExt::DimmedIconButton(ICON_VS_SETTINGS_GEAR, ImGui::GetStyleColorVec4(ImGuiCol_Text)))
RequestOpenPopup::post("##DiffingAlgorithmSettings");
ImGui::SameLine();
// Draw first provider selector
if (drawProviderSelector(a)) m_analyzed = false;
// Draw second provider selector
ImGui::TableNextColumn();
if (drawProviderSelector(b)) m_analyzed = false;
}
ImGui::EndDisabled();
ImGui::TableNextRow();
// Draw first hex editor column
ImGui::TableNextColumn();
bool scrollB = drawDiffColumn(a, height - 250_scaled);
// Draw second hex editor column
ImGui::TableNextColumn();
bool scrollA = drawDiffColumn(b, height - 250_scaled);
// Sync the scroll positions of the hex editors
{
if (scrollA && a.scrollLock == 0) {
a.hexEditor.setScrollPosition(b.hexEditor.getScrollPosition());
a.hexEditor.forceUpdateScrollPosition();
}
if (scrollB && b.scrollLock == 0) {
b.hexEditor.setScrollPosition(a.hexEditor.getScrollPosition());
b.hexEditor.forceUpdateScrollPosition();
}
}
ImGui::EndTable();
}
// Draw the differences table
if (ImGui::BeginTable("##differences", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_ScrollY | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Resizable, ImVec2(0, 200_scaled))) {
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::TableHeadersRow();
// Draw the differences if the providers have been analyzed
if (m_analyzed) {
ImGuiListClipper clipper;
auto &differencesA = m_columns[0].differences;
auto &differencesB = m_columns[1].differences;
clipper.Begin(int(std::min(differencesA.size(), differencesB.size())));
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
ImGui::TableNextRow();
ImGui::PushID(i);
const auto &[regionA, typeA] = differencesA[i];
const auto &[regionB, typeB] = differencesB[i];
// Draw a clickable row for each difference that will select the difference in both hex editors
// Draw start address
ImGui::TableNextColumn();
if (ImGui::Selectable(hex::format("0x{:02X}", regionA.start).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
a.hexEditor.setSelection({ regionA.start, ((regionA.end - regionA.start) + 1) });
a.hexEditor.jumpToSelection();
b.hexEditor.setSelection({ regionB.start, ((regionB.end - regionB.start) + 1) });
b.hexEditor.jumpToSelection();
}
// Draw end address
ImGui::TableNextColumn();
ImGui::TextUnformatted(hex::format("0x{:02X}", regionA.end).c_str());
// Draw difference type
ImGui::TableNextColumn();
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);
break;
case DifferenceType::Deletion:
ImGuiExt::TextFormattedColored(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_DiffRemoved), "hex.diffing.view.diff.removed"_lang);
break;
default:
break;
}
ImGui::PopID();
}
}
}
ImGui::EndTable();
}
}
void ViewDiff::drawAlwaysVisibleContent() {
ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(400_scaled, 600_scaled));
if (ImGui::BeginPopup("##DiffingAlgorithmSettings")) {
ImGuiExt::Header("hex.diffing.view.diff.algorithm"_lang, true);
ImGui::PushItemWidth(300_scaled);
if (ImGui::BeginCombo("##Algorithm", m_algorithm == nullptr ? "" : Lang(m_algorithm->getUnlocalizedName()))) {
for (const auto &algorithm : ContentRegistry::Diffing::impl::getAlgorithms()) {
ImGui::PushID(algorithm.get());
if (ImGui::Selectable(Lang(algorithm->getUnlocalizedName()))) {
m_algorithm = algorithm.get();
m_analyzed = false;
}
ImGui::PopID();
}
ImGui::EndCombo();
}
ImGui::PopItemWidth();
if (m_algorithm != nullptr) {
ImGuiExt::TextFormattedWrapped("{}", Lang(m_algorithm->getUnlocalizedDescription()));
}
ImGuiExt::Header("hex.diffing.view.diff.settings"_lang);
if (m_algorithm != nullptr) {
auto drawList = ImGui::GetWindowDrawList();
auto prevIdx = drawList->_VtxCurrentIdx;
m_algorithm->drawSettings();
auto currIdx = drawList->_VtxCurrentIdx;
if (prevIdx == currIdx)
ImGuiExt::TextFormatted("hex.diffing.view.diff.settings.no_settings"_lang);
}
ImGui::EndPopup();
}
}
}