Ejemplo n.º 1
0
        private bool CheckAssetsIndex(KMCCC.Launcher.Version kver)
        {
            var result = CoreMCL.CheckAssets(kver);

            return(result.hasValidIndex);
        }
Ejemplo n.º 2
0
        private async Task launch()
        {
            Config.INSTANCE.User = ViewModel.User;
            Config.SaveConfigToFileAsync();
            KMCCC.Launcher.Version kver = ViewModel.SelectedVersion;
            if (kver == null)
            {
                await MainWindow.Current.ShowMessageAsync("启动失败", "版本未指定,请选择一个要启动的Minecraft版本");

                return;
            }
            if (CoreMCL.UserAuthenticator == null)
            {
                await MainWindow.Current.ShowMessageAsync("启动失败", "未指定用户,请前往账户设置选择要登入Minecraft的用户");

                return;
            }
            Option.versionId = kver.Id;
            Option.javaExt   = Config.INSTANCE.JavaExt;
            Option.javaArgs  = Config.INSTANCE.JavaArgs;
            if (Config.INSTANCE.MaxMemory > 0)
            {
                Option.javaArgs = string.Format("-Xmx{0}M {1}", Config.INSTANCE.MaxMemory, Option.javaArgs);
            }

            #region Check libraries and natives
            ViewModel.LaunchButtonContent = "正在检查核心文件...";

            List <MinecraftAssembly> missing = null;
            await Task.Run(() =>
            {
                missing = CoreMCL.CheckLibraries(kver);
                missing?.AddRange(CoreMCL.CheckNatives(kver));
            });

            if (missing?.Count > 0)
            {
                ViewModel.LaunchButtonContent = "正在下载核心文件...";
                DownloadDialog dialog = new DownloadDialog("正在下载运行Minecraft所需的文件...");
                missing.ForEach(lib =>
                {
                    if (Uri.TryCreate(lib.Url, UriKind.Absolute, out Uri uri))
                    {
                        DownloadItem item = new DownloadItem(lib.Name, lib.Path, uri);
                        dialog.DownloadQuene.Add(item);
                    }
                });
                dialog.StartDownload();
                if (await ShowDownloadDialog(dialog))
                {
                    return;
                }
            }
            #endregion

            // Check Assets
            ViewModel.LaunchButtonContent = "正在检查资源文件";
            if (!CheckAssetsIndex(kver))
            {
                ViewModel.LaunchButtonContent = "正在获取资源元数据";
                try
                {
                    await Task.Run(async() =>
                    {
                        using (HttpClient client = new HttpClient())
                        {
                            string json       = await client.GetStringAsync(kver.AssetsIndex.Url);
                            string path       = string.Format(@"{0}\assets\indexes\{1}.json", CoreMCL.Core.GameRootPath, kver.Assets);
                            FileInfo fileInfo = new FileInfo(path);
                            if (!fileInfo.Directory.Exists)
                            {
                                fileInfo.Directory.Create();
                            }
                            fileInfo.Create().Dispose();
                            File.WriteAllText(path, json);
                        }
                    });
                }
                catch (HttpRequestException ex)
                {
                    await MainWindow.Current.ShowMessageAsync("获取资源元数据失败", ex.Message + ex.StackTrace, MessageDialogStyle.Affirmative, DefaultDialogSettings);

                    return;
                }
                catch (IOException ex)
                {
                    await MainWindow.Current.ShowMessageAsync("获取资源元数据失败", ex.Message + ex.StackTrace, MessageDialogStyle.Affirmative, DefaultDialogSettings);

                    return;
                }
            }

            ViewModel.LaunchButtonContent = "正在检查资源文件...";
            (bool hasValidIndex, List <MinecraftAsset> missingAssets)assetsResult = (false, null);
            await Task.Run(() =>
            {
                assetsResult = CoreMCL.CheckAssets(kver);
            });

            if (!assetsResult.hasValidIndex)
            {
                await MainWindow.Current.ShowMessageAsync("获取资源元数据失败", "发生未知错误,无法获取有效的资源元数据,我们将为您继续启动游戏,但这可能会导致游戏中出现无翻译和无声音等问题");
            }
            else
            {
                if (assetsResult.missingAssets.Count > 0)
                {
                    DownloadDialog dialog = new DownloadDialog("正在下载资源文件...");
                    assetsResult.missingAssets.ForEach(ass =>
                    {
                        if (Uri.TryCreate(ass.GetDownloadUrl(), UriKind.Absolute, out Uri uri))
                        {
                            DownloadItem item = new DownloadItem("资源: " + ass.Hash, CoreMCL.Core.GameRootPath + "\\" + ass.GetPath(), uri);
                            dialog.DownloadQuene.Add(item);
                        }
                    });
                    if (await ShowDownloadDialog(dialog))
                    {
                        return;
                    }
                }
            }

            ViewModel.LaunchButtonContent = "正在启动...";
            LaunchResult result = CoreMCL.Launch(Option);
            if (!result.Success)
            {
                await MainWindow.Current.ShowMessageAsync("启动失败", result.ErrorMessage + "\r\n" + result.Exception);
            }
        }