1
0
mirror of synced 2024-11-24 15:50:16 +01:00

Improve bookmark API

This commit is contained in:
WerWolv 2021-01-20 20:16:24 +01:00
parent be738eb5e7
commit b2648afc7b
17 changed files with 108 additions and 67 deletions

View File

@ -5,7 +5,7 @@
#include <string_view>
#include "patches.hpp"
#include <hex/helpers/utils.hpp>
#include <hex/api/imhex_api.hpp>
namespace hex {
@ -30,8 +30,8 @@ namespace hex {
[[nodiscard]] static const Patches& getPatches() { return ProjectFile::s_patches; }
static void setPatches(const Patches &patches) { ProjectFile::s_hasUnsavedChanged = true; ProjectFile::s_patches = patches; }
[[nodiscard]] static const std::list<Bookmark>& getBookmarks() { return ProjectFile::s_bookmarks; }
static void setBookmarks(const std::list<Bookmark> &bookmarks) { ProjectFile::s_hasUnsavedChanged = true; ProjectFile::s_bookmarks = bookmarks; }
[[nodiscard]] static const std::list<ImHexApi::Bookmarks::Entry>& getBookmarks() { return ProjectFile::s_bookmarks; }
static void setBookmarks(const std::list<ImHexApi::Bookmarks::Entry> &bookmarks) { ProjectFile::s_hasUnsavedChanged = true; ProjectFile::s_bookmarks = bookmarks; }
private:
static inline std::string s_currProjectFilePath;
@ -40,7 +40,7 @@ namespace hex {
static inline std::string s_filePath;
static inline std::string s_pattern;
static inline Patches s_patches;
static inline std::list<Bookmark> s_bookmarks;
static inline std::list<ImHexApi::Bookmarks::Entry> s_bookmarks;
};
}

View File

@ -13,14 +13,11 @@ namespace hex {
class ViewBookmarks : public View {
public:
explicit ViewBookmarks(std::list<Bookmark> &bookmarks);
ViewBookmarks();
~ViewBookmarks() override;
void drawContent() override;
void drawMenu() override;
private:
std::list<Bookmark> &m_bookmarks;
};
}

View File

@ -21,7 +21,7 @@ namespace hex {
class ViewHexEditor : public View {
public:
ViewHexEditor(std::vector<lang::PatternData*> &patternData, const std::list<Bookmark> &bookmarks);
ViewHexEditor(std::vector<lang::PatternData*> &patternData);
~ViewHexEditor() override;
void drawContent() override;
@ -33,7 +33,6 @@ namespace hex {
imgui_addons::ImGuiFileBrowser m_fileBrowser;
std::vector<lang::PatternData*> &m_patternData;
const std::list<Bookmark> &m_bookmarks;
std::map<u64, u32> m_highlightedBytes;

View File

@ -17,6 +17,7 @@ set(CMAKE_SHARED_LIBRARY_PREFIX "")
add_library(libimhex SHARED
source/api/event.cpp
source/api/imhex_api.cpp
source/api/content_registry.cpp
source/helpers/utils.cpp
source/helpers/shared_data.cpp

View File

@ -22,8 +22,7 @@ namespace hex {
It allows you to add/register new content that will be picked up and used by the ImHex core or by other
plugins when needed.
*/
class ContentRegistry {
public:
struct ContentRegistry {
ContentRegistry() = delete;
/* Settings Registry. Allows adding of new entries into the ImHex preferences window. */

View File

@ -0,0 +1,31 @@
#pragma once
#include <hex.hpp>
#include <hex/helpers/utils.hpp>
#include <list>
namespace hex {
struct ImHexApi {
ImHexApi() = delete;
struct Bookmarks {
Bookmarks() = delete;
struct Entry {
Region region;
std::vector<char> name;
std::vector<char> comment;
u32 color;
};
static void add(Region region, std::string_view name, std::string_view comment, u32 color = 0x00000000);
static void add(u64 addr, size_t size, std::string_view name, std::string_view comment, u32 color = 0x00000000);
static std::list<Entry>& getEntries();
};
};
}

View File

@ -2,10 +2,12 @@
#include <any>
#include <functional>
#include <vector>
#include <list>
#include <map>
#include <vector>
#include <hex/api/content_registry.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/event.hpp>
#include <hex/views/view.hpp>
@ -55,6 +57,7 @@ namespace hex {
static std::vector<ContentRegistry::DataInspector::Entry> dataInspectorEntries;
static u32 patternPaletteOffset;
static std::string errorPopupMessage;
static std::list<ImHexApi::Bookmarks::Entry> bookmarkEntries;
static int mainArgc;
static char **mainArgv;

View File

@ -197,11 +197,4 @@ namespace hex {
size_t size;
};
struct Bookmark {
Region region;
std::vector<char> name;
std::vector<char> comment;
u32 color;
};
}

View File

@ -4,6 +4,7 @@
#include <imgui.h>
#include <hex.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/views/view.hpp>
#include <hex/providers/provider.hpp>

View File

@ -0,0 +1,31 @@
#include <hex/api/imhex_api.hpp>
#include <hex/api/event.hpp>
#include <hex/helpers/shared_data.hpp>
namespace hex {
void ImHexApi::Bookmarks::add(Region region, std::string_view name, std::string_view comment, u32 color) {
Entry entry;
entry.region = region;
entry.name.reserve(name.length());
entry.comment.reserve(comment.length());
std::copy(name.begin(), name.end(), std::back_inserter(entry.name));
std::copy(comment.begin(), comment.end(), std::back_inserter(entry.comment));
entry.color = color;
EventManager::post(Events::AddBookmark, &entry);
}
void ImHexApi::Bookmarks::add(u64 addr, size_t size, std::string_view name, std::string_view comment, u32 color) {
Bookmarks::add(Region{addr, size}, name, comment, color);
}
std::list<ImHexApi::Bookmarks::Entry>& ImHexApi::Bookmarks::getEntries() {
return SharedData::bookmarkEntries;
}
}

View File

@ -16,6 +16,7 @@ namespace hex {
std::vector<ContentRegistry::DataInspector::Entry> SharedData::dataInspectorEntries;
u32 SharedData::patternPaletteOffset;
std::string SharedData::errorPopupMessage;
std::list<ImHexApi::Bookmarks::Entry> SharedData::bookmarkEntries;
int SharedData::mainArgc;
char **SharedData::mainArgv;

View File

@ -45,12 +45,13 @@ namespace hex {
}
PyObject* LoaderScript::Py_addBookmark(PyObject *self, PyObject *args) {
Bookmark bookmark;
u64 address;
size_t size;
char *name = nullptr;
char *comment = nullptr;
if (!PyArg_ParseTuple(args, "K|n|s|s", &bookmark.region.address, &bookmark.region.size, &name, &comment)) {
if (!PyArg_ParseTuple(args, "K|n|s|s", &address, &size, &name, &comment)) {
PyErr_BadArgument();
return nullptr;
}
@ -60,10 +61,7 @@ namespace hex {
return nullptr;
}
std::copy(name, name + std::strlen(name), std::back_inserter(bookmark.name));
std::copy(comment, comment + std::strlen(comment), std::back_inserter(bookmark.comment));
View::postEvent(Events::AddBookmark, &bookmark);
ImHexApi::Bookmarks::add(address, size, name, comment);
Py_RETURN_NONE;
}

View File

@ -7,11 +7,11 @@ using json = nlohmann::json;
namespace hex {
void to_json(json& j, const hex::Bookmark& b) {
void to_json(json& j, const ImHexApi::Bookmarks::Entry& b) {
j = json{ { "address", b.region.address }, { "size", b.region.size }, { "name", b.name.data() }, { "comment", b.comment.data() } };
}
void from_json(const json& j, hex::Bookmark& b) {
void from_json(const json& j, ImHexApi::Bookmarks::Entry& b) {
std::string name, comment;
j.at("address").get_to(b.region.address);
@ -38,7 +38,7 @@ namespace hex {
ProjectFile::s_patches = projectFileData["patches"].get<Patches>();
for (auto &element : projectFileData["bookmarks"].items()) {
ProjectFile::s_bookmarks.push_back(element.value().get<Bookmark>());
ProjectFile::s_bookmarks.push_back(element.value().get<ImHexApi::Bookmarks::Entry>());
}
} catch (json::exception &e) {

View File

@ -29,10 +29,9 @@ int main(int argc, char **argv) {
// Shared Data
std::vector<lang::PatternData*> patternData;
std::list<Bookmark> bookmarks;
// Create views
ContentRegistry::Views::add<ViewHexEditor>(patternData, bookmarks);
ContentRegistry::Views::add<ViewHexEditor>(patternData);
ContentRegistry::Views::add<ViewPattern>(patternData);
ContentRegistry::Views::add<ViewPatternData>(patternData);
ContentRegistry::Views::add<ViewDataInspector>();
@ -40,7 +39,7 @@ int main(int argc, char **argv) {
ContentRegistry::Views::add<ViewInformation>();
ContentRegistry::Views::add<ViewStrings>();
ContentRegistry::Views::add<ViewDisassembler>();
ContentRegistry::Views::add<ViewBookmarks>(bookmarks);
ContentRegistry::Views::add<ViewBookmarks>();
ContentRegistry::Views::add<ViewPatches>();
ContentRegistry::Views::add<ViewTools>();
ContentRegistry::Views::add<ViewCommandPalette>();

View File

@ -7,9 +7,9 @@
namespace hex {
ViewBookmarks::ViewBookmarks(std::list<Bookmark> &bookmarks) : View("Bookmarks"), m_bookmarks(bookmarks) {
ViewBookmarks::ViewBookmarks() : View("Bookmarks") {
View::subscribeEvent(Events::AddBookmark, [this](const void *userData) {
Bookmark bookmark = *reinterpret_cast<const Bookmark*>(userData);
auto bookmark = *reinterpret_cast<const ImHexApi::Bookmarks::Entry*>(userData);
bookmark.comment.resize(0xF'FFFF);
if (bookmark.name.empty()) {
@ -25,15 +25,15 @@ namespace hex {
bookmark.color = ImGui::GetColorU32(ImGuiCol_Header);
this->m_bookmarks.push_back(bookmark);
SharedData::bookmarkEntries.push_back(bookmark);
ProjectFile::markDirty();
});
View::subscribeEvent(Events::ProjectFileLoad, [this](const void*) {
this->m_bookmarks = ProjectFile::getBookmarks();
View::subscribeEvent(Events::ProjectFileLoad, [](const void*) {
SharedData::bookmarkEntries = ProjectFile::getBookmarks();
});
View::subscribeEvent(Events::ProjectFileStore, [this](const void*) {
ProjectFile::setBookmarks(this->m_bookmarks);
View::subscribeEvent(Events::ProjectFileStore, [](const void*) {
ProjectFile::setBookmarks(SharedData::bookmarkEntries);
});
}
@ -47,15 +47,17 @@ namespace hex {
if (ImGui::Begin("Bookmarks", &this->getWindowOpenState())) {
if (ImGui::BeginChild("##scrolling")) {
if (this->m_bookmarks.empty()) {
auto &bookmarks = ImHexApi::Bookmarks::getEntries();
if (bookmarks.empty()) {
ImGui::NewLine();
ImGui::Indent(30);
ImGui::TextWrapped("No bookmarks created yet. Add one with Edit -> Add Bookmark");
}
u32 id = 1;
std::list<Bookmark>::const_iterator bookmarkToRemove = this->m_bookmarks.end();
for (auto iter = this->m_bookmarks.begin(); iter != this->m_bookmarks.end(); iter++) {
auto bookmarkToRemove = bookmarks.end();
for (auto iter = bookmarks.begin(); iter != bookmarks.end(); iter++) {
auto &[region, name, comment, color] = *iter;
auto headerColor = ImColor(color);
@ -97,14 +99,14 @@ namespace hex {
ImGui::NewLine();
ImGui::TextUnformatted("Name");
ImGui::Separator();
ImGui::InputText("##nameInput", name.data(), 64);
ImGui::InputText("##nameInput", std::string(name.data()).data(), 64);
ImGui::SameLine();
ImGui::ColorEdit4("Color", (float*)&headerColor.Value, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoAlpha);
color = headerColor;
ImGui::NewLine();
ImGui::TextUnformatted("Comment");
ImGui::Separator();
ImGui::InputTextMultiline("##colorInput", comment.data(), 0xF'FFFF);
ImGui::InputTextMultiline("##colorInput", std::string(comment.data()).data(), 0xF'FFFF);
ImGui::NewLine();
}
@ -113,8 +115,8 @@ namespace hex {
id++;
}
if (bookmarkToRemove != this->m_bookmarks.end()) {
this->m_bookmarks.erase(bookmarkToRemove);
if (bookmarkToRemove != bookmarks.end()) {
bookmarks.erase(bookmarkToRemove);
ProjectFile::markDirty();
}

View File

@ -31,18 +31,6 @@ namespace hex {
return hex::format("#%s = ???", input.data());
});
ContentRegistry::CommandPaletteCommands::add(
ContentRegistry::CommandPaletteCommands::Type::KeywordCommand,
"/bm", "Create Bookmark",
[](auto input) {
Bookmark bookmark;
bookmark.name.resize(64);
std::strncpy(bookmark.name.data(), input.c_str(), bookmark.name.size());
View::postEvent(Events::AddBookmark, &bookmark);
return "";
});
this->m_lastResults = this->getCommandResults("");
}

View File

@ -1,6 +1,7 @@
#include "views/view_hexeditor.hpp"
#include <hex/providers/provider.hpp>
#include <hex/api/imhex_api.hpp>
#include "providers/file_provider.hpp"
#include <GLFW/glfw3.h>
@ -15,8 +16,8 @@
namespace hex {
ViewHexEditor::ViewHexEditor(std::vector<lang::PatternData*> &patternData, const std::list<Bookmark> &bookmarks)
: View("Hex Editor"), m_patternData(patternData), m_bookmarks(bookmarks) {
ViewHexEditor::ViewHexEditor(std::vector<lang::PatternData*> &patternData)
: View("Hex Editor"), m_patternData(patternData) {
this->m_memoryEditor.ReadFn = [](const ImU8 *data, size_t off) -> ImU8 {
auto provider = SharedData::currentProvider;
@ -44,7 +45,7 @@ namespace hex {
std::optional<u32> currColor, prevColor;
for (const auto &[region, name, comment, color] : _this->m_bookmarks) {
for (const auto &[region, name, comment, color] : ImHexApi::Bookmarks::getEntries()) {
if (off >= region.address && off < (region.address + region.size))
currColor = (color & 0x00FFFFFF) | 0x80000000;
if ((off - 1) >= region.address && (off - 1) < (region.address + region.size))
@ -74,11 +75,9 @@ namespace hex {
};
this->m_memoryEditor.HoverFn = [](const ImU8 *data, size_t addr) {
ViewHexEditor *_this = (ViewHexEditor *) data;
bool tooltipShown = false;
for (const auto &[region, name, comment, color] : _this->m_bookmarks) {
for (const auto &[region, name, comment, color] : ImHexApi::Bookmarks::getEntries()) {
if (addr >= region.address && addr < (region.address + region.size)) {
if (!tooltipShown) {
ImGui::BeginTooltip();
@ -1041,14 +1040,13 @@ R"(
if (ImGui::MenuItem("Create bookmark", nullptr, false, this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1)) {
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
Bookmark bookmark = { start, end - start + 1, { }, { } };
View::postEvent(Events::AddBookmark, &bookmark);
ImHexApi::Bookmarks::add(start, end - start + 1, { }, { });
}
auto provider = SharedData::currentProvider;
if (ImGui::MenuItem("Set base address", nullptr, false, provider != nullptr && provider->isReadable())) {
std::memset(this->m_baseAddressBuffer, sizeof(this->m_baseAddressBuffer), 0x00);
std::memset(this->m_baseAddressBuffer, 0x00, sizeof(this->m_baseAddressBuffer));
View::doLater([]{ ImGui::OpenPopup("Set base address"); });
}
}