From 255e76386cbb42ba7dd92d3cefde2d20fedd35b2 Mon Sep 17 00:00:00 2001 From: Mili Date: Wed, 29 Mar 2023 22:32:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8Async/Await=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core/Library/Constant/TypeEnum.cs | 1 + FunGame.Desktop/Controller/LoginController.cs | 27 +++- .../Controller/RunTimeController.cs | 12 ++ FunGame.Desktop/Model/LoginModel.cs | 127 ++++++++---------- FunGame.Desktop/UI/Login/Login.cs | 13 +- FunGame.Desktop/UI/Main/Main.cs | 2 +- FunGame.Desktop/UI/Register/Register.cs | 2 +- 7 files changed, 92 insertions(+), 92 deletions(-) diff --git a/FunGame.Core/Library/Constant/TypeEnum.cs b/FunGame.Core/Library/Constant/TypeEnum.cs index 8d1654d..9f68b6b 100644 --- a/FunGame.Core/Library/Constant/TypeEnum.cs +++ b/FunGame.Core/Library/Constant/TypeEnum.cs @@ -237,6 +237,7 @@ Connected, Disconnect, Disconnected, + AutoLogin, Close } diff --git a/FunGame.Desktop/Controller/LoginController.cs b/FunGame.Desktop/Controller/LoginController.cs index 9b7f331..2ca0ee9 100644 --- a/FunGame.Desktop/Controller/LoginController.cs +++ b/FunGame.Desktop/Controller/LoginController.cs @@ -2,6 +2,7 @@ using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Model; +using Milimoe.FunGame.Core.Library.Exception; namespace Milimoe.FunGame.Desktop.Controller { @@ -19,14 +20,26 @@ namespace Milimoe.FunGame.Desktop.Controller LoginModel.Dispose(); } - public static bool LoginAccount(params object[]? objs) + public static async Task LoginAccount(params object[]? objs) { - LoginEventArgs LoginEventArgs = new(objs); - if (RunTime.Login?.OnBeforeLoginEvent(LoginEventArgs) == Core.Library.Constant.EventResult.Fail) return false; - bool result = LoginModel.LoginAccountAsync(objs).Result; - if (result) RunTime.Login?.OnSucceedLoginEvent(LoginEventArgs); - else RunTime.Login?.OnFailedLoginEvent(LoginEventArgs); - RunTime.Login?.OnAfterLoginEvent(LoginEventArgs); + bool result = false; + + try + { + LoginEventArgs LoginEventArgs = new(objs); + if (RunTime.Login?.OnBeforeLoginEvent(LoginEventArgs) == Core.Library.Constant.EventResult.Fail) return false; + + result = await LoginModel.LoginAccountAsync(objs); + + if (result) RunTime.Login?.OnSucceedLoginEvent(LoginEventArgs); + else RunTime.Login?.OnFailedLoginEvent(LoginEventArgs); + RunTime.Login?.OnAfterLoginEvent(LoginEventArgs); + } + catch (Exception e) + { + RunTime.WritelnSystemInfo(e.GetErrorInfo()); + } + return result; } } diff --git a/FunGame.Desktop/Controller/RunTimeController.cs b/FunGame.Desktop/Controller/RunTimeController.cs index 4decc72..7b10f2d 100644 --- a/FunGame.Desktop/Controller/RunTimeController.cs +++ b/FunGame.Desktop/Controller/RunTimeController.cs @@ -1,5 +1,6 @@ using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Model; using Milimoe.FunGame.Desktop.UI; @@ -50,6 +51,9 @@ namespace Milimoe.FunGame.Desktop.Controller case RunTimeInvokeType.Disconnected: break; + + case RunTimeInvokeType.AutoLogin: + break; case RunTimeInvokeType.Close: if (args != null && args.Length > 0) @@ -96,5 +100,13 @@ namespace Milimoe.FunGame.Desktop.Controller { return Do(RunTimeInvokeType.Close, e); } + + public async void AutoLogin(params object[] objs) + { + Do(RunTimeInvokeType.AutoLogin); + LoginController LoginController = new(); + await LoginController.LoginAccount(objs); + LoginController.Dispose(); + } } } diff --git a/FunGame.Desktop/Model/LoginModel.cs b/FunGame.Desktop/Model/LoginModel.cs index 1a42cf5..956339e 100644 --- a/FunGame.Desktop/Model/LoginModel.cs +++ b/FunGame.Desktop/Model/LoginModel.cs @@ -1,4 +1,5 @@ using System.Data; +using System.Windows.Forms; using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Common.Architecture; @@ -55,17 +56,25 @@ namespace Milimoe.FunGame.Desktop.Model if (Socket.Send(SocketMessageType.Login, username, password, autokey) == SocketResult.Success) { SetWorking(); - await Task.Factory.StartNew(async() => + string ErrorMsg = ""; + Guid CheckLoginKey = Guid.Empty; + (CheckLoginKey, ErrorMsg) = await Task.Factory.StartNew(GetCheckLoginKeyAsync); + if (ErrorMsg != null && ErrorMsg.Trim() != "") { - while (true) + ShowMessage.ErrorMessage(ErrorMsg, "登录失败"); + return false; + } + if (Socket.Send(SocketMessageType.CheckLogin, CheckLoginKey) == SocketResult.Success) + { + SetWorking(); + DataSet ds = await Task.Factory.StartNew(GetLoginUserAsync); + if (ds != null) { - if (!Working) break; + // 创建User对象并返回到Main + RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.GetInstance(ds) }); + return true; } - if (Success) - { - await CheckLoginAsync(Work); - } - }); + } } } } @@ -77,86 +86,56 @@ namespace Milimoe.FunGame.Desktop.Model return false; } - public static bool CheckLogin(params object[]? objs) - { - try - { - Socket? Socket = RunTime.Socket; - if (Socket != null && objs != null) - { - Guid key = Guid.Empty; - if (objs.Length > 0) key = (Guid)objs[0]; - if (Socket.Send(SocketMessageType.CheckLogin, key) == SocketResult.Success) - { - return true; - } - } - } - catch (Exception e) - { - RunTime.WritelnSystemInfo(e.GetErrorInfo()); - } - return false; - } - - private static async Task CheckLoginAsync(SocketObject SocketObject) + public static (Guid, string) GetCheckLoginKeyAsync() { Guid key = Guid.Empty; string? msg = ""; - // 返回一个Key,再发回去给服务器就行了 - if (SocketObject.Length > 0) key = SocketObject.GetParam(0); - if (SocketObject.Length > 1) msg = SocketObject.GetParam(1); - // 如果返回了msg,说明验证错误。 - if (msg != null && msg.Trim() != "") + try { - ShowMessage.ErrorMessage(msg, "登录失败"); - return false; - } - else - { - if (key != Guid.Empty) + while (true) { - Config.Guid_LoginKey = key; - Socket Socket = RunTime.Socket!; - if (Socket.Send(SocketMessageType.CheckLogin, key) == SocketResult.Success) + if (!Working) break; + } + if (Success) + { + // 返回一个确认登录的Key + if (Work.Length > 0) key = Work.GetParam(0); + if (Work.Length > 1) msg = Work.GetParam(1); + if (key != Guid.Empty) { - SetWorking(); - await Task.Factory.StartNew(() => - { - while (true) - { - if (!Working) break; - } - if (Success) - { - SocketHandler_CheckLogin(Work); - } - }); - return true; + Config.Guid_LoginKey = key; } } - else - { - ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); - return false; - } } - return true; + catch (Exception e) + { + RunTime.WritelnSystemInfo(e.GetErrorInfo()); + } + msg ??= ""; + return (key, msg); } - private static bool SocketHandler_CheckLogin(SocketObject SocketObject) + private static DataSet GetLoginUserAsync() { - // 返回构造User对象的DataTable - object[] objs = SocketObject.Parameters; - if (objs != null && objs.Length > 0) + DataSet? ds = new(); + try { - DataSet? DataSet = SocketObject.GetParam(0); - // 创建User对象并返回到Main - RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.GetInstance(DataSet) }); - return true; + while (true) + { + if (!Working) break; + } + if (Success) + { + // 返回构造User对象的DataTable + if (Work.Length > 0) ds = Work.GetParam(0); + } } - ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); - return false; + catch (Exception e) + { + RunTime.WritelnSystemInfo(e.GetErrorInfo()); + } + ds ??= new(); + return ds; } private static void SetWorking() diff --git a/FunGame.Desktop/UI/Login/Login.cs b/FunGame.Desktop/UI/Login/Login.cs index f496d74..047f503 100644 --- a/FunGame.Desktop/UI/Login/Login.cs +++ b/FunGame.Desktop/UI/Login/Login.cs @@ -34,7 +34,7 @@ namespace Milimoe.FunGame.Desktop.UI LoginController.Dispose(); } - private bool Login_Handler() + private async Task Login_Handler() { try { @@ -46,18 +46,13 @@ namespace Milimoe.FunGame.Desktop.UI UsernameText.Focus(); return false; } - if (!LoginController.LoginAccount(username, password)) - { - ShowMessage.Message("登录失败!!", "登录失败"); - return false; - } + return await LoginController.LoginAccount(username, password); } catch (Exception e) { RunTime.WritelnSystemInfo(e.GetErrorInfo()); return false; } - return true; } /// @@ -75,10 +70,10 @@ namespace Milimoe.FunGame.Desktop.UI ShowMessage.TipMessage("与No.16对话即可获得快速登录秘钥,快去试试吧!"); } - private void GoToLogin_Click(object sender, EventArgs e) + private async void GoToLogin_Click(object sender, EventArgs e) { GoToLogin.Enabled = false; - if (!Login_Handler()) GoToLogin.Enabled = true; + if (await Login_Handler() == false) GoToLogin.Enabled = true; } private void ForgetPassword_Click(object sender, EventArgs e) diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index b14c5bf..cd8f376 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -1204,7 +1204,7 @@ namespace Milimoe.FunGame.Desktop.UI if (MainController != null && Config.FunGame_isAutoLogin && Config.FunGame_AutoLoginUser != "" && Config.FunGame_AutoLoginPassword != "" && Config.FunGame_AutoLoginKey != "") { // 自动登录 - LoginController.LoginAccount(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey); + RunTime.Connector?.AutoLogin(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey); } return EventResult.Success; } diff --git a/FunGame.Desktop/UI/Register/Register.cs b/FunGame.Desktop/UI/Register/Register.cs index 1f1b7e4..c1e0428 100644 --- a/FunGame.Desktop/UI/Register/Register.cs +++ b/FunGame.Desktop/UI/Register/Register.cs @@ -165,7 +165,7 @@ namespace Milimoe.FunGame.Desktop.UI { string username = ((RegisterEventArgs)e).Username; string password = ((RegisterEventArgs)e).Password; - LoginController.LoginAccount(username, password); + RunTime.Connector?.AutoLogin(username, password); return EventResult.Success; }