10#include <BufferStream.h>
31 this->
value = std::move(value_);
47 this->
children.push_back(std::move(elem));
90 for (
const auto& element : this->
children) {
99 for (
auto& element : this->
children) {
108 unsigned int count = 0;
109 for (
const auto& element : this->
children) {
123 unsigned int count = 0;
124 for (
auto& element: this->
children) {
144 unsigned int count = 0;
145 for (
auto element = this->
children.begin(); element != this->children.end(); ++element) {
147 if (n < 0 || count == n) {
148 element = this->
children.erase(element);
167KV1BinaryElement::operator bool()
const {
173 if (kv1Data.empty()) {
176 BufferStreamReadOnly stream{kv1Data};
178 std::function<
void(std::vector<KV1BinaryElement>&)> recursiveReader;
179 recursiveReader = [use64BitPointers, &stream, &recursiveReader](std::vector<KV1BinaryElement>& elements) {
181 const auto type = stream.read<KV1BinaryValueType>();
182 if (type == KV1BinaryValueType::COUNT) {
185 KV1BinaryElement& element = elements.emplace_back();
186 element.setKey(stream.read_string());
188 using enum KV1BinaryValueType;
190 recursiveReader(element.getChildren());
193 element.setValue(stream.read_string());
196 element.setValue(stream.read<int32_t>());
199 element.setValue(stream.read<float>());
202 const auto data32 = stream.read<uint32_t>();
203 if (use64BitPointers && data32 == KV1BinaryPointer::MAGIC_64_BIT) {
204 const auto data64 = stream.read<uint64_t>();
205 element.setValue(KV1BinaryPointer{
210 element.setValue(KV1BinaryPointer{
218 const auto len = stream.read<uint16_t>();
220 for (int i = 0; i < len; i++) {
221 value += stream.read<char16_t>();
223 element.setValue(std::move(value));
228 value[0] = stream.read<uint8_t>();
229 value[1] = stream.read<uint8_t>();
230 value[2] = stream.read<uint8_t>();
231 value[3] = stream.read<uint8_t>();
232 element.setValue(value);
236 element.setValue(stream.read<uint64_t>());
243 recursiveReader(this->children);
247 :
KV1Binary(
fs::readFileBuffer(kv1Path), use64BitPointers) {}
250 std::vector<std::byte> buffer;
251 BufferStream stream{buffer};
253 std::function<void(
const std::vector<KV1BinaryElement>&)> recursiveWriter;
254 recursiveWriter = [&stream, &recursiveWriter](
const std::vector<KV1BinaryElement>& elements){
255 for (
const auto& element : elements) {
259 .write(element.getKey());
263 recursiveWriter(element.getChildren());
266 stream.write(*element.getValue<std::string>());
269 stream.write(*element.getValue<int32_t>());
272 stream.write(*element.getValue<
float>());
276 if (pointer.is64Bit) {
281 stream.write(pointer.v32);
286 const auto val = *element.getValue<std::wstring>();
288 .write<uint16_t>(val.size() + 1)
289 .write(
reinterpret_cast<const char16_t*
>(val.data()), val.size() *
sizeof(
char16_t) /
sizeof(
wchar_t))
294 const auto val = *element.getValue<math::Vec4ui8>();
295 stream << val[0] << val[1] << val[2] << val[3];
299 stream.write(*element.getValue<uint64_t>());
309 buffer.resize(stream.size());
321 auto& child = kv.addChild(element.
getKey());
325 for (
const auto& elementChild : element.
getChildren()) {
326 recurseBinaryKeyValues(elementChild, child);
330 child = *element.
getValue<std::string>();
333 child = *element.
getValue<int32_t>();
344 value.resize(std::wcstombs(
nullptr, element.
getValue<std::wstring>()->c_str(), 0));
346 child = std::move(
value);
350 const auto val = *element.
getValue<math::Vec4ui8>();
351 child = std::format(
"{} {} {} {}", val[0], val[1], val[2], val[3]);
355 child = std::format(
"{}", *element.
getValue<uint64_t>());
361 recurseBinaryKeyValues(*
this, writer);
362 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={}, bool use64BitPointers=true)
std::string bakeText() const
std::variant< std::monostate, std::string, int32_t, float, KV1BinaryPointer, std::wstring, sourcepp::math::Vec4ui8, uint64_t > KV1BinaryValue
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)
static constexpr auto MAGIC_64_BIT