using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using System.Text; using UnityEngine; using GFGGame; using System.Text.RegularExpressions; namespace GFGEditor { public class CodeBuilder { private const string PREFIX = "zzz_temp_"; private const string SQL_PREFIX = "_"; private static StringBuilder _declarationBuilder = new StringBuilder(); private static StringBuilder _assignmentBuilder = new StringBuilder(); private static StringBuilder _parseBuilder = new StringBuilder(); private static bool _hasSameIds; private static List _names = new List(); private static List _types = new List(); private static List _indexs = new List(); private static Dictionary _idDic = new Dictionary(); private static Dictionary _itemTypeDicByName = new Dictionary(); public static void GenerateCode(DataRowCollection rowCollection, string configName, string configArrayName) { _declarationBuilder.Clear(); _assignmentBuilder.Clear(); _parseBuilder.Clear(); _names.Clear(); _types.Clear(); _idDic.Clear(); _indexs.Clear(); _hasSameIds = false; string keyName = null; string keyType = null; int len = rowCollection[1].ItemArray.Length; if (len <= 0) { return; } for (int i = 0; i < len; ++i) { string annotation = rowCollection[0][i].ToString(); string type = rowCollection[1][i].ToString(); string name = rowCollection[2][i].ToString(); if (i == 0) { keyName = name; keyType = type; } ParseDataColumn(annotation, type, name, i); } //创建sqlite表 SQLiteHelper.Instance.CreateTable(configArrayName, _names.ToArray(), _types.ToArray()); AddDataToSql(rowCollection, configName, configArrayName); //生成游戏反序列化代码 string configStr = CodeTemplateFactory.ConfigTemplate.Replace("{CfgName}", configName); configStr = configStr.Replace("{variable declaration}", _declarationBuilder.ToString()); configStr = configStr.Replace("{variable assignment}", _assignmentBuilder.ToString()); configStr = configStr.Replace("{variable parse}", _parseBuilder.ToString()); using (StreamWriter sw = new StreamWriter(ExcelConfig.configCodePath + configName + ".cs")) { sw.Write(configStr); } _declarationBuilder.Clear(); string template = CodeTemplateFactory.ConfigArrayTemplate; if (_hasSameIds) { template = CodeTemplateFactory.ConfigArrayTemplate2; } string configArrayStr = template.Replace("{CfgName}", configName); configArrayStr = configArrayStr.Replace("{CfgArrayName}", configArrayName); configArrayStr = configArrayStr.Replace("{keyName}", keyName); configArrayStr = configArrayStr.Replace("{keyType}", keyType); using (StreamWriter sw = new StreamWriter(ExcelConfig.configArrayCodePath + configArrayName + ".cs")) { sw.Write(configArrayStr); } Debug.LogFormat("生成{0}", configName); } private static void ParseDataColumn(string annotation, string type, string name, int index) { if (type.Length > 0 && name.Length > 0) { _names.Add(SQL_PREFIX + name); _types.Add("TEXT"); _indexs.Add(index); //生成解析后的数据变量 _declarationBuilder.AppendFormat("\t\t//{0}", annotation); _declarationBuilder.AppendLine(); string tempName = PREFIX + name; if (type.Contains("[]")) { //_declarationBuilder.AppendFormat("\t\tpublic {0} {1};", "string", tempName); //_declarationBuilder.AppendLine(); //_declarationBuilder.AppendLine("\t\t[NonSerialized]"); _declarationBuilder.AppendFormat("\t\tpublic {0} {1}Arr;", type, name); _declarationBuilder.AppendLine(); } else { _declarationBuilder.AppendFormat("\t\tpublic {0} {1};", type, name); _declarationBuilder.AppendLine(); } //添加解析代码 if (type == "int") { //_assignmentBuilder.AppendFormat("\t\t\t{0} = ConfigUtil.GetInt(0, dataRow, {1});", name, index); _assignmentBuilder.AppendFormat("\t\t\t{0} = ConfigUtil.GetInt(reader.GetString(reader.GetOrdinal(\"_{1}\")));", name, name); _assignmentBuilder.AppendLine(); } else if (type == "float") { _assignmentBuilder.AppendFormat("\t\t\t{0} = ConfigUtil.GetFloat(reader.GetString(reader.GetOrdinal(\"_{1}\")));", name, name); _assignmentBuilder.AppendLine(); } else if (type == "string") { _assignmentBuilder.AppendFormat("\t\t\t{0} = reader.GetString(reader.GetOrdinal(\"_{1}\"));", name, name); _assignmentBuilder.AppendLine(); } else { _assignmentBuilder.AppendFormat("\t\t\tvar temp{0} = reader.GetString(reader.GetOrdinal(\"_{1}\"));", name, name); _assignmentBuilder.AppendLine(); if (type.Contains("[][]")) { string subType = type.Replace("[][]", ""); _assignmentBuilder.AppendFormat("\t\t\t{0}Arr = ConfigUtil.GetTwoDimensionalArr<{1}>(temp{2});", name, subType, name); _assignmentBuilder.AppendLine(); } else if (type.Contains("[]")) { string subType = type.Replace("[]", ""); _assignmentBuilder.AppendFormat("\t\t\t{0}Arr = ConfigUtil.GetLinearArr<{1}>(temp{2});", name, subType, name); _assignmentBuilder.AppendLine(); } } } } private static void AddDataToSql(DataRowCollection rowCollection, string configName, string configArrayName) { int rowNum = rowCollection.Count; for (int i = 4; i < rowNum; i++) { DataRow dataRow = rowCollection[i]; string key = dataRow[0].ToString(); if (key.Length == 0) { continue; } //解析每行的数据 WriteRowDataToSqlite(dataRow, configArrayName); if (_idDic.ContainsKey(key)) { _hasSameIds = true; } else { _idDic[key] = key; } } } private static void WriteRowDataToSqlite(DataRow row, string configArrayName) { List values = new List(); var keyValue = row[0].ToString(); foreach (var i in _indexs) { var fieldName = _names[i]; var value = row[i].ToString(); if (configArrayName == nameof(ItemTypeCfgArray)) { CacheItemTypeByName(keyValue, fieldName, value); } else if (configArrayName == nameof(ItemCfgArray)) { HandleItemCfgField(keyValue, fieldName, ref value); } //value = Regex.Replace(value, ":", "/:"); values.Add(value); } SQLiteHelper.Instance.InsertValues(configArrayName, values.ToArray()); } private static void CacheItemTypeByName(string idStr, string fieldName, string value) { if (fieldName == SQL_PREFIX + "name") { _itemTypeDicByName[value] = idStr; } } private static void HandleItemCfgField(string idStr, string fieldName, ref string value) { fieldName = fieldName.Substring(1); int id = int.Parse(idStr); int itemType = (int)Mathf.Floor(id / GameConst.MAX_COUNT_EVERY_TYPE_ITEM); if (fieldName == "itemType") { if (itemType <= ConstDressUpItemType.MAX) { value = ConstItemType.DRESS_UP.ToString(); } else { value = itemType.ToString(); } } else if (fieldName == "subType") { if (itemType <= ConstDressUpItemType.MAX) { string subType; if (_itemTypeDicByName.TryGetValue(value, out subType)) { value = subType; } else { Debug.LogError($"请检查物品 {idStr} 的subType值"); } } } else if (fieldName == "resLayer1" || fieldName == "resLayer2") { value = value.Replace('n', '1'); value = value.Replace('t', '2'); } } } }