From c9139577e690baff59da1b1edf13ef209c00ee40 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 18 Aug 2025 18:50:04 -0400 Subject: Added the_repetitive --- tools/util/godot_scene.cpp | 80 ++++++---------------------------------------- tools/util/godot_scene.h | 58 ++++++++++++++------------------- 2 files changed, 34 insertions(+), 104 deletions(-) (limited to 'tools/util') 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 @@ #include "godot_scene.h" #include +#include #include #include @@ -10,32 +11,6 @@ namespace com::fourisland::lingo2_archipelago { namespace { -class GodotSceneImpl : public GodotScene { - public: - GodotSceneImpl(std::map ext_resources, - std::unique_ptr root, - std::vector> descendents) - : ext_resources_(std::move(ext_resources)), - root_(std::move(root)), - descendents_(std::move(descendents)) {} - - virtual const GodotExtResource* GetExtResource(const std::string& id) const { - auto it = ext_resources_.find(id); - if (it != ext_resources_.end()) { - return &it->second; - } else { - return nullptr; - } - } - - virtual const GodotNode& GetRoot() const { return *root_; } - - private: - std::map ext_resources_; - std::unique_ptr root_; - std::vector> descendents_; -}; - struct Heading { std::string type; @@ -159,45 +134,17 @@ Heading ParseTscnHeading(absl::string_view line) { } // namespace -void GodotNode::AddChild(GodotNode& child) { - children_[child.GetName()] = &child; - child.parent_ = this; -} - std::string GodotNode::GetPath() const { - if (parent_ == nullptr || parent_->GetName() == "") { - return name_; + if (parent.empty() || parent == ".") { + return name; } else { - return parent_->GetPath() + "/" + name_; + return parent + "/" + name; } } -const GodotNode* GodotNode::GetNode(absl::string_view path) const { - std::vector names = absl::StrSplit(path, "/"); - - auto it = children_.find(names[0]); - if (it == children_.end()) { - return nullptr; - } else { - if (names.size() == 1) { - return it->second; - } else { - path.remove_prefix(names[0].size() + 1); - - return it->second->GetNode(path); - } - } -} - -GodotNode* GodotNode::GetNode(absl::string_view path) { - return const_cast( - const_cast(this)->GetNode(path)); -} - -std::unique_ptr ReadGodotSceneFromFile(const std::string& path) { +GodotScene ReadGodotSceneFromFile(const std::string& path) { std::map ext_resources; - auto root = std::make_unique("", GodotInstanceType{}); - std::vector> descendents; + std::vector nodes; std::ifstream input(path); @@ -235,15 +182,9 @@ std::unique_ptr ReadGodotSceneFromFile(const std::string& path) { ext_resources[heading.id] = ext_resource; } else if (heading.type == "node") { if (heading.parent != "") { - descendents.push_back( - std::make_unique(heading.name, heading.instance_type)); - GodotNode* child = descendents.back().get(); - - if (heading.parent == ".") { - root->AddChild(*child); - } else { - root->GetNode(heading.parent)->AddChild(*child); - } + nodes.push_back(GodotNode{.name = heading.name, + .parent = heading.parent, + .instance_type = heading.instance_type}); } } else { cur_heading = heading; @@ -262,8 +203,7 @@ std::unique_ptr ReadGodotSceneFromFile(const std::string& path) { handle_end_of_section(); } - return std::make_unique( - std::move(ext_resources), std::move(root), std::move(descendents)); + return GodotScene(std::move(ext_resources), std::move(nodes)); } } // 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 @@ #ifndef TOOLS_UTIL_TSCN_H_ #define TOOLS_UTIL_TSCN_H_ -#include - #include #include #include -#include +#include #include +#include namespace com::fourisland::lingo2_archipelago { @@ -22,45 +21,36 @@ struct GodotExtResourceRef { using GodotInstanceType = std::variant; -class GodotNode { - public: - GodotNode(std::string name, GodotInstanceType instance_type) - : name_(std::move(name)), instance_type_(std::move(instance_type)) {} - - const std::string& GetName() const { return name_; } - - const GodotInstanceType& GetInstanceType() const { return instance_type_; } - - const GodotNode* GetParent() const { return parent_; } - GodotNode* GetParent() { return parent_; } +struct GodotNode { + std::string name; + std::string parent; + GodotInstanceType instance_type; std::string GetPath() const; - - void AddChild(GodotNode& child); - - const GodotNode* GetNode(absl::string_view path) const; - GodotNode* GetNode(absl::string_view path); - - const std::map GetChildren() const { - return children_; - } - - private: - std::string name_; - GodotInstanceType instance_type_; - - GodotNode* parent_ = nullptr; - std::map children_; }; class GodotScene { public: - virtual const GodotExtResource* GetExtResource( - const std::string& id) const = 0; - virtual const GodotNode& GetRoot() const = 0; + GodotScene(std::map ext_resources, + std::vector nodes) + : ext_resources_(std::move(ext_resources)), nodes_(std::move(nodes)) {} + + const GodotExtResource* GetExtResource(const std::string& id) const { + auto it = ext_resources_.find(id); + if (it != ext_resources_.end()) { + return &it->second; + } else { + return nullptr; + } + } + const std::vector& GetNodes() const { return nodes_; } + + private: + std::map ext_resources_; + std::vector nodes_; }; -std::unique_ptr ReadGodotSceneFromFile(const std::string& path); +GodotScene ReadGodotSceneFromFile(const std::string& path); } // namespace com::fourisland::lingo2_archipelago -- cgit 1.4.1