Esempio n. 1
0
        private async Task LaunchGameFromWindow()
        {
            try
            {
                string userName = playerNameTextBox.Text;

                #region 检查有效数据
                if (authTypeCombobox.SelectedItem == null)
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Message.EmptyAuthType"),
                                                App.GetResourceString("String.Message.EmptyAuthType2"));

                    return;
                }
                if (string.IsNullOrWhiteSpace(userName))
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Message.EmptyUsername"),
                                                App.GetResourceString("String.Message.EmptyUsername2"));

                    return;
                }
                if (launchVersionCombobox.SelectedItem == null)
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Message.EmptyLaunchVersion"),
                                                App.GetResourceString("String.Message.EmptyLaunchVersion2"));

                    return;
                }
                if (App.handler.Java == null)
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Message.NoJava"), App.GetResourceString("String.Message.NoJava2"));

                    return;
                }
                #endregion

                LaunchSetting launchSetting = new LaunchSetting()
                {
                    Version = (Core.Modules.Version)launchVersionCombobox.SelectedItem
                };

                this.loadingGrid.Visibility = Visibility.Visible;
                this.loadingRing.IsActive   = true;

                #region 验证
                AuthTypeItem auth = (AuthTypeItem)authTypeCombobox.SelectedItem;
                //设置CLIENT TOKEN
                if (string.IsNullOrWhiteSpace(App.config.MainConfig.User.ClientToken))
                {
                    App.config.MainConfig.User.ClientToken = Guid.NewGuid().ToString("N");
                }
                else
                {
                    Requester.ClientToken = App.config.MainConfig.User.ClientToken;
                }

                #region NewData
                string autoVerifyingMsg           = null;
                string autoVerifyingMsg2          = null;
                string autoVerificationFailedMsg  = null;
                string autoVerificationFailedMsg2 = null;
                string loginMsg  = null;
                string loginMsg2 = null;
                LoginDialogSettings loginDialogSettings = new LoginDialogSettings()
                {
                    NegativeButtonText         = App.GetResourceString("String.Base.Cancel"),
                    AffirmativeButtonText      = App.GetResourceString("String.Base.Login"),
                    RememberCheckBoxText       = App.GetResourceString("String.Base.ShouldRememberLogin"),
                    UsernameWatermark          = App.GetResourceString("String.Base.Username"),
                    InitialUsername            = userName,
                    RememberCheckBoxVisibility = Visibility,
                    EnablePasswordPreview      = true,
                    PasswordWatermark          = App.GetResourceString("String.Base.Password"),
                    NegativeButtonVisibility   = Visibility.Visible
                };
                string verifyingMsg = null, verifyingMsg2 = null, verifyingFailedMsg = null, verifyingFailedMsg2 = null;
                #endregion

                switch (auth.Type)
                {
                    #region 离线验证
                case Config.AuthenticationType.OFFLINE:
                    var loginResult = Core.Auth.OfflineAuthenticator.DoAuthenticate(userName);
                    launchSetting.AuthenticateAccessToken = loginResult.Item1;
                    launchSetting.AuthenticateUUID        = loginResult.Item2;
                    break;
                    #endregion

                    #region Mojang验证
                case Config.AuthenticationType.MOJANG:
                    Requester.AuthURL          = "https://authserver.mojang.com";
                    autoVerifyingMsg           = App.GetResourceString("String.Mainwindow.Auth.Mojang.AutoVerifying");
                    autoVerifyingMsg2          = App.GetResourceString("String.Mainwindow.Auth.Mojang.AutoVerifying2");
                    autoVerificationFailedMsg  = App.GetResourceString("String.Mainwindow.Auth.Mojang.AutoVerificationFailed");
                    autoVerificationFailedMsg2 = App.GetResourceString("String.Mainwindow.Auth.Mojang.AutoVerificationFailed2");
                    loginMsg            = App.GetResourceString("String.Mainwindow.Auth.Mojang.Login");
                    loginMsg2           = App.GetResourceString("String.Mainwindow.Auth.Mojang.Login2");
                    verifyingMsg        = App.GetResourceString("String.Mainwindow.Auth.Mojang.Verifying");
                    verifyingMsg2       = App.GetResourceString("String.Mainwindow.Auth.Mojang.Verifying2");
                    verifyingFailedMsg  = App.GetResourceString("String.Mainwindow.Auth.Mojang.VerificationFailed");
                    verifyingFailedMsg2 = App.GetResourceString("String.Mainwindow.Auth.Mojang.VerificationFailed2");
                    break;
                    #endregion

                    #region 统一通行证验证
                case Config.AuthenticationType.NIDE8:
                    if (App.nide8Handler == null)
                    {
                        await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.Auth.Nide8.NoID"),
                                                    App.GetResourceString("String.Mainwindow.Auth.Nide8.NoID2"));

                        return;
                    }
                    Requester.AuthURL          = string.Format("{0}authserver", App.nide8Handler.BaseURL);
                    autoVerifyingMsg           = App.GetResourceString("String.Mainwindow.Auth.Nide8.AutoVerifying");
                    autoVerifyingMsg2          = App.GetResourceString("String.Mainwindow.Auth.Nide8.AutoVerifying2");
                    autoVerificationFailedMsg  = App.GetResourceString("String.Mainwindow.Auth.Nide8.AutoVerificationFailed");
                    autoVerificationFailedMsg2 = App.GetResourceString("String.Mainwindow.Auth.Nide8.AutoVerificationFailed2");
                    loginMsg            = App.GetResourceString("String.Mainwindow.Auth.Nide8.Login");
                    loginMsg2           = App.GetResourceString("String.Mainwindow.Auth.Nide8.Login2");
                    verifyingMsg        = App.GetResourceString("String.Mainwindow.Auth.Nide8.Verifying");
                    verifyingMsg2       = App.GetResourceString("String.Mainwindow.Auth.Nide8.Verifying2");
                    verifyingFailedMsg  = App.GetResourceString("String.Mainwindow.Auth.Nide8.VerificationFailed");
                    verifyingFailedMsg2 = App.GetResourceString("String.Mainwindow.Auth.Nide8.VerificationFailed2");
                    loginDialogSettings.NegativeButtonVisibility = Visibility.Visible;
                    var nide8ChooseResult = await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.Auth.Nide8.Login2"), App.GetResourceString("String.Base.Choose"),
                                                                        MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary,
                                                                        new MetroDialogSettings()
                    {
                        AffirmativeButtonText    = App.GetResourceString("String.Base.Login"),
                        NegativeButtonText       = App.GetResourceString("String.Base.Cancel"),
                        FirstAuxiliaryButtonText = App.GetResourceString("String.Base.Register"),
                        DefaultButtonFocus       = MessageDialogResult.Affirmative
                    });

                    if (nide8ChooseResult == MessageDialogResult.Negative)
                    {
                        return;
                    }
                    if (nide8ChooseResult == MessageDialogResult.FirstAuxiliary)
                    {
                        System.Diagnostics.Process.Start(string.Format("https://login2.nide8.com:233/{0}/register", App.nide8Handler.ServerID));
                        return;
                    }
                    break;
                    #endregion

                    #region 自定义验证
                case Config.AuthenticationType.CUSTOM_SERVER:
                    string customAuthServer = App.config.MainConfig.User.AuthServer;
                    if (string.IsNullOrWhiteSpace(customAuthServer))
                    {
                        await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.Auth.Custom.NoAdrress"),
                                                    App.GetResourceString("String.Mainwindow.Auth.Custom.NoAdrress2"));

                        return;
                    }
                    else
                    {
                        Requester.AuthURL = customAuthServer;
                    }

                    autoVerifyingMsg           = App.GetResourceString("String.Mainwindow.Auth.Custom.AutoVerifying");
                    autoVerifyingMsg2          = App.GetResourceString("String.Mainwindow.Auth.Custom.AutoVerifying2");
                    autoVerificationFailedMsg  = App.GetResourceString("String.Mainwindow.Auth.Custom.AutoVerificationFailed");
                    autoVerificationFailedMsg2 = App.GetResourceString("String.Mainwindow.Auth.Custom.AutoVerificationFailed2");
                    loginMsg            = App.GetResourceString("String.Mainwindow.Auth.Custom.Login");
                    loginMsg2           = App.GetResourceString("String.Mainwindow.Auth.Custom.Login2");
                    verifyingMsg        = App.GetResourceString("String.Mainwindow.Auth.Custom.Verifying");
                    verifyingMsg2       = App.GetResourceString("String.Mainwindow.Auth.Custom.Verifying2");
                    verifyingFailedMsg  = App.GetResourceString("String.Mainwindow.Auth.Custom.VerificationFailed");
                    verifyingFailedMsg2 = App.GetResourceString("String.Mainwindow.Auth.Custom.VerificationFailed2");
                    break;
                    #endregion

                default:
                    var defaultLoginResult = Core.Auth.OfflineAuthenticator.DoAuthenticate(userName);
                    launchSetting.AuthenticateAccessToken = defaultLoginResult.Item1;
                    launchSetting.AuthenticateUUID        = defaultLoginResult.Item2;
                    break;
                }

                if (auth.Type != Config.AuthenticationType.OFFLINE)
                {
                    try
                    {
                        #region 如果记住登陆(有登陆记录)
                        if ((!string.IsNullOrWhiteSpace(App.config.MainConfig.User.AccessToken)) && (App.config.MainConfig.User.AuthenticationUUID != null))
                        {
                            var loader = await this.ShowProgressAsync(autoVerifyingMsg, autoVerifyingMsg2);

                            loader.SetIndeterminate();
                            Validate validate       = new Validate(App.config.MainConfig.User.AccessToken);
                            var      validateResult = await validate.PerformRequestAsync();

                            if (validateResult.IsSuccess)
                            {
                                launchSetting.AuthenticateAccessToken = App.config.MainConfig.User.AccessToken;
                                launchSetting.AuthenticateUUID        = App.config.MainConfig.User.AuthenticationUUID;
                                await loader.CloseAsync();
                            }
                            else
                            {
                                Refresh refresher     = new Refresh(App.config.MainConfig.User.AccessToken);
                                var     refreshResult = await refresher.PerformRequestAsync();

                                await loader.CloseAsync();

                                if (refreshResult.IsSuccess)
                                {
                                    App.config.MainConfig.User.AccessToken = refreshResult.AccessToken;

                                    launchSetting.AuthenticateUUID        = App.config.MainConfig.User.AuthenticationUUID;
                                    launchSetting.AuthenticateAccessToken = refreshResult.AccessToken;
                                }
                                else
                                {
                                    App.config.MainConfig.User.AccessToken = string.Empty;
                                    App.config.Save();
                                    await this.ShowMessageAsync(autoVerificationFailedMsg, autoVerificationFailedMsg2);

                                    return;
                                }
                            }
                        }
                        #endregion

                        #region 从零登陆
                        else
                        {
                            var loginMsgResult = await this.ShowLoginAsync(loginMsg, loginMsg2, loginDialogSettings);

                            if (loginMsgResult == null)
                            {
                                return;
                            }
                            var loader = await this.ShowProgressAsync(verifyingMsg, verifyingMsg2);

                            loader.SetIndeterminate();
                            userName = loginMsgResult.Username;
                            Authenticate authenticate = new Authenticate(new Credentials()
                            {
                                Username = loginMsgResult.Username, Password = loginMsgResult.Password
                            });
                            var aloginResult = await authenticate.PerformRequestAsync();

                            await loader.CloseAsync();

                            if (aloginResult.IsSuccess)
                            {
                                if (loginMsgResult.ShouldRemember)
                                {
                                    App.config.MainConfig.User.AccessToken = aloginResult.AccessToken;
                                }
                                App.config.MainConfig.User.AuthenticationUserData = aloginResult.User;
                                App.config.MainConfig.User.AuthenticationUUID     = aloginResult.SelectedProfile;

                                launchSetting.AuthenticateAccessToken = aloginResult.AccessToken;
                                launchSetting.AuthenticateUUID        = aloginResult.SelectedProfile;
                                launchSetting.AuthenticationUserData  = aloginResult.User;
                            }
                            else
                            {
                                await this.ShowMessageAsync(verifyingFailedMsg, verifyingFailedMsg2 + aloginResult.Error.ErrorMessage);

                                return;
                            }
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        await this.ShowMessageAsync(verifyingFailedMsg, verifyingFailedMsg2 + ex.Message);

                        return;
                    }
                }
                App.config.MainConfig.User.AuthenticationType = auth.Type;
                #endregion

                #region 检查游戏完整
                List <DownloadTask> losts = new List <DownloadTask>();

                App.logHandler.AppendInfo("检查丢失的依赖库文件中...");
                var lostDepend = await GetLost.GetLostDependDownloadTaskAsync(
                    App.config.MainConfig.Download.DownloadSource,
                    App.handler,
                    launchSetting.Version);

                if (auth.Type == Config.AuthenticationType.NIDE8)
                {
                    string nideJarPath = App.handler.GetNide8JarPath();
                    if (!File.Exists(nideJarPath))
                    {
                        lostDepend.Add(new DownloadTask("统一通行证核心", "https://login2.nide8.com:233/index/jar", nideJarPath));
                    }
                }
                if (App.config.MainConfig.Environment.DownloadLostDepend && lostDepend.Count != 0)
                {
                    MessageDialogResult downDependResult = await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.NeedDownloadDepend"),
                                                                                       App.GetResourceString("String.Mainwindow.NeedDownloadDepend2"),
                                                                                       MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, new MetroDialogSettings()
                    {
                        AffirmativeButtonText    = App.GetResourceString("String.Base.Download"),
                        NegativeButtonText       = App.GetResourceString("String.Base.Cancel"),
                        FirstAuxiliaryButtonText = App.GetResourceString("String.Base.Unremember"),
                        DefaultButtonFocus       = MessageDialogResult.Affirmative
                    });

                    switch (downDependResult)
                    {
                    case MessageDialogResult.Affirmative:
                        losts.AddRange(lostDepend);
                        break;

                    case MessageDialogResult.FirstAuxiliary:
                        App.config.MainConfig.Environment.DownloadLostDepend = false;
                        break;

                    default:
                        break;
                    }
                }

                App.logHandler.AppendInfo("检查丢失的资源文件中...");
                if (App.config.MainConfig.Environment.DownloadLostAssets && (await GetLost.IsLostAssetsAsync(App.config.MainConfig.Download.DownloadSource,
                                                                                                             App.handler, launchSetting.Version)))
                {
                    MessageDialogResult downDependResult = await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.NeedDownloadAssets"),
                                                                                       App.GetResourceString("String.Mainwindow.NeedDownloadAssets2"),
                                                                                       MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, new MetroDialogSettings()
                    {
                        AffirmativeButtonText    = App.GetResourceString("String.Base.Download"),
                        NegativeButtonText       = App.GetResourceString("String.Base.Cancel"),
                        FirstAuxiliaryButtonText = App.GetResourceString("String.Base.Unremember"),
                        DefaultButtonFocus       = MessageDialogResult.Affirmative
                    });

                    switch (downDependResult)
                    {
                    case MessageDialogResult.Affirmative:
                        var lostAssets = await GetLost.GetLostAssetsDownloadTaskAsync(
                            App.config.MainConfig.Download.DownloadSource,
                            App.handler, launchSetting.Version);

                        losts.AddRange(lostAssets);
                        break;

                    case MessageDialogResult.FirstAuxiliary:
                        App.config.MainConfig.Environment.DownloadLostAssets = false;
                        break;

                    default:
                        break;
                    }
                }

                if (losts.Count != 0)
                {
                    App.downloader.SetDownloadTasks(losts);
                    App.downloader.StartDownload();
                    await new Windows.DownloadWindow().ShowWhenDownloading();
                }

                #endregion

                #region 根据配置文件设置
                launchSetting.AdvencedGameArguments += App.config.MainConfig.Environment.AdvencedGameArguments;
                launchSetting.AdvencedJvmArguments  += App.config.MainConfig.Environment.AdvencedJvmArguments;
                launchSetting.GCArgument            += App.config.MainConfig.Environment.GCArgument;
                launchSetting.GCEnabled              = App.config.MainConfig.Environment.GCEnabled;
                launchSetting.GCType     = App.config.MainConfig.Environment.GCType;
                launchSetting.JavaAgent += App.config.MainConfig.Environment.JavaAgent;
                if (auth.Type == Config.AuthenticationType.NIDE8)
                {
                    launchSetting.JavaAgent += string.Format(" \"{0}\"={1}", App.handler.GetNide8JarPath(), App.config.MainConfig.User.Nide8ServerID);
                }

                //直连服务器设置
                if ((auth.Type == Config.AuthenticationType.NIDE8) && App.config.MainConfig.User.AllUsingNide8)
                {
                    var nide8ReturnResult = await App.nide8Handler.GetInfoAsync();

                    if (App.config.MainConfig.User.AllUsingNide8 && !string.IsNullOrWhiteSpace(nide8ReturnResult.Meta.ServerIP))
                    {
                        Server   server   = new Server();
                        string[] serverIp = nide8ReturnResult.Meta.ServerIP.Split(':');
                        if (serverIp.Length == 2)
                        {
                            server.Address = serverIp[0];
                            server.Port    = ushort.Parse(serverIp[1]);
                        }
                        else
                        {
                            server.Address = nide8ReturnResult.Meta.ServerIP;
                            server.Port    = 25565;
                        }
                        launchSetting.LaunchToServer = server;
                    }
                }
                else if (App.config.MainConfig.Server.LaunchToServer)
                {
                    launchSetting.LaunchToServer = new Server()
                    {
                        Address = App.config.MainConfig.Server.Address, Port = App.config.MainConfig.Server.Port
                    };
                }

                //自动内存设置
                if (App.config.MainConfig.Environment.AutoMemory)
                {
                    var m = SystemTools.GetBestMemory(App.handler.Java);
                    App.config.MainConfig.Environment.MaxMemory = m;
                    launchSetting.MaxMemory = m;
                }
                else
                {
                    launchSetting.MaxMemory = App.config.MainConfig.Environment.MaxMemory;
                }
                launchSetting.VersionType = App.config.MainConfig.Customize.VersionInfo;
                launchSetting.WindowSize  = App.config.MainConfig.Environment.WindowSize;
                #endregion

                #region 配置文件处理
                App.config.MainConfig.User.UserName = userName;
                App.config.Save();
                #endregion

                #region 启动

                App.logHandler.OnLog += (a, b) => { this.Invoke(() => { launchInfoBlock.Text = b.Message; }); };
                var result = await App.handler.LaunchAsync(launchSetting);

                App.logHandler.OnLog -= (a, b) => { this.Invoke(() => { launchInfoBlock.Text = b.Message; }); };

                //程序猿是找不到女朋友的了 :)
                if (!result.IsSuccess)
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.LaunchError") + result.LaunchException.Title, result.LaunchException.Message);

                    App.logHandler.AppendError(result.LaunchException);
                }
                else
                {
                    try
                    {
                        await Task.Factory.StartNew(() =>
                        {
                            result.Process.WaitForInputIdle();
                        });
                    }
                    catch (Exception ex)
                    {
                        await this.ShowMessageAsync("启动后等待游戏窗口响应异常", "这可能是由于游戏进程发生意外(闪退)导致的。具体原因:" + ex.Message);

                        return;
                    }
                    if (App.config.MainConfig.Environment.ExitAfterLaunch)
                    {
                        Application.Current.Shutdown();
                    }
                    this.WindowState = WindowState.Minimized;
                    Refresh();

                    //自定义处理
                    if (!string.IsNullOrWhiteSpace(App.config.MainConfig.Customize.GameWindowTitle))
                    {
                        GameHelper.SetGameTitle(result, App.config.MainConfig.Customize.GameWindowTitle);
                    }
                    if (App.config.MainConfig.Customize.CustomBackGroundMusic)
                    {
                        mediaElement.Volume = 0.5;
                        await Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                for (int i = 0; i < 50; i++)
                                {
                                    this.Dispatcher.Invoke(new Action(() =>
                                    {
                                        this.mediaElement.Volume -= 0.01;
                                    }));
                                    Thread.Sleep(50);
                                }
                                this.Dispatcher.Invoke(new Action(() =>
                                {
                                    this.mediaElement.Stop();
                                }));
                            }
                            catch (Exception) {}
                        });
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                App.logHandler.AppendFatal(ex);
            }
            finally
            {
                this.loadingGrid.Visibility = Visibility.Hidden;
                this.loadingRing.IsActive   = false;
            }
        }
