1
0
mirror of synced 2024-11-16 03:53:22 +01:00
ImHex/plugins/builtin/source/content/views/view_patches.cpp

101 lines
4.1 KiB
C++
Raw Normal View History

2021-12-07 22:47:41 +01:00
#include "content/views/view_patches.hpp"
2020-11-29 02:06:41 +01:00
#include <hex/providers/provider.hpp>
2020-11-29 02:06:41 +01:00
2021-12-07 22:47:41 +01:00
#include <hex/helpers/project_file_handler.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") {
EventManager::subscribe<EventProjectFileStore>(this, []{
auto provider = ImHexApi::Provider::get();
if (ImHexApi::Provider::isValid())
ProjectFile::setPatches(provider->getPatches());
2020-11-30 00:03:12 +01:00
});
EventManager::subscribe<EventProjectFileLoad>(this, []{
auto provider = ImHexApi::Provider::get();
if (ImHexApi::Provider::isValid())
provider->getPatches() = ProjectFile::getPatches();
2020-11-30 00:03:12 +01:00
});
2020-11-29 02:06:41 +01:00
}
ViewPatches::~ViewPatches() {
EventManager::unsubscribe<EventProjectFileStore>(this);
EventManager::unsubscribe<EventProjectFileLoad>(this);
2020-11-29 02:06:41 +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)) {
auto provider = ImHexApi::Provider::get();
2020-11-29 02:06:41 +01:00
if (ImHexApi::Provider::isValid() && provider->isReadable()) {
2020-11-29 02:06:41 +01:00
if (ImGui::BeginTable("##patchesTable", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable |
ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
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();
auto& patches = provider->getPatches();
2020-11-29 02:06:41 +01:00
u32 index = 0;
ImGuiListClipper clipper(patches.size());
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
ImGui::TableNextRow();
ImGui::TableNextColumn();
2020-11-29 02:06:41 +01:00
if (ImGui::Selectable(("##patchLine" + std::to_string(index)).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
EventManager::post<RequestSelectionChange>(Region { address, 1 });
}
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);
2020-11-30 00:03:12 +01:00
ProjectFile::markDirty();
2020-11-29 02:06:41 +01:00
}
ImGui::EndPopup();
}
ImGui::EndTable();
}
}
}
ImGui::End();
}
}