From 72f433170347e8a48c86cc0ba69c78d9d2c35130 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 11 Jan 2024 20:11:22 +0100 Subject: [PATCH] feat: Added virtual files to the pattern language --- .../include/hex/api/event_manager.hpp | 1 + lib/libimhex/include/hex/api/imhex_api.hpp | 8 +++ .../include/hex/providers/memory_provider.hpp | 10 ++- .../include/hex/providers/provider.hpp | 5 ++ .../include/hex/providers/provider_data.hpp | 15 +++- lib/libimhex/source/api/imhex_api.cpp | 3 + .../content/views/view_pattern_data.hpp | 5 +- .../content/views/view_pattern_editor.hpp | 16 ++++- plugins/builtin/romfs/lang/en_US.json | 1 + .../source/content/pl_builtin_functions.cpp | 15 ++++ .../content/views/view_pattern_data.cpp | 37 +++++----- .../content/views/view_pattern_editor.cpp | 70 ++++++++++++++++++- plugins/ui/include/ui/hex_editor.hpp | 12 +++- 13 files changed, 171 insertions(+), 27 deletions(-) diff --git a/lib/libimhex/include/hex/api/event_manager.hpp b/lib/libimhex/include/hex/api/event_manager.hpp index 455b576aa..9745568ff 100644 --- a/lib/libimhex/include/hex/api/event_manager.hpp +++ b/lib/libimhex/include/hex/api/event_manager.hpp @@ -285,6 +285,7 @@ namespace hex { EVENT_DEF(RequestOpenFile, std::fs::path); EVENT_DEF(RequestChangeTheme, std::string); EVENT_DEF(RequestOpenPopup, std::string); + EVENT_DEF(RequestAddVirtualFile, std::fs::path, std::vector, Region); /** * @brief Creates a provider from it's unlocalized name, and add it to the provider list diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index 16d2f800d..c937d9036 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -202,6 +202,14 @@ namespace hex { */ void setSelection(u64 address, size_t size, prv::Provider *provider = nullptr); + /** + * @brief Adds a virtual file to the list in the Hex Editor + * @param path The path of the file + * @param data The data of the file + * @param region The location of the file in the Hex Editor if available + */ + void addVirtualFile(const std::fs::path &path, std::vector data, Region region = Region::Invalid()); + } /* Functions to interact with Bookmarks */ diff --git a/lib/libimhex/include/hex/providers/memory_provider.hpp b/lib/libimhex/include/hex/providers/memory_provider.hpp index 5c7508a9c..33bfb7aed 100644 --- a/lib/libimhex/include/hex/providers/memory_provider.hpp +++ b/lib/libimhex/include/hex/providers/memory_provider.hpp @@ -11,9 +11,15 @@ namespace hex::prv { class MemoryProvider : public hex::prv::Provider { public: MemoryProvider() = default; - explicit MemoryProvider(std::vector data) : m_data(std::move(data)) { } + explicit MemoryProvider(std::vector data, std::string name = "") : m_data(std::move(data)), m_name(std::move(name)) { } ~MemoryProvider() override = default; + MemoryProvider(const MemoryProvider&) = delete; + MemoryProvider& operator=(const MemoryProvider&) = delete; + + MemoryProvider(MemoryProvider &&provider) noexcept = default; + MemoryProvider& operator=(MemoryProvider &&provider) noexcept = default; + [[nodiscard]] bool isAvailable() const override { return true; } [[nodiscard]] bool isReadable() const override { return true; } [[nodiscard]] bool isWritable() const override { return true; } @@ -32,7 +38,7 @@ namespace hex::prv { void insertRaw(u64 offset, u64 size) override; void removeRaw(u64 offset, u64 size) override; - [[nodiscard]] std::string getName() const override { return ""; } + [[nodiscard]] std::string getName() const override { return m_name; } [[nodiscard]] std::string getTypeName() const override { return "MemoryProvider"; } private: diff --git a/lib/libimhex/include/hex/providers/provider.hpp b/lib/libimhex/include/hex/providers/provider.hpp index e17d147ca..8ef87d93f 100644 --- a/lib/libimhex/include/hex/providers/provider.hpp +++ b/lib/libimhex/include/hex/providers/provider.hpp @@ -37,6 +37,11 @@ namespace hex::prv { Provider(); virtual ~Provider(); + Provider(const Provider&) = delete; + Provider& operator=(const Provider&) = delete; + + Provider(Provider &&provider) noexcept = default; + Provider& operator=(Provider &&provider) noexcept = default; /** * @brief Opens this provider diff --git a/lib/libimhex/include/hex/providers/provider_data.hpp b/lib/libimhex/include/hex/providers/provider_data.hpp index 05ad034e3..1ef62e056 100644 --- a/lib/libimhex/include/hex/providers/provider_data.hpp +++ b/lib/libimhex/include/hex/providers/provider_data.hpp @@ -4,6 +4,7 @@ #include #include +#include #include namespace hex { @@ -71,10 +72,21 @@ namespace hex { return this->get(); } + auto all() { + return m_data | std::views::values; + } + + void setOnCreateCallback(std::function callback) { + m_onCreateCallback = std::move(callback); + } + private: void onCreate() { EventProviderOpened::subscribe(this, [this](prv::Provider *provider) { - m_data.emplace(provider, T()); + auto [it, inserted] = m_data.emplace(provider, T()); + auto &[key, value] = *it; + if (m_onCreateCallback) + m_onCreateCallback(key, value); }); EventProviderDeleted::subscribe(this, [this](prv::Provider *provider){ @@ -111,6 +123,7 @@ namespace hex { private: std::map m_data; + std::function m_onCreateCallback; }; } \ No newline at end of file diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 45f404e66..73f209f4f 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -197,6 +197,9 @@ namespace hex { setSelection({ { address, size }, provider == nullptr ? Provider::get() : provider }); } + void addVirtualFile(const std::fs::path &path, std::vector data, Region region) { + RequestAddVirtualFile::post(path, std::move(data), region); + } } diff --git a/plugins/builtin/include/content/views/view_pattern_data.hpp b/plugins/builtin/include/content/views/view_pattern_data.hpp index 3f37b87f4..17eab426b 100644 --- a/plugins/builtin/include/content/views/view_pattern_data.hpp +++ b/plugins/builtin/include/content/views/view_pattern_data.hpp @@ -14,7 +14,10 @@ namespace hex::plugin::builtin { void drawContent() override; private: - std::unique_ptr m_patternDrawer; + bool m_rowColoring = false; + ui::PatternDrawer::TreeStyle m_treeStyle = ui::PatternDrawer::TreeStyle::Default; + + PerProvider> m_patternDrawer; }; } \ No newline at end of file diff --git a/plugins/builtin/include/content/views/view_pattern_editor.hpp b/plugins/builtin/include/content/views/view_pattern_editor.hpp index 17486e980..3fd51761c 100644 --- a/plugins/builtin/include/content/views/view_pattern_editor.hpp +++ b/plugins/builtin/include/content/views/view_pattern_editor.hpp @@ -35,13 +35,20 @@ namespace hex::plugin::builtin { return ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse; } - private: + public: + struct VirtualFile { + std::fs::path path; + std::vector data; + Region region; + }; + enum class DangerousFunctionPerms : u8 { Ask, Allow, Deny }; + private: class PopupAcceptPattern : public Popup { public: explicit PopupAcceptPattern(ViewPatternEditor *view) : Popup("hex.builtin.view.pattern_editor.accept_pattern"), m_view(view) {} @@ -145,7 +152,7 @@ namespace hex::plugin::builtin { bool m_triggerEvaluation = false; std::atomic m_triggerAutoEvaluate = false; - bool m_lastEvaluationProcessed = true; + volatile bool m_lastEvaluationProcessed = true; bool m_lastEvaluationResult = false; std::atomic m_runningEvaluators = 0; @@ -177,6 +184,8 @@ namespace hex::plugin::builtin { PerProvider> m_patternVariables; PerProvider> m_sections; + PerProvider> m_virtualFiles; + PerProvider> m_envVarEntries; PerProvider m_shouldAnalyze; @@ -185,7 +194,7 @@ namespace hex::plugin::builtin { std::atomic m_resetDebuggerVariables; int m_debuggerScopeIndex = 0; - std::array m_accessHistory; + std::array m_accessHistory = {}; u32 m_accessHistoryIndex = 0; private: @@ -193,6 +202,7 @@ namespace hex::plugin::builtin { void drawEnvVars(ImVec2 size, std::list &envVars); void drawVariableSettings(ImVec2 size, std::map &patternVariables); void drawSectionSelector(ImVec2 size, const std::map §ions); + void drawVirtualFiles(ImVec2 size, const std::vector &virtualFiles) const; void drawDebugger(ImVec2 size); void drawPatternTooltip(pl::ptrn::Pattern *pattern); diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 47bc521f9..5d538187d 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -876,6 +876,7 @@ "hex.builtin.view.pattern_editor.shortcut.step_debugger": "Step Debugger", "hex.builtin.view.pattern_editor.shortcut.continue_debugger": "Continue Debugger", "hex.builtin.view.pattern_editor.shortcut.add_breakpoint": "Add Breakpoint", + "hex.builtin.view.pattern_editor.virtual_files": "Virtual Filesystem", "hex.builtin.view.provider_settings.load_error": "An error occurred while trying to open this provider!", "hex.builtin.view.provider_settings.load_error_details": "An error occurred while trying to open this provider!\nDetails: {}", "hex.builtin.view.provider_settings.load_popup": "Open Provider", diff --git a/plugins/builtin/source/content/pl_builtin_functions.cpp b/plugins/builtin/source/content/pl_builtin_functions.cpp index 95f47e94f..d2ec0c461 100644 --- a/plugins/builtin/source/content/pl_builtin_functions.cpp +++ b/plugins/builtin/source/content/pl_builtin_functions.cpp @@ -8,6 +8,7 @@ #include #include +#include namespace hex::plugin::builtin { @@ -27,6 +28,20 @@ namespace hex::plugin::builtin { return u128(u128(selection->getStartAddress()) << 64 | u128(selection->getSize())); }); + + /* add_virtual_file(path, pattern) */ + ContentRegistry::PatternLanguage::addFunction(nsHexCore, "add_virtual_file", FunctionParameterCount::exactly(2), [](Evaluator *, auto params) -> std::optional { + auto path = params[0].toString(false); + auto pattern = params[1].toPattern(); + + Region region = Region::Invalid(); + if (pattern->getSection() == pl::ptrn::Pattern::MainSectionId) + region = Region(pattern->getOffset(), pattern->getSize()); + + ImHexApi::HexEditor::addVirtualFile(path, pattern->getBytes(), region); + + return std::nullopt; + }); } { diff --git a/plugins/builtin/source/content/views/view_pattern_data.cpp b/plugins/builtin/source/content/views/view_pattern_data.cpp index c41d11616..25f3709cd 100644 --- a/plugins/builtin/source/content/views/view_pattern_data.cpp +++ b/plugins/builtin/source/content/views/view_pattern_data.cpp @@ -2,43 +2,44 @@ #include +#include + #include #include namespace hex::plugin::builtin { ViewPatternData::ViewPatternData() : View::Window("hex.builtin.view.pattern_data.name", ICON_VS_DATABASE) { - m_patternDrawer = std::make_unique(); - // Handle tree style setting changes EventSettingsChanged::subscribe(this, [this] { - auto patternStyle = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_tree_style", 0); - m_patternDrawer->setTreeStyle(patternStyle); + m_treeStyle = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_tree_style", 0); + for (auto &drawer : m_patternDrawer.all()) + drawer->setTreeStyle(m_treeStyle); - auto rowColoring = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_data_row_bg", false); - m_patternDrawer->enableRowColoring(rowColoring); - }); - - // Reset the pattern drawer when the provider changes - EventProviderChanged::subscribe(this, [this](auto, auto) { - m_patternDrawer->reset(); + m_rowColoring = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_data_row_bg", false); + for (auto &drawer : m_patternDrawer.all()) + drawer->enableRowColoring(m_rowColoring); }); EventPatternEvaluating::subscribe(this, [this]{ - m_patternDrawer->reset(); + (*m_patternDrawer)->reset(); }); EventPatternExecuted::subscribe(this, [this](auto){ - m_patternDrawer->reset(); + (*m_patternDrawer)->reset(); }); - // Handle jumping to a pattern's location when it is clicked - m_patternDrawer->setSelectionCallback([](Region region){ ImHexApi::HexEditor::setSelection(region); }); + m_patternDrawer.setOnCreateCallback([this](prv::Provider *, auto &drawer) { + drawer = std::make_unique(); + + drawer->setSelectionCallback([](Region region){ ImHexApi::HexEditor::setSelection(region); }); + drawer->setTreeStyle(m_treeStyle); + drawer->enableRowColoring(m_rowColoring); + }); } ViewPatternData::~ViewPatternData() { EventSettingsChanged::unsubscribe(this); - EventProviderChanged::unsubscribe(this); EventPatternEvaluating::unsubscribe(this); EventPatternExecuted::unsubscribe(this); } @@ -51,11 +52,11 @@ namespace hex::plugin::builtin { const auto height = std::max(ImGui::GetContentRegionAvail().y - ImGui::GetTextLineHeightWithSpacing() - ImGui::GetStyle().FramePadding.y * 2, ImGui::GetTextLineHeightWithSpacing() * 5); if (!runtime.arePatternsValid()) { - m_patternDrawer->draw({ }, nullptr, height); + (*m_patternDrawer)->draw({ }, nullptr, height); } else { // If the runtime has finished evaluating, draw the patterns if (TRY_LOCK(ContentRegistry::PatternLanguage::getRuntimeLock())) { - m_patternDrawer->draw(runtime.getPatterns(), &runtime, height); + (*m_patternDrawer)->draw(runtime.getPatterns(), &runtime, height); } } } diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 9aa14810c..3dea5e956 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -129,6 +129,49 @@ namespace hex::plugin::builtin { return langDef; } + static void drawVirtualFileTree(const std::vector &virtualFiles, u32 level = 0) { + std::map> currFolderEntries; + for (const auto &file : virtualFiles) { + const auto &path = file->path; + + auto currSegment = wolv::util::toUTF8String(*std::next(path.begin(), level)); + if (std::distance(path.begin(), path.end()) == (level + 1)) { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + + ImGui::TextUnformatted(ICON_VS_FILE); + ImGui::SameLine(); + + ImGui::TreeNodeEx(currSegment.c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen); + + if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { + ImHexApi::Provider::add(file->data, wolv::util::toUTF8String(file->path.filename())); + } + + continue; + } + + currFolderEntries[currSegment].emplace_back(file); + } + + for (const auto &[segment, entries] : currFolderEntries) { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + + if (level == 0) { + ImGui::TextUnformatted(ICON_VS_DATABASE); + } else { + ImGui::TextUnformatted(ICON_VS_FOLDER); + } + + ImGui::SameLine(); + if (ImGui::TreeNodeEx(segment.c_str(), ImGuiTreeNodeFlags_SpanFullWidth)) { + drawVirtualFileTree(entries, level + 1); + ImGui::TreePop(); + } + } + } + ViewPatternEditor::ViewPatternEditor() : View::Window("hex.builtin.view.pattern_editor.name", ICON_VS_SYMBOL_NAMESPACE) { m_parserRuntime = std::make_unique(); ContentRegistry::PatternLanguage::configureRuntime(*m_parserRuntime, nullptr); @@ -153,6 +196,7 @@ namespace hex::plugin::builtin { EventFileLoaded::unsubscribe(this); EventProviderChanged::unsubscribe(this); EventProviderClosed::unsubscribe(this); + RequestAddVirtualFile::unsubscribe(this); } void ViewPatternEditor::drawContent() { @@ -237,6 +281,10 @@ namespace hex::plugin::builtin { this->drawSectionSelector(settingsSize, *m_sections); ImGui::EndTabItem(); } + if (ImGui::BeginTabItem("hex.builtin.view.pattern_editor.virtual_files"_lang)) { + this->drawVirtualFiles(settingsSize, *m_virtualFiles); + ImGui::EndTabItem(); + } if (ImGui::BeginTabItem("hex.builtin.view.pattern_editor.debugger"_lang)) { this->drawDebugger(settingsSize); ImGui::EndTabItem(); @@ -296,7 +344,7 @@ namespace hex::plugin::builtin { const auto dataSize = runtime.getInternals().evaluator->getDataSize(); const auto insertPos = [&, this](u64 address, u32 color) { - const auto progress = (address - dataBaseAddress) / float(dataSize); + const auto progress = float(address - dataBaseAddress) / float(dataSize); m_accessHistory[m_accessHistoryIndex] = { progress, color }; m_accessHistoryIndex = (m_accessHistoryIndex + 1) % m_accessHistory.size(); @@ -659,6 +707,22 @@ namespace hex::plugin::builtin { } } + void ViewPatternEditor::drawVirtualFiles(ImVec2 size, const std::vector &virtualFiles) const { + std::vector virtualFilePointers; + + for (const auto &file : virtualFiles) + virtualFilePointers.emplace_back(&file); + + if (ImGui::BeginTable("Virtual File Tree", 1, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_RowBg, size)) { + ImGui::TableSetupColumn("##path", ImGuiTableColumnFlags_WidthStretch); + + drawVirtualFileTree(virtualFilePointers); + + ImGui::EndTable(); + } + } + + void ViewPatternEditor::drawDebugger(ImVec2 size) { auto &runtime = ContentRegistry::PatternLanguage::getRuntime(); auto &evaluator = runtime.getInternals().evaluator; @@ -1179,6 +1243,10 @@ namespace hex::plugin::builtin { m_sourceCode = ""; } }); + + RequestAddVirtualFile::subscribe(this, [this](const std::fs::path &path, const std::vector &data, Region region) { + m_virtualFiles->emplace_back(path, data, region); + }); } static void createNestedMenu(const std::vector &menus, const std::function &function) { diff --git a/plugins/ui/include/ui/hex_editor.hpp b/plugins/ui/include/ui/hex_editor.hpp index 303c24cc9..ce9279033 100644 --- a/plugins/ui/include/ui/hex_editor.hpp +++ b/plugins/ui/include/ui/hex_editor.hpp @@ -76,11 +76,21 @@ namespace hex::ui { ~HexEditor(); void draw(float height = ImGui::GetContentRegionAvail().y); + HexEditor(const HexEditor&) = default; + HexEditor& operator=(const HexEditor&) = default; + + HexEditor(HexEditor &&editor) noexcept = default; + HexEditor& operator=(HexEditor &&) noexcept = default; + void setProvider(prv::Provider *provider) { m_provider = provider; m_currValidRegion = { Region::Invalid(), false }; m_scrollPosition.setProvider(provider); } + prv::Provider* getProvider() const { + return m_provider; + } + void setUnknownDataCharacter(char character) { m_unknownDataCharacter = character; } private: enum class CellType { None, Hex, ASCII }; @@ -285,7 +295,7 @@ namespace hex::ui { } private: - prv::Provider *m_provider; + prv::Provider *m_provider = nullptr; std::optional m_selectionStart; std::optional m_selectionEnd;