private async Task InstallUpdater(Action <int> onProgress = null) { var downloadedZipPath = string.Empty; var unzippedDirPath = string.Empty; try { var latestVersions = await GetLatestVersionResponse(); downloadedZipPath = await _downloadService.DownloadZipFile( latestVersions.Updater.Url, DownloadPath, (progress) => { onProgress?.Invoke(progress); }); unzippedDirPath = await _downloadService.UnzipFile(downloadedZipPath); var newUpdater = Path.Combine(unzippedDirPath, UpdaterName); var targetUpdater = Path.Combine(FileUtilities.AppDataPath, UpdaterName); FileUtilities.CopyFile(newUpdater, targetUpdater, true); } finally { if (!string.IsNullOrEmpty(downloadedZipPath) && File.Exists(downloadedZipPath)) { File.Delete(downloadedZipPath); } if (!string.IsNullOrEmpty(unzippedDirPath) && Directory.Exists(unzippedDirPath)) { Directory.Delete(unzippedDirPath, true); } } }
public async Task InstallAddon(int addonId, Action <AddonInstallState, decimal> updateAction) { var addon = GetAddon(addonId); if (addon == null || string.IsNullOrEmpty(addon.DownloadUrl)) { throw new Exception("Addon not found or invalid"); } updateAction?.Invoke(AddonInstallState.Downloading, 25m); string downloadedFilePath = string.Empty; string unzippedDirectory = string.Empty; string downloadedThumbnail = string.Empty; try { await CacheThumbnail(addon); downloadedFilePath = await _downloadService.DownloadZipFile(addon.DownloadUrl, FileUtilities.DownloadPath); if (!string.IsNullOrEmpty(addon.InstalledVersion)) { updateAction?.Invoke(AddonInstallState.BackingUp, 0.50m); var backupZipFilePath = Path.Combine(BackupPath, $"{addon.Name}-{addon.InstalledVersion}.zip"); //await _downloadService.ZipFile(downloadedFilePath, backupZipFilePath); } updateAction?.Invoke(AddonInstallState.Installing, 75m); unzippedDirectory = await _downloadService.UnzipFile(downloadedFilePath); await InstallUnzippedDirectory(unzippedDirectory, addon.ClientType); var unzippedDirectoryNames = FileUtilities.GetDirectoryNames(unzippedDirectory); addon.InstalledVersion = addon.LatestVersion; addon.InstalledAt = DateTime.UtcNow; addon.InstalledFolders = string.Join(',', unzippedDirectoryNames); if (string.IsNullOrEmpty(addon.GameVersion)) { addon.GameVersion = GetLatestGameVersion(unzippedDirectory, unzippedDirectoryNames); } _addonRepository.UpdateItem(addon); await _analyticsService.TrackUserAction("Addons", "InstallById", $"{addon.ClientType}|{addon.Name}"); AddonInstalled?.Invoke(this, new AddonEventArgs(addon, AddonChangeType.Installed)); } catch (Exception ex) { _analyticsService.Track(ex, "InstallAddon"); } finally { if (!string.IsNullOrEmpty(unzippedDirectory)) { await FileUtilities.DeleteDirectory(unzippedDirectory); } if (!string.IsNullOrEmpty(downloadedFilePath)) { File.Delete(downloadedFilePath); } } updateAction?.Invoke(AddonInstallState.Complete, 100m); }