Skip to content

Commit 6fb3a8b

Browse files
committed
Refactor IO
1 parent 2b2ba33 commit 6fb3a8b

22 files changed

Lines changed: 197 additions & 611 deletions

UnityAsset.NET/Asset.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace UnityAsset.NET;
77
public class Asset
88
{
99
public AssetFileInfo Info;
10-
public HeapDataBuffer RawData;
10+
public DataBuffer RawData;
1111
public NodeData NodeData;
1212

13-
public Asset(AssetFileInfo info, HeapDataBuffer hdb)
13+
public Asset(AssetFileInfo info, DataBuffer db)
1414
{
1515
Info = info;
16-
RawData = hdb;
16+
RawData = db;
1717
var nodeDataList = new List<NodeData>();
1818
for (int i = 0; i < info.Type.Nodes.Count; i++)
1919
{
@@ -36,7 +36,7 @@ public Asset(AssetFileInfo info, HeapDataBuffer hdb)
3636
for (int i = 0; i < nodeDataList.Count; i++)
3737
{
3838
var node = nodeDataList[i];
39-
node.Value = NodeData.ReadValue(nodeDataList, hdb, ref i);
39+
node.Value = NodeData.ReadValue(nodeDataList, db, ref i);
4040
}
4141
NodeData = nodeDataList[0];
4242
}

UnityAsset.NET/Files/BundleFileExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void PatchCrc32(this BundleFile bf, uint newCrc32)
1717
if (bf.Crc32 != newCrc32)
1818
{
1919
var patchBytes = CRC32.rCRC(newCrc32, bf.Crc32);
20-
bf.Files.Add(new FileWrapper(new HeapDataBuffer(patchBytes), new FileEntry(0, 4, 0, "crc32-patch-data")));
20+
bf.Files.Add(new FileWrapper(new DataBuffer(patchBytes), new FileEntry(0, 4, 0, "crc32-patch-data")));
2121
}
2222
}
2323
}

UnityAsset.NET/Files/BundleFiles/BlocksAndDirectoryInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ public BlocksAndDirectoryInfo(byte[] uncompressedDataHash,
1818
DirectoryInfo = directoryInfo;
1919
}
2020

21-
public static BlocksAndDirectoryInfo Parse(ref DataBuffer db) => new (
21+
public static BlocksAndDirectoryInfo Parse(DataBuffer db) => new (
2222
db.ReadBytes(16),
2323
db.ReadList(db.ReadInt32(), StorageBlockInfo.Parse),
2424
db.ReadList(db.ReadInt32(), FileEntry.Parse)
2525
);
2626

27-
public void Serialize(ref DataBuffer db) {
27+
public void Serialize(DataBuffer db) {
2828
db.WriteBytes(UncompressedDataHash);
29-
db.WriteListWithCount(BlocksInfo, (ref DataBuffer d, StorageBlockInfo info) => info.Serialize(ref d));
30-
db.WriteListWithCount(DirectoryInfo, (ref DataBuffer d, FileEntry info) => info.Serialize(ref d));
29+
db.WriteListWithCount(BlocksInfo, (DataBuffer d, StorageBlockInfo info) => info.Serialize(d));
30+
db.WriteListWithCount(DirectoryInfo, (DataBuffer d, FileEntry info) => info.Serialize(d));
3131
}
3232

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

UnityAsset.NET/Files/BundleFiles/BundleFile.cs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class BundleFile
3131

3232
public uint Crc32;
3333

34-
public static UnityCN? ParseUnityCnInfo(ref DataBuffer db, Header header, string? key)
34+
public static UnityCN? ParseUnityCnInfo(DataBuffer db, Header header, string? key)
3535
{
3636
var unityCnMask = VersionJudge1(ParseVersion(header))
3737
? ArchiveFlags.BlockInfoNeedPaddingAtStart
@@ -42,12 +42,12 @@ public class BundleFile
4242
key ??= Setting.DefaultUnityCNKey;
4343
if (key == null)
4444
throw new Exception($"UnityCN key is required for decryption. UnityCN Flag Mask: {unityCnMask}");
45-
return new UnityCN(ref db, key);
45+
return new UnityCN(db, key);
4646
}
4747
return null;
4848
}
4949

50-
public static void AlignAfterHeader(ref DataBuffer db, Header header)
50+
public static void AlignAfterHeader(DataBuffer db, Header header)
5151
{
5252
if (header.Version >= 7)
5353
db.Align(16);
@@ -59,21 +59,21 @@ public static void AlignAfterHeader(ref DataBuffer db, Header header)
5959
}
6060
}
6161

