1
0
mirror of synced 2024-11-12 10:10:53 +01:00

patterns: Fixed [[static]] attribute and majorly reduced memory usage

Fixes #394
This commit is contained in:
WerWolv 2022-01-12 22:02:47 +01:00
parent 0d02af3cf0
commit f6e4d56de6
7 changed files with 189 additions and 83 deletions

View File

@ -30,8 +30,6 @@ namespace hex::plugin::builtin {
private:
MemoryEditor m_memoryEditor;
std::map<u64, u32> m_highlightedBytes;
std::vector<char> m_searchStringBuffer;
std::vector<char> m_searchHexBuffer;
SearchFunction m_searchFunction = nullptr;
@ -47,6 +45,7 @@ namespace hex::plugin::builtin {
u64 m_resizeSize = 0;
std::vector<u8> m_dataToSave;
std::set<pl::PatternData*> m_highlightedPatterns;
std::string m_loaderScriptScriptPath;
std::string m_loaderScriptFilePath;

View File

@ -75,13 +75,24 @@ namespace hex::plugin::builtin {
prevColor = (color & 0x00FFFFFF) | alpha;
}
if (_this->m_highlightedBytes.contains(off)) {
auto color = (_this->m_highlightedBytes[off] & 0x00FFFFFF) | alpha;
currColor = currColor.has_value() ? ImAlphaBlendColors(color, currColor.value()) : color;
}
if (_this->m_highlightedBytes.contains(off - 1)) {
auto color = (_this->m_highlightedBytes[off - 1] & 0x00FFFFFF) | alpha;
prevColor = prevColor.has_value() ? ImAlphaBlendColors(color, prevColor.value()) : color;
{
for (const auto &pattern : SharedData::patternData) {
auto child = pattern->getPattern(off);
if (child != nullptr) {
auto color = (child->getColor() & 0x00FFFFFF) | alpha;
currColor = currColor.has_value() ? ImAlphaBlendColors(color, currColor.value()) : color;
break;
}
}
for (const auto &pattern : SharedData::patternData) {
auto child = pattern->getPattern(off - 1);
if (child != nullptr) {
auto color = (child->getColor() & 0x00FFFFFF) | alpha;
prevColor = prevColor.has_value() ? ImAlphaBlendColors(color, currColor.value()) : color;
break;
}
}
}
if (next && prevColor != currColor) {
@ -151,7 +162,6 @@ namespace hex::plugin::builtin {
EventManager::unsubscribe<RequestSelectionChange>(this);
EventManager::unsubscribe<EventProjectFileLoad>(this);
EventManager::unsubscribe<EventWindowClosing>(this);
EventManager::unsubscribe<EventPatternChanged>(this);
EventManager::unsubscribe<RequestOpenWindow>(this);
EventManager::unsubscribe<EventSettingsChanged>(this);
}
@ -642,7 +652,11 @@ namespace hex::plugin::builtin {
EventManager::post<EventFileLoaded>(path);
EventManager::post<EventDataChanged>();
EventManager::post<EventPatternChanged>();
{
std::vector<pl::PatternData*> patterns;
EventManager::post<EventPatternChanged>(patterns);
}
}
bool ViewHexEditor::saveToFile(const std::string &path, const std::vector<u8>& data) {
@ -1282,14 +1296,6 @@ R"(
}
});
EventManager::subscribe<EventPatternChanged>(this, [this]() {
this->m_highlightedBytes.clear();
for (const auto &pattern : SharedData::patternData) {
this->m_highlightedBytes.merge(pattern->getHighlightedAddresses());
}
});
EventManager::subscribe<RequestOpenWindow>(this, [this](std::string name) {
if (name == "Create File") {
hex::openFileBrowser("hex.builtin.view.hexeditor.create_file"_lang, DialogMode::Save, { }, [this](auto path) {

View File

@ -7,7 +7,7 @@ namespace hex::plugin::builtin {
ViewPatternData::ViewPatternData() : View("hex.builtin.view.pattern_data.name") {
EventManager::subscribe<EventPatternChanged>(this, [this]() {
EventManager::subscribe<EventPatternChanged>(this, [this](auto&) {
this->m_sortedPatternData.clear();
});
}

View File

@ -661,7 +661,11 @@ namespace hex::plugin::builtin {
this->m_textEditor.SetErrorMarkers({ });
this->m_console.clear();
this->clearPatternData();
EventManager::post<EventPatternChanged>();
{
std::vector<pl::PatternData*> patterns;
EventManager::post<EventPatternChanged>(patterns);
}
std::thread([this, code] {
std::map<std::string, pl::Token::Literal> envVars;
@ -691,7 +695,7 @@ namespace hex::plugin::builtin {
if (result.has_value()) {
SharedData::patternData = std::move(result.value());
EventManager::post<EventPatternChanged>();
EventManager::post<EventPatternChanged>(SharedData::patternData);
}
this->m_evaluatorRunning = false;

View File

@ -95,11 +95,13 @@ namespace hex {
static EventList s_events;
};
namespace pl { class PatternData; }
/* Default Events */
EVENT_DEF(EventFileLoaded, std::string);
EVENT_DEF(EventFileUnloaded);
EVENT_DEF(EventDataChanged);
EVENT_DEF(EventPatternChanged);
EVENT_DEF(EventPatternChanged, std::vector<pl::PatternData*>&);
EVENT_DEF(EventWindowClosing, GLFWwindow*);
EVENT_DEF(EventRegionSelected, Region);
EVENT_DEF(EventProjectFileStore);

View File

@ -15,31 +15,10 @@
namespace hex::pl {
class ASTNode;
class ASTNodeAttribute;
class PatternData;
class Evaluator;
class Attributable {
protected:
Attributable() = default;
Attributable(const Attributable &) = default;
public:
void addAttribute(ASTNodeAttribute *attribute) {
this->m_attributes.push_back(attribute);
}
[[nodiscard]] const auto &getAttributes() const {
return this->m_attributes;
}
private:
std::vector<ASTNodeAttribute *> m_attributes;
};
class ASTNode;
class Clonable {
public:
@ -99,6 +78,33 @@ namespace hex::pl {
std::optional<std::string> m_value;
};
class Attributable {
protected:
Attributable() = default;
~Attributable() {
for (auto &attribute : this->m_attributes)
delete attribute;
}
Attributable(const Attributable &other) {
for (auto &attribute : other.m_attributes)
this->m_attributes.push_back(static_cast<ASTNodeAttribute*>(attribute->clone()));
}
public:
void addAttribute(ASTNodeAttribute *attribute) {
this->m_attributes.push_back(attribute);
}
[[nodiscard]] const auto &getAttributes() const {
return this->m_attributes;
}
private:
std::vector<ASTNodeAttribute *> m_attributes;
};
class ASTNodeLiteral : public ASTNode {
public:
explicit ASTNodeLiteral(Token::Literal literal) : ASTNode(), m_literal(literal) { }
@ -429,7 +435,14 @@ namespace hex::pl {
[[nodiscard]] std::optional<std::endian> getEndian() const { return this->m_endian; }
[[nodiscard]] ASTNode *evaluate(Evaluator *evaluator) const override {
return this->m_type->evaluate(evaluator);
auto type = this->m_type->evaluate(evaluator);
if (auto attributable = dynamic_cast<Attributable*>(type)) {
for (auto &attribute : this->getAttributes())
attributable->addAttribute(static_cast<ASTNodeAttribute*>(attribute->clone()));
}
return type;
}
[[nodiscard]] std::vector<PatternData*> createPatterns(Evaluator *evaluator) const override {
@ -978,7 +991,6 @@ namespace hex::pl {
delete entries.back();
entries.pop_back();
entryIndex--;
};
if (this->m_size != nullptr) {

View File

@ -101,7 +101,7 @@ namespace hex::pl {
virtual PatternData* clone() = 0;
[[nodiscard]] u64 getOffset() const { return this->m_offset; }
void setOffset(u64 offset) { this->m_offset = offset; }
virtual void setOffset(u64 offset) { this->m_offset = offset; }
[[nodiscard]] size_t getSize() const { return this->m_size; }
void setSize(size_t size) { this->m_size = size; }
@ -135,14 +135,21 @@ namespace hex::pl {
virtual void createEntry(prv::Provider* &provider) = 0;
[[nodiscard]] virtual std::string getFormattedName() const = 0;
virtual std::map<u64, u32> getHighlightedAddresses() {
if (this->isHidden()) return { };
[[nodiscard]]
virtual const PatternData* getPattern(u64 offset) const {
if (offset >= this->getOffset() && offset < (this->getOffset() + this->getSize()))
return this;
else
return nullptr;
}
virtual void getHighlightedAddresses(std::map<u64, u32> &highlight) const {
if (this->isHidden()) return;
std::map<u64, u32> result;
for (u64 i = 0; i < this->getSize(); i++)
result.insert({ this->getOffset() + i, this->getColor() });
highlight.insert({ this->getOffset() + i, this->getColor() });
return result;
this->m_evaluator->handleAbort();
}
virtual void sort(ImGuiTableSortSpecs *sortSpecs, prv::Provider *provider) { }
@ -382,14 +389,11 @@ namespace hex::pl {
}
}
std::map<u64, u32> getHighlightedAddresses() override {
auto ownAddresses = PatternData::getHighlightedAddresses();
auto pointedToAddresses = this->m_pointedAt->getHighlightedAddresses();
ownAddresses.merge(pointedToAddresses);
return ownAddresses;
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
PatternData::getHighlightedAddresses(highlight);
this->m_pointedAt->getHighlightedAddresses(highlight);
}
[[nodiscard]] std::string getFormattedName() const override {
std::string result = this->m_pointedAt->getFormattedName() + "* : ";
switch (this->getSize()) {
@ -440,6 +444,14 @@ namespace hex::pl {
this->m_pointerBase = base;
}
[[nodiscard]]
const PatternData* getPattern(u64 offset) const override {
if (offset >= this->getOffset() && offset < (this->getOffset() + this->getSize()))
return this;
else
return this->m_pointedAt->getPattern(offset);
}
private:
PatternData *m_pointedAt = nullptr;
u64 m_pointedAtAddress = 0;
@ -807,20 +819,23 @@ namespace hex::pl {
}
}
std::map<u64, u32> getHighlightedAddresses() override {
std::map<u64, u32> result;
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
for (auto &entry : this->m_entries) {
result.merge(entry->getHighlightedAddresses());
entry->getHighlightedAddresses(highlight);
}
return result;
}
[[nodiscard]] std::string getFormattedName() const override {
return this->m_entries[0]->getTypeName() + "[" + std::to_string(this->m_entries.size()) + "]";
}
void setOffset(u64 offset) override {
for (auto &entry : this->m_entries)
entry->setOffset(entry->getOffset() - this->getOffset() + offset);
PatternData::setOffset(offset);
}
[[nodiscard]] const std::vector<PatternData*>& getEntries() {
return this->m_entries;
}
@ -851,6 +866,18 @@ namespace hex::pl {
return true;
}
[[nodiscard]]
const PatternData* getPattern(u64 offset) const override {
auto iter = std::find_if(this->m_entries.begin(), this->m_entries.end(), [this, offset](PatternData *pattern){
return offset >= this->getOffset() && offset < (this->getOffset() + this->getSize());
});
if (iter == this->m_entries.end())
return nullptr;
else
return *iter;
}
private:
std::vector<PatternData*> m_entries;
u64 m_displayEnd = 50;
@ -868,6 +895,7 @@ namespace hex::pl {
~PatternDataStaticArray() override {
delete this->m_template;
delete this->m_highlightTemplate;
}
PatternData* clone() override {
@ -932,18 +960,21 @@ namespace hex::pl {
}
}
std::map<u64, u32> getHighlightedAddresses() override {
auto startOffset = this->m_template->getOffset();
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
auto entry = this->m_template->clone();
std::map<u64, u32> result;
for (u64 address = this->getOffset(); address < this->getOffset() + this->getSize(); address += this->m_template->getSize()) {
this->m_template->setOffset(address);
result.merge(this->m_template->getHighlightedAddresses());
for (u64 address = this->getOffset(); address < this->getOffset() + this->getSize(); address += entry->getSize()) {
entry->setOffset(address);
entry->getHighlightedAddresses(highlight);
}
this->m_template->setOffset(startOffset);
delete entry;
}
return result;
void setOffset(u64 offset) override {
this->m_template->setOffset(this->m_template->getOffset() - this->getOffset() + offset);
PatternData::setOffset(offset);
}
void setColor(u32 color) override {
@ -969,6 +1000,7 @@ namespace hex::pl {
void setEntries(PatternData* templ, size_t count) {
this->m_template = templ;
this->m_highlightTemplate = this->m_template->clone();
this->m_entryCount = count;
if (this->hasOverriddenColor()) this->setColor(this->m_template->getColor());
@ -983,8 +1015,19 @@ namespace hex::pl {
return *this->m_template == *otherArray.m_template && this->m_entryCount == otherArray.m_entryCount;
}
[[nodiscard]]
const PatternData* getPattern(u64 offset) const override {
if (offset >= this->getOffset() && offset < (this->getOffset() + this->getSize())) {
this->m_highlightTemplate->setOffset((offset / this->m_highlightTemplate->getSize()) * this->m_highlightTemplate->getSize());
return this->m_highlightTemplate;
} else {
return nullptr;
}
}
private:
PatternData *m_template;
mutable PatternData *m_highlightTemplate;
size_t m_entryCount;
u64 m_displayEnd = 50;
};
@ -1039,13 +1082,17 @@ namespace hex::pl {
}
std::map<u64, u32> getHighlightedAddresses() override {
std::map<u64, u32> result;
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
for (auto &member : this->m_members) {
result.merge(member->getHighlightedAddresses());
member->getHighlightedAddresses(highlight);
}
}
return result;
void setOffset(u64 offset) override {
for (auto &member : this->m_members)
member->setOffset(member->getOffset() - this->getOffset() + offset);
PatternData::setOffset(offset);
}
void setColor(u32 color) override {
@ -1101,6 +1148,19 @@ namespace hex::pl {
return true;
}
[[nodiscard]]
const PatternData* getPattern(u64 offset) const override {
auto iter = std::find_if(this->m_members.begin(), this->m_members.end(), [offset](PatternData *pattern){
return offset >= pattern->getOffset() && offset < (pattern->getOffset() + pattern->getSize());
});
if (iter == this->m_members.end())
return nullptr;
else
return *iter;
}
private:
std::vector<PatternData*> m_members;
std::vector<PatternData*> m_sortedMembers;
@ -1158,14 +1218,17 @@ namespace hex::pl {
}
std::map<u64, u32> getHighlightedAddresses() override {
std::map<u64, u32> result;
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
for (auto &member : this->m_members) {
result.merge(member->getHighlightedAddresses());
member->getHighlightedAddresses(highlight);
}
}
return result;
void setOffset(u64 offset) override {
for (auto &member : this->m_members)
member->setOffset(member->getOffset() - this->getOffset() + offset);
PatternData::setOffset(offset);
}
void setColor(u32 color) override {
@ -1220,6 +1283,19 @@ namespace hex::pl {
return true;
}
[[nodiscard]]
const PatternData* getPattern(u64 offset) const override {
auto largestMember = std::find_if(this->m_members.begin(), this->m_members.end(), [this](PatternData *pattern) {
return pattern->getSize() == this->getSize();
});
if (largestMember == this->m_members.end())
return nullptr;
else
return *largestMember;
}
private:
std::vector<PatternData*> m_members;
std::vector<PatternData*> m_sortedMembers;
@ -1451,6 +1527,13 @@ namespace hex::pl {
}
void setOffset(u64 offset) override {
for (auto &field : this->m_fields)
field->setOffset(field->getOffset() - this->getOffset() + offset);
PatternData::setOffset(offset);
}
[[nodiscard]] std::string getFormattedName() const override {
return "bitfield " + PatternData::getTypeName();
}