From 763f15fa36d382382c04ad36d1c21431df177828 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 31 Jul 2022 20:07:15 +0200 Subject: [PATCH] sys: Added setting to synchronize pattern source code between providers --- .../content/views/view_pattern_editor.hpp | 71 ++++++++++--------- .../source/content/settings_entries.cpp | 11 +++ .../content/views/view_pattern_editor.cpp | 37 +++++++--- .../source/content/views/view_settings.cpp | 12 +++- plugins/builtin/source/lang/de_DE.cpp | 3 +- plugins/builtin/source/lang/en_US.cpp | 1 + plugins/builtin/source/lang/it_IT.cpp | 1 + plugins/builtin/source/lang/ja_JP.cpp | 1 + plugins/builtin/source/lang/pt_BR.cpp | 1 + plugins/builtin/source/lang/zh_CN.cpp | 1 + plugins/builtin/source/lang/zh_TW.cpp | 1 + 11 files changed, 96 insertions(+), 44 deletions(-) diff --git a/plugins/builtin/include/content/views/view_pattern_editor.hpp b/plugins/builtin/include/content/views/view_pattern_editor.hpp index 288be915c..57dd2ee3f 100644 --- a/plugins/builtin/include/content/views/view_pattern_editor.hpp +++ b/plugins/builtin/include/content/views/view_pattern_editor.hpp @@ -24,6 +24,40 @@ namespace hex::plugin::builtin { void drawAlwaysVisible() override; void drawContent() override; + private: + struct PatternVariable { + bool inVariable; + bool outVariable; + + pl::Token::ValueType type; + pl::Token::Literal value; + }; + + enum class EnvVarType + { + Integer, + Float, + String, + Bool + }; + + struct EnvVar { + u64 id; + std::string name; + pl::Token::Literal value; + EnvVarType type; + + bool operator==(const EnvVar &other) const { + return this->id == other.id; + } + }; + + enum class DangerousFunctionPerms : u8 { + Ask, + Allow, + Deny + }; + private: std::unique_ptr m_parserRuntime; @@ -47,47 +81,18 @@ namespace hex::plugin::builtin { TextEditor m_textEditor; std::vector> m_console; - struct PatternVariable { - bool inVariable; - bool outVariable; - - pl::Token::ValueType type; - pl::Token::Literal value; - }; - std::map m_patternVariables; - enum class EnvVarType - { - Integer, - Float, - String, - Bool - }; - - struct EnvVar { - u64 id; - std::string name; - pl::Token::Literal value; - EnvVarType type; - - bool operator==(const EnvVar &other) const { - return this->id == other.id; - } - }; - u64 m_envVarIdCounter; std::list m_envVarEntries; - enum class DangerousFunctionPerms : u8 { - Ask, - Allow, - Deny - }; - std::atomic m_dangerousFunctionCalled = false; std::atomic m_dangerousFunctionsAllowed = DangerousFunctionPerms::Ask; + bool m_syncPatternSourceCode = false; + bool m_autoLoadPatterns = true; + + private: void drawConsole(ImVec2 size); void drawEnvVars(ImVec2 size); void drawVariableSettings(ImVec2 size); diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index ef74614a8..9b4a6df7f 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -54,6 +54,17 @@ namespace hex::plugin::builtin { return false; }); + ContentRegistry::Settings::add("hex.builtin.setting.general", "hex.builtin.setting.general.sync_pattern_source", 0, [](auto name, nlohmann::json &setting) { + static bool enabled = static_cast(setting); + + if (ImGui::Checkbox(name.data(), &enabled)) { + setting = static_cast(enabled); + return true; + } + + return false; + }); + /* Interface */ ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", 0, [](auto name, nlohmann::json &setting) { diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 2fd62917f..d266a83c5 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -1,18 +1,21 @@ #include "content/views/view_pattern_editor.hpp" +#include + #include #include #include #include #include -#include #include #include #include #include #include +#include + namespace hex::plugin::builtin { using namespace hex::literals; @@ -96,12 +99,28 @@ namespace hex::plugin::builtin { this->m_textEditor.SetText(code); }); + EventManager::subscribe(this, [this] { + { + auto syncPatternSource = ContentRegistry::Settings::getSetting("hex.builtin.setting.general", "hex.builtin.setting.general.sync_pattern_source"); + + if (syncPatternSource.is_number()) + this->m_syncPatternSourceCode = static_cast(syncPatternSource); + } + + { + auto autoLoadPatterns = ContentRegistry::Settings::getSetting("hex.builtin.setting.general", "hex.builtin.setting.general.auto_load_patterns"); + + if (autoLoadPatterns.is_number()) + this->m_autoLoadPatterns = static_cast(autoLoadPatterns); + } + }); + EventManager::subscribe(this, [this](prv::Provider *provider) { - if (!ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.auto_load_patterns", 1)) + if (!this->m_autoLoadPatterns) return; // Copy over current pattern source code to the new provider - { + if (!this->m_syncPatternSourceCode) { provider->getPatternLanguageSourceCode() = this->m_textEditor.GetText(); } @@ -154,12 +173,14 @@ namespace hex::plugin::builtin { }); EventManager::subscribe(this, [this](prv::Provider *oldProvider, prv::Provider *newProvider) { - if (oldProvider != nullptr) oldProvider->getPatternLanguageSourceCode() = this->m_textEditor.GetText(); - if (newProvider != nullptr) this->m_textEditor.SetText(newProvider->getPatternLanguageSourceCode()); + if (!this->m_syncPatternSourceCode) { + if (oldProvider != nullptr) oldProvider->getPatternLanguageSourceCode() = this->m_textEditor.GetText(); + if (newProvider != nullptr) this->m_textEditor.SetText(newProvider->getPatternLanguageSourceCode()); - auto lines = this->m_textEditor.GetTextLines(); - lines.pop_back(); - this->m_textEditor.SetTextLines(lines); + auto lines = this->m_textEditor.GetTextLines(); + lines.pop_back(); + this->m_textEditor.SetTextLines(lines); + } }); /* Settings */ diff --git a/plugins/builtin/source/content/views/view_settings.cpp b/plugins/builtin/source/content/views/view_settings.cpp index 275fba414..1efe834a8 100644 --- a/plugins/builtin/source/content/views/view_settings.cpp +++ b/plugins/builtin/source/content/views/view_settings.cpp @@ -56,8 +56,16 @@ namespace hex::plugin::builtin { ImGui::Separator(); for (auto &[name, requiresRestart, callback] : settings) { - if (callback(LangEntry(name), ContentRegistry::Settings::getSettingsData()[category.name][name])) { - log::debug("Setting [{}]: {} was changed", category.name, name); + auto &setting = ContentRegistry::Settings::getSettingsData()[category.name][name]; + if (callback(LangEntry(name), setting)) { + log::debug("Setting [{}]: {} was changed to {}", category.name, name, [&] -> std::string{ + if (setting.is_number()) + return std::to_string(setting.get()); + else if (setting.is_string()) + return setting.get(); + else + return ""; + }()); EventManager::post(); if (requiresRestart) diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index a92a0b16e..ef7477207 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -682,7 +682,8 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.imhex.recent_files", "Kürzlich geöffnete Dateien" }, { "hex.builtin.setting.general", "Allgemein" }, { "hex.builtin.setting.general.show_tips", "Tipps beim start anzeigen" }, - { "hex.builtin.setting.general.auto_load_patterns", "Unterstützte Pattern automatisch laden" }, + { "hex.builtin.setting.general.auto_load_patterns", "Automatisches Pattern laden" }, + { "hex.builtin.setting.general.sync_pattern_source", "Pattern Source Code zwischen Providern synchronisieren" }, { "hex.builtin.setting.interface", "Aussehen" }, { "hex.builtin.setting.interface.color", "Farbthema" }, { "hex.builtin.setting.interface.color.system", "System" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 0766e9bdd..38ce6e4f4 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -687,6 +687,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.general", "General" }, { "hex.builtin.setting.general.show_tips", "Show tips on startup" }, { "hex.builtin.setting.general.auto_load_patterns", "Auto-load supported pattern" }, + { "hex.builtin.setting.general.sync_pattern_source", "Sync pattern source code between providers" }, { "hex.builtin.setting.interface", "Interface" }, { "hex.builtin.setting.interface.color", "Color theme" }, { "hex.builtin.setting.interface.color.system", "System" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index 563301a0c..40e0739e7 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -691,6 +691,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.general", "Generali" }, { "hex.builtin.setting.general.show_tips", "Mostra consigli all'avvio" }, { "hex.builtin.setting.general.auto_load_patterns", "Auto-caricamento del pattern supportato" }, + // { "hex.builtin.setting.general.sync_pattern_source", "Sync pattern source code between providers" }, { "hex.builtin.setting.interface", "Interfaccia" }, { "hex.builtin.setting.interface.color", "Colore del Tema" }, { "hex.builtin.setting.interface.color.system", "Sistema" }, diff --git a/plugins/builtin/source/lang/ja_JP.cpp b/plugins/builtin/source/lang/ja_JP.cpp index e5b45cf99..6998be5b0 100644 --- a/plugins/builtin/source/lang/ja_JP.cpp +++ b/plugins/builtin/source/lang/ja_JP.cpp @@ -685,6 +685,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.general", "一般" }, { "hex.builtin.setting.general.show_tips", "起動時に豆知識を表示" }, { "hex.builtin.setting.general.auto_load_patterns", "対応するパターンを自動で読み込む" }, + // { "hex.builtin.setting.general.sync_pattern_source", "Sync pattern source code between providers" }, { "hex.builtin.setting.interface", "インタフェース" }, { "hex.builtin.setting.interface.color", "カラーテーマ" }, { "hex.builtin.setting.interface.color.system", "システム設定に従う" }, diff --git a/plugins/builtin/source/lang/pt_BR.cpp b/plugins/builtin/source/lang/pt_BR.cpp index 1b9b3915f..75206f4fc 100644 --- a/plugins/builtin/source/lang/pt_BR.cpp +++ b/plugins/builtin/source/lang/pt_BR.cpp @@ -683,6 +683,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.general", "General" }, { "hex.builtin.setting.general.show_tips", "Mostrar dicas na inicialização" }, { "hex.builtin.setting.general.auto_load_patterns", "Padrão compatível com carregamento automático" }, + // { "hex.builtin.setting.general.sync_pattern_source", "Sync pattern source code between providers" }, { "hex.builtin.setting.interface", "Interface" }, { "hex.builtin.setting.interface.color", "Color theme" }, { "hex.builtin.setting.interface.color.system", "Sistema" }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index 4199cc76b..d12cc2351 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -688,6 +688,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.general", "通用" }, { "hex.builtin.setting.general.show_tips", "在启动时显示每日提示" }, { "hex.builtin.setting.general.auto_load_patterns", "自动加载支持的模式" }, + // { "hex.builtin.setting.general.sync_pattern_source", "Sync pattern source code between providers" }, { "hex.builtin.setting.interface", "界面" }, { "hex.builtin.setting.interface.color", "颜色主题" }, { "hex.builtin.setting.interface.color.system", "跟随系统" }, diff --git a/plugins/builtin/source/lang/zh_TW.cpp b/plugins/builtin/source/lang/zh_TW.cpp index 8451e5669..729632092 100644 --- a/plugins/builtin/source/lang/zh_TW.cpp +++ b/plugins/builtin/source/lang/zh_TW.cpp @@ -684,6 +684,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.general", "一般" }, { "hex.builtin.setting.general.show_tips", "啟動時顯示提示" }, { "hex.builtin.setting.general.auto_load_patterns", "自動載入支援的模式" }, + // { "hex.builtin.setting.general.sync_pattern_source", "Sync pattern source code between providers" }, { "hex.builtin.setting.interface", "介面" }, { "hex.builtin.setting.interface.color", "顏色主題" }, { "hex.builtin.setting.interface.color.system", "系統" },