CodeBuilder.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 == "float")
  114. {
  115. _assignmentBuilder.AppendFormat("\t\t\t{0} = ConfigUtil.GetFloat(reader.GetString(reader.GetOrdinal(\"_{1}\")));", name, name);
  116. _assignmentBuilder.AppendLine();
  117. }
  118. else if (type == "string")
  119. {
  120. _assignmentBuilder.AppendFormat("\t\t\t{0} = reader.GetString(reader.GetOrdinal(\"_{1}\"));", name, name);
  121. _assignmentBuilder.AppendLine();
  122. }
  123. else
  124. {
  125. _assignmentBuilder.AppendFormat("\t\t\tvar temp{0} = reader.GetString(reader.GetOrdinal(\"_{1}\"));", name, name);
  126. _assignmentBuilder.AppendLine();
  127. if (type.Contains("[][]"))
  128. {
  129. string subType = type.Replace("[][]", "");
  130. _assignmentBuilder.AppendFormat("\t\t\t{0}Arr = ConfigUtil.GetTwoDimensionalArr<{1}>(temp{2});", name, subType, name);
  131. _assignmentBuilder.AppendLine();
  132. }
  133. else if (type.Contains("[]"))
  134. {
  135. string subType = type.Replace("[]", "");
  136. _assignmentBuilder.AppendFormat("\t\t\t{0}Arr = ConfigUtil.GetLinearArr<{1}>(temp{2});", name, subType, name);
  137. _assignmentBuilder.AppendLine();
  138. }
  139. }
  140. }
  141. }
  142. private static void AddDataToSql(DataRowCollection rowCollection, string configName, string configArrayName)
  143. {
  144. int rowNum = rowCollection.Count;
  145. for (int i = 4; i < rowNum; i++)
  146. {
  147. DataRow dataRow = rowCollection[i];
  148. string key = dataRow[0].ToString();
  149. if (key.Length == 0)
  150. {
  151. continue;
  152. }
  153. //解析每行的数据
  154. WriteRowDataToSqlite(dataRow, configArrayName);
  155. if (_idDic.ContainsKey(key))
  156. {
  157. _hasSameIds = true;
  158. }
  159. else
  160. {
  161. _idDic[key] = key;
  162. }
  163. }
  164. }
  165. private static void WriteRowDataToSqlite(DataRow row, string configArrayName)
  166. {
  167. List<string> values = new List<string>();
  168. var keyValue = row[0].ToString();
  169. foreach (var i in _indexs)
  170. {
  171. var fieldName = _names[i];
  172. var value = row[i].ToString();
  173. if (configArrayName == nameof(ItemTypeCfgArray))
  174. {
  175. CacheItemTypeByName(keyValue, fieldName, value);
  176. }
  177. else if (configArrayName == nameof(ItemCfgArray))
  178. {
  179. HandleItemCfgField(keyValue, fieldName, ref value);
  180. }
  181. //value = Regex.Replace(value, ":", "/:");
  182. values.Add(value);
  183. }
  184. SQLiteHelper.Instance.InsertValues(configArrayName, values.ToArray());
  185. }
  186. private static void CacheItemTypeByName(string idStr, string fieldName, string value)
  187. {
  188. if (fieldName == SQL_PREFIX + "name")
  189. {
  190. _itemTypeDicByName[value] = idStr;
  191. }
  192. }
  193. private static void HandleItemCfgField(string idStr, string fieldName, ref string value)
  194. {
  195. fieldName = fieldName.Substring(1);
  196. int id = int.Parse(idStr);
  197. int itemType = (int)Mathf.Floor(id / GameConst.MAX_COUNT_EVERY_TYPE_ITEM);
  198. if (fieldName == "itemType")
  199. {
  200. if (itemType <= ConstDressUpItemType.MAX)
  201. {
  202. value = ConstItemType.DRESS_UP.ToString();
  203. }
  204. else
  205. {
  206. value = itemType.ToString();
  207. }
  208. }
  209. else if (fieldName == "subType")
  210. {
  211. if (itemType <= ConstDressUpItemType.MAX)
  212. {
  213. string subType;
  214. if (_itemTypeDicByName.TryGetValue(value, out subType))
  215. {
  216. value = subType;
  217. }
  218. else
  219. {
  220. Debug.LogError($"请检查物品 {idStr} 的subType值");
  221. }
  222. }
  223. }
  224. else if (fieldName == "resLayer1" || fieldName == "resLayer2")
  225. {
  226. value = value.Replace('n', '1');
  227. value = value.Replace('t', '2');
  228. }
  229. }
  230. }
  231. }