From 34379df88618ff283d6d4da5630bdcd8337ba2c8 Mon Sep 17 00:00:00 2001 From: Mili Date: Sat, 25 Feb 2023 18:15:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core/Api/Utility/General.cs | 5 +- FunGame.Core/Api/Utility/INIHelper.cs | 1 - FunGame.Core/Library/Constant/TypeEnum.cs | 17 +-- FunGame.Core/Library/Exception/Exception.cs | 48 ++++---- FunGame.Desktop/Controller/MainController.cs | 14 +-- .../Library/Component/GeneralForm.Designer.cs | 1 + .../Library/Component/GeneralForm.cs | 10 ++ .../Library/Component/ShowMessage.cs | 2 +- FunGame.Desktop/Library/Config/Config.cs | 1 + FunGame.Desktop/Model/LoginModel.cs | 4 +- FunGame.Desktop/Model/MainModel.cs | 106 +++++++++++++++--- FunGame.Desktop/UI/Login/Login.cs | 28 ++++- FunGame.Desktop/UI/Main/Main.cs | 80 ++++++++----- 13 files changed, 230 insertions(+), 87 deletions(-) diff --git a/FunGame.Core/Api/Utility/General.cs b/FunGame.Core/Api/Utility/General.cs index f46aa03..3001045 100644 --- a/FunGame.Core/Api/Utility/General.cs +++ b/FunGame.Core/Api/Utility/General.cs @@ -1,9 +1,8 @@ -using System.Net.NetworkInformation; +using Milimoe.FunGame.Core.Library.Constant; +using System.Net.NetworkInformation; using System.Security.Cryptography; -using System.Text; using System.Text.Json; using System.Text.RegularExpressions; -using Milimoe.FunGame.Core.Library.Constant; // 通用工具类,客户端和服务器端都可以直接调用的工具方法都可以写在这里 namespace Milimoe.FunGame.Core.Api.Utility diff --git a/FunGame.Core/Api/Utility/INIHelper.cs b/FunGame.Core/Api/Utility/INIHelper.cs index 362304c..8b8cb08 100644 --- a/FunGame.Core/Api/Utility/INIHelper.cs +++ b/FunGame.Core/Api/Utility/INIHelper.cs @@ -1,6 +1,5 @@ using Milimoe.FunGame.Core.Library.Constant; using System.Runtime.InteropServices; -using System.Text; namespace Milimoe.FunGame.Core.Api.Utility { diff --git a/FunGame.Core/Library/Constant/TypeEnum.cs b/FunGame.Core/Library/Constant/TypeEnum.cs index 657f829..27094d8 100644 --- a/FunGame.Core/Library/Constant/TypeEnum.cs +++ b/FunGame.Core/Library/Constant/TypeEnum.cs @@ -47,16 +47,17 @@ Red } + [Flags] public enum SocketMessageType { - Unknown, - Connect, - GetNotice, - Login, - CheckLogin, - Logout, - Disconnect, - HeartBeat + Unknown = 0, + Connect = 1 << 0, + GetNotice = 1 << 1, + Login = 1 << 2, + CheckLogin = 1 << 4, + Logout = 1 << 5, + Disconnect = 1 << 6, + HeartBeat = 1 << 7 } public enum SocketRuntimeType diff --git a/FunGame.Core/Library/Exception/Exception.cs b/FunGame.Core/Library/Exception/Exception.cs index 0fad8a8..9a3b751 100644 --- a/FunGame.Core/Library/Exception/Exception.cs +++ b/FunGame.Core/Library/Exception/Exception.cs @@ -2,96 +2,106 @@ { public class SystemError : Exception { - public override string Message => "系统错误 (#00001)"; + public override string Message => "系统错误 (#10001)"; } public class CanNotConnectException : Exception { - public override string Message => "无法连接至服务器,请检查网络并重启游戏再试 (#00002)"; + public override string Message => "无法连接至服务器,请检查网络并重启游戏再试 (#10002)"; } public class TimeOutException : Exception { - public override string Message => "连接超时 (#00003)"; + public override string Message => "连接超时 (#10003)"; } public class UnknownException : Exception { - public override string Message => "未知错误 (#00004)"; + public override string Message => "未知错误 (#10004)"; } public class ReadConfigException : Exception { - public override string Message => "读取配置文件出错,参数格式不正确 (#00005)"; + public override string Message => "读取配置文件出错,参数格式不正确 (#10005)"; } public class SingletonAddException : Exception { - public override string Message => "添加单例到单例表时遇到错误 (#00006)"; + public override string Message => "添加单例到单例表时遇到错误 (#10006)"; } public class SingletonGetException : Exception { - public override string Message => "不能从单例表中获取到指定的单例 (#00007)"; + public override string Message => "不能从单例表中获取到指定的单例 (#10007)"; } public class SocketWrongInfoException : Exception { - public override string Message => "收到错误的返回信息 (#00008)"; + public override string Message => "收到错误的返回信息 (#10008)"; } public class SocketCreateListenException : Exception { - public override string Message => "无法创建监听,请重新启动服务器再试 (#00009)"; + public override string Message => "无法创建监听,请重新启动服务器再试 (#10009)"; } public class SocketGetClientException : Exception { - public override string Message => "无法获取客户端信息 (#00010)"; + public override string Message => "无法获取客户端信息 (#10010)"; } public class ListeningSocketCanNotSendException : Exception { - public override string Message => "监听Socket不能用于发送信息 (#00011)"; + public override string Message => "监听Socket不能用于发送信息 (#10011)"; } public class ConnectFailedException : Exception { - public override string Message => "连接到服务器失败 (#00012)"; + public override string Message => "连接到服务器失败 (#10012)"; } public class LostConnectException : Exception { - public override string Message => "与服务器连接中断 (#00013)"; + public override string Message => "与服务器连接中断 (#10013)"; } public class FindServerFailedException : Exception { - public override string Message => "查找可用的服务器失败,请重启FunGame (#00014)"; + public override string Message => "查找可用的服务器失败,请重启FunGame (#10014)"; } public class FormHasBeenOpenedException : Exception { - public override string Message => "目标窗口可能已处于打开状态 (#00015)"; + public override string Message => "目标窗口可能已处于打开状态 (#10015)"; } public class FormCanNotOpenException : Exception { - public override string Message => "无法打开指定窗口 (#00016)"; + public override string Message => "无法打开指定窗口 (#10016)"; } public class ServerErrorException : Exception { - public override string Message => "服务器遇到问题需要关闭,请重新启动服务器! (#00017)"; + public override string Message => "服务器遇到问题需要关闭,请重新启动服务器! (#10017)"; } public class CanNotSendToClientException : Exception { - public override string Message => "无法向客户端传输消息 (#00018)"; + public override string Message => "无法向客户端传输消息 (#10018)"; } public class MySQLConfigException : Exception { - public override string Message => "MySQL服务启动失败:无法找到MySQL配置文件 (#00019)"; + public override string Message => "MySQL服务启动失败:无法找到MySQL配置文件 (#10019)"; + } + + public class CanNotLogOutException : Exception + { + public override string Message => "无法登出您的账号,请联系服务器管理员 (#10020)"; + } + + public class NoUserLogonException : Exception + { + public override string Message => "用户未登录 (#10021)"; } } diff --git a/FunGame.Desktop/Controller/MainController.cs b/FunGame.Desktop/Controller/MainController.cs index 703ad10..3a90626 100644 --- a/FunGame.Desktop/Controller/MainController.cs +++ b/FunGame.Desktop/Controller/MainController.cs @@ -33,17 +33,12 @@ namespace Milimoe.FunGame.Desktop.Controller break; case MainSet.Connect: - Main.OnBeforeConnectEvent(new GeneralEventArgs()); result = MainModel.Connect(); - if ((ConnectResult)result == ConnectResult.Success) - { - Main.OnSucceedConnectEvent(new GeneralEventArgs()); - } - else if ((ConnectResult)result == ConnectResult.ConnectFailed) + if ((ConnectResult)result != ConnectResult.Success) { Main.OnFailedConnectEvent(new GeneralEventArgs()); + Main.OnAfterConnectEvent(new GeneralEventArgs()); } - Main.OnAfterConnectEvent(new GeneralEventArgs()); break; case MainSet.Connected: @@ -53,11 +48,9 @@ namespace Milimoe.FunGame.Desktop.Controller case MainSet.Disconnect: Main.OnBeforeDisconnectEvent(new GeneralEventArgs()); MainModel.Disconnect(); - Main.OnAfterDisconnectEvent(new GeneralEventArgs()); break; case MainSet.Disconnected: - MainModel.Disconnect(); break; case MainSet.WaitConnectAndSetYellow: @@ -82,7 +75,8 @@ namespace Milimoe.FunGame.Desktop.Controller break; case MainSet.LogOut: - result = MainModel.Logout(); + Main.OnBeforeLogoutEvent(new GeneralEventArgs()); + result = MainModel.LogOut(); break; case MainSet.Close: diff --git a/FunGame.Desktop/Library/Component/GeneralForm.Designer.cs b/FunGame.Desktop/Library/Component/GeneralForm.Designer.cs index 340d408..a2e0fa3 100644 --- a/FunGame.Desktop/Library/Component/GeneralForm.Designer.cs +++ b/FunGame.Desktop/Library/Component/GeneralForm.Designer.cs @@ -46,6 +46,7 @@ this.Name = "GeneralForm"; this.Text = "GeneralForm"; this.FormClosed += FormClosedEvent; + this.Load += LoadEvent; this.ResumeLayout(false); } diff --git a/FunGame.Desktop/Library/Component/GeneralForm.cs b/FunGame.Desktop/Library/Component/GeneralForm.cs index 3fd6025..4783e9e 100644 --- a/FunGame.Desktop/Library/Component/GeneralForm.cs +++ b/FunGame.Desktop/Library/Component/GeneralForm.cs @@ -90,5 +90,15 @@ namespace Milimoe.FunGame.Desktop.Library.Component RunTime.UserCenter = null; } } + + /// + /// 窗体加载事件,触发BindEvent() + /// + /// + /// + protected virtual void LoadEvent(object? sender, EventArgs e) + { + BindEvent(); + } } } diff --git a/FunGame.Desktop/Library/Component/ShowMessage.cs b/FunGame.Desktop/Library/Component/ShowMessage.cs index 2f830da..dbbf68e 100644 --- a/FunGame.Desktop/Library/Component/ShowMessage.cs +++ b/FunGame.Desktop/Library/Component/ShowMessage.cs @@ -115,7 +115,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component { TaskAutoClose = Task.Factory.StartNew(() => { - Thread.Sleep(100); + Thread.Sleep(1); string msg = MsgText.Text; int s = AutoClose; BeginInvoke(() => ChangeSecond(msg, s)); diff --git a/FunGame.Desktop/Library/Config/Config.cs b/FunGame.Desktop/Library/Config/Config.cs index 3eaa112..76138b7 100644 --- a/FunGame.Desktop/Library/Config/Config.cs +++ b/FunGame.Desktop/Library/Config/Config.cs @@ -23,5 +23,6 @@ /*** GUID For Socket ***/ public static Guid Guid_Socket { get; set; } = Guid.Empty; + public static Guid Guid_LoginKey { get; set; } = Guid.Empty; } } diff --git a/FunGame.Desktop/Model/LoginModel.cs b/FunGame.Desktop/Model/LoginModel.cs index 73bfc06..3638120 100644 --- a/FunGame.Desktop/Model/LoginModel.cs +++ b/FunGame.Desktop/Model/LoginModel.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Desktop.Library; @@ -22,6 +23,7 @@ namespace Milimoe.FunGame.Desktop.Model if (objs.Length > 0) username = (string)objs[0]; if (objs.Length > 1) password = (string)objs[1]; if (objs.Length > 2) autokey = (string)objs[2]; + password = password.Encrypt(username); if (Socket.Send(SocketMessageType.Login, username, password, autokey) == SocketResult.Success) { return true; diff --git a/FunGame.Desktop/Model/MainModel.cs b/FunGame.Desktop/Model/MainModel.cs index e7cfc49..bc7ba02 100644 --- a/FunGame.Desktop/Model/MainModel.cs +++ b/FunGame.Desktop/Model/MainModel.cs @@ -18,21 +18,35 @@ namespace Milimoe.FunGame.Desktop.Model private readonly Main Main; private Task? ReceivingTask; private Core.Library.Common.Network.Socket? Socket; + private bool IsReceiving = false; public MainModel(Main main) { Main = main; } - public bool Logout() + #region 公开方法 + + public bool LogOut() { try { - //Socket?.Send(SocketMessageType.Logout, ""); + // 需要当时登录给的Key发回去,确定是账号本人在操作才允许登出 + if (Config.Guid_LoginKey != Guid.Empty) + { + if (Socket?.Send(SocketMessageType.Logout, Config.Guid_LoginKey) == SocketResult.Success) + { + return true; + } + } + else throw new CanNotLogOutException(); } catch (Exception e) { + ShowMessage.ErrorMessage("无法登出您的账号,请联系服务器管理员。", "登出失败", 5); Main.GetMessage(e.GetErrorInfo()); + Main.OnFailedLogoutEvent(new GeneralEventArgs()); + Main.OnAfterLogoutEvent(new GeneralEventArgs()); } return false; } @@ -41,15 +55,13 @@ namespace Milimoe.FunGame.Desktop.Model { try { - if (Socket?.Send(SocketMessageType.Disconnect, "") == SocketResult.Success) - { - Main.OnSucceedDisconnectEvent(new GeneralEventArgs()); - } + Socket?.Send(SocketMessageType.Disconnect, ""); } catch (Exception e) { Main.GetMessage(e.GetErrorInfo()); Main.OnFailedDisconnectEvent(new GeneralEventArgs()); + Main.OnAfterDisconnectEvent(new GeneralEventArgs()); } } @@ -91,9 +103,12 @@ namespace Milimoe.FunGame.Desktop.Model public ConnectResult Connect() { + Main.OnBeforeConnectEvent(new GeneralEventArgs()); if (Constant.Server_Address == "" || Constant.Server_Port <= 0) { ShowMessage.ErrorMessage("查找可用的服务器失败!"); + Main.OnFailedConnectEvent(new GeneralEventArgs()); + Main.OnAfterConnectEvent(new GeneralEventArgs()); return ConnectResult.FindServerFailed; } try @@ -102,6 +117,8 @@ namespace Milimoe.FunGame.Desktop.Model { Main.GetMessage("正在连接服务器,请耐心等待。"); Config.FunGame_isRetrying = false; + Main.OnFailedConnectEvent(new GeneralEventArgs()); + Main.OnAfterConnectEvent(new GeneralEventArgs()); return ConnectResult.CanNotConnect; } if (!Config.FunGame_isConnected) @@ -132,6 +149,15 @@ namespace Milimoe.FunGame.Desktop.Model Main.GetMessage("连接服务器成功,请登录账号以体验FunGame。"); Main.UpdateUI(MainSet.Connected); StartReceiving(); + while (true) + { + if (IsReceiving) + { + Main.OnSucceedConnectEvent(new GeneralEventArgs()); + Main.OnAfterConnectEvent(new GeneralEventArgs()); + break; + } + } } }); return ConnectResult.Success; @@ -152,6 +178,11 @@ namespace Milimoe.FunGame.Desktop.Model Main.GetMessage(e.GetErrorInfo(), TimeType.None); Main.UpdateUI(MainSet.SetRed); Config.FunGame_isRetrying = false; + Task.Factory.StartNew(() => + { + Main.OnFailedConnectEvent(new GeneralEventArgs()); + Main.OnAfterConnectEvent(new GeneralEventArgs()); + }); return ConnectResult.ConnectFailed; } return ConnectResult.CanNotConnect; @@ -170,6 +201,7 @@ namespace Milimoe.FunGame.Desktop.Model { ReceivingTask.Wait(1); ReceivingTask = null; + IsReceiving = false; } } catch (Exception e) @@ -215,16 +247,16 @@ namespace Milimoe.FunGame.Desktop.Model throw new NotImplementedException(); } - public bool LogOut() - { - throw new NotImplementedException(); - } + #endregion + + #region 私有方法 private void StartReceiving() { ReceivingTask = Task.Factory.StartNew(() => { Thread.Sleep(100); + IsReceiving = true; while (Socket != null && Socket.Connected) { Receiving(); @@ -272,6 +304,7 @@ namespace Milimoe.FunGame.Desktop.Model break; case SocketMessageType.Logout: + SocketHandler_LogOut(objs); break; case SocketMessageType.Disconnect: @@ -300,6 +333,10 @@ namespace Milimoe.FunGame.Desktop.Model return result; } + #endregion + + #region SocketHandler + private void SocketHandler_Connect(object[] objs) { string msg = ""; @@ -324,15 +361,52 @@ namespace Milimoe.FunGame.Desktop.Model private void SocketHandler_Login(object[] objs) { Guid key = Guid.Empty; + string? msg = ""; // 返回一个Key,再发回去给服务器就行了 - if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject(objs[0])!; - if (key != Guid.Empty) LoginController.CheckLogin(key); - else + if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject(objs[0]); + if (objs.Length > 1) msg = NetworkUtility.ConvertJsonObject(objs[1]); + // 如果返回了msg,说明验证错误。 + if (msg != null && msg.Trim() != "") { - ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); + ShowMessage.ErrorMessage(msg, "登录失败"); RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs()); } + else + { + if (key != Guid.Empty) + { + Config.Guid_LoginKey = key; + LoginController.CheckLogin(key); + } + else + { + ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); + RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); + RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs()); + } + } + } + + private void SocketHandler_LogOut(object[] objs) + { + Guid key = Guid.Empty; + string? msg = ""; + // 返回一个Key,如果这个Key是空的,登出失败 + if (objs != null && objs.Length > 0) key = NetworkUtility.ConvertJsonObject(objs[0]); + if (objs != null && objs.Length > 1) msg = NetworkUtility.ConvertJsonObject(objs[1]); + if (key != Guid.Empty) + { + Config.Guid_LoginKey = Guid.Empty; + Main.UpdateUI(MainSet.LogOut, msg ?? ""); + Main.OnSucceedLogoutEvent(new GeneralEventArgs()); + } + else + { + ShowMessage.ErrorMessage("无法登出您的账号,请联系服务器管理员。", "登出失败", 5); + Main.OnFailedLogoutEvent(new GeneralEventArgs()); + } + Main.OnAfterLogoutEvent(new GeneralEventArgs()); } private void SocketHandler_CheckLogin(object[] objs) @@ -356,6 +430,10 @@ namespace Milimoe.FunGame.Desktop.Model Main.GetMessage(msg); Main.UpdateUI(MainSet.Disconnect); Close(); + Main.OnSucceedDisconnectEvent(new GeneralEventArgs()); + Main.OnAfterDisconnectEvent(new GeneralEventArgs()); } + + #endregion } } diff --git a/FunGame.Desktop/UI/Login/Login.cs b/FunGame.Desktop/UI/Login/Login.cs index 84f378e..f0829b2 100644 --- a/FunGame.Desktop/UI/Login/Login.cs +++ b/FunGame.Desktop/UI/Login/Login.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Api.Utility; +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; @@ -15,7 +16,13 @@ namespace Milimoe.FunGame.Desktop.UI InitializeComponent(); } - private void Login_Handler() + protected override void BindEvent() + { + base.BindEvent(); + FailedLogin += FailedLoginEvent; + } + + private bool Login_Handler() { try { @@ -25,16 +32,20 @@ namespace Milimoe.FunGame.Desktop.UI { ShowMessage.ErrorMessage("账号或密码不能为空!"); UsernameText.Focus(); - return; + return false; } - password = password.Encrypt(username); if (!LoginController.LoginAccount(username, password)) + { ShowMessage.Message("登录失败!!", "登录失败"); + return false; + } } catch (Exception e) { RunTime.WritelnSystemInfo(e.GetErrorInfo()); + return false; } + return true; } /// @@ -54,12 +65,19 @@ namespace Milimoe.FunGame.Desktop.UI private void GoToLogin_Click(object sender, EventArgs e) { - Login_Handler(); + GoToLogin.Enabled = false; + if (!Login_Handler()) GoToLogin.Enabled = true; } private void ForgetPassword_Click(object sender, EventArgs e) { ShowMessage.TipMessage("暂不支持找回密码~"); } + + public EventResult FailedLoginEvent(object sender, GeneralEventArgs e) + { + GoToLogin.Enabled = true; + return EventResult.Success; + } } } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index c815c37..469c53b 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -1,6 +1,5 @@ using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; -using Milimoe.FunGame.Core.Interface; using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; @@ -10,7 +9,6 @@ using Milimoe.FunGame.Desktop.Library.Base; using Milimoe.FunGame.Desktop.Library.Component; using Milimoe.FunGame.Desktop.Utility; using System.Diagnostics; -using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; namespace Milimoe.FunGame.Desktop.UI { @@ -42,7 +40,6 @@ namespace Milimoe.FunGame.Desktop.UI { InitializeComponent(); Init(); - BindEvent(); } /// @@ -76,6 +73,9 @@ namespace Milimoe.FunGame.Desktop.UI }); } + /// + /// 绑定事件 + /// protected override void BindEvent() { base.BindEvent(); @@ -92,7 +92,7 @@ namespace Milimoe.FunGame.Desktop.UI /// /// string? /// object[]? - public void UpdateUI(string? updatetype, object[]? objs = null) + public void UpdateUI(string? updatetype, params object[]? objs) { void action() { @@ -159,6 +159,7 @@ namespace Milimoe.FunGame.Desktop.UI SetServerStatusLight((int)LightType.Red); SetButtonEnableIfLogon(false, ClientState.WaitConnect); LogoutAccount(); + CloseConnectedWindows(); break; case MainSet.Disconnect: @@ -177,19 +178,17 @@ namespace Milimoe.FunGame.Desktop.UI case MainSet.LogOut: Config.FunGame_isRetrying = false; - Config.FunGame_isConnected = false; Config.FunGame_isAutoLogin = false; - SetServerStatusLight((int)LightType.Yellow); - SetButtonEnableIfLogon(false, ClientState.WaitConnect); + SetServerStatusLight((int)LightType.Yellow, true); + SetButtonEnableIfLogon(false, ClientState.WaitLogin); LogoutAccount(); - if (Config.FunGame_isAutoConnect) + if (objs != null && objs.Length > 0) { - CurrentRetryTimes = -1; - Task.Run(() => + if (objs[0].GetType() == typeof(string)) { - Thread.Sleep(1000); - MainController?.Connect(); - }); + WritelnSystemInfo((string)objs[0]); + ShowMessage.Message((string)objs[0], "退出登录成功", 5); + } } break; @@ -206,7 +205,7 @@ namespace Milimoe.FunGame.Desktop.UI default: // 直接调用UpdateUI(string)相当于调用GetMessage(string),输出该string到控制台。 - // 尽量避免使用除MainControllerSet之外的string调用此方法 + // 尽量避免使用除MainSet之外的string调用此方法 WritelnSystemInfo(updatetype); break; } @@ -221,6 +220,11 @@ namespace Milimoe.FunGame.Desktop.UI InvokeUpdateUI(action); } + /// + /// 提供公共方法给Controller发送系统信息 + /// + /// + /// public void GetMessage(string? msg, TimeType timetype = TimeType.TimeOnly) { void action() @@ -270,9 +274,9 @@ namespace Milimoe.FunGame.Desktop.UI { string isAutoConnect = INIHelper.ReadINI("Config", "AutoConnect"); string isAutoLogin = INIHelper.ReadINI("Config", "AutoLogin"); - string strUserName = INIHelper.ReadINI("Config", "UserName"); - string strPassword = INIHelper.ReadINI("Config", "Password"); - string strAutoKey = INIHelper.ReadINI("Config", "AutoKey"); + string strUserName = INIHelper.ReadINI("Account", "UserName"); + string strPassword = INIHelper.ReadINI("Account", "Password"); + string strAutoKey = INIHelper.ReadINI("Account", "AutoKey"); if (isAutoConnect != null && isAutoConnect.Trim() != "" && (isAutoConnect.ToLower().Equals("false") || isAutoConnect.ToLower().Equals("true"))) Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConnect); @@ -325,7 +329,7 @@ namespace Milimoe.FunGame.Desktop.UI /// 设置登录信息 /// /// - private void SetLoginUser(object[]? objs = null) + private void SetLoginUser(params object[]? objs) { if (InvokeRequired) Invoke(LoginAccount, objs); @@ -388,8 +392,6 @@ namespace Milimoe.FunGame.Desktop.UI { // 显示:匹配、创建房间 // 隐藏:退出房间、房间设定 - WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 离开房间"); - WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + Config.FunGame_Roomid + " ]"); SetRoomid("-1"); QuitRoom.Visible = false; StartMatch.Visible = true; @@ -412,7 +414,10 @@ namespace Milimoe.FunGame.Desktop.UI /// /// 未登录和离线时,停用按钮 + /// 登录的时候要激活按钮 /// + /// 是否登录 + /// 客户端状态 private void SetButtonEnableIfLogon(bool isLogon, ClientState status) { switch (status) @@ -615,17 +620,21 @@ namespace Milimoe.FunGame.Desktop.UI /// /// 登录账号,显示登出按钮 /// - private void LoginAccount(object[]? objs = null) + private void LoginAccount(params object[]? objs) { if (objs != null && objs.Length > 0) { Usercfg.LoginUser = (User)objs[0]; + if (Usercfg.LoginUser is null) + { + throw new NoUserLogonException(); + } Usercfg.LoginUserName = Usercfg.LoginUser.Username; } NowAccount.Text = "[ID] " + Usercfg.LoginUserName; Login.Visible = false; Logout.Visible = true; - SetServerStatusLight((int)LightType.Green); + UpdateUI(MainSet.SetGreenAndPing); RunTime.Login?.Close(); Thread.Sleep(100); string welcome = $"欢迎回来, {Usercfg.LoginUserName}!"; @@ -638,6 +647,7 @@ namespace Milimoe.FunGame.Desktop.UI /// private void LogoutAccount() { + InMain(); Usercfg.LoginUser = null; Usercfg.LoginUserName = ""; NowAccount.Text = "请登录账号"; @@ -796,6 +806,19 @@ namespace Milimoe.FunGame.Desktop.UI WritelnGameInfo(FunGameInfo.GetInfo((FunGameInfo.FunGame)Constant.FunGameType)); } + /// + /// 关闭所有登录后才能访问的窗口 + /// + private void CloseConnectedWindows() + { + RunTime.Login?.Close(); + RunTime.Register?.Close(); + RunTime.Store?.Close(); + RunTime.Inventory?.Close(); + RunTime.RoomSetting?.Close(); + RunTime.UserCenter?.Close(); + } + #endregion #region 事件 @@ -916,6 +939,8 @@ namespace Milimoe.FunGame.Desktop.UI /// private void QuitRoom_Click(object sender, EventArgs e) { + WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 离开房间"); + WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + Config.FunGame_Roomid + " ]"); InMain(); } @@ -1149,12 +1174,12 @@ namespace Milimoe.FunGame.Desktop.UI /// public EventResult FailedConnectEvent(object sender, GeneralEventArgs e) { - if (Config.FunGame_isAutoRetry && CurrentRetryTimes <= MaxRetryTimes) + if (Config.FunGame_isConnected && Config.FunGame_isAutoRetry && CurrentRetryTimes <= MaxRetryTimes) { Task.Run(() => { Thread.Sleep(5000); - if (Config.FunGame_isAutoRetry) MainController?.Connect(); // 再次判断是否开启自动重连 + if (Config.FunGame_isConnected && Config.FunGame_isAutoRetry) MainController?.Connect(); // 再次判断是否开启自动重连 }); GetMessage("连接服务器失败,5秒后自动尝试重连。"); } @@ -1257,6 +1282,7 @@ namespace Milimoe.FunGame.Desktop.UI if (!Config.FunGame_isRetrying) { CurrentRetryTimes = -1; + Config.FunGame_isAutoLogin = true; MainController?.Connect(); } else @@ -1266,13 +1292,16 @@ namespace Milimoe.FunGame.Desktop.UI if (!Config.FunGame_isConnected) { CurrentRetryTimes = -1; + Config.FunGame_isAutoLogin = true; MainController?.GetServerConnection(); } break; case Constant.FunGame_Disconnect: if (Config.FunGame_isConnected && MainController != null) { - MainController?.Disconnect(); + // 先退出登录再断开连接 + bool? @bool = MainController?.LogOut(); + if (@bool ?? false) MainController?.Disconnect(); } break; case Constant.FunGame_DisconnectWhenNotLogin: @@ -1308,6 +1337,7 @@ namespace Milimoe.FunGame.Desktop.UI Constant.Server_Address = ip; Constant.Server_Port = port; CurrentRetryTimes = -1; + Config.FunGame_isAutoLogin = true; MainController?.Connect(); } else if (ErrorType == Core.Library.Constant.ErrorType.IsNotIP) ShowMessage.ErrorMessage("这不是一个IP地址!");