diff options
Diffstat (limited to 'main.cpp')
| -rw-r--r-- | main.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
| diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9da0c40 --- /dev/null +++ b/main.cpp | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | #include <filesystem> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <list> | ||
| 5 | #include <utility> | ||
| 6 | |||
| 7 | #include "heading.h" | ||
| 8 | #include "scene.h" | ||
| 9 | #include "script.h" | ||
| 10 | |||
| 11 | struct EmbeddedScript { | ||
| 12 | Script script; | ||
| 13 | bool internal = false; // true if referred from within another script | ||
| 14 | int id = -1; // if internal is false, ExtResource id | ||
| 15 | std::string path; // if internal is true | ||
| 16 | }; | ||
| 17 | |||
| 18 | void show_usage() { | ||
| 19 | std::cout << "godot3_tscn_compactor [path to root tscn] [file prefix]" | ||
| 20 | << std::endl; | ||
| 21 | } | ||
| 22 | |||
| 23 | int main(int argc, char** argv) { | ||
| 24 | if (argc != 3) { | ||
| 25 | show_usage(); | ||
| 26 | return 0; | ||
| 27 | } | ||
| 28 | |||
| 29 | std::filesystem::path root_tscn_path(argv[1]); | ||
| 30 | std::string file_prefix(argv[2]); | ||
| 31 | |||
| 32 | Scene root_tscn(root_tscn_path); | ||
| 33 | |||
| 34 | std::vector<EmbeddedScript> sub_scripts; | ||
| 35 | |||
| 36 | for (const auto& [id, heading] : root_tscn.ext_resources()) { | ||
| 37 | std::string resource_filename = | ||
| 38 | std::get<std::string>(heading.GetKeyval("path")); | ||
| 39 | if (resource_filename.substr(0, file_prefix.size()) == file_prefix) { | ||
| 40 | std::filesystem::path relative_filename = | ||
| 41 | resource_filename.substr(file_prefix.size()); | ||
| 42 | std::filesystem::path resource_path = | ||
| 43 | root_tscn_path.parent_path() / relative_filename; | ||
| 44 | |||
| 45 | EmbeddedScript embedded_script; | ||
| 46 | embedded_script.script = Script(resource_path, file_prefix); | ||
| 47 | embedded_script.id = id; | ||
| 48 | |||
| 49 | sub_scripts.push_back(std::move(embedded_script)); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | std::list<EmbeddedScript> all_scripts; | ||
| 54 | std::vector<EmbeddedScript> scripts_to_check = sub_scripts; | ||
| 55 | while (!scripts_to_check.empty()) { | ||
| 56 | std::vector<EmbeddedScript> cur_scripts; | ||
| 57 | cur_scripts.swap(scripts_to_check); | ||
| 58 | |||
| 59 | for (const EmbeddedScript& embedded_script : cur_scripts) { | ||
| 60 | all_scripts.push_front(embedded_script); | ||
| 61 | |||
| 62 | for (const std::string& loaded_script_path : | ||
| 63 | embedded_script.script.loaded_scripts()) { | ||
| 64 | std::filesystem::path resource_path = | ||
| 65 | root_tscn_path.parent_path() / loaded_script_path; | ||
| 66 | |||
| 67 | EmbeddedScript embedded_script; | ||
| 68 | embedded_script.script = Script(resource_path, file_prefix); | ||
| 69 | embedded_script.internal = true; | ||
| 70 | embedded_script.path = loaded_script_path; | ||
| 71 | |||
| 72 | scripts_to_check.push_back(std::move(embedded_script)); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | std::map<std::string, int> new_sub_scripts; | ||
| 78 | for (EmbeddedScript embedded_script : all_scripts) { | ||
| 79 | embedded_script.script.ReplaceScriptReferences(new_sub_scripts); | ||
| 80 | int sub_resource_id = root_tscn.AddSubResource(embedded_script.script); | ||
| 81 | |||
| 82 | if (embedded_script.internal) { | ||
| 83 | new_sub_scripts[embedded_script.path] = sub_resource_id; | ||
| 84 | } else { | ||
| 85 | root_tscn.RemoveExtResource(embedded_script.id); | ||
| 86 | root_tscn.ReplaceScriptReferences(embedded_script.id, sub_resource_id); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | std::filesystem::path output_name = root_tscn_path; | ||
| 91 | output_name.replace_filename(std::string(output_name.stem()) + | ||
| 92 | "_compiled.tscn"); | ||
| 93 | |||
| 94 | std::ofstream output_stream(output_name); | ||
| 95 | output_stream << root_tscn.ToString(); | ||
| 96 | |||
| 97 | return 0; | ||
| 98 | } | ||
