From d1024b559c44a143eca214fb8732001080e8b037 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 6 Dec 2024 17:33:40 -0500 Subject: Change output format to zstd compressed Godot serialized variant --- generator/godot_variant.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 generator/godot_variant.cpp (limited to 'generator/godot_variant.cpp') diff --git a/generator/godot_variant.cpp b/generator/godot_variant.cpp new file mode 100644 index 0000000..dd1ad33 --- /dev/null +++ b/generator/godot_variant.cpp @@ -0,0 +1,69 @@ +#include "godot_variant.h" + +#include + +bool GodotVariant::operator<(const GodotVariant& rhs) const { + if (value.index() != rhs.value.index()) { + return value.index() < rhs.value.index(); + } + + if (const int32_t* pval = std::get_if(&value)) { + return *pval < std::get(rhs.value); + } else if (const std::string* pval = std::get_if(&value)) { + return *pval < std::get(rhs.value); + } else if (const std::map* pval = + std::get_if>(&value)) { + return *pval < std::get>(rhs.value); + } else if (const std::vector* pval = + std::get_if>(&value)) { + return *pval < std::get>(rhs.value); + } + + throw std::invalid_argument("Incomparable variants."); +} + +void GodotVariant::Serialize(std::basic_ostream& output) const { + if (const int32_t* pval = std::get_if(&value)) { + uint32_t typebits = 2; + output.write(reinterpret_cast(&typebits), 4); + output.write(reinterpret_cast(pval), 4); + } else if (const std::string* pval = std::get_if(&value)) { + uint32_t typebits = 4; + output.write(reinterpret_cast(&typebits), 4); + + uint32_t lengthbits = pval->length(); + output.write(reinterpret_cast(&lengthbits), 4); + + output.write(pval->data(), pval->length()); + + if (pval->length() % 4 != 0) { + int padding = 4 - (pval->length() % 4); + for (int i = 0; i < padding; i++) { + output.put(0); + } + } + } else if (const std::map* pval = + std::get_if>(&value)) { + uint32_t typebits = 18; + output.write(reinterpret_cast(&typebits), 4); + + uint32_t lengthbits = pval->size() & 0x7fffffff; + output.write(reinterpret_cast(&lengthbits), 4); + + for (const auto& mapping : *pval) { + mapping.first.Serialize(output); + mapping.second.Serialize(output); + } + } else if (const std::vector* pval = + std::get_if>(&value)) { + uint32_t typebits = 19; + output.write(reinterpret_cast(&typebits), 4); + + uint32_t lengthbits = pval->size() & 0x7fffffff; + output.write(reinterpret_cast(&lengthbits), 4); + + for (const auto& entry : *pval) { + entry.Serialize(output); + } + } +} -- cgit 1.4.1