1
0
mirror of synced 2025-01-31 12:03:46 +01:00

feat: Added virtual files to the pattern language

This commit is contained in:
WerWolv 2024-01-11 20:11:22 +01:00
parent 5f02320e8e
commit 72f4331703
13 changed files with 171 additions and 27 deletions

View File

@ -285,6 +285,7 @@ namespace hex {
EVENT_DEF(RequestOpenFile, std::fs::path); EVENT_DEF(RequestOpenFile, std::fs::path);
EVENT_DEF(RequestChangeTheme, std::string); EVENT_DEF(RequestChangeTheme, std::string);
EVENT_DEF(RequestOpenPopup, std::string); EVENT_DEF(RequestOpenPopup, std::string);
EVENT_DEF(RequestAddVirtualFile, std::fs::path, std::vector<u8>, Region);
/** /**
* @brief Creates a provider from it's unlocalized name, and add it to the provider list * @brief Creates a provider from it's unlocalized name, and add it to the provider list

View File

@ -202,6 +202,14 @@ namespace hex {
*/ */
void setSelection(u64 address, size_t size, prv::Provider *provider = nullptr); 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<u8> data, Region region = Region::Invalid());
} }
/* Functions to interact with Bookmarks */ /* Functions to interact with Bookmarks */

View File

