Update manifest file Reader
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
using K4os.Compression.LZ4.Streams;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
public class BSVReader
|
||||
{
|
||||
public enum ReadMode
|
||||
{
|
||||
Memory = 0,
|
||||
Stream = 1
|
||||
}
|
||||
|
||||
private enum Phase : byte
|
||||
{
|
||||
Header = 0,
|
||||
Body = 1,
|
||||
End = 2
|
||||
}
|
||||
|
||||
private enum HeaderPhaseApriori : byte
|
||||
{
|
||||
Start = 0,
|
||||
RowCount = 1,
|
||||
MaxRowSize = 2
|
||||
}
|
||||
|
||||
public enum Format : byte
|
||||
{
|
||||
Apriori = 0,
|
||||
AnonymousSchema = 1
|
||||
}
|
||||
|
||||
public static ulong ReadVLQ(byte[] buf, ref int offset, int max_bytes = 8)
|
||||
{
|
||||
ulong n = 0;
|
||||
int count = 0;
|
||||
while (count < max_bytes)
|
||||
{
|
||||
count++;
|
||||
var val = (buf[offset] & 127Lu);
|
||||
n |= val;
|
||||
var isEnd = ((buf[offset] & 128) == 0);
|
||||
offset++;
|
||||
if (isEnd)
|
||||
{
|
||||
return n;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = n << 7;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
public static ulong ReadUNum(byte[] buf, ref int offset, int bytes)
|
||||
{
|
||||
var arraySegment = new ArraySegment<byte>(buf, offset, bytes);
|
||||
offset += bytes;
|
||||
ulong n = 0;
|
||||
for(int i = arraySegment.Offset; i < arraySegment.Offset+arraySegment.Count; i++)
|
||||
{
|
||||
n = n << 8;
|
||||
n |= arraySegment.Array[i];
|
||||
}
|
||||
return n;
|
||||
}
|
||||
public static ArraySegment<byte> ReadText(byte[] buf, ref int offset)
|
||||
{
|
||||
int start = offset;
|
||||
while (buf[offset] != 0 && offset < buf.Length)
|
||||
{
|
||||
offset++;
|
||||
}
|
||||
var arraySegment = new ArraySegment<byte>(buf, start, offset - start);
|
||||
offset++;
|
||||
return arraySegment;
|
||||
}
|
||||
public static ArraySegment<byte> ReadFixedBlob(byte[] buf, ref int offset, int bytes)
|
||||
{
|
||||
var arraySegment = new ArraySegment<byte>(buf, offset, bytes);
|
||||
offset += bytes;
|
||||
return arraySegment;
|
||||
}
|
||||
public static ArraySegment<byte> ReadFixedText(byte[] buf, ref int offset, int bytes)
|
||||
{
|
||||
var arraySegment = new ArraySegment<byte>(buf, offset, bytes);
|
||||
offset += bytes;
|
||||
return arraySegment;
|
||||
}
|
||||
public static ArraySegment<byte> ReadBlob(byte[] buf, ref int offset)
|
||||
{
|
||||
ulong size = ReadVLQ(buf, ref offset);
|
||||
var arraySegment = new ArraySegment<byte>(buf, offset, (int)size);
|
||||
offset += (int)size;
|
||||
return arraySegment;
|
||||
}
|
||||
|
||||
public static T[] GetArray<T>(ArraySegment<T> array)
|
||||
{
|
||||
T[] subArray = new T[array.Count];
|
||||
Buffer.BlockCopy(array.Array, array.Offset, subArray, 0, array.Count);
|
||||
return subArray;
|
||||
}
|
||||
|
||||
internal static IBSVReader Init(string path, ReadMode mode, bool isCompressed)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] input;
|
||||
if (isCompressed)
|
||||
{
|
||||
var source = new MemoryStream(File.ReadAllBytes(path));
|
||||
var decoder = LZ4Stream.Decode(source);
|
||||
var target = new MemoryStream();
|
||||
decoder.CopyTo(target);
|
||||
input = target.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
input = File.ReadAllBytes(path);
|
||||
}
|
||||
|
||||
if (mode == ReadMode.Stream)
|
||||
{
|
||||
return new AprioriBSVReader(input, 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new AnonymousSchemaBSVReader(input, 2);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IBSVReader : IDisposable
|
||||
{
|
||||
public abstract ulong GetRowCount();
|
||||
public abstract Format GetFormat();
|
||||
|
||||
public abstract bool ReadLine<T>(ILineParser<T> parser, ref T dat);
|
||||
}
|
||||
|
||||
public interface ILineParser<T>
|
||||
{
|
||||
public abstract void Parse(byte[] buf, ref int offset, ref T dat);
|
||||
}
|
||||
|
||||
internal abstract class BaseBSVReader : IBSVReader, IDisposable
|
||||
{
|
||||
protected readonly byte[] buf;
|
||||
protected int offset;
|
||||
protected ulong rowCount;
|
||||
protected ulong maxRowSize;
|
||||
protected ulong row;
|
||||
protected readonly Format format;
|
||||
public ulong GetRowCount() { return rowCount; }
|
||||
public Format GetFormat() { return format; }
|
||||
public BaseBSVReader(Format format, byte[] buf, int offset)
|
||||
{
|
||||
this.format = format;
|
||||
this.buf = buf;
|
||||
this.offset = offset;
|
||||
}
|
||||
public bool ReadLine<T>(ILineParser<T> parser, ref T dat)
|
||||
{
|
||||
if (offset < buf.Length)
|
||||
{
|
||||
parser.Parse(buf, ref offset, ref dat);
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
public abstract void Dispose();
|
||||
}
|
||||
|
||||
internal class AnonymousSchemaBSVReader : BaseBSVReader
|
||||
{
|
||||
private readonly ushort headerSize;
|
||||
private readonly uint schemaCount;
|
||||
private readonly uint schemaVersion;
|
||||
private readonly Schema[] schemas;
|
||||
|
||||
internal uint GetSchemaCount() { return schemaCount; }
|
||||
|
||||
internal uint GetSchemaVersion() { return schemaVersion; }
|
||||
|
||||
public AnonymousSchemaBSVReader(byte[] buf, int offset) : base(Format.AnonymousSchema, buf, offset)
|
||||
{
|
||||
headerSize = (ushort)ReadUNum(buf, ref this.offset, 2);
|
||||
rowCount = ReadVLQ(buf, ref this.offset);
|
||||
maxRowSize = ReadVLQ(buf, ref this.offset);
|
||||
schemaVersion = (uint)ReadVLQ(buf, ref this.offset);
|
||||
schemaCount = (uint)ReadVLQ(buf, ref this.offset);
|
||||
if (schemaCount > 0)
|
||||
{
|
||||
schemas = new Schema[schemaCount];
|
||||
for (int i = 0; i < schemaCount; i++)
|
||||
{
|
||||
Schema.Type type = (Schema.Type)ReadUNum(buf, ref this.offset, 1);
|
||||
ulong? fixSize = null;
|
||||
if (type == Schema.Type.UNumFixed || type == Schema.Type.TextFixed || type == Schema.Type.BlobFixed)
|
||||
{
|
||||
fixSize = ReadVLQ(buf, ref this.offset);
|
||||
}
|
||||
schemas[i] = new Schema(type, fixSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose() { }
|
||||
}
|
||||
|
||||
public class Schema
|
||||
{
|
||||
public enum Type : byte
|
||||
{
|
||||
UShort = 16,
|
||||
UInt = 17,
|
||||
ULong = 18,
|
||||
UNum = 32,
|
||||
UNumFixed = 33,
|
||||
Blob = 48,
|
||||
BlobFixed = 49,
|
||||
Text = 64,
|
||||
TextFixed = 65
|
||||
}
|
||||
public readonly Type type;
|
||||
public readonly ulong? fixedSize;
|
||||
|
||||
public Schema(Type type, ulong? fixedSize) { }
|
||||
}
|
||||
|
||||
internal class AprioriBSVReader : BaseBSVReader
|
||||
{
|
||||
public AprioriBSVReader(byte[] buf, int offset) : base(Format.Apriori, buf, offset) { }
|
||||
public override void Dispose() { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a15e32c966280446ba094c5db043677
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
public static class ManifestCategory
|
||||
{
|
||||
public const int APP_DEFINED_KIND_BASE = 10;
|
||||
public const string ASSET_MANIFEST = "manifest";
|
||||
public const string PLATFORM_MANIFEST = "manifest2";
|
||||
public const string ROOT_MANIFEST = "manifest3";
|
||||
|
||||
public enum Kind : int
|
||||
{
|
||||
Default = 0,
|
||||
AssetManifest = 1,
|
||||
PlatformManifest = 2,
|
||||
RootManifest = 3
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: be0b7cdd8f20ce14b8d0e1d129684d19
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,43 @@
|
||||
using Mono.Data.Sqlite;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using static BSVReader;
|
||||
using static ManifestCategory;
|
||||
|
||||
public class ManifestDB
|
||||
{
|
||||
public SqliteConnection metaDb;
|
||||
|
||||
public static ManifestEntry[] GetManifest(string path, bool isCompressed, Kind kind)
|
||||
{
|
||||
IBSVReader reader = Init(path, ReadMode.Memory, isCompressed);
|
||||
ulong count = reader.GetRowCount();
|
||||
ManifestEntry[] manifests = new ManifestEntry[count];
|
||||
if (kind == Kind.AssetManifest)
|
||||
{
|
||||
for (ulong i = 0; i < count; i++)
|
||||
{
|
||||
manifests[i].kind = (int)kind;
|
||||
if (!reader.ReadLine(ManifestEntry.GetBsvParser(ManifestEntry.Format.Full, reader), ref manifests[i]))
|
||||
{
|
||||
throw new Exception("ReadLineError");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (ulong i = 0; i < count; i++)
|
||||
{
|
||||
manifests[i].kind = (int)kind;
|
||||
if (!reader.ReadLine(ManifestEntry.GetBsvParser(ManifestEntry.Format.Simplified, reader), ref manifests[i]))
|
||||
{
|
||||
throw new Exception("ReadLineError");
|
||||
}
|
||||
}
|
||||
}
|
||||
return manifests;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c5745921b997134aa8682f304140a8a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,124 @@
|
||||
using BaseNcoding;
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using static BSVReader;
|
||||
|
||||
public struct ManifestEntry
|
||||
{
|
||||
public byte[] name;
|
||||
public byte[] deps;
|
||||
public uint group;
|
||||
public uint priority;
|
||||
public ulong size;
|
||||
public ulong checksum;
|
||||
public string hname;
|
||||
public int kind;
|
||||
|
||||
public enum Format
|
||||
{
|
||||
Simplified = 0,
|
||||
Full = 1
|
||||
}
|
||||
|
||||
public static byte[] CalHame(ulong checksum, ulong size, byte[] name)
|
||||
{
|
||||
if (name == null) return null;
|
||||
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
|
||||
byte[] allbyte = new byte[name.Length + 16];
|
||||
byte[] checksumByte = BitConverter.GetBytes(checksum);
|
||||
byte[] sizeByte = BitConverter.GetBytes(size);
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
allbyte[i] = (i < 8 ? checksumByte[7 - i] : sizeByte[15 - i]);
|
||||
}
|
||||
Buffer.BlockCopy(name, 0, allbyte, 16, name.Length);
|
||||
return sha1.ComputeHash(allbyte);
|
||||
}
|
||||
|
||||
public static string CalHameString(ulong checksum, ulong size, byte[] name)
|
||||
{
|
||||
var bytes = CalHame(checksum, size, name);
|
||||
Base32 base32 = new Base32();
|
||||
return base32.Encode(bytes);
|
||||
}
|
||||
|
||||
public static string CalHameString(ulong checksum, ulong size, string name)
|
||||
{
|
||||
return CalHameString(checksum, size, Encoding.UTF8.GetBytes(name));
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.AppendFormat("Name : {0} ", Encoding.UTF8.GetString(name));
|
||||
s.AppendFormat("Deps : {0} \n", deps == null ? "NULL" : Encoding.UTF8.GetString(deps));
|
||||
s.AppendFormat("Size : {0} ", size);
|
||||
s.AppendFormat("Checksum : {0} ", (long)checksum);
|
||||
s.AppendFormat("Group : {0} ", group);
|
||||
s.AppendFormat("Priority : {0} ", priority);
|
||||
s.AppendFormat("Kind : {0} ", kind);
|
||||
s.AppendFormat("Hash : {0} ", CalHameString(checksum, size, name));
|
||||
return s.ToString();
|
||||
}
|
||||
|
||||
public static ILineParser<ManifestEntry> GetBsvParser(Format format, IBSVReader bsvReader)
|
||||
{
|
||||
if (format == Format.Simplified)
|
||||
{
|
||||
return new SimplifiedEntryLineReader();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (bsvReader is AnonymousSchemaBSVReader)
|
||||
{
|
||||
return new FullEntryLineReader();
|
||||
}
|
||||
else if (bsvReader is AprioriBSVReader)
|
||||
{
|
||||
return new LegacyFullEntryLineReader();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class SimplifiedEntryLineReader : ILineParser<ManifestEntry>
|
||||
{
|
||||
public void Parse(byte[] buf, ref int offset, ref ManifestEntry dat)
|
||||
{
|
||||
dat.name = GetArray(ReadText(buf, ref offset));
|
||||
dat.size = ReadVLQ(buf, ref offset);
|
||||
dat.checksum = ReadUNum(buf, ref offset, 8);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class FullEntryLineReader : ILineParser<ManifestEntry>
|
||||
{
|
||||
|
||||
public void Parse(byte[] buf, ref int offset, ref ManifestEntry dat)
|
||||
{
|
||||
dat.name = GetArray(ReadText(buf, ref offset));
|
||||
dat.deps = GetArray(ReadText(buf, ref offset));
|
||||
dat.group = (uint)ReadVLQ(buf, ref offset, 4);
|
||||
dat.priority = (uint)ReadVLQ(buf, ref offset, 4);
|
||||
dat.size = (uint)ReadVLQ(buf, ref offset, 8);
|
||||
dat.checksum = ReadUNum(buf, ref offset, 8);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class LegacyFullEntryLineReader : ILineParser<ManifestEntry>
|
||||
{
|
||||
public void Parse(byte[] buf, ref int offset, ref ManifestEntry dat)
|
||||
{
|
||||
dat.name = GetArray(ReadText(buf, ref offset));
|
||||
dat.deps = GetArray(ReadText(buf, ref offset));
|
||||
dat.group = (uint)ReadVLQ(buf, ref offset, 4);
|
||||
dat.size = (uint)ReadVLQ(buf, ref offset, 8);
|
||||
dat.checksum = ReadUNum(buf, ref offset, 8);
|
||||
dat.priority = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f446452d64e1ef54399daba449bbb5dc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user