9#include <BufferStream.h>
30 this->
value = std::move(value_);
46 this->
children.push_back(std::move(elem));
89 for (
const auto& element : this->
children) {
98 for (
auto& element : this->
children) {
107 unsigned int count = 0;
108 for (
const auto& element : this->
children) {
122 unsigned int count = 0;
123 for (
auto& element: this->
children) {
143 unsigned int count = 0;
144 for (
auto element = this->
children.begin(); element != this->children.end(); ++element) {
146 if (n < 0 || count == n) {
147 element = this->
children.erase(element);
166KV1BinaryElement::operator bool()
const {
172 if (kv1Data.empty()) {
175 BufferStreamReadOnly stream{kv1Data};
177 std::function<
void(std::vector<KV1BinaryElement>&)> recursiveReader;
178 recursiveReader = [&stream, &recursiveReader](std::vector<KV1BinaryElement>& elements) {
180 const auto type = stream.read<KV1BinaryValueType>();
181 if (type == KV1BinaryValueType::COUNT) {
184 KV1BinaryElement& element = elements.emplace_back();
185 element.setKey(stream.read_string());
187 using enum KV1BinaryValueType;
189 recursiveReader(element.getChildren());
192 element.setValue(stream.read_string());
195 element.setValue(stream.read<int32_t>());
198 element.setValue(stream.read<float>());
201 element.setValue(stream.read<KV1BinaryPointer>());
204 const auto len = stream.read<uint16_t>();
206 for (int i = 0; i < len; i++) {
207 value += stream.read<char16_t>();
209 element.setValue(std::move(value));
214 value[0] = stream.read<uint8_t>();
215 value[1] = stream.read<uint8_t>();
216 value[2] = stream.read<uint8_t>();
217 value[3] = stream.read<uint8_t>();
218 element.setValue(value);
222 element.setValue(stream.read<uint64_t>());
229 recursiveReader(this->children);
236 std::vector<std::byte> buffer;
237 BufferStream stream{buffer};
239 std::function<void(
const std::vector<KV1BinaryElement>&)> recursiveWriter;
240 recursiveWriter = [&stream, &recursiveWriter](
const std::vector<KV1BinaryElement>& elements){
241 for (
const auto& element : elements) {
245 .write(element.getKey());
249 recursiveWriter(element.getChildren());
252 stream.write(*element.getValue<std::string>());
255 stream.write(*element.getValue<int32_t>());
258 stream.write(*element.getValue<
float>());
264 const auto val = *element.getValue<std::wstring>();
266 .write<uint16_t>(val.size() + 1)
267 .write(
reinterpret_cast<const char16_t*
>(val.data()), val.size() *
sizeof(
char16_t) /
sizeof(
wchar_t))
272 const auto val = *element.getValue<math::Vec4ui8>();
273 stream << val[0] << val[1] << val[2] << val[3];
277 stream.write(*element.getValue<uint64_t>());
287 buffer.resize(stream.size());
299 auto& child = kv.addChild(element.
getKey());
303 for (
const auto& elementChild : element.
getChildren()) {
304 recurseBinaryKeyValues(elementChild, child);
308 child = *element.
getValue<std::string>();
311 child = *element.
getValue<int32_t>();
322 const auto val = *element.
getValue<math::Vec4ui8>();
323 child = std::format(
"{} {} {} {}", val[0], val[1], val[2], val[3]);
327 child = std::format(
"{}", *element.
getValue<uint64_t>());
333 recurseBinaryKeyValues(*
this, writer);
334 return writer.
bake();
const std::vector< KV1BinaryElement > & getChildren() const
Get the child elements of the element.
std::vector< KV1BinaryElement > children
const KV1BinaryElement & operator()(std::string_view childKey) const
Get the first child element of the element with the given key.
KV1BinaryElement & addChild(std::string_view key_, KV1BinaryValue value_={})
Add a child element to the element.
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.
void setKey(std::string_view key_)
Set the key associated with the element.
const KV1BinaryValue & getValue() const
Get the value associated with the element.
KV1BinaryElement()=default
uint64_t getChildCount() const
Get the number of child elements.
KV1BinaryElement & operator=(KV1BinaryValue value_)
Set the value associated with the element.
void setValue(KV1BinaryValue value_)
Set the value associated with the element.
const KV1BinaryElement & operator[](unsigned int n) const
Get the child element of the element at the given index.
bool isInvalid() const
Check if the given element is invalid.
std::string_view getKey() const
Get the key associated with the element.
std::vector< std::byte > bake() const
KV1Binary(std::span< const std::byte > kv1Data={})
std::string bakeText() const
std::variant< std::monostate, std::string, int32_t, float, KV1BinaryPointer, std::wstring, sourcepp::math::Vec4ui8, uint64_t > KV1BinaryValue
uint32_t KV1BinaryPointer
bool writeFileText(const std::filesystem::path &filepath, std::string_view text)
bool writeFileBuffer(const std::filesystem::path &filepath, std::span< const std::byte > buffer)
bool iequals(std::string_view s1, std::string_view s2)