/// <summary> /// 在指定的sheet中读取key对应的所有条目 /// </summary> /// <typeparam name="T">条目的类型或该类型的基类</typeparam> /// <param name="sheetName">指定的sheet名称</param> /// <param name="key">要获取的条目的键</param> /// <returns>键对应的所有条目</returns> public List <T> GetSheetItems <T>(string sheetName, string key) { List <T> items = new List <T>(); KeySheetData data; if (mKeySheets.TryGetValue(sheetName, out data)) { mCachedOffsets.Clear(); if (data.GetItemOffsets(key, mCachedOffsets)) { Type t = typeof(T); Stream s = data.stream; for (int i = 0, imax = mCachedOffsets.Count; i < imax; i++) { s.Position = (long)mCachedOffsets[i]; ushort typeIndex = DatabaseStreamReader.ReadUShort(s); Type type = data.typeFinder.GetType(typeIndex); if (type == null) { items.Add(Serializer.DeserializeWithLengthPrefix <T>(s, PrefixStyle.Fixed32)); } if (t == type || type.IsSubclassOf(t)) { items.Add((T)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(s, null, type, PrefixStyle.Fixed32, 0)); } } } } return(items); }
/// <summary> /// 在指定的sheet中读取key对应条目 /// </summary> /// <param name="sheetName">指定的sheet名称</param> /// <param name="key">要获取的条目的键</param> /// <returns>键对应的条目</returns> public byte[] GetSheetBytes(string sheetName, string key, out string sheetType) { sheetType = ""; KeySheetData data; if (mKeySheets.TryGetValue(sheetName, out data)) { mCachedOffsets.Clear(); if (data.GetItemOffsets(key, mCachedOffsets)) { Stream s = data.stream; if (mCachedOffsets.Count > 0) { s.Position = (long)mCachedOffsets[0]; ushort typeIndex = DatabaseStreamReader.ReadUShort(s); sheetType = data.typeFinder.GetTypeName(typeIndex); int length = DatabaseStreamReader.ReadInt(s); byte[] bytes = new byte[length]; s.Read(bytes, 0, length); return(bytes); } } } return(null); }
/// <summary> /// 根据xml名称从数据库中获取该xml定义的数据 /// </summary> /// <typeparam name="T">数据类型或该类型的基类</typeparam> /// <param name="xmlName">xml名称</param> /// <returns>该xml中的数据</returns> public T Get <T>(string xmlName) where T : class { if (string.IsNullOrEmpty(xmlName)) { return(null); } Type type; DataInfo info; if (!mDataInfos.TryGetValue(xmlName, out info)) { return(null); } info.usedTimes++; Stream s = info.stream; if (s == null) { return(null); } s.Position = (long)info.offset; ushort typeIndex = DatabaseStreamReader.ReadUShort(s); type = info.typeFinder.GetType(typeIndex); if (type == null) { return(Serializer.DeserializeWithLengthPrefix <T>(s, PrefixStyle.Fixed32)); } if (typeof(T) == type || type.IsSubclassOf(typeof(T))) { return((T)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(s, null, type, PrefixStyle.Fixed32, 0)); } return(null); }
/// <summary> /// 根据xml名称从数据库中获取该xml定义的数据 /// </summary> /// <param name="xmlName">xml名称</param> /// <returns>该xml中的二进制数据</returns> public byte[] GetXMLBytes(string xmlName, out string xmlType) { xmlType = ""; if (string.IsNullOrEmpty(xmlName)) { return(null); } DataInfo info; if (!mDataInfos.TryGetValue(xmlName, out info)) { return(null); } info.usedTimes++; Stream s = info.stream; if (s == null) { return(null); } s.Position = (long)info.offset; ushort typeIndex = DatabaseStreamReader.ReadUShort(s); xmlType = info.typeFinder.GetTypeName(typeIndex); int length = DatabaseStreamReader.ReadInt(s); byte[] bytes = new byte[length]; s.Read(bytes, 0, length); return(bytes); }
/// <summary> /// 将一个组中所有的xml数据加到数据库中 /// </summary> /// <param name="stream">该组的数据流</param> /// <param name="addedXmls">从组中加到数据库中的所有xml名称</param> /// <returns>组名称,若添加失败或该组已存在,则返回null</returns> public string AddToDatabase(Stream stream, IList <string> addedXmls) { stream.Position = 0L; byte[] buffer = new byte[1024]; ushort groupLen = DatabaseStreamReader.ReadUShort(stream); stream.Read(buffer, 0, groupLen); string groupKey = Encoding.UTF8.GetString(buffer, 0, groupLen); if (mGroupXmls.ContainsKey(groupKey)) { return(null); } ushort count = DatabaseStreamReader.ReadUShort(stream); string[] xmls = new string[count]; GroupTypeFinder typeFinder = new GroupTypeFinder("Assembly-CSharp", groupKey); for (ushort i = 0; i < count; i++) { ushort nameLen = DatabaseStreamReader.ReadUShort(stream); //Debug.LogWarning(nameLen); stream.Read(buffer, 0, nameLen); string xmlName = Encoding.UTF8.GetString(buffer, 0, nameLen); int offset = DatabaseStreamReader.ReadInt(stream); if (mDataInfos.ContainsKey(xmlName)) { Debug.LogError(string.Format("Xml '{0}' is already existed !", xmlName)); continue; } mDataInfos.Add(xmlName, new DataInfo(stream, typeFinder, offset)); xmls[i] = xmlName; if (addedXmls != null) { addedXmls.Add(xmlName); } } ushort typeCount = DatabaseStreamReader.ReadUShort(stream); string[] typeNames = new string[typeCount]; for (uint i = 0; i < typeCount; i++) { int len = stream.ReadByte(); stream.Read(buffer, 0, len); typeNames[i] = Encoding.UTF8.GetString(buffer, 0, len); } typeFinder.SetTypes(typeNames); mGroupXmls.Add(groupKey, xmls); return(groupKey); }
/// <summary> /// 将一个组中所有的sheet加到数据库中 /// </summary> /// <param name="stream">该组的数据流</param> /// <param name="addedSheets">从组中加到数据库中的所有sheet名称</param> /// <returns>组名称,若添加失败或该组已存在,则返回null</returns> public string AddToDatabase(Stream stream, IList <string> addedSheets) { stream.Position = 0L; byte[] buffer = new byte[1024]; ushort groupKeyLen = DatabaseStreamReader.ReadUShort(stream); stream.Read(buffer, 0, groupKeyLen); string groupKey = Encoding.UTF8.GetString(buffer, 0, groupKeyLen); if (mGroupSheets.ContainsKey(groupKey)) { return(null); } ushort sheetCount = DatabaseStreamReader.ReadUShort(stream); int[] sheetOffsets = new int[sheetCount]; for (ushort i = 0; i < sheetCount; i++) { sheetOffsets[i] = DatabaseStreamReader.ReadInt(stream); } string[] sheets = new string[sheetCount]; GroupTypeFinder typeFinder = new GroupTypeFinder("Assembly-CSharp", groupKey); for (ushort i = 0; i < sheetCount; i++) { stream.Position = sheetOffsets[i]; ushort nameLen = DatabaseStreamReader.ReadUShort(stream); stream.Read(buffer, 0, nameLen); string sheetName = Encoding.UTF8.GetString(buffer, 0, nameLen); uint sheetItemCount = DatabaseStreamReader.ReadUInt(stream); //UnityEngine.Debug.LogError("sheet name : " + sheetName + " item count : " + sheetItemCount); bool strKey = (sheetItemCount & 0x80000000) != 0u; sheetItemCount = sheetItemCount & 0x7fffffff; //UnityEngine.Debug.LogError("sheet item count : " + sheetItemCount); //UnityEngine.Debug.LogError("str key ? " + strKey); bool existed = mKeySheets.ContainsKey(sheetName) || mIdSheets.ContainsKey(sheetName); if (strKey) { string[] keys = new string[sheetItemCount]; int[] offsets = new int[sheetItemCount]; for (int j = 0; j < sheetItemCount; j++) { int keyLen = stream.ReadByte(); //UnityEngine.Debug.Log(keyLen); stream.Read(buffer, 0, keyLen); keys[j] = Encoding.UTF8.GetString(buffer, 0, keyLen); offsets[j] = DatabaseStreamReader.ReadInt(stream); //UnityEngine.Debug.LogWarning(string.Format("key : {0} offset : {1}", keys[j], offsets[j])); } if (!existed) { KeySheetData sheet = new KeySheetData(stream, typeFinder, (int)sheetItemCount, keys, offsets); mKeySheets.Add(sheetName, sheet); } } else { uint[] ids = new uint[sheetItemCount]; int[] offsets = new int[sheetItemCount]; for (int j = 0; j < sheetItemCount; j++) { ids[j] = (uint)DatabaseStreamReader.ReadInt(stream); offsets[j] = DatabaseStreamReader.ReadInt(stream); //UnityEngine.Debug.LogWarning(string.Format("id : {0} offset : {1}", ids[j], offsets[j])); } if (!existed) { IdSheetData sheet = new IdSheetData(stream, typeFinder, (int)sheetItemCount, ids, offsets); mIdSheets.Add(sheetName, sheet); } } if (existed) { Debug.LogError(string.Format("Sheet '{0}' is already existed !", sheetName)); } else { sheets[i] = sheetName; if (addedSheets != null) { addedSheets.Add(sheetName); } //Debug.Log("sheet : " + sheetName); } } ushort typeCount = DatabaseStreamReader.ReadUShort(stream); string[] typeNames = new string[typeCount]; for (uint i = 0; i < typeCount; i++) { int len = stream.ReadByte(); stream.Read(buffer, 0, len); typeNames[i] = Encoding.UTF8.GetString(buffer, 0, len); } typeFinder.SetTypes(typeNames); mGroupSheets.Add(groupKey, sheets); return(groupKey); }