Пример #1
0
 public static BackupResultHelper ToggleAutoBackup(List<Game> gamesToBackup, bool backupEnabled, BackupSyncOptions backupSyncOptions, int intervalMinute, int intervalHour, DirectoryInfo autobackupDi) {
     if (backupEnabled) return ShutdownAutobackup();
     GamesToBackup = gamesToBackup;
     _backupEnabled = false;
     _backupSyncOptions = backupSyncOptions;
     _autoBackupDirectoryInfo = autobackupDi;
     _progress = new ProgressHelper(){ FilesComplete = 0, TotalFiles = 0};
     return InitializeAutobackup(intervalMinute, intervalHour);
 }
Пример #2
0
        public static BackupResultHelper BackupSaves(List<Game> gamesList, DirectoryInfo targetDi)
        {
            _progress = new ProgressHelper(){FilesComplete=0, TotalFiles = 0};
            Debug.WriteLine(@"Starting BackupSaves");

            //Check for and create target directory
            if (!Directory.Exists(targetDi.FullName) && !string.IsNullOrWhiteSpace(targetDi.FullName))
                Directory.CreateDirectory(targetDi.FullName);

            //Get file count for progress bar
            Debug.WriteLine(@"Getting file count");
            var totalFiles = 0;
            foreach (var game in gamesList) {
                var files = Directory.GetFiles(game.Path, "*", SearchOption.AllDirectories);
                if (files.Any())
                    totalFiles += files.Count();
                else {
                    ErrorResultHelper.Message = @"No files found for " + game.Name;
                    return ErrorResultHelper;
                }
            }
            Debug.WriteLine(@"Found {0} files to copy", totalFiles);
            _progress.TotalFiles = totalFiles;

            //Copy files for each game to folder.
            foreach (var game in gamesList) {
                BackupGame(game, targetDi.FullName);
            }

            Debug.WriteLine(@"Backup saves complete");
            var time = DateTime.Now.ToLongTimeString();
            return new BackupResultHelper(){
                AutobackupEnabled = false,
                Message = @"Backup complete",
                Success = true,
                BackupDateTime = time,
                BackupButtonText = "Backup to folder"};
        }
Пример #3
0
        public static async Task<BackupResultHelper> BackupAndZip(List<Game> gamesList, FileInfo targetFi) {
            //Initialize progress helper
            _progress = new ProgressHelper(){FilesComplete = 0, TotalFiles = 0};
            
            //Delete existing file if it exists
            if (targetFi.Exists)
                targetFi.Delete();

            //Establish total file count for all games. If no files found for a game, return an error.
            var totalFiles = 0;
            foreach (var game in gamesList) {
                var files = Directory.GetFiles(game.Path, "*", SearchOption.AllDirectories);
                if (files.Any())
                    totalFiles += files.Count();
                else {
                    ErrorResultHelper.Message = @"No files found for " + game.Name;
                    return ErrorResultHelper;
                }
            }
            _progress.TotalFiles = totalFiles;


            try {
                //Create dictionary of source FileInfo's and target truncated paths
                var zipFileDictionary = new Dictionary<FileInfo, string>();
                foreach (var game in gamesList) {
                    var gameFiles = new DirectoryInfo(game.Path).GetFiles("*", SearchOption.AllDirectories);
                    foreach (var file in gameFiles) {
                        var index = file.FullName.IndexOf(game.RootFolder, StringComparison.Ordinal);
                        var substring = file.FullName.Substring(index);
                        zipFileDictionary.Add(file, substring);
                    }
                }

                //Zip files
                using (var destination = ZipFile.Open(targetFi.FullName, ZipArchiveMode.Create)) {
                    foreach (var pair in zipFileDictionary) {
                        await
                            Task.Run(
                                () =>
                                    destination.CreateEntryFromFile(pair.Key.FullName, pair.Value,
                                        CompressionLevel.Optimal));
                        _progress.FilesComplete++;
                        Messenger.Default.Send(_progress);
                    }
                }
            }
            catch (ArgumentException ex) {
                SBTErrorLogger.Log(ex.Message);
            } catch (FileNotFoundException ex) {
                SBTErrorLogger.Log(ex.Message);
            } catch (UnauthorizedAccessException ex) {
                SBTErrorLogger.Log(ex.Message);
            } catch (IOException ex) {
                SBTErrorLogger.Log(ex.Message);
            } catch (Exception ex) {
                SBTErrorLogger.Log(ex.Message);
            }
            var time = DateTime.Now.ToLongTimeString();
            return new BackupResultHelper(){Success = true, Message = "Zip backup complete!", BackupDateTime = time};
        }