| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 | 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<string> _names = new List<string>();        private static List<string> _types = new List<string>();        private static List<int> _indexs = new List<int>();        private static Dictionary<string, string> _idDic = new Dictionary<string, string>();        private static Dictionary<string, string> _itemTypeDicByName = new Dictionary<string, string>();        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<string> values = new List<string>();            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');            }        }    }}
 |