Skip to content

Commit f231140

Browse files
committed
2024/6/5
1 parent 2cbe13e commit f231140

12 files changed

Lines changed: 651 additions & 60 deletions

UnityAsset.NET/BundleFile/BlocksAndDirectoryInfo.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public void uncompresseFlags()
8080
{
8181
foreach (var block in BlocksInfo)
8282
{
83-
block.flags &= ~StorageBlockFlags.CompressionTypeMask;
83+
//block.flags &= ~StorageBlockFlags.CompressionTypeMask;
84+
block.flags &= 0x0;
8485
block.compressedSize = block.uncompressedSize;
8586
}
8687
Update();
@@ -118,6 +119,33 @@ public void merge()
118119
BlocksInfo = newBlocksInfo;
119120
Update();
120121
}
122+
123+
public void Split()
124+
{
125+
List<StorageBlockInfo> newBlocksInfo = new List<StorageBlockInfo>();
126+
foreach (var block in BlocksInfo)
127+
{
128+
if (block.uncompressedSize > 0x00020000)
129+
{
130+
uint num = block.uncompressedSize / 0x00020000;
131+
uint num2 = block.uncompressedSize % 0x00020000;
132+
for (uint i = 0; i < num; i++)
133+
{
134+
newBlocksInfo.Add(new StorageBlockInfo(0x00020000, 0x00020000, block.flags));
135+
}
136+
if (num2 > 0)
137+
{
138+
newBlocksInfo.Add(new StorageBlockInfo(num2, num2, block.flags));
139+
}
140+
}
141+
else
142+
{
143+
newBlocksInfo.Add(block);
144+
}
145+
}
146+
BlocksInfo = newBlocksInfo;
147+
Update();
148+
}
121149

