@@ -8,26 +8,45 @@ namespace UnityAsset.NET.BundleFile;
88
99public 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
0 commit comments