about summary refs log tree commit diff stats
path: root/tools/util/godot_scene.cpp
blob: 1e77c9ebaa49cfa8752b41b1c2737e3d85cd0a0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pre { 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 .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.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: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.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 */
Akismet.api_key = Rails.application.config_for(:akismet)[:api_key]
Akismet.app_url = Rails.application.config_for(:akismet
#include "godot_scene.h"

#include <absl/strings/str_split.h>
#include <absl/strings/string_view.h>

#include <fstream>
#include <sstream>
#include <variant>

namespace com::fourisland::lingo2_archipelago {

namespace {

struct Heading {
  std::string type;

  std::string id;
  std::string path;
  std::string resource_type;

  std::string name;
  std::string parent;
  GodotInstanceType instance_type;
};

Heading ParseTscnHeading(absl::string_view line) {
  std::string original_line(line);
  Heading heading;

  if (line[0] != '[') {
    std::ostringstream errormsg;
    errormsg << "Heading must start with [." << std::endl
             << "Bad heading: " << original_line;
    throw std::invalid_argument(errormsg.str());
  }

  line.remove_prefix(1);
  int divider = line.find_first_of(" ]");
  if (divider == std::string_view::npos) {
    std::ostringstream errormsg;
    errormsg << "Malformatted heading: " << line << std::endl
             << "Original line: " << original_line;
    throw std::invalid_argument(errormsg.str());
  }

  heading.type = std::string(line.substr(0, divider));
  line.remove_prefix(divider + 1);

  while (!line.empty()) {
    divider = line.find_first_of("=");
    if (divider == std::string_view::npos) {
      std::ostringstream errormsg;
      errormsg << "Malformatted heading: " << line << std::endl
               << "Original line: " << original_line;
      throw std::invalid_argument(errormsg.str());
    }

    std::string key(line.substr(0, divider));
    line.remove_prefix(divider + 1);

    if (line[0] == '"') {
      line.remove_prefix(1);
      divider = line.find_first_of("\"");

      if (divider == std::string_view::npos) {
        std::ostringstream errormsg;
        errormsg << "Malformatted heading: " << line << std::endl
                 << "Original line: " << original_line;
        throw std::invalid_argument(errormsg.str());
      }

      std::string strval(line.substr(0, divider));
      line.remove_prefix(divider + 2);

      if (key == "name") {
        heading.name = strval;
      } else if (key == "parent") {
        heading.parent = strval;
      } else if (key == "path") {
        heading.path = strval;
      } else if (key == "type") {
        heading.resource_type = strval;
      } else if (key == "id") {
        heading.id = strval;
      }
    } else if (line[0] == 'S' || line[0] == 'E') {
      GodotInstanceType rrval;
      char internal = line[0];

      line.remove_prefix(13);  // SubResource("
      divider = line.find_first_of("\"");

      if (divider == std::string_view::npos) {
        std::ostringstream errormsg;
        errormsg << "Malformatted heading: " << line << std::endl
                 << "Original line: " << original_line;
        throw std::invalid_argument(errormsg.str());
      }

      std::string refid = std::string(line.substr(0, divider));
      line.remove_prefix(divider + 3);

      GodotInstanceType instance_type;
      if (internal == 'E') {
        instance_type = GodotExtResourceRef{.id = refid};
      } else {
        // SubResource is not supported right now.
      }

      if (key == "instance") {
        heading.instance_type = instance_type;
      } else {
        // Other keys aren't supported right now.
      }
    } else {
      divider = line.find_first_of(" ]");

      if (divider == std::string_view::npos) {
        std::ostringstream errormsg;
        errormsg << "Malformatted heading: " << line << std::endl
                 << "Original line: " << original_line;
        throw std::invalid_argument(errormsg.str());
      }

      int numval = std::atoi(line.substr(0, divider).data());
      line.remove_prefix(divider + 1);

      // keyvals_[key] = numval;
    }
  }

  return heading;
}

}  // namespace

std::string GodotNode::GetPath() const {
  if (parent.empty() || parent == ".") {
    return name;
  } else {
    return parent + "/" + name;
  }
}

GodotScene ReadGodotSceneFromFile(const std::string& path) {
  std::map<std::string, GodotExtResource> ext_resources;
  std::vector<GodotNode> nodes;

  std::ifstream input(path);

  std::string line;
  bool section_started = false;
  Heading cur_heading;
  std::ostringstream cur_value;
  bool value_started = false;
  auto handle_end_of_section = [&]() {
    section_started = false;
    value_started = false;

    if (cur_heading.type == "sub_resource") {
      // sub_resources_[std::get<int>(cur_heading.GetKeyval("id"))] =
      // {cur_heading, cur_value.str(), ""};
    } else {
      // other_.emplace_back(cur_heading, cur_value.str());
    }

    cur_value = {};
  };
  while (std::getline(input, line)) {
    if (section_started && (line.empty() || line[0] == '[')) {
      handle_end_of_section();
    }
    if (!line.empty() && line[0] == '[') {
      Heading heading = ParseTscnHeading(line);
      if (heading.type == "gd_scene") {
        // file_descriptor_ = heading;
      } else if (heading.type == "ext_resource") {
        GodotExtResource ext_resource;
        ext_resource.path = heading.path;
        ext_resource.type = heading.resource_type;

        ext_resources[heading.id] = ext_resource;
      } else if (heading.type == "node") {
        if (heading.parent != "") {
          nodes.push_back(GodotNode{.name = heading.name,
                                    .parent = heading.parent,
                                    .instance_type = heading.instance_type});
        }
      } else {
        cur_heading = heading;
        section_started = true;
      }
    } else if (!line.empty()) {
      if (value_started) {
        cur_value << std::endl;
      } else {
        value_started = true;
      }
      cur_value << line;
    }
  }
  if (section_started) {
    handle_end_of_section();
  }

  return GodotScene(std::move(ext_resources), std::move(nodes));
}

}  // namespace com::fourisland::lingo2_archipelago