CodeBuilder.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Text;
  7. using UnityEngine;
  8. using GFGGame;
  9. using System.Text.RegularExpressions;
  10. namespace GFGEditor
  11. {
  12. public class CodeBuilder
  13. {
  14. private const string PREFIX = "zzz_temp_";
  15. private const string SQL_PREFIX = "_";
  16. private static StringBuilder _declarationBuilder = new StringBuilder();
  17. private static StringBuilder _assignmentBuilder = new StringBuilder();
  18. private static StringBuilder _parseBuilder = new StringBuilder();
  19. private static bool _hasSameIds;
  20. private static List<string> _names = new List<string>();
  21. private static List<string> _types = new List<string>();
  22. private static List<int> _indexs = new List<int>();
  23. private static Dictionary<string, string> _idDic = new Dictionary<string, string>();
  24. private static Dictionary<string, string> _itemTypeDicByName = new Dictionary<string, string>();
  25. public static void GenerateCode(DataRowCollection rowCollection, string configName, string configArrayName)
  26. {
  27. _declarationBuilder.Clear();
  28. _assignmentBuilder.Clear();
  29. _parseBuilder.Clear();
  30. _names.Clear();
  31. _types.Clear();
  32. _idDic.Clear();
  33. _indexs.Clear();
  34. _hasSameIds = false;
  35. string keyName = null;
  36. string keyType = null;
  37. int len = rowCollection[1].ItemArray.Length;
  38. if (len <= 0)
  39. {
  40. return;
  41. }
  42. for (int i = 0; i < len; ++i)
  43. {
  44. string annotation = rowCollection[0][i].ToString();
  45. string type = rowCollection[1][i].ToString();
  46. string name = rowCollection[2][i].ToString();
  47. if (i == 0)
  48. {
  49. keyName = name;
  50. keyType = type;
  51. }
  52. ParseDataColumn(annotation, type, name, i);
  53. }
  54. //创建sqlite表
  55. SQLiteHelper.Instance.CreateTable(configArrayName, _names.ToArray(), _types.ToArray());
  56. AddDataToSql(rowCollection, configName, configArrayName);
  57. //生成游戏反序列化代码
  58. string configStr = CodeTemplateFactory.ConfigTemplate.Replace("{CfgName}", configName);
  59. configStr = configStr.Replace("{variable declaration}", _declarationBuilder.ToString());
  60. configStr = configStr.Replace("{variable assignment}", _assignmentBuilder.ToString());
  61. configStr = configStr.Replace("{variable parse}", _parseBuilder.ToString());
  62. using (StreamWriter sw = new StreamWriter(ExcelConfig.configCodePath + configName + ".cs"))
  63. {
  64. sw.Write(configStr);
  65. }
  66. _declarationBuilder.Clear();
  67. string template = CodeTemplateFactory.ConfigArrayTemplate;
  68. if (_hasSameIds)
  69. {
  70. template = CodeTemplateFactory.ConfigArrayTemplate2;
  71. }
  72. string configArrayStr = template.Replace("{CfgName}", configName);
  73. configArrayStr = configArrayStr.Replace("{CfgArrayName}", configArrayName);
  74. configArrayStr = configArrayStr.Replace("{keyName}", keyName);
  75. configArrayStr = configArrayStr.Replace("{keyType}", keyType);
  76. using (StreamWriter sw = new StreamWriter(ExcelConfig.configArrayCodePath + configArrayName + ".cs"))
  77. {
  78. sw.Write(configArrayStr);
  79. }
  80. Debug.LogFormat("生成{0}", configName);
  81. }
  82. private static void ParseDataColumn(string annotation, string type, string name, int index)
  83. {
  84. if (type.Length > 0 && name.Length > 0)
  85. {
  86. _names.Add(SQL_PREFIX + name);
  87. _types.Add("TEXT");
  88. _indexs.Add(index);
  89. //生成解析后的数据变量
  90. _declarationBuilder.AppendFormat("\t\t//{0}", annotation);
  91. _declarationBuilder.AppendLine();
  92. string tempName = PREFIX + name;
  93. if (type.Contains("[]"))
  94. {
  95. //_declarationBuilder.AppendFormat("\t\tpublic {0} {1};", "string", tempName);
  96. //_declarationBuilder.AppendLine();
  97. //_declarationBuilder.AppendLine("\t\t[NonSerialized]");
  98. _declarationBuilder.AppendFormat("\t\tpublic {0} {1}Arr;", type, name);
  99. _declarationBuilder.AppendLine();
  100. }
  101. else
  102. {
  103. _declarationBuilder.AppendFormat("\t\tpublic {0} {1};", type, name);
  104. _declarationBuilder.AppendLine();
  105. }
  106. //添加解析代码
  107. if (type == "int")
  108. {
  109. //_assignmentBuilder.AppendFormat("\t\t\t{0} = ConfigUtil.GetInt(0, dataRow, {1});", name, index);
  110. _assignmentBuilder.AppendFormat("\t\t\t{0} = ConfigUtil.GetInt(reader.GetString(reader.GetOrdinal(\"_{1}\")));", name, name);
  111. _assignmentBuilder.AppendLine();
  112. }
  113. else if (type == "string")
  114. {
  115. _assignmentBuilder.AppendFormat("\t\t\t{0} = reader.GetString(reader.GetOrdinal(\"_{1}\"));", name, name);
  116. _assignmentBuilder.AppendLine();
  117. }
  118. else
  119. {
  120. _assignmentBuilder.AppendFormat("\t\t\tvar temp{0} = reader.GetString(reader.GetOrdinal(\"_{1}\"));", name, name);
  121. _assignmentBuilder.AppendLine();
  122. if (type.Contains("[][]"))
  123. {
  124. string subType = type.Replace("[][]", "");
  125. _assignmentBuilder.AppendFormat("\t\t\t{0}Arr = ConfigUtil.GetTwoDimensionalArr<{1}>(temp{2});", name, subType, name);
  126. _assignmentBuilder.AppendLine();
  127. }
  128. else if (type.Contains("[]"))
  129. {
  130. string subType = type.Replace("[]", "");
  131. _assignmentBuilder.AppendFormat("\t\t\t{0}Arr = ConfigUtil.GetLinearArr<{1}>(temp{2});", name, subType, name);
  132. _assignmentBuilder.AppendLine();
  133. }
  134. }
  135. }
  136. }
  137. private static void AddDataToSql(DataRowCollection rowCollection, string configName, string configArrayName)
  138. {
  139. int rowNum = rowCollection.Count;
  140. for (int i = 4; i < rowNum; i++)
  141. {
  142. DataRow dataRow = rowCollection[i];
  143. string key = dataRow[0].ToString();
  144. if (key.Length == 0)
  145. {
  146. continue;
  147. }
  148. //解析每行的数据
  149. WriteRowDataToSqlite(dataRow, configArrayName);
  150. if (_idDic.ContainsKey(key))
  151. {
  152. _hasSameIds = true;
  153. }
  154. else
  155. {
  156. _idDic[key] = key;
  157. }
  158. }
  159. }
  160. private static void WriteRowDataToSqlite(DataRow row, string configArrayName)
  161. {
  162. List<string> values = new List<string>();
  163. var keyValue = row[0].ToString();
  164. foreach (var i in _indexs)
  165. {
  166. var fieldName = _names[i];
  167. var value = row[i].ToString();
  168. if (configArrayName == nameof(ItemTypeCfgArray))
  169. {
  170. CacheItemTypeByName(keyValue, fieldName, value);
  171. }
  172. else if (configArrayName == nameof(ItemCfgArray))
  173. {
  174. HandleItemCfgField(keyValue, fieldName, ref value);
  175. }
  176. //value = Regex.Replace(value, ":", "/:");
  177. values.Add(value);
  178. }
  179. SQLiteHelper.Instance.InsertValues(configArrayName, values.ToArray());
  180. }
  181. private static void CacheItemTypeByName(string idStr, string fieldName, string value)
  182. {
  183. if (fieldName == SQL_PREFIX + "name")
  184. {
  185. _itemTypeDicByName[value] = idStr;
  186. }
  187. }
  188. private static void HandleItemCfgField(string idStr, string fieldName, ref string value)
  189. {
  190. fieldName = fieldName.Substring(1);
  191. int id = int.Parse(idStr);
  192. int itemType = (int)Mathf.Floor(id / GameConst.MAX_COUNT_EVERY_TYPE_ITEM);
  193. if (fieldName == "itemType")
  194. {
  195. if (itemType <= ConstDressUpItemType.MAX)
  196. {
  197. value = ConstItemType.DRESS_UP.ToString();
  198. }
  199. else
  200. {
  201. value = itemType.ToString();
  202. }
  203. }
  204. else if (fieldName == "subType")
  205. {
  206. if (itemType <= ConstDressUpItemType.MAX)
  207. {
  208. string subType;
  209. if (_itemTypeDicByName.TryGetValue(value, out subType))
  210. {
  211. value = subType;
  212. }
  213. else
  214. {
  215. Debug.LogError($"请检查物品 {idStr} 的subType值");
  216. }
  217. }
  218. }
  219. else if (fieldName == "resLayer1" || fieldName == "resLayer2")
  220. {
  221. value = value.Replace('n', '1');
  222. value = value.Replace('t', '2');
  223. }
  224. }
  225. }
  226. }