mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-28 15:57:03 -05:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86e33a1ee9 | ||
|
|
37850ad85a | ||
|
|
daca49658e | ||
|
|
6975d7e2cd | ||
|
|
5b8bed6083 | ||
|
|
7474aa3e5d | ||
|
|
c6d2d51d4c | ||
|
|
9055105627 | ||
|
|
a44de63e24 | ||
|
|
8fc7931132 | ||
|
|
4070833229 | ||
|
|
3a9c3f939e | ||
|
|
aa42fb5076 | ||
|
|
50158a7977 |
2
lib/external/pattern_language
vendored
2
lib/external/pattern_language
vendored
Submodule lib/external/pattern_language updated: 34d6ad5f02...3d192fb629
@@ -137,6 +137,7 @@ set(LIBIMHEX_SOURCES
|
||||
source/helpers/encoding_file.cpp
|
||||
source/helpers/logger.cpp
|
||||
source/helpers/tar.cpp
|
||||
source/helpers/types.cpp
|
||||
|
||||
source/providers/provider.cpp
|
||||
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
#include <hex/helpers/types.hpp>
|
||||
#include <hex/helpers/intrinsics.hpp>
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
using u8 = std::uint8_t;
|
||||
using u16 = std::uint16_t;
|
||||
using u32 = std::uint32_t;
|
||||
@@ -20,29 +23,14 @@ namespace hex {
|
||||
u64 address;
|
||||
size_t size;
|
||||
|
||||
[[nodiscard]] constexpr bool isWithin(const Region &other) const {
|
||||
return (this->getStartAddress() >= other.getStartAddress()) && (this->getEndAddress() <= other.getEndAddress()) && *this != Invalid() && other != Invalid();
|
||||
}
|
||||
[[nodiscard]] bool isWithin(const Region &other) const;
|
||||
[[nodiscard]] bool overlaps(const Region &other) const;
|
||||
|
||||
[[nodiscard]] constexpr bool overlaps(const Region &other) const {
|
||||
return (this->getEndAddress() >= other.getStartAddress()) && (this->getStartAddress() < other.getEndAddress()) && *this != Invalid() && other != Invalid();
|
||||
}
|
||||
[[nodiscard]] u64 getStartAddress() const;
|
||||
[[nodiscard]] u64 getEndAddress() const;
|
||||
[[nodiscard]] size_t getSize() const;
|
||||
|
||||
[[nodiscard]] constexpr u64 getStartAddress() const {
|
||||
return this->address;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr u64 getEndAddress() const {
|
||||
return this->address + this->size - 1;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr size_t getSize() const {
|
||||
return this->size;
|
||||
}
|
||||
|
||||
constexpr bool operator==(const Region &other) const {
|
||||
return this->address == other.address && this->size == other.size;
|
||||
}
|
||||
bool operator==(const Region &other) const;
|
||||
|
||||
constexpr static Region Invalid() {
|
||||
return { 0, 0 };
|
||||
|
||||
41
lib/libimhex/source/helpers/types.cpp
Normal file
41
lib/libimhex/source/helpers/types.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <hex/helpers/types.hpp>
|
||||
|
||||
namespace hex {
|
||||
|
||||
[[nodiscard]] bool Region::isWithin(const Region &other) const {
|
||||
if (*this == Invalid() || other == Invalid())
|
||||
return false;
|
||||
|
||||
if (this->getStartAddress() >= other.getStartAddress() && this->getEndAddress() <= other.getEndAddress())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool Region::overlaps(const Region &other) const {
|
||||
if (*this == Invalid() || other == Invalid())
|
||||
return false;
|
||||
|
||||
if (this->getEndAddress() >= other.getStartAddress() && this->getStartAddress() <= other.getEndAddress())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] u64 Region::getStartAddress() const {
|
||||
return this->address;
|
||||
}
|
||||
|
||||
[[nodiscard]] u64 Region::getEndAddress() const {
|
||||
return this->address + this->size - 1;
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t Region::getSize() const {
|
||||
return this->size;
|
||||
}
|
||||
|
||||
bool Region::operator==(const Region &other) const {
|
||||
return this->address == other.address && this->size == other.size;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -68,8 +68,11 @@ namespace hex::plugin::builtin {
|
||||
return data.selectionStart.has_value() && data.selectionEnd.has_value();
|
||||
}
|
||||
|
||||
void jumpToSelection() {
|
||||
void jumpToSelection(bool center = true) {
|
||||
this->m_shouldJumpToSelection = true;
|
||||
|
||||
if (center)
|
||||
this->m_centerOnJump = true;
|
||||
}
|
||||
|
||||
void scrollToSelection() {
|
||||
@@ -111,6 +114,7 @@ namespace hex::plugin::builtin {
|
||||
void drawFooter(const ImVec2 &size);
|
||||
|
||||
void handleSelection(u64 address, u32 bytesPerCell, const u8 *data, bool cellHovered);
|
||||
std::optional<color_t> applySelectionColor(u64 byteAddress, std::optional<color_t> color);
|
||||
|
||||
private:
|
||||
u16 m_bytesPerRow = 16;
|
||||
@@ -118,6 +122,7 @@ namespace hex::plugin::builtin {
|
||||
ContentRegistry::HexEditor::DataVisualizer *m_currDataVisualizer;
|
||||
|
||||
bool m_shouldJumpToSelection = false;
|
||||
bool m_centerOnJump = false;
|
||||
bool m_shouldScrollToSelection = false;
|
||||
bool m_shouldJumpWhenOffScreen = false;
|
||||
bool m_shouldUpdateScrollPosition = false;
|
||||
|
||||
@@ -450,7 +450,8 @@ namespace hex::plugin::builtin {
|
||||
[](auto c) { return c == 0x00; });
|
||||
buffer.erase(it, buffer.end());
|
||||
|
||||
value = copyValue = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>("Invalid").to_bytes(stringBuffer.data());
|
||||
auto string = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>("Invalid").to_bytes(stringBuffer.data());
|
||||
value = copyValue = hex::encodeByteString({ string.begin(), string.end() });
|
||||
|
||||
if (value.size() > MaxStringLength) {
|
||||
value.resize(MaxStringLength);
|
||||
|
||||
@@ -34,6 +34,43 @@ namespace hex {
|
||||
|
||||
using namespace ::std::literals::string_literals;
|
||||
|
||||
bool isPatternSelected(u64 address, u64 size) {
|
||||
auto currSelection = ImHexApi::HexEditor::getSelection();
|
||||
if (!currSelection.has_value())
|
||||
return false;
|
||||
|
||||
return Region{ address, size }.overlaps(*currSelection);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto highlightWhenSelected(u64 address, u64 size, const T &callback) {
|
||||
constexpr bool HasReturn = !requires(T t) { { t() } -> std::same_as<void>; };
|
||||
|
||||
auto selected = isPatternSelected(address, size);
|
||||
|
||||
if (selected)
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_HeaderActive));
|
||||
|
||||
if constexpr (HasReturn) {
|
||||
auto result = callback();
|
||||
|
||||
if (selected)
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
return result;
|
||||
} else {
|
||||
callback();
|
||||
|
||||
if (selected)
|
||||
ImGui::PopStyleColor();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto highlightWhenSelected(const pl::ptrn::Pattern& pattern, const T &callback) {
|
||||
return highlightWhenSelected(pattern.getOffset(), pattern.getSize(), callback);
|
||||
}
|
||||
|
||||
void createLeafNode(const pl::ptrn::Pattern& pattern) {
|
||||
ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf |
|
||||
ImGuiTreeNodeFlags_NoTreePushOnOpen |
|
||||
@@ -44,12 +81,13 @@ namespace hex {
|
||||
bool createTreeNode(const pl::ptrn::Pattern& pattern) {
|
||||
if (pattern.isSealed()) {
|
||||
ImGui::Indent();
|
||||
ImGui::TextUnformatted(pattern.getDisplayName().c_str());
|
||||
highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(pattern.getDisplayName().c_str()); });
|
||||
ImGui::Unindent();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
|
||||
else {
|
||||
return highlightWhenSelected(pattern, [&]{ return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);});
|
||||
}
|
||||
}
|
||||
|
||||
void drawTypenameColumn(const pl::ptrn::Pattern& pattern, const std::string& pattern_name) {
|
||||
@@ -60,7 +98,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
void drawNameColumn(const pl::ptrn::Pattern& pattern) {
|
||||
ImGui::TextUnformatted(pattern.getDisplayName().c_str());
|
||||
highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(pattern.getDisplayName().c_str()); });
|
||||
ImGui::TableNextColumn();
|
||||
}
|
||||
|
||||
@@ -387,7 +425,7 @@ namespace hex {
|
||||
|
||||
size_t chunkSize = (endOffset - startOffset) + endSize;
|
||||
|
||||
auto chunkOpen = ImGui::TreeNode(hex::format("[{} ... {}]", i, endIndex - 1).c_str());
|
||||
auto chunkOpen = highlightWhenSelected(startOffset, ((endOffset + endSize) - startOffset) - 1, [&]{ return ImGui::TreeNodeEx(hex::format("[{} ... {}]", i, endIndex - 1).c_str(), ImGuiTreeNodeFlags_SpanFullWidth); });
|
||||
ImGui::TableNextColumn();
|
||||
drawColorColumn(pattern);
|
||||
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", startOffset, startOffset + chunkSize - (pattern.getSize() == 0 ? 0 : 1));
|
||||
|
||||
@@ -522,6 +522,27 @@ namespace hex::plugin::builtin {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<color_t> ViewHexEditor::applySelectionColor(u64 byteAddress, std::optional<color_t> color) {
|
||||
if (!color.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
if (this->isSelectionValid()) {
|
||||
auto selection = this->getSelection();
|
||||
|
||||
if (byteAddress >= selection.getStartAddress() && byteAddress <= selection.getEndAddress()) {
|
||||
if (color.has_value())
|
||||
color = (ImAlphaBlendColors(color.value(), this->m_selectionColor)) & 0x00FFFFFF;
|
||||
else
|
||||
color = this->m_selectionColor;
|
||||
}
|
||||
}
|
||||
|
||||
if (color.has_value())
|
||||
color = (*color & 0x00FFFFFF) | (this->m_selectionColor & 0xFF000000);
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
struct CustomEncodingData {
|
||||
std::string displayValue;
|
||||
size_t advance;
|
||||
@@ -811,25 +832,7 @@ namespace hex::plugin::builtin {
|
||||
// Query cell colors
|
||||
if (x < std::ceil(float(validBytes) / bytesPerCell)) {
|
||||
const auto foregroundColor = queryForegroundColor(byteAddress, &bytes[x * cellBytes], cellBytes);
|
||||
const auto backgroundColor = [&]{
|
||||
auto color = queryBackgroundColor(byteAddress, &bytes[x * cellBytes], cellBytes);
|
||||
|
||||
if (this->isSelectionValid()) {
|
||||
auto selection = this->getSelection();
|
||||
|
||||
if (byteAddress >= selection.getStartAddress() && byteAddress <= selection.getEndAddress()) {
|
||||
if (color.has_value())
|
||||
color = (ImAlphaBlendColors(color.value(), this->m_selectionColor)) & 0x00FFFFFF;
|
||||
else
|
||||
color = this->m_selectionColor;
|
||||
}
|
||||
}
|
||||
|
||||
if (color.has_value())
|
||||
color = (*color & 0x00FFFFFF) | (this->m_selectionColor & 0xFF000000);
|
||||
|
||||
return color;
|
||||
}();
|
||||
const auto backgroundColor = queryBackgroundColor(byteAddress, &bytes[x * cellBytes], cellBytes);
|
||||
|
||||
cellColors.emplace_back(
|
||||
foregroundColor,
|
||||
@@ -862,8 +865,9 @@ namespace hex::plugin::builtin {
|
||||
auto [foregroundColor, backgroundColor] = cellColors[x];
|
||||
|
||||
if (isColumnSeparatorColumn(x + 1, columnCount)) {
|
||||
if (this->isSelectionValid() && this->getSelection().getEndAddress() != x + y * columnCount)
|
||||
cellSize.x += SeparatorColumWidth + 1;
|
||||
auto separatorAddress = x + y * columnCount;
|
||||
if ((this->isSelectionValid() && this->getSelection().overlaps({ separatorAddress, 1 }) && this->getSelection().getEndAddress() != separatorAddress) || cellColors[x] == cellColors[x + 1])
|
||||
cellSize.x += SeparatorColumWidth + 1;
|
||||
}
|
||||
|
||||
if (y == u64(clipper.DisplayStart))
|
||||
@@ -874,7 +878,7 @@ namespace hex::plugin::builtin {
|
||||
auto drawList = ImGui::GetWindowDrawList();
|
||||
|
||||
// Draw background color
|
||||
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, backgroundColor.value());
|
||||
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, applySelectionColor(byteAddress, backgroundColor).value());
|
||||
|
||||
// Draw frame around mouse selection
|
||||
this->drawSelectionFrame(x, y, byteAddress, bytesPerCell, cellStartPos, cellSize);
|
||||
@@ -936,7 +940,7 @@ namespace hex::plugin::builtin {
|
||||
auto drawList = ImGui::GetWindowDrawList();
|
||||
|
||||
// Draw background color
|
||||
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, backgroundColor.value());
|
||||
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, applySelectionColor(byteAddress, backgroundColor).value());
|
||||
|
||||
this->drawSelectionFrame(x, y, byteAddress, 1, cellStartPos, cellSize);
|
||||
}
|
||||
@@ -994,7 +998,7 @@ namespace hex::plugin::builtin {
|
||||
auto drawList = ImGui::GetWindowDrawList();
|
||||
|
||||
// Draw background color
|
||||
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, backgroundColor.value());
|
||||
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, applySelectionColor(address, backgroundColor).value());
|
||||
|
||||
this->drawSelectionFrame(x, y, address, 1, cellStartPos, cellSize);
|
||||
}
|
||||
@@ -1017,21 +1021,20 @@ namespace hex::plugin::builtin {
|
||||
// Scroll to the cursor if it's either at the top or bottom edge of the screen
|
||||
if (this->m_shouldScrollToSelection && this->isSelectionValid()) {
|
||||
// Make sure simply clicking on a byte at the edge of the screen won't cause scrolling
|
||||
if ((ImGui::IsMouseDown(ImGuiMouseButton_Left) && providerData.selectionStart != providerData.selectionEnd && providerData.selectionEnd.has_value())) {
|
||||
auto scrollPerLine = ImGui::GetScrollMaxY() / (provider->getSize() / (long double)(this->m_bytesPerRow));
|
||||
if ((ImGui::IsMouseDown(ImGuiMouseButton_Left) && providerData.selectionStart != providerData.selectionEnd)) {
|
||||
auto fractionPerLine = 1.0 / (this->m_visibleRowCount + 1);
|
||||
|
||||
auto scrollPos = ImGui::GetScrollY();
|
||||
auto scrollUpStartPos = scrollPos + scrollPerLine * 2;
|
||||
auto scrollDownStartPos = scrollPos + ImGui::GetWindowHeight() - scrollPerLine * 4;
|
||||
if (y == (u64(clipper.DisplayStart) + 3)) {
|
||||
if (i128(*providerData.selectionEnd - provider->getBaseAddress() - provider->getCurrentPageAddress()) <= (i64(clipper.DisplayStart + 3) * this->m_bytesPerRow)) {
|
||||
this->m_shouldScrollToSelection = false;
|
||||
ImGui::SetScrollHereY(fractionPerLine * 5);
|
||||
|
||||
auto cursorPos = float(u64(*providerData.selectionEnd / this->m_bytesPerRow)) * CharacterSize.y;
|
||||
|
||||
if (cursorPos <= scrollUpStartPos && y == u64(clipper.DisplayStart)) {
|
||||
this->m_shouldScrollToSelection = false;
|
||||
ImGui::SetScrollHereY(0.1F);
|
||||
} else if (cursorPos >= scrollDownStartPos && y == u64(clipper.DisplayEnd - 1)) {
|
||||
this->m_shouldScrollToSelection = false;
|
||||
ImGui::SetScrollHereY(0.95F);
|
||||
}
|
||||
} else if (y == (u64(clipper.DisplayEnd) - 1)) {
|
||||
if (i128(*providerData.selectionEnd - provider->getBaseAddress() - provider->getCurrentPageAddress()) >= (i64(clipper.DisplayEnd - 2) * this->m_bytesPerRow)) {
|
||||
this->m_shouldScrollToSelection = false;
|
||||
ImGui::SetScrollHereY(fractionPerLine * (this->m_visibleRowCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1044,9 +1047,9 @@ namespace hex::plugin::builtin {
|
||||
newSelection.address -= pageAddress;
|
||||
|
||||
if ((newSelection.getStartAddress()) < u64(clipper.DisplayStart * this->m_bytesPerRow))
|
||||
this->jumpToSelection();
|
||||
this->jumpToSelection(false);
|
||||
if ((newSelection.getEndAddress()) > u64(clipper.DisplayEnd * this->m_bytesPerRow))
|
||||
this->jumpToSelection();
|
||||
this->jumpToSelection(false);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1061,7 +1064,16 @@ namespace hex::plugin::builtin {
|
||||
provider->setCurrentPage(provider->getPageOfAddress(newSelection.address).value_or(0));
|
||||
|
||||
const auto pageAddress = provider->getCurrentPageAddress() + provider->getBaseAddress();
|
||||
ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + (static_cast<long double>(newSelection.getStartAddress() - pageAddress) / this->m_bytesPerRow) * CharacterSize.y, 0.5);
|
||||
auto scrollPos = (static_cast<long double>(newSelection.getStartAddress() - pageAddress) / this->m_bytesPerRow) * CharacterSize.y;
|
||||
bool scrollUpwards = scrollPos < ImGui::GetScrollY();
|
||||
auto scrollFraction = scrollUpwards ? 0.0F : (1.0F - ((1.0F / this->m_visibleRowCount) * 2));
|
||||
|
||||
if (this->m_centerOnJump) {
|
||||
scrollFraction = 0.5F;
|
||||
this->m_centerOnJump = false;
|
||||
}
|
||||
|
||||
ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + scrollPos, scrollFraction);
|
||||
}
|
||||
|
||||
if (!this->m_syncScrolling) {
|
||||
|
||||
@@ -854,16 +854,18 @@ namespace hex::plugin::builtin {
|
||||
return this->m_dangerousFunctionsAllowed == DangerousFunctionPerms::Allow;
|
||||
});
|
||||
|
||||
ON_SCOPE_EXIT {
|
||||
this->m_lastEvaluationLog = runtime->getConsoleLog();
|
||||
this->m_lastEvaluationOutVars = runtime->getOutVariables();
|
||||
this->m_runningEvaluators--;
|
||||
|
||||
this->m_lastEvaluationProcessed = false;
|
||||
};
|
||||
|
||||
this->m_lastEvaluationResult = runtime->executeString(code, envVars, inVariables);
|
||||
if (!this->m_lastEvaluationResult) {
|
||||
this->m_lastEvaluationError = runtime->getError();
|
||||
}
|
||||
|
||||
this->m_lastEvaluationLog = runtime->getConsoleLog();
|
||||
this->m_lastEvaluationOutVars = runtime->getOutVariables();
|
||||
this->m_runningEvaluators--;
|
||||
|
||||
this->m_lastEvaluationProcessed = false;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user