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

@@ -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"); });
}
}