Example #1
0
    public void LoadConfig(ConfigData data, ConfigSheetLine[] lines)
    {
        Debug.Assert(data != null && lines != null && lines.Length > 0);
        ConfigSheetLine content = lines[0];

        LoadConfigByLine(data, content);
    }
Example #2
0
    public void LoadConfig(ConfigData data, int id)
    {
        Debug.Assert(data != null);
        ConfigSheetLine content = data.GetData(id);

        LoadConfigByLine(data, content);
    }
Example #3
0
    public void LoadConfigByLine(ConfigData data, ConfigSheetLine content)
    {
        Debug.Assert(data != null && content != null);

        FieldInfo[] infos = GetType().GetFields();
        for (int i = 0; i < infos.Length; ++i)
        {
            FieldInfo info       = infos[i];
            int       fieldIndex = data.GetFieldByName(info.Name);
            if (fieldIndex == -1)
            {
                continue;
            }

            object fieldValue = content.GetData(fieldIndex);
            int    fieldType  = data.GetDataType(fieldIndex);
            switch (fieldType)
            {
            case ConfigDataType.UNKNOWN:
            case ConfigDataType.INT:
            case ConfigDataType.BOOL:
            case ConfigDataType.FLOAT:
            case ConfigDataType.LONG:
            case ConfigDataType.DOUBLE:
            case ConfigDataType.STRING:
            case ConfigDataType.ENUM:
            case ConfigDataType.JSON:
                info.SetValue(this, fieldValue);
                break;

            case ConfigDataType.ARRAY:
                Type dataType    = info.FieldType;
                Type elementType = dataType.GetElementType();
                if (dataType.IsArray && (elementType == typeof(int) || elementType.IsEnum || elementType == typeof(bool) || elementType == typeof(float)) || elementType == typeof(long) || elementType == typeof(double) || elementType == typeof(string))
                {
                    DecodeArrayValue(info, fieldValue, elementType);
                }
                else
                {
                    DecodeSpecialValue(info, fieldValue);
                }
                break;

            case ConfigDataType.CUSTOM:
                DecodeSpecialValue(info, fieldValue);
                break;

            case ConfigDataType.DATETIME:
                if (!string.IsNullOrEmpty((string)fieldValue))
                {
                    info.SetValue(this, Convert.ToDateTime(fieldValue));
                }
                break;
            }
        }
    }
Example #4
0
    public object GetDataByRow(int row, int field)
    {
        ConfigSheetLine dataByRow = GetDataByRow(row);

        if (dataByRow != null)
        {
            return(dataByRow.GetData(field));
        }
        return(null);
    }
Example #5
0
    public object GetData(int id, int field)
    {
        ConfigSheetLine data = GetData(id);

        if (data != null)
        {
            return(data.GetData(field));
        }
        return(null);
    }
Example #6
0
    public int[] fieldTypes;    // 字段类型列表
    //public byte FileType; // 配置表类型:1-制表符分割的csv文件,2-xml文件
    //public string xmlText;

    public ConfigSheetLine GetData(int id)
    {
        for (int i = 0; i < this.data.Length; ++i)
        {
            ConfigSheetLine line = this.data[i];
            if (((int)line.GetData(0)) == id)
            {
                return(line);
            }
        }
        return(null);
    }
Example #7
0
 public void LoadConfig(ConfigData data, string[] fieldNames, object[] values)
 {
     if (data != null)
     {
         ConfigSheetLine[] linesByFieldNameValue = data.GetLinesByFieldNameValue(fieldNames, values);
         if (linesByFieldNameValue != null && linesByFieldNameValue.Length > 0)
         {
             ConfigSheetLine content = linesByFieldNameValue[0];
             LoadConfigByLine(data, content);
         }
     }
 }
Example #8
0
    public object GetDataByRow(int row, string fieldName)
    {
        ConfigSheetLine dataByRow = GetDataByRow(row);

        if (dataByRow != null)
        {
            int field = GetFieldByName(fieldName);
            if (field >= 0)
            {
                return(dataByRow.GetData(field));
            }
        }
        return(null);
    }
