1- using System ;
1+ using ModApi . Common ;
2+ using ModAPI . Common ;
3+ using ModAPI . Common . Dialog ;
4+ using ModAPI . Common . Types ;
5+ using ModAPI . Common . Update ;
6+ using System ;
27using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Windows . Forms ;
5-
6- using System . Threading ;
8+ using System . ComponentModel ;
9+ using System . Diagnostics ;
710using System . IO ;
811using System . IO . Compression ;
12+ using System . Linq ;
913using System . Net ;
10- using System . ComponentModel ;
11-
12- using System . Diagnostics ;
13-
14- using ModAPI . Common ;
14+ using System . Threading ;
15+ using System . Windows . Forms ;
1516using System . Windows . Interop ;
1617using System . Xml ;
17- using ModAPI . Common . Update ;
18- using ModAPI . Common . Types ;
1918
2019namespace Spore_ModAPI_Easy_Installer
2120{
@@ -53,7 +52,6 @@ enum FileType
5352 //
5453
5554 public static InstalledMods ModList = new InstalledMods ( ) ;
56- static ProgressWindow ProgressDialog = new ProgressWindow ( Strings . InstallingModTitle ) ;
5755 public static string outcome = string . Empty ;
5856
5957 /// <summary>
@@ -302,22 +300,6 @@ static string GetSporeDataPath()
302300 return path ;
303301 }
304302
305- static void TryCloseProgressDialog ( )
306- {
307- while ( ! ProgressDialog . IsDisposed )
308- {
309- try
310- {
311- ProgressDialog . Close ( ) ;
312- }
313- catch
314- {
315- Thread . Sleep ( 250 ) ;
316- }
317- }
318- }
319-
320-
321303 static ResultType InstallPackage ( string inputFile , string modName )
322304 {
323305 ResultType result = ResultType . Success ;
@@ -331,53 +313,39 @@ static ResultType InstallPackage(string inputFile, string modName)
331313
332314 try
333315 {
334- // File.Copy(inputFile, Path.Combine(new string[] { outputPath, Path.GetFileName(inputFile) }), true);
335-
336- ProgressDialog . SetDescriptionText ( Strings . ModIsInstalling1 + modName + Strings . ModIsInstalling2 ) ;
337-
338316 string fileName = Path . GetFileName ( inputFile ) ;
317+ string outputFile = Path . Combine ( outputPath , fileName ) ;
339318
340- var client = new WebClient ( ) ;
341-
342- bool isFinished = false ;
343-
344- client . DownloadFileCompleted += ( object sender , AsyncCompletedEventArgs args ) =>
319+ Thread thread = new Thread ( ( ) =>
320+ {
321+ var dialog = new ProgressDialog ( Strings . CopyingFile + " " + fileName , Strings . InstallingModTitle , ( s , e ) =>
345322 {
346- isFinished = true ;
347- // if the transfer is too fast, it tries to close the dialog before it even loaded. So keep trying until we have it
348- TryCloseProgressDialog ( ) ;
349-
350- if ( args . Error != null )
323+ try
351324 {
352- if ( args . Error . GetType ( ) == typeof ( WebException ) )
325+ using ( FileStream inputFileStream = File . Open ( inputFile , FileMode . Open ) )
326+ using ( FileStream outputFileStream = File . Open ( outputFile , FileMode . Create ) )
353327 {
354- result = ResultType . UnauthorizedAccess ;
355- ex = ( ( WebException ) args . Error ) . InnerException ;
356- }
357- else
358- {
359- ex = args . Error ;
328+ StreamUtils . CopyStreamWithProgress ( inputFileStream , outputFileStream , null , ( _ , progress ) =>
329+ {
330+ ( s as BackgroundWorker ) . ReportProgress ( progress ) ;
331+ } ) ;
332+
333+ outputFileStream . Flush ( ) ;
360334 }
361335 }
362- } ;
363-
364- client . DownloadProgressChanged += ( object sender , DownloadProgressChangedEventArgs args ) =>
365- {
366- if ( ! isFinished )
367- {
368- try
336+ catch ( Exception copyException )
369337 {
370- ProgressDialog . SetProgress ( args . ProgressPercentage ) ;
371- ProgressDialog . SetProgressText ( Strings . CopyingFile + " \" " + fileName + " \" (" + ( args . BytesReceived / 1000 ) + " / " + ( args . TotalBytesToReceive / 1000 ) + " KBs)" ) ;
338+ ex = copyException ;
339+ result = ResultType . ModNotInstalled ;
372340 }
373- catch { }
374- }
375- } ;
341+ } ) ;
376342
343+ dialog . ShowDialog ( ) ;
344+ } ) ;
345+ thread . SetApartmentState ( ApartmentState . STA ) ;
346+ thread . Start ( ) ;
347+ thread . Join ( ) ;
377348
378- ShowProgressDialog ( client , inputFile , Path . Combine ( new string [ ] { outputPath , Path . GetFileName ( inputFile ) } ) ) ;
379-
380-
381349 if ( ex != null )
382350 {
383351 throw ex ;
@@ -407,7 +375,7 @@ static SporePath.Game GetGameFromFileType(FileType type)
407375 }
408376
409377 // Installs the files and adds them to the list in the ModConfiguration (so they can be removed if something goes wrong)
410- private static ResultType ExtractSporemodZip ( string inputFile , ModConfiguration mod )
378+ private static ResultType ExtractSporemodZip ( string inputFile , ModConfiguration mod , StreamUtils . StreamProgressEventHandler eventHandler )
411379 {
412380 //TODO check if it contains an installer
413381 string modName = Path . GetFileNameWithoutExtension ( inputFile ) ;
@@ -422,11 +390,8 @@ private static ResultType ExtractSporemodZip(string inputFile, ModConfiguration
422390 if ( ! Directory . Exists ( modPath ) )
423391 Directory . CreateDirectory ( modPath ) ;
424392
425-
426393 foreach ( var entry in archive . Entries )
427394 {
428- ProgressDialog . SetProgressText ( Strings . ExtractingFile + " \" " + entry . Name + "\" (" + entriesExtracted + " / " + numEntries + " files)." ) ;
429-
430395 // we use the FullName because we also might check the folder that contains that file
431396 var type = GetFileType ( entry . FullName ) ;
432397 string outputPath = GetOutputPath ( type ) ;
@@ -440,11 +405,11 @@ private static ResultType ExtractSporemodZip(string inputFile, ModConfiguration
440405 {
441406 string fileOutPath = Path . Combine ( outputPath , entry . Name ) ;
442407
443- File . Copy ( configOutPath , fileOutPath ) ;
408+ File . Copy ( configOutPath , fileOutPath , true ) ;
444409 }
445410 }
446- ProgressDialog . SetProgress ( ( int ) ( ( entriesExtracted / ( float ) numEntries ) * 100.0f ) ) ;
447411
412+ eventHandler ? . Invoke ( null , ( int ) ( ( entriesExtracted / ( float ) numEntries ) * 100.0f ) ) ;
448413 entriesExtracted ++ ;
449414 }
450415 }
@@ -565,49 +530,60 @@ static ResultType InstallSporemod(string inputFile, string modName)
565530 if ( result == ResultType . NoInstallerFound )
566531 {
567532 // the custom installer just didn't exist, extract as ZIP file
568-
569533 var mod = ModList . AddMod ( modName ) ;
534+ Exception exception = null ;
570535
571- ShowProgressDialog ( ) ;
572- // ProgressDialog.Show();
573- ProgressDialog . SetDescriptionText ( Strings . ModIsInstalling1 + modName + Strings . ModIsInstalling2 ) ;
574-
575- try
536+ Thread thread = new Thread ( ( ) =>
576537 {
577- ExtractSporemodZip ( inputFile , mod ) ;
578- //ModList.Save();
538+ var dialog = new ProgressDialog ( Strings . ModIsInstalling1 + modName + "\" is being installed" , Strings . InstallingModTitle , ( s , e ) =>
539+ {
540+ try
541+ {
542+ ExtractSporemodZip ( inputFile , mod , ( _ , progress ) =>
543+ {
544+ ( s as BackgroundWorker ) . ReportProgress ( progress ) ;
545+ } ) ;
579546
580- TryCloseProgressDialog ( ) ;
581- return ResultType . Success ;
582- }
583- catch ( UnauthorizedAccessException )
584- {
585- // remove all the files we added (so the mod is not only partially installed)
586- RemoveModFiles ( mod ) ;
587- ModList . RemoveMod ( mod ) ;
547+ result = ResultType . Success ;
548+ }
549+ catch ( UnauthorizedAccessException )
550+ {
551+ // remove all the files we added (so the mod is not only partially installed)
552+ RemoveModFiles ( mod ) ;
553+ ModList . RemoveMod ( mod ) ;
588554
589- TryCloseProgressDialog ( ) ;
590- return ResultType . UnauthorizedAccess ;
591- }
592- catch ( IOException )
593- {
594- // remove all the files we added (so the mod is not only partially installed)
595- RemoveModFiles ( mod ) ;
596- ModList . RemoveMod ( mod ) ;
555+ result = ResultType . UnauthorizedAccess ;
556+ }
557+ catch ( IOException )
558+ {
559+ // remove all the files we added (so the mod is not only partially installed)
560+ RemoveModFiles ( mod ) ;
561+ ModList . RemoveMod ( mod ) ;
562+ result = ResultType . InvalidPath ;
563+ }
564+ catch ( Exception ex )
565+ {
566+ // remove all the files we added (so the mod is not only partially installed)
567+ RemoveModFiles ( mod ) ;
568+ ModList . RemoveMod ( mod ) ;
597569
598- TryCloseProgressDialog ( ) ;
599- return ResultType . InvalidPath ;
600- }
601- catch ( Exception ex )
602- {
603- // remove all the files we added (so the mod is not only partially installed)
604- RemoveModFiles ( mod ) ;
605- ModList . RemoveMod ( mod ) ;
570+ // just propagate the exception
571+ exception = ex ;
572+ }
573+ } ) ;
606574
607- TryCloseProgressDialog ( ) ;
608- // just propagate the exception
609- throw ex ;
575+ dialog . ShowDialog ( ) ;
576+ } ) ;
577+ thread . SetApartmentState ( ApartmentState . STA ) ;
578+ thread . Start ( ) ;
579+ thread . Join ( ) ;
580+
581+ if ( exception != null )
582+ {
583+ throw exception ;
610584 }
585+
586+ return result ;
611587 }
612588 else
613589 {
@@ -653,51 +629,7 @@ static string GetErrorMessage(ResultType errorType)
653629 }
654630
655631
656- // Progress dialog
657-
658- static void ShowProgressDialog ( WebClient client , string input , string output )
659- {
660- Exception exception = null ;
661-
662- Thread thread = new Thread ( ( ) =>
663- {
664- try
665- {
666- client . DownloadFileAsync ( new Uri ( input ) , output ) ;
667-
668- if ( ! ProgressDialog . IsDisposed )
669- {
670- ProgressDialog . ShowDialog ( ) ;
671- }
672-
673- }
674- catch ( Exception ex )
675- {
676- exception = ex ;
677- }
678- } ) ;
679- thread . SetApartmentState ( ApartmentState . STA ) ;
680- thread . Start ( ) ;
681- thread . Join ( ) ;
682-
683- if ( exception != null )
684- {
685- throw exception ;
686- }
687- }
688-
689632 // this one does not block the thread
690- static void ShowProgressDialog ( )
691- {
692- if ( ProgressDialog . IsDisposed )
693- ProgressDialog = new ProgressWindow ( Strings . InstallingModTitle ) ;
694- Thread thread = new Thread ( ( ) =>
695- {
696- ProgressDialog . ShowDialog ( ) ;
697- } ) ;
698- thread . SetApartmentState ( ApartmentState . STA ) ;
699- thread . Start ( ) ;
700- }
701633 static string GetResultText ( ResultType result , string modName , string errorString )
702634 {
703635 if ( result == ResultType . Success )
0 commit comments