1
0
mirror of synced 2025-02-20 04:01:01 +01:00

feat: Added bookmark import/export function

This commit is contained in:
WerWolv 2022-08-14 14:45:32 +02:00
parent 78785ddc3c
commit 49d47a0eed
10 changed files with 113 additions and 58 deletions

View File

@ -14,6 +14,11 @@ namespace hex::plugin::builtin {
void drawContent() override;
private:
static bool importBookmarks(prv::Provider *provider, const nlohmann::json &json);
static bool exportBookmarks(prv::Provider *provider, nlohmann::json &json);
void registerMenuItems();
private:
std::string m_currFilter;
};

View File

@ -251,15 +251,6 @@ namespace hex::plugin::builtin {
provider->redo();
});
ContentRegistry::Interface::addMenuItem("hex.builtin.menu.edit", 1050, [&] {
bool providerValid = ImHexApi::Provider::isValid();
auto selection = ImHexApi::HexEditor::getSelection();
if (ImGui::MenuItem("hex.builtin.menu.edit.bookmark"_lang, nullptr, false, selection.has_value() && providerValid)) {
ImHexApi::Bookmarks::add(selection->getStartAddress(), selection->getSize(), {}, {});
}
});
}
static void createViewMenu() {

View File

@ -1,8 +1,10 @@
#include "content/views/view_bookmarks.hpp"
#include <hex/api/content_registry.hpp>
#include <hex/api/project_file_manager.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/file.hpp>
#include <nlohmann/json.hpp>
#include <cstring>
@ -107,55 +109,20 @@ namespace hex::plugin::builtin {
return true;
auto data = nlohmann::json::parse(fileContent.begin(), fileContent.end());
if (!data.contains("bookmarks"))
return false;
auto &bookmarks = ProviderExtraData::get(provider).bookmarks;
bookmarks.clear();
for (const auto &bookmark : data["bookmarks"]) {
if (!bookmark.contains("name") || !bookmark.contains("comment") || !bookmark.contains("color") || !bookmark.contains("region") || !bookmark.contains("locked"))
continue;
const auto &region = bookmark["region"];
if (!region.contains("address") || !region.contains("size"))
continue;
bookmarks.push_back({
.region = { region["address"], region["size"] },
.name = bookmark["name"],
.comment = bookmark["comment"],
.color = bookmark["color"],
.locked = bookmark["locked"]
});
}
return true;
ProviderExtraData::get(provider).bookmarks.clear();
return ViewBookmarks::importBookmarks(provider, data);
},
.store = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
nlohmann::json data;
data["bookmarks"] = nlohmann::json::array();
size_t index = 0;
for (const auto &bookmark : ProviderExtraData::get(provider).bookmarks) {
data["bookmarks"][index] = {
{ "name", bookmark.name },
{ "comment", bookmark.comment },
{ "color", bookmark.color },
{ "region", {
{ "address", bookmark.region.address },
{ "size", bookmark.region.size }
}
},
{ "locked", bookmark.locked }
};
index++;
}
bool result = ViewBookmarks::exportBookmarks(provider, data);
tar.write(basePath, data.dump(4));
return true;
return result;
}
});
this->registerMenuItems();
}
ViewBookmarks::~ViewBookmarks() {
@ -300,4 +267,82 @@ namespace hex::plugin::builtin {
ImGui::End();
}
bool ViewBookmarks::importBookmarks(prv::Provider *provider, const nlohmann::json &json) {
if (!json.contains("bookmarks"))
return false;
auto &bookmarks = ProviderExtraData::get(provider).bookmarks;
for (const auto &bookmark : json["bookmarks"]) {
if (!bookmark.contains("name") || !bookmark.contains("comment") || !bookmark.contains("color") || !bookmark.contains("region") || !bookmark.contains("locked"))
continue;
const auto &region = bookmark["region"];
if (!region.contains("address") || !region.contains("size"))
continue;
bookmarks.push_back({
.region = { region["address"], region["size"] },
.name = bookmark["name"],
.comment = bookmark["comment"],
.color = bookmark["color"],
.locked = bookmark["locked"]
});
}
return true;
}
bool ViewBookmarks::exportBookmarks(prv::Provider *provider, nlohmann::json &json) {
json["bookmarks"] = nlohmann::json::array();
size_t index = 0;
for (const auto &bookmark : ProviderExtraData::get(provider).bookmarks) {
json["bookmarks"][index] = {
{ "name", bookmark.name },
{ "comment", bookmark.comment },
{ "color", bookmark.color },
{ "region", {
{ "address", bookmark.region.address },
{ "size", bookmark.region.size }
}
},
{ "locked", bookmark.locked }
};
index++;
}
return true;
}
void ViewBookmarks::registerMenuItems() {
ContentRegistry::Interface::addMenuItem("hex.builtin.menu.edit", 1050, [&] {
bool providerValid = ImHexApi::Provider::isValid();
auto selection = ImHexApi::HexEditor::getSelection();
if (ImGui::MenuItem("hex.builtin.menu.edit.bookmark.create"_lang, nullptr, false, selection.has_value() && providerValid)) {
ImHexApi::Bookmarks::add(selection->getStartAddress(), selection->getSize(), {}, {});
}
});
ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 4000, [&] {
bool providerValid = ImHexApi::Provider::isValid();
auto selection = ImHexApi::HexEditor::getSelection();
if (ImGui::MenuItem("hex.builtin.menu.file.bookmark.import"_lang, nullptr, false, selection.has_value() && providerValid)) {
fs::openFileBrowser(fs::DialogMode::Open, { { "Bookmarks File", "hexbm"} }, [&](const std::fs::path &path) {
try {
importBookmarks(ImHexApi::Provider::get(), nlohmann::json::parse(fs::File(path, fs::File::Mode::Read).readString()));
} catch (...) { }
});
}
if (ImGui::MenuItem("hex.builtin.menu.file.bookmark.export"_lang, nullptr, false, selection.has_value() && providerValid)) {
fs::openFileBrowser(fs::DialogMode::Save, { { "Bookmarks File", "hexbm"} }, [&](const std::fs::path &path) {
nlohmann::json json;
exportBookmarks(ImHexApi::Provider::get(), json);
fs::File(path, fs::File::Mode::Create).write(json.dump(4));
});
}
});
}
}