@ -11,9 +11,15 @@ namespace hex::prv {
class MemoryProvider : public hex::prv::Provider { class MemoryProvider : public hex::prv::Provider {
public: public:
MemoryProvider() = default; MemoryProvider() = default;
explicit MemoryProvider(std::vector<u8> data) : m_data(std::move(data)) { } explicit MemoryProvider(std::vector<u8> data, std::string name = "") : m_data(std::move(data)), m_name(std::move(name)) { }
~MemoryProvider() override = default; ~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 isAvailable() const override { return true; }
[[nodiscard]] bool isReadable() const override { return true; } [[nodiscard]] bool isReadable() const override { return true; }
[[nodiscard]] bool isWritable() 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 insertRaw(u64 offset, u64 size) override;
void removeRaw(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"; } [[nodiscard]] std::string getTypeName() const override { return "MemoryProvider"; }
private: private:

View File

@ -37,6 +37,11 @@ namespace hex::prv {
Provider(); Provider();
virtual ~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 * @brief Opens this provider

View File

@ -4,6 +4,7 @@
#include <hex/api/event_manager.hpp> #include <hex/api/event_manager.hpp>
#include <map> #include <map>
#include <ranges>
#include <utility> #include <utility>
namespace hex { namespace hex {
@ -71,10 +72,21 @@ namespace hex {
return this->get(); return this->get();
} }
auto all() {
return m_data | std::views::values;
}
void setOnCreateCallback(std::function<void(prv::Provider *, T&)> callback) {
m_onCreateCallback = std::move(callback);
}
private: private:
void onCreate() { void onCreate() {
EventProviderOpened::subscribe(this, [this](prv::Provider *provider) { 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){ EventProviderDeleted::subscribe(this, [this](prv::Provider *provider){
@ -111,6 +123,7 @@ namespace hex {
private: private:
std::map<prv::Provider *, T> m_data; std::map<prv::Provider *, T> m_data;
std::function<void(prv::Provider *, T&)> m_onCreateCallback;
}; };
} }

View File

@ -197,6 +197,9 @@ namespace hex {
setSelection({ { address, size }, provider == nullptr ? Provider::get() : provider }); setSelection({ { address, size }, provider == nullptr ? Provider::get() : provider });
} }
void addVirtualFile(const std::fs::path &path, std::vector<u8> data, Region region) {
RequestAddVirtualFile::post(path, std::move(data), region);
}
} }

View File

@ -14,7 +14,10 @@ namespace hex::plugin::builtin {
void drawContent() override; void drawContent() override;
private: private:
std::unique_ptr<ui::PatternDrawer> m_patternDrawer; bool m_rowColoring = false;
ui::PatternDrawer::TreeStyle m_treeStyle = ui::PatternDrawer::TreeStyle::Default;
PerProvider<std::unique_ptr<ui::PatternDrawer>> m_patternDrawer;
}; };
} }

View File

@ -35,13 +35,20 @@ namespace hex::plugin::builtin {
return ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse; return ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse;
} }
private: public:
struct VirtualFile {
std::fs::path path;
std::vector<u8> data;
Region region;
};
enum class DangerousFunctionPerms : u8 { enum class DangerousFunctionPerms : u8 {
Ask, Ask,
Allow, Allow,
Deny Deny
}; };
private:
class PopupAcceptPattern : public Popup<PopupAcceptPattern> { class PopupAcceptPattern : public Popup<PopupAcceptPattern> {
public: public:
explicit PopupAcceptPattern(ViewPatternEditor *view) : Popup("hex.builtin.view.pattern_editor.accept_pattern"), m_view(view) {} 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; bool m_triggerEvaluation = false;
std::atomic<bool> m_triggerAutoEvaluate = false; std::atomic<bool> m_triggerAutoEvaluate = false;
bool m_lastEvaluationProcessed = true; volatile bool m_lastEvaluationProcessed = true;
bool m_lastEvaluationResult = false; bool m_lastEvaluationResult = false;
std::atomic<u32> m_runningEvaluators = 0; std::atomic<u32> m_runningEvaluators = 0;
@ -177,6 +184,8 @@ namespace hex::plugin::builtin {
PerProvider<std::map<std::string, PatternVariable>> m_patternVariables; PerProvider<std::map<std::string, PatternVariable>> m_patternVariables;
PerProvider<std::map<u64, pl::api::Section>> m_sections; PerProvider<std::map<u64, pl::api::Section>> m_sections;
PerProvider<std::vector<VirtualFile>> m_virtualFiles;
PerProvider<std::list<EnvVar>> m_envVarEntries; PerProvider<std::list<EnvVar>> m_envVarEntries;
PerProvider<bool> m_shouldAnalyze; PerProvider<bool> m_shouldAnalyze;
@ -185,7 +194,7 @@ namespace hex::plugin::builtin {
std::atomic<bool> m_resetDebuggerVariables; std::atomic<bool> m_resetDebuggerVariables;
int m_debuggerScopeIndex = 0; int m_debuggerScopeIndex = 0;
std::array<AccessData, 512> m_accessHistory; std::array<AccessData, 512> m_accessHistory = {};
u32 m_accessHistoryIndex = 0; u32 m_accessHistoryIndex = 0;
private: private:
@ -193,6 +202,7 @@ namespace hex::plugin::builtin {
void drawEnvVars(ImVec2 size, std::list<EnvVar> &envVars); void drawEnvVars(ImVec2 size, std::list<EnvVar> &envVars);
void drawVariableSettings(ImVec2 size, std::map<std::string, PatternVariable> &patternVariables); void drawVariableSettings(ImVec2 size, std::map<std::string, PatternVariable> &patternVariables);
void drawSectionSelector(ImVec2 size, const std::map<u64, pl::api::Section> &sections); void drawSectionSelector(ImVec2 size, const std::map<u64, pl::api::Section> &sections);
void drawVirtualFiles(ImVec2 size, const std::vector<VirtualFile> &virtualFiles) const;
void drawDebugger(ImVec2 size); void drawDebugger(ImVec2 size);
void drawPatternTooltip(pl::ptrn::Pattern *pattern); void drawPatternTooltip(pl::ptrn::Pattern *pattern);

View File

@ -876,6 +876,7 @@
"hex.builtin.view.pattern_editor.shortcut.step_debugger": "Step Debugger", "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.continue_debugger": "Continue Debugger",
"hex.builtin.view.pattern_editor.shortcut.add_breakpoint": "Add Breakpoint", "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": "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_error_details": "An error occurred while trying to open this provider!\nDetails: {}",
"hex.builtin.view.provider_settings.load_popup": "Open Provider", "hex.builtin.view.provider_settings.load_popup": "Open Provider",

View File

@ -8,6 +8,7 @@
#include <pl/core/evaluator.hpp> #include <pl/core/evaluator.hpp>
#include <llvm/Demangle/Demangle.h> #include <llvm/Demangle/Demangle.h>
#include <pl/patterns/pattern.hpp>
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
@ -27,6 +28,20 @@ namespace hex::plugin::builtin {
return u128(u128(selection->getStartAddress()) << 64 | u128(selection->getSize())); 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<Token::Literal> {
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;
});
} }
{ {

View File

@ -2,43 +2,44 @@
#include <hex/api/content_registry.hpp> #include <hex/api/content_registry.hpp>
#include <hex/providers/memory_provider.hpp>
#include <pl/patterns/pattern.hpp> #include <pl/patterns/pattern.hpp>
#include <wolv/utils/lock.hpp> #include <wolv/utils/lock.hpp>
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
ViewPatternData::ViewPatternData() : View::Window("hex.builtin.view.pattern_data.name", ICON_VS_DATABASE) { ViewPatternData::ViewPatternData() : View::Window("hex.builtin.view.pattern_data.name", ICON_VS_DATABASE) {
m_patternDrawer = std::make_unique<ui::PatternDrawer>();
// Handle tree style setting changes // Handle tree style setting changes
EventSettingsChanged::subscribe(this, [this] { EventSettingsChanged::subscribe(this, [this] {
auto patternStyle = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_tree_style", 0); m_treeStyle = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_tree_style", 0);
m_patternDrawer->setTreeStyle(patternStyle); 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_rowColoring = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_data_row_bg", false);
m_patternDrawer->enableRowColoring(rowColoring); for (auto &drawer : m_patternDrawer.all())
}); drawer->enableRowColoring(m_rowColoring);
// Reset the pattern drawer when the provider changes
EventProviderChanged::subscribe(this, [this](auto, auto) {
m_patternDrawer->reset();
}); });
EventPatternEvaluating::subscribe(this, [this]{ EventPatternEvaluating::subscribe(this, [this]{
m_patternDrawer->reset(); (*m_patternDrawer)->reset();
}); });
EventPatternExecuted::subscribe(this, [this](auto){ EventPatternExecuted::subscribe(this, [this](auto){
m_patternDrawer->reset(); (*m_patternDrawer)->reset();
}); });
// Handle jumping to a pattern's location when it is clicked m_patternDrawer.setOnCreateCallback([this](prv::Provider *, auto &drawer) {
m_patternDrawer->setSelectionCallback([](Region region){ ImHexApi::HexEditor::setSelection(region); }); drawer = std::make_unique<ui::PatternDrawer>();
drawer->setSelectionCallback([](Region region){ ImHexApi::HexEditor::setSelection(region); });
drawer->setTreeStyle(m_treeStyle);
drawer->enableRowColoring(m_rowColoring);
});
} }
ViewPatternData::~ViewPatternData() { ViewPatternData::~ViewPatternData() {
EventSettingsChanged::unsubscribe(this); EventSettingsChanged::unsubscribe(this);
EventProviderChanged::unsubscribe(this);
EventPatternEvaluating::unsubscribe(this); EventPatternEvaluating::unsubscribe(this);
EventPatternExecuted::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); const auto height = std::max(ImGui::GetContentRegionAvail().y - ImGui::GetTextLineHeightWithSpacing() - ImGui::GetStyle().FramePadding.y * 2, ImGui::GetTextLineHeightWithSpacing() * 5);
if (!runtime.arePatternsValid()) { if (!runtime.arePatternsValid()) {
m_patternDrawer->draw({ }, nullptr, height); (*m_patternDrawer)->draw({ }, nullptr, height);
} else { } else {
// If the runtime has finished evaluating, draw the patterns // If the runtime has finished evaluating, draw the patterns
if (TRY_LOCK(ContentRegistry::PatternLanguage::getRuntimeLock())) { if (TRY_LOCK(ContentRegistry::PatternLanguage::getRuntimeLock())) {
m_patternDrawer->draw(runtime.getPatterns(), &runtime, height); (*m_patternDrawer)->draw(runtime.getPatterns(), &runtime, height);
} }
} }
} }

View File

@ -129,6 +129,49 @@ namespace hex::plugin::builtin {
return langDef; return langDef;
} }
static void drawVirtualFileTree(const std::vector<const ViewPatternEditor::VirtualFile*> &virtualFiles, u32 level = 0) {
std::map<std::string, std::vector<const ViewPatternEditor::VirtualFile*>> 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<prv::MemoryProvider>(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) { ViewPatternEditor::ViewPatternEditor() : View::Window("hex.builtin.view.pattern_editor.name", ICON_VS_SYMBOL_NAMESPACE) {
m_parserRuntime = std::make_unique<pl::PatternLanguage>(); m_parserRuntime = std::make_unique<pl::PatternLanguage>();
ContentRegistry::PatternLanguage::configureRuntime(*m_parserRuntime, nullptr); ContentRegistry::PatternLanguage::configureRuntime(*m_parserRuntime, nullptr);
@ -153,6 +196,7 @@ namespace hex::plugin::builtin {
EventFileLoaded::unsubscribe(this); EventFileLoaded::unsubscribe(this);
EventProviderChanged::unsubscribe(this); EventProviderChanged::unsubscribe(this);
EventProviderClosed::unsubscribe(this); EventProviderClosed::unsubscribe(this);
RequestAddVirtualFile::unsubscribe(this);
} }
void ViewPatternEditor::drawContent() { void ViewPatternEditor::drawContent() {
@ -237,6 +281,10 @@ namespace hex::plugin::builtin {
this->drawSectionSelector(settingsSize, *m_sections); this->drawSectionSelector(settingsSize, *m_sections);
ImGui::EndTabItem(); 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)) { if (ImGui::BeginTabItem("hex.builtin.view.pattern_editor.debugger"_lang)) {
this->drawDebugger(settingsSize); this->drawDebugger(settingsSize);
ImGui::EndTabItem(); ImGui::EndTabItem();
@ -296,7 +344,7 @@ namespace hex::plugin::builtin {
const auto dataSize = runtime.getInternals().evaluator->getDataSize(); const auto dataSize = runtime.getInternals().evaluator->getDataSize();
const auto insertPos = [&, this](u64 address, u32 color) { 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_accessHistory[m_accessHistoryIndex] = { progress, color };
m_accessHistoryIndex = (m_accessHistoryIndex + 1) % m_accessHistory.size(); m_accessHistoryIndex = (m_accessHistoryIndex + 1) % m_accessHistory.size();
@ -659,6 +707,22 @@ namespace hex::plugin::builtin {
} }
} }
void ViewPatternEditor::drawVirtualFiles(ImVec2 size, const std::vector<VirtualFile> &virtualFiles) const {
std::vector<const VirtualFile*> 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) { void ViewPatternEditor::drawDebugger(ImVec2 size) {
auto &runtime = ContentRegistry::PatternLanguage::getRuntime(); auto &runtime = ContentRegistry::PatternLanguage::getRuntime();
auto &evaluator = runtime.getInternals().evaluator; auto &evaluator = runtime.getInternals().evaluator;
@ -1179,6 +1243,10 @@ namespace hex::plugin::builtin {
m_sourceCode = ""; m_sourceCode = "";
} }
}); });
RequestAddVirtualFile::subscribe(this, [this](const std::fs::path &path, const std::vector<u8> &data, Region region) {
m_virtualFiles->emplace_back(path, data, region);
});
} }
static void createNestedMenu(const std::vector<std::string> &menus, const std::function<void()> &function) { static void createNestedMenu(const std::vector<std::string> &menus, const std::function<void()> &function) {

View File

@ -76,11 +76,21 @@ namespace hex::ui {
~HexEditor(); ~HexEditor();
void draw(float height = ImGui::GetContentRegionAvail().y); 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) { void setProvider(prv::Provider *provider) {
m_provider = provider; m_provider = provider;
m_currValidRegion = { Region::Invalid(), false }; m_currValidRegion = { Region::Invalid(), false };
m_scrollPosition.setProvider(provider); m_scrollPosition.setProvider(provider);
} }
prv::Provider* getProvider() const {
return m_provider;
}
void setUnknownDataCharacter(char character) { m_unknownDataCharacter = character; } void setUnknownDataCharacter(char character) { m_unknownDataCharacter = character; }
private: private:
enum class CellType { None, Hex, ASCII }; enum class CellType { None, Hex, ASCII };
@ -285,7 +295,7 @@ namespace hex::ui {
} }
private: private:
prv::Provider *m_provider; prv::Provider *m_provider = nullptr;
std::optional<u64> m_selectionStart; std::optional<u64> m_selectionStart;
std::optional<u64> m_selectionEnd; std::optional<u64> m_selectionEnd;