Ver Fonte

refactor: config-importer

LanzaSchneider há 1 ano atrás
pai
commit
f122e1364c

+ 21 - 21
editor/config-importer.md

@@ -21,40 +21,40 @@
 
 * 数据类型说明
 
-    格式为 ```:数据类型```
+	格式为 ```:数据类型```
 
-    如 :int 则为整数类型,所有类型详见下表
+	如 :int 则为整数类型,所有类型详见下表
 
-    | 标识 | 说明 |
-    |---|---|
-    | int | 整数类型 |
-    | float | 浮点类型 |
-    | string | 字符串类型 |
-    | text | 文本类型,和字符串类型的不同在于,它是会自动处理成本地化表项的 |
-    | enum | 枚举类型,具体的枚举规则要看补充说明 |
-    | id | 数据主键类型 |
-    | id_name | 数据主键引用名类型,能够在引用时解析为 id |
+	| 标识 | 说明 |
+	|---|---|
+	| int | 整数类型 |
+	| float | 浮点类型 |
+	| string | 字符串类型 |
+	| text | 文本类型,和字符串类型的不同在于,它是会自动处理成本地化表项的 |
+	| enum | 枚举类型,具体的枚举规则要看补充说明 |
+	| id | 数据主键类型 |
+	| id_name | 数据主键引用名类型,能够在引用时解析为 id |
 
-    如果最后追加 ```[]``` 则可以形成列表类型
+	如果最后追加 ```[]``` 则可以形成列表类型
 
 * 数据类型补充说明
 
-    格式为 ```:数据类型,补充说明```
+	格式为 ```:数据类型,补充说明```
 
-    某些数据类型支持进一步的补充说明,详见下表
+	某些数据类型支持进一步的补充说明,详见下表
 
-    | 补充说明 | 适用的数据类型 | 说明 |
-    |---|---|---|
-    | auto | enum | 自动枚举项,会将配置数据中所有此列出现过的值都归并为枚举值 |
+	| 补充说明 | 适用的数据类型 | 说明 |
+	|---|---|---|
+	| auto | enum | 自动枚举项,会将配置数据中所有此列出现过的值都归并为枚举值 |
 
 * 引用说明
 
-    格式为 ```@表名```
+	格式为 ```@表名```
 
-    表示这个字段是引用某个表中的主键,可以用 ```:id_name``` 配置的引用名
+	表示这个字段是引用某个表中的主键,可以用 ```:id_name``` 配置的引用名
 
 * 默认值说明
 
-    格式为 ```=默认值```
+	格式为 ```=默认值```
 
-    说明这个配置列的默认值,配置了默认值的表项,在没有配置该值的条目中,就会被忽略
+	说明这个配置列的默认值,配置了默认值的表项,在没有配置该值的条目中,就会被忽略

+ 0 - 115
editor/config-importer.tscn