Esempio n. 2
0
        private async Task LaunchGameFromWindow()
        {
            try
            {
                string userName = playerNameTextBox.Text;

                #region 检查有效数据
                if (authTypeCombobox.SelectedItem == null)
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Message.EmptyAuthType"),
                                                App.GetResourceString("String.Message.EmptyAuthType2"));

                    return;
                }
                if (string.IsNullOrWhiteSpace(userName))
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Message.EmptyUsername"),
                                                App.GetResourceString("String.Message.EmptyUsername2"));

                    return;
                }
                if (launchVersionCombobox.SelectedItem == null)
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Message.EmptyLaunchVersion"),
                                                App.GetResourceString("String.Message.EmptyLaunchVersion2"));

                    return;
                }
                if (App.handler.Java == null)
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Message.NoJava"), App.GetResourceString("String.Message.NoJava2"));

                    return;
                }
                #endregion

                NsisoLauncherCore.Modules.Version launchVersion = (NsisoLauncherCore.Modules.Version)launchVersionCombobox.SelectedItem;
                AuthTypeItem auth = (AuthTypeItem)authTypeCombobox.SelectedItem;

                #region 保存启动数据
                App.config.MainConfig.History.LastLaunchVersion = launchVersion.ID;
                App.config.MainConfig.User.UserName             = userName;
                #endregion

                LaunchSetting launchSetting = new LaunchSetting()
                {
                    Version = (NsisoLauncherCore.Modules.Version)launchVersionCombobox.SelectedItem
                };

                this.loadingGrid.Visibility = Visibility.Visible;
                this.loadingRing.IsActive   = true;

                #region 验证

                #region 设置ClientToken
                if (string.IsNullOrWhiteSpace(App.config.MainConfig.User.ClientToken))
                {
                    App.config.MainConfig.User.ClientToken = Guid.NewGuid().ToString("N");
                }
                else
                {
                    Requester.ClientToken = App.config.MainConfig.User.ClientToken;
                }
                #endregion

                #region 多语言支持变量(old)
                //string autoVerifyingMsg = null;
                //string autoVerifyingMsg2 = null;
                //string autoVerificationFailedMsg = null;
                //string autoVerificationFailedMsg2 = null;
                //string loginMsg = null;
                //string loginMsg2 = null;
                LoginDialogSettings loginDialogSettings = new LoginDialogSettings()
                {
                    NegativeButtonText         = App.GetResourceString("String.Base.Cancel"),
                    AffirmativeButtonText      = App.GetResourceString("String.Base.Login"),
                    RememberCheckBoxText       = App.GetResourceString("String.Base.ShouldRememberLogin"),
                    UsernameWatermark          = App.GetResourceString("String.Base.Username"),
                    InitialUsername            = userName,
                    RememberCheckBoxVisibility = Visibility,
                    EnablePasswordPreview      = true,
                    PasswordWatermark          = App.GetResourceString("String.Base.Password"),
                    NegativeButtonVisibility   = Visibility.Visible
                };
                //string verifyingMsg = null, verifyingMsg2 = null, verifyingFailedMsg = null, verifyingFailedMsg2 = null;
                #endregion

                //主验证器接口
                IAuthenticator authenticator  = null;
                bool           shouldRemember = false;

                bool isSameAuthType = App.config.MainConfig.User.AuthenticationType == auth.Type;
                bool isRemember     = (!string.IsNullOrWhiteSpace(App.config.MainConfig.User.AccessToken)) && (App.config.MainConfig.User.AuthenticationUUID != null);

                switch (auth.Type)
                {
                    #region 离线验证
                case Config.AuthenticationType.OFFLINE:
                    authenticator = new OfflineAuthenticator(userName);
                    break;
                    #endregion

                    #region Mojang验证
                case Config.AuthenticationType.MOJANG:
                    if (isSameAuthType && isRemember)
                    {
                        var mYggTokenAuthenticator = new YggdrasilTokenAuthenticator(App.config.MainConfig.User.AccessToken,
                                                                                     App.config.MainConfig.User.AuthenticationUUID,
                                                                                     App.config.MainConfig.User.AuthenticationUserData);
                        mYggTokenAuthenticator.ProxyAuthServerAddress = "https://authserver.mojang.com";
                        authenticator = mYggTokenAuthenticator;
                    }
                    else
                    {
                        var mojangLoginDResult = await this.ShowLoginAsync(App.GetResourceString("String.Mainwindow.Auth.Mojang.Login"),
                                                                           App.GetResourceString("String.Mainwindow.Auth.Mojang.Login2"),
                                                                           loginDialogSettings);

                        if (IsValidateLoginData(mojangLoginDResult))
                        {
                            var mYggAuthenticator = new YggdrasilAuthenticator(new Credentials()
                            {
                                Username = mojangLoginDResult.Username,
                                Password = mojangLoginDResult.Password
                            });
                            mYggAuthenticator.ProxyAuthServerAddress = "https://authserver.mojang.com";
                            authenticator  = mYggAuthenticator;
                            shouldRemember = mojangLoginDResult.ShouldRemember;
                        }
                        else
                        {
                            await this.ShowMessageAsync("您输入的账号或密码为空", "请检查您是否正确填写登陆信息");

                            return;
                        }
                    }
                    break;
                    #endregion

                    #region 统一通行证验证
                case Config.AuthenticationType.NIDE8:
                    if (App.nide8Handler == null)
                    {
                        await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.Auth.Nide8.NoID"),
                                                    App.GetResourceString("String.Mainwindow.Auth.Nide8.NoID2"));

                        return;
                    }
                    var nide8ChooseResult = await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.Auth.Nide8.Login2"), App.GetResourceString("String.Base.Choose"),
                                                                        MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary,
                                                                        new MetroDialogSettings()
                    {
                        AffirmativeButtonText    = App.GetResourceString("String.Base.Login"),
                        NegativeButtonText       = App.GetResourceString("String.Base.Cancel"),
                        FirstAuxiliaryButtonText = App.GetResourceString("String.Base.Register"),
                        DefaultButtonFocus       = MessageDialogResult.Affirmative
                    });

                    switch (nide8ChooseResult)
                    {
                    case MessageDialogResult.Canceled:
                        return;

                    case MessageDialogResult.Negative:
                        return;

                    case MessageDialogResult.FirstAuxiliary:
                        System.Diagnostics.Process.Start(string.Format("https://login2.nide8.com:233/{0}/register", App.nide8Handler.ServerID));
                        return;

                    case MessageDialogResult.SecondAuxiliary:
                        if (isSameAuthType && isRemember)
                        {
                            var nYggTokenCator = new Nide8TokenAuthenticator(App.config.MainConfig.User.AccessToken,
                                                                             App.config.MainConfig.User.AuthenticationUUID,
                                                                             App.config.MainConfig.User.AuthenticationUserData);
                            nYggTokenCator.ProxyAuthServerAddress = string.Format("{0}authserver", App.nide8Handler.BaseURL);
                            authenticator = nYggTokenCator;
                        }
                        else
                        {
                            var nide8LoginDResult = await this.ShowLoginAsync(App.GetResourceString("String.Mainwindow.Auth.Nide8.Login"),
                                                                              App.GetResourceString("String.Mainwindow.Auth.Nide8.Login2"),
                                                                              loginDialogSettings);

                            if (IsValidateLoginData(nide8LoginDResult))
                            {
                                var nYggCator = new Nide8Authenticator(new Credentials()
                                {
                                    Username = nide8LoginDResult.Username,
                                    Password = nide8LoginDResult.Password
                                });
                                nYggCator.ProxyAuthServerAddress = string.Format("{0}authserver", App.nide8Handler.BaseURL);
                                authenticator  = nYggCator;
                                shouldRemember = nide8LoginDResult.ShouldRemember;
                            }
                            else
                            {
                                await this.ShowMessageAsync("您输入的账号或密码为空", "请检查您是否正确填写登陆信息");

                                return;
                            }
                        }
                        break;

                    default:
                        break;
                    }
                    break;
                    #endregion

                    #region 自定义验证
                case Config.AuthenticationType.CUSTOM_SERVER:
                    string customAuthServer = App.config.MainConfig.User.AuthServer;
                    if (string.IsNullOrWhiteSpace(customAuthServer))
                    {
                        await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.Auth.Custom.NoAdrress"),
                                                    App.GetResourceString("String.Mainwindow.Auth.Custom.NoAdrress2"));

                        return;
                    }
                    else
                    {
                        if (shouldRemember && isSameAuthType)
                        {
                            var cYggTokenCator = new YggdrasilTokenAuthenticator(App.config.MainConfig.User.AccessToken,
                                                                                 App.config.MainConfig.User.AuthenticationUUID,
                                                                                 App.config.MainConfig.User.AuthenticationUserData);
                            cYggTokenCator.ProxyAuthServerAddress = customAuthServer;
                        }
                        else
                        {
                            var customLoginDResult = await this.ShowLoginAsync(App.GetResourceString("String.Mainwindow.Auth.Custom.Login"),
                                                                               App.GetResourceString("String.Mainwindow.Auth.Custom.Login2"),
                                                                               loginDialogSettings);

                            if (IsValidateLoginData(customLoginDResult))
                            {
                                var cYggAuthenticator = new YggdrasilAuthenticator(new Credentials()
                                {
                                    Username = customLoginDResult.Username,
                                    Password = customLoginDResult.Password
                                });
                                cYggAuthenticator.ProxyAuthServerAddress = customAuthServer;
                                authenticator  = cYggAuthenticator;
                                shouldRemember = customLoginDResult.ShouldRemember;
                            }
                            else
                            {
                                await this.ShowMessageAsync("您输入的账号或密码为空", "请检查您是否正确填写登陆信息");

                                return;
                            }
                        }
                    }
                    break;
                    #endregion

                default:
                    authenticator = new OfflineAuthenticator(userName);
                    break;
                }

                #region Old
                //if (auth.Type != Config.AuthenticationType.OFFLINE)
                //{
                //    try
                //    {
                //        #region 如果记住登陆(有登陆记录)
                //        if ((!string.IsNullOrWhiteSpace(App.config.MainConfig.User.AccessToken)) && (App.config.MainConfig.User.AuthenticationUUID != null))
                //        {
                //            authenticator = new YggdrasilTokenAuthenticator(App.config.MainConfig.User.AccessToken,
                //                App.config.MainConfig.User.AuthenticationUUID,
                //                App.config.MainConfig.User.AuthenticationUserData);
                //            //var loader = await this.ShowProgressAsync(autoVerifyingMsg, autoVerifyingMsg2);
                //            //loader.SetIndeterminate();
                //            //Validate validate = new Validate(App.config.MainConfig.User.AccessToken);
                //            //var validateResult = await validate.PerformRequestAsync();
                //            //if (validateResult.IsSuccess)
                //            //{

                //            //    launchSetting.AuthenticateAccessToken = App.config.MainConfig.User.AccessToken;
                //            //    launchSetting.AuthenticateUUID = App.config.MainConfig.User.AuthenticationUUID;
                //            //    await loader.CloseAsync();
                //            //}
                //            //else
                //            //{
                //            //    Refresh refresher = new Refresh(App.config.MainConfig.User.AccessToken);
                //            //    var refreshResult = await refresher.PerformRequestAsync();
                //            //    await loader.CloseAsync();
                //            //    if (refreshResult.IsSuccess)
                //            //    {
                //            //        App.config.MainConfig.User.AccessToken = refreshResult.AccessToken;

                //            //        launchSetting.AuthenticateUUID = App.config.MainConfig.User.AuthenticationUUID;
                //            //        launchSetting.AuthenticateAccessToken = refreshResult.AccessToken;
                //            //    }
                //            //    else
                //            //    {
                //            //        App.config.MainConfig.User.AccessToken = string.Empty;
                //            //        App.config.Save();
                //            //        await this.ShowMessageAsync(autoVerificationFailedMsg, autoVerificationFailedMsg2);
                //            //        return;
                //            //    }
                //            //}
                //        }
                //        #endregion

                //        #region 从零登陆
                //        else
                //        {
                //            var loginMsgResult = await this.ShowLoginAsync(loginMsg, loginMsg2, loginDialogSettings);

                //            if (loginMsgResult == null)
                //            {
                //                return;
                //            }
                //            var loader = await this.ShowProgressAsync(verifyingMsg, verifyingMsg2);
                //            loader.SetIndeterminate();
                //            userName = loginMsgResult.Username;
                //            Authenticate authenticate = new Authenticate(new Credentials() { Username = loginMsgResult.Username, Password = loginMsgResult.Password });
                //            var aloginResult = await authenticate.PerformRequestAsync();
                //            await loader.CloseAsync();
                //            if (aloginResult.IsSuccess)
                //            {
                //                if (loginMsgResult.ShouldRemember)
                //                {
                //                    App.config.MainConfig.User.AccessToken = aloginResult.AccessToken;
                //                }
                //                App.config.MainConfig.User.AuthenticationUserData = aloginResult.User;
                //                App.config.MainConfig.User.AuthenticationUUID = aloginResult.SelectedProfile;

                //                launchSetting.AuthenticateAccessToken = aloginResult.AccessToken;
                //                launchSetting.AuthenticateUUID = aloginResult.SelectedProfile;
                //                launchSetting.AuthenticationUserData = aloginResult.User;
                //            }
                //            else
                //            {
                //                await this.ShowMessageAsync(verifyingFailedMsg, verifyingFailedMsg2 + aloginResult.Error.ErrorMessage);
                //                return;
                //            }
                //        }
                //        #endregion
                //    }
                //    catch (Exception ex)
                //    {
                //        await this.ShowMessageAsync(verifyingFailedMsg, verifyingFailedMsg2 + ex.Message);
                //        return;
                //    }
                //}
                #endregion

                if (auth.Type != Config.AuthenticationType.OFFLINE)
                {
                    string currentLoginType = string.Format("正在进行{0}中...", auth.Name);
                    string loginMsg         = "这需要联网进行操作,可能需要一分钟的时间";
                    var    loader           = await this.ShowProgressAsync(currentLoginType, loginMsg);

                    loader.SetIndeterminate();
                    var authResult = await authenticator.DoAuthenticateAsync();

                    await loader.CloseAsync();

                    switch (authResult.State)
                    {
                    case AuthState.SUCCESS:
                        if (shouldRemember)
                        {
                            App.config.MainConfig.User.AccessToken            = authResult.AccessToken;
                            App.config.MainConfig.User.AuthenticationUUID     = authResult.UUID;
                            App.config.MainConfig.User.AuthenticationUserData = authResult.UserData;
                        }
                        launchSetting.AuthenticateResult = authResult;
                        break;

                    case AuthState.REQ_LOGIN:
                        await this.ShowMessageAsync("验证失败:您的登陆信息已过期",
                                                    string.Format("请您重新进行登陆。具体信息:{0}", authResult.Error.ErrorMessage));

                        return;

                    case AuthState.ERR_INVALID_CRDL:
                        await this.ShowMessageAsync("验证失败:您的登陆账号或密码错误",
                                                    string.Format("请您确认您输入的账号密码正确。具体信息:{0}", authResult.Error.ErrorMessage));

                        return;

                    case AuthState.ERR_NOTFOUND:
                        await this.ShowMessageAsync("验证失败:您的账号未找到",
                                                    string.Format("请确认您的账号和游戏角色存在。具体信息:{0}", authResult.Error.ErrorMessage));

                        return;

                    case AuthState.ERR_OTHER:
                        await this.ShowMessageAsync("验证失败:其他错误",
                                                    string.Format("具体信息:{0}", authResult.Error.ErrorMessage));

                        return;

                    case AuthState.ERR_INSIDE:
                        await this.ShowMessageAsync("验证失败:启动器内部错误",
                                                    string.Format("建议您联系启动器开发者进行解决。具体信息:{0}", authResult.Error.ErrorMessage));

                        return;

                    default:
                        await this.ShowMessageAsync("验证失败:未知错误",
                                                    "建议您联系启动器开发者进行解决。");

                        return;
                    }
                }
                else
                {
                    launchSetting.AuthenticateResult = await authenticator.DoAuthenticateAsync();
                }
                App.config.MainConfig.User.AuthenticationType = auth.Type;
                #endregion

                #region 检查游戏完整
                List <DownloadTask> losts = new List <DownloadTask>();

                App.logHandler.AppendInfo("检查丢失的依赖库文件中...");
                var lostDepend = await FileHelper.GetLostDependDownloadTaskAsync(
                    App.config.MainConfig.Download.DownloadSource,
                    App.handler,
                    launchSetting.Version);

                if (auth.Type == Config.AuthenticationType.NIDE8)
                {
                    string nideJarPath = App.handler.GetNide8JarPath();
                    if (!File.Exists(nideJarPath))
                    {
                        lostDepend.Add(new DownloadTask("统一通行证核心", "https://login2.nide8.com:233/index/jar", nideJarPath));
                    }
                }
                if (App.config.MainConfig.Environment.DownloadLostDepend && lostDepend.Count != 0)
                {
                    MessageDialogResult downDependResult = await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.NeedDownloadDepend"),
                                                                                       App.GetResourceString("String.Mainwindow.NeedDownloadDepend2"),
                                                                                       MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, new MetroDialogSettings()
                    {
                        AffirmativeButtonText    = App.GetResourceString("String.Base.Download"),
                        NegativeButtonText       = App.GetResourceString("String.Base.Cancel"),
                        FirstAuxiliaryButtonText = App.GetResourceString("String.Base.Unremember"),
                        DefaultButtonFocus       = MessageDialogResult.Affirmative
                    });

                    switch (downDependResult)
                    {
                    case MessageDialogResult.Affirmative:
                        losts.AddRange(lostDepend);
                        break;

                    case MessageDialogResult.FirstAuxiliary:
                        App.config.MainConfig.Environment.DownloadLostDepend = false;
                        break;

                    default:
                        break;
                    }
                }

                App.logHandler.AppendInfo("检查丢失的资源文件中...");
                if (App.config.MainConfig.Environment.DownloadLostAssets && (await FileHelper.IsLostAssetsAsync(App.config.MainConfig.Download.DownloadSource,
                                                                                                                App.handler, launchSetting.Version)))
                {
                    MessageDialogResult downDependResult = await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.NeedDownloadAssets"),
                                                                                       App.GetResourceString("String.Mainwindow.NeedDownloadAssets2"),
                                                                                       MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, new MetroDialogSettings()
                    {
                        AffirmativeButtonText    = App.GetResourceString("String.Base.Download"),
                        NegativeButtonText       = App.GetResourceString("String.Base.Cancel"),
                        FirstAuxiliaryButtonText = App.GetResourceString("String.Base.Unremember"),
                        DefaultButtonFocus       = MessageDialogResult.Affirmative
                    });

                    switch (downDependResult)
                    {
                    case MessageDialogResult.Affirmative:
                        var lostAssets = await FileHelper.GetLostAssetsDownloadTaskAsync(
                            App.config.MainConfig.Download.DownloadSource,
                            App.handler, launchSetting.Version);

                        losts.AddRange(lostAssets);
                        break;

                    case MessageDialogResult.FirstAuxiliary:
                        App.config.MainConfig.Environment.DownloadLostAssets = false;
                        break;

                    default:
                        break;
                    }
                }

                if (losts.Count != 0)
                {
                    App.downloader.SetDownloadTasks(losts);
                    App.downloader.StartDownload();
                    await new Windows.DownloadWindow().ShowWhenDownloading();
                }

                #endregion

                #region 根据配置文件设置
                launchSetting.AdvencedGameArguments += App.config.MainConfig.Environment.AdvencedGameArguments;
                launchSetting.AdvencedJvmArguments  += App.config.MainConfig.Environment.AdvencedJvmArguments;
                launchSetting.GCArgument            += App.config.MainConfig.Environment.GCArgument;
                launchSetting.GCEnabled              = App.config.MainConfig.Environment.GCEnabled;
                launchSetting.GCType     = App.config.MainConfig.Environment.GCType;
                launchSetting.JavaAgent += App.config.MainConfig.Environment.JavaAgent;
                if (auth.Type == Config.AuthenticationType.NIDE8)
                {
                    launchSetting.JavaAgent += string.Format(" \"{0}\"={1}", App.handler.GetNide8JarPath(), App.config.MainConfig.User.Nide8ServerID);
                }

                //直连服务器设置
                if ((auth.Type == Config.AuthenticationType.NIDE8) && App.config.MainConfig.User.AllUsingNide8)
                {
                    var nide8ReturnResult = await App.nide8Handler.GetInfoAsync();

                    if (App.config.MainConfig.User.AllUsingNide8 && !string.IsNullOrWhiteSpace(nide8ReturnResult.Meta.ServerIP))
                    {
                        Server   server   = new Server();
                        string[] serverIp = nide8ReturnResult.Meta.ServerIP.Split(':');
                        if (serverIp.Length == 2)
                        {
                            server.Address = serverIp[0];
                            server.Port    = ushort.Parse(serverIp[1]);
                        }
                        else
                        {
                            server.Address = nide8ReturnResult.Meta.ServerIP;
                            server.Port    = 25565;
                        }
                        launchSetting.LaunchToServer = server;
                    }
                }
                else if (App.config.MainConfig.Server.LaunchToServer)
                {
                    launchSetting.LaunchToServer = new Server()
                    {
                        Address = App.config.MainConfig.Server.Address, Port = App.config.MainConfig.Server.Port
                    };
                }

                //自动内存设置
                if (App.config.MainConfig.Environment.AutoMemory)
                {
                    var m = SystemTools.GetBestMemory(App.handler.Java);
                    App.config.MainConfig.Environment.MaxMemory = m;
                    launchSetting.MaxMemory = m;
                }
                else
                {
                    launchSetting.MaxMemory = App.config.MainConfig.Environment.MaxMemory;
                }
                launchSetting.VersionType = App.config.MainConfig.Customize.VersionInfo;
                launchSetting.WindowSize  = App.config.MainConfig.Environment.WindowSize;
                #endregion

                #region 配置文件处理
                App.config.Save();
                #endregion

                #region 启动

                App.logHandler.OnLog += (a, b) => { this.Invoke(() => { launchInfoBlock.Text = b.Message; }); };
                var result = await App.handler.LaunchAsync(launchSetting);

                App.logHandler.OnLog -= (a, b) => { this.Invoke(() => { launchInfoBlock.Text = b.Message; }); };

                //程序猿是找不到女朋友的了 :)
                if (!result.IsSuccess)
                {
                    await this.ShowMessageAsync(App.GetResourceString("String.Mainwindow.LaunchError") + result.LaunchException.Title, result.LaunchException.Message);

                    App.logHandler.AppendError(result.LaunchException);
                }
                else
                {
                    try
                    {
                        await Task.Factory.StartNew(() =>
                        {
                            result.Process.WaitForInputIdle();
                        });
                    }
                    catch (Exception ex)
                    {
                        await this.ShowMessageAsync("启动后等待游戏窗口响应异常", "这可能是由于游戏进程发生意外(闪退)导致的。具体原因:" + ex.Message);

                        return;
                    }
                    if (App.config.MainConfig.Environment.ExitAfterLaunch)
                    {
                        Application.Current.Shutdown();
                    }
                    this.WindowState = WindowState.Minimized;
                    Refresh();

                    //自定义处理
                    if (!string.IsNullOrWhiteSpace(App.config.MainConfig.Customize.GameWindowTitle))
                    {
                        GameHelper.SetGameTitle(result, App.config.MainConfig.Customize.GameWindowTitle);
                    }
                    if (App.config.MainConfig.Customize.CustomBackGroundMusic)
                    {
                        mediaElement.Volume = 0.5;
                        await Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                for (int i = 0; i < 50; i++)
                                {
                                    this.Dispatcher.Invoke(new Action(() =>
                                    {
                                        this.mediaElement.Volume -= 0.01;
                                    }));
                                    Thread.Sleep(50);
                                }
                                this.Dispatcher.Invoke(new Action(() =>
                                {
                                    this.mediaElement.Stop();
                                }));
                            }
                            catch (Exception) { }
                        });
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                App.logHandler.AppendFatal(ex);
            }
            finally
            {
                this.loadingGrid.Visibility = Visibility.Hidden;
                this.loadingRing.IsActive   = false;
            }
        }