mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 05:27:41 -05:00
patterns: Huge refactor of Pattern Language runtime to use smart pointers (#458)
* patterns: Initial work to refactor pattern language to use smart pointers * patterns: Fixed remaining issues, moved patterns to unique files * sys: Added missing includes for macOS
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <hex/helpers/socket.hpp>
|
||||
#include <hex/providers/provider.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <mutex>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace hex::plugin::builtin {
|
||||
u64 m_resizeSize = 0;
|
||||
|
||||
std::vector<u8> m_dataToSave;
|
||||
std::set<pl::PatternData *> m_highlightedPatterns;
|
||||
std::set<pl::Pattern *> m_highlightedPatterns;
|
||||
|
||||
std::string m_loaderScriptScriptPath;
|
||||
std::string m_loaderScriptFilePath;
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace hex::plugin::builtin {
|
||||
void drawContent() override;
|
||||
|
||||
private:
|
||||
std::map<prv::Provider *, std::vector<pl::PatternData *>> m_sortedPatterns;
|
||||
std::map<prv::Provider *, std::vector<std::shared_ptr<pl::Pattern>>> m_sortedPatterns;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -51,7 +51,8 @@ namespace hex::plugin::builtin {
|
||||
std::map<std::string, PatternVariable> m_patternVariables;
|
||||
std::vector<std::string> m_patternTypes;
|
||||
|
||||
enum class EnvVarType {
|
||||
enum class EnvVarType
|
||||
{
|
||||
Integer,
|
||||
Float,
|
||||
String,
|
||||
@@ -77,7 +78,7 @@ namespace hex::plugin::builtin {
|
||||
void drawVariableSettings(ImVec2 size);
|
||||
|
||||
void loadPatternFile(const fs::path &path);
|
||||
void clearPatternData();
|
||||
void clearPatterns();
|
||||
|
||||
void parsePattern(const std::string &code);
|
||||
void evaluatePattern(const std::string &code);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <hex/pattern_language/token.hpp>
|
||||
#include <hex/pattern_language/log_console.hpp>
|
||||
#include <hex/pattern_language/evaluator.hpp>
|
||||
#include <hex/pattern_language/pattern_data.hpp>
|
||||
#include <hex/pattern_language/patterns/pattern.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
std::string format(pl::Evaluator *ctx, auto params) {
|
||||
std::string format(pl::Evaluator *ctx, const auto ¶ms) {
|
||||
auto format = pl::Token::literalToString(params[0], true);
|
||||
std::string message;
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace hex::plugin::builtin {
|
||||
auto ¶m = params[i];
|
||||
|
||||
std::visit(overloaded {
|
||||
[&](pl::PatternData *value) {
|
||||
[&](const std::shared_ptr<pl::Pattern> &value) {
|
||||
formatArgs.push_back(value->toString(ctx->getProvider()));
|
||||
},
|
||||
[&](auto &&value) {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include "content/providers/disk_provider.hpp"
|
||||
|
||||
#include <hex/helpers/fmt.hpp>
|
||||
#include <hex/api/localization.hpp>
|
||||
|
||||
#include <hex/helpers/fmt.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
|
||||
#include <bitset>
|
||||
#include <filesystem>
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "content/views/view_pattern_data.hpp"
|
||||
|
||||
#include <hex/providers/provider.hpp>
|
||||
#include <hex/pattern_language/pattern_data.hpp>
|
||||
|
||||
#include <hex/pattern_language/pattern_language.hpp>
|
||||
#include <hex/pattern_language/patterns/pattern.hpp>
|
||||
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
@@ -18,8 +20,8 @@ namespace hex::plugin::builtin {
|
||||
EventManager::unsubscribe<EventHighlightingChanged>(this);
|
||||
}
|
||||
|
||||
static bool beginPatternDataTable(prv::Provider *&provider, const std::vector<pl::PatternData *> &patterns, std::vector<pl::PatternData *> &sortedPatterns) {
|
||||
if (ImGui::BeginTable("##patterndatatable", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
||||
static bool beginPatternTable(prv::Provider *&provider, const std::vector<std::shared_ptr<pl::Pattern>> &patterns, std::vector<std::shared_ptr<pl::Pattern>> &sortedPatterns) {
|
||||
if (ImGui::BeginTable("##Patterntable", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
||||
ImGui::TableSetupScrollFreeze(0, 1);
|
||||
ImGui::TableSetupColumn("hex.builtin.view.pattern_data.var_name"_lang, 0, 0, ImGui::GetID("name"));
|
||||
ImGui::TableSetupColumn("hex.builtin.view.pattern_data.color"_lang, 0, 0, ImGui::GetID("color"));
|
||||
@@ -33,8 +35,8 @@ namespace hex::plugin::builtin {
|
||||
if (sortSpecs->SpecsDirty || sortedPatterns.empty()) {
|
||||
sortedPatterns = patterns;
|
||||
|
||||
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs, &provider](pl::PatternData *left, pl::PatternData *right) -> bool {
|
||||
return pl::PatternData::sortPatternDataTable(sortSpecs, provider, left, right);
|
||||
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs, &provider](const std::shared_ptr<pl::Pattern> &left, const std::shared_ptr<pl::Pattern> &right) -> bool {
|
||||
return pl::Pattern::sortPatternTable(sortSpecs, provider, left.get(), right.get());
|
||||
});
|
||||
|
||||
for (auto &pattern : sortedPatterns)
|
||||
@@ -55,12 +57,12 @@ namespace hex::plugin::builtin {
|
||||
if (ImHexApi::Provider::isValid() && provider->isReadable()) {
|
||||
|
||||
auto &sortedPatterns = this->m_sortedPatterns[ImHexApi::Provider::get()];
|
||||
if (beginPatternDataTable(provider, provider->getPatternLanguageRuntime().getPatterns(), sortedPatterns)) {
|
||||
if (beginPatternTable(provider, provider->getPatternLanguageRuntime().getPatterns(), sortedPatterns)) {
|
||||
ImGui::TableHeadersRow();
|
||||
if (!sortedPatterns.empty()) {
|
||||
|
||||
for (auto &patternData : sortedPatterns)
|
||||
patternData->draw(provider);
|
||||
for (auto &patterns : sortedPatterns)
|
||||
patterns->draw(provider);
|
||||
}
|
||||
|
||||
ImGui::EndTable();
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
#include "content/views/view_pattern_editor.hpp"
|
||||
|
||||
#include <hex/helpers/project_file_handler.hpp>
|
||||
|
||||
#include <hex/pattern_language/preprocessor.hpp>
|
||||
#include <hex/pattern_language/pattern_data.hpp>
|
||||
#include <hex/pattern_language/ast_node.hpp>
|
||||
#include <hex/pattern_language/patterns/pattern.hpp>
|
||||
#include <hex/pattern_language/ast/ast_node.hpp>
|
||||
#include <hex/pattern_language/ast/ast_node_variable_decl.hpp>
|
||||
#include <hex/pattern_language/ast/ast_node_type_decl.hpp>
|
||||
#include <hex/pattern_language/ast/ast_node_builtin_type.hpp>
|
||||
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/file.hpp>
|
||||
|
||||
#include <hex/helpers/project_file_handler.hpp>
|
||||
#include <hex/helpers/magic.hpp>
|
||||
|
||||
namespace hex::plugin::builtin {
|
||||
@@ -225,7 +229,7 @@ namespace hex::plugin::builtin {
|
||||
|
||||
|
||||
ImHexApi::HexEditor::addHighlightingProvider([](u64 address) -> std::optional<ImHexApi::HexEditor::Highlighting> {
|
||||
auto patterns = ImHexApi::Provider::get()->getPatternLanguageRuntime().getPatterns();
|
||||
const auto &patterns = ImHexApi::Provider::get()->getPatternLanguageRuntime().getPatterns();
|
||||
for (const auto &pattern : patterns) {
|
||||
auto child = pattern->getPattern(address);
|
||||
if (child != nullptr) {
|
||||
@@ -586,7 +590,7 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
}
|
||||
|
||||
void ViewPatternEditor::clearPatternData() {
|
||||
void ViewPatternEditor::clearPatterns() {
|
||||
if (!ImHexApi::Provider::isValid()) return;
|
||||
|
||||
ImHexApi::Provider::get()->getPatternLanguageRuntime().reset();
|
||||
@@ -603,12 +607,12 @@ namespace hex::plugin::builtin {
|
||||
this->m_patternTypes.clear();
|
||||
|
||||
if (ast) {
|
||||
for (auto node : *ast) {
|
||||
if (auto variableDecl = dynamic_cast<pl::ASTNodeVariableDecl *>(node)) {
|
||||
auto type = dynamic_cast<pl::ASTNodeTypeDecl *>(variableDecl->getType());
|
||||
for (auto &node : *ast) {
|
||||
if (auto variableDecl = dynamic_cast<pl::ASTNodeVariableDecl *>(node.get())) {
|
||||
auto type = dynamic_cast<pl::ASTNodeTypeDecl *>(variableDecl->getType().get());
|
||||
if (type == nullptr) continue;
|
||||
|
||||
auto builtinType = dynamic_cast<pl::ASTNodeBuiltinType *>(type->getType());
|
||||
auto builtinType = dynamic_cast<pl::ASTNodeBuiltinType *>(type->getType().get());
|
||||
if (builtinType == nullptr) continue;
|
||||
|
||||
PatternVariable variable = {
|
||||
@@ -634,7 +638,7 @@ namespace hex::plugin::builtin {
|
||||
|
||||
this->m_textEditor.SetErrorMarkers({});
|
||||
this->m_console.clear();
|
||||
this->clearPatternData();
|
||||
this->clearPatterns();
|
||||
|
||||
EventManager::post<EventHighlightingChanged>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user