示例#1
0
        public static Dictionary <K, List <V> > ToObjectList <K, V>(Stream stream, bool isLoadAll            = false,
                                                                    UnityEngine.MonoBehaviour loadAllCortine = null, Action <float> onProcess = null, int maxAsyncReadCnt = 500) where V : ConfigBase <K>
        {
            if (stream == null)
            {
                return(null);
            }
            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                return(null);
            }
            // 读取索引
            stream.Seek(header.indexOffset, SeekOrigin.Begin);

            ConfigValueType valueType = (ConfigValueType)stream.ReadByte();

            if (valueType != ConfigValueType.cvList)
            {
                return(null);
            }

            Dictionary <K, List <V> > maps = null;

            for (uint i = 0; i < header.Count; ++i)
            {
                V config = Activator.CreateInstance <V>();
                config.stream = stream;
                K    key        = config.ReadKey();
                long dataOffset = FilePathMgr.Instance.ReadLong(stream);
                config.dataOffset = dataOffset;
                int listCnt = FilePathMgr.Instance.ReadInt(stream);
                if (maps == null)
                {
                    maps = new Dictionary <K, List <V> >((int)header.Count);
                }
                List <V> vs = new List <V>(listCnt);
                maps[key] = vs;
                vs.Add(config);
                for (int j = 1; j < listCnt; ++j)
                {
                    config            = Activator.CreateInstance <V>();
                    config.stream     = stream;
                    config.dataOffset = dataOffset;
                    vs.Add(config);
                }
            }

            if (isLoadAll && maps != null && maps.Count > 0)
            {
                StartLoadAllCortine(maps, loadAllCortine, valueType, onProcess, maxAsyncReadCnt);
            }


            return(maps);
        }
示例#2
0
        private static IEnumerator _ToObjectAsync <K, V>(Stream stream, Dictionary <K, V> maps,
                                                         bool isLoadAll            = false,
                                                         Action <IDictionary> onOK = null, Action <float> onProcess = null, int maxAsyncReadCnt = 500) where V : ConfigBase <K>
        {
            if (stream == null || maps == null)
            {
                yield break;
            }
            maps.Clear();
            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                yield break;
            }

            // 读取索引
            stream.Seek(header.indexOffset, SeekOrigin.Begin);

            ConfigValueType valueType = (ConfigValueType)stream.ReadByte();

            if (valueType != ConfigValueType.cvObject)
            {
                yield break;
            }

            int curCnt = 0;

            for (uint i = 0; i < header.Count; ++i)
            {
                V config = Activator.CreateInstance <V>();
                config.stream = stream;
                K key = config.ReadKey();
                config.dataOffset = FilePathMgr.Instance.ReadLong(stream);
                if (maps == null)
                {
                    maps = new Dictionary <K, V>((int)header.Count);
                }
                maps[key] = config;

                ++curCnt;
                if (curCnt >= maxAsyncReadCnt)
                {
                    curCnt = 0;
                    InitEndFrame();
                    yield return(m_EndFrame);
                }
            }

            yield return(StartLoadCortine(maps, valueType, onProcess, maxAsyncReadCnt));

            if (onOK != null)
            {
                onOK(maps);
            }
        }
示例#3
0
        // 首次读取
        public static Dictionary <K, V> ToObject <K, V>(Stream stream, bool isLoadAll            = false,
                                                        UnityEngine.MonoBehaviour loadAllCortine = null, Action <float> onProcess = null, int maxAsynReadCnt = 500) where V : ConfigBase <K>, new()
        {
            if (stream == null)
            {
                return(null);
            }
            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                return(null);
            }

            // 读取索引
            stream.Seek(header.indexOffset, SeekOrigin.Begin);

            ConfigValueType valueType = (ConfigValueType)stream.ReadByte();

            if (valueType != ConfigValueType.cvObject)
            {
                return(null);
            }

            Dictionary <K, V> maps = null;

            for (uint i = 0; i < header.Count; ++i)
            {
                V config = new V();
                config.stream = stream;
                K key = config.ReadKey();
                config.dataOffset = FilePathMgr.Instance.ReadLong(stream);
                if (maps == null)
                {
                    maps = new Dictionary <K, V>((int)header.Count);
                }
                maps[key] = config;
            }

            if (isLoadAll && maps != null && maps.Count > 0)
            {
                StartLoadAllCortine(maps, loadAllCortine, valueType, onProcess, maxAsynReadCnt);
                if (loadAllCortine == null)
                {
                    stream.Close();
                    stream.Dispose();
                    ConfigStringKey.ClearPropertys(typeof(V));
                }
            }

            return(maps);
        }
