From 0e2add204b9bb6cefda8f2847f606fe0ee09ff24 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 22 Feb 2021 11:56:33 +0100 Subject: [PATCH] ux: Disassemble asynchronously --- include/views/view_disassembler.hpp | 4 ++-- include/views/view_pattern.hpp | 2 +- plugins/builtin/source/lang/en_US.cpp | 7 +++--- source/views/view_disassembler.cpp | 32 +++++++++++++++++++-------- source/views/view_pattern.cpp | 12 +++++----- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/include/views/view_disassembler.hpp b/include/views/view_disassembler.hpp index 8349e67c4..831467834 100644 --- a/include/views/view_disassembler.hpp +++ b/include/views/view_disassembler.hpp @@ -30,7 +30,7 @@ namespace hex { void drawMenu() override; private: - bool m_shouldInvalidate = false; + bool m_disassembling = false; u64 m_baseAddress = 0; u64 m_codeRegion[2] = { 0 }; @@ -42,7 +42,7 @@ namespace hex { std::vector m_disassembly; - + void disassemble(); }; } \ No newline at end of file diff --git a/include/views/view_pattern.hpp b/include/views/view_pattern.hpp index 36782c5cc..c9de2badc 100644 --- a/include/views/view_pattern.hpp +++ b/include/views/view_pattern.hpp @@ -32,7 +32,7 @@ namespace hex { std::vector m_possiblePatternFiles; int m_selectedPatternFile = 0; bool m_runAutomatically = false; - bool m_compilerRunning = false; + bool m_evaluatorRunning = false; TextEditor m_textEditor; std::vector> m_console; diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 6b7ca3621..d9cae8f57 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -108,6 +108,7 @@ namespace hex::plugin::builtin { { "hex.view.disassembler.sparc.v9", "Sparc V9 mode" }, { "hex.view.disassembler.disassemble", "Disassemble" }, + { "hex.view.disassembler.disassembling", "[%c] Disassembling..." }, { "hex.view.disassembler.disassembly.title", "Disassembly" }, { "hex.view.disassembler.disassembly.address", "Address" }, { "hex.view.disassembler.disassembly.offset", "Offset" }, @@ -220,8 +221,8 @@ namespace hex::plugin::builtin { { "hex.view.pattern.accept_pattern.question", "Do you want to apply the selected pattern?" }, { "hex.view.pattern.menu.file.load_pattern", "Load pattern..." }, { "hex.view.pattern.open_pattern", "Open pattern" }, - { "hex.view.pattern.compiling", "[%c] Compiling..." }, - { "hex.view.pattern.auto", "Auto compile" }, + { "hex.view.pattern.evaluating", "[%c] Evaluating..." }, + { "hex.view.pattern.auto", "Auto evaluate" }, { "hex.view.pattern_data.name", "Pattern Data" }, { "hex.view.pattern_data.name", "Name" }, @@ -239,7 +240,7 @@ namespace hex::plugin::builtin { { "hex.view.strings.min_length", "Minimum length" }, { "hex.view.strings.filter", "Filter" }, { "hex.view.strings.extract", "Extract" }, - { "hex.view.strings.searching", "[%c] Searching..."} + { "hex.view.strings.searching", "[%c] Searching..." }, { "hex.view.strings.offset", "Offset" }, { "hex.view.strings.size", "Size" }, { "hex.view.strings.string", "String" }, diff --git a/source/views/view_disassembler.cpp b/source/views/view_disassembler.cpp index e6fba7d51..ef9461856 100644 --- a/source/views/view_disassembler.cpp +++ b/source/views/view_disassembler.cpp @@ -4,6 +4,9 @@ #include #include +#include + +#include using namespace std::literals::string_literals; @@ -11,7 +14,7 @@ namespace hex { ViewDisassembler::ViewDisassembler() : View("hex.view.disassembler.name"_lang) { View::subscribeEvent(Events::DataChanged, [this](auto){ - this->m_shouldInvalidate = true; + this->disassemble(); }); View::subscribeEvent(Events::RegionSelected, [this](auto userData) { @@ -29,10 +32,11 @@ namespace hex { View::unsubscribeEvent(Events::RegionSelected); } - void ViewDisassembler::drawContent() { - if (this->m_shouldInvalidate) { - this->m_disassembly.clear(); + void ViewDisassembler::disassemble() { + this->m_disassembly.clear(); + this->m_disassembling = true; + std::thread([this] { csh capstoneHandle; cs_insn *instructions = nullptr; @@ -89,9 +93,12 @@ namespace hex { cs_close(&capstoneHandle); } - this->m_shouldInvalidate = false; - } + this->m_disassembling = false; + }).detach(); + } + + void ViewDisassembler::drawContent() { if (ImGui::Begin("hex.view.disassembler.name"_lang, &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { @@ -235,9 +242,16 @@ namespace hex { } ImGui::EndChild(); - ImGui::SetCursorPosX((ImGui::GetContentRegionAvail().x - 300) / 2); - if (ImGui::Button("hex.view.disassembler.disassemble"_lang, ImVec2(300, 20))) - this->m_shouldInvalidate = true; + ImGui::Disabled([this] { + if (ImGui::Button("hex.view.disassembler.disassemble"_lang)) + this->disassemble(); + }, this->m_disassembling); + + if (this->m_disassembling) { + ImGui::SameLine(); + ImGui::Text("hex.view.disassembler.disassembling"_lang, "|/-\\"[u8(ImGui::GetTime() * 20) % 4]); + } + ImGui::NewLine(); ImGui::TextUnformatted("hex.view.disassembler.disassembly.title"_lang); diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index 69af337bc..1ae2833d1 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -258,16 +258,16 @@ namespace hex { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(ImColor(0x20, 0x85, 0x20))); ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1); - if (ImGui::ArrowButton("compile", ImGuiDir_Right)) + if (ImGui::ArrowButton("evaluate", ImGuiDir_Right)) this->parsePattern(this->m_textEditor.GetText().data()); ImGui::PopStyleVar(); ImGui::PopStyleColor(); - }, this->m_compilerRunning); + }, this->m_evaluatorRunning); ImGui::SameLine(); - if (this->m_compilerRunning) - ImGui::Text("hex.view.pattern.compiling"_lang, "|/-\\"[u8(ImGui::GetTime() * 20) % 4]); + if (this->m_evaluatorRunning) + ImGui::Text("hex.view.pattern.evaluating"_lang, "|/-\\"[u8(ImGui::GetTime() * 20) % 4]); else ImGui::Checkbox("hex.view.pattern.auto"_lang, &this->m_runAutomatically); @@ -344,7 +344,7 @@ namespace hex { } void ViewPattern::parsePattern(char *buffer) { - this->m_compilerRunning = true; + this->m_evaluatorRunning = true; this->clearPatternData(); this->m_textEditor.SetErrorMarkers({ }); @@ -366,7 +366,7 @@ namespace hex { View::doLater([]{ View::postEvent(Events::PatternChanged); }); } - this->m_compilerRunning = false; + this->m_evaluatorRunning = false; }).detach(); }