1
0
mirror of synced 2024-09-25 03:58:27 +02:00

api: Moved file extension handling to plugin and added API

This commit is contained in:
WerWolv 2022-01-13 14:34:19 +01:00
parent c88d428fb5
commit 54785b29dd
13 changed files with 100 additions and 17 deletions

View File

@ -4,14 +4,15 @@
#include <hex/providers/provider.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/helpers/file.hpp>
#include <hex/pattern_language/pattern_data.hpp>
#include "content/providers/file_provider.hpp"
#include <hex/helpers/paths.hpp>
#include <hex/helpers/patches.hpp>
#include <hex/helpers/project_file_handler.hpp>
#include <hex/helpers/loader_script_handler.hpp>
#include <hex/pattern_language/pattern_data.hpp>
#include <content/providers/file_provider.hpp>
#include <GLFW/glfw3.h>
#include <nlohmann/json.hpp>
@ -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<RequestOpenFile>(path);
}
}

View File

@ -42,6 +42,18 @@ namespace hex::plugin::builtin {
EventManager::subscribe<EventFileUnloaded>(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() {

View File

@ -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<RequestSetPatternLanguageCode>(file.readString());
return true;
} else {
return false;
}
});
}
ViewPatternEditor::~ViewPatternEditor() {

View File

@ -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() {

View File

@ -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" },

View File

@ -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" },

View File

@ -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" },

View File

@ -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", "分类" },

View File

@ -273,6 +273,24 @@ namespace hex {
std::vector<impl::Entry>& getEntries();
}
namespace FileHandler {
namespace impl {
using Callback = std::function<bool(fs::path)>;
struct Entry {
std::vector<std::string> extensions;
Callback callback;
};
}
void add(const std::vector<std::string> &extensions, const impl::Callback &callback);
std::vector<impl::Entry>& getEntries();
}
};
}

View File

@ -92,6 +92,7 @@ namespace hex {
static u32 dataProcessorAttrIdCounter;
static std::vector<ContentRegistry::DataFormatter::impl::Entry> dataFormatters;
static std::vector<ContentRegistry::FileHandler::impl::Entry> fileHandlers;
static std::list<std::string> recentFilePaths;

View File

@ -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::impl::Entry> &ContentRegistry::DataFormatter::getEntries() {
return SharedData::dataFormatters;
}
/* File Handlers */
void ContentRegistry::FileHandler::add(const std::vector<std::string> &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::impl::Entry> &ContentRegistry::FileHandler::getEntries() {
return SharedData::fileHandlers;
}
}

View File

@ -42,6 +42,7 @@ namespace hex {
u32 SharedData::dataProcessorAttrIdCounter = 1;
std::vector<ContentRegistry::DataFormatter::impl::Entry> SharedData::dataFormatters;
std::vector<ContentRegistry::FileHandler::impl::Entry> SharedData::fileHandlers;
std::list<std::string> SharedData::recentFilePaths;

View File

@ -201,6 +201,8 @@ namespace hex::init {
SharedData::recentFilePaths.clear();
SharedData::dataFormatters.clear();
SharedData::fileHandlers.clear();
SharedData::clearVariables();