summary refs log tree commit diff stats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/util/godot_scene.cpp80
-rw-r--r--tools/util/godot_scene.h58
-rw-r--r--tools/validator/godot_processor.cpp16
3 files changed, 40 insertions, 114 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
11namespace { 12namespace {
12 13
13class 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
39struct Heading { 14struct 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
162void GodotNode::AddChild(GodotNode& child) {
163 children_[child.GetName()] = &child;
164 child.parent_ = this;
165}
166
167std::string GodotNode::GetPath() const { 137std::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
175const GodotNode* GodotNode::GetNode(absl::string_view path) const { 145GodotScene 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
192GodotNode* GodotNode::GetNode(absl::string_view path) {
193 return const_cast<GodotNode*>(
194 const_cast<const GodotNode*>(this)->GetNode(path));
195}
196
197std::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
diff --git a/tools/util/godot_scene.h b/tools/util/godot_scene.h index 529e38e..17f3f50 100644 --- a/tools/util/godot_scene.h +++ b/tools/util/godot_scene.h
@@ -1,13 +1,12 @@
1#ifndef TOOLS_UTIL_TSCN_H_ 1#ifndef TOOLS_UTIL_TSCN_H_
2#define TOOLS_UTIL_TSCN_H_ 2#define TOOLS_UTIL_TSCN_H_
3 3
4#include <absl/strings/string_view.h>
5
6#include <map> 4#include <map>
7#include <memory> 5#include <memory>
8#include <string> 6#include <string>
9#include <string_view> 7#include <utility>
10#include <variant> 8#include <variant>
9#include <vector>
11 10
12namespace com::fourisland::lingo2_archipelago { 11namespace com::fourisland::lingo2_archipelago {
13 12
@@ -22,45 +21,36 @@ struct GodotExtResourceRef {
22 21
23using GodotInstanceType = std::variant<std::monostate, GodotExtResourceRef>; 22using GodotInstanceType = std::variant<std::monostate, GodotExtResourceRef>;
24 23
25class GodotNode { 24struct GodotNode {
26 public: 25 std::string name;
27 GodotNode(std::string name, GodotInstanceType instance_type) 26 std::string parent;
28 : name_(std::move(name)), instance_type_(std::move(instance_type)) {} 27 GodotInstanceType instance_type;
29
30 const std::string& GetName() const { return name_; }
31
32 const GodotInstanceType& GetInstanceType() const { return instance_type_; }
33
34 const GodotNode* GetParent() const { return parent_; }
35 GodotNode* GetParent() { return parent_; }
36 28
37 std::string GetPath() const; 29 std::string GetPath() const;
38
39 void AddChild(GodotNode& child);
40
41 const GodotNode* GetNode(absl::string_view path) const;
42 GodotNode* GetNode(absl::string_view path);
43
44 const std::map<std::string, GodotNode*> GetChildren() const {
45 return children_;
46 }
47
48 private:
49 std::string name_;
50 GodotInstanceType instance_type_;
51
52 GodotNode* parent_ = nullptr;
53 std::map<std::string, GodotNode*> children_;
54}; 30};
55 31
56class GodotScene { 32class GodotScene {
57 public: 33 public:
58 virtual const GodotExtResource* GetExtResource( 34 GodotScene(std::map<std::string, GodotExtResource> ext_resources,
59 const std::string& id) const = 0; 35 std::vector<GodotNode> nodes)
60 virtual const GodotNode& GetRoot() const = 0; 36 : ext_resources_(std::move(ext_resources)), nodes_(std::move(nodes)) {}
37
38 const GodotExtResource* GetExtResource(const std::string& id) const {
39 auto it = ext_resources_.find(id);
40 if (it != ext_resources_.end()) {
41 return &it->second;
42 } else {
43 return nullptr;
44 }
45 }
46 const std::vector<GodotNode>& GetNodes() const { return nodes_; }
47
48 private:
49 std::map<std::string, GodotExtResource> ext_resources_;
50 std::vector<GodotNode> nodes_;
61}; 51};
62 52
63std::unique_ptr<GodotScene> ReadGodotSceneFromFile(const std::string& path); 53GodotScene ReadGodotSceneFromFile(const std::string& path);
64 54
65} // namespace com::fourisland::lingo2_archipelago 55} // namespace com::fourisland::lingo2_archipelago
66 56
diff --git a/tools/validator/godot_processor.cpp b/tools/validator/godot_processor.cpp index f345cff..ad2be78 100644 --- a/tools/validator/godot_processor.cpp +++ b/tools/validator/godot_processor.cpp
@@ -35,17 +35,17 @@ class GodotProcessor {
35 std::string scene_path_str = scene_path.string(); 35 std::string scene_path_str = scene_path.string();
36 std::cout << "Processing " << scene_path_str << std::endl; 36 std::cout << "Processing " << scene_path_str << std::endl;
37 37
38 std::unique_ptr<GodotScene> scene = 38 GodotScene scene = ReadGodotSceneFromFile(scene_path_str);
39 ReadGodotSceneFromFile(scene_path_str); 39 for (const GodotNode& node : scene.GetNodes()) {
40 40 ProcessMapNode(scene, node, map_info);
41 ProcessMapNode(*scene, scene->GetRoot(), map_info); 41 }
42 } 42 }
43 43
44 void ProcessMapNode(const GodotScene& scene, const GodotNode& node, 44 void ProcessMapNode(const GodotScene& scene, const GodotNode& node,
45 MapInfo& map_info) { 45 MapInfo& map_info) {
46 if (std::holds_alternative<GodotExtResourceRef>(node.GetInstanceType())) { 46 if (std::holds_alternative<GodotExtResourceRef>(node.instance_type)) {
47 const GodotExtResourceRef& ext_resource_ref = 47 const GodotExtResourceRef& ext_resource_ref =
48 std::get<GodotExtResourceRef>(node.GetInstanceType()); 48 std::get<GodotExtResourceRef>(node.instance_type);
49 const GodotExtResource* ext_resource = 49 const GodotExtResource* ext_resource =
50 scene.GetExtResource(ext_resource_ref.id); 50 scene.GetExtResource(ext_resource_ref.id);
51 51
@@ -55,10 +55,6 @@ class GodotProcessor {
55 map_info.game_nodes[node.GetPath()].defined = true; 55 map_info.game_nodes[node.GetPath()].defined = true;
56 } 56 }
57 } 57 }
58
59 for (const auto& [child_name, child_node] : node.GetChildren()) {
60 ProcessMapNode(scene, *child_node, map_info);
61 }
62 } 58 }
63 59
64 private: 60 private: