config-importer.tscn 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if DirAccess.dir_exists_absolute(full_output_path):
  64. OS.move_to_trash(full_output_path)
  65. DirAccess.make_dir_recursive_absolute(full_output_path)
  66. _setup_task(func():
  67. var dir_access := DirAccess.open($config_path.text)
  68. for config_type in dir_access.get_directories():
  69. var config_type_dir: String = $config_path.text.path_join(config_type)
  70. var config_dir_access := DirAccess.open(config_type_dir)
  71. var ini := FileAccess.open($output_path.text.path_join(config_type) + '.ini', FileAccess.WRITE)
  72. for config_file in config_dir_access.get_files():
  73. if config_file.get_extension() != 'csv':
  74. continue
  75. var file_access := FileAccess.open(config_type_dir.path_join(config_file), FileAccess.READ)
  76. var fields = ConfigField.parse(file_access)
  77. var id_field = ConfigField.pick(fields, 'Id')
  78. var id_name_field = ConfigField.pick(fields, 'IdName')
  79. fields.erase(id_field)
  80. fields.erase(id_name_field)
  81. ini.store_line('; --------------- {0} ---------------'.format([config_file.get_basename()]))
  82. if $generate_field_info.button_pressed:
  83. ini.store_line('; {0} data fields:'.format([config_file.get_basename()]))
  84. for field in fields:
  85. ini.store_line('; {0}={1} ; {2}'.format([field.name, field.default_record, field.comment]))
  86. ini.store_line('')
  87. while !file_access.eof_reached():
  88. var line := file_access.get_csv_line()
  89. ini.store_line('[{0}]'.format([line[id_field.column_id]]))
  90. for field in fields:
  91. var i:int = field.column_id
  92. var is_use_default := line[i].is_empty()
  93. if is_use_default:
  94. continue
  95. ini.store_line('{0}={1}'.format([field.name, line[field.column_id]]))
  96. ini.store_line('')
  97. )
  98. "
  99. [node name="配置" type="VBoxContainer"]
  100. anchors_preset = 15
  101. anchor_right = 1.0
  102. anchor_bottom = 1.0
  103. grow_horizontal = 2
  104. grow_vertical = 2
  105. script = SubResource("GDScript_d7xax")
  106. [node name="config_path" type="LineEdit" parent="."]
  107. layout_mode = 2
  108. placeholder_text = "配置路径"
  109. [node name="output_path" type="LineEdit" parent="."]
  110. layout_mode = 2
  111. placeholder_text = "生成路径"
  112. [node name="generate_field_info" type="CheckBox" parent="."]
  113. layout_mode = 2
  114. button_pressed = true
  115. text = "生成所有字段注释"
  116. [node name="import_perform" type="Button" parent="."]
  117. layout_mode = 2
  118. text = "读取表格生成配置"
  119. [connection signal="button_down" from="import_perform" to="." method="_on_import_perform_button_down"]