Пример #1
0
        public static Dictionary <K, T> ParseEx <K, T>(string csvPath, string keyCSVMark) where T : new()
        {
            string text = string.Empty;

            text = ResMgr.instance.LoadText(csvPath);
            if (string.IsNullOrEmpty(text))
            {
                Debug.LogErrorFormat("load {0} fail", csvPath);
                return(null);
            }
            Dictionary <K, T> result = new Dictionary <K, T>();

            string[] lineArr = SplitLines(text);
            Dictionary <string, int> titleDic = ParseTitle(lineArr[0]);

            for (int i = 1, lineLen = lineArr.Length; i < lineLen; i++)//第一行是title所以i=1开始
            {
                if (lineArr[i].StartsWith(SYMBOL_COMMENT))
                {
                    continue;
                }
                K        key    = default(K);
                string[] strArr = lineArr[i].Split(SYMBOL_FIELD, StringSplitOptions.None);

                T           instance = System.Activator.CreateInstance <T>();
                FieldInfo[] fieldArr = instance.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
                for (int k = 0, fieldLen = fieldArr.Length; k < fieldLen; k++)
                {
                    FieldInfo       field   = fieldArr[k];
                    System.Object[] attrArr = field.GetCustomAttributes(typeof(CSVElementAttribute), false);
                    if (attrArr.Length > 0)
                    {
                        CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                        if (!titleDic.ContainsKey(csvele.key))
                        {
                            Debug.Log(csvele.key);
                        }
                        string        valueString = strArr[titleDic[csvele.key]];
                        System.Object valueObject = null;
                        try
                        {
                            if (field.FieldType == typeof(string))
                            {
                                valueObject = valueString;
                            }
                            else if (field.FieldType == typeof(bool))
                            {
                                valueObject = valueString != "0";
                            }
                            else
                            {
                                valueObject = Convert.ChangeType(valueString, field.FieldType);
                            }
                        }
                        catch (Exception)
                        {
                            Debug.LogError("change type error,can't find type:" + field.FieldType + "  fieldName:" + field.Name + " value:" + valueString);
                        }
                        field.SetValue(instance, valueObject);

                        if (csvele.key == keyCSVMark)
                        {
                            key = (K)valueObject;
                        }
                    }
                }



                PropertyInfo[] propertyInfoArr = instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
                for (int k = 0, propertyLen = propertyInfoArr.Length; k < propertyLen; k++)
                {
                    PropertyInfo    pi      = propertyInfoArr[k];
                    System.Object[] attrArr = pi.GetCustomAttributes(typeof(CSVElementAttribute), false);
                    if (attrArr.Length > 0)
                    {
                        CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                        string        valueString  = strArr[titleDic[csvele.key]];
                        System.Object valueObject  = null;
                        try
                        {
                            if (pi.PropertyType == typeof(string))
                            {
                                valueObject = valueString;
                            }
                            else
                            {
                                valueObject = Convert.ChangeType(valueString, pi.PropertyType);
                            }
                        }
                        catch (Exception)
                        {
                            Debug.LogError("change type error,can't find type:" + pi.PropertyType + "  fieldName:" + pi.Name + " value:" + valueString);
                        }
                        pi.SetValue(instance, valueObject, null);

                        if (csvele.key == keyCSVMark)
                        {
                            key = (K)valueObject;
                        }
                    }
                }
                if (!result.ContainsKey(key))
                {
                    result.Add(key, instance);
                }
                else
                {
                    result[key] = instance;
                }
            }

            return(result);
        }
Пример #2
0
        public static Dictionary <K, T> ParseEx <K, T>(string csvPath, string keyCSVMark) where T : new()
        {
            string text = string.Empty;

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                TextAsset ta = Resources.Load <TextAsset>(csvPath);
                if (ta == null)
                {
                    return(null);
                }
                text = ta.text;
                Resources.UnloadAsset(ta);
            }
            else
            {
#endif
            string localPath = csvPath + ".csv";
#if UNITY_EDITOR
            if (Logic.Game.GameConfig.instance.loadCSVRemote)
#else
            if (Common.ResMgr.ResUtil.ExistsInLocal(localPath))
#endif
            { text = ResMgr.ResMgr.instance.LoadText(localPath); }
            else
            {
                TextAsset ta = Common.ResMgr.ResMgr.instance.Load <TextAsset>(csvPath);
                if (ta == null)
                {
                    return(null);
                }
                text = ta.text;
                Resources.UnloadAsset(ta);
            }
#if UNITY_EDITOR
        }
