private async Task DownloadVersion(MCVersion v, VersionDownloader downloader, string dlPath, CancellationTokenSource cancelSource) { try { ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.isInitializing; await downloader.Download(v, v.UUID, "1", dlPath, (current, total) => { if (ViewModels.LauncherModel.Default.CurrentState == ViewModels.LauncherModel.StateChange.isInitializing) { ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.isDownloading; System.Diagnostics.Debug.WriteLine("Actual download started"); if (total.HasValue) { ViewModels.LauncherModel.Default.TotalProgress = total.Value; } } ViewModels.LauncherModel.Default.CurrentProgress = current; }, cancelSource.Token); System.Diagnostics.Debug.WriteLine("Download complete"); ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; } catch (Exception e) { ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; System.Diagnostics.Debug.WriteLine("Download failed:\n" + e.ToString()); throw e; } }
private void InvokeDownload(MCVersion v, bool RunAfterwards = false) { System.Diagnostics.Debug.WriteLine("Download start"); bool wasCanceled = false; Task.Run(async() => { cancelSource = new CancellationTokenSource(); ViewModels.LauncherModel.Default.ShowProgressBar = true; ViewModels.LauncherModel.Default.AllowCancel = true; ViewModels.LauncherModel.Default.CancelCommand = new RelayCommand((o) => InvokeCancel()); try { string dlPath = "Minecraft-" + v.Name + ".Appx"; VersionDownloader downloader = _anonVersionDownloader; downloader = _userVersionDownloader; if (v.IsBeta) { await BetaAuthenticate(); } await DownloadVersion(v, downloader, dlPath, cancelSource); await ExtractPackage(v, dlPath, cancelSource); } catch (TaskCanceledException) { wasCanceled = true; } if (!RunAfterwards || wasCanceled) { ViewModels.LauncherModel.Default.ShowProgressBar = false; } ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; ViewModels.LauncherModel.Default.AllowCancel = false; ViewModels.LauncherModel.Default.CancelCommand = null; cancelSource = null; v.UpdateInstallStatus(); if (RunAfterwards && !wasCanceled) { InvokeLaunch(v); } }); }
private void InvokeDownload(Version v) { CancellationTokenSource cancelSource = new CancellationTokenSource(); v.StateChangeInfo = new VersionStateChangeInfo(); v.StateChangeInfo.IsInitializing = true; v.StateChangeInfo.CancelCommand = new RelayCommand((o) => cancelSource.Cancel()); Debug.WriteLine("Download start"); Task.Run(async() => { string dlPath = "Minecraft-" + v.Name + ".Appx"; VersionDownloader downloader = _anonVersionDownloader; if (v.IsBeta) { downloader = _userVersionDownloader; if (Interlocked.CompareExchange(ref _userVersionDownloaderLoginTaskStarted, 1, 0) == 0) { _userVersionDownloaderLoginTask.Start(); } Debug.WriteLine("Waiting for authentication"); try { await _userVersionDownloaderLoginTask; Debug.WriteLine("Authentication complete"); } catch (Exception e) { v.StateChangeInfo = null; Debug.WriteLine("Authentication failed:\n" + e.ToString()); MessageBox.Show("Failed to authenticate. Please make sure your account is subscribed to the beta programme.\n\n" + e.ToString(), "Authentication failed"); return; } } try { await downloader.Download(v.UUID, "1", dlPath, (current, total) => { if (v.StateChangeInfo.IsInitializing) { IsDownloading = true; OnGameStateChanged(GameStateArgs.Empty); Debug.WriteLine("Actual download started"); v.StateChangeInfo.IsInitializing = false; if (total.HasValue) { v.StateChangeInfo.TotalSize = total.Value; } } v.StateChangeInfo.DownloadedBytes = current; }, cancelSource.Token); Debug.WriteLine("Download complete"); } catch (Exception e) { Debug.WriteLine("Download failed:\n" + e.ToString()); if (!(e is TaskCanceledException)) { MessageBox.Show("Download failed:\n" + e.ToString()); } v.StateChangeInfo = null; IsDownloading = false; return; } try { Debug.WriteLine("Extraction started"); v.StateChangeInfo.IsExtracting = true; string dirPath = v.GameDirectory; if (Directory.Exists(dirPath)) { Directory.Delete(dirPath, true); } ZipFile.ExtractToDirectory(dlPath, dirPath); v.StateChangeInfo = null; File.Delete(Path.Combine(dirPath, "AppxSignature.p7x")); File.Delete(dlPath); Debug.WriteLine("Extracted successfully"); InvokeLaunch(v); } catch (Exception e) { Debug.WriteLine("Extraction failed:\n" + e.ToString()); MessageBox.Show("Extraction failed:\n" + e.ToString()); v.StateChangeInfo = null; IsDownloading = false; return; } v.StateChangeInfo = null; v.UpdateInstallStatus(); OnGameStateChanged(GameStateArgs.Empty); }); }
private void InvokeDownload(MCVersion v) { CancellationTokenSource cancelSource = new CancellationTokenSource(); Model.StateChangeInfo = new VersionStateChangeInfo(); Model.StateChangeInfo.IsInitializing = true; Model.StateChangeInfo.CancelCommand = new RelayCommand((o) => cancelSource.Cancel()); Program.Log("Download start"); Task.Run(async() => { string dlPath = "Minecraft-" + v.Name + ".Appx"; VersionDownloader downloader = _anonVersionDownloader; if (v.IsBeta) { downloader = _userVersionDownloader; if (Interlocked.CompareExchange(ref _userVersionDownloaderLoginTaskStarted, 1, 0) == 0) { _userVersionDownloaderLoginTask.Start(); } Program.Log("Waiting for authentication"); try { await _userVersionDownloaderLoginTask; Program.Log("Authentication complete"); } catch (Exception e) { Model.StateChangeInfo = null; Program.Log("Authentication failed:\n" + e.ToString()); MessageBox.Show("Failed to authenticate. Please make sure your account is subscribed to the beta programme.\n\n" + e.ToString(), "Authentication failed"); return; } } try { await downloader.Download(v.UUID, "1", dlPath, (current, total) => { if (Model.StateChangeInfo.IsInitializing) { Model.StateChangeInfo.IsDownloading = true; IsDownloading = true; Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); Program.Log("Actual download started"); Model.StateChangeInfo.IsInitializing = false; if (total.HasValue) { Model.StateChangeInfo.TotalSize_Downloading = total.Value; } } Model.StateChangeInfo.DownloadedBytes_Downloading = current; }, cancelSource.Token); Program.Log("Download complete"); Model.StateChangeInfo.IsDownloading = false; IsDownloading = false; } catch (Exception e) { Program.Log("Download failed:\n" + e.ToString()); if (!(e is TaskCanceledException)) { MessageBox.Show("Download failed:\n" + e.ToString()); } Model.StateChangeInfo = null; Model.StateChangeInfo.IsDownloading = false; IsDownloading = false; return; } await ExtractPackage(v, dlPath); Model.StateChangeInfo = null; v.UpdateInstallStatus(); Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); }); }