config-importer.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. require_relative 'common'
  2. options = get_options(__FILE__)
  3. config_path = options['config_path']
  4. ini_path = options['generate_ini_path']
  5. text_path = options['generate_text_path']
  6. code_path = options['generate_code_path']
  7. language = options['source_text_language']
  8. is_generate_field_info = options['generate_field_info']
  9. recreate_dir ini_path
  10. recreate_dir text_path
  11. recreate_dir code_path
  12. require 'csv'
  13. # 各类型定义
  14. TypeInfo = Struct.new(:data_type, :data_type_extra, :ref_info, :default_value, :struct_info)
  15. FieldInfo = Struct.new(:column_id, :identifier, :comment, :type_info)
  16. Table = Struct.new(:csv, :fields, :name2id, :id_field, :raw_name)
  17. Ini = Struct.new(:io, :tables)
  18. inis = {}
  19. # 读取配置表子目录,以形成配置数据目录
  20. Dir["#{config_path}/*"].each do |path|
  21. ini_name = File::basename(path)
  22. ini = (inis[ini_name] ||= Ini.new(File.open("#{ini_path}/#{ini_name}.ini", 'wb'), {}))
  23. next unless File::directory?(path)
  24. # 每个配置表子目录将成为一个 ini 文件
  25. # 内含全部子 .csv 表格的导入数据
  26. Dir["#{path}/*.csv"].each do |file|
  27. table_name = File::basename(file, '.csv')
  28. table = (ini.tables[convert_case(table_name)] = Table.new(csv = CSV.open(file), [], {}, nil, table_name))
  29. identifiers = csv.readline
  30. comments = csv.readline
  31. type_infos = csv.readline
  32. field_id = nil
  33. field_id_name = nil
  34. # 处理表头列
  35. identifiers.each_with_index do |identifier, index|
  36. next if identifier.nil? || identifier.empty?
  37. type_info = TypeInfo.new(nil, nil, nil, nil, nil)
  38. type_infos[index].gsub(/\s+/, '').scan(/[(:|,|@|=)][a-zA-Z_]+/).each do |info|
  39. type_info.data_type = info[1..info.size] if info.start_with?(':')
  40. type_info.data_type_extra = info[1..info.size] if info.start_with?(',')
  41. type_info.ref_info = convert_case(info[1..info.size]) if info.start_with?('@')
  42. type_info.default_value = info[1..info.size] if info.start_with?('=')
  43. type_info.struct_info = info[1..info.size] if info.start_with?('!')
  44. end
  45. field = FieldInfo.new(index, identifier, comments[index], type_info)
  46. # 处理特殊类型的字段
  47. case type_info.data_type
  48. when 'id'
  49. field_id = field
  50. next
  51. when 'id_name'
  52. field_id_name = field
  53. next
  54. end
  55. # 参与导入的条目
  56. table.fields << field
  57. end
  58. # 验证 Id 列
  59. next if field_id.nil?
  60. table.id_field = field_id
  61. # 处理条目形成引用表
  62. while !csv.eof?
  63. line = csv.readline
  64. next if field_id_name.nil?
  65. table.name2id[line[field_id_name.column_id]] = line[field_id.column_id].upcase
  66. end
  67. # 回滚 csv 状态至表头读取后
  68. csv.rewind
  69. 3.times { csv.readline }
  70. end
  71. end
  72. # 所有表格缓存
  73. tables = inis.values.collect{|ini| ini.tables}.inject(:merge)
  74. # 导入实际数据条目
  75. inis.each_pair do |ini_name, ini_data|
  76. register_table = {}
  77. enum_table = {}
  78. buffer = StringIO.new
  79. # 逐目录生成 ini
  80. ini_data.tables.each_pair do |table_name, table|
  81. register = (register_table[table_name] = [])
  82. texts = {}
  83. count = 0
  84. # 逐行扫描
  85. while !table.csv.eof?
  86. line = table.csv.readline
  87. id = line[table.id_field.column_id]
  88. next if id.nil? || id.empty?
  89. id = id.upcase
  90. buffer.puts "[#{id}]"
  91. table.fields.each do |field|
  92. value = line[field.column_id]
  93. next if value.nil? || value.empty?
  94. # 引用名替换处理
  95. if field.type_info.ref_info
  96. ref_table = tables[field.type_info.ref_info]
  97. value = value.split(',').collect do |ref_name|
  98. ref_name.strip!
  99. ref_table.name2id[ref_name] || ref_name.upcase
  100. end.join(',')
  101. # 数据类型处理
  102. else
  103. case field.type_info.data_type
  104. # 文本内容
  105. when 'text'
  106. index = 0
  107. value = value.split(',').collect do |text|
  108. key = "#{id}"
  109. key = "#{key}:#{index}" if index > 0
  110. global_key = "C:#{table_name}:#{field.identifier}:#{key}"
  111. texts[global_key] = text
  112. index += 1
  113. key
  114. end.join(',')
  115. # 枚举内容
  116. when 'enum'
  117. case field.type_info.data_type_extra
  118. # 自动型枚举
  119. when 'auto'
  120. enum_key = "#{table_name}#{field.identifier}"
  121. value.split(',').each do |v|
  122. enums = (enum_table[enum_key] ||= {})
  123. enums[v] = enums.size
  124. end
  125. end
  126. end
  127. end
  128. buffer.puts "#{field.identifier}=#{value}"
  129. count += 1
  130. end
  131. buffer.puts
  132. register << id
  133. end
  134. # 导出语言表
  135. if !texts.empty?
  136. csf = CSV.open("#{text_path}/#{table.raw_name}.csv", 'wb')
  137. csf << ['', language]
  138. texts.keys.sort.each do |k|
  139. csf << [k, texts[k]]
  140. end
  141. end
  142. end
  143. # 导出 ini
  144. File.open("#{ini_path}/#{ini_name}.ini", 'wb') do |ini|
  145. # 导出注册表
  146. if !register_table.empty?
  147. ini.puts '[REGISTER]'
  148. register_table.each_pair do |table_name, register|
  149. ini.puts "#{table_name}=#{register.join(',')}"
  150. end
  151. ini.puts
  152. end
  153. # 导出枚举
  154. if !enum_table.empty?
  155. ini.puts '[ENUM]'
  156. enum_table.keys.sort.each do |enum_name|
  157. ini.puts "#{enum_name}=#{enum_table[enum_name].keys.join(',')}"
  158. end
  159. ini.puts
  160. end
  161. # 导出所有条目
  162. ini.puts buffer.string
  163. end
  164. end