示例#4
0
        public static bool GetConfigValueType(Stream stream, out ConfigValueType valueType)
        {
            valueType = ConfigValueType.cvObject;
            if (stream == null)
            {
                return(false);
            }

            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                return(false);
            }

            stream.Seek(header.indexOffset, SeekOrigin.Begin);
            valueType = (ConfigValueType)stream.ReadByte();

            return(true);
        }
示例#5
0
        /// <summary>
        /// Converts an array of configuration lines to a dictionary name-value structure,
        /// while checking if all names are defined, values are valid, and no required name is missing.
        /// </summary>
        /// <param name="Lines">Array of configuration lines.</param>
        /// <param name="NamesDefinition">Definition of configuration names.</param>
        /// <param name="NameVal">Empty dictionary to be filled with name-value pairs if the function succeeds.</param>
        /// <returns>true if the function succeeds, false otherwise.</returns>
        public bool LinesToNameValueDictionary(string[] Lines, Dictionary <string, ConfigValueType> NamesDefinition, Dictionary <string, object> NameVal)
        {
            bool error      = false;
            int  lineNumber = 0;

            foreach (string aline in Lines)
            {
                lineNumber++;
                string line = aline.Trim();
                if ((line.Length == 0) || (line[0] == '#'))
                {
                    continue;
                }

                int epos = line.IndexOf('=');
                if (epos == -1)
                {
                    log.Error("Line {0} does not contain equal sign.", lineNumber);
                    error = true;
                    break;
                }

                string name  = line.Substring(0, epos).Trim();
                string value = line.Substring(epos + 1).Trim();

                if (string.IsNullOrEmpty(name))
                {
                    log.Error("No name before equal sign on line {0}.", lineNumber);
                    error = true;
                    break;
                }

                if (NameVal.ContainsKey(name))
                {
                    log.Error("Name '{0}' redefined on line {1}.", name, lineNumber);
                    error = true;
                    break;
                }

                if (!NamesDefinition.ContainsKey(name))
                {
                    log.Error("Unknown name '{0}' on line {1}.", name, lineNumber);
                    error = true;
                    break;
                }

                error = true;
                ConfigValueType type = NamesDefinition[name];
                switch (type)
                {
                case ConfigValueType.Int:
                {
                    int val;
                    if (int.TryParse(value, out val))
                    {
                        NameVal.Add(name, val);
                        error = false;
                    }
                    else
                    {
                        log.Error("Invalid integer value '{0}' on line {1}.", value, lineNumber);
                    }
                    break;
                }

                case ConfigValueType.Port:
                {
                    int val;
                    if (int.TryParse(value, out val))
                    {
                        if ((val >= 0) || (val <= 65535))
                        {
                            NameVal.Add(name, val);
                            error = false;
                        }
                        else
                        {
                            log.Error("Invalid port value '{0}' on line {1}.", value, lineNumber);
                        }
                    }
                    else
                    {
                        log.Error("Invalid port value '{0}' on line {1}.", value, lineNumber);
                    }

                    break;
                }

                case ConfigValueType.IpAddress:
                {
                    IPAddress val = IPAddress.Any;
                    if (IPAddress.TryParse(value, out val))
                    {
                        NameVal.Add(name, val);
                        error = false;
                    }
                    else
                    {
                        log.Error("Invalid IP address interface value '{0}' on line {1}.", value, lineNumber);
                    }

                    break;
                }

                case ConfigValueType.StringEmpty:
                case ConfigValueType.StringNonEmpty:
                {
                    if (!string.IsNullOrEmpty(value) || (type == ConfigValueType.StringEmpty))
                    {
                        NameVal.Add(name, value);
                        error = false;
                    }
                    else
                    {
                        log.Error("Value for name '{0}' on line {1} can not be empty.", name, lineNumber);
                    }

                    break;
                }

                case ConfigValueType.OnOffSwitch:
                {
                    if ((value == "on") || (value == "off"))
                    {
                        NameVal.Add(name, value == "on");
                        error = false;
                    }
                    else
                    {
                        log.Error("Value for name '{0}' on line {1} can only be either 'on' or 'off'.", name, lineNumber);
                    }

                    break;
                }

                default:
                    log.Error("Internal error parsing line line {0}, type '{1}'.", lineNumber, type);
                    break;
                }

                if (error)
                {
                    break;
                }
            }

            if (!error)
            {
                // Check that all values are in NameVal dictionary.
                foreach (string key in NamesDefinition.Keys)
                {
                    if (!NameVal.ContainsKey(key))
                    {
                        log.Error("Missing definition of '{0}'.", key);
                        error = true;
                        break;
                    }
                }
            }

            bool res = !error;

            return(res);
        }
