config-importer.tscn 4.3 KB

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