2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_patches.hpp"
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
|
|
|
#include <nlohmann/json.hpp>
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
#include <content/providers/undo_operations/operation_write.hpp>
|
|
|
|
#include <content/providers/undo_operations/operation_insert.hpp>
|
|
|
|
#include <content/providers/undo_operations/operation_remove.hpp>
|
|
|
|
|
|
|
|
#include <ranges>
|
2020-11-29 02:06:41 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2024-01-08 21:51:48 +01:00
|
|
|
ViewPatches::ViewPatches() : View::Window("hex.builtin.view.patches.name", ICON_VS_GIT_PULL_REQUEST_NEW_CHANGES) {
|
2020-11-30 00:03:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
ProjectFile::registerPerProviderHandler({
|
|
|
|
.basePath = "patches.json",
|
2022-09-20 15:33:36 +02:00
|
|
|
.required = false,
|
2023-12-27 16:33:49 +01:00
|
|
|
.load = [](prv::Provider *provider, const std::fs::path &basePath, const Tar &tar) {
|
2023-03-23 11:23:07 +01:00
|
|
|
auto json = nlohmann::json::parse(tar.readString(basePath));
|
2023-11-25 12:43:48 +01:00
|
|
|
auto patches = json.at("patches").get<std::map<u64, u8>>();
|
|
|
|
|
|
|
|
for (const auto &[address, value] : patches) {
|
|
|
|
provider->write(address, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
provider->getUndoStack().groupOperations(patches.size(), "hex.builtin.undo_operation.patches");
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
return true;
|
|
|
|
},
|
2023-11-25 12:43:48 +01:00
|
|
|
.store = [](prv::Provider *, const std::fs::path &, Tar &) {
|
2022-08-08 21:23:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 00:03:12 +01:00
|
|
|
});
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2024-02-15 22:09:37 +01:00
|
|
|
MovePerProviderData::subscribe(this, [this](prv::Provider *from, prv::Provider *to) {
|
|
|
|
m_savedOperations.get(from) = 0;
|
|
|
|
m_savedOperations.get(to) = 0;
|
|
|
|
});
|
|
|
|
|
2024-02-15 21:54:41 +01:00
|
|
|
ImHexApi::HexEditor::addForegroundHighlightingProvider([this](u64 offset, const u8* buffer, size_t, bool) -> std::optional<color_t> {
|
2022-10-08 10:00:37 +02:00
|
|
|
hex::unused(buffer);
|
|
|
|
|
2022-09-28 15:01:43 +02:00
|
|
|
if (!ImHexApi::Provider::isValid())
|
|
|
|
return std::nullopt;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2022-10-08 10:00:37 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
2024-03-10 14:31:39 +01:00
|
|
|
if (!provider->isSavable())
|
|
|
|
return std::nullopt;
|
2022-10-08 10:00:37 +02:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
offset -= provider->getBaseAddress();
|
2022-10-08 10:00:37 +02:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
const auto &undoStack = provider->getUndoStack();
|
2024-02-15 21:54:41 +01:00
|
|
|
const auto stackSize = undoStack.getAppliedOperations().size();
|
|
|
|
const auto savedStackSize = m_savedOperations.get(provider);
|
|
|
|
|
|
|
|
if (stackSize == savedStackSize) {
|
|
|
|
// Do nothing
|
|
|
|
} else if (stackSize > savedStackSize) {
|
|
|
|
for (const auto &operation : undoStack.getAppliedOperations() | std::views::drop(savedStackSize)) {
|
|
|
|
if (!operation->shouldHighlight())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (operation->getRegion().overlaps(Region { offset, 1}))
|
|
|
|
return ImGuiExt::GetCustomColorU32(ImGuiCustomCol_Patches);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const auto &operation : undoStack.getUndoneOperations() | std::views::reverse | std::views::take(savedStackSize - stackSize)) {
|
|
|
|
if (!operation->shouldHighlight())
|
|
|
|
continue;
|
2023-11-25 12:43:48 +01:00
|
|
|
|
2024-02-15 21:54:41 +01:00
|
|
|
if (operation->getRegion().overlaps(Region { offset, 1}))
|
|
|
|
return ImGuiExt::GetCustomColorU32(ImGuiCustomCol_Patches);
|
|
|
|
}
|
2023-11-25 12:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
2022-09-28 15:01:43 +02:00
|
|
|
});
|
2023-07-27 02:05:35 +02:00
|
|
|
|
2024-02-15 21:54:41 +01:00
|
|
|
EventProviderSaved::subscribe([this](prv::Provider *provider) {
|
|
|
|
m_savedOperations.get(provider) = provider->getUndoStack().getAppliedOperations().size();
|
2023-12-08 10:29:44 +01:00
|
|
|
EventHighlightingChanged::post();
|
2023-07-27 02:05:35 +02:00
|
|
|
});
|
2023-11-25 12:43:48 +01:00
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderDataModified::subscribe(this, [](prv::Provider *provider, u64 offset, u64 size, const u8 *data) {
|
2023-11-27 15:48:56 +01:00
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
offset -= provider->getBaseAddress();
|
|
|
|
|
|
|
|
std::vector<u8> oldData(size, 0x00);
|
|
|
|
provider->read(offset, oldData.data(), size);
|
|
|
|
provider->getUndoStack().add<undo::OperationWrite>(offset, size, oldData.data(), data);
|
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderDataInserted::subscribe(this, [](prv::Provider *provider, u64 offset, u64 size) {
|
2023-11-25 12:43:48 +01:00
|
|
|
offset -= provider->getBaseAddress();
|
|
|
|
|
|
|
|
provider->getUndoStack().add<undo::OperationInsert>(offset, size);
|
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderDataRemoved::subscribe(this, [](prv::Provider *provider, u64 offset, u64 size) {
|
2023-11-25 12:43:48 +01:00
|
|
|
offset -= provider->getBaseAddress();
|
|
|
|
|
|
|
|
provider->getUndoStack().add<undo::OperationRemove>(offset, size);
|
|
|
|
});
|
2020-11-29 02:06:41 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 22:09:37 +01:00
|
|
|
ViewPatches::~ViewPatches() {
|
|
|
|
MovePerProviderData::unsubscribe(this);
|
|
|
|
EventProviderSaved::unsubscribe(this);
|
|
|
|
EventProviderDataModified::unsubscribe(this);
|
|
|
|
EventProviderDataInserted::unsubscribe(this);
|
|
|
|
EventProviderDataRemoved::unsubscribe(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewPatches::drawContent() {
|
2023-11-21 13:47:50 +01:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
if (ImHexApi::Provider::isValid() && provider->isReadable()) {
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
if (ImGui::BeginTable("##patchesTable", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
2023-11-25 12:43:48 +01:00
|
|
|
ImGui::TableSetupColumn("##PatchID", ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoReorder | ImGuiTableColumnFlags_NoResize);
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.patches.offset"_lang, ImGuiTableColumnFlags_WidthFixed);
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.patches.patch"_lang, ImGuiTableColumnFlags_WidthStretch);
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TableHeadersRow();
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
const auto &undoRedoStack = provider->getUndoStack();
|
|
|
|
std::vector<prv::undo::Operation*> operations;
|
|
|
|
for (const auto &operation : undoRedoStack.getUndoneOperations())
|
|
|
|
operations.push_back(operation.get());
|
|
|
|
for (const auto &operation : undoRedoStack.getAppliedOperations() | std::views::reverse)
|
|
|
|
operations.push_back(operation.get());
|
|
|
|
|
|
|
|
u32 index = 0;
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGuiListClipper clipper;
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
clipper.Begin(operations.size());
|
2023-11-21 13:47:50 +01:00
|
|
|
while (clipper.Step()) {
|
2023-11-25 12:43:48 +01:00
|
|
|
auto iter = operations.begin();
|
2023-11-21 13:47:50 +01:00
|
|
|
for (auto i = 0; i < clipper.DisplayStart; i++)
|
|
|
|
++iter;
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
auto undoneOperationsCount = undoRedoStack.getUndoneOperations().size();
|
2023-11-21 13:47:50 +01:00
|
|
|
for (auto i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
|
2023-11-25 12:43:48 +01:00
|
|
|
const auto &operation = *iter;
|
|
|
|
|
|
|
|
const auto [address, size] = operation->getRegion();
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
ImGui::BeginDisabled(size_t(i) < undoneOperationsCount);
|
|
|
|
|
|
|
|
if (ImGui::Selectable(hex::format("{} {}", index == undoneOperationsCount ? ICON_VS_ARROW_SMALL_RIGHT : " ", index).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
|
|
|
|
ImHexApi::HexEditor::setSelection(address, size);
|
|
|
|
}
|
|
|
|
if (ImGui::IsItemHovered()) {
|
|
|
|
const auto content = operation->formatContent();
|
|
|
|
if (!content.empty()) {
|
|
|
|
if (ImGui::BeginTooltip()) {
|
|
|
|
if (ImGui::BeginTable("##content_table", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders)) {
|
|
|
|
for (const auto &entry : content) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
ImGuiExt::TextFormatted("{}", entry);
|
|
|
|
}
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 13:47:50 +01:00
|
|
|
}
|
|
|
|
if (ImGui::IsMouseReleased(1) && ImGui::IsItemHovered()) {
|
|
|
|
ImGui::OpenPopup("PatchContextMenu");
|
2023-12-19 13:10:25 +01:00
|
|
|
m_selectedPatch = address;
|
2023-11-21 13:47:50 +01:00
|
|
|
}
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TableNextColumn();
|
2023-11-25 12:43:48 +01:00
|
|
|
ImGuiExt::TextFormatted("0x{0:08X}", address);
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TableNextColumn();
|
2023-11-25 12:43:48 +01:00
|
|
|
ImGuiExt::TextFormatted("{}", operation->format());
|
2023-11-21 13:47:50 +01:00
|
|
|
index += 1;
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
++iter;
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
ImGui::EndDisabled();
|
2020-11-29 02:06:41 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-21 13:47:50 +01:00
|
|
|
|
|
|
|
ImGui::EndTable();
|
2020-11-29 02:06:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
void ViewPatches::drawAlwaysVisibleContent() {
|
2023-07-24 23:24:31 +02:00
|
|
|
if (auto provider = ImHexApi::Provider::get(); provider != nullptr) {
|
2023-11-25 12:43:48 +01:00
|
|
|
const auto &operations = provider->getUndoStack().getAppliedOperations();
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_numOperations.get(provider) != operations.size()) {
|
|
|
|
m_numOperations.get(provider) = operations.size();
|
2023-12-08 10:29:44 +01:00
|
|
|
EventHighlightingChanged::post();
|
2023-07-24 23:24:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 02:06:41 +01:00
|
|
|
}
|