示例#6
0
        private static IEnumerator _ToObjectMapAsync <K1, K2, V>(Stream stream,
                                                                 Dictionary <K1, Dictionary <K2, V> > maps, bool isLoadAll = false,
                                                                 Action <IDictionary> onOK = null, Action <float> onProcess = null, int maxAsyncReadCnt = 500) where V : ConfigBase <K2>
        {
            if (stream == null || maps == null)
            {
                yield break;
            }

            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                yield break;
            }

            maps.Clear();

            // 读取索引
            stream.Seek(header.indexOffset, SeekOrigin.Begin);

            ConfigValueType valueType = (ConfigValueType)stream.ReadByte();

            if (valueType != ConfigValueType.cvMap)
            {
                yield break;
            }

            int curCnt = 0;

            System.Type keyType1 = typeof(K1);
            //   System.Type keyType2 = typeof(K2);
            //    System.Type subDictType = typeof(Dictionary<K2, V>);
            for (uint i = 0; i < header.Count; ++i)
            {
                System.Object key1       = FilePathMgr.Instance.ReadObject(stream, keyType1);
                long          dataOffset = FilePathMgr.Instance.ReadLong(stream);
                int           DictCnt    = FilePathMgr.Instance.ReadInt(stream);
                if (DictCnt > 0)
                {
                    Dictionary <K2, V> subMap = new Dictionary <K2, V>();
                    for (int j = 0; j < DictCnt; ++j)
                    {
                        V config = Activator.CreateInstance <V>();
                        config.stream     = stream;
                        config.dataOffset = dataOffset;
                        K2 key2 = config.ReadKey();
                        subMap[(K2)key2] = config;

                        ++curCnt;
                        if (curCnt >= maxAsyncReadCnt)
                        {
                            curCnt = 0;
                            InitEndFrame();
                            yield return(m_EndFrame);
                        }
                    }


                    if (subMap != null && subMap.Count > 0)
                    {
                        maps[((K1)key1)] = subMap;
                    }
                }
            }

            if (isLoadAll && maps.Count > 0)
            {
                yield return(StartLoadCortine(maps, valueType, onProcess, maxAsyncReadCnt));
            }

            if (onOK != null)
            {
                onOK(maps);
            }
        }