Example #9
0
    public string GetCommmonValue(string key)
    {
        if (_commonData == null)
        {
            _commonData = GetConfig("Common") as ConfigData;
        }

        ConfigSheetLine line = _commonData.GetDataByName(key);

        if (line != null)
        {
            return((string)line.GetData(1, ConfigDataType.STRING));
        }
        return(null);
    }
Example #10
0
    public void LoadConfigByLine(ConfigData data, ConfigSheetLine content)
    {
        if (data != null && content != null)
        {
            FieldInfo[] fields = GetType().GetFields();
            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo fi = fields[i];

                int fieldIndex = data.GetFieldByName(fi.Name);
                if (fieldIndex != -1)
                {
                    int    defineType = data.GetDataType(fieldIndex);
                    int    typeindex  = data.GetTypeIndex(fieldIndex);
                    object ovalue     = content.GetData(typeindex, defineType);

                    switch (defineType)
                    {
                    case ConfigDataType.INT:
                    case ConfigDataType.ENUM:
                    case ConfigDataType.BOOL:
                    case ConfigDataType.FLOAT:
                    case ConfigDataType.STRING:
                        fi.SetValue(this, ovalue);
                        break;

                    case ConfigDataType.ARRAY:
                        Type type  = fi.FieldType;
                        Type stype = type.GetElementType();
                        if (type.IsArray && (stype == typeof(int) || stype == typeof(string) || stype == typeof(float)))
                        {
                            DecodeArrayValue(fi, ovalue, stype);
                        }
                        else
                        {
                            DecodeSpecialValue(fi, ovalue);
                        }
                        break;

                    case ConfigDataType.CUSTOM:
                        //交由各个具体的子列去实现解析
                        DecodeSpecialValue(fi, ovalue);
                        break;
                    }
                }
            }
        }
    }
Example #11
0
    public ConfigSheetLine[] GetLinesByFieldNameValue(string[] fieldNames, object[] values)
    {
        Debug.Assert(((fieldNames != null) && (values != null)) && (fieldNames.Length == values.Length));

        int length = fieldNames.Length;

        int[] fieldIndexs = new int[length];
        for (int i = 0; i < length; i++)
        {
            fieldIndexs[i] = GetFieldByName(fieldNames[i]);
            if (fieldIndexs[i] < 0)
            {
                return(null);
            }
        }
        List <ConfigSheetLine> list = null;

        for (int i = 0; i < this.data.Length; ++i)
        {
            ConfigSheetLine line = this.data[i];
            bool            flag = true;
            for (int j = 0; j < length; j++)
            {
                if (!line.GetData(fieldIndexs[j]).Equals(values[j]))
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                if (list == null)
                {
                    list = new List <ConfigSheetLine>();
                }
                list.Add(line);
            }
        }
        if (list != null)
        {
            return(list.ToArray());
        }
        return(null);
    }
Example #12
0
 public bool Add(ConfigSheetLine line)
 {
             #if UNITY_EDITOR
     if (_dicId != null && _dicId.ContainsKey(line.GetId()))
     {
         return(false);
     }
     if (_dicName != null && _dicName.ContainsKey(line.GetKey()))
     {
         return(false);
     }
             #endif
     if (_dicId != null)
     {
         _dicId[line.GetId()] = line;
     }
     else
     {
         _dicName[line.GetKey()] = line;
     }
     return(true);
 }