@@ -1,115 +0,0 @@
-[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
-
-@export_file('*.rb')
-var _tool_file: String
-
-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_code_path.text = _config.get_value(_config_section, 'generate_code_path', '')
-	$generate_text_path.text = _config.get_value(_config_section, 'generate_text_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_code_path', $generate_code_path.text)
-	_config.set_value(_config_section, 'generate_text_path', $generate_text_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)
-
-func _recreate_dir(full_path:String) -> void:
-	if DirAccess.dir_exists_absolute(full_path):
-		OS.move_to_trash(full_path)
-	DirAccess.make_dir_recursive_absolute(full_path)
-
-func _on_import_perform_button_down() -> void:
-	_setup_task(perform)
-
-func perform():
-	var full_output_path := ProjectSettings.globalize_path($output_path.text)
-	var full_gen_code_path := ProjectSettings.globalize_path($generate_code_path.text)
-	var full_gen_text_path := ProjectSettings.globalize_path($generate_text_path.text)
-	var params: PackedStringArray = [
-		ProjectSettings.globalize_path(_tool_file),
-		ProjectSettings.globalize_path($config_path.text),
-		ProjectSettings.globalize_path($output_path.text),
-		full_gen_code_path,
-		full_gen_text_path,
-		$generate_field_info.button_pressed
-		]
-	for i in range(0, len(params)):
-		params[i] = ('\"' + params[i] + '\"').strip_edges()
-	_recreate_dir(full_output_path)
-	_recreate_dir(full_gen_code_path)
-	_recreate_dir(full_gen_text_path)
-	OS.execute('ruby', params)
-"
-
-[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")
-_tool_file = "res://addons/godot-TBL-extension-base/editor/tools/config-importer.rb"
-
-[node name="config_path" type="LineEdit" parent="."]
-layout_mode = 2
-placeholder_text = "配置路径"
-
-[node name="output_path" type="LineEdit" parent="."]
-layout_mode = 2
-placeholder_text = "ini 生成路径"
-
-[node name="generate_code_path" type="LineEdit" parent="."]
-layout_mode = 2
-placeholder_text = "代码生成路径"
-
-[node name="generate_text_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"]

+ 18 - 0
editor/scripts/dev_tool.gd

@@ -0,0 +1,18 @@
+@tool
+class_name DevTool
+extends Resource
+
+@export
+var tool_dock: EditorPlugin.DockSlot
+
+@export_file("*.json")
+var tool_config: String
+
+@export_file("*.rb")
+var tool_script: String
+
+@export
+var tool_name: String
+
+@export
+var tool_command_name: String

+ 35 - 0
editor/tools/common.rb

@@ -0,0 +1,35 @@
+require 'json'
+require 'fileutils'
+
+$res_root = (lambda {
+  dir = File.dirname(__FILE__)
+  loop do
+    parent_dir = File.expand_path('..', dir)
+    break if dir.empty? || parent_dir == dir
+    dir = parent_dir
+    break if File.exist?("#{dir}/project.godot")
+  end
+  return dir
+}).call
+
+CONFIG_ROOT = "#{$res_root}/.tbl"
+
+def get_options(tool_file)
+  options = {}
+  name = File.basename(tool_file, ".rb")
+  JSON.parse(File.read("#{CONFIG_ROOT}/#{name}.json")).each do |unit|
+    key, value = unit['name'], unit['value']
+    value.gsub!('res:', $res_root) if value.is_a?(String)
+    options[key] = value
+  end
+  return options
+end
+
+def convert_case(source)
+  return source.split('_').collect(&:capitalize).join
+end
+
+def recreate_dir(dir)
+  FileUtils.rm_rf dir
+  FileUtils.mkdir_p dir
+end

+ 14 - 6
editor/tools/config-importer.rb

@@ -1,11 +1,19 @@
-config_path, ini_path, code_path, text_path, is_generate_field_info = *ARGV
-is_generate_field_info = (is_generate_field_info == 'true')
+require_relative 'common'
 
-require 'csv'
+options = get_options(__FILE__)
 
-def convert_case(source)
-  return source.split('_').collect(&:capitalize).join
-end
+config_path = options['config_path']
+ini_path = options['generate_ini_path']
+text_path = options['generate_text_path']
+code_path = options['generate_code_path']
+is_generate_field_info = options['source_text_language']
+language = options['generate_field_info']
+
+recreate_dir ini_path
+recreate_dir text_path
+recreate_dir code_path
+
+require 'csv'
 
 # 各类型定义
 TypeInfo = Struct.new(:data_type, :data_type_extra, :ref_info, :default_value)

+ 11 - 0
editor/tools/config-importer.tres

@@ -0,0 +1,11 @@
+[gd_resource type="Resource" script_class="DevTool" load_steps=2 format=3 uid="uid://cgu1tpbnhjpjc"]
+
+[ext_resource type="Script" path="res://addons/godot-TBL-extension-base/editor/scripts/dev_tool.gd" id="1_5dgtc"]
+
+[resource]
+script = ExtResource("1_5dgtc")
+tool_dock = 3
+tool_config = "res://.tbl/config-importer.json"
+tool_script = "res://addons/godot-TBL-extension-base/editor/tools/config-importer.rb"
+tool_name = "配置"
+tool_command_name = "导入配置"

+ 40 - 8
tblext.gd

@@ -1,15 +1,47 @@
 @tool
 extends EditorPlugin
 
-const config_root_path := 'res://.tbl'
-
 func _enter_tree() -> void:
-	_setup_panel(preload('editor/config-importer.tscn'))
+	var helper := preload('tblext.gd')
+	var base_dir: String = helper.resource_path.get_base_dir()
+	make_panel(load(base_dir.path_join('editor/tools/config-importer.tres')))
 
-func _setup_panel(prefab:PackedScene):
-	var panel := prefab.instantiate()
-	panel.set('path', config_root_path)
-	add_control_to_dock(EditorPlugin.DOCK_SLOT_LEFT_BR, panel)
+func make_panel(dev_tool:DevTool) -> VBoxContainer:
+	var panel := VBoxContainer.new()
+	var options := JSON.parse_string(FileAccess.get_file_as_string(dev_tool.tool_config))
+	for option in options:
+		if option.value is String:
+			var comment := Label.new()
+			var line_edit := LineEdit.new()
+			comment.text = option.comment
+			line_edit.text = option.value
+			panel.add_child(comment)
+			panel.add_child(line_edit)
+			panel.tree_exiting.connect(func():
+				option.value = line_edit.text
+				)
+		elif option.value is bool:
+			var check_box := CheckBox.new()
+			check_box.text = option.comment
+			check_box.button_pressed = option.value
+			panel.add_child(check_box)
+			panel.tree_exiting.connect(func():
+				option.value = check_box.button_pressed
+				)
+	var command := Button.new()
+	command.text = dev_tool.tool_command_name
+	command.button_up.connect(func():
+		OS.execute('ruby', [dev_tool.tool_script])
+		)
+	panel.add_child(command)
+	panel.name = dev_tool.tool_name
+	add_control_to_dock(dev_tool.tool_dock, panel)
+	panel.tree_exited.connect(func():
+		var json := FileAccess.open(dev_tool.tool_config, FileAccess.WRITE)
+		json.store_string(JSON.stringify(options, "\t"))
+		)
 	tree_exiting.connect(func():
 		remove_control_from_docks(panel)
-		panel.queue_free())
+		panel.queue_free()
+		)
+	return panel