123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- [gd_scene load_steps=2 format=3 uid="uid://c7m7hu5uvgbit"]
- [sub_resource type="GDScript" id="GDScript_d7xax"]
- script/source = "@tool
- extends Control
- var path: String
- var _config_path: String
- var _config := ConfigFile.new()
- var _task: Thread
- const _config_section := 'CONFIG'
- func _enter_tree() -> void:
- if path.is_empty():
- return
- _config_path = path.path_join('config-importer.ini')
- _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_field_info.button_pressed = _config.get_value(_config_section, 'generate_field_info', true)
- func _exit_tree() -> void:
- if path.is_empty():
- return
- _config_save()
- 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_field_info', $generate_field_info.button_pressed)
- _config.save(_config_path)
- func _process(delta) -> void:
- if _task == null || !_task.is_alive():
- if _task != null && !_task.is_alive():
- _task.wait_to_finish()
- _task = null
- scale = Vector2.ONE
- func _setup_task(task:Callable) -> void:
- scale = Vector2.ZERO
- _task = Thread.new()
- _task.start(task)
- class ConfigField:
- var column_id: int
- var name: String
- var comment: String
- var default_record: String
- static func parse(file_access:FileAccess):
- var fields := []
- var names := file_access.get_csv_line()
- var comments := file_access.get_csv_line()
- var infos := file_access.get_csv_line()
- for i in range(0, len(names)):
- if (names[i].is_empty()):
- continue
- var field := ConfigField.new()
- field.column_id = i
- field.name = names[i]
- field.comment = comments[i]
- fields.push_back(field)
- return fields
- static func pick(fields, id):
- for field in fields:
- if field.name == id:
- return field
- return null
- 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)
- _setup_task(func():
- var dir_access := DirAccess.open($config_path.text)
- 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)
- var ini := FileAccess.open($output_path.text.path_join(config_type) + '.ini', FileAccess.WRITE)
- for config_file in config_dir_access.get_files():
- if config_file.get_extension() != 'csv':
- continue
- var file_access := FileAccess.open(config_type_dir.path_join(config_file), FileAccess.READ)
- var fields = ConfigField.parse(file_access)
- var id_field = ConfigField.pick(fields, 'Id')
- var id_name_field = ConfigField.pick(fields, 'IdName')
- fields.erase(id_field)
- fields.erase(id_name_field)
- ini.store_line('; --------------- {0} ---------------'.format([config_file.get_basename()]))
- if $generate_field_info.button_pressed:
- ini.store_line('; {0} data fields:'.format([config_file.get_basename()]))
- for field in fields:
- ini.store_line('; {0}={1} ; {2}'.format([field.name, field.default_record, field.comment]))
- ini.store_line('')
- while !file_access.eof_reached():
- var line := file_access.get_csv_line()
- ini.store_line('[{0}]'.format([line[id_field.column_id]]))
- for field in fields:
- var i:int = field.column_id
- var is_use_default := line[i].is_empty()
- if is_use_default:
- continue
- ini.store_line('{0}={1}'.format([field.name, line[field.column_id]]))
- ini.store_line('')
- )
- "
- [node name="配置" type="VBoxContainer"]
- anchors_preset = 15
- anchor_right = 1.0
- anchor_bottom = 1.0
- grow_horizontal = 2
- grow_vertical = 2
- script = SubResource("GDScript_d7xax")
- [node name="config_path" type="LineEdit" parent="."]
- layout_mode = 2
- placeholder_text = "配置路径"
- [node name="output_path" type="LineEdit" parent="."]
- layout_mode = 2
- placeholder_text = "生成路径"
- [node name="generate_field_info" type="CheckBox" parent="."]
- layout_mode = 2
- button_pressed = true
- text = "生成所有字段注释"
- [node name="import_perform" type="Button" parent="."]
- layout_mode = 2
- text = "读取表格生成配置"
- [connection signal="button_down" from="import_perform" to="." method="_on_import_perform_button_down"]
|