public static void SaveTo([NotNull] this AssetInfoList list, [NotNull] Stream stream) { var dict = new Dictionary <string, object[]>(); foreach (var assetInfo in list.Assets) { var key = assetInfo.ResourceName; var arr = new object[3]; arr[0] = assetInfo.ContentHash; arr[1] = assetInfo.RemoteName; if (assetInfo.Size > ushort.MaxValue) { arr[2] = assetInfo.Size; } else { arr[2] = (ushort)assetInfo.Size; } dict.Add(key, arr); } var databaseObject = new object[] { dict }; MessagePackSerializer.Serialize(stream, databaseObject); }
public static AssetInfoList Parse([NotNull] byte[] data, [NotNull] Encoding encoding) { var assetInfoEntries = new List <AssetInfo>(); byte[] headerBytes; int entryCount; using (var stream = new MemoryStream(data, false)) { var streamLength = stream.Length; using (var bigEndianReader = new EndianBinaryReader(stream, Endian.BigEndian)) { // Assumption: "91 de" is the file header, followed by entry (entrycount+1) headerBytes = stream.ReadBytes(4); // Now read again. stream.Position = 0; var fileHeader = bigEndianReader.ReadUInt16(); if (fileHeader != FileHeaderInt) { throw new FormatException($"File header is 0x{fileHeader:x4}, expected 0x91de."); } entryCount = bigEndianReader.ReadUInt16(); while (stream.Position < streamLength - 1) { var resourceName = ReadString(stream, encoding); var resourceNameEndingByte = (byte)stream.ReadByte(); if (resourceNameEndingByte != 0x93) { throw new FormatException($"Expected name ending byte = 0x93, actual = 0x{resourceNameEndingByte:x2}."); } var resourceHash = ReadString(stream, encoding); var remoteName = ReadString(stream, encoding); var resourceSize = ReadVarSize(bigEndianReader); var assetInfo = new AssetInfo(resourceName, resourceHash, remoteName, resourceSize, resourceNameEndingByte); assetInfoEntries.Add(assetInfo); } } } if (entryCount != assetInfoEntries.Count) { Debug.WriteLine($"Warning: recorded entry count {entryCount} does not equal to real entry count {assetInfoEntries.Count}!"); } var assetInfoList = new AssetInfoList(assetInfoEntries, headerBytes); return(assetInfoList); }
public static bool TryParse([NotNull] byte[] data, [CanBeNull] out AssetInfoList result) { try { result = Parse(data); return(true); } catch (Exception) { result = null; return(false); } }
public static byte[] ToBytes([NotNull] this AssetInfoList list) { byte[] result; using (var stream = new MemoryStream()) { list.SaveTo(stream); result = stream.ToArray(); } return(result); }