示例#7
0
        private static IEnumerator _ToObjectListAsync <K, V>(Stream stream,
                                                             Dictionary <K, List <V> > maps, bool isLoadAll = false,
                                                             Action <IDictionary> onOK = null, Action <float> onProcess = null, int maxAsyncReadCnt = 500) where V : ConfigBase <K>
        {
            if (stream == null || maps == null)
            {
                yield break;
            }

            maps.Clear();

            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                yield break;
            }

            // 读取索引
            stream.Seek(header.indexOffset, SeekOrigin.Begin);

            ConfigValueType valueType = (ConfigValueType)stream.ReadByte();

            if (valueType != ConfigValueType.cvList)
            {
                yield break;
            }

            int curCnt = 0;

            for (uint i = 0; i < header.Count; ++i)
            {
                V config = Activator.CreateInstance <V>();
                config.stream = stream;
                K    key        = config.ReadKey();
                long dataOffset = FilePathMgr.Instance.ReadLong(stream);
                config.dataOffset = dataOffset;
                int listCnt = FilePathMgr.Instance.ReadInt(stream);
                if (maps == null)
                {
                    maps = new Dictionary <K, List <V> >((int)header.Count);
                }
                List <V> vs = new List <V>(listCnt);
                maps[key] = vs;
                vs.Add(config);
                for (int j = 1; j < listCnt; ++j)
                {
                    config            = Activator.CreateInstance <V>();
                    config.stream     = stream;
                    config.dataOffset = dataOffset;
                    vs.Add(config);

                    ++curCnt;
                    if (curCnt >= maxAsyncReadCnt)
                    {
                        curCnt = 0;
                        InitEndFrame();
                        yield return(m_EndFrame);
                    }
                }

                if (onProcess != null)
                {
                    float delta   = isLoadAll ? 0.5f : 1f;
                    float process = ((float)i / (float)header.Count) * delta;
                    onProcess(process);
                }
            }

            if (isLoadAll && maps.Count > 0)
            {
                yield return(StartLoadCortine(maps, valueType, onProcess, maxAsyncReadCnt));
            }

            if (onOK != null)
            {
                onOK(maps);
            }
        }
示例#8
0
        public static Dictionary <K, V> TestCommonToObject <K, V>(Stream stream,
                                                                  bool isLoadAll = false,
                                                                  UnityEngine.MonoBehaviour loadAllCortine = null,
                                                                  int maxAsyncReadCnt = 500) where V : class
        {
            if (stream == null)
            {
                return(null);
            }

            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                return(null);
            }

            // 读取索引
            stream.Seek(header.indexOffset, SeekOrigin.Begin);
            // 读取类型(之前已经获取到了)
            ConfigValueType valueType = (ConfigValueType)stream.ReadByte();

            Dictionary <K, V> maps = null;

            switch (valueType)
            {
            case ConfigValueType.cvObject: {
                for (uint i = 0; i < header.Count; ++i)
                {
                    ConfigBase <K> config = Activator.CreateInstance <V>() as ConfigBase <K>;
                    config.stream = stream;
                    K key = config.ReadKey();
                    config.dataOffset = FilePathMgr.Instance.ReadLong(stream);
                    if (maps == null)
                    {
                        maps = new Dictionary <K, V>((int)header.Count);
                    }
                    maps[key] = config as V;
                }
                break;
            }

            case ConfigValueType.cvList: {
                System.Type t = typeof(V);
                // 这里有数组分配,不要频繁使用TestCommonToObject
                var interfaces = t.GetInterfaces();
                if (interfaces == null || interfaces.Length <= 0)
                {
                    return(null);
                }
                var inter = interfaces[0];
                if (inter == null)
                {
                    return(null);
                }
                for (uint i = 0; i < header.Count; ++i)
                {
                    ConfigBase <K> config = Activator.CreateInstance(inter) as ConfigBase <K>;
                    config.stream = stream;
                    K    key        = config.ReadKey();
                    long dataOffset = FilePathMgr.Instance.ReadLong(stream);
                    config.dataOffset = dataOffset;
                    int listCnt = FilePathMgr.Instance.ReadInt(stream);
                    if (maps == null)
                    {
                        maps = new Dictionary <K, V>((int)header.Count);
                    }
                    V vs = Activator.CreateInstance <V>();
                    maps[key] = vs;
                    IList list = vs as IList;
                    list.Add(config);
                    for (int j = 1; j < listCnt; ++j)
                    {
                        config            = Activator.CreateInstance(inter) as ConfigBase <K>;
                        config.stream     = stream;
                        config.dataOffset = dataOffset;
                        list.Add(config);
                    }
                }
                break;
            }

            default:
                return(null);
            }

            if (isLoadAll && maps != null && maps.Count > 0)
            {
                StartLoadAllCortine(maps, loadAllCortine, valueType, null, maxAsyncReadCnt);
            }

            return(maps);
        }