Example #13
0
    public SOBase GetConfig(string url)
    {
        if (_configs.ContainsKey(url) == false)
        {
            //TextAsset asset =  ResourcesManager.Instance.LoadAsset("Config/"+url) as TextAsset;

            TextAsset asset = Resources.Load <TextAsset>("Config/" + url);

            using (var sr = new StringReader(asset.text))
            {
                var names      = ReadLine(sr);
                var configData = ScriptableObject.CreateInstance <ConfigData>();
                configData.fieldNames = names;
                int column = names.Length;
                var types  = ReadLine(sr);
                configData.fieldTypes = new int[column];
                configData.typeIndexs = new int[column];
                configData.InitData(types[0]);
                int intlen    = 0;
                int stringlen = 0;
                int boollen   = 0;
                int floatlen  = 0;
                for (int i = 0; i < column; i++)
                {
                    string type = types[i].ToLower();
                    SetIndex(type, configData.typeIndexs, i, ref intlen, ref stringlen, ref boollen, ref floatlen);

                    switch (type)
                    {
                    case "int":
                        configData.fieldTypes[i] = ConfigDataType.INT;
                        break;

                    case "string":
                        configData.fieldTypes[i] = ConfigDataType.STRING;
                        break;

                    case "bool":
                        configData.fieldTypes[i] = ConfigDataType.BOOL;
                        break;

                    case "float":
                        configData.fieldTypes[i] = ConfigDataType.FLOAT;
                        break;

                    case "enum":
                        configData.fieldTypes[i] = ConfigDataType.ENUM;
                        break;

                    case "array":
                        configData.fieldTypes[i] = ConfigDataType.ARRAY;
                        break;

                    case "custom":
                        configData.fieldTypes[i] = ConfigDataType.CUSTOM;
                        break;
                    }
                }
                configData.InitLine(intlen, stringlen, boollen, floatlen);

                ReadLine(sr);

                string[] arr       = null;
                int      lineCount = 3;
                while ((arr = ReadLine(sr)) != null)
                {
                    lineCount++;
                    var lineObj = new ConfigSheetLine();
                    lineObj.InitLine(intlen, stringlen, boollen, floatlen);
                    for (int i = 0; i < column; i++)
                    {
                        try
                        {
                            lineObj.SetData(arr[i], configData.fieldTypes[i], configData.typeIndexs[i]);
                        }
                        catch (FormatException e)
                        {
                            throw new FormatException(url + " 数据格式转换错误:第" + lineCount + "行,第" + (i + 1) + "列\r\n" + e.StackTrace);
                        }
                    }

                    bool b = configData.Add(lineObj);
                    if (b == false)
                    {
                        throw new Exception("重复的ID项:第" + lineCount + "行" + ";" + url);
                    }
                }

                _configs[url] = configData;
            }
        }
        return(_configs[url]);
    }
