summary refs log tree commit diff stats
path: root/scene.cpp
blob: f0b40d3f64db06d97aa2a076d1ec8b07835314f6 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string_view>
#include <variant>

#include "heading.h"
#include "scene.h"

Scene::Scene(const std::filesystem::path& path) {
  std::ifstream input(path.c_str());
  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(line);
      if (heading.type() == "gd_scene") {
        file_descriptor_ = heading;
      } else if (heading.type() == "ext_resource") {
        ext_resources_[std::get<int>(heading.GetKeyval("id"))] = heading;
      } 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();
  }

  for (const auto& [id, stuff] : sub_resources_) {
    if (id >= next_sub_id_) {
      next_sub_id_ = id + 1;
    }
  }
}

int Scene::AddSubResource(const Script& script) {
  int new_id = next_sub_id_++;

  std::ostringstream resource_contents;
  resource_contents << "script/source = " << std::quoted(script.ToString());

  std::ostringstream variables_str;
  for (const auto& [variable, value] : script.exports()) {
    variables_str << variable << " = SubResource( " << value << " )"
                  << std::endl;
  }

  Heading heading;
  heading.set_type("sub_resource");
  heading.SetKeyval("type", "GDScript");
  heading.SetKeyval("id", new_id);

  sub_resources_[new_id] = {heading, resource_contents.str(),
                            variables_str.str()};

  return new_id;
}

void Scene::RemoveExtResource(int id) { ext_resources_.erase(id); }

void Scene::ReplaceScriptReferences(int ext_id, int sub_id) {
  auto replace_refs = [&](std::string& text) {
    std::ostringstream search_str;
    search_str << "ExtResource( " << ext_id << " )";
    std::string search = search_str.str();

    bool found_it = false;
    std::ostringstream new_text;
    std::string_view corpus = text;
    int location;
    while ((location = corpus.find(search)) != std::string_view::npos) {
      new_text << corpus.substr(0, location) << "SubResource( " << sub_id
               << " )";
      corpus.remove_prefix(location + search.size());
      found_it = true;
    }
    new_text << corpus;

    if (found_it) {
      const std::string& variables = std::get<2>(sub_resources_[sub_id]);
      new_text << std::endl << variables;
    }

    text = new_text.str();
  };

  for (auto& [id, stuff] : sub_resources_) {
    auto& [heading, text, variables] = stuff;
    replace_refs(text);
  }

  for (auto& [heading, text] : other_) {
    replace_refs(text);
  }
}

std::string Scene::ToString() const {
  std::ostringstream output;
  output << file_descriptor_.ToString() << std::endl;

  for (const auto& [id, heading] : ext_resources_) {
    output << heading.ToString();
  }

  output << std::endl;

  for (const auto& [id, resource] : sub_resources_) {
    const auto& [heading, value, variables] = resource;
    output << heading.ToString() << value << std::endl << std::endl;
  }

  for (const auto& [heading, value] : other_) {
    output << heading.ToString() << value << std::endl << std::endl;
  }

  return output.str();
}