feat: Finish up work on new pl section system

This commit is contained in:
WerWolv
2022-11-08 21:43:22 +01:00
parent 23ce2ec271
commit 4c5d2f6ebb
22 changed files with 250 additions and 215 deletions

View File

@@ -1,204 +0,0 @@
#pragma once
#include <hex.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/encoding_file.hpp>
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>
#include <imgui_internal.h>
#include <hex/ui/imgui_imhex_extensions.h>
#include <fonts/codicons_font.h>
#include <content/helpers/math_evaluator.hpp>
#include <hex/ui/view.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/providers/provider.hpp>
#include <hex/providers/buffered_reader.hpp>
namespace hex {
class HexEditor {
public:
HexEditor();
~HexEditor();
void draw(prv::Provider *provider, float height = ImGui::GetContentRegionAvail().y);
private:
enum class CellType { None, Hex, ASCII };
void drawCell(prv::Provider *provider, u64 address, u8 *data, size_t size, bool hovered, CellType cellType);
void drawSelectionFrame(u32 x, u32 y, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize) const;
void drawEditor(prv::Provider *provider, const ImVec2 &size);
void drawFooter(prv::Provider *provider, const ImVec2 &size);
void drawTooltip(u64 address, const u8 *data, size_t size);
void handleSelection(u64 address, u32 bytesPerCell, const u8 *data, bool cellHovered);
std::optional<color_t> applySelectionColor(u64 byteAddress, std::optional<color_t> color);
public:
void setSelectionUnchecked(std::optional<u64> start, std::optional<u64> end) {
this->m_selectionStart = start;
this->m_selectionEnd = end;
}
void setSelection(const Region &region) { this->setSelection(region.getStartAddress(), region.getEndAddress()); }
void setSelection(u128 start, u128 end) {
if (!ImHexApi::Provider::isValid())
return;
auto provider = ImHexApi::Provider::get();
const size_t maxAddress = provider->getActualSize() + provider->getBaseAddress() - 1;
this->m_selectionChanged = this->m_selectionStart != start || this->m_selectionEnd != end;
this->m_selectionStart = std::clamp<u128>(start, 0, maxAddress);
this->m_selectionEnd = std::clamp<u128>(end, 0, maxAddress);
if (this->m_selectionChanged) {
EventManager::post<EventRegionSelected>(this->getSelection());
this->m_shouldModifyValue = true;
}
}
[[nodiscard]] Region getSelection() const {
if (!isSelectionValid())
return Region::Invalid();
const auto start = std::min(this->m_selectionStart.value(), this->m_selectionEnd.value());
const auto end = std::max(this->m_selectionStart.value(), this->m_selectionEnd.value());
const size_t size = end - start + 1;
return { start, size };
}
[[nodiscard]] bool isSelectionValid() const {
return this->m_selectionStart.has_value() && this->m_selectionEnd.has_value();
}
void jumpToSelection(bool center = true) {
this->m_shouldJumpToSelection = true;
if (center)
this->m_centerOnJump = true;
}
void scrollToSelection() {
this->m_shouldScrollToSelection = true;
}
void jumpIfOffScreen() {
this->m_shouldJumpWhenOffScreen = true;
}
[[nodiscard]] u16 getBytesPerRow() const {
return this->m_bytesPerRow;
}
void setBytesPerRow(u16 bytesPerRow) {
this->m_bytesPerRow = bytesPerRow;
}
[[nodiscard]] u16 getVisibleRowCount() const {
return this->m_visibleRowCount;
}
void setSelectionColor(color_t color) {
this->m_selectionColor = color;
}
void enableUpperCaseHex(bool upperCaseHex) {
this->m_upperCaseHex = upperCaseHex;
}
void enableGrayOutZeros(bool grayOutZeros) {
this->m_grayOutZero = grayOutZeros;
}
void enableShowAscii(bool showAscii) {
this->m_showAscii = showAscii;
}
void enableSyncScrolling(bool syncScrolling) {
this->m_syncScrolling = syncScrolling;
}
void setByteCellPadding(u32 byteCellPadding) {
this->m_byteCellPadding = byteCellPadding;
}
void setCharacterCellPadding(u32 characterCellPadding) {
this->m_characterCellPadding = characterCellPadding;
}
void setCustomEncoding(EncodingFile encoding) {
this->m_currCustomEncoding = std::move(encoding);
}
void forceUpdateScrollPosition() {
this->m_shouldUpdateScrollPosition = true;
}
void setForegroundHighlightCallback(const std::function<std::optional<color_t>(u64, const u8 *, size_t)> &callback) {
this->m_foregroundColorCallback = callback;
}
void setBackgroundHighlightCallback(const std::function<std::optional<color_t>(u64, const u8 *, size_t)> &callback) {
this->m_backgroundColorCallback = callback;
}
void setTooltipCallback(const std::function<void(u64, const u8 *, size_t)> &callback) {
this->m_tooltipCallback = callback;
}
[[nodiscard]] float getScrollPosition() const {
return this->m_scrollPosition;
}
void setScrollPosition(float scrollPosition) {
this->m_scrollPosition = scrollPosition;
}
private:
std::optional<u64> m_selectionStart;
std::optional<u64> m_selectionEnd;
float m_scrollPosition = 0;
u16 m_bytesPerRow = 16;
ContentRegistry::HexEditor::DataVisualizer *m_currDataVisualizer;
u32 m_grayZeroHighlighter = 0;
bool m_shouldJumpToSelection = false;
bool m_centerOnJump = false;
bool m_shouldScrollToSelection = false;
bool m_shouldJumpWhenOffScreen = false;
bool m_shouldUpdateScrollPosition = false;
bool m_selectionChanged = false;
u16 m_visibleRowCount = 0;
CellType m_editingCellType = CellType::None;
std::optional<u64> m_editingAddress;
bool m_shouldModifyValue = false;
bool m_enteredEditingMode = false;
bool m_shouldUpdateEditingValue = false;
std::vector<u8> m_editingBytes;
color_t m_selectionColor = 0x00;
bool m_upperCaseHex = true;
bool m_grayOutZero = true;
bool m_showAscii = true;
bool m_syncScrolling = false;
u32 m_byteCellPadding = 0, m_characterCellPadding = 0;
std::optional<EncodingFile> m_currCustomEncoding;
static std::optional<color_t> defaultColorCallback(u64, const u8 *, size_t) { return std::nullopt; }
static void defaultTooltipCallback(u64, const u8 *, size_t) { }
std::function<std::optional<color_t>(u64, const u8 *, size_t)> m_foregroundColorCallback = defaultColorCallback, m_backgroundColorCallback = defaultColorCallback;
std::function<void(u64, const u8 *, size_t)> m_tooltipCallback = defaultTooltipCallback;
};
}