示例#9
0
        public static IDictionary TestCommonToObject(Stream stream, System.Type configType,
                                                     System.Type dictType, bool isLoadAll     = false,
                                                     UnityEngine.MonoBehaviour loadAllCortine = null, int maxAsyncReadCnt = 500)
        {
            if (stream == null || configType == null || dictType == null)
            {
                return(null);
            }

            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                return(null);
            }

            // 读取索引
            stream.Seek(header.indexOffset, SeekOrigin.Begin);
            // 读取类型(之前已经获取到了)
            ConfigValueType valueType = (ConfigValueType)stream.ReadByte();

            IDictionary maps = null;

            switch (valueType)
            {
            case ConfigValueType.cvObject: {
                for (uint i = 0; i < header.Count; ++i)
                {
                    IConfigBase config = Activator.CreateInstance(configType) as IConfigBase;
                    config.stream = stream;
                    System.Object key = config.ReadKEY();
                    config.dataOffset = FilePathMgr.Instance.ReadLong(stream);
                    if (maps == null)
                    {
                        maps = Activator.CreateInstance(dictType) as IDictionary;
                    }
                    maps[key] = config;
                }
                break;
            }

            case ConfigValueType.cvList: {
                var vsType = typeof(List <>).MakeGenericType(configType);
                for (uint i = 0; i < header.Count; ++i)
                {
                    IConfigBase config = Activator.CreateInstance(configType) as IConfigBase;
                    config.stream = stream;
                    System.Object key        = config.ReadKEY();
                    long          dataOffset = FilePathMgr.Instance.ReadLong(stream);
                    config.dataOffset = dataOffset;
                    int listCnt = FilePathMgr.Instance.ReadInt(stream);
                    if (maps == null)
                    {
                        maps = Activator.CreateInstance(dictType) as IDictionary;
                    }
                    IList list = Activator.CreateInstance(vsType) as IList;
                    maps[key] = list;
                    list.Add(config);
                    for (int j = 1; j < listCnt; ++j)
                    {
                        config            = Activator.CreateInstance(configType) as IConfigBase;
                        config.stream     = stream;
                        config.dataOffset = dataOffset;
                        list.Add(config);
                    }
                }
                break;
            }

            // 有问题
            case ConfigValueType.cvMap: {
                Type[] vTypes = dictType.GetInterfaces();
                if (vTypes == null || vTypes.Length < 2)
                {
                    return(null);
                }
                Type k1 = vTypes[0];
                if (k1 == null)
                {
                    return(null);
                }

                Type vType = vTypes[1];
                if (vType == null)
                {
                    return(null);
                }

                if (!vType.IsSubclassOf(typeof(IDictionary)))
                {
                    return(null);
                }

                Type[] subTypes = vType.GetInterfaces();
                if (subTypes == null || subTypes.Length < 2)
                {
                    return(null);
                }

                Type k2 = subTypes[0];
                Type v  = subTypes[1];
                if (k2 == null || v == null)
                {
                    return(null);
                }

                var subDictType = typeof(Dictionary <System.Object, System.Object>).MakeGenericType(k2, v);
                if (subDictType == null)
                {
                    return(null);
                }

                for (uint i = 0; i < header.Count; ++i)
                {
                }

                break;
            }

            default:
                return(null);
            }

            if (isLoadAll && maps != null && maps.Count > 0)
            {
                StartLoadAllCortine(maps, loadAllCortine, valueType, null, maxAsyncReadCnt);
            }

            return(maps);
        }
