SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
KV1Binary.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <optional>
5#include <span>
6#include <string>
7#include <string_view>
8#include <variant>
9#include <vector>
10
11#include <BufferStream.h>
13
14namespace kvpp {
15
16enum class KV1BinaryValueType : uint8_t {
18 STRING,
19 INT32,
20 FLOAT,
21 POINTER,
22 WSTRING,
24 UINT64,
25 COUNT,
26};
27
28using KV1BinaryValue = std::variant<
29 std::monostate,
30 std::string,
31 int32_t,
32 float,
33 void*,
34 std::wstring,
35 sourcepp::math::Vec4ui8,
36 uint64_t
37>;
38
39template<typename V>
40concept KV1BinaryValueNoChildren = std::same_as<V, std::string>
41 || std::same_as<V, int32_t>
42 || std::same_as<V, float>
43 || std::same_as<V, void*>
44 || std::same_as<V, std::wstring>
45 || std::same_as<V, sourcepp::math::Vec4ui8>
46 || std::same_as<V, uint64_t>;
47
49public:
50 KV1BinaryElement() = default;
51
53 [[nodiscard]] std::string_view getKey() const;
54
56 void setKey(std::string_view key_);
57
59 [[nodiscard]] const KV1BinaryValue& getValue() const;
60
62 template<KV1BinaryValueNoChildren V>
63 [[nodiscard]] std::optional<V> getValue() const {
64 if (!std::holds_alternative<V>(this->value)) {
65 return std::nullopt;
66 }
67 return std::get<V>(this->value);
68 }
69
71 void setValue(KV1BinaryValue value_);
72
75
77 template<KV1BinaryValueNoChildren V>
78 void setValue(V value_) {
79 this->value = std::move(value_);
80 }
81
83 template<KV1BinaryValueNoChildren V>
85 this->setValue(std::move(value_));
86 return *this;
87 }
88
90 [[nodiscard]] bool hasChild(std::string_view childKey) const;
91
93 [[nodiscard]] uint64_t getChildCount() const;
94
96 [[nodiscard]] uint64_t getChildCount(std::string_view childKey) const;
97
99 [[nodiscard]] const std::vector<KV1BinaryElement>& getChildren() const;
100
102 KV1BinaryElement& addChild(std::string_view key_);
103
105 template<KV1BinaryValueNoChildren V>
106 KV1BinaryElement& addChild(std::string_view key_, V value_ = {}) {
107 KV1BinaryElement elem;
108 elem.setKey(key_);
109 elem.setValue(std::move(value_));
110 this->children.push_back(elem);
111 return this->children.back();
112 }
113
115 [[nodiscard]] const KV1BinaryElement& operator[](unsigned int n) const;
116
118 [[nodiscard]] KV1BinaryElement& operator[](unsigned int n);
119
121 [[nodiscard]] const KV1BinaryElement& operator[](std::string_view childKey) const;
122
124 [[nodiscard]] KV1BinaryElement& operator[](std::string_view childKey);
125
127 [[nodiscard]] const KV1BinaryElement& operator()(std::string_view childKey) const;
128
130 [[nodiscard]] KV1BinaryElement& operator()(std::string_view childKey);
131
133 [[nodiscard]] const KV1BinaryElement& operator()(std::string_view childKey, unsigned int n) const;
134
136 [[nodiscard]] KV1BinaryElement& operator()(std::string_view childKey, unsigned int n);
137
139 void removeChild(unsigned int n);
140
142 void removeChild(std::string_view childKey, int n = -1);
143
144 using iterator = std::vector<KV1BinaryElement>::iterator;
145
146 [[nodiscard]] constexpr iterator begin() {
147 return this->children.begin();
148 }
149
150 [[nodiscard]] constexpr iterator end() {
151 return this->children.end();
152 }
153
154 using const_iterator = std::vector<KV1BinaryElement>::const_iterator;
155
156 [[nodiscard]] constexpr const_iterator begin() const {
157 return this->children.begin();
158 }
159
160 [[nodiscard]] constexpr const_iterator end() const {
161 return this->children.end();
162 }
163
164 [[nodiscard]] constexpr const_iterator cbegin() const {
165 return this->children.cbegin();
166 }
167
168 [[nodiscard]] constexpr const_iterator cend() const {
169 return this->children.cend();
170 }
171
173 [[nodiscard]] bool isInvalid() const;
174
175 static const KV1BinaryElement& getInvalid();
176
177protected:
178 // NOLINTNEXTLINE(*-no-recursion)
179 static void read(BufferStreamReadOnly& stream, std::vector<KV1BinaryElement>& elements, const sourcepp::parser::text::EscapeSequenceMap& escapeSequences);
180
181 static void write(BufferStream& stream, const std::vector<KV1BinaryElement>& elements, const sourcepp::parser::text::EscapeSequenceMap& escapeSequences);
182
183 std::string key;
185 std::vector<KV1BinaryElement> children;
186};
187
189public:
190 explicit KV1Binary(std::span<const std::byte> kv1Data = {}, bool useEscapeSequences_ = false);
191
192 [[nodiscard]] std::vector<std::byte> bake() const;
193
194 void bake(const std::string& kv1Path) const;
195
196 [[nodiscard]] std::string bakeText() const;
197
198 void bakeText(const std::string& kv1Path) const;
199
200protected:
202};
203
204} // namespace kvpp
const std::vector< KV1BinaryElement > & getChildren() const
Get the child elements of the element.
Definition: KV1Binary.cpp:51
KV1BinaryElement & addChild(std::string_view key_)
Add a child element to the element.
Definition: KV1Binary.cpp:55
static void read(BufferStreamReadOnly &stream, std::vector< KV1BinaryElement > &elements, const sourcepp::parser::text::EscapeSequenceMap &escapeSequences)
Definition: KV1Binary.cpp:152
constexpr const_iterator cend() const
Definition: KV1Binary.h:168
std::vector< KV1BinaryElement > children
Definition: KV1Binary.h:185
const KV1BinaryElement & operator()(std::string_view childKey) const
Get the first child element of the element with the given key.
Definition: KV1Binary.cpp:78
KV1BinaryElement & operator=(V value_)
Set the value associated with the element.
Definition: KV1Binary.h:84
constexpr iterator begin()
Definition: KV1Binary.h:146
std::optional< V > getValue() const
Get the value associated with the element as the given type.
Definition: KV1Binary.h:63
static const KV1BinaryElement & getInvalid()
Definition: KV1Binary.cpp:147
void removeChild(unsigned int n)
Remove a child element from the element.
Definition: KV1Binary.cpp:122
bool hasChild(std::string_view childKey) const
Check if the element has one or more children with the given name.
Definition: KV1Binary.cpp:33
KV1BinaryValue value
Definition: KV1Binary.h:184
constexpr iterator end()
Definition: KV1Binary.h:150
void setValue(V value_)
Set the value associated with the element.
Definition: KV1Binary.h:78
void setKey(std::string_view key_)
Set the key associated with the element.
Definition: KV1Binary.cpp:16
const KV1BinaryValue & getValue() const
Get the value associated with the element.
Definition: KV1Binary.cpp:20
KV1BinaryElement & addChild(std::string_view key_, V value_={})
Add a child element to the element.
Definition: KV1Binary.h:106
uint64_t getChildCount() const
Get the number of child elements.
Definition: KV1Binary.cpp:37
constexpr const_iterator cbegin() const
Definition: KV1Binary.h:164
KV1BinaryElement & operator=(KV1BinaryValue value_)
Set the value associated with the element.
Definition: KV1Binary.cpp:28
std::vector< KV1BinaryElement >::iterator iterator
Definition: KV1Binary.h:144
void setValue(KV1BinaryValue value_)
Set the value associated with the element.
Definition: KV1Binary.cpp:24
const KV1BinaryElement & operator[](unsigned int n) const
Get the child element of the element at the given index.
Definition: KV1Binary.cpp:62
constexpr const_iterator end() const
Definition: KV1Binary.h:160
bool isInvalid() const
Check if the given element is invalid.
Definition: KV1Binary.cpp:143
std::vector< KV1BinaryElement >::const_iterator const_iterator
Definition: KV1Binary.h:154
constexpr const_iterator begin() const
Definition: KV1Binary.h:156
static void write(BufferStream &stream, const std::vector< KV1BinaryElement > &elements, const sourcepp::parser::text::EscapeSequenceMap &escapeSequences)
Definition: KV1Binary.cpp:204
std::string_view getKey() const
Get the key associated with the element.
Definition: KV1Binary.cpp:12
std::vector< std::byte > bake() const
Definition: KV1Binary.cpp:260
bool useEscapeSequences
Definition: KV1Binary.h:201
std::string bakeText() const
Definition: KV1Binary.cpp:272
Definition: KV1.h:13
KV1BinaryValueType
Definition: KV1Binary.h:16
std::variant< std::monostate, std::string, int32_t, float, void *, std::wstring, sourcepp::math::Vec4ui8, uint64_t > KV1BinaryValue
Definition: KV1Binary.h:37
std::unordered_map< char, char > EscapeSequenceMap
Definition: Text.h:17