View File

@@ -1,43 +0,0 @@
#pragma once
#include <pl/patterns/pattern.hpp>
#include <pl/pattern_visitor.hpp>
#include <hex/providers/provider.hpp>
namespace hex {
class PatternDrawer : public pl::PatternVisitor {
public:
PatternDrawer() = default;
void draw(pl::ptrn::Pattern& pattern);
void visit(pl::ptrn::PatternArrayDynamic& pattern) override;
void visit(pl::ptrn::PatternArrayStatic& pattern) override;
void visit(pl::ptrn::PatternBitfieldField& pattern) override;
void visit(pl::ptrn::PatternBitfield& pattern) override;
void visit(pl::ptrn::PatternBoolean& pattern) override;
void visit(pl::ptrn::PatternCharacter& pattern) override;
void visit(pl::ptrn::PatternEnum& pattern) override;
void visit(pl::ptrn::PatternFloat& pattern) override;
void visit(pl::ptrn::PatternPadding& pattern) override;
void visit(pl::ptrn::PatternPointer& pattern) override;
void visit(pl::ptrn::PatternSigned& pattern) override;
void visit(pl::ptrn::PatternString& pattern) override;
void visit(pl::ptrn::PatternStruct& pattern) override;
void visit(pl::ptrn::PatternUnion& pattern) override;
void visit(pl::ptrn::PatternUnsigned& pattern) override;
void visit(pl::ptrn::PatternWideCharacter& pattern) override;
void visit(pl::ptrn::PatternWideString& pattern) override;
private:
constexpr static auto ChunkSize = 512;
constexpr static auto DisplayEndStep = 64;
void drawArray(pl::ptrn::Pattern& pattern, pl::ptrn::Iteratable &iteratable, bool isInlined);
u64& getDisplayEnd(const pl::ptrn::Pattern& pattern);
private:
std::map<const pl::ptrn::Pattern*, u64> m_displayEnd;
};
}

