config-importer.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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)
  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)
  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. end
  44. field = FieldInfo.new(index, identifier, comments[index], type_info)
  45. # 处理特殊类型的字段
  46. case type_info.data_type
  47. when 'id'
  48. field_id = field
  49. next
  50. when 'id_name'
  51. field_id_name = field
  52. next
  53. end
  54. # 参与导入的条目
  55. table.fields << field
  56. end
  57. # 验证 Id 列
  58. next if field_id.nil?
  59. table.id_field = field_id
  60. # 处理条目形成引用表
  61. while !csv.eof?
  62. line = csv.readline
  63. next if field_id_name.nil?
  64. table.name2id[line[field_id_name.column_id]] = line[field_id.column_id].upcase
  65. end
  66. # 回滚 csv 状态至表头读取后
  67. csv.rewind
  68. 3.times { csv.readline }
  69. end
  70. end
  71. # 所有表格缓存
  72. tables = inis.values.collect{|ini| ini.tables}.inject(:merge)
  73. # 导入实际数据条目
  74. inis.each_pair do |ini_name, ini_data|
  75. register_table = {}
  76. enum_table = {}
  77. buffer = StringIO.new
  78. # 逐目录生成 ini
  79. ini_data.tables.each_pair do |table_name, table|
  80. register = (register_table[table_name] = [])
  81. texts = {}
  82. count = 0
  83. # 逐行扫描
  84. while !table.csv.eof?
  85. line = table.csv.readline
  86. id = line[table.id_field.column_id].upcase
  87. buffer.puts "[#{id}]"
  88. table.fields.each do |field|
  89. value = line[field.column_id]
  90. next if value.nil? || value.empty?
  91. # 引用名替换处理
  92. if field.type_info.ref_info
  93. ref_table = tables[field.type_info.ref_info]
  94. value = value.split(',').collect do |ref_name|
  95. ref_table.name2id[ref_name] || ref_name.upcase
  96. end.join(',')
  97. # 数据类型处理
  98. else
  99. case field.type_info.data_type
  100. # 文本内容
  101. when 'text'
  102. index = 0
  103. value = value.split(',').collect do |text|
  104. key = "C:#{table_name.upcase}:#{field.column_id}:#{count}"
  105. key = "#{key}:#{index}" if index > 0
  106. texts[key] = text
  107. index += 1
  108. key
  109. end.join(',')
  110. # 枚举内容
  111. when 'enum'
  112. case field.type_info.data_type_extra
  113. # 自动型枚举
  114. when 'auto'
  115. enum_key = "#{table_name}#{field.identifier}"
  116. value.split(',').each do |v|
  117. enums = (enum_table[enum_key] ||= {})
  118. enums[v] = enums.size
  119. end
  120. end
  121. end
  122. end
  123. buffer.puts "#{field.identifier}=#{value}"
  124. count += 1
  125. end
  126. buffer.puts
  127. register << id
  128. end
  129. # 导出语言表
  130. if !texts.empty?
  131. csf = CSV.open("#{text_path}/#{table.raw_name}.csv", 'wb')
  132. csf << ['', language]
  133. texts.keys.sort.each do |k|
  134. csf << [k, texts[k]]
  135. end
  136. end
  137. end
  138. # 导出 ini
  139. File.open("#{ini_path}/#{ini_name}.ini", 'wb') do |ini|
  140. # 导出注册表
  141. if !register_table.empty?
  142. ini.puts '[REGISTER]'
  143. register_table.each_pair do |table_name, register|
  144. ini.puts "#{table_name}=#{register.join(',')}"
  145. end
  146. ini.puts
  147. end
  148. # 导出枚举
  149. if !enum_table.empty?
  150. ini.puts '[ENUM]'
  151. enum_table.keys.sort.each do |enum_name|
  152. ini.puts "#{enum_name}=#{enum_table[enum_name].keys.join(',')}"
  153. end
  154. ini.puts
  155. end
  156. # 导出所有条目
  157. ini.puts buffer.string
  158. end
  159. end