2021-08-28 21:51:33 +02:00
|
|
|
#include "views/view_pattern_editor.hpp"
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-30 00:03:12 +01:00
|
|
|
#include "helpers/project_file_handler.hpp"
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
#include <hex/lang/preprocessor.hpp>
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-21 14:39:16 +01:00
|
|
|
#include <magic.h>
|
|
|
|
|
2021-02-20 22:38:31 +01:00
|
|
|
#include <imgui_imhex_extensions.h>
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2020-11-10 21:31:04 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2020-11-20 18:24:59 +01:00
|
|
|
static const TextEditor::LanguageDefinition& PatternLanguage() {
|
|
|
|
static bool initialized = false;
|
|
|
|
static TextEditor::LanguageDefinition langDef;
|
|
|
|
if (!initialized) {
|
|
|
|
static const char* const keywords[] = {
|
2021-08-25 17:07:01 +02:00
|
|
|
"using", "struct", "union", "enum", "bitfield", "be", "le", "if", "else", "false", "true", "parent", "addressof", "sizeof", "$", "while", "fn", "return", "namespace"
|
2020-11-20 18:24:59 +01:00
|
|
|
};
|
|
|
|
for (auto& k : keywords)
|
|
|
|
langDef.mKeywords.insert(k);
|
|
|
|
|
2020-11-20 22:21:59 +01:00
|
|
|
static std::pair<const char* const, size_t> builtInTypes[] = {
|
|
|
|
{ "u8", 1 }, { "u16", 2 }, { "u32", 4 }, { "u64", 8 }, { "u128", 16 },
|
|
|
|
{ "s8", 1 }, { "s16", 2 }, { "s32", 4 }, { "s64", 8 }, { "s128", 16 },
|
2021-05-02 20:13:37 +02:00
|
|
|
{ "float", 4 }, { "double", 8 }, { "char", 1 }, { "char16", 2 }, { "bool", 1 }, { "padding", 1 }
|
2020-11-20 18:24:59 +01:00
|
|
|
};
|
2021-01-09 23:48:42 +01:00
|
|
|
|
2020-11-20 22:21:59 +01:00
|
|
|
for (const auto &[name, size] : builtInTypes) {
|
2020-11-20 18:24:59 +01:00
|
|
|
TextEditor::Identifier id;
|
2020-11-20 22:21:59 +01:00
|
|
|
id.mDeclaration = std::to_string(size);
|
|
|
|
id.mDeclaration += size == 1 ? " byte" : " bytes";
|
|
|
|
langDef.mIdentifiers.insert(std::make_pair(std::string(name), id));
|
2020-11-20 18:24:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
langDef.mTokenize = [](const char * inBegin, const char * inEnd, const char *& outBegin, const char *& outEnd, TextEditor::PaletteIndex & paletteIndex) -> bool {
|
|
|
|
paletteIndex = TextEditor::PaletteIndex::Max;
|
|
|
|
|
|
|
|
while (inBegin < inEnd && isascii(*inBegin) && isblank(*inBegin))
|
|
|
|
inBegin++;
|
|
|
|
|
|
|
|
if (inBegin == inEnd) {
|
|
|
|
outBegin = inEnd;
|
|
|
|
outEnd = inEnd;
|
|
|
|
paletteIndex = TextEditor::PaletteIndex::Default;
|
|
|
|
}
|
2021-01-22 18:01:39 +01:00
|
|
|
else if (TokenizeCStyleIdentifier(inBegin, inEnd, outBegin, outEnd)) {
|
2021-08-27 09:56:20 +02:00
|
|
|
paletteIndex = TextEditor::PaletteIndex::Identifier;
|
2021-01-22 18:01:39 +01:00
|
|
|
}
|
2020-11-20 18:24:59 +01:00
|
|
|
else if (TokenizeCStyleNumber(inBegin, inEnd, outBegin, outEnd))
|
|
|
|
paletteIndex = TextEditor::PaletteIndex::Number;
|
2020-11-27 14:18:28 +01:00
|
|
|
else if (TokenizeCStyleCharacterLiteral(inBegin, inEnd, outBegin, outEnd))
|
|
|
|
paletteIndex = TextEditor::PaletteIndex::CharLiteral;
|
2021-01-09 23:48:42 +01:00
|
|
|
else if (TokenizeCStyleString(inBegin, inEnd, outBegin, outEnd))
|
|
|
|
paletteIndex = TextEditor::PaletteIndex::String;
|
2020-11-20 18:24:59 +01:00
|
|
|
|
|
|
|
return paletteIndex != TextEditor::PaletteIndex::Max;
|
|
|
|
};
|
|
|
|
|
|
|
|
langDef.mCommentStart = "/*";
|
|
|
|
langDef.mCommentEnd = "*/";
|
|
|
|
langDef.mSingleLineComment = "//";
|
|
|
|
|
|
|
|
langDef.mCaseSensitive = true;
|
|
|
|
langDef.mAutoIndentation = true;
|
|
|
|
langDef.mPreprocChar = '#';
|
|
|
|
|
|
|
|
langDef.mName = "Pattern Language";
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
return langDef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-28 21:51:33 +02:00
|
|
|
ViewPatternEditor::ViewPatternEditor() : View("hex.view.pattern.name") {
|
2021-01-23 14:00:09 +01:00
|
|
|
this->m_patternLanguageRuntime = new lang::PatternLanguage();
|
2020-11-13 14:35:52 +01:00
|
|
|
|
2020-11-20 18:24:59 +01:00
|
|
|
this->m_textEditor.SetLanguageDefinition(PatternLanguage());
|
|
|
|
this->m_textEditor.SetShowWhitespaces(false);
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventProjectFileStore>(this, [this]() {
|
2020-11-30 00:03:12 +01:00
|
|
|
ProjectFile::setPattern(this->m_textEditor.GetText());
|
|
|
|
});
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventProjectFileLoad>(this, [this]() {
|
2020-11-30 00:03:12 +01:00
|
|
|
this->m_textEditor.SetText(ProjectFile::getPattern());
|
|
|
|
this->parsePattern(this->m_textEditor.GetText().data());
|
|
|
|
});
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<RequestAppendPatternLanguageCode>(this, [this](std::string code) {
|
2020-12-01 16:41:38 +01:00
|
|
|
this->m_textEditor.InsertText("\n");
|
|
|
|
this->m_textEditor.InsertText(code);
|
|
|
|
});
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventFileLoaded>(this, [this](const std::string &path) {
|
2020-12-05 22:30:09 +01:00
|
|
|
if (this->m_textEditor.GetText().find_first_not_of(" \f\n\r\t\v") != std::string::npos)
|
2020-11-30 00:03:12 +01:00
|
|
|
return;
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2020-11-30 00:03:12 +01:00
|
|
|
lang::Preprocessor preprocessor;
|
2020-11-21 14:39:16 +01:00
|
|
|
std::string magicFiles;
|
|
|
|
|
|
|
|
std::error_code error;
|
2021-03-01 08:56:49 +01:00
|
|
|
for (const auto &dir : hex::getPath(ImHexPath::Magic)) {
|
2021-04-03 17:08:16 +02:00
|
|
|
if (!std::filesystem::is_directory(dir))
|
|
|
|
continue;
|
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
for (const auto &entry : std::filesystem::directory_iterator(dir, error)) {
|
|
|
|
if (entry.is_regular_file() && entry.path().extension() == ".mgc")
|
|
|
|
magicFiles += entry.path().string() + MAGIC_PATH_SEPARATOR;
|
|
|
|
}
|
2020-11-21 14:39:16 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 22:30:09 +01:00
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
2021-01-12 23:28:41 +01:00
|
|
|
auto provider = SharedData::currentProvider;
|
2020-12-27 15:39:06 +01:00
|
|
|
|
|
|
|
if (provider == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<u8> buffer(std::min(provider->getSize(), size_t(0xFFFF)), 0x00);
|
2021-04-16 21:50:15 +02:00
|
|
|
provider->readRelative(0, buffer.data(), buffer.size());
|
2020-11-21 14:39:16 +01:00
|
|
|
|
|
|
|
std::string mimeType;
|
|
|
|
|
|
|
|
magic_t cookie = magic_open(MAGIC_MIME_TYPE);
|
|
|
|
if (magic_load(cookie, magicFiles.c_str()) != -1)
|
|
|
|
mimeType = magic_buffer(cookie, buffer.data(), buffer.size());
|
|
|
|
|
|
|
|
magic_close(cookie);
|
|
|
|
|
|
|
|
bool foundCorrectType = false;
|
|
|
|
preprocessor.addPragmaHandler("MIME", [&mimeType, &foundCorrectType](std::string value) {
|
|
|
|
if (value == mimeType) {
|
|
|
|
foundCorrectType = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !std::all_of(value.begin(), value.end(), isspace) && !value.ends_with('\n') && !value.ends_with('\r');
|
|
|
|
});
|
2020-11-27 21:20:23 +01:00
|
|
|
preprocessor.addDefaultPragmaHandlers();
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2021-05-18 18:27:36 +02:00
|
|
|
this->m_possiblePatternFiles.clear();
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2020-11-23 00:12:33 +01:00
|
|
|
std::error_code errorCode;
|
2021-03-01 08:56:49 +01:00
|
|
|
for (const auto &dir : hex::getPath(ImHexPath::Patterns)) {
|
|
|
|
for (auto &entry : std::filesystem::directory_iterator(dir, errorCode)) {
|
|
|
|
if (!entry.is_regular_file())
|
|
|
|
continue;
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
FILE *file = fopen(entry.path().string().c_str(), "r");
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
if (file == nullptr)
|
|
|
|
continue;
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size_t size = ftell(file);
|
|
|
|
rewind(file);
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
std::vector<char> patternBuffer( size + 1, 0x00);
|
|
|
|
fread(patternBuffer.data(), 1, size, file);
|
|
|
|
fclose(file);
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
preprocessor.preprocess(patternBuffer.data());
|
2020-11-21 14:39:16 +01:00
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
if (foundCorrectType)
|
|
|
|
this->m_possiblePatternFiles.push_back(entry.path().string());
|
|
|
|
}
|
2021-01-23 14:01:23 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
|
2021-01-23 14:01:23 +01:00
|
|
|
if (!this->m_possiblePatternFiles.empty()) {
|
|
|
|
this->m_selectedPatternFile = 0;
|
2021-02-11 23:09:45 +01:00
|
|
|
View::doLater([] { ImGui::OpenPopup("hex.view.pattern.accept_pattern"_lang); });
|
2020-11-21 14:39:16 +01:00
|
|
|
}
|
|
|
|
});
|
2021-01-11 20:32:12 +01:00
|
|
|
|
|
|
|
/* Settings */
|
|
|
|
{
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventSettingsChanged>(this, [this]() {
|
2021-02-16 23:42:35 +01:00
|
|
|
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
|
|
|
|
|
2021-03-06 12:40:29 +01:00
|
|
|
if (theme.is_number()) {
|
|
|
|
switch (static_cast<int>(theme)) {
|
2021-02-16 23:42:35 +01:00
|
|
|
default:
|
|
|
|
case 0: /* Dark theme */
|
|
|
|
this->m_textEditor.SetPalette(TextEditor::GetDarkPalette());
|
|
|
|
break;
|
|
|
|
case 1: /* Light theme */
|
|
|
|
this->m_textEditor.SetPalette(TextEditor::GetLightPalette());
|
|
|
|
break;
|
|
|
|
case 2: /* Classic theme */
|
|
|
|
this->m_textEditor.SetPalette(TextEditor::GetRetroBluePalette());
|
|
|
|
break;
|
|
|
|
}
|
2021-01-11 20:32:12 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
2020-11-20 18:24:59 +01:00
|
|
|
|
2021-08-28 21:51:33 +02:00
|
|
|
ViewPatternEditor::~ViewPatternEditor() {
|
2021-02-01 20:07:57 +01:00
|
|
|
delete this->m_patternLanguageRuntime;
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::unsubscribe<EventProjectFileStore>(this);
|
|
|
|
EventManager::unsubscribe<EventProjectFileLoad>(this);
|
|
|
|
EventManager::unsubscribe<RequestAppendPatternLanguageCode>(this);
|
|
|
|
EventManager::unsubscribe<EventFileLoaded>(this);
|
|
|
|
EventManager::unsubscribe<EventSettingsChanged>(this);
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:51:33 +02:00
|
|
|
void ViewPatternEditor::drawMenu() {
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::BeginMenu("hex.menu.file"_lang)) {
|
|
|
|
if (ImGui::MenuItem("hex.view.pattern.menu.file.load_pattern"_lang)) {
|
2021-08-28 00:45:59 +02:00
|
|
|
hex::openFileBrowser("hex.view.pattern.open_pattern"_lang, DialogMode::Open, { { "Pattern File", "hexpat" } }, [this](auto path) {
|
2021-01-27 00:44:10 +01:00
|
|
|
this->loadPatternFile(path);
|
|
|
|
});
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 21:51:33 +02:00
|
|
|
void ViewPatternEditor::drawContent() {
|
2021-03-03 22:26:17 +01:00
|
|
|
if (ImGui::Begin(View::toWindowName("hex.view.pattern.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_None | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse)) {
|
2021-01-12 23:28:41 +01:00
|
|
|
auto provider = SharedData::currentProvider;
|
2020-12-27 15:39:06 +01:00
|
|
|
|
|
|
|
if (provider != nullptr && provider->isAvailable()) {
|
2021-01-09 21:45:21 +01:00
|
|
|
auto textEditorSize = ImGui::GetContentRegionAvail();
|
|
|
|
textEditorSize.y *= 4.0/5.0;
|
2021-02-20 22:38:31 +01:00
|
|
|
textEditorSize.y -= ImGui::GetTextLineHeightWithSpacing();
|
2021-02-21 13:49:03 +01:00
|
|
|
this->m_textEditor.Render("hex.view.pattern.name"_lang, textEditorSize, true);
|
2021-01-09 21:45:21 +01:00
|
|
|
|
|
|
|
auto consoleSize = ImGui::GetContentRegionAvail();
|
2021-02-20 22:38:31 +01:00
|
|
|
consoleSize.y -= ImGui::GetTextLineHeightWithSpacing();
|
2021-01-09 21:45:21 +01:00
|
|
|
|
2021-02-20 22:38:31 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_ChildBg, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Background)]);
|
2021-01-09 21:45:21 +01:00
|
|
|
if (ImGui::BeginChild("##console", consoleSize, true, ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
|
2021-01-09 23:48:42 +01:00
|
|
|
for (auto &[level, message] : this->m_console) {
|
|
|
|
switch (level) {
|
2021-01-21 11:36:58 +01:00
|
|
|
case lang::LogConsole::Level::Debug:
|
2021-01-11 20:32:12 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Comment)]);
|
2021-01-09 23:48:42 +01:00
|
|
|
break;
|
2021-01-21 11:36:58 +01:00
|
|
|
case lang::LogConsole::Level::Info:
|
2021-01-11 20:32:12 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Default)]);
|
2021-01-09 23:48:42 +01:00
|
|
|
break;
|
2021-01-21 11:36:58 +01:00
|
|
|
case lang::LogConsole::Level::Warning:
|
2021-01-11 20:32:12 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Preprocessor)]);
|
2021-01-09 23:48:42 +01:00
|
|
|
break;
|
2021-01-21 11:36:58 +01:00
|
|
|
case lang::LogConsole::Level::Error:
|
2021-01-11 20:32:12 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::ErrorMarker)]);
|
2021-01-09 23:48:42 +01:00
|
|
|
break;
|
|
|
|
default: continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::TextUnformatted(message.c_str());
|
|
|
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
}
|
2021-08-28 14:22:02 +02:00
|
|
|
|
2021-01-09 21:45:21 +01:00
|
|
|
}
|
2021-08-28 16:02:53 +02:00
|
|
|
ImGui::EndChild();
|
2021-01-09 23:48:42 +01:00
|
|
|
ImGui::PopStyleColor(1);
|
2020-11-20 20:26:19 +01:00
|
|
|
|
2021-08-28 16:16:06 +02:00
|
|
|
ImGui::Disabled([this] {
|
2021-02-20 22:38:31 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(ImColor(0x20, 0x85, 0x20)));
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1);
|
|
|
|
|
2021-02-22 11:56:33 +01:00
|
|
|
if (ImGui::ArrowButton("evaluate", ImGuiDir_Right))
|
2021-02-20 22:38:31 +01:00
|
|
|
this->parsePattern(this->m_textEditor.GetText().data());
|
|
|
|
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
ImGui::PopStyleColor();
|
2021-02-22 11:56:33 +01:00
|
|
|
}, this->m_evaluatorRunning);
|
2021-02-20 22:38:31 +01:00
|
|
|
|
|
|
|
ImGui::SameLine();
|
2021-02-22 11:56:33 +01:00
|
|
|
if (this->m_evaluatorRunning)
|
2021-08-28 16:16:06 +02:00
|
|
|
ImGui::TextSpinner("hex.view.pattern.evaluating"_lang);
|
2021-02-20 22:38:31 +01:00
|
|
|
else
|
2021-02-20 22:41:17 +01:00
|
|
|
ImGui::Checkbox("hex.view.pattern.auto"_lang, &this->m_runAutomatically);
|
2021-02-20 22:38:31 +01:00
|
|
|
|
2021-08-28 16:16:06 +02:00
|
|
|
if (this->m_textEditor.IsTextChanged()) {
|
|
|
|
if (this->m_runAutomatically)
|
|
|
|
this->parsePattern(this->m_textEditor.GetText().data());
|
|
|
|
|
|
|
|
ProjectFile::markDirty();
|
2020-11-21 00:12:58 +01:00
|
|
|
}
|
2020-11-20 20:26:19 +01:00
|
|
|
}
|
2021-01-21 17:48:24 +01:00
|
|
|
|
2021-01-28 13:23:50 +01:00
|
|
|
View::discardNavigationRequests();
|
2020-11-11 10:47:02 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
ImGui::End();
|
2021-02-07 14:29:13 +01:00
|
|
|
}
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2021-08-28 21:51:33 +02:00
|
|
|
void ViewPatternEditor::drawAlwaysVisible() {
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::BeginPopupModal("hex.view.pattern.accept_pattern"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
|
|
|
|
ImGui::TextWrapped("hex.view.pattern.accept_pattern.desc"_lang);
|
2021-01-23 14:01:23 +01:00
|
|
|
|
2021-05-18 18:27:36 +02:00
|
|
|
std::vector<std::string> entries;
|
|
|
|
entries.resize(this->m_possiblePatternFiles.size());
|
2021-01-23 14:01:23 +01:00
|
|
|
|
2021-05-18 18:27:36 +02:00
|
|
|
for (u32 i = 0; i < entries.size(); i++) {
|
|
|
|
entries[i] = std::filesystem::path(this->m_possiblePatternFiles[i]).filename().string();
|
2021-01-23 14:01:23 +01:00
|
|
|
}
|
|
|
|
|
2021-05-18 18:27:36 +02:00
|
|
|
ImGui::ListBox("hex.view.pattern.accept_pattern.patterns"_lang, &this->m_selectedPatternFile, [](void *data, int id, const char** outText) -> bool {
|
|
|
|
auto &entries = *static_cast<std::vector<std::string>*>(data);
|
|
|
|
|
|
|
|
*outText = entries[id].c_str();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}, &entries, entries.size(), 4);
|
2021-01-23 14:01:23 +01:00
|
|
|
|
2020-11-21 14:39:16 +01:00
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::Text("hex.view.pattern.accept_pattern.question"_lang);
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
confirmButtons("hex.common.yes"_lang, "hex.common.no"_lang, [this]{
|
2021-03-01 08:56:49 +01:00
|
|
|
this->loadPatternFile(this->m_possiblePatternFiles[this->m_selectedPatternFile]);
|
2020-11-21 14:39:16 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
2021-01-14 17:01:44 +01:00
|
|
|
}, []{
|
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape)))
|
2020-11-21 14:39:16 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-21 14:39:16 +01:00
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 18:24:59 +01:00
|
|
|
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2021-08-28 21:51:33 +02:00
|
|
|
void ViewPatternEditor::loadPatternFile(std::string_view path) {
|
2021-02-07 13:40:47 +01:00
|
|
|
FILE *file = fopen(path.data(), "rb");
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-21 14:39:16 +01:00
|
|
|
if (file != nullptr) {
|
|
|
|
char *buffer;
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size_t size = ftell(file);
|
|
|
|
rewind(file);
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-21 14:39:16 +01:00
|
|
|
buffer = new char[size + 1];
|
2020-11-20 18:24:59 +01:00
|
|
|
|
2020-11-21 14:39:16 +01:00
|
|
|
fread(buffer, size, 1, file);
|
|
|
|
buffer[size] = 0x00;
|
|
|
|
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
this->parsePattern(buffer);
|
|
|
|
this->m_textEditor.SetText(buffer);
|
|
|
|
|
|
|
|
delete[] buffer;
|
2020-11-12 21:20:51 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:51:33 +02:00
|
|
|
void ViewPatternEditor::clearPatternData() {
|
2021-04-20 21:46:48 +02:00
|
|
|
for (auto &data : SharedData::patternData)
|
2020-11-14 14:42:21 +01:00
|
|
|
delete data;
|
2020-11-13 12:07:05 +01:00
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
SharedData::patternData.clear();
|
2020-11-19 11:36:52 +01:00
|
|
|
lang::PatternData::resetPalette();
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:51:33 +02:00
|
|
|
void ViewPatternEditor::parsePattern(char *buffer) {
|
2021-02-22 11:56:33 +01:00
|
|
|
this->m_evaluatorRunning = true;
|
2021-02-20 22:38:31 +01:00
|
|
|
|
2020-11-14 14:42:21 +01:00
|
|
|
this->clearPatternData();
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_textEditor.SetErrorMarkers({ });
|
2021-01-09 21:45:21 +01:00
|
|
|
this->m_console.clear();
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::post<EventPatternChanged>();
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2021-02-20 22:38:31 +01:00
|
|
|
std::thread([this, buffer = std::string(buffer)] {
|
|
|
|
auto result = this->m_patternLanguageRuntime->executeString(SharedData::currentProvider, buffer);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2021-02-20 22:38:31 +01:00
|
|
|
auto error = this->m_patternLanguageRuntime->getError();
|
|
|
|
if (error.has_value()) {
|
|
|
|
this->m_textEditor.SetErrorMarkers({ error.value() });
|
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2021-02-20 22:38:31 +01:00
|
|
|
this->m_console = this->m_patternLanguageRuntime->getConsoleLog();
|
|
|
|
|
|
|
|
if (result.has_value()) {
|
2021-04-20 21:46:48 +02:00
|
|
|
SharedData::patternData = std::move(result.value());
|
2021-03-27 11:36:36 +01:00
|
|
|
View::doLater([]{
|
|
|
|
EventManager::post<EventPatternChanged>();
|
|
|
|
});
|
2021-02-20 22:38:31 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 11:56:33 +01:00
|
|
|
this->m_evaluatorRunning = false;
|
2021-02-20 22:38:31 +01:00
|
|
|
}).detach();
|
2020-11-10 21:31:04 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|