protected override PatchResult Execute() { if (comms.Cancel) { return(PatchResult.Failed); } if (comms.IsUnderMaintenance()) { return(PatchResult.Failed); } string rootPath = comms.SelfPatching ? comms.DecompressedFilesPath : comms.RootPath; string patchDecompressPath = comms.GetDecompressPathForPatch(PatchParameters.INSTALLER_PATCH_DIRECTORY); string patchDownloadPath = comms.CachePath + PatchParameters.INSTALLER_PATCH_FILENAME; InstallerPatch patchInfo = comms.VersionInfo.InstallerPatch; Stopwatch timer = Stopwatch.StartNew(); comms.Stage = PatchStage.CalculatingFilesToUpdate; comms.Log(Localization.Get(StringId.CalculatingNewOrChangedFiles)); List <VersionItem> filesToUpdate = comms.FindFilesToUpdate(); if (filesToUpdate.Count == 0) { return(PatchResult.AlreadyUpToDate); } if (comms.Cancel) { return(PatchResult.Failed); } InitializeProgress(filesToUpdate.Count, patchInfo.PatchSize); PatchUtils.DeleteDirectory(patchDecompressPath); Directory.CreateDirectory(patchDecompressPath); FileInfo patchFile = new FileInfo(patchDownloadPath); if (!patchFile.Exists || !patchFile.MatchesSignature(patchInfo.PatchSize, patchInfo.PatchMd5Hash)) { comms.Stage = PatchStage.DownloadingFiles; Stopwatch downloadTimer = Stopwatch.StartNew(); comms.Log(Localization.Get(StringId.DownloadingPatchX, PatchParameters.INSTALLER_PATCH_FILENAME)); patchFile = comms.DownloadManager.DownloadFileFromURLToPath(comms.VersionInfo.GetDownloadURLFor(patchInfo), patchDownloadPath, patchInfo.PatchSize); if (patchFile == null) { comms.FailReason = PatchFailReason.DownloadError; comms.FailDetails = Localization.Get(StringId.E_XCouldNotBeDownloaded, PatchParameters.INSTALLER_PATCH_FILENAME); return(PatchResult.Failed); } else if (!patchFile.MatchesSignature(patchInfo.PatchSize, patchInfo.PatchMd5Hash)) { comms.FailReason = PatchFailReason.CorruptDownloadError; comms.FailDetails = Localization.Get(StringId.E_DownloadedFileXIsCorrupt, PatchParameters.INSTALLER_PATCH_FILENAME); return(PatchResult.Failed); } else { comms.Log(Localization.Get(StringId.XDownloadedInYSeconds, PatchParameters.INSTALLER_PATCH_FILENAME, downloadTimer.ElapsedSeconds())); } } else { ReportProgress(0, patchInfo.PatchSize); } if (comms.Cancel) { return(PatchResult.Failed); } comms.Stage = PatchStage.ExtractingFilesFromArchive; comms.Log(Localization.Get(StringId.DecompressingPatchX, PatchParameters.INSTALLER_PATCH_FILENAME)); ZipUtils.DecompressFolder(patchFile.FullName, patchDecompressPath, patchInfo.CompressionFormat); comms.Stage = PatchStage.UpdatingFiles; comms.Log(Localization.Get(StringId.UpdatingXFiles, filesToUpdate.Count)); for (int i = 0; i < filesToUpdate.Count; i++) { if (comms.Cancel) { return(PatchResult.Failed); } string fileRelativePath = filesToUpdate[i].Path; string targetAbsolutePath = rootPath + fileRelativePath; comms.Log(Localization.Get(StringId.UpdatingXthFile, i + 1, filesToUpdate.Count, fileRelativePath)); Directory.CreateDirectory(Path.GetDirectoryName(targetAbsolutePath)); PatchUtils.CopyFile(patchDecompressPath + fileRelativePath, targetAbsolutePath); ReportProgress(1, 0L); } PatchUtils.DeleteDirectory(patchDecompressPath); File.Delete(patchDownloadPath); comms.Log(Localization.Get(StringId.PatchAppliedInXSeconds, timer.ElapsedSeconds())); return(PatchResult.Success); }
protected override PatchResult Execute() { if (comms.Cancel) { return(PatchResult.Failed); } if (comms.IsUnderMaintenance()) { return(PatchResult.Failed); } string patchVersion = patchInfo.PatchVersion(); string patchDownloadPath = comms.GetDownloadPathForPatch(patchVersion); string patchDecompressPath = comms.GetDecompressPathForPatch(patchVersion); if (patchInfo.Files.Count > 0) { PatchUtils.DeleteDirectory(patchDecompressPath); Directory.CreateDirectory(patchDecompressPath); } if (comms.Cancel) { return(PatchResult.Failed); } InitializeProgress(patchInfo.Files.Count, patchInfo.CompressedFileSize); Stopwatch timer = Stopwatch.StartNew(); if (patchInfo.Files.Count > 0) { FileInfo patchFile = new FileInfo(patchDownloadPath); if (!patchFile.Exists || !patchFile.MatchesSignature(patchInfo.CompressedFileSize, patchInfo.CompressedMd5Hash)) { comms.Stage = PatchStage.DownloadingFiles; Stopwatch downloadTimer = Stopwatch.StartNew(); comms.Log(Localization.Get(StringId.DownloadingPatchX, patchInfo.PatchVersion())); patchFile = comms.DownloadManager.DownloadFileFromURLToPath(patchInfo.DownloadURL, patchDownloadPath, patchInfo.CompressedFileSize); if (patchFile == null) { comms.FailReason = PatchFailReason.DownloadError; comms.FailDetails = Localization.Get(StringId.E_PatchXCouldNotBeDownloaded, patchInfo.PatchVersion()); return(PatchResult.Failed); } else if (!patchFile.MatchesSignature(patchInfo.CompressedFileSize, patchInfo.CompressedMd5Hash)) { comms.FailReason = PatchFailReason.CorruptDownloadError; comms.FailDetails = Localization.Get(StringId.E_DownloadedFileXIsCorrupt, patchInfo.PatchVersion()); return(PatchResult.Failed); } else { comms.Log(Localization.Get(StringId.XDownloadedInYSeconds, patchInfo.PatchVersion(), downloadTimer.ElapsedSeconds())); } } else { ReportProgress(0, patchInfo.CompressedFileSize); } if (comms.Cancel) { return(PatchResult.Failed); } comms.Stage = PatchStage.ExtractingFilesFromArchive; comms.Log(Localization.Get(StringId.DecompressingPatchX, patchInfo.PatchVersion())); ZipUtils.DecompressFolder(patchFile.FullName, patchDecompressPath, patchInfo.CompressionFormat); comms.Stage = PatchStage.UpdatingFiles; comms.Log(Localization.Get(StringId.UpdatingXFiles, patchInfo.Files.Count)); int failedItemCount = 0; for (int i = 0; i < patchInfo.Files.Count; i++) { if (comms.Cancel) { return(PatchResult.Failed); } ReportProgress(1, 0L); string fileRelativePath = patchInfo.Files[i].Path; string diffFileAbsolutePath = patchDecompressPath + fileRelativePath; if (!File.Exists(diffFileAbsolutePath)) { comms.Log(Localization.Get(StringId.E_DiffOfXDoesNotExist, Path.GetFileName(fileRelativePath))); failedItemCount++; continue; } string decompressAbsolutePath = comms.DecompressedFilesPath + fileRelativePath; if (comms.SelfPatching && ApplyDiffToFile(decompressAbsolutePath, diffFileAbsolutePath, decompressAbsolutePath, i)) { continue; } string localFileAbsolutePath = comms.RootPath + fileRelativePath; string targetPath = comms.SelfPatching ? decompressAbsolutePath : localFileAbsolutePath; if (!ApplyDiffToFile(localFileAbsolutePath, diffFileAbsolutePath, targetPath, i)) { failedItemCount++; } } comms.Log(Localization.Get(StringId.XFilesUpdatedSuccessfully, patchInfo.Files.Count - failedItemCount, patchInfo.Files.Count)); } if (patchInfo.RenamedFiles.Count > 0) { comms.Log(Localization.Get(StringId.RenamingXFiles, patchInfo.RenamedFiles.Count)); if (!RenameItems(patchInfo.RenamedFiles)) { return(PatchResult.Failed); } } // Updating version code to the latest one will be done by SimplePatchTool, after checking // whether or not all files are correctly updated if (patchInfo.ToVersion < comms.VersionInfo.Version) { comms.UpdateVersion(patchInfo.ToVersion); } if (patchInfo.Files.Count > 0) { PatchUtils.DeleteDirectory(patchDecompressPath); File.Delete(patchDownloadPath); } comms.Log(Localization.Get(StringId.PatchAppliedInXSeconds, timer.ElapsedSeconds())); return(PatchResult.Success); }