Ejemplo n.º 1
0
        /// <summary>
        /// 真正进行读取
        /// </summary>
        /// <param name="type"></param>
        /// <param name="contents"></param>
        private void DoLoadTab(Type type, IEnumerable <string> contents)
        {
            Debuger.Assert(typeof(CBaseInfo).IsAssignableFrom(type));
            foreach (string content in contents)
            {
                using (KTabFile tabFile = KTabFile.LoadFromString(content))
                {
                    Dictionary <string, CBaseInfo> dict;
                    if (!SettingInfos.TryGetValue(type, out dict)) // 如果没有才添加
                    {
                        dict = new Dictionary <string, CBaseInfo>();
                    }

                    const int rowStart = 1;
                    for (int row = rowStart; row <= tabFile.GetHeight(); row++)
                    {
                        // 先读取ID, 获取是否之前已经读取过配置,
                        // 如果已经读取过,那么获取原配置对象,并重新赋值 (因为游戏中其它地方已经存在它的引用了,直接替换内存泄露)
                        string    id = tabFile.GetString(row, "Id"); // 获取ID是否存在, 如果已经存在,那么替换其属性,不new一个
                        CBaseInfo existOne;
                        if (dict.TryGetValue(id, out existOne))
                        {
                            if (FoundDuplicatedIdEvent != null)
                            {
                                FoundDuplicatedIdEvent(type, row, id);
                            }

                            CBaseInfo existInfo = existOne;
                            existInfo.ReadFromTab(type, ref existInfo, tabFile, row); // 修改原对象,不new
                            existInfo.CustomReadLine(tabFile, row);
                            (existInfo as CBaseInfo).Parse();
                        }
                        else
                        {
                            CBaseInfo pInfo = CBaseInfo.NewFromTab(type, tabFile, row);
                            pInfo.CustomReadLine(tabFile, row);
                            pInfo.Parse();
                            dict[pInfo.Id] = pInfo; // 不存在,直接new
                        }
                    }

                    SettingInfos[type] = dict;
                }
            }
        }
Ejemplo n.º 2
0
        public void ReadFromTab(Type type, ref CBaseInfo newT, IKTabReadble tabFile, int row)
        {
            if (Debug.isDebugBuild)
            {
                Debuger.Assert(typeof(CBaseInfo).IsAssignableFrom(type));
            }

            // 缓存字段Field, 每个Type只反射一次!
            LinkedList <FieldInfo> okFields;

            if (!CacheTypeFields.TryGetValue(type, out okFields))
            {
                okFields = CacheTypeFields[type] = new LinkedList <FieldInfo>();
                var allFields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                foreach (FieldInfo field in allFields)
                {
                    if (field.Name.StartsWith("_") || field.IsInitOnly) // 筛掉_ 和 readonly
                    {
                        continue;
                    }
                    if (!tabFile.HasColumn(field.Name))
                    {
                        if (Debug.isDebugBuild)
                        {
                            Logger.LogError("表{0} 找不到表头{1}", type.Name, field.Name);
                        }
                        continue;
                    }
                    okFields.AddLast(field);
                }
            }

            // 读字段
            foreach (var field in okFields)
            {
                var fieldName = field.Name;
                var fieldType = field.FieldType;

                object value;
                if (fieldType == typeof(int))
                {
                    value = tabFile.GetInteger(row, fieldName);
                }
                else if (fieldType == typeof(long))
                {
                    value = (long)tabFile.GetInteger(row, fieldName);
                }
                else if (fieldType == typeof(string))
                {
                    value = tabFile.GetString(row, fieldName).Replace("\\n", "\n");
                }
                else if (fieldType == typeof(float))
                {
                    value = tabFile.GetFloat(row, fieldName);
                }
                else if (fieldType == typeof(bool))
                {
                    value = tabFile.GetBool(row, fieldName);
                }
                else if (fieldType == typeof(double))
                {
                    value = tabFile.GetDouble(row, fieldName);
                }
                else if (fieldType == typeof(uint))
                {
                    value = tabFile.GetUInteger(row, fieldName);
                }
                else if (fieldType == typeof(Regex))
                {
                    var str = tabFile.GetString(row, fieldName);
                    value = string.IsNullOrEmpty(str) ? null : new Regex(str);
                }
                else if (fieldType == typeof(List <string>))
                {
                    string sz = tabFile.GetString(row, fieldName);
                    value = KTool.Split <string>(sz, '|');
                }
                else if (fieldType == typeof(List <int>))
                {
                    //List<int> retInt = new List<int>();
                    string szArr = tabFile.GetString(row, fieldName);
                    value = KTool.Split <int>(szArr, '|');
                    //if (!string.IsNullOrEmpty(szArr))
                    //{
                    //    string[] szIntArr = szArr.Split('|');
                    //    foreach (string szInt in szIntArr)
                    //    {
                    //        float parseFloat;
                    //        float.TryParse(szInt, out parseFloat);
                    //        int parseInt_ = (int)parseFloat;
                    //        retInt.Add(parseInt_);
                    //    }

                    //}
                    //else
                    //    value = new List<int>();
                }
                else if (fieldType == typeof(List <List <string> >))
                {
                    string sz = tabFile.GetString(row, fieldName);
                    if (!string.IsNullOrEmpty(sz))
                    {
                        var      szOneList = new List <List <string> >();
                        string[] szArr     = sz.Split('|');
                        foreach (string szOne in szArr)
                        {
                            string[] szOneArr = szOne.Split('-', ':');
                            szOneList.Add(new List <string>(szOneArr));
                        }
                        value = szOneList;
                    }
                    else
                    {
                        value = new List <List <string> >();
                    }
                }
                else if (fieldType == typeof(List <List <int> >))
                {
                    string sz = tabFile.GetString(row, fieldName);
                    if (!string.IsNullOrEmpty(sz))
                    {
                        var      zsOneIntList = new List <List <int> >();
                        string[] szArr        = sz.Split('|');
                        foreach (string szOne in szArr)
                        {
                            List <int> retInts  = new List <int>();
                            string[]   szOneArr = szOne.Split('-', ':');
                            foreach (string szOneInt in szOneArr)
                            {
                                float parseFloat;
                                float.TryParse(szOneInt, out parseFloat);
                                int parseInt_ = (int)parseFloat;
                                retInts.Add(parseInt_);
                            }
                            zsOneIntList.Add(retInts);
                        }
                        value = zsOneIntList;
                    }
                    else
                    {
                        value = new List <List <int> >();
                    }
                }
                //else if (fieldType == typeof (JsonObject))
                //{
                //    string sz = tabFile.GetString(row, fieldName);
                //    value = string.IsNullOrEmpty(sz) ? new JsonObject() : KTool.SplitToJson(sz);
                //}
                else
                {
                    Logger.LogWarning("未知类型: {0}", fieldName);
                    value = null;
                }

                if (fieldName == "Id") // 如果是Id主键,确保数字成整数!
                {
                    int fValue;
                    if (int.TryParse((string)value, out fValue))
                    {
                        try
                        {
                            value = fValue.ToString();
                        }
                        catch
                        {
                            Logger.LogError("转型错误...{0}", value.ToString());
                        }
                    }
                }

                field.SetValue(newT, value);
            }
        }