62-
public static BlocksAndDirectoryInfo ParseDataInfo(ref DataBuffer db, Header header)
62+
public static BlocksAndDirectoryInfo ParseDataInfo(DataBuffer db, Header header)
6363
{
6464
Span<byte> blocksInfoBytes = (header.Flags & ArchiveFlags.BlocksInfoAtTheEnd) == 0
6565
? db.ReadSpanBytes((int)header.CompressedBlocksInfoSize)
66-
: db[(int)(header.Size - header.CompressedBlocksInfoSize)..(int)(header.Size)];
66+
: db[(int)(header.Size - header.CompressedBlocksInfoSize)..(int)header.Size];
6767
var compressionType = (CompressionType)(header.Flags & ArchiveFlags.CompressionTypeMask);
6868
DataBuffer blocksInfoUncompressedData = new DataBuffer((int)header.UncompressedBlocksInfoSize);
6969
Compression.DecompressToBytes(blocksInfoBytes, blocksInfoUncompressedData.AsSpan(), compressionType);
70-
var dataInfo = BlocksAndDirectoryInfo.Parse(ref blocksInfoUncompressedData);
70+
var dataInfo = BlocksAndDirectoryInfo.Parse(blocksInfoUncompressedData);
7171
if (!VersionJudge1(ParseVersion(header)) && (header.Flags & ArchiveFlags.BlockInfoNeedPaddingAtStart) != 0)
7272
db.Align(16);
7373
return dataInfo;
7474
}
7575

