2022-04-08 16:08:02 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/task_manager.hpp>
|
2023-07-04 22:18:06 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2023-06-24 16:40:34 +02:00
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
#include <pl/patterns/pattern.hpp>
|
|
|
|
#include <pl/pattern_visitor.hpp>
|
2023-06-05 11:57:26 +02:00
|
|
|
|
|
|
|
#include <pl/formatters.hpp>
|
|
|
|
|
2023-11-18 14:34:33 +01:00
|
|
|
#include <set>
|
|
|
|
|
2023-07-23 09:14:00 +02:00
|
|
|
struct ImGuiTableSortSpecs;
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
namespace hex::plugin::builtin::ui {
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
class PatternDrawer : public pl::PatternVisitor {
|
2022-04-08 16:08:02 +02:00
|
|
|
public:
|
2023-06-05 11:57:26 +02:00
|
|
|
PatternDrawer() {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_formatters = pl::gen::fmt::createFormatters();
|
2023-06-05 11:57:26 +02:00
|
|
|
}
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
virtual ~PatternDrawer() = default;
|
|
|
|
|
2023-12-20 10:08:40 +01:00
|
|
|
void draw(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, pl::PatternLanguage *runtime = nullptr, float height = 0.0F);
|
2022-11-08 21:43:22 +01:00
|
|
|
|
2023-01-08 16:06:26 +01:00
|
|
|
enum class TreeStyle {
|
|
|
|
Default = 0,
|
|
|
|
AutoExpanded = 1,
|
|
|
|
Flattened = 2
|
|
|
|
};
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
void setTreeStyle(TreeStyle style) { m_treeStyle = style; }
|
|
|
|
void setSelectionCallback(std::function<void(Region)> callback) { m_selectionCallback = std::move(callback); }
|
|
|
|
void enableRowColoring(bool enabled) { m_rowColoring = enabled; }
|
2023-01-24 23:27:15 +01:00
|
|
|
void reset();
|
2023-01-08 16:06:26 +01:00
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
private:
|
2022-10-12 14:49:48 +02:00
|
|
|
void draw(pl::ptrn::Pattern& pattern);
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
public:
|
2022-08-06 21:38:09 +02:00
|
|
|
void visit(pl::ptrn::PatternArrayDynamic& pattern) override;
|
|
|
|
void visit(pl::ptrn::PatternArrayStatic& pattern) override;
|
|
|
|
void visit(pl::ptrn::PatternBitfieldField& pattern) override;
|
2023-03-26 11:01:37 +02:00
|
|
|
void visit(pl::ptrn::PatternBitfieldArray& pattern) override;
|
2022-08-06 21:38:09 +02:00
|
|
|
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;
|
2022-04-08 16:08:02 +02:00
|
|
|
|
|
|
|
private:
|
2022-09-19 14:16:23 +02:00
|
|
|
constexpr static auto ChunkSize = 512;
|
|
|
|
constexpr static auto DisplayEndStep = 64;
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2023-06-04 18:30:47 +02:00
|
|
|
void drawArray(pl::ptrn::Pattern& pattern, pl::ptrn::IIterable &iterable, bool isInlined);
|
2022-08-06 21:38:09 +02:00
|
|
|
u64& getDisplayEnd(const pl::ptrn::Pattern& pattern);
|
2022-12-18 23:11:22 +01:00
|
|
|
void makeSelectable(const pl::ptrn::Pattern &pattern);
|
2023-01-25 10:16:31 +01:00
|
|
|
|
2023-06-04 18:30:47 +02:00
|
|
|
void drawValueColumn(pl::ptrn::Pattern& pattern);
|
2023-07-04 22:18:06 +02:00
|
|
|
void drawVisualizer(const std::map<std::string, ContentRegistry::PatternLanguage::impl::Visualizer> &visualizers, const std::vector<pl::core::Token::Literal> &arguments, pl::ptrn::Pattern &pattern, pl::ptrn::IIterable &iterable, bool reset);
|
2023-06-04 18:30:47 +02:00
|
|
|
void drawFavoriteColumn(const pl::ptrn::Pattern& pattern);
|
2023-01-08 16:06:26 +01:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
bool beginPatternTable(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, std::vector<pl::ptrn::Pattern*> &sortedPatterns, float height) const;
|
2023-06-04 18:30:47 +02:00
|
|
|
bool createTreeNode(const pl::ptrn::Pattern& pattern, bool leaf = false);
|
2023-11-10 20:47:08 +01:00
|
|
|
void createDefaultEntry(const pl::ptrn::Pattern &pattern);
|
|
|
|
void closeTreeNode(bool inlined) const;
|
2023-01-08 16:06:26 +01:00
|
|
|
|
2023-07-23 09:14:00 +02:00
|
|
|
bool sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) const;
|
2023-01-28 00:03:20 +01:00
|
|
|
bool isEditingPattern(const pl::ptrn::Pattern& pattern) const;
|
|
|
|
void resetEditing();
|
2023-11-10 20:47:08 +01:00
|
|
|
bool matchesFilter(const std::vector<std::string> &filterPath, const std::vector<std::string> &patternPath, bool fullMatch) const;
|
2023-06-24 16:40:34 +02:00
|
|
|
void traversePatternTree(pl::ptrn::Pattern &pattern, std::vector<std::string> &patternPath, const std::function<void(pl::ptrn::Pattern&)> &callback);
|
2023-07-23 09:14:00 +02:00
|
|
|
std::string getDisplayName(const pl::ptrn::Pattern& pattern) const;
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2023-11-10 14:48:13 +01:00
|
|
|
struct Filter {
|
|
|
|
std::vector<std::string> path;
|
|
|
|
std::optional<pl::core::Token::Literal> value;
|
|
|
|
};
|
|
|
|
|
2023-12-20 10:08:40 +01:00
|
|
|
std::optional<Filter> parseRValueFilter(pl::PatternLanguage *runtime, const std::string &filter) const;
|
2023-11-10 14:48:13 +01:00
|
|
|
|
2022-04-08 16:08:02 +02:00
|
|
|
private:
|
2022-08-06 21:38:09 +02:00
|
|
|
std::map<const pl::ptrn::Pattern*, u64> m_displayEnd;
|
2022-11-08 21:43:22 +01:00
|
|
|
std::vector<pl::ptrn::Pattern*> m_sortedPatterns;
|
2022-12-18 23:11:22 +01:00
|
|
|
|
|
|
|
const pl::ptrn::Pattern *m_editingPattern = nullptr;
|
2023-01-28 00:03:20 +01:00
|
|
|
u64 m_editingPatternOffset = 0;
|
|
|
|
|
2023-01-08 16:06:26 +01:00
|
|
|
TreeStyle m_treeStyle = TreeStyle::Default;
|
2023-11-25 13:42:51 +01:00
|
|
|
bool m_rowColoring = false;
|
2023-01-18 17:17:34 +01:00
|
|
|
pl::ptrn::Pattern *m_currVisualizedPattern = nullptr;
|
2023-01-24 23:27:15 +01:00
|
|
|
|
|
|
|
std::set<pl::ptrn::Pattern*> m_visualizedPatterns;
|
2023-01-25 10:16:31 +01:00
|
|
|
std::string m_lastVisualizerError;
|
2023-02-17 14:53:15 +01:00
|
|
|
|
2023-06-04 16:13:46 +02:00
|
|
|
std::string m_filterText;
|
2023-11-10 14:48:13 +01:00
|
|
|
Filter m_filter;
|
2023-06-04 18:30:47 +02:00
|
|
|
std::vector<std::string> m_currPatternPath;
|
|
|
|
std::map<std::vector<std::string>, std::unique_ptr<pl::ptrn::Pattern>> m_favorites;
|
feat: Added hex::group attribute and various fixes (#1302)
As discussed (many times) on Discord, does the same as the new favorite
tag, but instead allows you to add multiple groups.
Initially, this would cause some insane issues with draw/reset
(apparantly) fighting eachother in the pattern drawer. After a lot of
trial and error, I decided to rewrite the flow that is responsible for
calling reset. Now evaluating patterns is the one to decide when the
reset happens, not the core "game"-loop.
To make sure that draw and reset can never happen at the same time, the
mutex originally used for the favorites has been repurposed. Due to the
restructuring, the mutex in the favorite-task is no longer needed, as
that will only ever kick-off after reset is called and if there are
actually patterns, which can never line up to be accessed on different
threads at the same time.
Last but not least, I noticed that hard crashes could result in your
config file getting overridden. I added a check to prevent that.
Last I issue I can see is that if you use an excessive amount of
favorites/groups, a crash can still happen, but it only happens when you
close the program (occasionally, but unpredictable). Before, this would
happen if you ran the evaluation a second time. I boiled the cause of
the crash down to these lines of code in evaluator.cpp >
patternDestroyed:
```cpp
if (pattern->isPatternLocal()) {
if (auto it = this->m_patternLocalStorage.find(pattern->getHeapAddress()); it != this->m_patternLocalStorage.end()) {
auto &[key, data] = *it;
data.referenceCount--;
if (data.referenceCount == 0)
this->m_patternLocalStorage.erase(it);
} else if (!this->m_evaluated) {
err::E0001.throwError(fmt::format("Double free of variable named '{}'.", pattern->getVariableName()));
}
}
```
Specifically, trying to access the `*it` is the reason for the crash
(this was also the cause of the crashes before my fixes, but then during
evaluation).
I'm suspecting the root cause is somewhere in the `.clone` methods of
the patterns. I'd say that for now a crash when closing the program is
more acceptable than during evaluation (which can even happen if you use
favorites).
2023-09-16 13:09:59 +02:00
|
|
|
std::map<std::string, std::vector<std::unique_ptr<pl::ptrn::Pattern>>> m_groups;
|
2023-06-04 18:30:47 +02:00
|
|
|
bool m_showFavoriteStars = false;
|
|
|
|
bool m_favoritesUpdated = false;
|
2023-07-23 09:14:00 +02:00
|
|
|
bool m_showSpecName = false;
|
|
|
|
|
2023-06-24 16:40:34 +02:00
|
|
|
TaskHolder m_favoritesUpdateTask;
|
2023-06-04 18:30:47 +02:00
|
|
|
|
2023-02-17 14:53:15 +01:00
|
|
|
std::function<void(Region)> m_selectionCallback = [](Region) { };
|
2023-06-05 11:57:26 +02:00
|
|
|
|
|
|
|
pl::gen::fmt::FormatterArray m_formatters;
|
2022-04-08 16:08:02 +02:00
|
|
|
};
|
2022-10-02 17:30:26 +02:00
|
|
|
}
|