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); }
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); } }
// 首次读取 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); }
private void LoadObjectIndex(ConfigFileHeader header, Stream stream) { var map = this.IndexDataMap; for (int i = 0; i < header.Count; ++i) { K key = ReadKey(stream); long offset = FilePathMgr.GetInstance().ReadLong(stream); int index = FilePathMgr.GetInstance().ReadInt(stream); IndexFileData data = new IndexFileData(); data.Index = index; data.Offset = offset; data.Count = 1; m_IndexDataMap[key] = data; } }
public bool LoadFromStream(Stream stream) { if (stream == null || !stream.CanRead) { return(false); } ConfigFileHeader header = new ConfigFileHeader(); header.SeekFileToHeader(stream); if (!header.LoadFromStream(stream) || !header.IsVaild) { return(false); } if (!header.IsSplitFile) { return(false); } // 索引偏移 stream.Seek(header.indexOffset, SeekOrigin.Begin); ConfigWrap.ConfigValueType valueType = (ConfigWrap.ConfigValueType)stream.ReadByte(); m_ValueType = valueType; switch (valueType) { case ConfigWrap.ConfigValueType.cvObject: LoadObjectIndex(header, stream); break; case ConfigWrap.ConfigValueType.cvList: LoadListIndex(header, stream); break; case ConfigWrap.ConfigValueType.cvMap: LoadMapIndex(header, stream); break; default: return(false); } return(true); }
private void LoadMapIndex(ConfigFileHeader header, Stream stream) { for (int i = 0; i < header.Count; ++i) { K key = ReadKey(stream); long offset = FilePathMgr.GetInstance().ReadLong(stream); int cnt = FilePathMgr.GetInstance().ReadInt(stream); int index = FilePathMgr.GetInstance().ReadInt(stream); IndexFileData data = new IndexFileData(); data.Index = index; data.Offset = offset; data.Count = cnt; if (m_IndexDataMap == null) { m_IndexDataMap = new Dictionary <K, IndexFileData>(); } m_IndexDataMap[key] = data; } }
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); }
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); } }
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); } }
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); }
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); }
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); }
// 转换成字典类型 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); }