@@ -74,13 +74,8 @@ private async Task<OperationResult> DownloadAndExtractRetroRewind(ProgressWindow
7474 //where all distributions are stored
7575 var destinationParentDir = _fileSystem . DirectoryInfo . New ( PathManager . RiivolutionWhWzFolderPath ) ;
7676
77- //where the RR distribution lives
78- var distributionDataDestination = _fileSystem . Path . Combine ( destinationParentDir . FullName , FolderName ) ;
79- //where the RR wiiDisc xml file lives
80- var riivolutionFolderDestination = PathManager . RiivolutionXmlFolderPath ;
81- var riivolutionDiscXMLFile = _fileSystem . Path . Combine ( riivolutionFolderDestination , $ "{ XMLFileName } .xml") ;
82-
8377 Exception ? exception = null ;
78+ OperationResult ? result = null ;
8479 try
8580 {
8681 // 1) Download
@@ -110,34 +105,30 @@ private async Task<OperationResult> DownloadAndExtractRetroRewind(ProgressWindow
110105 return new DirectoryNotFoundException ( $ "Could not find a '{ FolderName } ' folder inside { tempExtractionPath } ") ;
111106 }
112107
113- // 4) Replace existing install, if any
114- if ( _fileSystem . Directory . Exists ( distributionDataDestination ) )
115- _fileSystem . Directory . Delete ( distributionDataDestination , recursive : true ) ;
116- if ( _fileSystem . File . Exists ( riivolutionDiscXMLFile ) )
117- _fileSystem . File . Delete ( riivolutionDiscXMLFile ) ;
118-
119- // 5) Make sure the target directory exists
120- var parentDirectory = _fileSystem . DirectoryInfo . New ( distributionDataDestination ) . Parent ;
121- parentDirectory ? . Create ( ) ;
122- if ( ( ! parentDirectory ? . Exists ) ?? true )
123- throw new DirectoryNotFoundException ( $ "Could not find destination `{ parentDirectory ? . FullName } `") ;
124-
125- // 5) Move over distribution data
126- _fileSystem . Directory . Move ( sourceFolder , distributionDataDestination ) ;
108+ // 4) Remove existing install, if any
109+ var removeResult = await RemoveAsync ( progressWindow ) ;
110+ if ( removeResult . IsFailure )
111+ {
112+ result = removeResult ;
113+ throw new Exception ( removeResult . Error . Message ) ;
114+ }
127115
128- // 6 ) Move over 'riivolution/' folder. skip existing files
116+ // 5 ) Move over RetroRewind
129117 var xmlFolderSource = _fileSystem . Path . Combine ( tempExtractionPath , XMLFolderName ) ;
130- foreach ( var file in _fileSystem . Directory . EnumerateFiles ( xmlFolderSource , "*" , SearchOption . AllDirectories ) )
118+ var riivolutionFiles = _fileSystem . Directory . EnumerateFiles ( xmlFolderSource , "*" , SearchOption . AllDirectories ) ;
119+ var folderSource = _fileSystem . Path . Combine ( tempExtractionPath , FolderName ) ;
120+ var retroRewindFiles = _fileSystem . Directory . EnumerateFiles ( folderSource , "*" , SearchOption . AllDirectories ) ;
121+ foreach ( var file in riivolutionFiles . Concat ( retroRewindFiles ) )
131122 {
132- var destinationPath = _fileSystem . Path . Combine ( riivolutionFolderDestination , _fileSystem . Path . GetRelativePath ( xmlFolderSource , file ) ) ;
123+ var destinationPath = _fileSystem . Path . Combine ( destinationParentDir . FullName , _fileSystem . Path . GetRelativePath ( tempExtractionPath , file ) ) ;
133124 var destinationDirectoryName = _fileSystem . Path . GetDirectoryName ( destinationPath ) ;
134125 if ( destinationDirectoryName != null )
135126 {
136127 var directory = _fileSystem . DirectoryInfo . New ( destinationDirectoryName ) ;
137128 if ( ! directory ? . Exists ?? false )
138129 directory ? . Create ( ) ;
139130 }
140- _fileSystem . File . Move ( file , destinationPath , false ) ;
131+ _fileSystem . File . Move ( file , destinationPath , false ) ; //skip existing files for safety
141132 }
142133 }
143134 catch ( Exception e )
@@ -152,7 +143,7 @@ private async Task<OperationResult> DownloadAndExtractRetroRewind(ProgressWindow
152143 if ( _fileSystem . Directory . Exists ( tempExtractionPath ) )
153144 _fileSystem . Directory . Delete ( tempExtractionPath , recursive : true ) ;
154145 }
155- return exception is null ? Ok ( ) : Fail ( exception ) ;
146+ return result ?? ( exception is not null ? Fail ( exception ) : Ok ( ) ) ;
156147 }
157148
158149 private async Task BackupOldrksys ( )
@@ -542,9 +533,16 @@ List<DeletionData> allDeletions
542533
543534 public Task < OperationResult > RemoveAsync ( ProgressWindow progressWindow )
544535 {
545- var retroRewindPath = _fileSystem . Path . Combine ( PathManager . RiivolutionWhWzFolderPath , FolderName ) ;
546- if ( _fileSystem . Directory . Exists ( retroRewindPath ) )
547- _fileSystem . Directory . Delete ( retroRewindPath , true ) ;
536+ //where the RR distribution lives
537+ var distributionDataDestination = _fileSystem . Path . Combine ( PathManager . RiivolutionWhWzFolderPath , FolderName ) ;
538+ //where the RR wiiDisc xml file lives
539+ var riivolutionDiscXMLFile = _fileSystem . Path . Combine ( PathManager . RiivolutionWhWzFolderPath , XMLFolderName , $ "{ XMLFileName } .xml") ;
540+
541+ if ( _fileSystem . Directory . Exists ( distributionDataDestination ) )
542+ _fileSystem . Directory . Delete ( distributionDataDestination , recursive : true ) ;
543+ if ( _fileSystem . File . Exists ( riivolutionDiscXMLFile ) )
544+ _fileSystem . File . Delete ( riivolutionDiscXMLFile ) ;
545+
548546 return Task . FromResult ( Ok ( ) ) ;
549547 }
550548
0 commit comments