2020-11-10 21:31:04 +01:00
|
|
|
#include "views/view_pattern.hpp"
|
|
|
|
|
2020-11-17 02:31:51 +01:00
|
|
|
#include "lang/preprocessor.hpp"
|
|
|
|
#include "lang/parser.hpp"
|
|
|
|
#include "lang/lexer.hpp"
|
|
|
|
#include "lang/validator.hpp"
|
2020-11-19 11:36:52 +01:00
|
|
|
#include "lang/evaluator.hpp"
|
2020-11-10 21:31:04 +01:00
|
|
|
#include "utils.hpp"
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2020-11-19 11:36:52 +01:00
|
|
|
ViewPattern::ViewPattern(prv::Provider* &dataProvider, std::vector<lang::PatternData*> &patternData)
|
2020-11-14 14:42:21 +01:00
|
|
|
: View(), m_dataProvider(dataProvider), m_patternData(patternData) {
|
2020-11-13 14:35:52 +01:00
|
|
|
|
|
|
|
this->m_buffer = new char[0xFF'FFFF];
|
|
|
|
std::memset(this->m_buffer, 0x00, 0xFF'FFFF);
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
ViewPattern::~ViewPattern() {
|
2020-11-13 13:50:59 +01:00
|
|
|
if (this->m_buffer != nullptr)
|
|
|
|
delete[] this->m_buffer;
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewPattern::createMenu() {
|
|
|
|
if (ImGui::BeginMenu("File")) {
|
|
|
|
if (ImGui::MenuItem("Load pattern...")) {
|
2020-11-17 15:38:24 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("Open Hex Pattern"); });
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:22:55 +01:00
|
|
|
if (ImGui::BeginMenu("View")) {
|
2020-11-10 21:31:04 +01:00
|
|
|
ImGui::MenuItem("Pattern View", "", &this->m_windowOpen);
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewPattern::createView() {
|
|
|
|
if (!this->m_windowOpen)
|
|
|
|
return;
|
|
|
|
|
2020-11-11 10:47:02 +01:00
|
|
|
if (ImGui::Begin("Pattern", &this->m_windowOpen, ImGuiWindowFlags_None)) {
|
2020-11-13 14:35:52 +01:00
|
|
|
if (this->m_buffer != nullptr && this->m_dataProvider != nullptr && this->m_dataProvider->isReadable()) {
|
2020-11-13 13:06:22 +01:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-13 13:06:22 +01:00
|
|
|
auto size = ImGui::GetWindowSize();
|
|
|
|
size.y -= 50;
|
|
|
|
ImGui::InputTextMultiline("Pattern", this->m_buffer, 0xFFFF, size,
|
|
|
|
ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CallbackEdit,
|
|
|
|
[](ImGuiInputTextCallbackData *data) -> int {
|
|
|
|
auto _this = static_cast<ViewPattern *>(data->UserData);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-13 13:06:22 +01:00
|
|
|
_this->parsePattern(data->Buf);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-13 13:06:22 +01:00
|
|
|
return 0;
|
|
|
|
}, this
|
|
|
|
);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-13 13:06:22 +01:00
|
|
|
ImGui::PopStyleVar(2);
|
|
|
|
}
|
2020-11-11 10:47:02 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
ImGui::End();
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-17 15:38:24 +01:00
|
|
|
if (this->m_fileBrowser.showFileDialog("Open Hex Pattern", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(0, 0), ".hexpat")) {
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-17 15:38:24 +01:00
|
|
|
FILE *file = fopen(this->m_fileBrowser.selected_path.c_str(), "rb");
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-13 14:35:52 +01:00
|
|
|
if (file != nullptr) {
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size_t size = ftell(file);
|
|
|
|
rewind(file);
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-13 14:35:52 +01:00
|
|
|
if (size >= 0xFF'FFFF) {
|
|
|
|
fclose(file);
|
|
|
|
return;
|
|
|
|
}
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-13 14:35:52 +01:00
|
|
|
fread(this->m_buffer, size, 1, file);
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-13 14:35:52 +01:00
|
|
|
fclose(file);
|
2020-11-12 21:20:51 +01:00
|
|
|
|
2020-11-13 14:35:52 +01:00
|
|
|
this->parsePattern(this->m_buffer);
|
|
|
|
}
|
2020-11-12 21:20:51 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-14 14:42:21 +01:00
|
|
|
void ViewPattern::clearPatternData() {
|
|
|
|
for (auto &data : this->m_patternData)
|
|
|
|
delete data;
|
2020-11-13 12:07:05 +01:00
|
|
|
|
2020-11-14 14:42:21 +01:00
|
|
|
this->m_patternData.clear();
|
2020-11-19 11:36:52 +01:00
|
|
|
lang::PatternData::resetPalette();
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<std::derived_from<lang::ASTNode> T>
|
|
|
|
static std::vector<T*> findNodes(const lang::ASTNode::Type type, const std::vector<lang::ASTNode*> &nodes) {
|
|
|
|
std::vector<T*> result;
|
|
|
|
|
|
|
|
for (const auto & node : nodes)
|
|
|
|
if (node->getType() == type)
|
|
|
|
result.push_back(static_cast<T*>(node));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewPattern::parsePattern(char *buffer) {
|
2020-11-19 11:36:52 +01:00
|
|
|
hex::lang::Preprocessor preprocessor;
|
|
|
|
hex::lang::Lexer lexer;
|
|
|
|
hex::lang::Parser parser;
|
|
|
|
hex::lang::Validator validator;
|
|
|
|
hex::lang::Evaluator evaluator;
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-14 14:42:21 +01:00
|
|
|
this->clearPatternData();
|
2020-11-15 00:46:18 +01:00
|
|
|
this->postEvent(Events::PatternChanged);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-17 02:31:51 +01:00
|
|
|
auto [preprocessingResult, preprocesedCode] = preprocessor.preprocess(buffer);
|
|
|
|
if (preprocessingResult.failed())
|
|
|
|
return;
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-17 02:31:51 +01:00
|
|
|
auto [lexResult, tokens] = lexer.lex(preprocesedCode);
|
2020-11-10 21:31:04 +01:00
|
|
|
if (lexResult.failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto [parseResult, ast] = parser.parse(tokens);
|
|
|
|
if (parseResult.failed()) {
|
|
|
|
return;
|
2020-11-16 22:54:39 +01:00
|
|
|
}
|
|
|
|
|
2020-11-19 11:36:52 +01:00
|
|
|
hex::ScopeExit deleteAst([&ast]{ for(auto &node : ast) delete node; });
|
|
|
|
|
2020-11-16 22:54:39 +01:00
|
|
|
auto validatorResult = validator.validate(ast);
|
|
|
|
if (!validatorResult) {
|
|
|
|
return;
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-19 11:36:52 +01:00
|
|
|
auto [evaluateResult, patternData] = evaluator.evaluate(ast);
|
|
|
|
if (evaluateResult.failed()) {
|
|
|
|
return;
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
2020-11-19 11:36:52 +01:00
|
|
|
this->m_patternData = patternData;
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-15 00:46:18 +01:00
|
|
|
this->postEvent(Events::PatternChanged);
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|