private async Task ReRegisterPackage(MCVersion v, string gameDir) { try { foreach (var pkg in new PackageManager().FindPackages(MINECRAFT_PACKAGE_FAMILY)) { string location = GetPackagePath(pkg); if (location == gameDir) { System.Diagnostics.Debug.WriteLine("Skipping package removal - same path: " + pkg.Id.FullName + " " + location); return; } await RemovePackage(v, pkg); } System.Diagnostics.Debug.WriteLine("Registering package"); string manifestPath = Path.Combine(gameDir, "AppxManifest.xml"); ViewModels.LauncherModel.Default.DeploymentPackageName = GetPackageNameFromMainifest(manifestPath); ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.isRegisteringPackage; await DeploymentProgressWrapper(v, new PackageManager().RegisterPackageAsync(new Uri(manifestPath), null, DeploymentOptions.DevelopmentMode)); System.Diagnostics.Debug.WriteLine("App re-register done!"); ViewModels.LauncherModel.Default.DeploymentPackageName = ""; ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; } catch (Exception e) { System.Diagnostics.Debug.WriteLine("App re-register failed:\n" + e.ToString()); Application.Current.Dispatcher.Invoke(() => { ErrorScreenShow.errormsg("appregistererror"); }); throw e; } }
private void InvokeRemove(MCVersion v) { Task.Run(async() => { IsUninstalling = true; Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); Model.StateChangeInfo = new VersionStateChangeInfo(); Model.StateChangeInfo.IsUninstalling = true; try { await UnregisterPackage(v, Path.GetFullPath(v.GameDirectory)); Directory.Delete(v.GameDirectory, true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } Model.StateChangeInfo.IsUninstalling = false; IsUninstalling = false; v.UpdateInstallStatus(); Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); Model.StateChangeInfo = null; return; }); }
private void InvokeLaunch(MCVersion v) { Task.Run(async() => { StartLaunch(); await SetInstallationDataPath(); string gameDir = Path.GetFullPath(v.GameDirectory); await ReRegisterPackage(v, gameDir); bool closable = await LaunchGame(v); if (!closable) { EndLaunch(); } }); void StartLaunch() { ViewModels.LauncherModel.Default.ShowProgressBar = true; ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.isLaunching; } void EndLaunch() { ViewModels.LauncherModel.Default.ShowProgressBar = false; ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; } }
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; } }
public async void GetGameProcess(MCVersion v) { await Task.Run(() => { try { string FilePath = Path.GetDirectoryName(v.ExePath); string FileName = Path.GetFileNameWithoutExtension(v.ExePath).ToLower(); Process[] pList = Process.GetProcessesByName(FileName); foreach (Process p in pList) { string fileName = p.MainModule.FileName; if (fileName.StartsWith(FilePath, StringComparison.InvariantCultureIgnoreCase)) { ViewModels.LauncherModel.Default.IsGameRunning = true; GameProcess = p; p.EnableRaisingEvents = true; p.Exited += GameProcessExited; break; } } } catch (Exception ex) { Debug.WriteLine(ex); throw ex; } }); }
public void Installation_Edit(string uuid, string name, MCVersion version, string directory, string iconPath = null, bool isCustom = false) { if (CurrentProfile == null) { return; } if (CurrentInstallations == null) { return; } MCProfileExtensions.GetVersionParams(version, out VersioningMode versioningMode, out string version_uuid); BLInstallation new_installation = new BLInstallation() { DisplayName = name, IconPath = (iconPath == null ? @"Furnace.png" : iconPath), IsCustomIcon = isCustom, DirectoryName = directory, VersioningMode = versioningMode, VersionUUID = version_uuid }; if (CurrentInstallations.Any(x => x.InstallationUUID == uuid)) { int index = CurrentInstallations.FindIndex(x => x.InstallationUUID == uuid); CurrentInstallations[index] = new_installation; Save(); } }
private async Task DeploymentProgressWrapper(MCVersion version, IAsyncOperationWithProgress <DeploymentResult, DeploymentProgress> t) { TaskCompletionSource <int> src = new TaskCompletionSource <int>(); t.Progress += (v, p) => { System.Diagnostics.Debug.WriteLine("Deployment progress: " + p.state + " " + p.percentage + "%"); ViewModels.LauncherModel.Default.CurrentProgress = Convert.ToInt64(p.percentage); ViewModels.LauncherModel.Default.TotalProgress = ViewModels.LauncherModel.DeploymentMaximum; }; t.Completed += (v, p) => { if (p == AsyncStatus.Error) { System.Diagnostics.Debug.WriteLine("Deployment failed: " + v.GetResults().ErrorText); src.SetException(new Exception("Deployment failed: " + v.GetResults().ErrorText)); } else { System.Diagnostics.Debug.WriteLine("Deployment done: " + p); src.SetResult(1); } }; await src.Task; }
public void OpenFolder(MCVersion i) { string Directory = Path.GetFullPath(i.GameDirectory); if (!System.IO.Directory.Exists(Directory)) { System.IO.Directory.CreateDirectory(Directory); } Process.Start("explorer.exe", Directory); }
private async Task UnregisterPackage(MCVersion v, string gameDir) { foreach (var pkg in new PackageManager().FindPackages(MINECRAFT_PACKAGE_FAMILY)) { string location = GetPackagePath(pkg); if (location == "" || location == gameDir) { await RemovePackage(v, pkg); } } }
private async Task ExtractPackage(MCVersion v, string dlPath, CancellationTokenSource cancelSource) { Stream zipReadingStream = null; try { System.Diagnostics.Debug.WriteLine("Extraction started"); ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.isExtracting; string dirPath = v.GameDirectory; if (Directory.Exists(dirPath)) { Directory.Delete(dirPath, true); } zipReadingStream = File.OpenRead(dlPath); ZipArchive zip = new ZipArchive(zipReadingStream); var progress = new Progress <ZipProgress>(); progress.ProgressChanged += (s, z) => { ViewModels.LauncherModel.Default.CurrentProgress = z.Processed; ViewModels.LauncherModel.Default.TotalProgress = z.Total; }; await Task.Run(() => zip.ExtractToDirectory(dirPath, progress, cancelSource)); zipReadingStream.Close(); File.Delete(Path.Combine(dirPath, "AppxSignature.p7x")); File.Delete(dlPath); System.Diagnostics.Debug.WriteLine("Extracted successfully"); } catch (TaskCanceledException e) { if (zipReadingStream != null) { zipReadingStream.Close(); } Directory.Delete(v.GameDirectory, true); throw e; } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Extraction failed:\n" + e.ToString()); ErrorScreenShow.exceptionmsg("Extraction failed", e); if (zipReadingStream != null) { zipReadingStream.Close(); } ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; return; } }
private async Task ExtractPackage(MCVersion v, string dlPath) { Stream zipReadingStream = null; try { Program.Log("Extraction started"); Model.StateChangeInfo.IsExtracting = true; string dirPath = v.GameDirectory; if (Directory.Exists(dirPath)) { Directory.Delete(dirPath, true); } zipReadingStream = File.OpenRead(dlPath); ZipArchive zip = new ZipArchive(zipReadingStream); var progress = new Progress <ZipProgress>(); progress.ProgressChanged += (s, z) => { if (Model.StateChangeInfo != null) { Model.StateChangeInfo.ExtractedBytes_Extracting = z.Processed; Model.StateChangeInfo.TotalBytes_Extracting = z.Total; } }; await Task.Run(() => zip.ExtractToDirectory(dirPath, progress)); zipReadingStream.Close(); Model.StateChangeInfo = null; File.Delete(Path.Combine(dirPath, "AppxSignature.p7x")); File.Delete(dlPath); Program.Log("Extracted successfully"); InvokeLaunch(v); } catch (Exception e) { Program.Log("Extraction failed:\n" + e.ToString()); MessageBox.Show("Extraction failed:\n" + e.ToString()); if (zipReadingStream != null) { zipReadingStream.Close(); } Model.StateChangeInfo.IsDownloading = false; Model.StateChangeInfo = null; IsDownloading = false; return; } }
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 async Task RemovePackage(MCVersion v, Package pkg) { System.Diagnostics.Debug.WriteLine("Removing package: " + pkg.Id.FullName); ViewModels.LauncherModel.Default.DeploymentPackageName = pkg.Id.FullName; ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.isRemovingPackage; if (!pkg.IsDevelopmentMode) { await DeploymentProgressWrapper(v, new PackageManager().RemovePackageAsync(pkg.Id.FullName, 0)); } else { System.Diagnostics.Debug.WriteLine("Package is in development mode"); await DeploymentProgressWrapper(v, new PackageManager().RemovePackageAsync(pkg.Id.FullName, RemovalOptions.PreserveApplicationData)); } System.Diagnostics.Debug.WriteLine("Removal of package done: " + pkg.Id.FullName); ViewModels.LauncherModel.Default.DeploymentPackageName = ""; ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; }
private async Task <bool> LaunchGame(MCVersion v) { try { ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.isLaunching; var pkg = await AppDiagnosticInfo.RequestInfoForPackageAsync(MINECRAFT_PACKAGE_FAMILY); if (pkg.Count > 0) { await pkg[0].LaunchAsync(); } System.Diagnostics.Debug.WriteLine("App launch finished!"); if (Properties.LauncherSettings.Default.KeepLauncherOpen) { GetGameProcess(v); } if (Properties.LauncherSettings.Default.KeepLauncherOpen == false) { await Application.Current.Dispatcher.InvokeAsync(() => { Application.Current.MainWindow.Close(); }); return(true); } else { ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; return(false); } } catch (Exception e) { ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; System.Diagnostics.Debug.WriteLine("App launch failed:\n" + e.ToString()); Application.Current.Dispatcher.Invoke(() => { ErrorScreenShow.errormsg("applauncherror"); }); throw e; } }
private async Task ReRegisterPackage(MCVersion v, string gameDir) { foreach (var pkg in new PackageManager().FindPackages(MINECRAFT_PACKAGE_FAMILY)) { string location = GetPackagePath(pkg); if (location == gameDir) { Program.Log("Skipping package removal - same path: " + pkg.Id.FullName + " " + location); return; } await RemovePackage(v, pkg); } Program.Log("Registering package"); string manifestPath = Path.Combine(gameDir, "AppxManifest.xml"); Model.StateChangeInfo.DeploymentPackageName = GetPackageNameFromMainifest(manifestPath); Model.StateChangeInfo.IsRegisteringPackage = true; await DeploymentProgressWrapper(v, new PackageManager().RegisterPackageAsync(new Uri(manifestPath), null, DeploymentOptions.DevelopmentMode)); Program.Log("App re-register done!"); Model.StateChangeInfo.DeploymentPackageName = ""; Model.StateChangeInfo.IsRegisteringPackage = false; string GetPackageNameFromMainifest(string filePath) { try { string manifestXml = File.ReadAllText(filePath); XDocument XMLDoc = XDocument.Parse(manifestXml); var Descendants = XMLDoc.Descendants(); XElement Identity = Descendants.Where(x => x.Name.LocalName == "Identity").FirstOrDefault(); string Name = Identity.Attribute("Name").Value; string Version = Identity.Attribute("Version").Value; string ProcessorArchitecture = Identity.Attribute("ProcessorArchitecture").Value; return(String.Join("_", Name, Version, ProcessorArchitecture)); } catch { return("???"); } } }
private async Task RemovePackage(MCVersion v, Package pkg) { Program.Log("Removing package: " + pkg.Id.FullName); Model.StateChangeInfo.DeploymentPackageName = pkg.Id.FullName; Model.StateChangeInfo.IsRemovingPackage = true; if (!pkg.IsDevelopmentMode) { // somehow this cause errors for some people, idk why, disabled //BackupMinecraftDataForRemoval(); await DeploymentProgressWrapper(v, new PackageManager().RemovePackageAsync(pkg.Id.FullName, 0)); } else { Program.Log("Package is in development mode"); await DeploymentProgressWrapper(v, new PackageManager().RemovePackageAsync(pkg.Id.FullName, RemovalOptions.PreserveApplicationData)); } Program.Log("Removal of package done: " + pkg.Id.FullName); Model.StateChangeInfo.DeploymentPackageName = ""; Model.StateChangeInfo.IsRemovingPackage = false; }
public async void GetActiveProcess(MCVersion v) { await Task.Run(() => { string FilePath = Path.GetDirectoryName(v.ExePath); string FileName = Path.GetFileNameWithoutExtension(v.ExePath).ToLower(); Process[] pList = Process.GetProcessesByName(FileName); foreach (Process p in pList) { string fileName = p.MainModule.FileName; if (fileName.StartsWith(FilePath, StringComparison.InvariantCultureIgnoreCase)) { IsGameNotOpen = false; GameProcess = p; p.EnableRaisingEvents = true; p.Exited += GameProcess_Exited; break; } } }); }
public static void GetVersionParams(MCVersion version, out VersioningMode versioningMode, out string version_uuid) { version_uuid = "latest_release"; versioningMode = VersioningMode.LatestRelease; if (version != null) { if (version.UUID == "latest_beta") { versioningMode = VersioningMode.LatestBeta; } else if (version.UUID == "latest_release") { versioningMode = VersioningMode.LatestRelease; } else { versioningMode = VersioningMode.None; } version_uuid = version.UUID; } }
public void Installation_Create(string name, MCVersion version, string directory, string iconPath = null, bool isCustom = false) { if (CurrentProfile == null) { return; } if (CurrentInstallations == null) { return; } MCProfileExtensions.GetVersionParams(version, out VersioningMode versioningMode, out string version_uuid); BLInstallation new_installation = new BLInstallation() { DisplayName = name, IconPath = (iconPath == null ? @"Furnace.png" : iconPath), IsCustomIcon = isCustom, DirectoryName = directory, VersioningMode = versioningMode, VersionUUID = version_uuid }; Installation_Add(new_installation); }
public static void CreateInstallation(string name, MCVersion version, string directory, string iconPath = @"/BedrockLauncher;component/Resources/images/installation_icons/Furnace.png", bool isCustom = false) { if (ProfileList.profiles.ContainsKey(CurrentProfile)) { MCInstallation installation = new MCInstallation(); installation.DisplayName = name; installation.IconPath_Full = iconPath; installation.IsCustomIcon = isCustom; installation.DirectoryName = directory; if (version == null || version.UUID == "latest_release") { installation.UseLatestVersion = true; installation.VersionUUID = "latest_release"; } else if (version.UUID == "latest_beta") { installation.UseLatestVersion = true; installation.UseLatestBeta = true; installation.VersionUUID = "latest_beta"; } else { installation.VersionUUID = version.UUID; } if (ProfileList.profiles[CurrentProfile].Installations == null) { ProfileList.profiles[CurrentProfile].Installations = new List <MCInstallation>(); } ProfileList.profiles[CurrentProfile].Installations.Add(installation); SaveProfiles(); } }
public static bool Filter_VersionList(object obj) { MCVersion v = obj as MCVersion; if (v.IsInstalled) { if (!Properties.LauncherSettings.Default.ShowBetas && v.IsBeta) { return(false); } else if (!Properties.LauncherSettings.Default.ShowReleases && !v.IsBeta) { return(false); } else { return(true); } } else { return(false); } }
private void InvokeRemove(MCVersion v) { Task.Run(async() => { ViewModels.LauncherModel.Default.ShowProgressBar = true; ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.isUninstalling; try { await UnregisterPackage(v, Path.GetFullPath(v.GameDirectory)); Directory.Delete(v.GameDirectory, true); } catch (Exception ex) { ErrorScreenShow.exceptionmsg(ex); } v.UpdateInstallStatus(); ViewModels.LauncherModel.Default.CurrentState = ViewModels.LauncherModel.StateChange.None; ViewModels.LauncherModel.Default.ShowProgressBar = false; ConfigManager.OnConfigStateChanged(null, Events.ConfigStateArgs.Empty); return; }); }
private async Task DeploymentProgressWrapper(MCVersion version, IAsyncOperationWithProgress <DeploymentResult, DeploymentProgress> t) { TaskCompletionSource <int> src = new TaskCompletionSource <int>(); t.Progress += (v, p) => { Program.Log("Deployment progress: " + p.state + " " + p.percentage + "%"); Model.StateChangeInfo.DeploymentProgress = Convert.ToInt64(p.percentage); }; t.Completed += (v, p) => { if (p == AsyncStatus.Error) { Program.Log("Deployment failed: " + v.GetResults().ErrorText); src.SetException(new Exception("Deployment failed: " + v.GetResults().ErrorText)); } else { Program.Log("Deployment done: " + p); src.SetResult(1); } }; await src.Task; }
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); }); }); }
public void Remove(MCVersion v) { InvokeRemove(v); }
private void InvokeLaunch(MCVersion v) { if (HasLaunchTask) { Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); return; } else { HasLaunchTask = true; Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); } Task.Run(async() => { SetInstallationDataPath(); Model.StateChangeInfo = new VersionStateChangeInfo(); string gameDir = Path.GetFullPath(v.GameDirectory); try { await ReRegisterPackage(v, gameDir); } catch (Exception e) { Program.Log("App re-register failed:\n" + e.ToString()); Application.Current.Dispatcher.Invoke(() => { ErrorScreenShow.errormsg("appregistererror"); }); HasLaunchTask = false; Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); Model.StateChangeInfo = null; return; } try { Model.StateChangeInfo.IsLaunching = true; var pkg = await AppDiagnosticInfo.RequestInfoForPackageAsync(MINECRAFT_PACKAGE_FAMILY); if (pkg.Count > 0) { await pkg[0].LaunchAsync(); } Program.Log("App launch finished!"); if (Properties.LauncherSettings.Default.KeepLauncherOpen) { GetActiveProcess(v); } HasLaunchTask = false; Model.StateChangeInfo.IsLaunching = false; Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); Model.StateChangeInfo = null; if (Properties.LauncherSettings.Default.KeepLauncherOpen == false) { await Application.Current.Dispatcher.InvokeAsync(() => { Application.Current.MainWindow.Close(); }); } } catch (Exception e) { Program.Log("App launch failed:\n" + e.ToString()); Application.Current.Dispatcher.Invoke(() => { ErrorScreenShow.errormsg("applauncherror"); }); HasLaunchTask = false; Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); Model.StateChangeInfo = null; return; } }); }
public void Repair(MCVersion v) { InvokeDownload(v, false); }
public static BLVersion Convert(MCVersion obj) { return(JsonConvert.DeserializeObject <BLVersion>(JsonConvert.SerializeObject(obj))); }
private void InvokeDownload(MCVersion 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; Application.Current.Dispatcher.Invoke(() => { 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(); Application.Current.Dispatcher.Invoke(() => { OnGameStateChanged(GameStateArgs.Empty); }); }); }