示例#10
0
        private static IEnumerator StartLoadCortine(IDictionary maps,
                                                    ConfigValueType valueType, Action <float> onProcess, int maxAsyncReadCnt)
        {
            if (maps == null || maps.Count <= 0)
            {
                yield break;
            }

            int curCnt = 0;
            int idx    = 0;

            if (valueType == ConfigValueType.cvObject)
            {
                var iter = maps.GetEnumerator();
                while (iter.MoveNext())
                {
                    IConfigBase config = iter.Value as IConfigBase;
                    if (!config.StreamSeek())
                    {
                        continue;
                    }
                    config.ReadValue();

                    if (onProcess != null)
                    {
                        ++idx;
                        float process = 0.5f + (float)idx / (float)maps.Count;
                        onProcess(process);
                    }

                    ++curCnt;
                    if (curCnt >= maxAsyncReadCnt)
                    {
                        curCnt = 0;
                        InitEndFrame();
                        yield return(m_EndFrame);
                    }
                }
                iter.DisposeIter();
            }
            else if (valueType == ConfigValueType.cvList)
            {
                var iter = maps.GetEnumerator();
                while (iter.MoveNext())
                {
                    IList       vs = iter.Value as IList;
                    IConfigBase v  = vs [0] as IConfigBase;
                    if (!v.StreamSeek())
                    {
                        continue;
                    }
                    for (int i = 0; i < vs.Count; ++i)
                    {
                        v = vs[i] as IConfigBase;
                        v.ReadValue();

                        ++curCnt;
                        if (curCnt >= maxAsyncReadCnt)
                        {
                            curCnt = 0;
                            InitEndFrame();
                            yield return(m_EndFrame);
                        }
                    }

                    if (onProcess != null)
                    {
                        ++idx;
                        float process = 0.5f + (float)idx / (float)maps.Count;
                        onProcess(process);
                    }
                }
                iter.DisposeIter();
            }
            else if (valueType == ConfigValueType.cvMap)
            {
                // 字典类型
                var iter = maps.GetEnumerator();
                while (iter.MoveNext())
                {
                    IDictionary map     = iter.Value as IDictionary;
                    var         subIter = map.GetEnumerator();
                    if (subIter.MoveNext())
                    {
                        IConfigBase v = subIter.Value as IConfigBase;
                        if (!v.StreamSeek())
                        {
                            continue;
                        }
                        v.ReadValue();
                        while (subIter.MoveNext())
                        {
                            v = subIter.Value as IConfigBase;
                            v.ReadValue();

                            ++curCnt;
                            if (curCnt >= maxAsyncReadCnt)
                            {
                                curCnt = 0;
                                InitEndFrame();
                                yield return(m_EndFrame);
                            }
                        }
                    }
                    subIter.DisposeIter();

                    if (onProcess != null)
                    {
                        ++idx;
                        float process = 0.5f + (float)idx / (float)maps.Count;
                        onProcess(process);
                    }
                }
                iter.DisposeIter();
            }
        }