View File

@@ -12,9 +12,9 @@ namespace hex::plugin::builtin {
[[nodiscard]] bool isAvailable() const override { return true; }
[[nodiscard]] bool isReadable() const override { return true; }
[[nodiscard]] bool isWritable() const override { return true; }
[[nodiscard]] bool isResizable() const override { return true; }
[[nodiscard]] bool isSavable() const override { return true; }
[[nodiscard]] bool isWritable() const override { return !this->m_readOnly; }
[[nodiscard]] bool isResizable() const override { return !this->m_readOnly; }
[[nodiscard]] bool isSavable() const override { return !this->m_readOnly; }
[[nodiscard]] bool open() override;
void close() override { }
@@ -42,8 +42,11 @@ namespace hex::plugin::builtin {
void loadSettings(const nlohmann::json &settings) override { hex::unused(settings); }
[[nodiscard]] nlohmann::json storeSettings(nlohmann::json settings) const override { return settings; }
void setReadOnly(bool readOnly) { this->m_readOnly = readOnly; }
private:
std::vector<u8> m_data;
bool m_readOnly = false;
};
}

View File

@@ -33,6 +33,7 @@ namespace hex::plugin::builtin {
u64 m_startAddress = 0;
size_t m_validBytes = 0;
prv::Provider *m_selectedProvider = nullptr;
std::atomic<bool> m_dataValid = false;
std::vector<InspectorCacheEntry> m_cachedData, m_workData;
TaskHolder m_updateTask;

View File

@@ -7,7 +7,7 @@
#include <hex/helpers/encoding_file.hpp>
#include <content/helpers/provider_extra_data.hpp>
#include <content/helpers/hex_editor.hpp>
#include <ui/hex_editor.hpp>
namespace hex::plugin::builtin {
@@ -70,7 +70,7 @@ namespace hex::plugin::builtin {
void registerEvents();
void registerMenuItems();
HexEditor m_hexEditor;
ui::HexEditor m_hexEditor;
bool m_shouldOpenPopup = false;
std::unique_ptr<Popup> m_currPopup;

View File

@@ -3,7 +3,7 @@
#include <hex.hpp>
#include <hex/ui/view.hpp>
#include <content/helpers/pattern_drawer.hpp>
#include <ui/pattern_drawer.hpp>
#include <hex/providers/provider.hpp>
#include <vector>
@@ -20,7 +20,7 @@ namespace hex::plugin::builtin {
private:
std::map<hex::prv::Provider *, std::vector<pl::ptrn::Pattern*>> m_sortedPatterns;
hex::PatternDrawer m_patternDrawer;
ui::PatternDrawer m_patternDrawer;
};
}

View File

@@ -6,7 +6,8 @@
#include <hex/providers/provider.hpp>
#include <content/helpers/provider_extra_data.hpp>
#include <content/helpers/hex_editor.hpp>
#include <ui/hex_editor.hpp>
#include <ui/pattern_drawer.hpp>
#include <content/providers/memory_file_provider.hpp>
#include <cstring>
@@ -14,6 +15,7 @@
#include <string_view>
#include <thread>
#include <vector>
#include <functional>
#include <TextEditor.h>
@@ -63,9 +65,7 @@ namespace hex::plugin::builtin {
bool m_syncPatternSourceCode = false;
bool m_autoLoadPatterns = true;
std::unique_ptr<MemoryFileProvider> m_sectionProvider = nullptr;
HexEditor m_hexEditor;
std::map<prv::Provider*, std::move_only_function<void()>> m_sectionWindowDrawer;
private:
void drawConsole(ImVec2 size, const std::vector<std::pair<pl::core::LogConsole::Level, std::string>> &console);
void drawEnvVars(ImVec2 size, std::list<PlData::EnvVar> &envVars);