76-
public static (List<FileWrapper>, uint) ParseFiles(ref DataBuffer db, BlocksAndDirectoryInfo dataInfo, UnityCN? unityCnInfo = null)
76+
public static (List<FileWrapper>, uint) ParseFiles(DataBuffer db, BlocksAndDirectoryInfo dataInfo, UnityCN? unityCnInfo = null)
7777
{
7878
DataBuffer blocksBuffer = new DataBuffer(dataInfo.BlocksInfo.Sum(block => (int)block.UncompressedSize));
7979
if (unityCnInfo == null)
@@ -107,12 +107,12 @@ public static (List<FileWrapper>, uint) ParseFiles(ref DataBuffer db, BlocksAndD
107107
if (dir.Path.StartsWith("CAB-") && !dir.Path.EndsWith(".resS"))
108108
{
109109
var cabBuffer = blocksBuffer.SliceBuffer((int)dir.Size);
110-
files.Add(new FileWrapper(SerializedFile.Parse(ref cabBuffer), dir));
110+
files.Add(new FileWrapper(SerializedFile.Parse(cabBuffer), dir));
111111
blocksBuffer.Advance((int)dir.Size);
112112
}
113113
else
114114
{
115-
files.Add(new FileWrapper(new HeapDataBuffer(blocksBuffer.ReadSpanBytes((int)dir.Size).ToArray()), dir));
115+
files.Add(new FileWrapper(new DataBuffer(blocksBuffer.ReadSpanBytes((int)dir.Size).ToArray()), dir));
116116
}
117117
}
118118

@@ -127,37 +127,26 @@ public BundleFile(Header header, BlocksAndDirectoryInfo dataInfo, List<FileWrapp
127127
Files = files;
128128
}
129129

130-
public BundleFile(ref DataBuffer db, string? key = null)
130+
public BundleFile(DataBuffer db, string? key = null)
131131
{
132132
UnityCnKey = key;
133-
Header = Header.Parse(ref db);
134-
UnityCnInfo = ParseUnityCnInfo(ref db, Header, UnityCnKey);
135-
AlignAfterHeader(ref db, Header);
136-
DataInfo = ParseDataInfo(ref db, Header);
137-
(Files, Crc32) = ParseFiles(ref db, DataInfo, UnityCnInfo);
133+
Header = Header.Parse(db);
134+
UnityCnInfo = ParseUnityCnInfo(db, Header, UnityCnKey);
135+
AlignAfterHeader(db, Header);
136+
DataInfo = ParseDataInfo(db, Header);
137+
(Files, Crc32) = ParseFiles(db, DataInfo, UnityCnInfo);
138138
}
139139

140-
public BundleFile(string path, string? key = null)
141-
{
142-
var db = DataBuffer.FromFile(path);
143-
UnityCnKey = key;
144-
Header = Header.Parse(ref db);
145-
if (string.IsNullOrEmpty(Header.UnityRevision))
146-
Header.UnityRevision = Setting.DefaultUnityVerion;
147-
UnityCnInfo = ParseUnityCnInfo(ref db, Header, UnityCnKey);
148-
AlignAfterHeader(ref db, Header);
149-
DataInfo = ParseDataInfo(ref db, Header);
150-
(Files, Crc32) = ParseFiles(ref db, DataInfo, UnityCnInfo);
151-
}
140+
public BundleFile(string path, string? key = null) : this(DataBuffer.FromFile(path), key) {}
152141

153-
public void Serialize(ref DataBuffer db, CompressionType infoPacker = CompressionType.None, CompressionType dataPacker = CompressionType.None, string? unityCnKey = null)
142+
public void Serialize(DataBuffer db, CompressionType infoPacker = CompressionType.None, CompressionType dataPacker = CompressionType.None, string? unityCnKey = null)
154143
{
155144
if (Header == null || DataInfo == null || Files == null)
156145
throw new NullReferenceException("BundleFile has not read completely");
157146
var directoryInfo = new List<FileEntry>();
158147
var filesCapacity = Files.Sum(c =>
159148
{
160-
if (c.File is HeapDataBuffer hdb) return hdb.Capacity;
149+
if (c.File is DataBuffer hdb) return hdb.Capacity;
161150
if (c.File is SerializedFile sf) return sf.SerializeSize;
162151
return 0;
163152
});
@@ -168,13 +157,13 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
168157
Int64 cabSize;
169158
switch (file.File)
170159
{
171-
case HeapDataBuffer hdb:
172-
filesBuffer.WriteBytes(hdb.AsSpan());
173-
cabSize = hdb.Length;
160+
case DataBuffer dataBuffer:
161+
filesBuffer.WriteBytes(dataBuffer.AsSpan());
162+
cabSize = dataBuffer.Length;
174163
break;
175164
case SerializedFile sf:
176165
var subBuffer = filesBuffer.SliceBufferToEnd();
177-
sf.Serialize(ref subBuffer);
166+
sf.Serialize(subBuffer);
178167
filesBuffer.Advance(subBuffer.Position);
179168
cabSize = subBuffer.Position;
180169
break;
@@ -234,7 +223,7 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
234223

235224
var dataInfo = new BlocksAndDirectoryInfo(DataInfo.UncompressedDataHash, blocksInfo, directoryInfo);
236225
DataBuffer dataInfoBuffer = new DataBuffer((int)dataInfo.SerializeSize);
237-
dataInfo.Serialize(ref dataInfoBuffer);
226+
dataInfo.Serialize(dataInfoBuffer);
238227
var uncompressedBlocksInfoSize = dataInfoBuffer.Length;
239228
DataBuffer compressedDataInfoBuffer = new DataBuffer(uncompressedBlocksInfoSize);
240229
var compressedBlocksInfoSize = Compression.CompressToBytes(dataInfoBuffer.AsSpan(), compressedDataInfoBuffer.AsSpan(), infoPacker);
@@ -243,9 +232,9 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
243232
0, (uint)compressedBlocksInfoSize, (uint)uncompressedBlocksInfoSize,
244233
(Header.Flags & ~ArchiveFlags.CompressionTypeMask) | (ArchiveFlags)infoPacker);
245234
db.EnsureCapacity((int)(header.SerializeSize + 16 + compressedBlocksInfoSize + compressedBlocksDataBuffer.Length + unityCnInfo?.SerializeSize ?? 0));
246-
header.Serialize(ref db);
235+
header.Serialize(db);
247236
if (unityCnInfo != null)
248-
unityCnInfo.Serialize(ref db);
237+
unityCnInfo.Serialize(db);
249238
if (header.Version >= 7)
250239
db.Align(16);
251240
else // temp fix for 2019.4.x
@@ -263,14 +252,14 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
263252
var size = db.Position;
264253
header.Size = size;
265254
db.Seek(0);
266-
header.Serialize(ref db);
255+
header.Serialize(db);
267256
}
268257

269258
public void Serialize(string path, CompressionType infoPacker = CompressionType.None,
270259
CompressionType dataPacker = CompressionType.None, string? unityCnKey = null)
271260
{
272261
DataBuffer db = new DataBuffer(0);
273-
Serialize(ref db, infoPacker, dataPacker, unityCnKey);
262+
Serialize(db, infoPacker, dataPacker, unityCnKey);
274263
db.WriteToFile(path);
275264
}
276265

UnityAsset.NET/Files/BundleFiles/FileEntry.cs

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

21-
public static FileEntry Parse(ref DataBuffer db) => new (
21+
public static FileEntry Parse(DataBuffer db) => new (
2222
db.ReadInt64(),
2323
db.ReadInt64(),
2424
db.ReadUInt32(),
2525
db.ReadNullTerminatedString()
2626
);
2727

28-
public void Serialize(ref DataBuffer db) {
28+
public void Serialize(DataBuffer db) {
2929
db.WriteInt64(Offset);
3030
db.WriteInt64(Size);
3131
db.WriteUInt32(Flags);

UnityAsset.NET/Files/BundleFiles/Header.cs

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

32-
public static Header Parse(ref DataBuffer db) => new (
32+
public static Header Parse(DataBuffer db) => new (
3333
db.ReadNullTerminatedString(),
3434
db.ReadUInt32(),
3535
db.ReadNullTerminatedString(),
@@ -40,7 +40,7 @@ public Header(string signature, UInt32 version, string unityVersion, string unit
4040
(ArchiveFlags)db.ReadUInt32()
4141
);
4242

43-
public void Serialize(ref DataBuffer db)
43+
public void Serialize(DataBuffer db)
4444
{
4545
db.WriteNullTerminatedString(Signature);
4646
db.WriteUInt32(Version);

UnityAsset.NET/Files/BundleFiles/StorageBlockInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public StorageBlockInfo(UInt32 uncompressedSize, UInt32 compressedSize, StorageB
1717
Flags = flags;
1818
}
1919

20-
public static StorageBlockInfo Parse(ref DataBuffer db) => new (
20+
public static StorageBlockInfo Parse(DataBuffer db) => new (
2121
db.ReadUInt32(),
2222
db.ReadUInt32(),
2323
(StorageBlockFlags)db.ReadUInt16()
2424
);
2525

26-
public void Serialize(ref DataBuffer db) {
26+
public void Serialize(DataBuffer db) {
2727
db.WriteUInt32(UncompressedSize);
2828
db.WriteUInt32(CompressedSize);
2929
db.WriteUInt16((UInt16)Flags);

UnityAsset.NET/Files/BundleFiles/UnityCN.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed unsafe class UnityCN
2828

2929
private readonly bool _isIndexSpecial = true;
3030

31-
public UnityCN(ref DataBuffer db, string key)
31+
public UnityCN(DataBuffer db, string key)
3232
{
3333
SetKey(key);
3434

@@ -119,7 +119,7 @@ public void Reset()
119119
}
120120
}
121121

122-
public void Serialize(ref DataBuffer db)
122+
public void Serialize(DataBuffer db)
123123
{
124124
db.WriteUInt32(Value);
125125
db.WriteBytes(InfoBytes);

UnityAsset.NET/Files/SerializedFiles/AssetFileInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AssetFileInfo(Int64 pathId, UInt64 byteOffset, UInt32 byteSize,
2222
Type = type;
2323
}
2424

25-
public static AssetFileInfo Parse(ref DataBuffer db, SerializedFileFormatVersion version, List<SerializedType> types)
25+
public static AssetFileInfo Parse(DataBuffer db, SerializedFileFormatVersion version, List<SerializedType> types)
2626
{
2727
db.Align(4);
2828
var pathId = db.ReadInt64();
@@ -36,7 +36,7 @@ public static AssetFileInfo Parse(ref DataBuffer db, SerializedFileFormatVersion
3636
return new AssetFileInfo(pathId, byteOffset, byteSize, typeIdOrIndex, type);
3737
}
3838

39-
public void Serialize(ref DataBuffer db, SerializedFileFormatVersion version)
39+
public void Serialize(DataBuffer db, SerializedFileFormatVersion version)
4040
{
4141
db.Align(4);
4242
db.WriteInt64(PathId);

UnityAsset.NET/Files/SerializedFiles/AssetPPtr.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public AssetPPtr(Int32 fileId, Int64 pathId)
1414
PathId = pathId;
1515
}
1616

17-
public static AssetPPtr Parse(ref DataBuffer db)
17+
public static AssetPPtr Parse(DataBuffer db)
1818
{
1919
var fileId = db.ReadInt32();
2020
db.Align(4);
2121
var pathId = db.ReadInt64();
2222
return new AssetPPtr(fileId, pathId);
2323
}
2424

25-
public void Serialize(ref DataBuffer db)
25+
public void Serialize(DataBuffer db)
2626
{
2727
db.WriteInt32(FileId);
2828
db.Align(4);

0 commit comments

Comments
 (0)