about summary refs log tree commit diff stats
path: root/.gitignore
blob: a7cadc7da8f501ee355f89fcdb69d6bfcb8c287e (plain) (blame)
1
2
3
4
5
build/
builds/
assets/LL1.yaml
.DS_Store
.vs
*/ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
// Godot save decoder algorithm by Chris Souvey.

#include "godot_variant.h"

#include <algorithm>
#include <charconv>
#include <cstddef>
#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);
}