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 <filesystem>
5#include <optional>
6#include <span>
7#include <string>
8#include <string_view>
9#include <variant>
10#include <vector>
11
12namespace kvpp {
13
25
27 static constexpr auto MAGIC_64_BIT = 31337; // Valve this is stupid as hell
28
29 union {
30 uint32_t v32;
31 uint64_t v64;
32 };
33 bool is64Bit;
34
35 explicit operator uint64_t() const noexcept {
36 if (this->is64Bit) {
37 return this->v64;
38 }
39 return this->v32;
40 }
41};
42
43using KV1BinaryValue = std::variant<
44 std::monostate,
45 std::string,
46 int32_t,
47 float,
49 std::wstring,
50 sourcepp::math::Vec4ui8,
51 uint64_t
52>;
53
54template<typename V>
55concept KV1BinaryValueNoChildren = std::same_as<V, std::string>
56 || std::same_as<V, int32_t>
57 || std::same_as<V, float>
58 || std::same_as<V, KV1BinaryPointer>
59 || std::same_as<V, std::wstring>
60 || std::same_as<V, sourcepp::math::Vec4ui8>
61 || std::same_as<V, uint64_t>;
62
64public:
65 KV1BinaryElement() = default;
66
68 [[nodiscard]] std::string_view getKey() const;
69
71 void setKey(std::string_view key_);
72
74 [[nodiscard]] const KV1BinaryValue& getValue() const;
75
77 template<KV1BinaryValueNoChildren V>
78 [[nodiscard]] std::optional<V> getValue() const {
79 if (!std::holds_alternative<V>(this->value)) {
80 return std::nullopt;
81 }
82 return std::get<V>(this->value);
83 }
84
86 void setValue(KV1BinaryValue value_);
87
90
92 template<KV1BinaryValueNoChildren V>
93 void setValue(V value_) {
94 this->value = std::move(value_);
95 }
96
98 template<KV1BinaryValueNoChildren V>
100 this->setValue(std::move(value_));
101 return *this;
102 }
103
105 [[nodiscard]] bool hasChild(std::string_view childKey) const;
106
108 KV1BinaryElement& addChild(std::string_view key_, KV1BinaryValue value_ = {});
109
111 template<KV1BinaryValueNoChildren V>
112 KV1BinaryElement& addChild(std::string_view key_, V value_ = {}) {
113 KV1BinaryElement elem;
114 elem.setKey(key_);
115 elem.setValue(std::move(value_));
116 this->children.push_back(std::move(elem));
117 return this->children.back();
118 }
119
121 [[nodiscard]] uint64_t getChildCount() const;
122
124 [[nodiscard]] uint64_t getChildCount(std::string_view childKey) const;
125
127 [[nodiscard]] const std::vector<KV1BinaryElement>& getChildren() const;
128
130 [[nodiscard]] std::vector<KV1BinaryElement>& getChildren();
131
132 using iterator = std::vector<KV1BinaryElement>::iterator;
133
134 [[nodiscard]] constexpr iterator begin() {
135 return this->children.begin();
136 }
137
138 [[nodiscard]] constexpr iterator end() {
139 return this->children.end();
140 }
141
142 using const_iterator = std::vector<KV1BinaryElement>::const_iterator;
143
144 [[nodiscard]] constexpr const_iterator begin() const {
145 return this->children.begin();
146 }
147
148 [[nodiscard]] constexpr const_iterator end() const {
149 return this->children.end();
150 }
151
152 [[nodiscard]] constexpr const_iterator cbegin() const {
153 return this->children.cbegin();
154 }
155
156 [[nodiscard]] constexpr const_iterator cend() const {
157 return this->children.cend();
158 }
159
161 [[nodiscard]] const KV1BinaryElement& operator[](unsigned int n) const;
162
164 [[nodiscard]] KV1BinaryElement& operator[](unsigned int n);
165
167 [[nodiscard]] const KV1BinaryElement& operator[](std::string_view childKey) const;
168
170 [[nodiscard]] KV1BinaryElement& operator[](std::string_view childKey);
171
173 [[nodiscard]] const KV1BinaryElement& operator()(std::string_view childKey) const;
174
176 [[nodiscard]] KV1BinaryElement& operator()(std::string_view childKey);
177
179 [[nodiscard]] const KV1BinaryElement& operator()(std::string_view childKey, unsigned int n) const;
180
182 [[nodiscard]] KV1BinaryElement& operator()(std::string_view childKey, unsigned int n);
183
185 void removeChild(unsigned int n);
186
188 void removeChild(std::string_view childKey, int n = -1);
189
191 [[nodiscard]] bool isInvalid() const;
192
193 static const KV1BinaryElement& getInvalid();
194
195 [[nodiscard]] explicit operator bool() const;
196
197protected:
198 std::string key;
200 std::vector<KV1BinaryElement> children;
201};
202
204public:
205 explicit KV1Binary(std::span<const std::byte> kv1Data = {}, bool use64BitPointers = true);
206
207 explicit KV1Binary(const std::filesystem::path& kv1Path, bool use64BitPointers = true);
208
209 [[nodiscard]] std::vector<std::byte> bake() const;
210
211 void bake(const std::filesystem::path& kv1Path) const;
212
213 [[nodiscard]] std::string bakeText() const;
214
215 void bakeText(const std::filesystem::path& kv1Path) const;
216};
217
218} // namespace kvpp
const std::vector< KV1BinaryElement > & getChildren() const
Get the child elements of the element.
Definition KV1Binary.cpp:64
constexpr const_iterator cend() const
Definition KV1Binary.h:156
std::vector< KV1BinaryElement > children
Definition KV1Binary.h:200
const KV1BinaryElement & operator()(std::string_view childKey) const
Get the first child element of the element with the given key.
Definition KV1Binary.cpp:88
KV1BinaryElement & operator=(V value_)
Set the value associated with the element.
Definition KV1Binary.h:99
constexpr iterator begin()
Definition KV1Binary.h:134
std::optional< V > getValue() const
Get the value associated with the element as the given type.
Definition KV1Binary.h:78
KV1BinaryElement & addChild(std::string_view key_, KV1BinaryValue value_={})
Add a child element to the element.
Definition KV1Binary.cpp:42
static const KV1BinaryElement & getInvalid()
void removeChild(unsigned int n)
Remove a child element from the element.
bool hasChild(std::string_view childKey) const
Check if the element has one or more children with the given name.
Definition KV1Binary.cpp:38
KV1BinaryValue value
Definition KV1Binary.h:199
constexpr iterator end()
Definition KV1Binary.h:138
void setValue(V value_)
Set the value associated with the element.
Definition KV1Binary.h:93
void setKey(std::string_view key_)
Set the key associated with the element.
Definition KV1Binary.cpp:21
const KV1BinaryValue & getValue() const
Get the value associated with the element.
Definition KV1Binary.cpp:25
KV1BinaryElement & addChild(std::string_view key_, V value_={})
Add a child element to the element.
Definition KV1Binary.h:112
uint64_t getChildCount() const
Get the number of child elements.
Definition KV1Binary.cpp:50
constexpr const_iterator cbegin() const
Definition KV1Binary.h:152
KV1BinaryElement & operator=(KV1BinaryValue value_)
Set the value associated with the element.
Definition KV1Binary.cpp:33
std::vector< KV1BinaryElement >::iterator iterator
Definition KV1Binary.h:132
void setValue(KV1BinaryValue value_)
Set the value associated with the element.
Definition KV1Binary.cpp:29
const KV1BinaryElement & operator[](unsigned int n) const
Get the child element of the element at the given index.
Definition KV1Binary.cpp:72
constexpr const_iterator end() const
Definition KV1Binary.h:148
bool isInvalid() const
Check if the given element is invalid.
std::vector< KV1BinaryElement >::const_iterator const_iterator
Definition KV1Binary.h:142
constexpr const_iterator begin() const
Definition KV1Binary.h:144
std::string_view getKey() const
Get the key associated with the element.
Definition KV1Binary.cpp:17
std::vector< std::byte > bake() const
KV1Binary(std::span< const std::byte > kv1Data={}, bool use64BitPointers=true)
std::string bakeText() const
Definition DMX.h:13
std::variant< std::monostate, std::string, int32_t, float, KV1BinaryPointer, std::wstring, sourcepp::math::Vec4ui8, uint64_t > KV1BinaryValue
Definition KV1Binary.h:43
KV1BinaryValueType
Definition KV1Binary.h:14
static constexpr auto MAGIC_64_BIT
Definition KV1Binary.h:27