SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
FGD.h
Go to the documentation of this file.
1#pragma once
2
3#include <initializer_list>
4#include <list>
5#include <span>
6#include <string>
7#include <string_view>
8#include <unordered_map>
9#include <vector>
10
11#include <BufferStream.h>
12#include <sourcepp/Math.h>
13
14namespace toolpp {
15
16class FGD {
17public:
18 struct Entity {
20 std::string_view name;
21 std::string_view arguments;
22 };
23
24 struct Field {
25 std::string_view name;
26 std::string_view valueType;
29 std::string_view displayName;
30 std::string_view valueDefault;
31 std::string_view description;
32 };
33
34 struct FieldChoices {
35 struct Choice {
36 std::string_view value;
37 std::string_view displayName;
38 };
39
40 std::string_view name;
43 std::string_view displayName;
44 std::string_view valueDefault;
45 std::string_view description;
46 std::vector<Choice> choices;
47 };
48
49 struct FieldFlags {
50 struct Flag {
51 uint64_t value;
52 std::string_view displayName;
54 std::string_view description;
55 };
56
57 std::string_view name;
60 std::string_view displayName;
61 std::string_view description;
62 std::vector<Flag> flags;
63 };
64
65 struct IO {
66 std::string_view name;
67 std::string_view valueType;
68 std::string_view description;
69 };
70
71 std::string_view classType;
72 std::vector<ClassProperty> classProperties;
73
74 std::string_view description;
75 std::string_view docsURL;
76
77 std::vector<Field> fields;
78 std::vector<FieldChoices> fieldsWithChoices;
79 std::vector<FieldFlags> fieldsWithFlags;
80 std::vector<IO> inputs;
81 std::vector<IO> outputs;
82 };
83
84 struct AutoVisGroup {
85 std::string_view parentName;
86 std::string_view name;
87 std::vector<std::string_view> entities;
88 };
89
90 FGD() = default;
91
92 explicit FGD(const std::filesystem::path& fgdPath);
93
99 void load(const std::filesystem::path& fgdPath);
100
101 [[nodiscard]] int getVersion() const;
102
103 [[nodiscard]] sourcepp::math::Vec2i getMapSize() const;
104
105 [[nodiscard]] const std::unordered_map<std::string_view, Entity>& getEntities() const;
106
107 [[nodiscard]] const std::vector<std::string_view>& getMaterialExclusionDirs() const;
108
109 [[nodiscard]] const std::unordered_map<std::string_view, std::string_view>& getMainKeyValues() const;
110
111 [[nodiscard]] const std::vector<AutoVisGroup>& getAutoVisGroups() const;
112
113protected:
114 void readEntities(BufferStreamReadOnly& stream, const std::filesystem::path& path, std::vector<std::filesystem::path>& seenPaths);
115
116 std::list<std::string> backingData;
117
118 int version = 0;
119 sourcepp::math::Vec2i mapSize{};
120 std::unordered_map<std::string_view, Entity> entities;
121 std::vector<std::string_view> materialExclusionDirs;
122 std::unordered_map<std::string_view, std::string_view> mainKeyValues;
123 std::vector<AutoVisGroup> autoVisGroups;
124};
125
127public:
129 public:
130 explicit AutoVisGroupWriter(FGDWriter& parent_);
131
132 AutoVisGroupWriter& visGroup(std::string_view name, std::initializer_list<std::string_view> entities);
133
134 AutoVisGroupWriter& visGroup(std::string_view name, std::span<const std::string_view> entities);
135
136 FGDWriter& endAutoVisGroup() const; // NOLINT(*-use-nodiscard)
137
138 private:
139 FGDWriter& parent;
140 };
141
143 public:
145 public:
146 explicit KeyValueChoicesWriter(EntityWriter& parent_);
147
148 KeyValueChoicesWriter& choice(std::string_view value, std::string_view displayName);
149
150 EntityWriter& endKeyValueChoices() const; // NOLINT(*-use-nodiscard)
151
152 private:
153 EntityWriter& parent;
154 };
155
157 public:
158 explicit KeyValueFlagsWriter(EntityWriter& parent_);
159
160 KeyValueFlagsWriter& flag(uint64_t value, std::string_view displayName, bool enabledByDefault, std::string_view description = "");
161
162 EntityWriter& endKeyValueFlags() const; // NOLINT(*-use-nodiscard)
163
164 private:
165 EntityWriter& parent;
166 };
167
168 explicit EntityWriter(FGDWriter& parent_);
169
170 EntityWriter& keyValue(std::string_view name, std::string_view valueType, std::string_view displayName = "", std::string_view valueDefault = "", std::string_view description = "", bool readOnly = false, bool report = false);
171
172 KeyValueChoicesWriter beginKeyValueChoices(std::string_view name, std::string_view displayName = "", std::string_view valueDefault = "", std::string_view description = "", bool readOnly = false, bool report = false);
173
174 KeyValueFlagsWriter beginKeyValueFlags(std::string_view name, std::string_view displayName = "", std::string_view description = "", bool readOnly = false, bool report = false);
175
176 EntityWriter& input(std::string_view name, std::string_view valueType, std::string_view description = "");
177
178 EntityWriter& output(std::string_view name, std::string_view valueType, std::string_view description = "");
179
180 FGDWriter& endEntity() const; // NOLINT(*-use-nodiscard)
181
182 private:
183 FGDWriter& parent;
184 };
185
186 [[nodiscard]] static FGDWriter begin();
187
188 FGDWriter& include(const std::filesystem::path& fgdPath);
189
191
192 FGDWriter& mapSize(sourcepp::math::Vec2i mapSize);
193
194 FGDWriter& materialExclusionDirs(std::initializer_list<std::string_view> dirs);
195
196 FGDWriter& materialExclusionDirs(std::span<const std::string_view> dirs);
197
199
200 FGDWriter& mainKeyValue(std::string_view key, std::string_view value);
201
203
204 AutoVisGroupWriter beginAutoVisGroup(std::string_view parentName);
205
206 EntityWriter beginEntity(std::string_view classType, std::initializer_list<std::string_view> classProperties, std::string_view name, std::string_view description = "", std::string_view docsURL = "");
207
208 EntityWriter beginEntity(std::string_view classType, std::span<const std::string_view> classProperties, std::string_view name, std::string_view description = "", std::string_view docsURL = "");
209
210 [[nodiscard]] std::string bake() const;
211
212 bool bake(const std::filesystem::path& fgdPath) const; // NOLINT(*-use-nodiscard)
213
214protected:
215 FGDWriter();
216
217 std::string backingData;
218 BufferStream writer;
219};
220
221} // namespace fgdpp
AutoVisGroupWriter(FGDWriter &parent_)
Definition FGD.cpp:631
FGDWriter & endAutoVisGroup() const
Definition FGD.cpp:729
AutoVisGroupWriter & visGroup(std::string_view name, std::initializer_list< std::string_view > entities)
Definition FGD.cpp:710
KeyValueChoicesWriter & choice(std::string_view value, std::string_view displayName)
Definition FGD.cpp:812
KeyValueFlagsWriter & flag(uint64_t value, std::string_view displayName, bool enabledByDefault, std::string_view description="")
Definition FGD.cpp:843
FGDWriter & endEntity() const
Definition FGD.cpp:893
KeyValueChoicesWriter beginKeyValueChoices(std::string_view name, std::string_view displayName="", std::string_view valueDefault="", std::string_view description="", bool readOnly=false, bool report=false)
Definition FGD.cpp:796
EntityWriter & output(std::string_view name, std::string_view valueType, std::string_view description="")
Definition FGD.cpp:875
KeyValueFlagsWriter beginKeyValueFlags(std::string_view name, std::string_view displayName="", std::string_view description="", bool readOnly=false, bool report=false)
Definition FGD.cpp:827
EntityWriter & keyValue(std::string_view name, std::string_view valueType, std::string_view displayName="", std::string_view valueDefault="", std::string_view description="", bool readOnly=false, bool report=false)
Definition FGD.cpp:778
EntityWriter(FGDWriter &parent_)
Definition FGD.cpp:634
EntityWriter & input(std::string_view name, std::string_view valueType, std::string_view description="")
Definition FGD.cpp:857
std::string backingData
Definition FGD.h:217
BufferStream writer
Definition FGD.h:218
static FGDWriter begin()
Definition FGD.cpp:646
EntityWriter beginEntity(std::string_view classType, std::initializer_list< std::string_view > classProperties, std::string_view name, std::string_view description="", std::string_view docsURL="")
Definition FGD.cpp:734
FGDWriter & beginMainKeyValues()
Definition FGD.cpp:683
AutoVisGroupWriter beginAutoVisGroup(std::string_view parentName)
Definition FGD.cpp:702
FGDWriter & version(int version)
Definition FGD.cpp:658
FGDWriter & mapSize(sourcepp::math::Vec2i mapSize)
Definition FGD.cpp:663
FGDWriter & endMainKeyValues()
Definition FGD.cpp:697
FGDWriter & mainKeyValue(std::string_view key, std::string_view value)
Definition FGD.cpp:688
FGDWriter & include(const std::filesystem::path &fgdPath)
Definition FGD.cpp:650
std::string bake() const
Definition FGD.cpp:898
FGDWriter & materialExclusionDirs(std::initializer_list< std::string_view > dirs)
Definition FGD.cpp:668
std::vector< AutoVisGroup > autoVisGroups
Definition FGD.h:123
void readEntities(BufferStreamReadOnly &stream, const std::filesystem::path &path, std::vector< std::filesystem::path > &seenPaths)
Definition FGD.cpp:571
void load(const std::filesystem::path &fgdPath)
Can be called multiple times in succession to load multiple FGD files.
Definition FGD.cpp:533
sourcepp::math::Vec2i getMapSize() const
Definition FGD.cpp:550
std::vector< std::string_view > materialExclusionDirs
Definition FGD.h:121
FGD()=default
std::unordered_map< std::string_view, std::string_view > mainKeyValues
Definition FGD.h:122
std::unordered_map< std::string_view, Entity > entities
Definition FGD.h:120
const std::unordered_map< std::string_view, std::string_view > & getMainKeyValues() const
Definition FGD.cpp:562
const std::vector< AutoVisGroup > & getAutoVisGroups() const
Definition FGD.cpp:566
const std::unordered_map< std::string_view, Entity > & getEntities() const
Definition FGD.cpp:554
int getVersion() const
Definition FGD.cpp:546
sourcepp::math::Vec2i mapSize
Definition FGD.h:119
const std::vector< std::string_view > & getMaterialExclusionDirs() const
Definition FGD.cpp:558
std::list< std::string > backingData
Definition FGD.h:116
int version
Definition FGD.h:118
Definition CmdSeq.h:9
std::vector< std::string_view > entities
Definition FGD.h:87
std::string_view name
Definition FGD.h:86
std::string_view parentName
Definition FGD.h:85
std::string_view name
Definition FGD.h:20
std::string_view arguments
Definition FGD.h:21
std::string_view name
Definition FGD.h:40
std::string_view displayName
Definition FGD.h:43
std::string_view valueDefault
Definition FGD.h:44
std::string_view description
Definition FGD.h:45
std::vector< Choice > choices
Definition FGD.h:46
std::string_view displayName
Definition FGD.h:52
std::string_view description
Definition FGD.h:54
std::vector< Flag > flags
Definition FGD.h:62
std::string_view name
Definition FGD.h:57
std::string_view displayName
Definition FGD.h:60
std::string_view description
Definition FGD.h:61
std::string_view valueDefault
Definition FGD.h:30
std::string_view description
Definition FGD.h:31
std::string_view name
Definition FGD.h:25
std::string_view displayName
Definition FGD.h:29
std::string_view valueType
Definition FGD.h:26
std::string_view name
Definition FGD.h:66
std::string_view valueType
Definition FGD.h:67
std::string_view description
Definition FGD.h:68
std::vector< ClassProperty > classProperties
Definition FGD.h:72
std::vector< IO > inputs
Definition FGD.h:80
std::vector< FieldFlags > fieldsWithFlags
Definition FGD.h:79
std::vector< FieldChoices > fieldsWithChoices
Definition FGD.h:78
std::vector< IO > outputs
Definition FGD.h:81
std::string_view description
Definition FGD.h:74
std::vector< Field > fields
Definition FGD.h:77
std::string_view classType
Definition FGD.h:71
std::string_view docsURL
Definition FGD.h:75