122150
public void Update(string compressionType = "none")
123151
{

UnityAsset.NET/BundleFile/BundleFile.cs

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,45 @@ namespace UnityAsset.NET.BundleFile;
88

99
public sealed class BundleFile
1010
{
11-
public Header Header;
11+
/// <summary>
12+
/// BundleFile header
13+
/// </summary>
14+
public Header Header { get; set; }
1215

13-
public string? UnityCNKey;
16+
/// <summary>
17+
/// Key for UnityCN encryption
18+
/// </summary>
19+
public string? UnityCNKey { get; set; }
1420

15-
public UnityCN? UnityCNInfo;
21+
/// <summary>
22+
/// Data for UnityCN encryption
23+
/// </summary>
24+
public UnityCN? UnityCNInfo { get; set; }
1625

17-
public BlocksAndDirectoryInfo DataInfo;
26+
/// <summary>
27+
/// BlocksAndDirectoryInfo
28+
/// </summary>
29+
public BlocksAndDirectoryInfo DataInfo { get; set; }
1830

19-
public List<MemoryStream> cabStreams;
31+
public List<MemoryStream> cabStreams { get; set; }
2032

21-
public uint crc32;
33+
public uint crc32 { get; set; }
34+
35+
private bool _blocksInfoAtTheEnd;
2236

23-
public bool BlocksInfoAtTheEnd = false;
37+
public bool BlocksInfoAtTheEnd { get => _blocksInfoAtTheEnd; set => _blocksInfoAtTheEnd = value; }
2438

25-
public bool HasBlockInfoNeedPaddingAtStart;
39+
public bool HasBlockInfoNeedPaddingAtStart { get; set; }
2640

27-
private bool HeaderAligned = false;
28-
29-
private ArchiveFlags mask;
41+
private bool HeaderAligned { get; set; }
3042

43+
private ArchiveFlags mask { get; set; }
44+
45+
public BundleFile(string path, bool original = false, string? key = null)
46+
: this(new FileStream(path, FileMode.Open, FileAccess.Read), original, key)
47+
{
48+
}
49+
3150
public BundleFile(Stream input, bool original = false, string? key = null)
3251
{
3352
using AssetReader reader = new AssetReader(input);
@@ -37,26 +56,6 @@ public BundleFile(Stream input, bool original = false, string? key = null)
3756
Header = new Header(reader);
3857
var version = ParseVersion();
3958

40-
if (Header.version >= 7)
41-
{
42-
reader.AlignStream(16);
43-
HeaderAligned = true;
44-
}
45-
else if (version[0] == 2019 && version[1] == 4) // temp fix for 2019.4.x
46-
{
47-
var p = reader.Position;
48-
var len = 16 - p % 16;
49-
var bytes = reader.ReadBytes((int)len);
50-
if (bytes.Any(x => x != 0))
51-
{
52-
reader.Position = p;
53-
}
54-
else
55-
{
56-
HeaderAligned = true;
57-
}
58-
}
59-
6059
if (version[0] < 2020 || //2020 and earlier
6160
(version[0] == 2020 && version[1] == 3 && version[2] <= 34) || //2020.3.34 and earlier
6261
(version[0] == 2021 && version[1] == 3 && version[2] <= 2) || //2021.3.2 and earlier
@@ -81,7 +80,27 @@ public BundleFile(Stream input, bool original = false, string? key = null)
8180
Header.flags &= (ArchiveFlags)~mask;
8281
}
8382

84-
DataInfo = new BlocksAndDirectoryInfo(reader, Header, ref BlocksInfoAtTheEnd);
83+
if (Header.version >= 7)
84+
{
85+
reader.AlignStream(16);
86+
HeaderAligned = true;
87+
}
88+
else if (version[0] == 2019 && version[1] == 4) // temp fix for 2019.4.x
89+
{
90+
var p = reader.Position;
91+
var len = 16 - p % 16;
92+
var bytes = reader.ReadBytes((int)len);
93+
if (bytes.Any(x => x != 0))
94+
{
95+
reader.Position = p;
96+
}
97+
else
98+
{
99+
HeaderAligned = true;
100+
}
101+
}
102+
103+
DataInfo = new BlocksAndDirectoryInfo(reader, Header, ref _blocksInfoAtTheEnd);
85104

86105
foreach (var blockInfo in DataInfo.BlocksInfo)
87106
{
@@ -179,7 +198,7 @@ private string BlocksCompressionType
179198

180199
foreach (var block in DataInfo.BlocksInfo)
181200
{
182-
block.flags &= (StorageBlockFlags)~StorageBlockFlags.CompressionTypeMask;
201+
block.flags &= ~StorageBlockFlags.CompressionTypeMask;
183202
block.flags |= (StorageBlockFlags)newFlag;
184203
}
185204
}
@@ -234,12 +253,16 @@ private void ReadBlocks(AssetReader reader)
234253
}
235254
}
236255

256+
public void WriteToFile(string path, string infoPacker = "none", string dataPacker = "none", bool unityCN = false)
257+
{
258+
using FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
259+
Write(fs, infoPacker, dataPacker, unityCN);
260+
}
261+
237262
public void Write(Stream output, string infoPacker = "none", string dataPacker = "none", bool unityCN = false)
238263
{
239264
MemoryStream compressedStream = new MemoryStream();
240265

241-
//Bumbo();
242-
243266
fixCRC(crc32, CalculateCRC32());
244267

245268
BlocksCompressionType = dataPacker;
@@ -248,8 +271,15 @@ public void Write(Stream output, string infoPacker = "none", string dataPacker =
248271
{
249272
throw new Exception("UnityCN encryption requires lz4 or lz4hc compression type");
250273
}
251-
252-
//DataInfo.merge();
274+
275+
if (dataPacker == "none")
276+
{
277+
DataInfo.merge();
278+
}
279+
else
280+
{
281+
DataInfo.Split();
282+
}
253283

254284
MemoryStream BlocksStream = new MemoryStream();
255285

UnityAsset.NET/CRC32.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public static uint CalculateCRC32(byte[] data, uint initialCrc = 0)
7474
return ~crc;
7575
}
7676

77+
public static uint CalculateCRC32(List<byte> data, uint initialCrc = 0)
78+
{
79+
uint crc = ~initialCrc;
80+
foreach (var b in data)
81+
{
82+
crc = (crc >> 8) ^ Crc32Table[(crc ^ b) & 0xFF];
83+
}
84+
return ~crc;
85+
}
86+
7787
public static uint CalculateCRC32(Stream data, uint initialCrc = 0)
7888
{
7989
data.Position = 0;

0 commit comments

Comments
 (0)