2020-11-10 14:26:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-02-01 17:09:40 +00:00
|
|
|
#include <hex/ui/view.hpp>
|
2023-04-07 22:58:53 +00:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
#include <hex/ui/popup.hpp>
|
|
|
|
|
2022-04-17 14:57:30 +00:00
|
|
|
#include <pl/pattern_language.hpp>
|
2022-08-06 19:38:09 +00:00
|
|
|
#include <pl/core/errors/error.hpp>
|
2022-11-06 23:04:47 +00:00
|
|
|
|
2022-10-26 06:54:43 +00:00
|
|
|
#include <content/helpers/provider_extra_data.hpp>
|
2023-04-07 22:58:53 +00:00
|
|
|
#include <content/providers/memory_file_provider.hpp>
|
|
|
|
|
2022-11-08 20:43:22 +00:00
|
|
|
#include <ui/hex_editor.hpp>
|
|
|
|
#include <ui/pattern_drawer.hpp>
|
2023-04-07 22:58:53 +00:00
|
|
|
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
2020-11-21 13:39:16 +00:00
|
|
|
#include <filesystem>
|
2021-02-07 12:40:47 +00:00
|
|
|
#include <string_view>
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 19:27:11 +00:00
|
|
|
#include <thread>
|
2021-01-23 13:01:23 +00:00
|
|
|
#include <vector>
|
2022-11-08 20:43:22 +00:00
|
|
|
#include <functional>
|
2020-11-10 14:26:38 +00:00
|
|
|
|
2021-01-13 16:28:27 +00:00
|
|
|
#include <TextEditor.h>
|
2020-11-12 20:20:51 +00:00
|
|
|
|
2022-08-06 19:38:09 +00:00
|
|
|
namespace pl::ptrn { class Pattern; }
|
2022-05-27 18:42:07 +00:00
|
|
|
|
2021-12-07 21:47:41 +00:00
|
|
|
namespace hex::plugin::builtin {
|
2020-11-10 14:26:38 +00:00
|
|
|
|
2021-08-28 19:51:33 +00:00
|
|
|
class ViewPatternEditor : public View {
|
2020-11-10 14:26:38 +00:00
|
|
|
public:
|
2021-08-28 19:51:33 +00:00
|
|
|
ViewPatternEditor();
|
|
|
|
~ViewPatternEditor() override;
|
2020-11-10 14:26:38 +00:00
|
|
|
|
2021-02-07 13:29:13 +00:00
|
|
|
void drawAlwaysVisible() override;
|
2020-12-22 17:10:01 +00:00
|
|
|
void drawContent() override;
|
2020-11-10 14:26:38 +00:00
|
|
|
|
|
|
|
private:
|
2022-04-17 14:57:30 +00:00
|
|
|
enum class DangerousFunctionPerms : u8 {
|
|
|
|
Ask,
|
|
|
|
Allow,
|
|
|
|
Deny
|
|
|
|
};
|
2023-04-07 22:58:53 +00:00
|
|
|
|
|
|
|
class PopupAcceptPattern : public Popup<PopupAcceptPattern> {
|
|
|
|
public:
|
2023-04-16 20:12:35 +00:00
|
|
|
explicit PopupAcceptPattern(ViewPatternEditor *view) : Popup("hex.builtin.view.pattern_editor.accept_pattern"), m_view(view) {}
|
2023-04-07 22:58:53 +00:00
|
|
|
|
|
|
|
void drawContent() override {
|
2023-04-16 20:12:35 +00:00
|
|
|
auto* provider = ImHexApi::Provider::get();
|
2023-04-09 23:46:55 +00:00
|
|
|
|
2023-04-07 22:58:53 +00:00
|
|
|
ImGui::TextFormattedWrapped("{}", static_cast<const char *>("hex.builtin.view.pattern_editor.accept_pattern.desc"_lang));
|
|
|
|
|
|
|
|
std::vector<std::string> entries;
|
|
|
|
entries.resize(this->m_view->m_possiblePatternFiles.size());
|
|
|
|
|
|
|
|
for (u32 i = 0; i < entries.size(); i++) {
|
|
|
|
entries[i] = wolv::util::toUTF8String(this->m_view->m_possiblePatternFiles[i].filename());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) {
|
|
|
|
u32 index = 0;
|
|
|
|
for (auto &path : this->m_view->m_possiblePatternFiles) {
|
2023-04-16 20:12:35 +00:00
|
|
|
if (ImGui::Selectable(wolv::util::toUTF8String(path.filename()).c_str(), index == this->m_selectedPatternFile, ImGuiSelectableFlags_DontClosePopups))
|
|
|
|
this->m_selectedPatternFile = index;
|
2023-04-09 23:46:55 +00:00
|
|
|
|
|
|
|
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0))
|
2023-04-16 20:12:35 +00:00
|
|
|
this->m_view->loadPatternFile(this->m_view->m_possiblePatternFiles[this->m_selectedPatternFile], provider);
|
2023-04-09 23:46:55 +00:00
|
|
|
|
|
|
|
ImGui::InfoTooltip(wolv::util::toUTF8String(path).c_str());
|
|
|
|
|
2023-04-07 22:58:53 +00:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndListBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.builtin.view.pattern_editor.accept_pattern.question"_lang);
|
|
|
|
|
2023-04-16 19:34:29 +00:00
|
|
|
confirmButtons("hex.builtin.common.yes"_lang, "hex.builtin.common.no"_lang,
|
|
|
|
[this, provider] {
|
2023-04-16 20:12:35 +00:00
|
|
|
this->m_view->loadPatternFile(this->m_view->m_possiblePatternFiles[this->m_selectedPatternFile], provider);
|
2023-04-16 19:34:29 +00:00
|
|
|
this->close();
|
2023-04-07 22:58:53 +00:00
|
|
|
},
|
2023-04-16 19:34:29 +00:00
|
|
|
[this] {
|
|
|
|
this->close();
|
2023-04-07 22:58:53 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape)))
|
2023-04-16 19:34:29 +00:00
|
|
|
this->close();
|
2023-04-07 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-08 21:34:46 +00:00
|
|
|
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
|
|
|
|
return ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:58:53 +00:00
|
|
|
private:
|
|
|
|
ViewPatternEditor *m_view;
|
2023-04-16 20:12:35 +00:00
|
|
|
u32 m_selectedPatternFile = 0;
|
2023-04-07 22:58:53 +00:00
|
|
|
};
|
2022-04-17 14:57:30 +00:00
|
|
|
|
2022-07-31 18:07:15 +00:00
|
|
|
private:
|
2022-10-26 06:54:43 +00:00
|
|
|
using PlData = ProviderExtraData::Data::PatternLanguage;
|
|
|
|
|
2022-07-31 18:07:15 +00:00
|
|
|
std::unique_ptr<pl::PatternLanguage> m_parserRuntime;
|
|
|
|
|
|
|
|
std::vector<std::fs::path> m_possiblePatternFiles;
|
|
|
|
bool m_runAutomatically = false;
|
2023-04-06 21:01:45 +00:00
|
|
|
bool m_triggerEvaluation = false;
|
2022-07-31 18:07:15 +00:00
|
|
|
|
|
|
|
bool m_lastEvaluationProcessed = true;
|
|
|
|
bool m_lastEvaluationResult = false;
|
|
|
|
|
|
|
|
std::atomic<u32> m_runningEvaluators = 0;
|
|
|
|
std::atomic<u32> m_runningParsers = 0;
|
|
|
|
|
|
|
|
bool m_hasUnevaluatedChanges = false;
|
|
|
|
|
|
|
|
TextEditor m_textEditor;
|
|
|
|
|
2022-04-17 14:57:30 +00:00
|
|
|
std::atomic<bool> m_dangerousFunctionCalled = false;
|
|
|
|
std::atomic<DangerousFunctionPerms> m_dangerousFunctionsAllowed = DangerousFunctionPerms::Ask;
|
|
|
|
|
2022-07-31 18:07:15 +00:00
|
|
|
bool m_syncPatternSourceCode = false;
|
|
|
|
bool m_autoLoadPatterns = true;
|
|
|
|
|
2022-11-08 20:43:22 +00:00
|
|
|
std::map<prv::Provider*, std::move_only_function<void()>> m_sectionWindowDrawer;
|
2023-02-17 13:53:15 +00:00
|
|
|
|
|
|
|
ui::HexEditor m_sectionHexEditor;
|
|
|
|
|
2022-07-31 18:07:15 +00:00
|
|
|
private:
|
2022-10-26 06:54:43 +00:00
|
|
|
void drawConsole(ImVec2 size, const std::vector<std::pair<pl::core::LogConsole::Level, std::string>> &console);
|
|
|
|
void drawEnvVars(ImVec2 size, std::list<PlData::EnvVar> &envVars);
|
|
|
|
void drawVariableSettings(ImVec2 size, std::map<std::string, PlData::PatternVariable> &patternVariables);
|
2022-11-06 23:04:47 +00:00
|
|
|
void drawSectionSelector(ImVec2 size, std::map<u64, pl::api::Section> §ions);
|
2021-12-10 10:55:27 +00:00
|
|
|
|
2022-08-06 19:38:09 +00:00
|
|
|
void drawPatternTooltip(pl::ptrn::Pattern *pattern);
|
2022-05-27 18:42:07 +00:00
|
|
|
|
2022-10-26 06:54:43 +00:00
|
|
|
void loadPatternFile(const std::fs::path &path, prv::Provider *provider);
|
2021-12-18 21:56:36 +00:00
|
|
|
|
2022-10-26 06:54:43 +00:00
|
|
|
void parsePattern(const std::string &code, prv::Provider *provider);
|
|
|
|
void evaluatePattern(const std::string &code, prv::Provider *provider);
|
2022-10-21 08:03:37 +00:00
|
|
|
|
|
|
|
void registerEvents();
|
|
|
|
void registerMenuItems();
|
|
|
|
void registerHandlers();
|
2023-04-05 16:29:30 +00:00
|
|
|
|
|
|
|
void appendEditorText(const std::string &text);
|
|
|
|
void appendVariable(const std::string &type);
|
|
|
|
void appendArray(const std::string &type, size_t size);
|
2020-11-10 14:26:38 +00:00
|
|
|
};
|
|
|
|
|
2022-07-14 09:37:02 +00:00
|
|
|
}
|