示例#11
0
        private static UnityEngine.Coroutine StartLoadAllCortine(IDictionary maps,
                                                                 UnityEngine.MonoBehaviour parent, ConfigValueType valueType, Action <float> onProcess, int maxAsyncReadCnt)
        {
            if (maps == null || maps.Count <= 0)
            {
                return(null);
            }
            if (parent != null)
            {
                return(parent.StartCoroutine(StartLoadCortine(maps, valueType, onProcess, maxAsyncReadCnt)));
            }
            else
            {
                if (valueType == ConfigValueType.cvObject)
                {
                    var iter = maps.GetEnumerator();
                    while (iter.MoveNext())
                    {
                        IConfigBase config = iter.Value as IConfigBase;
                        Stream      stream = config.stream;
                        if (stream == null)
                        {
                            continue;
                        }
                        stream.Seek(config.dataOffset, SeekOrigin.Begin);
                        config.ReadValue();
                    }
                    iter.DisposeIter();
                }
                else if (valueType == ConfigValueType.cvList)
                {
                    var iter = maps.GetEnumerator();
                    while (iter.MoveNext())
                    {
                        IList       vs     = iter.Value as IList;
                        IConfigBase v      = vs[0] as IConfigBase;
                        Stream      stream = v.stream;
                        if (stream == null)
                        {
                            continue;
                        }
                        stream.Seek(v.dataOffset, SeekOrigin.Begin);
                        for (int i = 0; i < vs.Count; ++i)
                        {
                            v = vs[i] as IConfigBase;
                            v.ReadValue();
                        }
                    }
                    iter.DisposeIter();
                }
                else if (valueType == ConfigValueType.cvMap)
                {
                    // 字典类型
                    var iter = maps.GetEnumerator();
                    while (iter.MoveNext())
                    {
                        IDictionary map     = iter.Value as IDictionary;
                        var         subIter = map.GetEnumerator();
                        if (subIter.MoveNext())
                        {
                            IConfigBase v = subIter.Value as IConfigBase;
                            if (!v.StreamSeek())
                            {
                                continue;
                            }
                            v.ReadValue();
                            while (subIter.MoveNext())
                            {
                                v = subIter.Value as IConfigBase;
                                v.ReadValue();
                            }
                        }
                        subIter.DisposeIter();
                    }
                    iter.DisposeIter();
                }
            }

            return(null);
        }
示例#12
0
        internal static bool ToStream(Stream stream, System.Collections.IDictionary values)
        {
            if (stream == null || values == null || values.Count <= 0)
            {
                return(false);
            }

            ConfigFileHeader header = new ConfigFileHeader((uint)values.Count, 0);

            header.SaveToStream(stream);

            var             iter      = values.GetEnumerator();
            ConfigValueType valueType = ConfigValueType.cvObject;

            while (iter.MoveNext())
            {
                IList       vs     = iter.Value as IList;
                IDictionary subMap = iter.Value as IDictionary;
                if (vs != null)
                {
                    // 说明是List
                    valueType = ConfigValueType.cvList;
                    long dataOffset = stream.Position;
                    for (int i = 0; i < vs.Count; ++i)
                    {
                        IConfigBase v = vs[i] as IConfigBase;
                        v.stream     = stream;
                        v.dataOffset = dataOffset;
                        v.WriteValue();
                    }
                }
                else if (subMap != null)
                {
                    // 字典类型
                    valueType = ConfigValueType.cvMap;
                    long dataOffset = stream.Position;
                    var  subIter    = subMap.GetEnumerator();
                    while (subIter.MoveNext())
                    {
                        IConfigBase v = subIter.Value as IConfigBase;
                        v.stream     = stream;
                        v.dataOffset = dataOffset;
                        v.WriteValue();
                    }
                    subIter.DisposeIter();
                }
                else
                {
                    // 普通对象类型
                    valueType = ConfigValueType.cvObject;
                    IConfigBase v = iter.Value as IConfigBase;
                    v.stream     = stream;
                    v.dataOffset = stream.Position;
                    v.WriteValue();
                }
            }
            iter.DisposeIter();

            long indexOffset = stream.Position;

            stream.WriteByte((byte)valueType);

            if (valueType == ConfigValueType.cvList)
            {
                iter = values.GetEnumerator();
                while (iter.MoveNext())
                {
                    System.Object key = iter.Key;
                    IList         vs  = iter.Value as IList;
                    if (vs != null)
                    {
                        IConfigBase v = vs[0] as IConfigBase;
                        v.WriteKey(key);
                        // 偏移
                        FilePathMgr.Instance.WriteLong(stream, v.dataOffset);
                        // 数量
                        FilePathMgr.Instance.WriteInt(stream, vs.Count);
                    }
                }
                iter.DisposeIter();
            }
            else if (valueType == ConfigValueType.cvMap)
            {
                // 字典类型
                iter = values.GetEnumerator();
                while (iter.MoveNext())
                {
                    System.Object key = iter.Key;
                    IDictionary   vs  = iter.Value as IDictionary;
                    if (vs != null)
                    {
                        var subIter = vs.GetEnumerator();
                        // 取出第一个
                        if (subIter.MoveNext())
                        {
                            IConfigBase v = subIter.Value as IConfigBase;
                            FilePathMgr.Instance.WriteObject(stream, key, v.GetKeyType());
                            // 偏移
                            FilePathMgr.Instance.WriteLong(stream, v.dataOffset);
                            // 数量
                            FilePathMgr.Instance.WriteInt(stream, vs.Count);

                            System.Object key2 = subIter.Key;
                            v.WriteKey(key2);
                            while (subIter.MoveNext())
                            {
                                key2 = subIter.Key;
                                v.WriteKey(key2);
                            }
                        }
                        subIter.DisposeIter();
                    }
                }
                iter.DisposeIter();
            }
            else if (valueType == ConfigValueType.cvObject)
            {
                iter = values.GetEnumerator();
                while (iter.MoveNext())
                {
                    System.Object key = iter.Key;
                    IConfigBase   v   = iter.Value as IConfigBase;
                    v.WriteKey(key);
                    FilePathMgr.Instance.WriteLong(stream, v.dataOffset);
                }
                iter.DisposeIter();
            }

            // 重写Header
            header.indexOffset = indexOffset;
            header.SeekFileToHeader(stream);
            header.SaveToStream(stream);

            return(true);
        }
