// 首次读取 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); }
// 测试用 #if UNITY_EDITOR // 通用转换 // 可以使用List<ConfigBase>或者ConfigBase public static Dictionary <K, V> TestCommonToObject <K, V>(byte[] buffer, bool isLoadAll = false, UnityEngine.MonoBehaviour loadAllCortine = null) where V : class, new() { MemoryStream stream = new MemoryStream(buffer); Dictionary <K, V> ret = TestCommonToObject <K, V>(stream, isLoadAll, loadAllCortine); if (ret == null) { stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(typeof(V)); } return(ret); }
public static IDictionary TestCommonToObject(byte[] buffer, System.Type configType, System.Type dictType, bool isLoadAll = false, UnityEngine.MonoBehaviour loadAllCortine = null) { if (buffer == null || buffer.Length <= 0 || configType == null || dictType == null) { return(null); } MemoryStream stream = new MemoryStream(buffer); IDictionary ret = TestCommonToObject(stream, configType, dictType, isLoadAll, loadAllCortine); if (ret == null) { stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(configType); } return(ret); }
public static Dictionary <K, V> ToObject <K, V>(byte[] buffer, bool isLoadAll = false) where V : ConfigBase <K>, new() { Dictionary <K, V> ret = null; if (buffer == null || buffer.Length <= 0) { return(ret); } MemoryStream stream = new MemoryStream(buffer); ret = ToObject <K, V>(stream, isLoadAll); if (ret == null) { stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(typeof(V)); } return(ret); }
public static Dictionary <K1, Dictionary <K2, V> > ToObjectMap <K1, K2, V>(byte[] buffer, bool isLoadAll = false, UnityEngine.MonoBehaviour loadAllCortine = null) where V : ConfigBase <K2>, new() { Dictionary <K1, Dictionary <K2, V> > ret = null; if (buffer == null || buffer.Length <= 0) { return(ret); } MemoryStream stream = new MemoryStream(buffer); ret = ToObjectMap <K1, K2, V>(stream, isLoadAll, loadAllCortine); if (ret == null) { stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(typeof(V)); } return(ret); }
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>, new() { 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 = new 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)); stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(typeof(V)); } 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>, new() { 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 = new 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 = new 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)); stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(typeof(V)); } 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, 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(); Dictionary <K, V> maps = null; switch (valueType) { case ConfigValueType.cvObject: { for (uint i = 0; i < header.Count; ++i) { ConfigBase <K> config = new 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 = new 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); if (loadAllCortine == null) { stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(typeof(V)); } } 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); if (loadAllCortine == null) { stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(configType); } } return(maps); }
// 转换成字典类型 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>, 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.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 = new 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); if (loadAllCortine == null) { stream.Close(); stream.Dispose(); ConfigStringKey.ClearPropertys(typeof(V)); } } return(maps); }