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
|
|
|
|
|
|
|
#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
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ViewPatches::ViewPatches() : View("hex.builtin.view.patches.name") {
|
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,
|
2022-08-08 21:23:52 +02:00
|
|
|
.load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) {
|
2023-03-23 11:23:07 +01:00
|
|
|
auto json = nlohmann::json::parse(tar.readString(basePath));
|
2023-05-16 11:33:00 +02:00
|
|
|
provider->getPatches() = json.at("patches").get<std::map<u64, u8>>();
|
2022-08-08 21:23:52 +02:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
.store = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) {
|
|
|
|
nlohmann::json json;
|
|
|
|
json["patches"] = provider->getPatches();
|
2023-03-23 11:23:07 +01:00
|
|
|
tar.writeString(basePath, json.dump(4));
|
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
|
|
|
|
2022-09-28 15:01:43 +02:00
|
|
|
ImHexApi::HexEditor::addForegroundHighlightingProvider([](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();
|
|
|
|
|
|
|
|
u8 byte = 0x00;
|
2023-03-07 16:03:34 +01:00
|
|
|
provider->read(offset, &byte, sizeof(u8), false);
|
2022-10-08 10:00:37 +02:00
|
|
|
|
|
|
|
const auto &patches = provider->getPatches();
|
|
|
|
if (patches.contains(offset) && patches.at(offset) != byte)
|
2023-10-20 12:30:21 +02:00
|
|
|
return ImGui::GetCustomColorU32(ImGuiCustomCol_Patches);
|
2022-09-28 15:01:43 +02:00
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
});
|
2023-07-27 02:05:35 +02:00
|
|
|
|
|
|
|
EventManager::subscribe<EventProviderSaved>([](auto *) {
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
|
|
|
});
|
2020-11-29 02:06:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewPatches::drawContent() {
|
2021-12-07 22:47:41 +01:00
|
|
|
if (ImGui::Begin(View::toWindowName("hex.builtin.view.patches.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
|
2021-09-21 02:29:54 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
if (ImHexApi::Provider::isValid() && provider->isReadable()) {
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (ImGui::BeginTable("##patchesTable", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
2020-11-29 02:06:41 +01:00
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.patches.offset"_lang);
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.patches.orig"_lang);
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.patches.patch"_lang);
|
2020-11-29 02:06:41 +01:00
|
|
|
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
auto &patches = provider->getPatches();
|
2022-02-01 22:09:44 +01:00
|
|
|
u32 index = 0;
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2022-08-03 23:32:34 +02:00
|
|
|
ImGuiListClipper clipper;
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2022-08-03 23:32:34 +02:00
|
|
|
clipper.Begin(patches.size());
|
2022-01-09 21:27:59 +01:00
|
|
|
while (clipper.Step()) {
|
|
|
|
auto iter = patches.begin();
|
|
|
|
for (auto i = 0; i < clipper.DisplayStart; i++)
|
|
|
|
iter++;
|
|
|
|
|
|
|
|
for (auto i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
|
|
|
|
const auto &[address, patch] = *iter;
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2020-11-29 02:06:41 +01:00
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
if (ImGui::Selectable(("##patchLine" + std::to_string(index)).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
|
2022-02-08 18:38:54 +01:00
|
|
|
ImHexApi::HexEditor::setSelection(address, 1);
|
2022-01-09 21:27:59 +01:00
|
|
|
}
|
|
|
|
if (ImGui::IsMouseReleased(1) && ImGui::IsItemHovered()) {
|
|
|
|
ImGui::OpenPopup("PatchContextMenu");
|
|
|
|
this->m_selectedPatch = address;
|
|
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextFormatted("0x{0:08X}", address);
|
|
|
|
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
u8 previousValue = 0x00;
|
|
|
|
provider->readRaw(address, &previousValue, sizeof(u8));
|
|
|
|
ImGui::TextFormatted("0x{0:02X}", previousValue);
|
|
|
|
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormatted("0x{0:02X}", patch);
|
|
|
|
index += 1;
|
|
|
|
|
|
|
|
iter++;
|
|
|
|
}
|
2020-11-29 02:06:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup("PatchContextMenu")) {
|
2021-12-07 22:47:41 +01:00
|
|
|
if (ImGui::MenuItem("hex.builtin.view.patches.remove"_lang)) {
|
2020-11-29 02:06:41 +01:00
|
|
|
patches.erase(this->m_selectedPatch);
|
|
|
|
}
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
2023-07-24 23:24:31 +02:00
|
|
|
void ViewPatches::drawAlwaysVisible() {
|
|
|
|
if (auto provider = ImHexApi::Provider::get(); provider != nullptr) {
|
|
|
|
const auto &patches = provider->getPatches();
|
|
|
|
if (this->m_numPatches.get(provider) != patches.size()) {
|
|
|
|
this->m_numPatches.get(provider) = patches.size();
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 02:06:41 +01:00
|
|
|
}
|