55using System . IO ;
66using System . Linq ;
77using System . Reactive . Subjects ;
8- using System . Reflection ;
98using System . Runtime ;
109using System . Security ;
1110using System . Threading ;
1211using System . Threading . Tasks ;
1312using System . Windows . Forms ;
14- using Ionic . Zip ;
15- using Ionic . Zlib ;
1613using KKManager . Functions ;
17- using SharpCompress . Common ;
14+ using SharpCompress . Archives . Zip ;
1815using Sideloader ;
1916
2017namespace KKManager . Data . Zipmods
@@ -75,7 +72,7 @@ void ReadSideloaderModsAsync()
7572 }
7673
7774 var files = Directory . EnumerateFiles ( modDirectory , "*.*" , searchOption ) ;
78- Parallel . ForEach ( files , new ParallelOptions { MaxDegreeOfParallelism = 4 , CancellationToken = cancellationToken } , file =>
75+ Parallel . ForEach ( files , new ParallelOptions { MaxDegreeOfParallelism = 4 , CancellationToken = cancellationToken } , file =>
7976 {
8077 try
8178 {
@@ -139,11 +136,10 @@ public static SideloaderModInfo LoadFromFile(string filename)
139136 if ( ! IsValidZipmodExtension ( location . Extension ) )
140137 throw new ArgumentException ( $ "The file { filename } has an invalid extension and can't be a zipmod", nameof ( filename ) ) ;
141138
142- using ( var zf = new ZipFile ( ) )
139+ using ( var zf = ZipArchive . Open ( location ) )
143140 {
144141 // Without this reading crashes if any entry name has invalid characters
145- zf . IgnoreDuplicateFiles = true ;
146- zf . Initialize ( location . FullName ) ;
142+ // TODO not available in sharplib - zf.IgnoreDuplicateFiles = true;
147143
148144 var manifest = Manifest . LoadFromZip ( zf ) ;
149145
@@ -156,69 +152,45 @@ public static SideloaderModInfo LoadFromFile(string filename)
156152 {
157153 try
158154 {
159- return x . FileName . EndsWith ( ".jpg" , StringComparison . OrdinalIgnoreCase ) ||
160- x . FileName . EndsWith ( ".png" , StringComparison . OrdinalIgnoreCase ) ;
155+ return x . Key != null && ( x . Key . EndsWith ( ".jpg" , StringComparison . OrdinalIgnoreCase ) ||
156+ x . Key . EndsWith ( ".png" , StringComparison . OrdinalIgnoreCase ) ) ;
161157 }
162158 catch ( Exception e )
163159 {
164160 // Handle entries with invalid characters in filename
165- Console . WriteLine ( $ "WARN: Zipmod={ location . Name } Entry={ x . FileName } Error={ e . Message } ") ;
161+ Console . WriteLine ( $ "WARN: Zipmod={ location . Name } Entry={ x . Key } Error={ e . Message } ") ;
166162 return false ;
167163 }
168164 } )
169- . OrderBy ( x => x . FileName ) . Take ( 5 ) )
165+ . OrderBy ( x => x . Key ) . Take ( 5 ) )
170166 {
171- var imgName = imageFile . FileName ;
167+ var imgName = imageFile . Key ;
172168
173- if ( imageFile . CompressionLevel == CompressionLevel . None )
169+ images . Add ( ( ) =>
174170 {
175- var prop = typeof ( ZipEntry ) . GetProperty ( "FileDataPosition" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
176- if ( prop == null ) throw new ArgumentNullException ( nameof ( prop ) ) ;
177- var pos = ( long ) prop . GetValue ( imageFile , null ) ;
178- images . Add ( ( ) =>
171+ try
179172 {
180- try
173+ using ( var zf2 = ZipArchive . Open ( location ) )
181174 {
182- using ( var archiveStream = new FileStream ( location . FullName , FileMode . Open , FileAccess . Read , FileShare . Read ) )
175+ var if2 = zf2 . Entries . First ( x => x . Key == imgName ) ;
176+ using ( var archiveStream = if2 . OpenEntryStream ( ) )
183177 {
184- archiveStream . Position = pos ;
185178 using ( var img = Image . FromStream ( archiveStream ) )
186179 {
187180 return img . GetThumbnailImage ( 200 , 200 , null , IntPtr . Zero ) ;
188181 }
189182 }
190183 }
191- catch ( SystemException ex )
192- {
193- Console . WriteLine ( $ "Failed to load image \" { imgName } \" from mod archive \" { location . Name } \" with error: { ex . Message } ") ;
194- return null ;
195- }
196- } ) ;
197- }
198- else
199- {
200- images . Add ( ( ) =>
184+ }
185+ catch ( SystemException ex )
201186 {
202- try
203- {
204- using ( var archiveStream = new FileStream ( location . FullName , FileMode . Open , FileAccess . Read , FileShare . Read ) )
205- using ( var archive = ZipFile . Read ( archiveStream ) )
206- using ( var imgStream = archive . Entries . First ( x => x . FileName == imgName ) . OpenReader ( ) )
207- using ( var img = Image . FromStream ( imgStream ) )
208- {
209- return img . GetThumbnailImage ( 200 , 200 , null , IntPtr . Zero ) ;
210- }
211- }
212- catch ( SystemException ex )
213- {
214- Console . WriteLine ( $ "Failed to load image \" { imgName } \" from mod archive \" { location . Name } \" with error: { ex . Message } ") ;
215- return null ;
216- }
217- } ) ;
218- }
187+ Console . WriteLine ( $ "Failed to load image \" { imgName } \" from mod archive \" { location . Name } \" with error: { ex . Message } ") ;
188+ return null ;
189+ }
190+ } ) ;
219191 }
220192
221- var contents = zf . Entries . Where ( x => ! x . IsDirectory ) . Select ( x => x . FileName . Replace ( '/' , '\\ ' ) ) . ToList ( ) ;
193+ var contents = zf . Entries . Where ( x => ! x . IsDirectory ) . Select ( x => x . Key ? . Replace ( '/' , '\\ ' ) ) . ToList ( ) ;
222194
223195 return new SideloaderModInfo ( location , manifest , images , contents ) ;
224196 }
0 commit comments