diff --git a/plugins/builtin/source/content/views/view_hexeditor.cpp b/plugins/builtin/source/content/views/view_hexeditor.cpp index 11c2c26ee..5088e3364 100644 --- a/plugins/builtin/source/content/views/view_hexeditor.cpp +++ b/plugins/builtin/source/content/views/view_hexeditor.cpp @@ -4,14 +4,15 @@ #include #include #include -#include - -#include "content/providers/file_provider.hpp" - +#include #include #include #include +#include +#include + + #include #include @@ -36,6 +37,10 @@ namespace hex::plugin::builtin { this->m_searchStringBuffer.resize(0xFFF, 0x00); this->m_searchHexBuffer.resize(0xFFF, 0x00); + ContentRegistry::FileHandler::add({ ".hexproj" }, [](const auto &path) { + return ProjectFile::load(path.string()); + }); + this->m_memoryEditor.ReadFn = [](const ImU8 *data, size_t off) -> ImU8 { auto provider = ImHexApi::Provider::get(); if (!provider->isAvailable() || !provider->isReadable()) @@ -346,7 +351,7 @@ namespace hex::plugin::builtin { if (ImGui::BeginMenu("hex.builtin.view.hexeditor.menu.file.open_recent"_lang, !SharedData::recentFilePaths.empty())) { for (auto &path : SharedData::recentFilePaths) { - if (ImGui::MenuItem(std::filesystem::path(path).filename().string().c_str())) { + if (ImGui::MenuItem(fs::path(path).filename().string().c_str())) { EventManager::post(path); } } diff --git a/plugins/builtin/source/content/views/view_information.cpp b/plugins/builtin/source/content/views/view_information.cpp index 22e04c4b8..ac6710af9 100644 --- a/plugins/builtin/source/content/views/view_information.cpp +++ b/plugins/builtin/source/content/views/view_information.cpp @@ -42,6 +42,18 @@ namespace hex::plugin::builtin { EventManager::subscribe(this, [this]{ this->m_dataValid = false; }); + + ContentRegistry::FileHandler::add({ ".mgc" }, [](const auto &path) { + for (auto &destPath : hex::getPath(ImHexPath::Magic)) { + std::error_code error; + if (fs::copy_file(path, destPath / path.filename(), fs::copy_options::overwrite_existing, error)) { + View::showMessagePopup("hex.builtin.view.information.magic_db_added"_lang); + return true; + } + } + + return false; + }); } ViewInformation::~ViewInformation() { diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 062bd7df9..d61091528 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -183,6 +183,17 @@ namespace hex::plugin::builtin { }); } + + ContentRegistry::FileHandler::add({ ".hexpat", ".pat" }, [](const fs::path &path) -> bool{ + File file(path.string(), File::Mode::Read); + + if (file.isValid()) { + EventManager::post(file.readString()); + return true; + } else { + return false; + } + }); } ViewPatternEditor::~ViewPatternEditor() { diff --git a/plugins/builtin/source/content/views/view_yara.cpp b/plugins/builtin/source/content/views/view_yara.cpp index 050c8f33f..207344c53 100644 --- a/plugins/builtin/source/content/views/view_yara.cpp +++ b/plugins/builtin/source/content/views/view_yara.cpp @@ -18,6 +18,18 @@ namespace hex::plugin::builtin { yr_initialize(); this->reloadRules(); + + ContentRegistry::FileHandler::add({ ".yar" }, [](const auto &path) { + for (auto &destPath : hex::getPath(ImHexPath::Yara)) { + std::error_code error; + if (fs::copy_file(path, destPath / path.filename(), fs::copy_options::overwrite_existing, error)) { + View::showMessagePopup("hex.builtin.view.yara.rule_added"_lang); + return true; + } + } + + return false; + }); } ViewYara::~ViewYara() { diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index 85bafdc99..94db8ec99 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -86,8 +86,8 @@ namespace hex::plugin::builtin { { "hex.common.open", "Öffnen" }, { "hex.common.browse", "Druchsuchen..." }, - { "hex.message.yara_rule_added", "Yara Regel hinzugefügt!" }, - { "hex.message.magic_db_added", "Magic Datenbank hinzugefügt!" }, + { "hex.message.file_handler_failed", "Datei konnte nicht mit registriertem Dateihandler geöffnet werden." }, + /* Builtin plugin features */ @@ -260,6 +260,7 @@ namespace hex::plugin::builtin { { "hex.builtin.view.information.file_entropy", "Dateientropie" }, { "hex.builtin.view.information.highest_entropy", "Höchste Blockentropie" }, { "hex.builtin.view.information.encrypted", "Diese Daten sind vermutlich verschlüsselt oder komprimiert!" }, + { "hex.builtin.view.information.magic_db_added", "Magic Datenbank hinzugefügt!" }, { "hex.builtin.view.patches.name", "Patches" }, { "hex.builtin.view.patches.offset", "Offset" }, @@ -321,6 +322,7 @@ namespace hex::plugin::builtin { { "hex.builtin.view.yara.matches.variable", "Variable" }, { "hex.builtin.view.yara.whole_data", "Gesammte Daten Übereinstimmung!" }, { "hex.builtin.view.yara.no_rules", "Keine Yara Regeln gefunden. Platziere sie in ImHex's 'yara' Ordner" }, + { "hex.builtin.view.yara.rule_added", "Yara Regel hinzugefügt!" }, { "hex.builtin.view.constants.name", "Konstanten" }, { "hex.builtin.view.constants.row.category", "Kategorie" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index f762ad7bc..8890032c0 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -86,8 +86,7 @@ namespace hex::plugin::builtin { { "hex.common.open", "Open" }, { "hex.common.browse", "Browse..." }, - { "hex.message.yara_rule_added", "Yara rule added!" }, - { "hex.message.magic_db_added", "Magic database added!" }, + { "hex.message.file_handler_failed", "Failed to open file with registered file handler." }, /* Builtin plugin features */ @@ -263,6 +262,7 @@ namespace hex::plugin::builtin { { "hex.builtin.view.information.file_entropy", "File entropy" }, { "hex.builtin.view.information.highest_entropy", "Highest entropy block" }, { "hex.builtin.view.information.encrypted", "This data is most likely encrypted or compressed!" }, + { "hex.builtin.view.information.magic_db_added", "Magic database added!" }, { "hex.builtin.view.patches.name", "Patches" }, { "hex.builtin.view.patches.offset", "Offset" }, @@ -324,6 +324,8 @@ namespace hex::plugin::builtin { { "hex.builtin.view.yara.matches.variable", "Variable" }, { "hex.builtin.view.yara.whole_data", "Whole file matches!" }, { "hex.builtin.view.yara.no_rules", "No YARA rules found. Put them in ImHex's 'yara' folder" }, + { "hex.builtin.view.yara.rule_added", "Yara rule added!" }, + { "hex.builtin.view.constants.name", "Constants" }, { "hex.builtin.view.constants.row.category", "Category" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index e9e2439a8..cbbf3cb3a 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -85,8 +85,7 @@ namespace hex::plugin::builtin { //{ "hex.common.open", "Open" }, //{ "hex.common.browse", "Browse..." }, - //{ "hex.message.yara_rule_added", "Yara rule added!" }, - //{ "hex.message.magic_db_added", "Magic database added!" }, + //{ "hex.message.file_handler_failed", "Failed to open file with registered file handler." }, /* Builtin plugin features */ @@ -260,6 +259,8 @@ namespace hex::plugin::builtin { { "hex.builtin.view.information.file_entropy", "Entropia dei File" }, { "hex.builtin.view.information.highest_entropy", "Highest entropy block" }, { "hex.builtin.view.information.encrypted", "Questi dati sono probabilmente codificati o compressi!" }, + //{ "hex.builtin.view.information.magic_db_added", "Magic database added!" }, + { "hex.builtin.view.patches.name", "Patches" }, { "hex.builtin.view.patches.offset", "Offset" }, @@ -319,6 +320,7 @@ namespace hex::plugin::builtin { //{ "hex.builtin.view.yara.matches.variable", "Variable" }, { "hex.builtin.view.yara.whole_data", "Tutti i file combaciano!" }, { "hex.builtin.view.yara.no_rules", "Nessuna regola di YARA. Aggiungile in nella cartella 'yara' di 'ImHex'" }, + //{ "hex.builtin.view.yara.rule_added", "Yara rule added!" }, { "hex.builtin.view.constants.name", "Costanti" }, { "hex.builtin.view.constants.row.category", "Categoria" }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index dc331aff1..945605ef7 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -86,8 +86,7 @@ namespace hex::plugin::builtin { { "hex.common.open", "打开" }, { "hex.common.browse", "浏览..." }, - { "hex.message.yara_rule_added", "Yara规则已添加!" }, - { "hex.message.magic_db_added", "魔术数据库已添加!" }, + //{ "hex.message.file_handler_failed", "Failed to open file with registered file handler." }, /* Builtin plugin features */ @@ -260,6 +259,7 @@ namespace hex::plugin::builtin { { "hex.builtin.view.information.file_entropy", "文件熵" }, { "hex.builtin.view.information.highest_entropy", "最高熵" }, { "hex.builtin.view.information.encrypted", "此数据似乎经过了加密或压缩!" }, + { "hex.builtin.view.information.magic_db_added", "魔术数据库已添加!" }, { "hex.builtin.view.patches.name", "补丁" }, { "hex.builtin.view.patches.offset", "偏移" }, @@ -319,6 +319,7 @@ namespace hex::plugin::builtin { //{ "hex.builtin.view.yara.matches.variable", "Variable" }, { "hex.builtin.view.yara.whole_data", "全文件匹配!" }, { "hex.builtin.view.yara.no_rules", "没有找到YARA规则。请将规则放到ImHex的'yara'目录下。" }, + //{ "hex.builtin.view.yara.rule_added", "Yara规则已添加!" }, { "hex.builtin.view.constants.name", "常量" }, { "hex.builtin.view.constants.row.category", "分类" }, diff --git a/plugins/libimhex/include/hex/api/content_registry.hpp b/plugins/libimhex/include/hex/api/content_registry.hpp index 2aa65c25b..816eca35f 100644 --- a/plugins/libimhex/include/hex/api/content_registry.hpp +++ b/plugins/libimhex/include/hex/api/content_registry.hpp @@ -273,6 +273,24 @@ namespace hex { std::vector& getEntries(); } + + namespace FileHandler { + + namespace impl { + + using Callback = std::function; + struct Entry { + std::vector extensions; + Callback callback; + }; + + } + + void add(const std::vector &extensions, const impl::Callback &callback); + + std::vector& getEntries(); + + } }; } \ No newline at end of file diff --git a/plugins/libimhex/include/hex/helpers/shared_data.hpp b/plugins/libimhex/include/hex/helpers/shared_data.hpp index 43f937570..88be88ee2 100644 --- a/plugins/libimhex/include/hex/helpers/shared_data.hpp +++ b/plugins/libimhex/include/hex/helpers/shared_data.hpp @@ -92,6 +92,7 @@ namespace hex { static u32 dataProcessorAttrIdCounter; static std::vector dataFormatters; + static std::vector fileHandlers; static std::list recentFilePaths; diff --git a/plugins/libimhex/source/api/content_registry.cpp b/plugins/libimhex/source/api/content_registry.cpp index 1347226e0..f8e079ff3 100644 --- a/plugins/libimhex/source/api/content_registry.cpp +++ b/plugins/libimhex/source/api/content_registry.cpp @@ -295,13 +295,27 @@ namespace hex { /* Data Formatters */ void ContentRegistry::DataFormatter::add(const std::string &unlocalizedName, const impl::Callback &callback) { - ContentRegistry::DataFormatter::getEntries().push_back({ - unlocalizedName, - callback - }); + log::info("Registered new data formatter: {}", unlocalizedName); + + ContentRegistry::DataFormatter::getEntries().push_back({ unlocalizedName, callback }); } std::vector &ContentRegistry::DataFormatter::getEntries() { return SharedData::dataFormatters; } + + + + /* File Handlers */ + + void ContentRegistry::FileHandler::add(const std::vector &extensions, const impl::Callback &callback) { + for (const auto &extension : extensions) + log::info("Registered new data handler for extensions: {}", extension); + + ContentRegistry::FileHandler::getEntries().push_back({ extensions, callback }); + } + + std::vector &ContentRegistry::FileHandler::getEntries() { + return SharedData::fileHandlers; + } } \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp index 445bdc614..b1d072355 100644 --- a/plugins/libimhex/source/helpers/shared_data.cpp +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -42,6 +42,7 @@ namespace hex { u32 SharedData::dataProcessorAttrIdCounter = 1; std::vector SharedData::dataFormatters; + std::vector SharedData::fileHandlers; std::list SharedData::recentFilePaths; diff --git a/source/init/tasks.cpp b/source/init/tasks.cpp index ddf464098..e32e0a7c2 100644 --- a/source/init/tasks.cpp +++ b/source/init/tasks.cpp @@ -201,6 +201,8 @@ namespace hex::init { SharedData::recentFilePaths.clear(); + SharedData::dataFormatters.clear(); + SharedData::fileHandlers.clear(); SharedData::clearVariables();