@@ -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
0 commit comments