config-importer.tscn 5.3 KB

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