@@ -134,22 +134,25 @@ public static async Task<bool> UploadGameAsync(Repo repo, Game game, IProgress<S
134134 {
135135 try
136136 {
137+ // Do preparation work in a temporary directory
137138 string tmpPath = Path . Combine ( FileHelper . tmpPath , game . DirectoryName ) ;
138139 if ( Directory . Exists ( tmpPath ) )
139140 Directory . Delete ( tmpPath , true ) ;
140141 if ( ! await WebDAVClient . DeleteRemoteAsync ( $ "{ game . DirectoryName } /game") )
141142 Logger . Warn ( "Failed to delete remote game folder before upload. It may not exist." ) ;
142143 Directory . CreateDirectory ( tmpPath ) ;
144+ // Compress game folder into split zip files
143145 await CompressHelper . CompressSplitZipFileAsync ( Path . Combine ( tmpPath , "game" ) , Path . Combine ( repo . LocalPath , game . DirectoryName ) , progress ) ;
144146 progress . Report ( new StagedProgressInfo { StagePercentage = 0 , StageName = "Preparing upload..." } ) ;
145147 string [ ] files = Directory . GetFiles ( tmpPath ) ;
146-
148+ // Upload each split zip file
147149 for ( int i = 0 ; i < files . Length ; i ++ )
148150 {
149151 if ( await WebDAVClient . UploadFileAsync ( files [ i ] , $ "{ game . DirectoryName } /game/{ Path . GetFileName ( files [ i ] ) } ") == false )
150152 throw new Exception ( $ "Error uploading " + files [ i ] ) ;
151153 progress . Report ( new StagedProgressInfo { StagePercentage = ( double ) ( i + 1 ) / files . Length * 100 , StageName = "Uploading files..." } ) ;
152154 }
155+ Directory . Delete ( tmpPath , true ) ;
153156 progress . Report ( new StagedProgressInfo { StagePercentage = 100 , StageName = "Upload complete" } ) ;
154157 return true ;
155158 }
@@ -159,6 +162,34 @@ public static async Task<bool> UploadGameAsync(Repo repo, Game game, IProgress<S
159162 return false ;
160163 }
161164 }
165+ public static async Task < bool > DownloadGameAsync ( Repo repo , Game game , IProgress < StagedProgressInfo > progress )
166+ {
167+ try
168+ {
169+ string tmpPath = Path . Combine ( FileHelper . tmpPath , game . DirectoryName ) ;
170+ if ( Directory . Exists ( tmpPath ) )
171+ Directory . Delete ( tmpPath , true ) ;
172+ Directory . CreateDirectory ( tmpPath ) ;
173+ List < string > remoteFiles = new ( ) ;
174+ await WebDAVClient . ListRemoteAsync ( $ "{ game . DirectoryName } /game", remoteFiles ) ;
175+ for ( int i = 0 ; i < remoteFiles . Count ; i ++ )
176+ {
177+ if ( await WebDAVClient . DownloadFileAsync ( remoteFiles [ i ] , Path . Combine ( tmpPath , Path . GetFileName ( remoteFiles [ i ] ) ) ) == false )
178+ throw new Exception ( $ "Error downloading " + remoteFiles [ i ] ) ;
179+ progress . Report ( new StagedProgressInfo { StagePercentage = ( double ) ( i + 1 ) / remoteFiles . Count * 100 , StageName = "Downloading files..." } ) ;
180+ }
181+ progress . Report ( new StagedProgressInfo { StagePercentage = 100 , StageName = "Decompressing files..." } ) ;
182+ await CompressHelper . DecompressSplitZipsAsync ( Path . Combine ( tmpPath , "game.7z.001" ) , repo . LocalPath , progress ) ;
183+ Directory . Delete ( tmpPath , true ) ;
184+ progress . Report ( new StagedProgressInfo { StagePercentage = 100 , StageName = "Download complete" } ) ;
185+ return true ;
186+ }
187+ catch ( Exception ex )
188+ {
189+ Logger . Error ( $ "Error when downloading and decompressing game: { ex . Message } ", ex ) ;
190+ return false ;
191+ }
192+ }
162193 public static async Task < Repo > GetRemoteGamesAsync ( )
163194 {
164195 Repo games = new ( ) { LocalPath = "WebDAV" } ;
0 commit comments