Kaynağa Gözat

refactor: update config-importer.tscn

LanzaSchneider 1 yıl önce
ebeveyn
işleme
1604170194
1 değiştirilmiş dosya ile 32 ekleme ve 6 silme
  1. 32 6
      editor/config-importer.tscn

+ 32 - 6
editor/config-importer.tscn

@@ -18,6 +18,7 @@ func _enter_tree() -> void:
 	_config.load(_config_path)
 	$config_path.text = _config.get_value(_config_section, 'config_path', '')
 	$output_path.text = _config.get_value(_config_section, 'output_path', '')
+	$generate_code_path.text = _config.get_value(_config_section, 'generate_code_path', '')
 	$generate_field_info.button_pressed = _config.get_value(_config_section, 'generate_field_info', true)
 
 func _exit_tree() -> void:
@@ -28,6 +29,7 @@ func _exit_tree() -> void:
 func _config_save() -> void:
 	_config.set_value(_config_section, 'config_path', $config_path.text)
 	_config.set_value(_config_section, 'output_path', $output_path.text)
+	_config.set_value(_config_section, 'generate_code_path', $generate_code_path.text)
 	_config.set_value(_config_section, 'generate_field_info', $generate_field_info.button_pressed)
 	_config.save(_config_path)
 
@@ -70,13 +72,21 @@ class ConfigField:
 				return field
 		return null
 
+func _recreate_dir(full_path:String) -> void:
+	if DirAccess.dir_exists_absolute(full_path):
+		OS.move_to_trash(full_path)
+	DirAccess.make_dir_recursive_absolute(full_path)
+
 func _on_import_perform_button_down() -> void:
 	var full_output_path := ProjectSettings.globalize_path($output_path.text)
-	if DirAccess.dir_exists_absolute(full_output_path):
-		OS.move_to_trash(full_output_path)
-	DirAccess.make_dir_recursive_absolute(full_output_path)
+	var full_gen_code_path := ProjectSettings.globalize_path($generate_code_path.text)
+
+	_recreate_dir(full_output_path)
+	_recreate_dir(full_gen_code_path)
+
 	_setup_task(func():
 		var dir_access := DirAccess.open($config_path.text)
+		var gen_script := FileAccess.open($generate_code_path.text.path_join('config') + '.gen.gd', FileAccess.WRITE)
 		for config_type in dir_access.get_directories():
 			var config_type_dir: String = $config_path.text.path_join(config_type)
 			var config_dir_access := DirAccess.open(config_type_dir)
@@ -88,14 +98,23 @@ func _on_import_perform_button_down() -> void:
 				var fields = ConfigField.parse(file_access)
 				var id_field = ConfigField.pick(fields, 'Id')
 				var id_name_field = ConfigField.pick(fields, 'IdName')
+				var config_title := config_file.get_basename()
+				var config_class_name := config_title.capitalize().replace(' ', '')
 				fields.erase(id_field)
 				fields.erase(id_name_field)
-				ini.store_line('; --------------- {0} ---------------'.format([config_file.get_basename()]))
+
+				ini.store_line('; --------------- {0} ---------------'.format([config_title]))
+
 				if $generate_field_info.button_pressed:
-					ini.store_line('; {0} data fields:'.format([config_file.get_basename()]))
+					ini.store_line('; {0} data fields:'.format([config_title]))
 					for field in fields:
 						ini.store_line('; {0}={1} ; {2}'.format([field.name, field.default_record, field.comment]))
 					ini.store_line('')
+
+				gen_script.store_line('class {0}:'.format([config_class_name]))
+				for field in fields:
+					gen_script.store_line('\\t# {0}'.format([field.name]))
+
 				while !file_access.eof_reached():
 					var line := file_access.get_csv_line()
 					ini.store_line('[{0}]'.format([line[id_field.column_id]]))
@@ -106,6 +125,9 @@ func _on_import_perform_button_down() -> void:
 							continue
 						ini.store_line('{0}={1}'.format([field.name, line[field.column_id]]))
 					ini.store_line('')
+
+				gen_script.store_line('\\tpass')
+				gen_script.store_line('')
 	)
 "
 
@@ -123,7 +145,11 @@ placeholder_text = "配置路径"
 
 [node name="output_path" type="LineEdit" parent="."]
 layout_mode = 2
-placeholder_text = "生成路径"
+placeholder_text = "ini 生成路径"
+
+[node name="generate_code_path" type="LineEdit" parent="."]
+layout_mode = 2
+placeholder_text = "代码生成路径"
 
 [node name="generate_field_info" type="CheckBox" parent="."]
 layout_mode = 2