Skip to content

Commit 5a5d06f

Browse files
committed
Asset
1 parent b9b6153 commit 5a5d06f

22 files changed

Lines changed: 664 additions & 524 deletions

UnityAsset.NET/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
1+
/bin
2+
/obj
3+

UnityAsset.NET/Asset.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Specialized;
2+
using UnityAsset.NET.IO;
3+
using UnityAsset.NET.SerializedFiles;
4+
5+
namespace UnityAsset.NET;
6+
7+
public class Asset
8+
{
9+
public AssetFileInfo Info;
10+
public HeapDataBuffer RawData;
11+
public NodeData NodeData;
12+
13+
public Asset(AssetFileInfo info, HeapDataBuffer hdb)
14+
{
15+
Info = info;
16+
RawData = hdb;
17+
var nodeDataList = new List<NodeData>();
18+
for (int i = 0; i < info.Type.Nodes.Count; i++)
19+
{
20+
var typeTreeNode = info.Type.Nodes[i];
21+
var node = new NodeData(typeTreeNode);
22+
nodeDataList.Add(node);
23+
}
24+
var parent = nodeDataList[0];
25+
for (int i = 1; i < nodeDataList.Count; i++)
26+
{
27+
while (nodeDataList[i].Level <= parent.Level)
28+
{
29+
parent = parent.Parent;
30+
}
31+
nodeDataList[i].Parent = parent;
32+
parent.Children ??= new ();
33+
parent.Children.Add(nodeDataList[i]);
34+
parent = nodeDataList[i];
35+
}
36+
for (int i = 0; i < nodeDataList.Count; i++)
37+
{
38+
var node = nodeDataList[i];
39+
node.Value = NodeData.ReadValue(nodeDataList, hdb, ref i);
40+
}
41+
NodeData = nodeDataList[0];
42+
}
43+
}

UnityAsset.NET/BundleFiles/BlocksAndCabsInfo.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityAsset.NET.IO;
1+
using System.Text;
2+
using UnityAsset.NET.IO;
23

34
namespace UnityAsset.NET.BundleFiles;
45

@@ -32,4 +33,21 @@ public void Serialize(ref DataBuffer db) {
3233
public long SerializeSize => UncompressedDataHash.Length + 8 +
3334
BlocksInfo.Sum(item => item.SerializeSize) +
3435
DirectoryInfo.Sum(item => item.SerializeSize);
36+
37+
public override string ToString()
38+
{
39+
StringBuilder sb = new StringBuilder();
40+
sb.AppendLine($"UncompressedDataHash: {BitConverter.ToString(UncompressedDataHash)}");
41+
for (int i = 0; i < BlocksInfo.Count; ++i)
42+
{
43+
sb.Append($"Block {i}: {BlocksInfo[i]}");
44+
}
45+
sb.AppendLine();
46+
for (int i = 0; i < DirectoryInfo.Count; ++i)
47+
{
48+
sb.Append($"Directory {i}: {DirectoryInfo[i]}");
49+
}
50+
sb.AppendLine();
51+
return sb.ToString();
52+
}
3553
}

UnityAsset.NET/BundleFiles/BundleFile.cs

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ public class BundleFile
1818
/// </summary>
1919
public BlocksAndCabsInfo? DataInfo;
2020

21-
public List<ICabFile>? CabFiles;
21+
public List<CabFileWrapper>? CabFiles;
2222

23-
public UnityCN? UnityCN;
23+
public UnityCN? UnityCnInfo;
2424

25-
public string? UnityCNKey;
25+
public string? UnityCnKey;
2626

