diff options
Diffstat (limited to 'src/godot_variant.cpp')
| -rw-r--r-- | src/godot_variant.cpp | 84 |
1 files changed, 0 insertions, 84 deletions
| diff --git a/src/godot_variant.cpp b/src/godot_variant.cpp deleted file mode 100644 index 152b9ef..0000000 --- a/src/godot_variant.cpp +++ /dev/null | |||
| @@ -1,84 +0,0 @@ | |||
| 1 | // Godot save decoder algorithm by Chris Souvey. | ||
| 2 | |||
| 3 | #include "godot_variant.h" | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <charconv> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <fstream> | ||
| 10 | #include <string> | ||
| 11 | #include <tuple> | ||
| 12 | #include <variant> | ||
| 13 | #include <vector> | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | uint16_t ReadUint16(std::basic_istream<char>& stream) { | ||
| 18 | uint16_t result; | ||
| 19 | stream.read(reinterpret_cast<char*>(&result), 2); | ||
| 20 | return result; | ||
| 21 | } | ||
| 22 | |||
| 23 | uint32_t ReadUint32(std::basic_istream<char>& stream) { | ||
| 24 | uint32_t result; | ||
| 25 | stream.read(reinterpret_cast<char*>(&result), 4); | ||
| 26 | return result; | ||
| 27 | } | ||
| 28 | |||
| 29 | GodotVariant ParseVariant(std::basic_istream<char>& stream) { | ||
| 30 | uint16_t type = ReadUint16(stream); | ||
| 31 | stream.ignore(2); | ||
| 32 | |||
| 33 | switch (type) { | ||
| 34 | case 1: { | ||
| 35 | // bool | ||
| 36 | bool boolval = (ReadUint32(stream) == 1); | ||
| 37 | return {boolval}; | ||
| 38 | } | ||
| 39 | case 15: { | ||
| 40 | // nodepath | ||
| 41 | uint32_t name_length = ReadUint32(stream) & 0x7fffffff; | ||
| 42 | uint32_t subname_length = ReadUint32(stream) & 0x7fffffff; | ||
| 43 | uint32_t flags = ReadUint32(stream); | ||
| 44 | |||
| 45 | std::vector<std::string> result; | ||
| 46 | for (size_t i = 0; i < name_length + subname_length; i++) { | ||
| 47 | uint32_t char_length = ReadUint32(stream); | ||
| 48 | uint32_t padded_length = (char_length % 4 == 0) | ||
| 49 | ? char_length | ||
| 50 | : (char_length + 4 - (char_length % 4)); | ||
| 51 | std::vector<char> next_bytes(padded_length); | ||
| 52 | stream.read(next_bytes.data(), padded_length); | ||
| 53 | std::string next_piece; | ||
| 54 | std::copy(next_bytes.begin(), | ||
| 55 | std::next(next_bytes.begin(), char_length), | ||
| 56 | std::back_inserter(next_piece)); | ||
| 57 | result.push_back(next_piece); | ||
| 58 | } | ||
| 59 | |||
| 60 | return {result}; | ||
| 61 | } | ||
| 62 | case 19: { | ||
| 63 | // array | ||
| 64 | uint32_t length = ReadUint32(stream) & 0x7fffffff; | ||
| 65 | std::vector<GodotVariant> result; | ||
| 66 | for (size_t i = 0; i < length; i++) { | ||
| 67 | result.push_back(ParseVariant(stream)); | ||
| 68 | } | ||
| 69 | return {result}; | ||
| 70 | } | ||
| 71 | default: { | ||
| 72 | // eh | ||
| 73 | return {std::monostate{}}; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | } // namespace | ||
| 79 | |||
| 80 | GodotVariant ParseGodotFile(std::string filename) { | ||
| 81 | std::ifstream file_stream(filename, std::ios_base::binary); | ||
| 82 | file_stream.ignore(4); | ||
| 83 | return ParseVariant(file_stream); | ||
| 84 | } | ||
