config-importer.tscn 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. [gd_scene load_steps=2 format=3 uid="uid://c7m7hu5uvgbit"]
  2. [sub_resource type="GDScript" id="GDScript_d7xax"]
  3. script/source = "@tool
  4. extends Control
  5. var path: String
  6. var _config_path: String
  7. var _config := ConfigFile.new()
  8. var _task: Thread
  9. const _config_section := 'CONFIG'
  10. func _enter_tree() -> void:
  11. _config_path = path.path_join('config-importer.ini')
  12. _config.load(_config_path)
  13. $config_path.text = _config.get_value(_config_section, 'config_path', '')
  14. $output_path.text = _config.get_value(_config_section, 'output_path', '')
  15. $generate_field_info.button_pressed = _config.get_value(_config_section, 'generate_field_info', true)
  16. func _exit_tree() -> void:
  17. _config_save()
  18. func _config_save() -> void:
  19. _config.set_value(_config_section, 'config_path', $config_path.text)
  20. _config.set_value(_config_section, 'output_path', $output_path.text)
  21. _config.set_value(_config_section, 'generate_field_info', $generate_field_info.button_pressed)
  22. _config.save(_config_path)
  23. func _process(delta) -> void:
  24. if _task == null || !_task.is_alive():
  25. if _task != null && !_task.is_alive():
  26. _task.wait_to_finish()
  27. _task = null
  28. scale = Vector2.ONE
  29. func _setup_task(task:Callable) -> void:
  30. scale = Vector2.ZERO
  31. _task = Thread.new()
  32. _task.start(task)
  33. class ConfigField:
  34. var column_id: int
  35. var name: String
  36. var comment: String
  37. var default_record: String
  38. static func parse(file_access:FileAccess):
  39. var fields := []
  40. var names := file_access.get_csv_line()
  41. var comments := file_access.get_csv_line()
  42. var infos := file_access.get_csv_line()
  43. for i in range(0, len(names)):
  44. if (names[i].is_empty()):
  45. continue
  46. var field := ConfigField.new()
  47. field.column_id = i
  48. field.name = names[i]
  49. field.comment = comments[i]
  50. fields.push_back(field)
  51. return fields
  52. static func pick(fields, id):
  53. for field in fields:
  54. if field.name == id:
  55. return field
  56. return null
  57. func _on_import_perform_button_down() -> void:
  58. var full_output_path := ProjectSettings.globalize_path($output_path.text)
  59. OS.move_to_trash(full_output_path)
  60. DirAccess.make_dir_absolute(full_output_path)
  61. _setup_task(func():
  62. var dir_access := DirAccess.open($config_path.text)
  63. for config_type in dir_access.get_directories():
  64. var config_type_dir: String = $config_path.text.path_join(config_type)
  65. var config_dir_access := DirAccess.open(config_type_dir)
  66. var ini := FileAccess.open($output_path.text.path_join(config_type) + '.ini', FileAccess.WRITE)
  67. for config_file in config_dir_access.get_files():
  68. if config_file.get_extension() != 'csv':
  69. continue
  70. var file_access := FileAccess.open(config_type_dir.path_join(config_file), FileAccess.READ)
  71. var fields = ConfigField.parse(file_access)
  72. var id_field = ConfigField.pick(fields, 'Id')
  73. var id_name_field = ConfigField.pick(fields, 'IdName')
  74. fields.erase(id_field)
  75. fields.erase(id_name_field)
  76. ini.store_line('; --------------- {0} ---------------'.format([config_file.get_basename()]))
  77. if $generate_field_info.button_pressed:
  78. ini.store_line('; {0} data fields:'.format([config_file.get_basename()]))
  79. for field in fields:
  80. ini.store_line('; {0}={1} ; {2}'.format([field.name, field.default_record, field.comment]))
  81. ini.store_line('')
  82. while !file_access.eof_reached():
  83. var line := file_access.get_csv_line()
  84. ini.store_line('[{0}]'.format([line[id_field.column_id]]))
  85. for field in fields:
  86. var i:int = field.column_id
  87. var is_use_default := line[i].is_empty()
  88. if is_use_default:
  89. continue
  90. ini.store_line('{0}={1}'.format([field.name, line[field.column_id]]))
  91. ini.store_line('')
  92. )
  93. "
  94. [node name="配置" type="VBoxContainer"]
  95. anchors_preset = 15
  96. anchor_right = 1.0
  97. anchor_bottom = 1.0
  98. grow_horizontal = 2
  99. grow_vertical = 2
  100. script = SubResource("GDScript_d7xax")
  101. [node name="config_path" type="LineEdit" parent="."]
  102. layout_mode = 2
  103. placeholder_text = "配置路径"
  104. [node name="output_path" type="LineEdit" parent="."]
  105. layout_mode = 2
  106. placeholder_text = "生成路径"
  107. [node name="generate_field_info" type="CheckBox" parent="."]
  108. layout_mode = 2
  109. text = "生成所有字段注释"
  110. [node name="import_perform" type="Button" parent="."]
  111. layout_mode = 2
  112. text = "读取表格生成配置"
  113. [connection signal="button_down" from="import_perform" to="." method="_on_import_perform_button_down"]