diff options
Diffstat (limited to 'tools/util/godot_scene.cpp')
-rw-r--r-- | tools/util/godot_scene.cpp | 80 |
1 files changed, 10 insertions, 70 deletions
diff --git a/tools/util/godot_scene.cpp b/tools/util/godot_scene.cpp index 272111d..1e77c9e 100644 --- a/tools/util/godot_scene.cpp +++ b/tools/util/godot_scene.cpp | |||
@@ -1,6 +1,7 @@ | |||
1 | #include "godot_scene.h" | 1 | #include "godot_scene.h" |
2 | 2 | ||
3 | #include <absl/strings/str_split.h> | 3 | #include <absl/strings/str_split.h> |
4 | #include <absl/strings/string_view.h> | ||
4 | 5 | ||
5 | #include <fstream> | 6 | #include <fstream> |
6 | #include <sstream> | 7 | #include <sstream> |
@@ -10,32 +11,6 @@ namespace com::fourisland::lingo2_archipelago { | |||
10 | 11 | ||
11 | namespace { | 12 | namespace { |
12 | 13 | ||
13 | class GodotSceneImpl : public GodotScene { | ||
14 | public: | ||
15 | GodotSceneImpl(std::map<std::string, GodotExtResource> ext_resources, | ||
16 | std::unique_ptr<GodotNode> root, | ||
17 | std::vector<std::unique_ptr<GodotNode>> descendents) | ||
18 | : ext_resources_(std::move(ext_resources)), | ||
19 | root_(std::move(root)), | ||
20 | descendents_(std::move(descendents)) {} | ||
21 | |||
22 | virtual const GodotExtResource* GetExtResource(const std::string& id) const { | ||
23 | auto it = ext_resources_.find(id); | ||
24 | if (it != ext_resources_.end()) { | ||
25 | return &it->second; | ||
26 | } else { | ||
27 | return nullptr; | ||
28 | } | ||
29 | } | ||
30 | |||
31 | virtual const GodotNode& GetRoot() const { return *root_; } | ||
32 | |||
33 | private: | ||
34 | std::map<std::string, GodotExtResource> ext_resources_; | ||
35 | std::unique_ptr<GodotNode> root_; | ||
36 | std::vector<std::unique_ptr<GodotNode>> descendents_; | ||
37 | }; | ||
38 | |||
39 | struct Heading { | 14 | struct Heading { |
40 | std::string type; | 15 | std::string type; |
41 | 16 | ||
@@ -159,45 +134,17 @@ Heading ParseTscnHeading(absl::string_view line) { | |||
159 | 134 | ||
160 | } // namespace | 135 | } // namespace |
161 | 136 | ||
162 | void GodotNode::AddChild(GodotNode& child) { | ||
163 | children_[child.GetName()] = &child; | ||
164 | child.parent_ = this; | ||
165 | } | ||
166 | |||
167 | std::string GodotNode::GetPath() const { | 137 | std::string GodotNode::GetPath() const { |
168 | if (parent_ == nullptr || parent_->GetName() == "") { | 138 | if (parent.empty() || parent == ".") { |
169 | return name_; | 139 | return name; |
170 | } else { | 140 | } else { |
171 | return parent_->GetPath() + "/" + name_; | 141 | return parent + "/" + name; |
172 | } | 142 | } |
173 | } | 143 | } |
174 | 144 | ||
175 | const GodotNode* GodotNode::GetNode(absl::string_view path) const { | 145 | GodotScene ReadGodotSceneFromFile(const std::string& path) { |
176 | std::vector<std::string> names = absl::StrSplit(path, "/"); | ||
177 | |||
178 | auto it = children_.find(names[0]); | ||
179 | if (it == children_.end()) { | ||
180 | return nullptr; | ||
181 | } else { | ||
182 | if (names.size() == 1) { | ||
183 | return it->second; | ||
184 | } else { | ||
185 | path.remove_prefix(names[0].size() + 1); | ||
186 | |||
187 | return it->second->GetNode(path); | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | GodotNode* GodotNode::GetNode(absl::string_view path) { | ||
193 | return const_cast<GodotNode*>( | ||
194 | const_cast<const GodotNode*>(this)->GetNode(path)); | ||
195 | } | ||
196 | |||
197 | std::unique_ptr<GodotScene> ReadGodotSceneFromFile(const std::string& path) { | ||
198 | std::map<std::string, GodotExtResource> ext_resources; | 146 | std::map<std::string, GodotExtResource> ext_resources; |
199 | auto root = std::make_unique<GodotNode>("", GodotInstanceType{}); | 147 | std::vector<GodotNode> nodes; |
200 | std::vector<std::unique_ptr<GodotNode>> descendents; | ||
201 | 148 | ||
202 | std::ifstream input(path); | 149 | std::ifstream input(path); |
203 | 150 | ||
@@ -235,15 +182,9 @@ std::unique_ptr<GodotScene> ReadGodotSceneFromFile(const std::string& path) { | |||
235 | ext_resources[heading.id] = ext_resource; | 182 | ext_resources[heading.id] = ext_resource; |
236 | } else if (heading.type == "node") { | 183 | } else if (heading.type == "node") { |
237 | if (heading.parent != "") { | 184 | if (heading.parent != "") { |
238 | descendents.push_back( | 185 | nodes.push_back(GodotNode{.name = heading.name, |
239 | std::make_unique<GodotNode>(heading.name, heading.instance_type)); | 186 | .parent = heading.parent, |
240 | GodotNode* child = descendents.back().get(); | 187 | .instance_type = heading.instance_type}); |
241 | |||
242 | if (heading.parent == ".") { | ||
243 | root->AddChild(*child); | ||
244 | } else { | ||
245 | root->GetNode(heading.parent)->AddChild(*child); | ||
246 | } | ||
247 | } | 188 | } |
248 | } else { | 189 | } else { |
249 | cur_heading = heading; | 190 | cur_heading = heading; |
@@ -262,8 +203,7 @@ std::unique_ptr<GodotScene> ReadGodotSceneFromFile(const std::string& path) { | |||
262 | handle_end_of_section(); | 203 | handle_end_of_section(); |
263 | } | 204 | } |
264 | 205 | ||
265 | return std::make_unique<GodotSceneImpl>( | 206 | return GodotScene(std::move(ext_resources), std::move(nodes)); |
266 | std::move(ext_resources), std::move(root), std::move(descendents)); | ||
267 | } | 207 | } |
268 | 208 | ||
269 | } // namespace com::fourisland::lingo2_archipelago | 209 | } // namespace com::fourisland::lingo2_archipelago |