#endif
            if (string.IsNullOrEmpty(text))
            {
                Debugger.LogError("load {0} fail", csvPath);
                return(null);
            }
            Dictionary <K, T> result          = new Dictionary <K, T>();
            string[] lineArr                  = text.Split(SYMBOL_LINE, StringSplitOptions.RemoveEmptyEntries);
            Dictionary <string, int> titleDic = ParseTitle(lineArr[0]);
            for (int i = 1, lineLen = lineArr.Length; i < lineLen; i++)//第一行是title所以i=1开始
            {
                if (lineArr[i].StartsWith(SYMBOL_COMMENT))
                {
                    continue;
                }
                K key           = default(K);
                string[] strArr = lineArr[i].Split(SYMBOL_FIELD, StringSplitOptions.None);

                T instance           = System.Activator.CreateInstance <T>();
                FieldInfo[] fieldArr = instance.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
                for (int k = 0, fieldLen = fieldArr.Length; k < fieldLen; k++)
                {
                    FieldInfo field         = fieldArr[k];
                    System.Object[] attrArr = field.GetCustomAttributes(typeof(CSVElementAttribute), false);
                    if (attrArr.Length > 0)
                    {
                        CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                        if (!titleDic.ContainsKey(csvele.key))
                        {
                            Debugger.Log(csvele.key);
                        }
                        string valueString        = strArr[titleDic[csvele.key]];
                        System.Object valueObject = null;
                        try
                        {
                            if (field.FieldType == typeof(string))
                            {
                                valueObject = valueString;
                            }
                            else if (field.FieldType == typeof(bool))
                            {
                                valueObject = valueString != "0";
                            }
                            else
                            {
                                valueObject = Convert.ChangeType(valueString, field.FieldType);
                            }
                        }
                        catch (Exception)
                        {
                            Debugger.LogError("change type error,can't find type:" + field.FieldType + "  fieldName:" + field.Name + " value:" + valueString);
                        }
                        field.SetValue(instance, valueObject);

                        if (csvele.key == keyCSVMark)
                        {
                            key = (K)valueObject;
                        }
                    }
                }



                PropertyInfo[] propertyInfoArr = instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
                for (int k = 0, propertyLen = propertyInfoArr.Length; k < propertyLen; k++)
                {
                    PropertyInfo pi         = propertyInfoArr[k];
                    System.Object[] attrArr = pi.GetCustomAttributes(typeof(CSVElementAttribute), false);
                    if (attrArr.Length > 0)
                    {
                        CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                        string valueString         = strArr[titleDic[csvele.key]];
                        System.Object valueObject  = null;
                        try
                        {
                            if (pi.PropertyType == typeof(string))
                            {
                                valueObject = valueString;
                            }
                            else
                            {
                                valueObject = Convert.ChangeType(valueString, pi.PropertyType);
                            }
                        }
                        catch (Exception)
                        {
                            Debugger.LogError("change type error,can't find type:" + pi.PropertyType + "  fieldName:" + pi.Name + " value:" + valueString);
                        }
                        pi.SetValue(instance, valueObject, null);

                        if (csvele.key == keyCSVMark)
                        {
                            key = (K)valueObject;
                        }
                    }
                }
                if (!result.ContainsKey(key))
                {
                    result.Add(key, instance);
                }
                else
                {
                    result[key] = instance;
                }
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// 解析为单类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="csvPath"></param>
        /// <returns></returns>
        public static T ParseClass <T>(string csvPath) where T : new()
        {
            string text = string.Empty;

            text = ResMgr.instance.LoadText(csvPath);
            if (string.IsNullOrEmpty(text))
            {
                Debug.LogErrorFormat("load {0} fail", csvPath);
                return(default(T));
            }
            T instance = System.Activator.CreateInstance <T>();

            FieldInfo[] fieldArr = instance.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);

            string[] lineArr = SplitLines(text);
            Dictionary <string, string> lineDic = new Dictionary <string, string>();

            for (int i = 2, len = lineArr.Length; i < len; i++)
            {
                //忽略前2行
                string line = lineArr[i];
                if (line.StartsWith(SYMBOL_COMMENT))
                {
                    continue;
                }
                //                string[] strArr = line.Split(CSVUtil.SYMBOL_FIELD, 2);
                string[] strArr = line.Split(CSVUtil.SYMBOL_FIELD);
                lineDic.Add(strArr[0], strArr[1]);
            }

            for (int k = 0, fieldLen = fieldArr.Length; k < fieldLen; k++)
            {
                FieldInfo       field   = fieldArr[k];
                System.Object[] attrArr = field.GetCustomAttributes(typeof(CSVElementAttribute), false);
                if (attrArr.Length > 0)
                {
                    CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                    if (!lineDic.ContainsKey(csvele.key))
                    {
                        Debug.Log(csvele.key);
                    }
                    string        valueString = lineDic[csvele.key];
                    System.Object valueObject = null;
                    try
                    {
                        if (field.FieldType == typeof(string))
                        {
                            valueObject = valueString;
                        }
                        else if (field.FieldType == typeof(bool))
                        {
                            valueObject = valueString != "0";
                        }
                        else
                        {
                            valueObject = Convert.ChangeType(valueString, field.FieldType);
                        }
                    }
                    catch (Exception)
                    {
                        Debug.LogError("change type error,can't find type:" + field.FieldType + "  fieldName:" + field.Name + " value:" + valueString);
                    }
                    field.SetValue(instance, valueObject);
                }
            }



            PropertyInfo[] propertyInfoArr = instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            for (int k = 0, propertyLen = propertyInfoArr.Length; k < propertyLen; k++)
            {
                PropertyInfo    pi      = propertyInfoArr[k];
                System.Object[] attrArr = pi.GetCustomAttributes(typeof(CSVElementAttribute), false);
                if (attrArr.Length > 0)
                {
                    CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                    string valueString         = lineDic[csvele.key];
                    if (!lineDic.ContainsKey(csvele.key))
                    {
                        Debug.LogError(csvele.key);
                    }
                    System.Object valueObject = null;
                    try
                    {
                        if (pi.PropertyType == typeof(string))
                        {
                            valueObject = valueString;
                        }
                        else
                        {
                            valueObject = Convert.ChangeType(valueString, pi.PropertyType);
                        }
                    }
                    catch (Exception)
                    {
                        Debug.LogError("change type error,can't find type:" + pi.PropertyType + "  fieldName:" + pi.Name + " value:" + valueString);
                    }
                    pi.SetValue(instance, valueObject, null);
                }
            }
            return(instance);
        }