about summary refs log tree commit diff stats
path: root/src/godot_variant.cpp
blob: 152b9ef33fdc7e0d5671376b13fb3a375d965b7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #a
// Godot save decoder algorithm by Chris Souvey.

#include "godot_variant.h"

#include <algorithm>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <string>
#include <tuple>
#include <variant>
#include <vector>

namespace {

uint16_t ReadUint16(std::basic_istream<char>& stream) {
  uint16_t result;
  stream.read(reinterpret_cast<char*>(&result), 2);
  return result;
}

uint32_t ReadUint32(std::basic_istream<char>& stream) {
  uint32_t result;
  stream.read(reinterpret_cast<char*>(&result), 4);
  return result;
}

GodotVariant ParseVariant(std::basic_istream<char>& stream) {
  uint16_t type = ReadUint16(stream);
  stream.ignore(2);

  switch (type) {
    case 1: {
      // bool
      bool boolval = (ReadUint32(stream) == 1);
      return {boolval};
    }
    case 15: {
      // nodepath
      uint32_t name_length = ReadUint32(stream) & 0x7fffffff;
      uint32_t subname_length = ReadUint32(stream) & 0x7fffffff;
      uint32_t flags = ReadUint32(stream);

      std::vector<std::string> result;
      for (size_t i = 0; i < name_length + subname_length; i++) {
        uint32_t char_length = ReadUint32(stream);
        uint32_t padded_length = (char_length % 4 == 0)
                                     ? char_length
                                     : (char_length + 4 - (char_length % 4));
        std::vector<char> next_bytes(padded_length);
        stream.read(next_bytes.data(), padded_length);
        std::string next_piece;
        std::copy(next_bytes.begin(),
                  std::next(next_bytes.begin(), char_length),
                  std::back_inserter(next_piece));
        result.push_back(next_piece);
      }

      return {result};
    }
    case 19: {
      // array
      uint32_t length = ReadUint32(stream) & 0x7fffffff;
      std::vector<GodotVariant> result;
      for (size_t i = 0; i < length; i++) {
        result.push_back(ParseVariant(stream));
      }
      return {result};
    }
    default: {
      // eh
      return {std::monostate{}};
    }
  }
}

}  // namespace

GodotVariant ParseGodotFile(std::string filename) {
  std::ifstream file_stream(filename, std::ios_base::binary);
  file_stream.ignore(4);
  return ParseVariant(file_stream);
}