ImHex/plugins/builtin/include/content/views/view_pattern_editor.hpp

212 lines
7.1 KiB
C++
Raw Normal View History

#pragma once
#include <hex/ui/view.hpp>
#include <hex/providers/provider.hpp>
#include <hex/ui/popup.hpp>
#include <pl/pattern_language.hpp>
#include <pl/core/errors/error.hpp>
#include <content/providers/memory_file_provider.hpp>
#include <ui/hex_editor.hpp>
#include <ui/pattern_drawer.hpp>
#include <cstring>
#include <filesystem>
#include <string_view>
#include <functional>
#include <TextEditor.h>
namespace pl::ptrn { class Pattern; }
ui/ux: Rewrite of the entire hex editor view to make it more flexible (#512) * ui/ux: Initial recreation of the hex editor view * ui/ux: Added back support for editing cells * ux: Make scrolling and selecting bytes feel nice again * ui/ux: Improved byte selecting, added footer * sys: Make math evaluator more generic to support integer only calculations * patterns: Moved value formatting into pattern language * ui/ux: Added Goto and Search popups, improved selection * ui: Added better tooltips for bookmarks and patterns * sys: Use worse hex search algorithm on macOS Sadly it still doesn't support `std::boyer_moore_horsepool_searcher` * ui: Added back missing events, menu items and shortcuts * fix: Bookmark highlighting being rendered off by one * fix: Various macOS build errors * fix: size_t is not u64 on macos * fix: std::fmod and std::pow not working with integer types on macos * fix: Missing semicolons * sys: Added proper integer pow function * ui: Added back support for custom encodings * fix: Editor not jumping to selection when selection gets changed * ui: Turn Hexii setting into a data visualizer * sys: Added back remaining shortcuts * sys: Remove old hex editor files * sys: Moved more legacy things away from the hex editor view, updated localization * fix: Hex editor scrolling behaving weirdly and inconsistently * sys: Cleaned up Hex editor code * sys: Added selection color setting, localized all new settings * fix: Search feature not working correctly * ui: Replace custom ImGui::Disabled function with native ImGui ones * ui: Fix bookmark tooltip rendering issues * fix: Another size_t not being 64 bit issue on MacOS
2022-05-27 18:42:07 +00:00
2021-12-07 21:47:41 +00:00
namespace hex::plugin::builtin {
class ViewPatternEditor : public View {
public:
ViewPatternEditor();
~ViewPatternEditor() override;
void drawAlwaysVisible() override;
void drawContent() override;
private:
enum class DangerousFunctionPerms : u8 {
Ask,
Allow,
Deny
};
class PopupAcceptPattern : public Popup<PopupAcceptPattern> {
public:
explicit PopupAcceptPattern(ViewPatternEditor *view) : Popup("hex.builtin.view.pattern_editor.accept_pattern"), m_view(view) {}
void drawContent() override {
auto* provider = ImHexApi::Provider::get();
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.get(provider).size());
for (u32 i = 0; i < entries.size(); i++) {
entries[i] = wolv::util::toUTF8String(this->m_view->m_possiblePatternFiles.get(provider)[i].filename());
}
if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) {
u32 index = 0;
for (auto &path : this->m_view->m_possiblePatternFiles.get(provider)) {
if (ImGui::Selectable(wolv::util::toUTF8String(path.filename()).c_str(), index == this->m_selectedPatternFile, ImGuiSelectableFlags_DontClosePopups))
this->m_selectedPatternFile = index;
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0))
this->m_view->loadPatternFile(this->m_view->m_possiblePatternFiles.get(provider)[this->m_selectedPatternFile], provider);
ImGui::InfoTooltip(wolv::util::toUTF8String(path).c_str());
index++;
}
// Close the popup if there aren't any patterns available
if (index == 0)
this->close();
ImGui::EndListBox();
}
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.pattern_editor.accept_pattern.question"_lang);
confirmButtons("hex.builtin.common.yes"_lang, "hex.builtin.common.no"_lang,
[this, provider] {
this->m_view->loadPatternFile(this->m_view->m_possiblePatternFiles.get(provider)[this->m_selectedPatternFile], provider);
this->close();
},
[this] {
this->close();
}
);
if (ImGui::IsKeyPressed(ImGuiKey_Escape))
this->close();
}
2023-04-08 21:34:46 +00:00
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
}
private:
ViewPatternEditor *m_view;
u32 m_selectedPatternFile = 0;
};
private:
struct PatternVariable {
bool inVariable;
bool outVariable;
pl::core::Token::ValueType type;
pl::core::Token::Literal value;
};
enum class EnvVarType
{
Integer,
Float,
String,
Bool
};
struct EnvVar {
u64 id;
std::string name;
pl::core::Token::Literal value;
EnvVarType type;
bool operator==(const EnvVar &other) const {
return this->id == other.id;
}
};
struct AccessData {
float progress;
u32 color;
};
std::unique_ptr<pl::PatternLanguage> m_parserRuntime;
PerProvider<std::vector<std::fs::path>> m_possiblePatternFiles;
bool m_runAutomatically = false;
bool m_triggerEvaluation = false;
std::atomic<bool> m_triggerAutoEvaluate = false;
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, m_consoleEditor;
std::atomic<bool> m_consoleNeedsUpdate = false;
std::atomic<bool> m_dangerousFunctionCalled = false;
std::atomic<DangerousFunctionPerms> m_dangerousFunctionsAllowed = DangerousFunctionPerms::Ask;
bool m_syncPatternSourceCode = false;
bool m_autoLoadPatterns = true;
std::map<prv::Provider*, std::function<void()>> m_sectionWindowDrawer;
ui::HexEditor m_sectionHexEditor;
PerProvider<std::string> m_sourceCode;
PerProvider<std::vector<std::string>> m_console;
PerProvider<bool> m_executionDone = true;
std::mutex m_logMutex;
PerProvider<std::optional<pl::core::err::PatternLanguageError>> m_lastEvaluationError;
PerProvider<std::map<std::string, pl::core::Token::Literal>> m_lastEvaluationOutVars;
PerProvider<std::map<std::string, PatternVariable>> m_patternVariables;
PerProvider<std::map<u64, pl::api::Section>> m_sections;
PerProvider<std::list<EnvVar>> m_envVarEntries;
PerProvider<bool> m_shouldAnalyze;
PerProvider<bool> m_breakpointHit;
PerProvider<std::unique_ptr<ui::PatternDrawer>> m_debuggerDrawer;
std::atomic<bool> m_resetDebuggerVariables;
int m_debuggerScopeIndex = 0;
std::array<AccessData, 512> m_accessHistory;
u32 m_accessHistoryIndex = 0;
private:
void drawConsole(ImVec2 size);
void drawEnvVars(ImVec2 size, std::list<EnvVar> &envVars);
void drawVariableSettings(ImVec2 size, std::map<std::string, PatternVariable> &patternVariables);
2023-11-10 19:47:08 +00:00
void drawSectionSelector(ImVec2 size, const std::map<u64, pl::api::Section> &sections);
void drawDebugger(ImVec2 size);
void drawPatternTooltip(pl::ptrn::Pattern *pattern);
ui/ux: Rewrite of the entire hex editor view to make it more flexible (#512) * ui/ux: Initial recreation of the hex editor view * ui/ux: Added back support for editing cells * ux: Make scrolling and selecting bytes feel nice again * ui/ux: Improved byte selecting, added footer * sys: Make math evaluator more generic to support integer only calculations * patterns: Moved value formatting into pattern language * ui/ux: Added Goto and Search popups, improved selection * ui: Added better tooltips for bookmarks and patterns * sys: Use worse hex search algorithm on macOS Sadly it still doesn't support `std::boyer_moore_horsepool_searcher` * ui: Added back missing events, menu items and shortcuts * fix: Bookmark highlighting being rendered off by one * fix: Various macOS build errors * fix: size_t is not u64 on macos * fix: std::fmod and std::pow not working with integer types on macos * fix: Missing semicolons * sys: Added proper integer pow function * ui: Added back support for custom encodings * fix: Editor not jumping to selection when selection gets changed * ui: Turn Hexii setting into a data visualizer * sys: Added back remaining shortcuts * sys: Remove old hex editor files * sys: Moved more legacy things away from the hex editor view, updated localization * fix: Hex editor scrolling behaving weirdly and inconsistently * sys: Cleaned up Hex editor code * sys: Added selection color setting, localized all new settings * fix: Search feature not working correctly * ui: Replace custom ImGui::Disabled function with native ImGui ones * ui: Fix bookmark tooltip rendering issues * fix: Another size_t not being 64 bit issue on MacOS
2022-05-27 18:42:07 +00:00
void loadPatternFile(const std::fs::path &path, prv::Provider *provider);
void parsePattern(const std::string &code, prv::Provider *provider);
void evaluatePattern(const std::string &code, prv::Provider *provider);
void registerEvents();
void registerMenuItems();
void registerHandlers();
void appendEditorText(const std::string &text);
void appendVariable(const std::string &type);
void appendArray(const std::string &type, size_t size);
};
}