public void ImportSettings(BuildType buildType) { using (new WurmAssistantGateway(AppContext.WaGatewayErrorMessage)) { using (var tempDirMan = new TempDirManager()) { var tempDir = tempDirMan.GetHandle(); var dataDir = new DirectoryInfo(DataDirPath); var tempBackupDir = new DirectoryInfo(Path.Combine(tempDir.FullName, dataDir.Name)); DirectoryEx.DirectoryCopyRecursive(dataDir.FullName, tempBackupDir.FullName); try { dataDir.Delete(true); var otherBuildDirPath = AppContext.GetDataDir(buildType); DirectoryEx.DirectoryCopyRecursive(otherBuildDirPath, dataDir.FullName); } catch (Exception) { TransientHelper.Compensate(() => dataDir.Delete(true), retryDelay: TimeSpan.FromSeconds(5)); TransientHelper.Compensate(() => DirectoryEx.DirectoryCopyRecursive(tempBackupDir.FullName, dataDir.FullName), retryDelay: TimeSpan.FromSeconds(5)); throw; } } } }
public static WaBackup Create(string backupName, string buildType, string sourceDirPath, string targetDirPath, BackupsManager manager, WaBackupsViewModel baseViewModel) { ValidateName(backupName); using (new WurmAssistantGateway(AppContext.WaGatewayErrorMessage)) { using (var tempDirMan = new TempDirManager()) { var tempDir = tempDirMan.GetHandle(); string tempArchiveFilePath = Path.Combine( tempDir.FullName, string.Format("{0}_{1}_{2}.7z", backupName ?? "WurmAssistantBackup", buildType, DateTime.Now.ToStringFormattedForFileEx(highPrecision: true))); var compressor = new SevenZipCompressor() { ArchiveFormat = OutArchiveFormat.SevenZip, CompressionMode = CompressionMode.Create }; compressor.CompressDirectory(sourceDirPath, tempArchiveFilePath); var newBackup = new WaBackup(tempArchiveFilePath, buildType, manager, baseViewModel); var targetFile = new FileInfo(newBackup.FilePath); var targetArchiveFile = new FileInfo(Path.Combine(targetDirPath, targetFile.Name)); if (targetArchiveFile.Exists) { throw new LauncherException("backup with this file name already exists"); } newBackup.MoveInto(targetDirPath); return newBackup; } } }
public void Restore() { using (new WurmAssistantGateway(AppContext.WaGatewayErrorMessage)) { using (var tempDirMan = new TempDirManager()) { var tempDir = tempDirMan.GetHandle(); var dataDir = new DirectoryInfo(manager.DataDirPath); var tempBackupDir = new DirectoryInfo(Path.Combine(tempDir.FullName, dataDir.Name)); DirectoryEx.DirectoryCopyRecursive(dataDir.FullName, tempBackupDir.FullName); try { dataDir.Delete(true); ValidateNotDeleted(); using (var extractor = new SevenZipExtractor(FilePath)) { extractor.ExtractArchive(dataDir.FullName); } } catch (Exception) { TransientHelper.Compensate(() => dataDir.Delete(true), retryDelay: TimeSpan.FromSeconds(5)); TransientHelper.Compensate(() => DirectoryEx.DirectoryCopyRecursive(tempBackupDir.FullName, dataDir.FullName), retryDelay: TimeSpan.FromSeconds(5)); throw; } } } }