Skip to content

Commit 18434f2

Browse files
committed
Rewrite IO
1 parent d4205bc commit 18434f2

24 files changed

Lines changed: 1203 additions & 635 deletions

UnityAsset.NET.sln

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ VisualStudioVersion = 17.9.34607.119
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnityAsset.NET", "UnityAsset.NET\UnityAsset.NET.csproj", "{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj", "{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}"
9-
EndProject
108
Global
119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1210
Debug|Any CPU = Debug|Any CPU
@@ -17,10 +15,6 @@ Global
1715
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
1816
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
1917
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}.Release|Any CPU.Build.0 = Release|Any CPU
2418
EndGlobalSection
2519
GlobalSection(SolutionProperties) = preSolution
2620
HideSolutionNode = FALSE

UnityAsset.NET/BundleFiles/BlocksAndCabsInfo.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using UnityAsset.NET.Enums;
2-
using UnityAsset.NET.IO;
1+
using UnityAsset.NET.IO;
32

43
namespace UnityAsset.NET.BundleFiles;
54

@@ -18,16 +17,16 @@ public BlocksAndCabsInfo(byte[] uncompressedDataHash,
1817
DirectoryInfo = directoryInfo;
1918
}
2019

21-
public static BlocksAndCabsInfo ParseFromReader(AssetReader reader) => new (
22-
reader.ReadBytes(16),
23-
reader.ReadList(reader.ReadInt32(), StorageBlockInfo.ParseFromReader),
24-
reader.ReadList(reader.ReadInt32(), CabInfo.ParseFromReader)
20+
public static BlocksAndCabsInfo Parse(ref DataBuffer db) => new (
21+
db.ReadBytes(16),
22+
db.ReadList(db.ReadInt32(), StorageBlockInfo.Parse),
23+
db.ReadList(db.ReadInt32(), CabInfo.Parse)
2524
);
2625

27-
public void Serialize(AssetWriter writer) {
28-
writer.Write(UncompressedDataHash);
29-
writer.WriteListWithCount(BlocksInfo, (w, info) => info.Serialize(w));
30-
writer.WriteListWithCount(DirectoryInfo, (w, info) => info.Serialize(w));
26+
public void Serialize(ref DataBuffer db) {
27+
db.WriteBytes(UncompressedDataHash);
28+
db.WriteListWithCount(BlocksInfo, (ref DataBuffer d, StorageBlockInfo info) => info.Serialize(ref d));
29+
db.WriteListWithCount(DirectoryInfo, (ref DataBuffer d, CabInfo info) => info.Serialize(ref d));
3130
}
3231

3332
public long SerializeSize => UncompressedDataHash.Length + 8 +

UnityAsset.NET/BundleFiles/BundleFile.cs

Lines changed: 124 additions & 87 deletions
Large diffs are not rendered by default.

UnityAsset.NET/BundleFiles/CabInfo.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ public CabInfo(Int64 offset, Int64 size, UInt32 flags, string path)
1818
Path = path;
1919
}
2020

21-
public static CabInfo ParseFromReader(AssetReader reader) => new (
22-
reader.ReadInt64(),
23-
reader.ReadInt64(),
24-
reader.ReadUInt32(),
25-
reader.ReadStringToNull()
21+
public static CabInfo Parse(ref DataBuffer db) => new (
22+
db.ReadInt64(),
23+
db.ReadInt64(),
24+
db.ReadUInt32(),
25+
db.ReadNullTerminatedString()
2626
);
2727

28-
public void Serialize(AssetWriter writer) {
29-
writer.WriteInt64(Offset);
30-
writer.WriteInt64(Size);
31-
writer.WriteUInt32(Flags);
32-
writer.WriteStringToNull(Path);
28+
public void Serialize(ref DataBuffer db) {
29+
db.WriteInt64(Offset);
30+
db.WriteInt64(Size);
31+
db.WriteUInt32(Flags);
32+
db.WriteNullTerminatedString(Path);
3333
}
3434

3535
public long SerializeSize => 21 + Path.Length;

UnityAsset.NET/BundleFiles/Header.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,27 @@ public Header(string signature, UInt32 version, string unityVersion, string unit
2929
Flags = flags;
3030
}
3131

32-
public static Header ParseFromReader(AssetReader reader) => new (
33-
reader.ReadStringToNull(),
34-
reader.ReadUInt32(),
35-
reader.ReadStringToNull(),
36-
reader.ReadStringToNull(),
37-
reader.ReadInt64(),
38-
reader.ReadUInt32(),
39-
reader.ReadUInt32(),
40-
(ArchiveFlags)reader.ReadUInt32()
32+
public static Header Parse(ref DataBuffer db) => new (
33+
db.ReadNullTerminatedString(),
34+
db.ReadUInt32(),
35+
db.ReadNullTerminatedString(),
36+
db.ReadNullTerminatedString(),
37+
db.ReadInt64(),
38+
db.ReadUInt32(),
39+
db.ReadUInt32(),
40+
(ArchiveFlags)db.ReadUInt32()
4141
);
4242

43-
public void Serialize(AssetWriter writer)
43+
public void Serialize(ref DataBuffer db)
4444
{
45-
writer.WriteStringToNull(Signature);
46-
writer.WriteUInt32(Version);
47-
writer.WriteStringToNull(UnityVersion);
48-
writer.WriteStringToNull(UnityRevision);
49-
writer.WriteInt64(Size);
50-
writer.WriteUInt32(CompressedBlocksInfoSize);
51-
writer.WriteUInt32(UncompressedBlocksInfoSize);
52-
writer.WriteUInt32((UInt32)Flags);
45+
db.WriteNullTerminatedString(Signature);
46+
db.WriteUInt32(Version);
47+
db.WriteNullTerminatedString(UnityVersion);
48+
db.WriteNullTerminatedString(UnityRevision);
49+
db.WriteInt64(Size);
50+
db.WriteUInt32(CompressedBlocksInfoSize);
51+
db.WriteUInt32(UncompressedBlocksInfoSize);
52+
db.WriteUInt32((UInt32)Flags);
5353
}
5454

5555
public long SerializeSize => 27 + Signature.Length + UnityVersion.Length + UnityRevision.Length;

UnityAsset.NET/BundleFiles/StorageBlockInfo.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ public StorageBlockInfo(UInt32 uncompressedSize, UInt32 compressedSize, StorageB
1818
Flags = flags;
1919
}
2020

21-
public static StorageBlockInfo ParseFromReader(AssetReader reader) => new (
22-
reader.ReadUInt32(),
23-
reader.ReadUInt32(),
24-
(StorageBlockFlags)reader.ReadUInt16()
21+
public static StorageBlockInfo Parse(ref DataBuffer db) => new (
22+
db.ReadUInt32(),
23+
db.ReadUInt32(),
24+
(StorageBlockFlags)db.ReadUInt16()
2525
);
2626

27-
public void Serialize(AssetWriter writer) {
28-
writer.WriteUInt32(UncompressedSize);
29-
writer.WriteUInt32(CompressedSize);
30-
writer.WriteUInt16((UInt16)Flags);
27+
public void Serialize(ref DataBuffer db) {
28+
db.WriteUInt32(UncompressedSize);
29+
db.WriteUInt32(CompressedSize);
30+
db.WriteUInt16((UInt16)Flags);
3131
}
3232

3333
public long SerializeSize => 10;

UnityAsset.NET/BundleFiles/UnityCN.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ public sealed unsafe class UnityCN
3232

3333
private bool isIndexSpecial = true;
3434

35-
public UnityCN(AssetReader reader, string key)
35+
public UnityCN(ref DataBuffer db, string key)
3636
{
3737
SetKey(key);
3838

39-
value = reader.ReadUInt32();
39+
value = db.ReadUInt32();
4040

41-
InfoBytes = reader.ReadBytes(0x10);
42-
InfoKey = reader.ReadBytes(0x10);
43-
reader.Position += 1;
41+
InfoBytes = db.ReadBytes(0x10);
42+
InfoKey = db.ReadBytes(0x10);
43+
db.Advance(1);
4444

45-
SignatureBytes = reader.ReadBytes(0x10);
46-
SignatureKey = reader.ReadBytes(0x10);
47-
reader.Position += 1;
45+
SignatureBytes = db.ReadBytes(0x10);
46+
SignatureKey = db.ReadBytes(0x10);
47+
db.Advance(1);
4848

4949
reset();
5050

@@ -120,17 +120,19 @@ public void reset()
120120
}
121121
}
122122

123-
public void Write(AssetWriter writer)
123+
public void Serialize(ref DataBuffer db)
124124
{
125-
writer.WriteUInt32(value);
126-
writer.Write(InfoBytes);
127-
writer.Write(InfoKey);
128-
writer.Write((byte)0);
129-
writer.Write(SignatureBytes);
130-
writer.Write(SignatureKey);
131-
writer.Write((byte)0);
125+
db.WriteUInt32(value);
126+
db.WriteBytes(InfoBytes);
127+
db.WriteBytes(InfoKey);
128+
db.WriteByte((byte)0);
129+
db.WriteBytes(SignatureBytes);
130+
db.WriteBytes(SignatureKey);
131+
db.WriteByte((byte)0);
132132
}
133133

134+
public long SerializeSize => 70;
135+
134136
public bool SetKey(string key)
135137
{
136138
try

UnityAsset.NET/Compression/Compression.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,35 @@ public static long CompressStream(ReadOnlySpan<byte> uncompressedData, MemoryStr
103103
throw new ArgumentException($"Unsupported compression type {compressionType}");
104104
}
105105
}
106+
107+
public static long CompressToBytes(ReadOnlySpan<byte> uncompressedData, Span<byte> compressedData, CompressionType compressionType)
108+
{
109+
switch (compressionType)
110+
{
111+
case CompressionType.None:
112+
uncompressedData.CopyTo(compressedData);
113+
return uncompressedData.Length;
114+
case CompressionType.Lz4:
115+
int compressedSize = LZ4Codec.Encode(uncompressedData, compressedData);
116+
return compressedSize;
117+
case CompressionType.Lz4HC:
118+
int compressedSizeHc = LZ4Codec.Encode(uncompressedData, compressedData, LZ4Level.L12_MAX);
119+
return compressedSizeHc;
120+
case CompressionType.Lzma:
121+
var encoder = new Encoder();
122+
MemoryStream compressedStream = new MemoryStream();
123+
MemoryStream subStream = new MemoryStream();
124+
encoder.WriteCoderProperties(compressedStream);
125+
MemoryStream uncompressedStream = new MemoryStream(uncompressedData.ToArray());
126+
encoder.Code(uncompressedStream, subStream, -1, -1, null);
127+
subStream.Position = 0;
128+
subStream.CopyTo(compressedStream);
129+
var size = compressedStream.Position;
130+
compressedStream.Position = 0;
131+
compressedStream.ReadExactly(compressedData.Slice(0, (int)size));
132+
return size;
133+
default:
134+
throw new ArgumentException($"Unsupported compression type {compressionType}");
135+
}
136+
}
106137
}

UnityAsset.NET/Hash128.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public Hash128(byte[] data)
1111
{
1212
this.data = data;
1313
}
14-
public Hash128(AssetReader reader)
14+
public Hash128(ref DataBuffer db)
1515
{
16-
data = reader.ReadBytes(16);
16+
data = db.ReadBytes(16);
1717
}
1818

1919
public bool IsZero()
@@ -47,8 +47,8 @@ public static Hash128 NewBlankHash()
4747
return new Hash128() { data = new byte[16] };
4848
}
4949

50-
public void Write(AssetWriter writer)
50+
public void Serialize(ref DataBuffer db)
5151
{
52-
writer.Write(data);
52+
db.WriteBytes(data);
5353
}
5454
}

0 commit comments

Comments
 (0)