27-
public static UnityCN? ParseUnityCN(ref DataBuffer db, Header header, string? key)
27+
public static UnityCN? ParseUnityCnInfo(ref DataBuffer db, Header header, string? key)
2828
{
2929
var version = ParseVersion(header);
30-
var unityCNMask = (version[0] < 2020 || //2020 and earlier
30+
var unityCnMask = (version[0] < 2020 || //2020 and earlier
3131
(version[0] == 2020 && version[1] == 3 && version[2] <= 34) || //2020.3.34 and earlier
3232
(version[0] == 2021 && version[1] == 3 && version[2] <= 2) || //2021.3.2 and earlier
3333
(version[0] == 2022 && version[1] == 3 && version[2] <= 1)) //2022.3.1 and earlier
3434
? ArchiveFlags.BlockInfoNeedPaddingAtStart : ArchiveFlags.UnityCNEncryption | ArchiveFlags.UnityCNEncryptionNew;
3535

36-
if ((header.Flags & unityCNMask) != 0)
36+
if ((header.Flags & unityCnMask) != 0)
3737
{
3838
if (key == null) key = Setting.DefaultUnityCNKey;
3939
if (key == null)
4040
{
41-
throw new Exception($"UnityCN key is required for decryption. UnityCN Flag Mask: {unityCNMask}");
41+
throw new Exception($"UnityCN key is required for decryption. UnityCN Flag Mask: {unityCnMask}");
4242
}
43-
var unityCNInfo = new UnityCN(ref db, key);
44-
return unityCNInfo;
43+
var unityCnInfo = new UnityCN(ref db, key);
44+
return unityCnInfo;
4545
}
4646

4747
return null;
@@ -102,10 +102,10 @@ public static BlocksAndCabsInfo ParseDataInfo(ref DataBuffer db, Header header)
102102
return dataInfo;
103103
}
104104

105-
public static List<ICabFile> ParseCabFiles(ref DataBuffer db, BlocksAndCabsInfo dataInfo, UnityCN? unityCN = null)
105+
public static List<CabFileWrapper> ParseCabFiles(ref DataBuffer db, BlocksAndCabsInfo dataInfo, UnityCN? unityCnInfo = null)
106106
{
107107
DataBuffer blocksBuffer = new DataBuffer(dataInfo.BlocksInfo.Sum(block => (int)block.UncompressedSize));
108-
if (unityCN == null)
108+
if (unityCnInfo == null)
109109
{
110110
foreach (var blockInfo in dataInfo.BlocksInfo)
111111
{
@@ -125,7 +125,7 @@ public static List<ICabFile> ParseCabFiles(ref DataBuffer db, BlocksAndCabsInfo
125125
var compressionType = (CompressionType)(blockInfo.Flags & StorageBlockFlags.CompressionTypeMask);
126126
if (compressionType == CompressionType.Lz4 || compressionType == CompressionType.Lz4HC)
127127
{
128-
unityCN.DecryptAndDecompress(
128+
unityCnInfo.DecryptAndDecompress(
129129
db.SliceForward((int)blockInfo.CompressedSize),
130130
blocksBuffer.SliceForward((int)blockInfo.UncompressedSize),
131131
i);
@@ -139,54 +139,54 @@ public static List<ICabFile> ParseCabFiles(ref DataBuffer db, BlocksAndCabsInfo
139139
}
140140
}
141141
blocksBuffer.Seek(0);
142-
var cabFiles = new List<ICabFile>();
142+
var cabFiles = new List<CabFileWrapper>();
143143
foreach (var cab in dataInfo.DirectoryInfo)
144144
{
145145
if (cab.Path.EndsWith(".resS"))
146146
{
147-
cabFiles.Add(new HeapDataBuffer(blocksBuffer.SliceForward((int)cab.Size).ToArray()));
147+
cabFiles.Add(new CabFileWrapper(new HeapDataBuffer(blocksBuffer.SliceForward((int)cab.Size).ToArray()), cab));
148148
blocksBuffer.Advance((int)cab.Size);
149149
}
150150
else
151151
{
152152
var cabBuffer = blocksBuffer.SliceBuffer((int)cab.Size);
153-
cabFiles.Add(SerializedFile.Parse(ref cabBuffer));
153+
cabFiles.Add(new CabFileWrapper(SerializedFile.Parse(ref cabBuffer), cab));
154154
blocksBuffer.Advance((int)cab.Size);
155155
}
156156
}
157157
return cabFiles;
158158
}
159159

160-
public BundleFile(Header header, BlocksAndCabsInfo dataInfo, List<ICabFile> cabFiles, string? key = null)
160+
public BundleFile(Header header, BlocksAndCabsInfo dataInfo, List<CabFileWrapper> cabFiles, string? key = null)
161161
{
162-
UnityCNKey = key;
162+
UnityCnKey = key;
163163
Header = header;
164164
DataInfo = dataInfo;
165165
CabFiles = cabFiles;
166166
}
167167

168168
public BundleFile(ref DataBuffer db, string? key = null)
169169
{
170-
UnityCNKey = key;
170+
UnityCnKey = key;
171171
Header = Header.Parse(ref db);
172-
UnityCN = ParseUnityCN(ref db, Header, UnityCNKey);
172+
UnityCnInfo = ParseUnityCnInfo(ref db, Header, UnityCnKey);
173173
AlignAfterHeader(ref db, Header);
174174
DataInfo = ParseDataInfo(ref db, Header);
175-
CabFiles = ParseCabFiles(ref db, DataInfo, UnityCN);
175+
CabFiles = ParseCabFiles(ref db, DataInfo, UnityCnInfo);
176176
}
177177

178178
public BundleFile(string path, string? key = null)
179179
{
180180
var db = DataBuffer.FromFile(path);
181-
UnityCNKey = key;
181+
UnityCnKey = key;
182182
Header = Header.Parse(ref db);
183-
UnityCN = ParseUnityCN(ref db, Header, UnityCNKey);
183+
UnityCnInfo = ParseUnityCnInfo(ref db, Header, UnityCnKey);
184184
AlignAfterHeader(ref db, Header);
185185
DataInfo = ParseDataInfo(ref db, Header);
186-
CabFiles = ParseCabFiles(ref db, DataInfo, UnityCN);
186+
CabFiles = ParseCabFiles(ref db, DataInfo, UnityCnInfo);
187187
}
188188

189-
public void Serialize(ref DataBuffer db, CompressionType infoPacker = CompressionType.None, CompressionType dataPacker = CompressionType.None, string? unityCNKey = null)
189+
public void Serialize(ref DataBuffer db, CompressionType infoPacker = CompressionType.None, CompressionType dataPacker = CompressionType.None, string? unityCnKey = null)
190190
{
191191
if (Header == null || DataInfo == null || CabFiles == null)
192192
{
@@ -196,30 +196,29 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
196196
var directoryInfo = new List<CabInfo>();
197197
var cabDataBufferCapacity = CabFiles.Sum(c =>
198198
{
199-
if (c is HeapDataBuffer hdb) return hdb.Capacity;
200-
if (c is SerializedFile sf) return sf.SerializeSize;
199+
if (c.File is HeapDataBuffer hdb) return hdb.Capacity;
200+
if (c.File is SerializedFile sf) return sf.SerializeSize;
201201
return 0;
202202
});
203203
DataBuffer cabDataBuffer = new DataBuffer((int)cabDataBufferCapacity);
204204
Int64 offset = 0;
205205
for (int i = 0; i < CabFiles.Count; i++)
206206
{
207-
Int64 cabSize = 0;
208-
if (CabFiles[i] is HeapDataBuffer hdb)
207+
Int64 cabSize;
208+
switch (CabFiles[i].File)
209209
{
210-
cabDataBuffer.WriteBytes(hdb.AsSpan());
211-
cabSize = hdb.Length;
212-
}
213-
else if (CabFiles[i] is SerializedFile sf)
214-
{
215-
var subBuffer = cabDataBuffer.SliceBufferToEnd();
216-
sf.Serialize(ref subBuffer);
217-
cabSize = subBuffer.Position;
218-
cabDataBuffer.Advance((int)cabSize);
219-
}
220-
else
221-
{
222-
throw new Exception($"Unexpect type cab");
210+
case HeapDataBuffer hdb:
211+
cabDataBuffer.WriteBytes(hdb.AsSpan());
212+
cabSize = hdb.Length;
213+
break;
214+
case SerializedFile sf:
215+
var subBuffer = cabDataBuffer.SliceBufferToEnd();
216+
sf.Serialize(ref subBuffer);
217+
cabDataBuffer.Advance(subBuffer.Position);
218+
cabSize = subBuffer.Position;
219+
break;
220+
default:
221+
throw new Exception($"Unexpected type: {CabFiles[i].File.GetType().Name}");
223222
}
224223
directoryInfo.Add(new CabInfo(offset, cabSize, DataInfo.DirectoryInfo[i].Flags, DataInfo.DirectoryInfo[i].Path));
225224
offset += cabSize;
@@ -243,18 +242,18 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
243242
}
244243
compressedBlocksDataBuffer.Seek(0);
245244

246-
UnityCN? unityCn = null;
245+
UnityCN? unityCnInfo = null;
247246
var version = ParseVersion(Header);
248-
if (unityCNKey != null)
247+
if (unityCnKey != null)
249248
{
250-
unityCn = new UnityCN(unityCNKey);
249+
unityCnInfo = new UnityCN(unityCnKey);
251250
if (dataPacker == CompressionType.Lz4 || dataPacker == CompressionType.Lz4HC)
252251
{
253252
for (int i = 0; i < blocksInfo.Count; i++)
254253
{
255254
var blockInfo = blocksInfo[i];
256255
blockInfo.Flags |= (StorageBlockFlags)0x100;
257-
unityCn.EncryptBlock(compressedBlocksDataBuffer.SliceForward((int)blockInfo.CompressedSize), (int)blockInfo.CompressedSize, i);
256+
unityCnInfo.EncryptBlock(compressedBlocksDataBuffer.SliceForward((int)blockInfo.CompressedSize), (int)blockInfo.CompressedSize, i);
258257
compressedBlocksDataBuffer.Advance((int)blockInfo.CompressedSize);
259258
}
260259
compressedBlocksDataBuffer.Seek(0);
@@ -264,19 +263,19 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
264263
throw new Exception($"UnityCN Encryption only support Lz4/Lz4HC, but {dataPacker} was set.");
265264
}
266265
}
267-
else if (UnityCNKey != null)
266+
else if (UnityCnKey != null)
268267
{
269-
var unityCNMask = (version[0] < 2020 || //2020 and earlier
268+
var unityCnMask = (version[0] < 2020 || //2020 and earlier
270269
(version[0] == 2020 && version[1] == 3 && version[2] <= 34) || //2020.3.34 and earlier
271270
(version[0] == 2021 && version[1] == 3 && version[2] <= 2) || //2021.3.2 and earlier
272271
(version[0] == 2022 && version[1] == 3 && version[2] <= 1)) //2022.3.1 and earlier
273272
? ArchiveFlags.BlockInfoNeedPaddingAtStart : ArchiveFlags.UnityCNEncryption | ArchiveFlags.UnityCNEncryptionNew;
274-
if (unityCNMask == (ArchiveFlags.UnityCNEncryption | ArchiveFlags.UnityCNEncryptionNew))
273+
if (unityCnMask == (ArchiveFlags.UnityCNEncryption | ArchiveFlags.UnityCNEncryptionNew))
275274
{
276-
unityCNMask = ((Header.Flags & ArchiveFlags.UnityCNEncryptionNew) != 0) ?
275+
unityCnMask = ((Header.Flags & ArchiveFlags.UnityCNEncryptionNew) != 0) ?
277276
ArchiveFlags.UnityCNEncryptionNew : ArchiveFlags.UnityCNEncryption;
278277
}
279-
Header.Flags &= ~unityCNMask;
278+
Header.Flags &= ~unityCnMask;
280279
}
281280

282281
var dataInfo = new BlocksAndCabsInfo(DataInfo.UncompressedDataHash, blocksInfo, directoryInfo);
@@ -289,11 +288,11 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
289288
var header = new Header(Header.Signature, Header.Version, Header.UnityVersion, Header.UnityRevision,
290289
0, (uint)compressedBlocksInfoSize, (uint)uncompressedBlocksInfoSize,
291290
(Header.Flags & ~ArchiveFlags.CompressionTypeMask) | (ArchiveFlags)infoPacker);
292-
db.EnsureCapacity((int)(header.SerializeSize + 16 + compressedBlocksInfoSize + compressedBlocksDataBuffer.Length + (unityCn == null ? 0: unityCn.SerializeSize)));
291+
db.EnsureCapacity((int)(header.SerializeSize + 16 + compressedBlocksInfoSize + compressedBlocksDataBuffer.Length + (unityCnInfo == null ? 0: unityCnInfo.SerializeSize)));
293292
header.Serialize(ref db);
294-
if (unityCn != null)
293+
if (unityCnInfo != null)
295294
{
296-
unityCn.Serialize(ref db);
295+
unityCnInfo.Serialize(ref db);
297296
}
298297
if (header.Version >= 7)
299298
{
@@ -338,10 +337,10 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
338337
}
339338

340339
public void Serialize(string path, CompressionType infoPacker = CompressionType.None,
341-
CompressionType dataPacker = CompressionType.None, string? unityCNKey = null)
340+
CompressionType dataPacker = CompressionType.None, string? unityCnKey = null)
342341
{
343342
DataBuffer db = new DataBuffer(0);
344-
Serialize(ref db, infoPacker, dataPacker, unityCNKey);
343+
Serialize(ref db, infoPacker, dataPacker, unityCnKey);
345344
db.WriteToFile(path);
346345
}
347346

UnityAsset.NET/BundleFiles/CabInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public void Serialize(ref DataBuffer db) {
3737
public override string ToString()
3838
{
3939
StringBuilder sb = new StringBuilder();
40-
sb.AppendFormat("offset: 0x{0:X8} | ", Offset);
41-
sb.AppendFormat("size: 0x{0:X8} | ", Size);
42-
sb.AppendFormat("flags: {0} | ", Flags);
43-
sb.AppendFormat("path: {0}", Path);
40+
sb.Append($"Offset: 0x{Offset:X8} | ");
41+
sb.Append($"Size: 0x{Size:X8} | ");
42+
sb.Append($"Flags: {Flags} | ");
43+
sb.Append($"Path: {Path}");
4444
return sb.ToString();
4545
}
4646
}

UnityAsset.NET/BundleFiles/StorageBlockInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public void Serialize(ref DataBuffer db) {
3535
public override string ToString()
3636
{
3737
StringBuilder sb = new StringBuilder();
38-
sb.AppendFormat("uncompressedSize: 0x{0:X8} | ", UncompressedSize);
39-
sb.AppendFormat("compressedSize: 0x{0:X8} | ", CompressedSize);
40-
sb.AppendFormat("flags: 0x{0:X4}", (int)Flags);
38+
sb.Append($"UncompressedSize: 0x{UncompressedSize:X8} | ");
39+
sb.Append($"CompressedSize: 0x{CompressedSize:X8} | ");
40+
sb.Append($"Flags: 0x{(int)Flags:X4}");
4141
return sb.ToString();
4242
}
4343
}

0 commit comments

Comments
 (0)