summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2024-03-04 05:52:40 +0000
committerStar Rauchenberger <fefferburbia@gmail.com>2024-03-04 05:52:40 +0000
commit86c6f803ffb651df20509d4e1f1602352b88bd41 (patch)
treee9b9573e8065868280497b671ee4583fc8c9c8f6
parent4e206fae5be6093b36c7779ba2a4a3679f0e754b (diff)
downloadgodot3-tscn-compactor-master.tar.gz
godot3-tscn-compactor-master.tar.bz2
godot3-tscn-compactor-master.zip
something 2 HEAD master
-rw-r--r--scene.cpp24
-rw-r--r--scene.h2
2 files changed, 18 insertions, 8 deletions
diff --git a/scene.cpp b/scene.cpp index 6eae537..f0b40d3 100644 --- a/scene.cpp +++ b/scene.cpp
@@ -24,7 +24,7 @@ Scene::Scene(const std::filesystem::path& path) {
24 24
25 if (cur_heading.type() == "sub_resource") { 25 if (cur_heading.type() == "sub_resource") {
26 sub_resources_[std::get<int>(cur_heading.GetKeyval("id"))] = { 26 sub_resources_[std::get<int>(cur_heading.GetKeyval("id"))] = {
27 cur_heading, cur_value.str()}; 27 cur_heading, cur_value.str(), ""};
28 } else { 28 } else {
29 other_.emplace_back(cur_heading, cur_value.str()); 29 other_.emplace_back(cur_heading, cur_value.str());
30 } 30 }
@@ -69,18 +69,21 @@ int Scene::AddSubResource(const Script& script) {
69 int new_id = next_sub_id_++; 69 int new_id = next_sub_id_++;
70 70
71 std::ostringstream resource_contents; 71 std::ostringstream resource_contents;
72 resource_contents << "script/source = " << std::quoted(script.ToString());
73
74 std::ostringstream variables_str;
72 for (const auto& [variable, value] : script.exports()) { 75 for (const auto& [variable, value] : script.exports()) {
73 resource_contents << variable << " = SubResource( " << value << " )" 76 variables_str << variable << " = SubResource( " << value << " )"
74 << std::endl; 77 << std::endl;
75 } 78 }
76 resource_contents << "script/source = " << std::quoted(script.ToString());
77 79
78 Heading heading; 80 Heading heading;
79 heading.set_type("sub_resource"); 81 heading.set_type("sub_resource");
80 heading.SetKeyval("type", "GDScript"); 82 heading.SetKeyval("type", "GDScript");
81 heading.SetKeyval("id", new_id); 83 heading.SetKeyval("id", new_id);
82 84
83 sub_resources_[new_id] = {heading, resource_contents.str()}; 85 sub_resources_[new_id] = {heading, resource_contents.str(),
86 variables_str.str()};
84 87
85 return new_id; 88 return new_id;
86} 89}
@@ -93,6 +96,7 @@ void Scene::ReplaceScriptReferences(int ext_id, int sub_id) {
93 search_str << "ExtResource( " << ext_id << " )"; 96 search_str << "ExtResource( " << ext_id << " )";
94 std::string search = search_str.str(); 97 std::string search = search_str.str();
95 98
99 bool found_it = false;
96 std::ostringstream new_text; 100 std::ostringstream new_text;
97 std::string_view corpus = text; 101 std::string_view corpus = text;
98 int location; 102 int location;
@@ -100,14 +104,20 @@ void Scene::ReplaceScriptReferences(int ext_id, int sub_id) {
100 new_text << corpus.substr(0, location) << "SubResource( " << sub_id 104 new_text << corpus.substr(0, location) << "SubResource( " << sub_id
101 << " )"; 105 << " )";
102 corpus.remove_prefix(location + search.size()); 106 corpus.remove_prefix(location + search.size());
107 found_it = true;
103 } 108 }
104 new_text << corpus; 109 new_text << corpus;
105 110
111 if (found_it) {
112 const std::string& variables = std::get<2>(sub_resources_[sub_id]);
113 new_text << std::endl << variables;
114 }
115
106 text = new_text.str(); 116 text = new_text.str();
107 }; 117 };
108 118
109 for (auto& [id, stuff] : sub_resources_) { 119 for (auto& [id, stuff] : sub_resources_) {
110 auto& [heading, text] = stuff; 120 auto& [heading, text, variables] = stuff;
111 replace_refs(text); 121 replace_refs(text);
112 } 122 }
113 123
@@ -127,7 +137,7 @@ std::string Scene::ToString() const {
127 output << std::endl; 137 output << std::endl;
128 138
129 for (const auto& [id, resource] : sub_resources_) { 139 for (const auto& [id, resource] : sub_resources_) {
130 const auto& [heading, value] = resource; 140 const auto& [heading, value, variables] = resource;
131 output << heading.ToString() << value << std::endl << std::endl; 141 output << heading.ToString() << value << std::endl << std::endl;
132 } 142 }
133 143
diff --git a/scene.h b/scene.h index 0ee9450..db38f4c 100644 --- a/scene.h +++ b/scene.h
@@ -26,7 +26,7 @@ class Scene {
26 private: 26 private:
27 Heading file_descriptor_; 27 Heading file_descriptor_;
28 std::map<int, Heading> ext_resources_; 28 std::map<int, Heading> ext_resources_;
29 std::map<int, std::tuple<Heading, std::string>> sub_resources_; 29 std::map<int, std::tuple<Heading, std::string, std::string>> sub_resources_;
30 std::vector<std::tuple<Heading, std::string>> other_; 30 std::vector<std::tuple<Heading, std::string>> other_;
31 int next_sub_id_ = 0; 31 int next_sub_id_ = 0;
32}; 32};