From 5a516d72426d27c486d53eb37d30cc4264209fbe Mon Sep 17 00:00:00 2001 From: Mili Date: Thu, 23 Feb 2023 22:15:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=A1=86=E6=9E=B6+=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core/Api/Utility/INIHelper.cs | 17 ++++----- FunGame.Core/FunGame.Core.csproj | 1 + FunGame.Desktop/Controller/LoginController.cs | 31 +++++++++++----- FunGame.Desktop/Library/Config/Config.cs | 3 ++ FunGame.Desktop/Library/Interface/ILogin.cs | 1 + FunGame.Desktop/Model/LoginModel.cs | 31 ++++++++-------- FunGame.Desktop/Model/MainModel.cs | 10 ++++++ FunGame.Desktop/UI/Login/Login.cs | 11 ++---- FunGame.Desktop/UI/Main/Main.cs | 36 ++++++++++++++----- FunGame.Desktop/Utility/OpenForm.cs | 11 ++---- 10 files changed, 98 insertions(+), 54 deletions(-) diff --git a/FunGame.Core/Api/Utility/INIHelper.cs b/FunGame.Core/Api/Utility/INIHelper.cs index 1521215..698a1e5 100644 --- a/FunGame.Core/Api/Utility/INIHelper.cs +++ b/FunGame.Core/Api/Utility/INIHelper.cs @@ -4,15 +4,15 @@ using System.Text; namespace Milimoe.FunGame.Core.Api.Utility { - public class INIHelper + public partial class INIHelper { /* * 声明API函数 */ - [DllImport("kernel32", CharSet = CharSet.Unicode)] - private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); - [DllImport("kernel32", CharSet = CharSet.Unicode)] - private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); + [LibraryImport("kernel32", StringMarshalling = StringMarshalling.Utf16)] + private static partial long WritePrivateProfileString(string section, string key, string val, string filePath); + [LibraryImport("kernel32", StringMarshalling = StringMarshalling.Utf16)] + private static partial int GetPrivateProfileString(string section, string key, string def, byte[] val, int size, string filePath); /// /// 写入ini文件 @@ -35,9 +35,10 @@ namespace Milimoe.FunGame.Core.Api.Utility /// 读取到的值 public static string ReadINI(string Section, string Key, string FileName = @"FunGame.ini") { - StringBuilder str = new(256); - _ = GetPrivateProfileString(Section, Key, "", str, 256, Environment.CurrentDirectory.ToString() + @"\" + FileName); - return str.ToString(); + byte[] val = new byte[1024]; + _ = GetPrivateProfileString(Section, Key, "", val, 1024, Environment.CurrentDirectory.ToString() + @"\" + FileName); + string? read = val.ToString(); + return read ?? ""; } /// diff --git a/FunGame.Core/FunGame.Core.csproj b/FunGame.Core/FunGame.Core.csproj index 34de1c0..54151b5 100644 --- a/FunGame.Core/FunGame.Core.csproj +++ b/FunGame.Core/FunGame.Core.csproj @@ -12,6 +12,7 @@ ..\bin FunGame.Core Milimoe.$(MSBuildProjectName.Replace(" ", "_")) + true diff --git a/FunGame.Desktop/Controller/LoginController.cs b/FunGame.Desktop/Controller/LoginController.cs index 3d6a4f9..6eef745 100644 --- a/FunGame.Desktop/Controller/LoginController.cs +++ b/FunGame.Desktop/Controller/LoginController.cs @@ -1,40 +1,55 @@ using Milimoe.FunGame.Core.Library.Common.Event; +using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library.Interface; using Milimoe.FunGame.Desktop.Model; using Milimoe.FunGame.Desktop.UI; +using System.Windows.Forms; namespace Milimoe.FunGame.Desktop.Controller { public class LoginController : ILogin { - private LoginModel LoginModel { get; } private Login Login { get; } public LoginController(Login Login) { this.Login = Login; - LoginModel = new LoginModel(Login); } public static bool LoginAccount(params object[]? objs) { - return LoginModel.LoginAccount(objs); + RunTime.Login?.OnBeforeLoginEvent(new GeneralEventArgs()); + bool result = LoginModel.LoginAccount(objs); + if (!result) + { + RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); + } + RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs()); + return result; } public bool LoginAccount(string username, string password) { - Login.OnBeforeLoginEvent(new GeneralEventArgs()); - bool result = LoginModel.LoginAccount(username, password); + return LoginController.LoginAccount(username, password); + } + + public static bool CheckLogin(params object[]? objs) + { + bool result = LoginModel.CheckLogin(objs); if (result) { - Login.OnSucceedLoginEvent(new GeneralEventArgs()); + RunTime.Login?.OnSucceedLoginEvent(new GeneralEventArgs()); } else { - Login.OnFailedLoginEvent(new GeneralEventArgs()); + RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); } - Login.OnAfterLoginEvent(new GeneralEventArgs()); return result; } + + public bool CheckLogin(Guid key) + { + return LoginController.CheckLogin(key); + } } } diff --git a/FunGame.Desktop/Library/Config/Config.cs b/FunGame.Desktop/Library/Config/Config.cs index f4d8e7a..3eaa112 100644 --- a/FunGame.Desktop/Library/Config/Config.cs +++ b/FunGame.Desktop/Library/Config/Config.cs @@ -17,6 +17,9 @@ public static string FunGame_Roomid { get; set; } = "-1"; // 房间号 public static string FunGame_ServerName { get; set; } = ""; // 服务器名称 public static string FunGame_Notice { get; set; } = ""; // 公告 + public static string FunGame_AutoLoginUser { get; set; } = ""; // 自动登录的账号 + public static string FunGame_AutoLoginPassword { get; set; } = ""; // 自动登录的密码 + public static string FunGame_AutoLoginKey { get; set; } = ""; // 自动登录的秘钥 /*** GUID For Socket ***/ public static Guid Guid_Socket { get; set; } = Guid.Empty; diff --git a/FunGame.Desktop/Library/Interface/ILogin.cs b/FunGame.Desktop/Library/Interface/ILogin.cs index 7b49aae..70d2864 100644 --- a/FunGame.Desktop/Library/Interface/ILogin.cs +++ b/FunGame.Desktop/Library/Interface/ILogin.cs @@ -3,5 +3,6 @@ public interface ILogin { public bool LoginAccount(string username, string password); + public bool CheckLogin(Guid key); } } diff --git a/FunGame.Desktop/Model/LoginModel.cs b/FunGame.Desktop/Model/LoginModel.cs index b93e8ec..f128161 100644 --- a/FunGame.Desktop/Model/LoginModel.cs +++ b/FunGame.Desktop/Model/LoginModel.cs @@ -4,20 +4,15 @@ using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library.Interface; using Milimoe.FunGame.Desktop.UI; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; namespace Milimoe.FunGame.Desktop.Model { - public class LoginModel : ILogin + /// + /// 请不要越过Controller直接调用Model中的方法。 + /// + public class LoginModel { - private readonly Login Login; - private Core.Library.Common.Network.Socket? Socket; - - public LoginModel(Login login) - { - Login = login; - Socket = RunTime.Socket; - } - public static bool LoginAccount(params object[]? objs) { try @@ -27,9 +22,11 @@ namespace Milimoe.FunGame.Desktop.Model { string username = ""; string password = ""; + string autokey = ""; if (objs.Length > 0) username = (string)objs[0]; if (objs.Length > 1) password = (string)objs[1]; - if (Socket.Send(SocketMessageType.Login, username, password) == SocketResult.Success) + if (objs.Length > 2) autokey = (string)objs[2]; + if (Socket.Send(SocketMessageType.Login, username, password, autokey) == SocketResult.Success) { return true; } @@ -42,13 +39,19 @@ namespace Milimoe.FunGame.Desktop.Model return false; } - public bool LoginAccount(string username, string password) + public static bool CheckLogin(params object[]? objs) { try { - if (LoginModel.LoginAccount(username, password)) + Core.Library.Common.Network.Socket? Socket = RunTime.Socket; + if (Socket != null && objs != null) { - return true; + 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) diff --git a/FunGame.Desktop/Model/MainModel.cs b/FunGame.Desktop/Model/MainModel.cs index 1b23df1..3f3d1db 100644 --- a/FunGame.Desktop/Model/MainModel.cs +++ b/FunGame.Desktop/Model/MainModel.cs @@ -3,6 +3,7 @@ using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; +using Milimoe.FunGame.Desktop.Controller; using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library.Component; using Milimoe.FunGame.Desktop.Library.Interface; @@ -263,6 +264,7 @@ namespace Milimoe.FunGame.Desktop.Model break; case SocketMessageType.Login: + SocketHandler_Login(objs); break; case SocketMessageType.CheckLogin: @@ -319,6 +321,14 @@ namespace Milimoe.FunGame.Desktop.Model if (objs.Length > 0) Config.FunGame_Notice = NetworkUtility.ConvertJsonObject(objs[0])!; } + private void SocketHandler_Login(object[] objs) + { + Guid key = Guid.Empty; + // 返回一个Key,再发回去给服务器就行了 + if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject(objs[0])!; + LoginController.CheckLogin(key); + } + private void SocketHandler_CheckLogin(object[] objs) { string msg = ""; diff --git a/FunGame.Desktop/UI/Login/Login.cs b/FunGame.Desktop/UI/Login/Login.cs index 9c8d63f..bc977ca 100644 --- a/FunGame.Desktop/UI/Login/Login.cs +++ b/FunGame.Desktop/UI/Login/Login.cs @@ -11,12 +11,10 @@ namespace Milimoe.FunGame.Desktop.UI public partial class Login : BaseLogin { private LoginController LoginController; - private Main Main; - public Login(Main Main) + public Login() { InitializeComponent(); - this.Main = Main; LoginController = new LoginController(this); } @@ -33,11 +31,8 @@ namespace Milimoe.FunGame.Desktop.UI return; } password = Core.Api.Utility.Encryption.HmacSha512(password, username); - if (LoginController.LoginAccount(username, password)) - { - Main.UpdateUI(MainSet.LogIn, new object[] { Core.Api.Utility.Factory.NewSingle(username, password) }); - } - else ShowMessage.Message("登录失败!!", "登录失败"); + if (!LoginController.LoginAccount(username, password)) + ShowMessage.Message("登录失败!!", "登录失败"); } catch (Exception e) { diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 34852c2..21b0759 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -269,14 +269,31 @@ namespace Milimoe.FunGame.Desktop.UI { if (INIHelper.ExistINIFile()) { - string isAutoConncet = INIHelper.ReadINI("Config", "AutoConnect"); + string isAutoConnect = INIHelper.ReadINI("Config", "AutoConnect"); string isAutoLogin = INIHelper.ReadINI("Config", "AutoLogin"); - if (isAutoConncet != null && !isAutoConncet.Equals("") && (isAutoConncet.Equals("false") || isAutoConncet.Equals("true"))) - Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConncet); + string strUserName = INIHelper.ReadINI("Config", "UserName"); + string strPassword = INIHelper.ReadINI("Config", "Password"); + string strAutoKey = INIHelper.ReadINI("Config", "AutoKey"); + + if (isAutoConnect != null && isAutoConnect.Trim() != "" && (isAutoConnect.ToLower().Equals("false") || isAutoConnect.ToLower().Equals("true"))) + Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConnect); else throw new ReadConfigException(); - if (isAutoLogin != null && !isAutoLogin.Equals("") && (isAutoLogin.Equals("false") || isAutoLogin.Equals("true"))) + + if (isAutoLogin != null && isAutoLogin.Trim() != "" && (isAutoLogin.ToLower().Equals("false") || isAutoLogin.ToLower().Equals("true"))) Config.FunGame_isAutoLogin = Convert.ToBoolean(isAutoLogin); else throw new ReadConfigException(); + + if (strUserName != null && strUserName.Trim() != "") + Config.FunGame_AutoLoginUser = strUserName.Trim(); + else throw new ReadConfigException(); + + if (strPassword != null && strPassword.Trim() != "") + Config.FunGame_AutoLoginPassword = strPassword.Trim(); + else throw new ReadConfigException(); + + if (strAutoKey != null && strAutoKey.Trim() != "") + Config.FunGame_AutoLoginKey = strAutoKey.Trim(); + else throw new ReadConfigException(); } else { @@ -613,7 +630,10 @@ namespace Milimoe.FunGame.Desktop.UI Login.Visible = false; Logout.Visible = true; SetServerStatusLight((int)LightType.Green); - ShowMessage.Message($"欢迎回来,{Usercfg.LoginUserName}!", "登录成功", 5); + RunTime.Login?.Dispose(); + string welcome = $"欢迎回来, {Usercfg.LoginUserName}!"; + ShowMessage.Message(welcome, "登录成功", 5); + WritelnSystemInfo(welcome); } /// @@ -944,7 +964,7 @@ namespace Milimoe.FunGame.Desktop.UI private void Login_Click(object sender, EventArgs e) { if (MainController != null && Config.FunGame_isConnected) - OpenForm.SingleForm(FormType.Login, OpenFormType.Dialog, this); + OpenForm.SingleForm(FormType.Login, OpenFormType.Dialog); else ShowMessage.WarningMessage("请先连接服务器!"); } @@ -1155,8 +1175,8 @@ namespace Milimoe.FunGame.Desktop.UI { if (MainController != null && Config.FunGame_isAutoLogin) { - // 自动登录 [TODO] - + // 自动登录 + LoginController.LoginAccount(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey); } return EventResult.Success; } diff --git a/FunGame.Desktop/Utility/OpenForm.cs b/FunGame.Desktop/Utility/OpenForm.cs index adc80ef..c8d406e 100644 --- a/FunGame.Desktop/Utility/OpenForm.cs +++ b/FunGame.Desktop/Utility/OpenForm.cs @@ -29,14 +29,9 @@ namespace Milimoe.FunGame.Desktop.Utility RunTime.Register = (Register)form; break; case FormType.Login: - Main? main = default; - if (objs != null && objs.Length > 0) main = (Main)objs[0]; - if (main != null) - { - form = new Login(main); - IsExist = RunTime.Login != null; - RunTime.Login = (Login)form; - } + form = new Login(); + IsExist = RunTime.Login != null; + RunTime.Login = (Login)form; break; case FormType.Inventory: form = new InventoryUI();