1 #pragma once
2 
3 #include <hex.hpp>
4 
5 #include <hex/helpers/utils.hpp>
6 
7 #include <functional>
8 #include <map>
9 #include <string>
10 #include <string_view>
11 #include <vector>
12 
13 #include <nlohmann/json.hpp>
14 
15 namespace hex {
16 
17     class View;
18     namespace lang { class ASTNode; }
19     namespace lang { class Evaluator; }
20     namespace dp { class Node; }
21 
22     /*
23         The Content Registry is the heart of all features in ImHex that are in some way extendable by Plugins.
24         It allows you to add/register new content that will be picked up and used by the ImHex core or by other
25         plugins when needed.
26     */
27     struct ContentRegistry {
28         ContentRegistry() = delete;
29 
30         /* Settings Registry. Allows adding of new entries into the ImHex preferences window. */
31         struct Settings {
32             Settings() = delete;
33 
34             struct Entry {
35                 std::string name;
36                 std::function<bool(std::string_view, nlohmann::json&)> callback;
37             };
38 
39             static void load();
40             static void store();
41 
42             static void add(std::string_view unlocalizedCategory, std::string_view unlocalizedName, s64 defaultValue, const std::function<bool(std::string_view, nlohmann::json&)> &callback);
43             static void add(std::string_view unlocalizedCategory, std::string_view unlocalizedName, std::string_view defaultValue, const std::function<bool(std::string_view, nlohmann::json&)> &callback);
44 
45             static void write(std::string_view unlocalizedCategory, std::string_view unlocalizedName, s64 value);
46             static void write(std::string_view unlocalizedCategory, std::string_view unlocalizedName, std::string_view value);
47             static void write(std::string_view unlocalizedCategory, std::string_view unlocalizedName, const std::vector<std::string>& value);
48 
49             static s64 read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, s64 defaultValue);
50             static std::string read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, std::string_view defaultValue);
51             static std::vector<std::string> read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, const std::vector<std::string>& defaultValue = { });
52 
53             static std::map<std::string, std::vector<Entry>>& getEntries();
54             static std::optional<nlohmann::json> getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName);
55             static nlohmann::json& getSettingsData();
56         };
57 
58         /* Events Registry. Allows to define new events that can be used by other plugins later on subscribe to */
59         struct Events {
60             Events() = delete;
61 
62             static auto get(std::string_view name);
63         };
64 
65         /* Command Palette Command Registry. Allows adding of new commands to the command palette */
66         struct CommandPaletteCommands {
67             CommandPaletteCommands() = delete;
68 
69             enum class Type : u32 {
70                 SymbolCommand,
71                 KeywordCommand
72             };
73 
74             struct Entry {
75                 Type type;
76                 std::string command;
77                 std::string unlocalizedDescription;
78                 std::function<std::string(std::string)> displayCallback;
79                 std::function<void(std::string)> executeCallback;
80             };
81 
__anondb0614530102hex::ContentRegistry::CommandPaletteCommands82             static void add(Type type, std::string_view command, std::string_view unlocalizedDescription, const std::function<std::string(std::string)> &displayCallback, const std::function<void(std::string)> &executeCallback = [](auto){});
83             static std::vector<Entry>& getEntries();
84         };
85 
86         /* Pattern Language Function Registry. Allows adding of new functions that may be used inside the pattern language */
87         struct PatternLanguageFunctions {
88             PatternLanguageFunctions() = delete;
89 
90             constexpr static u32 UnlimitedParameters   = 0xFFFF'FFFF;
91             constexpr static u32 MoreParametersThan    = 0x8000'0000;
92             constexpr static u32 LessParametersThan    = 0x4000'0000;
93             constexpr static u32 NoParameters          = 0x0000'0000;
94 
95             struct Function {
96                 u32 parameterCount;
97                 std::function<hex::lang::ASTNode*(hex::lang::Evaluator&, std::vector<hex::lang::ASTNode*>)> func;
98             };
99 
100             static void add(std::string_view name, u32 parameterCount, const std::function<hex::lang::ASTNode*(hex::lang::Evaluator&, std::vector<hex::lang::ASTNode*>)> &func);
101             static std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function>& getEntries();
102         };
103 
104         /* View Registry. Allows adding of new windows */
105         struct Views {
106             Views() = delete;
107 
108             template<hex::derived_from<View> T, typename ... Args>
addhex::ContentRegistry::Views109             static void add(Args&& ... args) {
110                 return add(std::make_unique<T>(std::forward<Args>(args)...));
111             }
112 
113             static std::vector<std::unique_ptr<View>>& getEntries();
114 
115         private:
116             static void add(std::unique_ptr<View> &&view);
117 
118 
119         };
120 
121         /* Tools Registry. Allows adding new entries to the tools window */
122         struct Tools {
123             Tools() = delete;
124 
125             struct Entry {
126                 std::string name;
127                 std::function<void()> function;
128             };
129 
130             static void add(std::string_view unlocalizedName, const std::function<void()> &function);
131 
132             static std::vector<Entry>& getEntries();
133         };
134 
135         /* Data Inspector Registry. Allows adding of new types to the data inspector */
136         struct DataInspector {
137             DataInspector() = delete;
138 
139             enum class NumberDisplayStyle {
140                 Decimal,
141                 Hexadecimal,
142                 Octal
143             };
144 
145             using DisplayFunction = std::function<void()>;
146             using GeneratorFunction = std::function<DisplayFunction(const std::vector<u8>&, std::endian, NumberDisplayStyle)>;
147 
148             struct Entry {
149                 std::string unlocalizedName;
150                 size_t requiredSize;
151                 GeneratorFunction generatorFunction;
152             };
153 
154             static void add(std::string_view unlocalizedName, size_t requiredSize, GeneratorFunction function);
155 
156             static std::vector<Entry>& getEntries();
157         };
158 
159         struct DataProcessorNode {
160             using CreatorFunction = std::function<dp::Node*()>;
161             struct Entry {
162                 std::string category;
163                 std::string name;
164                 CreatorFunction creatorFunction;
165             };
166 
167             template<hex::derived_from<dp::Node> T, typename ... Args>
addhex::ContentRegistry::DataProcessorNode168             static void add(std::string_view unlocalizedCategory, std::string_view unlocalizedName, Args&& ... args) {
169                 add(Entry{ unlocalizedCategory.data(), unlocalizedName.data(), [args...]{ return new T(std::forward<Args>(args)...); } });
170             }
171 
172             static void addSeparator();
173 
174             static std::vector<Entry>& getEntries();
175         private:
176             static void add(const Entry &entry);
177         };
178 
179         struct Language {
180             static void registerLanguage(std::string_view name, std::string_view languageCode);
181             static void addLocalizations(std::string_view languageCode, const LanguageDefinition &definition);
182 
183             static std::map<std::string, std::string>& getLanguages();
184             static std::map<std::string, std::vector<LanguageDefinition>>& getLanguageDefinitions();
185         };
186 
187         struct Interface {
188             using DrawCallback = std::function<void()>;
189 
190             static void addWelcomeScreenEntry(const DrawCallback &function);
191             static void addFooterItem(const DrawCallback &function);
192 
193             static std::vector<DrawCallback>& getWelcomeScreenEntries();
194             static std::vector<DrawCallback>& getFooterItems();
195         };
196     };
197 
198 }