1
0
mirror of synced 2024-12-01 02:37:18 +01:00

fix: Crash when evaluating patterns to quickly

This commit is contained in:
WerWolv 2023-04-17 17:02:10 +02:00
parent 99a736df27
commit 04a5efc7a3
6 changed files with 27 additions and 23 deletions

@ -1 +1 @@
Subproject commit ce644cf16223f4e335a49e8f3e3cc3d9d9afe4ae Subproject commit a84deb2891c4bba0c694a85e05179dd791d0845f

View File

@ -585,7 +585,7 @@ namespace hex {
log::debug("Closing popup '{}'", name); log::debug("Closing popup '{}'", name);
positionSet = sizeSet = false; positionSet = sizeSet = false;
popups.pop_back(); popups.erase(std::find(popups.begin(), popups.end(), currPopup));
} }
} }
} }

View File

@ -20,6 +20,7 @@ namespace hex::plugin::builtin {
private: private:
ui::PatternDrawer m_patternDrawer; ui::PatternDrawer m_patternDrawer;
bool m_shouldReset = false;
}; };
} }

View File

@ -19,6 +19,10 @@ namespace hex::plugin::builtin {
}); });
this->m_patternDrawer.setSelectionCallback([](Region region){ ImHexApi::HexEditor::setSelection(region); }); this->m_patternDrawer.setSelectionCallback([](Region region){ ImHexApi::HexEditor::setSelection(region); });
EventManager::subscribe<EventPatternExecuted>([this](const auto&){
this->m_shouldReset = true;
});
} }
ViewPatternData::~ViewPatternData() { ViewPatternData::~ViewPatternData() {
@ -29,23 +33,19 @@ namespace hex::plugin::builtin {
void ViewPatternData::drawContent() { void ViewPatternData::drawContent() {
if (ImGui::Begin(View::toWindowName("hex.builtin.view.pattern_data.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { if (ImGui::Begin(View::toWindowName("hex.builtin.view.pattern_data.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
if (ImHexApi::Provider::isValid()) { if (ImHexApi::Provider::isValid()) {
auto provider = ImHexApi::Provider::get();
auto &runtime = ContentRegistry::PatternLanguage::getRuntime(); auto &runtime = ContentRegistry::PatternLanguage::getRuntime();
if (!runtime.arePatternsValid()) {
this->m_patternDrawer.draw({});
} else {
auto lock = ContentRegistry::PatternLanguage::getRuntimeLock();
const auto &patterns = [&] -> const auto& { if (this->m_shouldReset) {
if (provider->isReadable()) this->m_patternDrawer.reset();
return runtime.getAllPatterns(); this->m_shouldReset = false;
else {
static const std::vector<std::shared_ptr<pl::ptrn::Pattern>> empty;
return empty;
} }
}();
if (runtime.isRunning()) this->m_patternDrawer.draw(runtime.getAllPatterns());
this->m_patternDrawer.reset(); }
this->m_patternDrawer.draw(patterns);
} }
} }
ImGui::End(); ImGui::End();

View File

@ -396,7 +396,7 @@ namespace hex::plugin::builtin {
ImGui::Checkbox(label.c_str(), &value); ImGui::Checkbox(label.c_str(), &value);
variable.value = value; variable.value = value;
} else if (variable.type == pl::core::Token::ValueType::Character) { } else if (variable.type == pl::core::Token::ValueType::Character) {
std::array<char, 2> buffer; std::array<char, 2> buffer = { };
ImGui::InputText(label.c_str(), buffer.data(), buffer.size()); ImGui::InputText(label.c_str(), buffer.data(), buffer.size());
variable.value = buffer[0]; variable.value = buffer[0];
} }
@ -653,14 +653,13 @@ namespace hex::plugin::builtin {
this->m_sectionWindowDrawer.clear(); this->m_sectionWindowDrawer.clear();
EventManager::post<EventHighlightingChanged>(); EventManager::post<EventHighlightingChanged>();
EventManager::post<EventPatternExecuted>(code);
TaskManager::createTask("hex.builtin.view.pattern_editor.evaluating", TaskManager::NoProgress, [this, code, provider](auto &task) { auto &runtime = ContentRegistry::PatternLanguage::getRuntime();
ContentRegistry::PatternLanguage::configureRuntime(runtime, provider);
TaskManager::createTask("hex.builtin.view.pattern_editor.evaluating", TaskManager::NoProgress, [this, code, &runtime](auto &task) {
auto lock = ContentRegistry::PatternLanguage::getRuntimeLock(); auto lock = ContentRegistry::PatternLanguage::getRuntimeLock();
auto &runtime = ContentRegistry::PatternLanguage::getRuntime();
ContentRegistry::PatternLanguage::configureRuntime(runtime, provider);
task.setInterruptCallback([&runtime] { runtime.abort(); }); task.setInterruptCallback([&runtime] { runtime.abort(); });
std::map<std::string, pl::core::Token::Literal> envVars; std::map<std::string, pl::core::Token::Literal> envVars;
@ -703,6 +702,10 @@ namespace hex::plugin::builtin {
if (!this->m_lastEvaluationResult) { if (!this->m_lastEvaluationResult) {
*this->m_lastEvaluationError = runtime.getError(); *this->m_lastEvaluationError = runtime.getError();
} }
TaskManager::doLater([code = std::move(code)] {
EventManager::post<EventPatternExecuted>(code);
});
}); });
} }

View File

@ -81,14 +81,14 @@ namespace hex::plugin::builtin {
wolv::io::fs::remove(s_safetyBackupPath); wolv::io::fs::remove(s_safetyBackupPath);
Popup::close(); this->close();
} }
ImGui::SameLine(); ImGui::SameLine();
ImGui::SetCursorPosX(width / 9 * 5); ImGui::SetCursorPosX(width / 9 * 5);
if (ImGui::Button("hex.builtin.popup.safety_backup.delete"_lang, ImVec2(width / 3, 0))) { if (ImGui::Button("hex.builtin.popup.safety_backup.delete"_lang, ImVec2(width / 3, 0))) {
wolv::io::fs::remove(s_safetyBackupPath); wolv::io::fs::remove(s_safetyBackupPath);
Popup::close(); this->close();
} }
} }
}; };