From fc43f91ad23605785d3905d12e6f03bd52df022a Mon Sep 17 00:00:00 2001 From: Mili Date: Sat, 25 Feb 2023 00:11:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0checklogin=E5=90=8E=E7=BB=AD?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E3=80=81showmessage,=20factory=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core/Api/Utility/Factory.cs | 27 ++++++++++++++- FunGame.Core/Api/Utility/General.cs | 31 ++++++++++++----- ...ceptionHelper.cs => ExceptionExtension.cs} | 2 +- FunGame.Desktop/Controller/LoginController.cs | 10 +++--- .../Library/Component/ShowMessage.cs | 33 ++++++++++--------- FunGame.Desktop/Model/MainModel.cs | 23 +++++++++---- FunGame.Desktop/UI/Login/Login.cs | 5 +-- FunGame.Desktop/UI/Main/Main.cs | 6 ++-- 8 files changed, 93 insertions(+), 44 deletions(-) rename FunGame.Core/Library/Exception/{ExceptionHelper.cs => ExceptionExtension.cs} (85%) diff --git a/FunGame.Core/Api/Utility/Factory.cs b/FunGame.Core/Api/Utility/Factory.cs index cb5278c..b037cc8 100644 --- a/FunGame.Core/Api/Utility/Factory.cs +++ b/FunGame.Core/Api/Utility/Factory.cs @@ -43,7 +43,7 @@ namespace Milimoe.FunGame.Core.Api.Utility if (objs is null || objs.Length == 0) return (T)instance; if (typeof(T) == typeof(Entity.User)) { - instance = Api.Factory.UserFactory.GetInstance("Mili"); + instance = GetUser(objs); } else if (typeof(T) == typeof(Skill)) { @@ -90,5 +90,30 @@ namespace Milimoe.FunGame.Core.Api.Utility return true; return false; } + + private static User GetUser(params object[] objs) + { + string username = "", password = ""; + object objTemp; + if (objs.Length > 0) + { + objTemp = objs[0]; + if (objTemp.GetType() != typeof(string)) + { + username = NetworkUtility.ConvertJsonObject(objTemp) ?? ""; + } + else username = (string)objs[0]; + } + if (objs.Length > 1) + { + objTemp = objs[1]; + if (objTemp.GetType() != typeof(string)) + { + password = NetworkUtility.ConvertJsonObject(objTemp) ?? ""; + } + else password = (string)objs[1]; + } + return Api.Factory.UserFactory.GetInstance(username, password); + } } } diff --git a/FunGame.Core/Api/Utility/General.cs b/FunGame.Core/Api/Utility/General.cs index d5fa8bd..f46aa03 100644 --- a/FunGame.Core/Api/Utility/General.cs +++ b/FunGame.Core/Api/Utility/General.cs @@ -91,7 +91,7 @@ namespace Milimoe.FunGame.Core.Api.Utility DontFragment = true }; string data = "getserverping"; - byte[] buffer = Encoding.ASCII.GetBytes(data); + byte[] buffer = General.DefaultEncoding.GetBytes(data); int timeout = 120; PingReply reply = pingSender.Send(addr, timeout, buffer, options); if (reply.Status == IPStatus.Success) @@ -116,7 +116,7 @@ namespace Milimoe.FunGame.Core.Api.Utility /// /// Json工具类 /// - public static class JsonUtility + internal static class JsonUtility { /// /// 将JsonElement转换为泛型 @@ -124,10 +124,9 @@ namespace Milimoe.FunGame.Core.Api.Utility /// 泛型 /// JsonElement /// - public static T? ToObject(this JsonElement element) + internal static T? ToObject(this JsonElement element) { - var json = element.GetRawText(); - return JsonSerializer.Deserialize(json); + return JsonSerializer.Deserialize(element.GetRawText()); } } @@ -222,7 +221,7 @@ namespace Milimoe.FunGame.Core.Api.Utility /// /// 使用HMACSHA512算法加密 /// - public class Encryption + internal class Encryption { /// /// 使用HMACSHA512算法加密 @@ -230,15 +229,29 @@ namespace Milimoe.FunGame.Core.Api.Utility /// 需要加密的值 /// 秘钥 /// - public static string HmacSha512(string Message, string Key) + internal static string HmacSha512(string Message, string Key) { byte[] MessageBytes = General.DefaultEncoding.GetBytes(Message); Key = Convert.ToBase64String(General.DefaultEncoding.GetBytes(Key)); byte[] KeyBytes = General.DefaultEncoding.GetBytes(Key); HMACSHA512 Hmacsha512 = new(KeyBytes); byte[] Hash = Hmacsha512.ComputeHash(MessageBytes); - string Hamc = BitConverter.ToString(Hash).Replace("-", ""); - return Hamc.ToLower(); + string Hmac = BitConverter.ToString(Hash).Replace("-", ""); + return Hmac.ToLower(); + } + } + + public static class StringExtension + { + /// + /// 使用HMACSHA512算法加密 + /// + /// 需要加密的值 + /// 秘钥 + /// + public static string Encrypt(this string msg, string key) + { + return Encryption.HmacSha512(msg, Encryption.HmacSha512(msg, key)); } } diff --git a/FunGame.Core/Library/Exception/ExceptionHelper.cs b/FunGame.Core/Library/Exception/ExceptionExtension.cs similarity index 85% rename from FunGame.Core/Library/Exception/ExceptionHelper.cs rename to FunGame.Core/Library/Exception/ExceptionExtension.cs index 9b54770..fcb4728 100644 --- a/FunGame.Core/Library/Exception/ExceptionHelper.cs +++ b/FunGame.Core/Library/Exception/ExceptionExtension.cs @@ -1,6 +1,6 @@ namespace Milimoe.FunGame.Core.Library.Exception { - public static class ExceptionHelper + public static class ExceptionExtension { public static string GetErrorInfo(this System.Exception e) { diff --git a/FunGame.Desktop/Controller/LoginController.cs b/FunGame.Desktop/Controller/LoginController.cs index da3deb6..1c76276 100644 --- a/FunGame.Desktop/Controller/LoginController.cs +++ b/FunGame.Desktop/Controller/LoginController.cs @@ -1,5 +1,6 @@ using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Desktop.Library; +using Milimoe.FunGame.Desktop.Library.Component; using Milimoe.FunGame.Desktop.Model; namespace Milimoe.FunGame.Desktop.Controller @@ -12,21 +13,18 @@ namespace Milimoe.FunGame.Desktop.Controller bool result = LoginModel.LoginAccount(objs); if (!result) { + ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); } - RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs()); return result; } public static bool CheckLogin(params object[]? objs) { bool result = LoginModel.CheckLogin(objs); - if (result) - { - RunTime.Login?.OnSucceedLoginEvent(new GeneralEventArgs()); - } - else + if (!result) { + ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); } return result; diff --git a/FunGame.Desktop/Library/Component/ShowMessage.cs b/FunGame.Desktop/Library/Component/ShowMessage.cs index 6e005f7..2f830da 100644 --- a/FunGame.Desktop/Library/Component/ShowMessage.cs +++ b/FunGame.Desktop/Library/Component/ShowMessage.cs @@ -6,7 +6,8 @@ namespace Milimoe.FunGame.Desktop.Library.Component { private MessageResult MessageResult = MessageResult.Cancel; private string InputResult = ""; - private int AutoClose = 0; + private readonly int AutoClose = 0; + private readonly Task? TaskAutoClose; private const string TITLE_TIP = "提示"; private const string TITLE_WARNING = "警告"; @@ -112,29 +113,23 @@ namespace Milimoe.FunGame.Desktop.Library.Component } if (AutoClose > 0) { - Action action = new(() => + TaskAutoClose = Task.Factory.StartNew(() => { + Thread.Sleep(100); string msg = MsgText.Text; - MsgText.Text = msg + "\n[ " + AutoClose + " 秒后自动关闭 ]"; - while (AutoClose > 0) + int s = AutoClose; + BeginInvoke(() => ChangeSecond(msg, s)); + while (s > 0) { Thread.Sleep(1000); - AutoClose--; - MsgText.Text = msg + "\n[ " + AutoClose + " 秒后自动关闭 ]"; + s--; + if (IsHandleCreated) BeginInvoke(() => ChangeSecond(msg, s)); } MessageResult = MessageResult.OK; - Dispose(); + Close(); }); - Task.Run(() => - { - if (InvokeRequired) - Invoke(action); - else - action(); - }); - Show(); } - else ShowDialog(); + ShowDialog(); } /// @@ -152,6 +147,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component BUTTON_RETRY => MessageResult.Retry, _ => MessageResult.Cancel }; + TaskAutoClose?.Wait(1); Dispose(); } @@ -211,6 +207,11 @@ namespace Milimoe.FunGame.Desktop.Library.Component return result; } + private void ChangeSecond(string msg, int s) + { + MsgText.Text = msg + "\n[ " + s + " 秒后自动关闭 ]"; + } + private void LeftButton_Click(object sender, EventArgs e) { SetButtonResult(LeftButton.Text); diff --git a/FunGame.Desktop/Model/MainModel.cs b/FunGame.Desktop/Model/MainModel.cs index 3f3d1db..e7cfc49 100644 --- a/FunGame.Desktop/Model/MainModel.cs +++ b/FunGame.Desktop/Model/MainModel.cs @@ -251,7 +251,6 @@ namespace Milimoe.FunGame.Desktop.Model object[] ServerMessage = GetServerMessage(); SocketMessageType type = (SocketMessageType)ServerMessage[0]; object[] objs = (object[])ServerMessage[1]; - result = type; switch (type) { @@ -269,6 +268,7 @@ namespace Milimoe.FunGame.Desktop.Model case SocketMessageType.CheckLogin: SocketHandler_CheckLogin(objs); + RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs()); break; case SocketMessageType.Logout: @@ -326,16 +326,27 @@ namespace Milimoe.FunGame.Desktop.Model Guid key = Guid.Empty; // 返回一个Key,再发回去给服务器就行了 if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject(objs[0])!; - LoginController.CheckLogin(key); + if (key != Guid.Empty) LoginController.CheckLogin(key); + else + { + ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); + RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); + RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs()); + } } private void SocketHandler_CheckLogin(object[] objs) { - string msg = ""; // 返回的objs是该Login的User对象的各个属性 - if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject(objs[0])!; - Main.GetMessage(msg); - Main.UpdateUI(MainSet.SetUser, new object[] { Factory.New(msg) }); + if (objs != null && objs.Length > 0) + { + // 创建User对象并返回到Main + Main.UpdateUI(MainSet.SetUser, new object[] { Factory.New(objs) }); + RunTime.Login?.OnSucceedLoginEvent(new GeneralEventArgs()); + return; + } + ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); + RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); } private void SocketHandler_Disconnect(object[] objs) diff --git a/FunGame.Desktop/UI/Login/Login.cs b/FunGame.Desktop/UI/Login/Login.cs index 44a0d3a..84f378e 100644 --- a/FunGame.Desktop/UI/Login/Login.cs +++ b/FunGame.Desktop/UI/Login/Login.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Library.Exception; +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Desktop.Controller; using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library.Base; @@ -26,7 +27,7 @@ namespace Milimoe.FunGame.Desktop.UI UsernameText.Focus(); return; } - password = Core.Api.Utility.Encryption.HmacSha512(password, username); + password = password.Encrypt(username); if (!LoginController.LoginAccount(username, password)) ShowMessage.Message("登录失败!!", "登录失败"); } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 0a524a4..c815c37 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -173,7 +173,6 @@ namespace Milimoe.FunGame.Desktop.UI break; case MainSet.LogIn: - LoginAccount(objs); break; case MainSet.LogOut: @@ -627,7 +626,8 @@ namespace Milimoe.FunGame.Desktop.UI Login.Visible = false; Logout.Visible = true; SetServerStatusLight((int)LightType.Green); - RunTime.Login?.Dispose(); + RunTime.Login?.Close(); + Thread.Sleep(100); string welcome = $"欢迎回来, {Usercfg.LoginUserName}!"; ShowMessage.Message(welcome, "登录成功", 5); WritelnSystemInfo(welcome); @@ -1170,7 +1170,7 @@ namespace Milimoe.FunGame.Desktop.UI /// public EventResult SucceedConnectEvent(object sender, GeneralEventArgs e) { - if (MainController != null && Config.FunGame_isAutoLogin) + 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);