Example #14
0
    private static ConfigData ImportCsv(FileInfo file)
    {
        StreamReader sr     = null;
        FileStream   stream = null;

        try
        {
            #region 以Unicode编码打开csv文件
            try
            {
                stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sr     = new StreamReader(stream, Encoding.Unicode);
            }
            catch (IOException exception)
            {
                throw new IOException("读取文件出错:" + file.FullName + "\r\n" + exception.StackTrace);
            }
            #endregion

            #region 解析列表名和数据类型,忽略描述

            // 列名
            string[]      fieldNames    = ReadLine(sr);
            List <string> fieldNameList = new List <string>();
            for (int i = 0; i < fieldNames.Length; ++i)
            {
                if (Util.IsNullOrWhiteSpace(fieldNames[i]))
                {
                    continue;
                }
                fieldNameList.Add(fieldNames[i]);
            }
            ConfigData config = new ConfigData
            {
                //FileType = 1,
                fieldNames = fieldNameList.ToArray()
            };
            int column = config.fieldNames.Length;

            // 列类型
            string[] types = ReadLine(sr);
            config.fieldTypes = new int[column];
            for (int i = 0; i < column; i++)
            {
                if (i < types.Length)
                {
                    switch (types[i])
                    {
                    case "int":
                        config.fieldTypes[i] = ConfigDataType.INT;
                        break;

                    case "bool":
                        config.fieldTypes[i] = ConfigDataType.BOOL;
                        break;

                    case "float":
                        config.fieldTypes[i] = ConfigDataType.FLOAT;
                        break;

                    case "long":
                        config.fieldTypes[i] = ConfigDataType.LONG;
                        break;

                    case "double":
                        config.fieldTypes[i] = ConfigDataType.DOUBLE;
                        break;

                    case "string":
                        config.fieldTypes[i] = ConfigDataType.STRING;
                        break;

                    case "enum":
                        config.fieldTypes[i] = ConfigDataType.ENUM;
                        break;

                    case "array":
                        config.fieldTypes[i] = ConfigDataType.ARRAY;
                        break;

                    case "json":
                        config.fieldTypes[i] = ConfigDataType.JSON;
                        break;

                    case "date":
                        config.fieldTypes[i] = ConfigDataType.DATETIME;
                        break;

                    case "custom":
                        config.fieldTypes[i] = ConfigDataType.CUSTOM;
                        break;

                    default:
                        Debug.LogError(file.Name + " - 未识别的数据类型格式:\"" + types[i] + "\" 在第\"" + i + 1 + "\"列");
                        config.fieldTypes[i] = ConfigDataType.UNKNOWN;
                        break;
                    }
                }
                else
                {
                    throw new Exception("列表错误,列数不对应。");
                }
            }

            // 列描述,可忽略
            ReadLine(sr);
            #endregion

            #region 解析所有行数据
            // 数据
            List <ConfigSheetLine> list = new List <ConfigSheetLine>();
            int      lineCount          = 3;
            string[] sArray             = null;
            while ((sArray = ReadLine(sr)) != null)
            {
                if (Util.IsNullOrWhiteSpace(sArray[0]))
                {
                    continue;
                }
                lineCount++;
                ConfigSheetLine item = new ConfigSheetLine
                {
                    line = new object[column]
                };
                for (int j = 0; j < column; j++)
                {
                    try
                    {
                        if (config.fieldTypes[j] == ConfigDataType.INT)
                        {
                            item.line[j] = !(sArray[j] != string.Empty) ? 0 : int.Parse(sArray[j]);
                        }
                        else if (config.fieldTypes[j] == ConfigDataType.BOOL)
                        {
                            item.line[j] = !(sArray[j] != string.Empty) ? ((object)0) : ((object)bool.Parse(sArray[j]));
                        }
                        else if (config.fieldTypes[j] == ConfigDataType.FLOAT)
                        {
                            item.line[j] = !(sArray[j] != string.Empty) ? 0f : float.Parse(sArray[j]);
                        }
                        else if (config.fieldTypes[j] == ConfigDataType.LONG)
                        {
                            item.line[j] = !(sArray[j] != string.Empty) ? 0L : long.Parse(sArray[j]);
                        }
                        else if (config.fieldTypes[j] == ConfigDataType.DOUBLE)
                        {
                            item.line[j] = !(sArray[j] != string.Empty) ? 0.0 : double.Parse(sArray[j]);
                        }
                        else if (config.fieldTypes[j] == ConfigDataType.ENUM)
                        {
                            item.line[j] = !(sArray[j] != string.Empty) ? 0 : int.Parse(sArray[j]);
                        }
                        else if (config.fieldTypes[j] == ConfigDataType.ARRAY)
                        {
                            item.line[j] = !(sArray[j] != string.Empty) ? new string[0] : sArray[j].Split(new char[] { ';' });
                        }
                        else
                        {
                            item.line[j] = sArray[j];
                        }
                    }
                    catch (FormatException exception)
                    {
                        object[] exMsg = new object[] { file.Name, " 数据格式转换错误:第", lineCount, "行,第", j + 1, "列\r\n", exception.StackTrace };
                        throw new FormatException(string.Concat(exMsg));
                    }
                }
                list.Add(item);
            }
            #endregion

            // 检查是否有重复的id
            config.data = list.ToArray();
            if (!file.Name.Remove(file.Name.LastIndexOf(".csv", StringComparison.Ordinal)).Equals("配置文件说明"))
            {
                CheckRepeatedId(config);
            }
            return(config);
        }
        finally
        {
            try
            {
                stream.Close();
                sr.Close();
            }
            catch
            {
            }
        }
    }