From 6a07a2f85d6b34ee6479826f6ce09ae6b6a01b9b Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 2 Oct 2022 14:18:40 +0200 Subject: [PATCH] feat: Look for custom inspector entries in imhex/scripts/inspectors --- lib/external/pattern_language | 2 +- lib/libimhex/include/hex/helpers/fs.hpp | 12 ++- lib/libimhex/source/helpers/fs.cpp | 8 ++ main/source/init/tasks.cpp | 18 +--- .../content/views/view_data_inspector.cpp | 84 ++++++++++--------- 5 files changed, 64 insertions(+), 60 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 772269b23..303204a37 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 772269b235dd460318d81e8597d6828119d284dc +Subproject commit 303204a37e07fc2556d476b06acbbdb1e648840e diff --git a/lib/libimhex/include/hex/helpers/fs.hpp b/lib/libimhex/include/hex/helpers/fs.hpp index e61c7e918..862835c3a 100644 --- a/lib/libimhex/include/hex/helpers/fs.hpp +++ b/lib/libimhex/include/hex/helpers/fs.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -84,8 +86,8 @@ namespace hex::fs { void setFileBrowserErrorCallback(const std::function &callback); bool openFileBrowser(DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath = {}); - enum class ImHexPath { - Patterns, + enum class ImHexPath : u32 { + Patterns = 0, PatternsInclude, Magic, Python, @@ -96,7 +98,11 @@ namespace hex::fs { Constants, Encodings, Logs, - Recent + Recent, + Scripts, + Inspectors, + + END }; std::optional getExecutablePath(); diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index 3fcd4e02b..c695ce2e2 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -209,6 +209,8 @@ namespace hex::fs { std::vector result; switch (path) { + case ImHexPath::END: + return { }; case ImHexPath::Constants: result = appendPath(getDataPaths(), "constants"); break; @@ -245,6 +247,12 @@ namespace hex::fs { case ImHexPath::Recent: result = appendPath(getConfigPaths(), "recent"); break; + case ImHexPath::Scripts: + result = appendPath(getDataPaths(), "scripts"); + break; + case ImHexPath::Inspectors: + result = appendPath(getDefaultPaths(ImHexPath::Scripts), "inspectors"); + break; } if (!listNonExisting) { diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index f019295ad..6611b919d 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -61,20 +61,6 @@ namespace hex::init { bool result = true; using enum fs::ImHexPath; - constexpr std::array paths = { - Patterns, - PatternsInclude, - Magic, - Plugins, - Resources, - Config, - Constants, - Yara, - Encodings, - Python, - Logs, - Recent - }; // Check if ImHex is installed in portable mode { @@ -87,8 +73,8 @@ namespace hex::init { } // Create all folders - for (auto path : paths) { - for (auto &folder : fs::getDefaultPaths(path, true)) { + for (u32 path = 0; path < u32(fs::ImHexPath::END); path++) { + for (auto &folder : fs::getDefaultPaths(static_cast(path), true)) { try { fs::createDirectories(folder); } catch (...) { diff --git a/plugins/builtin/source/content/views/view_data_inspector.cpp b/plugins/builtin/source/content/views/view_data_inspector.cpp index 1bbd483c3..01fc11bd5 100644 --- a/plugins/builtin/source/content/views/view_data_inspector.cpp +++ b/plugins/builtin/source/content/views/view_data_inspector.cpp @@ -7,6 +7,8 @@ #include #include +#include + #include #include #include @@ -54,17 +56,20 @@ namespace hex::plugin::builtin { byte ^= 0xFF; } - InspectorCacheEntry cacheEntry = { - entry.unlocalizedName, - entry.generatorFunction(buffer, this->m_endian, this->m_numberDisplayStyle), - entry.editingFunction, - false - }; - - this->m_cachedData.push_back(cacheEntry); + this->m_cachedData.push_back({ + entry.unlocalizedName, + entry.generatorFunction(buffer, this->m_endian, this->m_numberDisplayStyle), + entry.editingFunction, + false + }); } + // Decode bytes using custom inspectors defined using the pattern language + const std::map inVariables = { + { "numberDisplayStyle", u128(this->m_numberDisplayStyle) } + }; + pl::PatternLanguage runtime; ContentRegistry::PatternLanguage::configureRuntime(runtime, nullptr); @@ -81,43 +86,42 @@ namespace hex::plugin::builtin { runtime.setDefaultEndian(this->m_endian); runtime.setStartAddress(this->m_startAddress); - std::map inVariables = { - { "numberDisplayStyle", u128(this->m_numberDisplayStyle) } - }; + for (const auto &folderPath : fs::getDefaultPaths(fs::ImHexPath::Inspectors)) { + for (const auto &filePath : std::fs::recursive_directory_iterator(folderPath)) { + if (!filePath.exists() || !filePath.is_regular_file() || filePath.path().extension() != ".hexpat") + continue; - bool ranSuccessfully = false; - for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) { - auto inspectorFilePath = path / "inspectors.hexpat"; + fs::File file(filePath, fs::File::Mode::Read); + if (file.isValid()) { + auto inspectorCode = file.readString(); - if (fs::exists(inspectorFilePath) && fs::isRegularFile(inspectorFilePath)) { - ranSuccessfully = runtime.executeFile(inspectorFilePath, {}, inVariables, true); - break; - } - } + if (!inspectorCode.empty()) { + if (runtime.executeString(inspectorCode, {}, inVariables, true)) { + const auto &patterns = runtime.getAllPatterns(); - if (ranSuccessfully) { - const auto &patterns = runtime.getAllPatterns(); + for (const auto &pattern : patterns) { + if (pattern->isHidden()) + continue; - for (const auto &pattern : patterns) { - InspectorCacheEntry cacheEntry = { - pattern->getDisplayName(), - [value = pattern->getFormattedValue()]() { - ImGui::TextUnformatted(value.c_str()); - return value; - }, - std::nullopt, - false - }; + this->m_cachedData.push_back({ + pattern->getDisplayName(), + [value = pattern->getFormattedValue()]() { + ImGui::TextUnformatted(value.c_str()); + return value; + }, + std::nullopt, + false + }); + } + } else { + const auto& error = runtime.getError(); - this->m_cachedData.push_back(cacheEntry); - } - } else { - const auto& error = runtime.getError(); - - if (error.has_value()) { - log::error("Failed to execute inspectors.hexpat:\n {}", error.value().what()); - } else { - log::error("Failed to execute inspectors.hexpat"); + log::error("Failed to execute inspectors.hexpat!"); + if (error.has_value()) + log::error("{}", error.value().what()); + } + } + } } } }