View File

@ -122,11 +122,13 @@ namespace hex::plugin::builtin {
{ "hex.builtin.menu.file.export.ips", "IPS Patch" },
{ "hex.builtin.menu.file.export.ips32", "IPS32 Patch" },
{ "hex.builtin.menu.file.export.base64.popup.export_error", "File is not in a valid Base64 format!" },
{ "hex.builtin.menu.file.bookmark.import", "Lesezeichen importieren" },
{ "hex.builtin.menu.file.bookmark.export", "Lesezeichen exportieren" },
{ "hex.builtin.menu.edit", "Bearbeiten" },
{ "hex.builtin.menu.edit.undo", "Rückgängig" },
{ "hex.builtin.menu.edit.redo", "Wiederholen" },
{ "hex.builtin.menu.edit.bookmark", "Lesezeichen erstellen" },
{ "hex.builtin.menu.edit.bookmark.create", "Lesezeichen erstellen" },
{ "hex.builtin.menu.view", "Ansicht" },
{ "hex.builtin.menu.layout", "Layout" },

View File

@ -124,12 +124,14 @@ namespace hex::plugin::builtin {
{ "hex.builtin.menu.file.export.ips", "IPS Patch" },
{ "hex.builtin.menu.file.export.ips32", "IPS32 Patch" },
{ "hex.builtin.menu.file.export.base64.popup.export_error", "File is not in a valid Base64 format!" },
{ "hex.builtin.menu.file.export.popup.create", "Cannot export data. Failed to create file!" },
{ "hex.builtin.menu.file.export.popup.create", "Cannot export data. Failed to create file!" },
{ "hex.builtin.menu.file.bookmark.import", "Import bookmarks" },
{ "hex.builtin.menu.file.bookmark.export", "Export bookmarks" },
{ "hex.builtin.menu.edit", "Edit" },
{ "hex.builtin.menu.edit.undo", "Undo" },
{ "hex.builtin.menu.edit.redo", "Redo" },
{ "hex.builtin.menu.edit.bookmark", "Create bookmark" },
{ "hex.builtin.menu.edit.bookmark.create", "Create bookmark" },
{ "hex.builtin.menu.view", "View" },
{ "hex.builtin.menu.layout", "Layout" },

View File

@ -124,11 +124,13 @@ namespace hex::plugin::builtin {
{ "hex.builtin.menu.file.export.ips32", "IPS32 Patch" },
//{ "hex.builtin.menu.file.export.base64.popup.export_error", "File is not in a valid Base64 format!" },
//{ "hex.builtin.menu.file.export.popup.create", "Cannot export data. Failed to create file!" },
//{ "hex.builtin.menu.file.bookmark.import", "Import bookmarks" },
//{ "hex.builtin.menu.file.bookmark.export", "Export bookmarks" },
{ "hex.builtin.menu.edit", "Modifica" },
{ "hex.builtin.menu.edit.undo", "Annulla" },
{ "hex.builtin.menu.edit.redo", "Ripeti" },
{ "hex.builtin.menu.edit.bookmark", "Crea segnalibro" },
{ "hex.builtin.menu.edit.bookmark.create", "Crea segnalibro" },
{ "hex.builtin.menu.view", "Vista" },
{ "hex.builtin.menu.layout", "Layout" },

View File

@ -124,11 +124,13 @@ namespace hex::plugin::builtin {
{ "hex.builtin.menu.file.export.ips32", "IPS32パッチ" },
{ "hex.builtin.menu.file.export.base64.popup.export_error", "有効なBase64形式ではありません。" },
{ "hex.builtin.menu.file.export.popup.create", "データをエクスポートできません。\nファイルの作成に失敗しました。" },
//{ "hex.builtin.menu.file.bookmark.import", "Import bookmarks" },
//{ "hex.builtin.menu.file.bookmark.export", "Export bookmarks" },
{ "hex.builtin.menu.edit", "編集" },
{ "hex.builtin.menu.edit.undo", "元に戻す" },
{ "hex.builtin.menu.edit.redo", "やり直す" },
{ "hex.builtin.menu.edit.bookmark", "ブックマークを作成" },
{ "hex.builtin.menu.edit.bookmark.create", "ブックマークを作成" },
{ "hex.builtin.menu.view", "表示" },
{ "hex.builtin.menu.layout", "レイアウト" },

View File

@ -123,11 +123,13 @@ namespace hex::plugin::builtin {
{ "hex.builtin.menu.file.export.ips32", "IPS32 Patch" },
{ "hex.builtin.menu.file.export.base64.popup.export_error", "Esse arquivo não é baseado em um formato Base64 valido!" },
{ "hex.builtin.menu.file.export.popup.create", "Não é possível exportar os dados. Falha ao criar arquivo!" },
//{ "hex.builtin.menu.file.bookmark.import", "Import bookmarks" },
//{ "hex.builtin.menu.file.bookmark.export", "Export bookmarks" },
{ "hex.builtin.menu.edit", "Editar" },
{ "hex.builtin.menu.edit.undo", "Desfazer" },
{ "hex.builtin.menu.edit.redo", "Refazer" },
{ "hex.builtin.menu.edit.bookmark", "Criar Marcador" },
{ "hex.builtin.menu.edit.bookmark.create", "Criar Marcador" },
{ "hex.builtin.menu.view", "Exibir" },
{ "hex.builtin.menu.layout", "Layout" },

View File

@ -123,11 +123,13 @@ namespace hex::plugin::builtin {
{ "hex.builtin.menu.file.export.ips32", "IPS32 补丁" },
{ "hex.builtin.menu.file.export.base64.popup.export_error", "文件不是有效的 Base64 格式!" },
{ "hex.builtin.menu.file.export.popup.create", "无法导出文件。文件创建失败!" },
//{ "hex.builtin.menu.file.bookmark.import", "Import bookmarks" },
//{ "hex.builtin.menu.file.bookmark.export", "Export bookmarks" },
{ "hex.builtin.menu.edit", "编辑" },
{ "hex.builtin.menu.edit.undo", "撤销" },
{ "hex.builtin.menu.edit.redo", "重做" },
{ "hex.builtin.menu.edit.bookmark", "添加书签" },
{ "hex.builtin.menu.edit.bookmark.create", "添加书签" },
{ "hex.builtin.menu.view", "视图" },
{ "hex.builtin.menu.layout", "布局" },

View File

@ -123,11 +123,13 @@ namespace hex::plugin::builtin {
{ "hex.builtin.menu.file.export.ips32", "IPS32 修補檔案" },
{ "hex.builtin.menu.file.export.base64.popup.export_error", "檔案並非有效的 Base64 格式!" },
{ "hex.builtin.menu.file.export.popup.create", "無法匯出資料。無法建立檔案!" },
//{ "hex.builtin.menu.file.bookmark.import", "Import bookmarks" },
//{ "hex.builtin.menu.file.bookmark.export", "Export bookmarks" },
{ "hex.builtin.menu.edit", "編輯" },
{ "hex.builtin.menu.edit.undo", "復原" },
{ "hex.builtin.menu.edit.redo", "取消復原" },
{ "hex.builtin.menu.edit.bookmark", "建立書籤" },
{ "hex.builtin.menu.edit.bookmark.create", "建立書籤" },
{ "hex.builtin.menu.view", "檢視" },
{ "hex.builtin.menu.layout", "版面配置" },