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

@@ -8,6 +8,7 @@
#include <pl/core/evaluator.hpp>
#include <llvm/Demangle/Demangle.h>
#include <pl/patterns/pattern.hpp>
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<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/providers/memory_provider.hpp>
#include <pl/patterns/pattern.hpp>
#include <wolv/utils/lock.hpp>
namespace hex::plugin::builtin {
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
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<ui::PatternDrawer>();
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);
}
}
}

View File

@@ -129,6 +129,49 @@ namespace hex::plugin::builtin {
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) {
m_parserRuntime = std::make_unique<pl::PatternLanguage>();
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<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) {
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<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) {