private async void BtnClick_MinecraftDownload(object sender, RoutedEventArgs e) { Btn_MinecraftDownload.IsEnabled = false; using (HttpClient client = new HttpClient()) { try { MinecraftVersion ver = VersionsList.SelectedItem as MinecraftVersion; if (ver == null) { return; } string json = await client.GetStringAsync(ver.url); string t = ver.GetJsonPath(); StorageFile file = await CoreManager.WorkDir.CreateFileAsync(ver.GetJsonPath(), CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, json); ValueSet valueSet = new ValueSet(); valueSet["type"] = "version-url"; valueSet["version"] = ver.id; AppServiceResponse response = await AppServiceManager.appServiceConnection.SendMessageAsync(valueSet); string url = response.Message["client"]?.ToString(); if (!string.IsNullOrWhiteSpace(url)) { DownloadItem item = new DownloadItem("Minecraft" + ver.id, ver.GetJarPath(), url); DownloadManager.DownloadQuene.Add(item); DownloadManager.StartDownload(); ViewModel.isPaneOpen = true; } } catch (HttpRequestException exp) { await dialog.Show(CoreManager.GetStringFromResource("/DownloadPage/DownloadFailed"), exp.Message + exp.StackTrace); } catch (IOException exp) { await dialog.Show(CoreManager.GetStringFromResource("/DownloadPage/DownloadFailed"), exp.Message + exp.StackTrace); } finally { Btn_MinecraftDownload.IsEnabled = true; } } }
private async void Button_Click(object sender, RoutedEventArgs e) { MinecraftVersion version = _verList.SelectedItem as MinecraftVersion; if (version == null) { return; } ViewModel.isWorking = true; try { string jsonPath = CoreManager.CoreMCL.Core.GameRootPath + "\\" + version.GetJsonPath(); using (HttpClient client = new HttpClient()) { FileInfo file = new FileInfo(jsonPath); if (!file.Directory.Exists) { file.Directory.Create(); } File.Create(jsonPath).Dispose(); File.WriteAllText(jsonPath, await client.GetStringAsync(version.url)); string jarPath; KMCCC.Launcher.Version kver = CoreManager.CoreMCL.Core.GetVersion(version.id); jarPath = CoreManager.CoreMCL.Core.GameRootPath + "\\" + version.GetJarPath(); file = new FileInfo(jarPath); if (!file.Directory.Exists) { file.Directory.Create(); } File.Create(jarPath); HttpDownloader downloader = new HttpDownloader(kver.Downloads.Client.Url, jarPath); var progressController = await MainWindow.Current.ShowProgressAsync("正在下载: " + kver.Id, "", true, DefaultDialogSettings); downloader.DownloadProgressChanged += async delegate { if (downloader.ProgressInPercent == 100) { await progressController.CloseAsync(); } else { progressController.SetProgress(downloader.ProgressInPercent / 100); progressController.SetMessage("下载速度: " + GetDownloadSpeedFriendlyText(downloader)); } }; progressController.Canceled += delegate { if (downloader.State != DownloadState.Completed) { downloader.Cancel(); } }; downloader.Start(); } } catch (IOException ex) { await MainWindow.Current.ShowMessageAsync("下载失败", ex.Message + ex.StackTrace); } catch (HttpRequestException ex) { await MainWindow.Current.ShowMessageAsync("下载失败", ex.Message + ex.StackTrace); } ViewModel.isWorking = false; }