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 <stdexcept>
7#include <string>
8#include <string_view>
9#include <unordered_map>
10#include <vector>
11
12#include <BufferStream.h>
13#include <sourcepp/Math.h>
14
15namespace toolpp {
16
17class FGD {
18public:
19 class SyntaxError : public std::runtime_error {
20 public:
21 using std::runtime_error::runtime_error;
22 };
23
24 struct Entity {
26 std::string_view name;
27 std::string_view arguments;
28 };
29
30 struct Field {
31 std::string_view name;
32 std::string_view valueType;
35 std::string_view displayName;
36 std::string_view valueDefault;
37 std::string_view description;
38 };
39
40 struct FieldChoices {
41 struct Choice {
42 std::string_view value;
43 std::string_view displayName;
44 };
45
46 std::string_view name;
49 std::string_view displayName;
50 std::string_view valueDefault;
51 std::string_view description;
52 std::vector<Choice> choices;
53 };
54
55 struct FieldFlags {
56 struct Flag {
57 uint64_t value;
58 std::string_view displayName;
60 std::string_view description;
61 };
62
63 std::string_view name;
66 std::string_view displayName;
67 std::string_view description;
68 std::vector<Flag> flags;
69 };
70
71 struct IO {
72 std::string_view name;
73 std::string_view valueType;
74 std::string_view description;
75 };
76
77 std::string_view classType;
78 std::vector<ClassProperty> classProperties;
79
80 std::string_view description;
81 std::string_view docsURL;
82
83 std::vector<Field> fields;
84 std::vector<FieldChoices> fieldsWithChoices;
85 std::vector<FieldFlags> fieldsWithFlags;
86 std::vector<IO> inputs;
87 std::vector<IO> outputs;
88 };
89
90 struct AutoVisGroup {
91 std::string_view parentName;
92 std::string_view name;
93 std::vector<std::string_view> entities;
94 };
95
96 FGD() = default;
97
98 explicit FGD(const std::filesystem::path& fgdPath);
99
105 void load(const std::filesystem::path& fgdPath);
106
107 [[nodiscard]] int getVersion() const;
108
109 [[nodiscard]] sourcepp::math::Vec2i getMapSize() const;
110
111 [[nodiscard]] const std::unordered_map<std::string_view, Entity>& getEntities() const;
112
113 [[nodiscard]] const std::vector<std::string_view>& getMaterialExclusionDirs() const;
114
115 [[nodiscard]] const std::unordered_map<std::string_view, std::string_view>& getMainKeyValues() const;
116
117 [[nodiscard]] const std::vector<AutoVisGroup>& getAutoVisGroups() const;
118
119protected:
120 void readEntities(BufferStreamReadOnly& stream, const std::filesystem::path& path, std::vector<std::filesystem::path>& seenPaths);
121
122 std::list<std::string> backingData;
123
124 int version = 0;
125 sourcepp::math::Vec2i mapSize{};
126 std::unordered_map<std::string_view, Entity> entities;
127 std::vector<std::string_view> materialExclusionDirs;
128 std::unordered_map<std::string_view, std::string_view> mainKeyValues;
129 std::vector<AutoVisGroup> autoVisGroups;
130};
131
133public:
135 public:
136 explicit AutoVisGroupWriter(FGDWriter& parent_);
137
138 AutoVisGroupWriter& visGroup(std::string_view name, std::initializer_list<std::string_view> entities);
139
140 AutoVisGroupWriter& visGroup(std::string_view name, std::span<const std::string_view> entities);
141
142 FGDWriter& endAutoVisGroup() const; // NOLINT(*-use-nodiscard)
143
144 private:
145 FGDWriter& parent;
146 };
147
149 public:
151 public:
152 explicit KeyValueChoicesWriter(EntityWriter& parent_);
153
154 KeyValueChoicesWriter& choice(std::string_view value, std::string_view displayName);
155
156 EntityWriter& endKeyValueChoices() const; // NOLINT(*-use-nodiscard)
157
158 private:
159 EntityWriter& parent;
160 };
161
163 public:
164 explicit KeyValueFlagsWriter(EntityWriter& parent_);
165
166 KeyValueFlagsWriter& flag(uint64_t value, std::string_view displayName, bool enabledByDefault, std::string_view description = "");
167
168 EntityWriter& endKeyValueFlags() const; // NOLINT(*-use-nodiscard)
169
170 private:
171 EntityWriter& parent;
172 };
173
174 explicit EntityWriter(FGDWriter& parent_);
175
176 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);
177
178 KeyValueChoicesWriter beginKeyValueChoices(std::string_view name, std::string_view displayName = "", std::string_view valueDefault = "", std::string_view description = "", bool readOnly = false, bool report = false);
179
180 KeyValueFlagsWriter beginKeyValueFlags(std::string_view name, std::string_view displayName = "", std::string_view description = "", bool readOnly = false, bool report = false);
181
182 EntityWriter& input(std::string_view name, std::string_view valueType, std::string_view description = "");
183
184 EntityWriter& output(std::string_view name, std::string_view valueType, std::string_view description = "");
185
186 FGDWriter& endEntity() const; // NOLINT(*-use-nodiscard)
187
188 private:
189 FGDWriter& parent;
190 };
191
192 [[nodiscard]] static FGDWriter begin();
193
194 FGDWriter& include(const std::filesystem::path& fgdPath);
195
197
198 FGDWriter& mapSize(sourcepp::math::Vec2i mapSize);
199
200 FGDWriter& materialExclusionDirs(std::initializer_list<std::string_view> dirs);
201
202 FGDWriter& materialExclusionDirs(std::span<const std::string_view> dirs);
203
205
206 FGDWriter& mainKeyValue(std::string_view key, std::string_view value);
207
209
210 AutoVisGroupWriter beginAutoVisGroup(std::string_view parentName);
211
212 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 = "");
213
214 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 = "");
215
216 [[nodiscard]] std::string bake() const;
217
218 bool bake(const std::filesystem::path& fgdPath) const; // NOLINT(*-use-nodiscard)
219
220protected:
221 FGDWriter();
222
223 std::string backingData;
224 BufferStream writer;
225};
226
227} // 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:223
BufferStream writer
Definition FGD.h:224
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:129
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:127
FGD()=default
std::unordered_map< std::string_view, std::string_view > mainKeyValues
Definition FGD.h:128
std::unordered_map< std::string_view, Entity > entities
Definition FGD.h:126
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:125
const std::vector< std::string_view > & getMaterialExclusionDirs() const
Definition FGD.cpp:558
std::list< std::string > backingData
Definition FGD.h:122
int version
Definition FGD.h:124
Definition CmdSeq.h:9
std::vector< std::string_view > entities
Definition FGD.h:93
std::string_view name
Definition FGD.h:92
std::string_view parentName
Definition FGD.h:91
std::string_view name
Definition FGD.h:26
std::string_view arguments
Definition FGD.h:27
std::string_view name
Definition FGD.h:46
std::string_view displayName
Definition FGD.h:49
std::string_view valueDefault
Definition FGD.h:50
std::string_view description
Definition FGD.h:51
std::vector< Choice > choices
Definition FGD.h:52
std::string_view displayName
Definition FGD.h:58
std::string_view description
Definition FGD.h:60
std::vector< Flag > flags
Definition FGD.h:68
std::string_view name
Definition FGD.h:63
std::string_view displayName
Definition FGD.h:66
std::string_view description
Definition FGD.h:67
std::string_view valueDefault
Definition FGD.h:36
std::string_view description
Definition FGD.h:37
std::string_view name
Definition FGD.h:31
std::string_view displayName
Definition FGD.h:35
std::string_view valueType
Definition FGD.h:32
std::string_view name
Definition FGD.h:72
std::string_view valueType
Definition FGD.h:73
std::string_view description
Definition FGD.h:74
std::vector< ClassProperty > classProperties
Definition FGD.h:78
std::vector< IO > inputs
Definition FGD.h:86
std::vector< FieldFlags > fieldsWithFlags
Definition FGD.h:85
std::vector< FieldChoices > fieldsWithChoices
Definition FGD.h:84
std::vector< IO > outputs
Definition FGD.h:87
std::string_view description
Definition FGD.h:80
std::vector< Field > fields
Definition FGD.h:83
std::string_view classType
Definition FGD.h:77
std::string_view docsURL
Definition FGD.h:81