From 5890a7cef3fca51cfb44b60f3754742dda4d511a Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 4 Jun 2022 13:01:41 +0200 Subject: [PATCH] More work on the scene to cpp converter. --- game/addons/gdc_converter/plugin.gd | 55 +++++++++++++---------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/game/addons/gdc_converter/plugin.gd b/game/addons/gdc_converter/plugin.gd index 90f8ff2b..d729c3d1 100644 --- a/game/addons/gdc_converter/plugin.gd +++ b/game/addons/gdc_converter/plugin.gd @@ -3,11 +3,11 @@ extends EditorPlugin func _enter_tree(): add_tool_menu_item("Convert Scripts to Cpp", self, "on_convert_script_menu_clicked") - #add_tool_menu_item("Convert Scenes to Cpp", self, "on_convert_scene_menu_clicked") + add_tool_menu_item("Convert Scenes to Cpp", self, "on_convert_scene_menu_clicked") func _exit_tree(): remove_tool_menu_item("Convert Scripts to Cpp") - #remove_tool_menu_item("Convert Scenes to Cpp") + remove_tool_menu_item("Convert Scenes to Cpp") func on_convert_script_menu_clicked(val) -> void: var GDSParser = load("res://addons/gdc_converter/gdsparser.gd") @@ -38,58 +38,51 @@ func on_convert_scene_menu_clicked(val) -> void: while file_name != "": if !dir.current_is_dir(): - if file_name.get_extension() == "gd": + if file_name.get_extension() == "tscn": process_file(dir_name + file_name) file_name = dir.get_next() class GDSSceneParser: - var root : String + var result : String + + func parse(file_name : String) -> void: + var ps : PackedScene = ResourceLoader.load(file_name, "PackedScene") + + if !ps: + print("ERROR! !ps :" + file_name) + return + + - func parse(contents : String, file_name : String) -> void: - #root = GDSScope.new() - #root.raw_scope_data = file_name -# root.scope_data = file_name.get_file().trim_suffix(".gd") -# root.camel_case_scope_data() # var c : PoolStringArray = split_preprocess_content(contents) # root.parse(c) pass func _to_string(): - return str(root) + return result - func get_cpp_impl_string(file_name : String) -> String: - var include_name : String = file_name.get_file() - include_name = include_name.to_lower() - include_name = include_name.trim_suffix(".gd") - include_name += ".h" - + func get_cpp_impl_string() -> String: var s : String = "\n" - s += "#include \"" + include_name + "\"\n" - s += "\n\n" - - #s += root.get_cpp_impl_string() - - s += "\n\n" + s += "void construct() {\n" + s += "\n" + s += result + s += "\n}\n" return s func process_file(file_name : String) -> void: - var file : File = File.new() - file.open(file_name, File.READ) - var contents : String = file.get_as_text() - file.close() - var parser : GDSSceneParser = GDSSceneParser.new() - parser.parse(contents, file_name) + parser.parse(file_name) var save_base_file_path : String = file_name.get_base_dir() - var save_base_file_name : String = file_name.get_file().to_lower().trim_suffix(".gd") + var save_base_file_name : String = file_name.get_file().to_lower().trim_suffix(".tscn") - var impl_file : String = save_base_file_path + "/" + save_base_file_name + ".cpp" + var impl_file : String = save_base_file_path + "/" + save_base_file_name + ".ctscn" + var impl_data : String = parser.get_cpp_impl_string() - #var impl_data : String = parser.get_cpp_impl_string(file_name) + print(impl_data) #file.open(impl_file, File.WRITE) #file.store_string(impl_data)