1
0
mirror of synced 2024-12-01 10:47:19 +01:00

ux: Added support for mathematical expressions in goto fields

This commit is contained in:
WerWolv 2022-03-22 09:06:02 +01:00
parent f7cfee55d5
commit ea848dbfc0
2 changed files with 67 additions and 29 deletions

View File

@ -51,9 +51,9 @@ namespace hex::plugin::builtin {
std::vector<std::pair<u64, u64>> m_lastStringSearch; std::vector<std::pair<u64, u64>> m_lastStringSearch;
std::vector<std::pair<u64, u64>> m_lastHexSearch; std::vector<std::pair<u64, u64>> m_lastHexSearch;
u64 m_gotoAddressAbsolute = 0; std::string m_gotoAddressInput;
i64 m_gotoAddressRelative = 0; bool m_gotoRequested = false;
bool m_gotoRequested = false; bool m_evaluateGoto = false;
u64 m_baseAddress = 0; u64 m_baseAddress = 0;
u64 m_resizeSize = 0; u64 m_resizeSize = 0;

View File

@ -13,6 +13,7 @@
#include <content/providers/file_provider.hpp> #include <content/providers/file_provider.hpp>
#include "math_evaluator.hpp"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -683,7 +684,8 @@ namespace hex::plugin::builtin {
ImGui::SetNextWindowPos(ImGui::GetWindowPos() + ImGui::GetWindowContentRegionMin() - ImGui::GetStyle().WindowPadding); ImGui::SetNextWindowPos(ImGui::GetWindowPos() + ImGui::GetWindowContentRegionMin() - ImGui::GetStyle().WindowPadding);
if (ImGui::BeginPopup("hex.builtin.view.hex_editor.menu.file.goto"_lang)) { if (ImGui::BeginPopup("hex.builtin.view.hex_editor.menu.file.goto"_lang)) {
bool runGoto = false; bool runGoto = this->m_evaluateGoto;
this->m_evaluateGoto = false;
if (ImGui::BeginTabBar("gotoTabs")) { if (ImGui::BeginTabBar("gotoTabs")) {
u64 newOffset = 0; u64 newOffset = 0;
@ -693,12 +695,19 @@ namespace hex::plugin::builtin {
this->m_gotoRequested = false; this->m_gotoRequested = false;
} }
runGoto = ImGui::InputHexadecimal("##goto", &this->m_gotoAddressAbsolute, ImGuiInputTextFlags_EnterReturnsTrue); runGoto = ImGui::InputText("##goto", this->m_gotoAddressInput.data(), this->m_gotoAddressInput.size(), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackResize, ImGui::UpdateStringSizeCallback, &this->m_gotoAddressInput) || runGoto;
if (runGoto) {
if (this->m_gotoAddressAbsolute < baseAddress || this->m_gotoAddressAbsolute > baseAddress + dataSize) MathEvaluator evaluator;
this->m_gotoAddressAbsolute = baseAddress; auto result = evaluator.evaluate(this->m_gotoAddressInput);
if (result) {
newOffset = this->m_gotoAddressAbsolute; if (*result < baseAddress || *result > baseAddress + dataSize)
newOffset = baseAddress;
else
newOffset = *result;
} else {
runGoto = false;
}
}
ImGui::EndTabItem(); ImGui::EndTabItem();
} }
@ -707,43 +716,72 @@ namespace hex::plugin::builtin {
ImGui::SetKeyboardFocusHere(); ImGui::SetKeyboardFocusHere();
this->m_gotoRequested = false; this->m_gotoRequested = false;
} }
runGoto = ImGui::InputScalar("##goto", ImGuiDataType_U64, &this->m_gotoAddressAbsolute, nullptr, nullptr, "%llx", ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_EnterReturnsTrue); runGoto = ImGui::InputText("##goto", this->m_gotoAddressInput.data(), this->m_gotoAddressInput.size(), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackResize, ImGui::UpdateStringSizeCallback, &this->m_gotoAddressInput) || runGoto;
if (runGoto) {
if (this->m_gotoAddressAbsolute < 0 || this->m_gotoAddressAbsolute > dataSize) MathEvaluator evaluator;
this->m_gotoAddressAbsolute = 0; auto result = evaluator.evaluate(this->m_gotoAddressInput);
if (result) {
newOffset = this->m_gotoAddressAbsolute + baseAddress; if (*result < 0 || *result > dataSize)
newOffset = 0;
else
newOffset = *result;
} else {
runGoto = false;
}
}
ImGui::EndTabItem(); ImGui::EndTabItem();
} }
if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.goto.offset.current"_lang)) { if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.goto.offset.current"_lang)) {
ImGui::SetKeyboardFocusHere(); ImGui::SetKeyboardFocusHere();
runGoto = ImGui::InputScalar("##goto", ImGuiDataType_S64, &this->m_gotoAddressRelative, nullptr, nullptr, "%lld", ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_EnterReturnsTrue); runGoto = ImGui::InputText("##goto", this->m_gotoAddressInput.data(), this->m_gotoAddressInput.size(), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackResize, ImGui::UpdateStringSizeCallback, &this->m_gotoAddressInput) || runGoto;
if (runGoto) {
MathEvaluator evaluator;
auto result = evaluator.evaluate(this->m_gotoAddressInput);
if (result) {
i64 currSelectionOffset = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
i64 currSelectionOffset = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); newOffset = 0;
if (currSelectionOffset + *result < 0)
newOffset = -currSelectionOffset;
else if (currSelectionOffset + *result > dataSize)
newOffset = dataSize - currSelectionOffset;
else
newOffset = *result;
if (currSelectionOffset + this->m_gotoAddressRelative < 0) newOffset = currSelectionOffset + *result + baseAddress;
this->m_gotoAddressRelative = -currSelectionOffset; } else {
else if (currSelectionOffset + this->m_gotoAddressRelative > dataSize) runGoto = false;
this->m_gotoAddressRelative = dataSize - currSelectionOffset; }
}
newOffset = currSelectionOffset + this->m_gotoAddressRelative + baseAddress;
ImGui::EndTabItem(); ImGui::EndTabItem();
} }
if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.goto.offset.end"_lang)) { if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.goto.offset.end"_lang)) {
ImGui::SetKeyboardFocusHere(); ImGui::SetKeyboardFocusHere();
runGoto = ImGui::InputHexadecimal("##goto", &this->m_gotoAddressAbsolute, ImGuiInputTextFlags_EnterReturnsTrue); runGoto = ImGui::InputText("##goto", this->m_gotoAddressInput.data(), this->m_gotoAddressInput.size(), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackResize, ImGui::UpdateStringSizeCallback, &this->m_gotoAddressInput) || runGoto;
if (runGoto) {
MathEvaluator evaluator;
auto result = evaluator.evaluate(this->m_gotoAddressInput);
if (result) {
if (*result < 0 || *result > dataSize)
newOffset = 0;
if (this->m_gotoAddressAbsolute < 0 || this->m_gotoAddressAbsolute > dataSize) newOffset = (baseAddress + dataSize) - *result - 1;
this->m_gotoAddressAbsolute = 0; } else {
runGoto = false;
newOffset = (baseAddress + dataSize) - this->m_gotoAddressAbsolute - 1; }
}
ImGui::EndTabItem(); ImGui::EndTabItem();
} }
if (ImGui::Button("hex.builtin.view.hex_editor.menu.file.goto"_lang) || runGoto) { if (ImGui::Button("hex.builtin.view.hex_editor.menu.file.goto"_lang)) {
this->m_evaluateGoto = true;
}
if (runGoto) {
this->m_gotoRequested = true; this->m_gotoRequested = true;
provider->setCurrentPage(std::floor(double(newOffset - baseAddress) / hex::prv::Provider::PageSize)); provider->setCurrentPage(std::floor(double(newOffset - baseAddress) / hex::prv::Provider::PageSize));
ImHexApi::HexEditor::setSelection(newOffset, 1); ImHexApi::HexEditor::setSelection(newOffset, 1);