2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_bookmarks.hpp"
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2023-03-16 14:40:26 +01:00
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
|
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
#include <content/providers/view_provider.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2020-11-28 00:33:26 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
2022-09-18 16:22:08 +02:00
|
|
|
#include <content/helpers/provider_extra_data.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
#include <wolv/utils/guards.hpp>
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ViewBookmarks::ViewBookmarks() : View("hex.builtin.view.bookmarks.name") {
|
2022-08-08 21:23:52 +02:00
|
|
|
EventManager::subscribe<RequestAddBookmark>(this, [](Region region, std::string name, std::string comment, color_t color) {
|
2022-02-01 18:09:40 +01:00
|
|
|
if (name.empty()) {
|
|
|
|
name = hex::format("hex.builtin.view.bookmarks.default_title"_lang, region.address, region.address + region.size - 1);
|
2021-01-14 17:01:44 +01:00
|
|
|
}
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
if (color == 0x00)
|
|
|
|
color = ImGui::GetColorU32(ImGuiCol_Header);
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
ProviderExtraData::getCurrent().bookmarks.push_back({
|
|
|
|
region,
|
2022-02-01 18:09:40 +01:00
|
|
|
name,
|
|
|
|
std::move(comment),
|
|
|
|
color,
|
2022-05-27 20:42:07 +02:00
|
|
|
false
|
|
|
|
});
|
2021-01-13 14:11:23 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
ImHexApi::Provider::markDirty();
|
2023-04-06 17:36:28 +02:00
|
|
|
|
|
|
|
EventManager::post<EventBookmarkCreated>(ProviderExtraData::getCurrent().bookmarks.back());
|
2020-11-30 00:03:12 +01:00
|
|
|
});
|
|
|
|
|
2022-09-28 15:01:43 +02:00
|
|
|
ImHexApi::HexEditor::addBackgroundHighlightingProvider([](u64 address, const u8* data, size_t size, bool) -> std::optional<color_t> {
|
2022-05-27 20:42:07 +02:00
|
|
|
hex::unused(data);
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
for (const auto &bookmark : ProviderExtraData::getCurrent().bookmarks) {
|
2022-05-27 20:42:07 +02:00
|
|
|
if (Region { address, size }.isWithin(bookmark.region))
|
|
|
|
return bookmark.color;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
});
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
ImHexApi::HexEditor::addTooltipProvider([](u64 address, const u8 *data, size_t size) {
|
2022-05-27 20:42:07 +02:00
|
|
|
hex::unused(data);
|
2022-08-08 21:23:52 +02:00
|
|
|
for (const auto &bookmark : ProviderExtraData::getCurrent().bookmarks) {
|
2022-05-27 20:42:07 +02:00
|
|
|
if (!Region { address, size }.isWithin(bookmark.region))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ImGui::BeginTooltip();
|
|
|
|
|
2022-07-17 13:12:28 +02:00
|
|
|
ImGui::PushID(&bookmark);
|
|
|
|
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
{
|
|
|
|
ImGui::ColorButton("##color", ImColor(bookmark.color));
|
|
|
|
ImGui::SameLine(0, 10);
|
2023-03-16 16:48:15 +01:00
|
|
|
ImGui::TextFormatted("{} ", bookmark.name);
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
if (ImGui::GetIO().KeyShift) {
|
|
|
|
ImGui::Indent();
|
2022-07-17 13:12:28 +02:00
|
|
|
if (ImGui::BeginTable("##extra_info", 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::TextFormatted("{}: ", "hex.builtin.common.region"_lang.get());
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TableNextColumn();
|
2023-03-16 16:48:15 +01:00
|
|
|
ImGui::TextFormatted("[ 0x{:08X} - 0x{:08X} ] ", bookmark.region.getStartAddress(), bookmark.region.getEndAddress());
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
if (!bookmark.comment.empty() && bookmark.comment[0] != '\x00') {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::TextFormatted("{}: ", "hex.builtin.view.bookmarks.header.comment"_lang.get());
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TableNextColumn();
|
2023-03-16 13:35:29 +01:00
|
|
|
ImGui::PushTextWrapPos(ImGui::CalcTextSize("X").x * 40);
|
|
|
|
ImGui::TextFormattedWrapped("{}", bookmark.comment);
|
|
|
|
ImGui::PopTextWrapPos();
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::Unindent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_TableRowBg, bookmark.color);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_TableRowBgAlt, bookmark.color);
|
|
|
|
ImGui::EndTable();
|
|
|
|
ImGui::PopStyleColor(2);
|
|
|
|
}
|
2022-07-17 13:12:28 +02:00
|
|
|
ImGui::PopID();
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
|
|
|
});
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
ProjectFile::registerPerProviderHandler({
|
|
|
|
.basePath = "bookmarks.json",
|
2022-09-20 15:33:36 +02:00
|
|
|
.required = false,
|
2022-08-08 21:23:52 +02:00
|
|
|
.load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
|
2023-03-23 11:23:07 +01:00
|
|
|
auto fileContent = tar.readString(basePath);
|
2022-08-08 21:23:52 +02:00
|
|
|
if (fileContent.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto data = nlohmann::json::parse(fileContent.begin(), fileContent.end());
|
2022-08-14 14:45:32 +02:00
|
|
|
ProviderExtraData::get(provider).bookmarks.clear();
|
|
|
|
return ViewBookmarks::importBookmarks(provider, data);
|
2022-08-08 21:23:52 +02:00
|
|
|
},
|
|
|
|
.store = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
|
|
|
|
nlohmann::json data;
|
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
bool result = ViewBookmarks::exportBookmarks(provider, data);
|
2023-03-23 11:23:07 +01:00
|
|
|
tar.writeString(basePath, data.dump(4));
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
return result;
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
});
|
2022-08-14 14:45:32 +02:00
|
|
|
|
|
|
|
this->registerMenuItems();
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewBookmarks::~ViewBookmarks() {
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::unsubscribe<RequestAddBookmark>(this);
|
2022-07-30 22:01:49 +02:00
|
|
|
EventManager::unsubscribe<EventProviderDeleted>(this);
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-07 10:53:08 +02:00
|
|
|
static void drawColorPopup(ImColor &color) {
|
|
|
|
static auto Palette = []{
|
|
|
|
constexpr static auto ColorCount = 36;
|
|
|
|
std::array<ImColor, ColorCount> result = { 0 };
|
|
|
|
|
|
|
|
u32 counter = 0;
|
|
|
|
for (auto &color : result) {
|
|
|
|
ImGui::ColorConvertHSVtoRGB(float(counter) / float(ColorCount - 1), 0.8F, 0.8F, color.Value.x, color.Value.y, color.Value.z);
|
|
|
|
color.Value.w = 0.7f;
|
|
|
|
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}();
|
|
|
|
|
|
|
|
ImGui::ColorPicker4("##picker", (float*)&color, ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoSmallPreview);
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
int id = 0;
|
|
|
|
for (const auto &paletteColor : Palette) {
|
|
|
|
ImGui::PushID(id);
|
|
|
|
if ((id % 9) != 0)
|
|
|
|
ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
|
|
|
|
|
|
|
|
constexpr static ImGuiColorEditFlags flags = ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoDragDrop;
|
|
|
|
if (ImGui::ColorButton("##palette", paletteColor.Value, flags, ImVec2(20, 20))) {
|
|
|
|
color = paletteColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::PopID();
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewBookmarks::drawContent() {
|
2021-12-07 22:47:41 +01:00
|
|
|
if (ImGui::Begin(View::toWindowName("hex.builtin.view.bookmarks.name").c_str(), &this->getWindowOpenState())) {
|
2023-03-16 14:40:26 +01:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
2022-07-23 20:38:38 +02:00
|
|
|
|
2022-08-03 23:32:34 +02:00
|
|
|
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::InputTextWithHint("##filter", "hex.builtin.common.filter"_lang, this->m_currFilter);
|
2022-07-23 20:38:38 +02:00
|
|
|
ImGui::PopItemWidth();
|
2022-07-23 20:46:20 +02:00
|
|
|
|
2022-07-23 20:38:38 +02:00
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
if (ImGui::BeginChild("##bookmarks")) {
|
2022-08-08 21:23:52 +02:00
|
|
|
auto &bookmarks = ProviderExtraData::getCurrent().bookmarks;
|
|
|
|
if (bookmarks.empty()) {
|
2022-01-24 00:46:19 +01:00
|
|
|
ImGui::TextFormattedCentered("hex.builtin.view.bookmarks.no_bookmarks"_lang);
|
2021-01-08 19:34:29 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 21:40:31 +02:00
|
|
|
int id = 1;
|
2022-08-08 21:23:52 +02:00
|
|
|
auto bookmarkToRemove = bookmarks.end();
|
|
|
|
for (auto iter = bookmarks.begin(); iter != bookmarks.end(); iter++) {
|
2022-05-27 20:42:07 +02:00
|
|
|
auto &[region, name, comment, color, locked] = *iter;
|
2021-01-13 14:11:23 +01:00
|
|
|
|
2022-07-23 20:38:38 +02:00
|
|
|
if (!this->m_currFilter.empty()) {
|
|
|
|
if (!name.contains(this->m_currFilter) && !comment.contains(this->m_currFilter))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:11:23 +01:00
|
|
|
auto headerColor = ImColor(color);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto hoverColor = ImColor(color);
|
2021-01-13 14:11:23 +01:00
|
|
|
hoverColor.Value.w *= 1.3F;
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2021-01-13 16:55:42 +01:00
|
|
|
ImGui::PushID(id);
|
2021-01-13 14:11:23 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_Header, color);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_HeaderActive, color);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, u32(hoverColor));
|
2022-03-26 16:55:48 +01:00
|
|
|
|
2022-09-28 21:40:31 +02:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
ImGui::PopID();
|
|
|
|
ImGui::PopStyleColor(3);
|
|
|
|
id++;
|
|
|
|
};
|
|
|
|
|
2022-03-26 16:55:48 +01:00
|
|
|
bool open = true;
|
2023-03-16 14:40:26 +01:00
|
|
|
if (!ImGui::CollapsingHeader(hex::format("{}###bookmark", name).c_str(), locked ? nullptr : &open)) {
|
2022-09-28 21:40:31 +02:00
|
|
|
if (ImGui::IsMouseClicked(0) && ImGui::IsItemActivated() && this->m_dragStartIterator == bookmarks.end())
|
|
|
|
this->m_dragStartIterator = iter;
|
|
|
|
|
|
|
|
if (ImGui::IsItemHovered() && this->m_dragStartIterator != bookmarks.end()) {
|
|
|
|
std::iter_swap(iter, this->m_dragStartIterator);
|
|
|
|
this->m_dragStartIterator = iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ImGui::IsMouseDown(0))
|
|
|
|
this->m_dragStartIterator = bookmarks.end();
|
|
|
|
} else {
|
2023-03-16 14:40:26 +01:00
|
|
|
const auto rowHeight = ImGui::GetTextLineHeightWithSpacing() + 2 * ImGui::GetStyle().FramePadding.y;
|
|
|
|
if (ImGui::BeginTable("##bookmark_table", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit)) {
|
|
|
|
ImGui::TableSetupColumn("##name");
|
|
|
|
ImGui::TableSetupColumn("##spacing", ImGuiTableColumnFlags_WidthFixed, 20);
|
|
|
|
ImGui::TableSetupColumn("##value", ImGuiTableColumnFlags_WidthStretch);
|
|
|
|
|
|
|
|
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
ImGui::TextUnformatted("hex.builtin.view.bookmarks.header.name"_lang);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
if (locked) {
|
|
|
|
if (ImGui::IconButton(ICON_VS_LOCK, ImGui::GetStyleColorVec4(ImGuiCol_Text))) locked = false;
|
|
|
|
} else {
|
|
|
|
if (ImGui::IconButton(ICON_VS_UNLOCK, ImGui::GetStyleColorVec4(ImGuiCol_Text))) locked = true;
|
|
|
|
}
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
ImGui::SameLine();
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
if (ImGui::ColorButton("hex.builtin.view.bookmarks.header.color"_lang, headerColor.Value, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoAlpha)) {
|
|
|
|
if (!locked)
|
|
|
|
ImGui::OpenPopup("hex.builtin.view.bookmarks.header.color"_lang);
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
if (ImGui::BeginPopup("hex.builtin.view.bookmarks.header.color"_lang)) {
|
|
|
|
drawColorPopup(headerColor);
|
|
|
|
color = headerColor;
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
2021-08-28 01:47:26 +02:00
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
ImGui::SameLine();
|
2021-08-28 01:47:26 +02:00
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
if (locked)
|
|
|
|
ImGui::TextUnformatted(name.data());
|
|
|
|
else {
|
|
|
|
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
|
|
|
|
ImGui::InputText("##nameInput", name);
|
|
|
|
ImGui::PopItemWidth();
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
ImGui::TextUnformatted("hex.builtin.common.address"_lang);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
if (ImGui::IconButton(ICON_VS_DEBUG_STEP_BACK, ImGui::GetStyleColorVec4(ImGuiCol_Text)))
|
|
|
|
ImHexApi::HexEditor::setSelection(region);
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::IconButton(ICON_VS_GO_TO_FILE, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
|
|
|
|
auto newProvider = ImHexApi::Provider::createProvider("hex.builtin.provider.view", true);
|
|
|
|
if (auto *viewProvider = dynamic_cast<ViewProvider*>(newProvider); viewProvider != nullptr) {
|
|
|
|
viewProvider->setProvider(region.getStartAddress(), region.getSize(), provider);
|
|
|
|
if (viewProvider->open())
|
|
|
|
EventManager::post<EventProviderOpened>(viewProvider);
|
2021-08-28 01:47:26 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 14:40:26 +01:00
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextFormatted("hex.builtin.view.bookmarks.address"_lang, region.getStartAddress(), region.getEndAddress());
|
2021-08-28 01:47:26 +02:00
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
|
|
|
|
ImGui::TableNextColumn();
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
ImGui::TextUnformatted("hex.builtin.common.size"_lang);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormatted(hex::toByteString(region.size));
|
2021-02-24 21:42:18 +01:00
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
ImGui::EndTable();
|
2022-10-07 10:53:08 +02:00
|
|
|
}
|
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
if (locked) {
|
|
|
|
if (!comment.empty()) {
|
|
|
|
ImGui::Header("hex.builtin.view.bookmarks.header.comment"_lang);
|
|
|
|
ImGui::TextFormattedWrapped("{}", comment.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ImGui::Header("hex.builtin.view.bookmarks.header.comment"_lang);
|
|
|
|
ImGui::InputTextMultiline("##commentInput", comment, ImVec2(ImGui::GetContentRegionAvail().x, 150_scaled));
|
2022-10-07 10:53:08 +02:00
|
|
|
}
|
2021-02-24 21:42:18 +01:00
|
|
|
|
2020-11-28 00:33:26 +01:00
|
|
|
ImGui::NewLine();
|
|
|
|
}
|
2022-03-26 16:55:48 +01:00
|
|
|
|
|
|
|
if (!open)
|
|
|
|
bookmarkToRemove = iter;
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
if (bookmarkToRemove != bookmarks.end()) {
|
|
|
|
bookmarks.erase(bookmarkToRemove);
|
2020-11-30 00:03:12 +01:00
|
|
|
}
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
2021-08-28 16:02:53 +02:00
|
|
|
ImGui::EndChild();
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
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 ®ion = 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() {
|
2023-03-20 13:11:43 +01:00
|
|
|
/* Create bookmark */
|
2023-03-20 15:12:12 +01:00
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.menu.edit.bookmark.create" }, 1900, CTRLCMD + Keys::B, [&] {
|
2023-03-20 13:11:43 +01:00
|
|
|
auto selection = ImHexApi::HexEditor::getSelection();
|
|
|
|
ImHexApi::Bookmarks::add(selection->getStartAddress(), selection->getSize(), {}, {});
|
|
|
|
}, []{ return ImHexApi::Provider::isValid() && ImHexApi::HexEditor::isSelectionValid(); });
|
2022-08-14 14:45:32 +02:00
|
|
|
|
|
|
|
|
2023-03-20 13:11:43 +01:00
|
|
|
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.file", "hex.builtin.menu.file.import" }, 3000);
|
2022-08-14 14:45:32 +02:00
|
|
|
|
2023-03-20 13:11:43 +01:00
|
|
|
/* Import bookmarks */
|
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.import", "hex.builtin.menu.file.import.bookmark" }, 3050, Shortcut::None, []{
|
|
|
|
fs::openFileBrowser(fs::DialogMode::Open, { { "Bookmarks File", "hexbm"} }, [&](const std::fs::path &path) {
|
|
|
|
try {
|
|
|
|
importBookmarks(ImHexApi::Provider::get(), nlohmann::json::parse(wolv::io::File(path, wolv::io::File::Mode::Read).readString()));
|
|
|
|
} catch (...) { }
|
|
|
|
});
|
|
|
|
}, ImHexApi::Provider::isValid);
|
2022-08-14 14:45:32 +02:00
|
|
|
|
2023-03-20 13:11:43 +01:00
|
|
|
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.file", "hex.builtin.menu.file.export" }, 6200);
|
|
|
|
|
|
|
|
|
|
|
|
/* Export bookmarks */
|
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.export", "hex.builtin.menu.file.export.bookmark" }, 6250, Shortcut::None, []{
|
|
|
|
fs::openFileBrowser(fs::DialogMode::Save, { { "Bookmarks File", "hexbm"} }, [&](const std::fs::path &path) {
|
|
|
|
nlohmann::json json;
|
|
|
|
exportBookmarks(ImHexApi::Provider::get(), json);
|
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
wolv::io::File(path, wolv::io::File::Mode::Create).writeString(json.dump(4));
|
2023-03-20 13:11:43 +01:00
|
|
|
});
|
|
|
|
}, []{
|
|
|
|
return ImHexApi::Provider::isValid() && !ProviderExtraData::getCurrent().bookmarks.empty();
|
2022-08-14 14:45:32 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|