private void InvokeDownload(Version v) { CancellationTokenSource cancelSource = new CancellationTokenSource(); v.IsNew = false; v.StateChangeInfo = new VersionStateChangeInfo(VersionState.Initializing); v.StateChangeInfo.CancelCommand = new RelayCommand((o) => cancelSource.Cancel()); Debug.WriteLine("Download start"); Task.Run(async() => { string dlPath = (v.VersionType == VersionType.Preview ? "Minecraft-Preview-" : "Minecraft-") + v.Name + ".Appx"; VersionDownloader downloader = _anonVersionDownloader; if (v.VersionType == VersionType.Beta) { 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 (WUTokenHelper.WUTokenException e) { Debug.WriteLine("Authentication failed:\n" + e.ToString()); MessageBox.Show("Failed to authenticate because: " + e.Message, "Authentication failed"); v.StateChangeInfo = null; return; } catch (Exception e) { Debug.WriteLine("Authentication failed:\n" + e.ToString()); MessageBox.Show(e.ToString(), "Authentication failed"); v.StateChangeInfo = null; return; } } try { await downloader.Download(v.UUID, "1", dlPath, (current, total) => { if (v.StateChangeInfo.VersionState != VersionState.Downloading) { Debug.WriteLine("Actual download started"); v.StateChangeInfo.VersionState = VersionState.Downloading; if (total.HasValue) { v.StateChangeInfo.TotalSize = total.Value; } } v.StateChangeInfo.DownloadedBytes = current; }, cancelSource.Token); Debug.WriteLine("Download complete"); } catch (BadUpdateIdentityException) { Debug.WriteLine("Download failed due to failure to fetch download URL"); MessageBox.Show( "Unable to fetch download URL for version." + (v.VersionType == VersionType.Beta ? "\nFor beta versions, please make sure your account is subscribed to the Minecraft beta programme in the Xbox Insider Hub app." : "") ); v.StateChangeInfo = null; return; } catch (Exception e) { Debug.WriteLine("Download failed:\n" + e.ToString()); if (!(e is TaskCanceledException)) { MessageBox.Show("Download failed:\n" + e.ToString()); } v.StateChangeInfo = null; return; } try { v.StateChangeInfo.VersionState = VersionState.Extracting; 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")); if (UserPrefs.DeleteAppxAfterDownload) { Debug.WriteLine("Deleting APPX to reduce disk usage"); File.Delete(dlPath); } else { Debug.WriteLine("Not deleting APPX due to user preferences"); } } catch (Exception e) { Debug.WriteLine("Extraction failed:\n" + e.ToString()); MessageBox.Show("Extraction failed:\n" + e.ToString()); v.StateChangeInfo = null; return; } v.StateChangeInfo = null; v.UpdateInstallStatus(); }); }
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) { 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; return; } try { 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")); } catch (Exception e) { Debug.WriteLine("Extraction failed:\n" + e.ToString()); MessageBox.Show("Extraction failed:\n" + e.ToString()); v.StateChangeInfo = null; return; } v.StateChangeInfo = null; v.UpdateInstallStatus(); }); }