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::vector<AutoVisGroup>& getAutoVisGroups() const;
110
111protected:
112 void readEntities(BufferStreamReadOnly& stream, const std::filesystem::path& path, std::vector<std::filesystem::path>& seenPaths);
113
114 std::list<std::string> backingData;
115
116 int version = 0;
117 sourcepp::math::Vec2i mapSize{};
118 std::unordered_map<std::string_view, Entity> entities;
119 std::vector<std::string_view> materialExclusionDirs;
120 std::vector<AutoVisGroup> autoVisGroups;
121};
122
124public:
126 public:
127 explicit AutoVisGroupWriter(FGDWriter& parent_);
128
129 AutoVisGroupWriter& visGroup(std::string_view name, std::initializer_list<std::string_view> entities);
130
131 AutoVisGroupWriter& visGroup(std::string_view name, std::span<const std::string_view> entities);
132
133 FGDWriter& endAutoVisGroup() const; // NOLINT(*-use-nodiscard)
134
135 private:
136 FGDWriter& parent;
137 };
138
140 public:
142 public:
143 explicit KeyValueChoicesWriter(EntityWriter& parent_);
144
145 KeyValueChoicesWriter& choice(std::string_view value, std::string_view displayName);
146
147 EntityWriter& endKeyValueChoices() const; // NOLINT(*-use-nodiscard)
148
149 private:
150 EntityWriter& parent;
151 };
152
154 public:
155 explicit KeyValueFlagsWriter(EntityWriter& parent_);
156
157 KeyValueFlagsWriter& flag(uint64_t value, std::string_view displayName, bool enabledByDefault, std::string_view description = "");
158
159 EntityWriter& endKeyValueFlags() const; // NOLINT(*-use-nodiscard)
160
161 private:
162 EntityWriter& parent;
163 };
164
165 explicit EntityWriter(FGDWriter& parent_);
166
167 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);
168
169 KeyValueChoicesWriter beginKeyValueChoices(std::string_view name, std::string_view displayName = "", std::string_view valueDefault = "", std::string_view description = "", bool readOnly = false, bool report = false);
170
171 KeyValueFlagsWriter beginKeyValueFlags(std::string_view name, std::string_view displayName = "", std::string_view description = "", bool readOnly = false, bool report = false);
172
173 EntityWriter& input(std::string_view name, std::string_view valueType, std::string_view description = "");
174
175 EntityWriter& output(std::string_view name, std::string_view valueType, std::string_view description = "");
176
177 FGDWriter& endEntity() const; // NOLINT(*-use-nodiscard)
178
179 private:
180 FGDWriter& parent;
181 };
182
183 [[nodiscard]] static FGDWriter begin();
184
185 FGDWriter& include(const std::filesystem::path& fgdPath);
186
188
189 FGDWriter& mapSize(sourcepp::math::Vec2i mapSize);
190
191 FGDWriter& materialExclusionDirs(std::initializer_list<std::string_view> dirs);
192
193 FGDWriter& materialExclusionDirs(std::span<const std::string_view> dirs);
194
195 AutoVisGroupWriter beginAutoVisGroup(std::string_view parentName);
196
197 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 = "");
198
199 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 = "");
200
201 [[nodiscard]] std::string bake() const;
202
203 bool bake(const std::filesystem::path& fgdPath) const; // NOLINT(*-use-nodiscard)
204
205protected:
206 FGDWriter();
207
208 std::string backingData;
209 BufferStream writer;
210};
211
212} // namespace fgdpp
AutoVisGroupWriter(FGDWriter &parent_)
Definition FGD.cpp:602
FGDWriter & endAutoVisGroup() const
Definition FGD.cpp:689
AutoVisGroupWriter & visGroup(std::string_view name, std::initializer_list< std::string_view > entities)
Definition FGD.cpp:670
KeyValueChoicesWriter & choice(std::string_view value, std::string_view displayName)
Definition FGD.cpp:772
KeyValueFlagsWriter & flag(uint64_t value, std::string_view displayName, bool enabledByDefault, std::string_view description="")
Definition FGD.cpp:803
FGDWriter & endEntity() const
Definition FGD.cpp:862
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:756
EntityWriter & output(std::string_view name, std::string_view valueType, std::string_view description="")
Definition FGD.cpp:844
KeyValueFlagsWriter beginKeyValueFlags(std::string_view name, std::string_view displayName="", std::string_view description="", bool readOnly=false, bool report=false)
Definition FGD.cpp:787
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:738
EntityWriter(FGDWriter &parent_)
Definition FGD.cpp:605
EntityWriter & input(std::string_view name, std::string_view valueType, std::string_view description="")
Definition FGD.cpp:826
std::string backingData
Definition FGD.h:208
BufferStream writer
Definition FGD.h:209
static FGDWriter begin()
Definition FGD.cpp:617
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:694
AutoVisGroupWriter beginAutoVisGroup(std::string_view parentName)
Definition FGD.cpp:662
FGDWriter & version(int version)
Definition FGD.cpp:629
FGDWriter & mapSize(sourcepp::math::Vec2i mapSize)
Definition FGD.cpp:637
FGDWriter & include(const std::filesystem::path &fgdPath)
Definition FGD.cpp:621
std::string bake() const
Definition FGD.cpp:867
FGDWriter & materialExclusionDirs(std::initializer_list< std::string_view > dirs)
Definition FGD.cpp:647
std::vector< AutoVisGroup > autoVisGroups
Definition FGD.h:120
void readEntities(BufferStreamReadOnly &stream, const std::filesystem::path &path, std::vector< std::filesystem::path > &seenPaths)
Definition FGD.cpp:544
void load(const std::filesystem::path &fgdPath)
Can be called multiple times in succession to load multiple FGD files.
Definition FGD.cpp:510
sourcepp::math::Vec2i getMapSize() const
Definition FGD.cpp:527
std::vector< std::string_view > materialExclusionDirs
Definition FGD.h:119
FGD()=default
std::unordered_map< std::string_view, Entity > entities
Definition FGD.h:118
const std::vector< AutoVisGroup > & getAutoVisGroups() const
Definition FGD.cpp:539
const std::unordered_map< std::string_view, Entity > & getEntities() const
Definition FGD.cpp:531
int getVersion() const
Definition FGD.cpp:523
sourcepp::math::Vec2i mapSize
Definition FGD.h:117
const std::vector< std::string_view > & getMaterialExclusionDirs() const
Definition FGD.cpp:535
std::list< std::string > backingData
Definition FGD.h:114
int version
Definition FGD.h:116
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