示例#13
0
        // 转换成字典类型
        public static Dictionary <K1, Dictionary <K2, V> > ToObjectMap <K1, K2, V>(Stream stream, bool isLoadAll            = false,
                                                                                   UnityEngine.MonoBehaviour loadAllCortine = null,
                                                                                   Action <float> onProcess = null, int maxAsyncReadCnt = 500)
            where V : ConfigBase <K2>
        {
            if (stream == null)
            {
                return(null);
            }
            ConfigFileHeader header = new ConfigFileHeader();

            if (!header.LoadFromStream(stream) || !header.IsVaild)
            {
                return(null);
            }

            // 读取索引
            stream.Seek(header.indexOffset, SeekOrigin.Begin);

            ConfigValueType valueType = (ConfigValueType)stream.ReadByte();

            if (valueType != ConfigValueType.cvMap)
            {
                return(null);
            }

            Dictionary <K1, Dictionary <K2, V> > maps = null;

            System.Type keyType1 = typeof(K1);
            //  System.Type keyType2 = typeof(K2);
            //   System.Type subDictType = typeof(Dictionary<K2, V>);
            for (uint i = 0; i < header.Count; ++i)
            {
                System.Object key1       = FilePathMgr.Instance.ReadObject(stream, keyType1);
                long          dataOffset = FilePathMgr.Instance.ReadLong(stream);
                int           DictCnt    = FilePathMgr.Instance.ReadInt(stream);
                if (DictCnt > 0)
                {
                    if (maps == null)
                    {
                        maps = new Dictionary <K1, Dictionary <K2, V> >();
                    }

                    Dictionary <K2, V> subMap = new Dictionary <K2, V>();
                    for (int j = 0; j < DictCnt; ++j)
                    {
                        V config = Activator.CreateInstance <V>();
                        config.stream     = stream;
                        config.dataOffset = dataOffset;
                        K2 key2 = config.ReadKey();
                        subMap[(K2)key2] = config;
                    }


                    if (subMap != null && subMap.Count > 0)
                    {
                        maps[((K1)key1)] = subMap;
                    }
                }
            }

            if (isLoadAll && maps != null && maps.Count > 0)
            {
                StartLoadAllCortine(maps, loadAllCortine, valueType, onProcess, maxAsyncReadCnt);
            }

            return(maps);
        }
 public static SettingType ToSettingType(this ConfigValueType valueType) =>
 valueType switch
 {