From e09da145b98284489c28a2960d29319cac355f01 Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Wed, 15 Jan 2025 09:13:27 +0800 Subject: [PATCH] =?UTF-8?q?.NET=209=EF=BC=9B=E7=AA=97=E4=BD=93=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20(#30)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 升级 .NET 9;异步事件优化 * 加入密码房间需要先检查密码 * 添加模组方法参数 --- .../Controller/RunTimeController.cs | 23 +- FunGame.Desktop/FunGame.Desktop.csproj | 4 +- .../Library/Component/ExitButton.cs | 1 + .../Library/Component/MinButton.cs | 1 + .../Library/Component/TransparentRect.cs | 7 + FunGame.Desktop/UI/Login/ForgetPassword.cs | 231 +++++++++--------- FunGame.Desktop/UI/Login/Login.cs | 56 +++-- FunGame.Desktop/UI/Main/Main.Designer.cs | 2 +- FunGame.Desktop/UI/Main/Main.cs | 171 +++++++------ FunGame.Desktop/UI/Register/Register.cs | 166 +++++++------ FunGame.Desktop/Utility/OpenForm.cs | 5 +- FunGame.Implement/FunGame.Implement.csproj | 4 +- 12 files changed, 347 insertions(+), 324 deletions(-) diff --git a/FunGame.Desktop/Controller/RunTimeController.cs b/FunGame.Desktop/Controller/RunTimeController.cs index 02a4494..4670241 100644 --- a/FunGame.Desktop/Controller/RunTimeController.cs +++ b/FunGame.Desktop/Controller/RunTimeController.cs @@ -29,12 +29,12 @@ namespace Milimoe.FunGame.Desktop.Controller try { // 构建AddonController - Hashtable delegates = []; - delegates.Add("WriteLine", new Action(WritelnSystemInfo)); + Dictionary delegates = []; + delegates.Add("WriteLine", new Action(WritelnSystemInfo)); delegates.Add("Error", new Action(Error)); delegates.Add("NewDataRequest", new Func(NewDataRequestForAddon)); delegates.Add("NewLongRunningDataRequest", new Func(NewLongRunningDataRequestForAddon)); - RunTime.PluginLoader = PluginLoader.LoadPlugins( delegates, RunTime.Session, RunTime.Config); + RunTime.PluginLoader = PluginLoader.LoadPlugins(delegates, RunTime.Session, RunTime.Config); foreach (string name in RunTime.PluginLoader.Plugins.Keys) { Main.GetMessage("[ Plugin ] Loaded: " + name); @@ -51,8 +51,8 @@ namespace Milimoe.FunGame.Desktop.Controller try { // 构建AddonController - Hashtable delegates = []; - delegates.Add("WriteLine", new Action(WritelnSystemInfo)); + Dictionary delegates = []; + delegates.Add("WriteLine", new Action(WritelnSystemInfo)); delegates.Add("Error", new Action(Error)); delegates.Add("NewGamingRequest", new Func(NewDataRequestForAddon)); delegates.Add("NewLongRunningGamingRequest", new Func(NewLongRunningDataRequestForAddon)); @@ -68,7 +68,12 @@ namespace Milimoe.FunGame.Desktop.Controller } } - public override void WritelnSystemInfo(string msg) + public override void WritelnSystemInfo(string msg, LogLevel level = LogLevel.Info, bool useLevel = true) + { + Main.GetMessage(msg); + } + + public void WritelnSystemInfo(string name, string msg, LogLevel level = LogLevel.Info, bool useLevel = true) { Main.GetMessage(msg); } @@ -79,7 +84,7 @@ namespace Milimoe.FunGame.Desktop.Controller Main.UpdateUI(MainInvokeType.Disconnected); ConnectEventArgs args = new(RunTime.Session.Server_Address, RunTime.Session.Server_Port, ConnectResult.ConnectFailed); Main.OnFailedConnectEvent(Main, args); - Close(); + Close_Socket(); } public override bool BeforeConnect(ref string ip, ref int port, ArrayList ConnectArgs) @@ -162,7 +167,7 @@ namespace Milimoe.FunGame.Desktop.Controller { try { - Core.Api.Utility.TaskUtility.NewTask(async () => await LoginController.LoginAccountAsync(Username, Password, AutoKey)); + TaskUtility.NewTask(async () => await LoginController.LoginAccountAsync(Username, Password, AutoKey)); } catch (Exception e) { @@ -177,7 +182,7 @@ namespace Milimoe.FunGame.Desktop.Controller if (ServerMessage.Parameters.Length > 0) msg = ServerMessage.GetParam(0) ?? ""; Main.GetMessage(msg); Main.UpdateUI(MainInvokeType.Disconnect); - Close(); + Close_Socket(); } protected override void SocketHandler_System(SocketObject ServerMessage) diff --git a/FunGame.Desktop/FunGame.Desktop.csproj b/FunGame.Desktop/FunGame.Desktop.csproj index d0f5525..6e4ff67 100644 --- a/FunGame.Desktop/FunGame.Desktop.csproj +++ b/FunGame.Desktop/FunGame.Desktop.csproj @@ -2,7 +2,7 @@ WinExe - net8.0-windows + net9.0-windows enable true enable @@ -40,7 +40,7 @@ - ..\..\FunGame.Core\bin\Debug\net8.0\FunGame.Core.dll + ..\..\FunGame.Core\bin\Debug\net9.0\FunGame.Core.dll diff --git a/FunGame.Desktop/Library/Component/ExitButton.cs b/FunGame.Desktop/Library/Component/ExitButton.cs index 3ac34e2..59a310c 100644 --- a/FunGame.Desktop/Library/Component/ExitButton.cs +++ b/FunGame.Desktop/Library/Component/ExitButton.cs @@ -4,6 +4,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component { public partial class ExitButton : Button { + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public GeneralForm? RelativeForm { get; set; } public ExitButton() diff --git a/FunGame.Desktop/Library/Component/MinButton.cs b/FunGame.Desktop/Library/Component/MinButton.cs index 3de3d8f..7779d07 100644 --- a/FunGame.Desktop/Library/Component/MinButton.cs +++ b/FunGame.Desktop/Library/Component/MinButton.cs @@ -4,6 +4,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component { public partial class MinButton : Button { + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public GeneralForm? RelativeForm { get; set; } public MinButton() diff --git a/FunGame.Desktop/Library/Component/TransparentRect.cs b/FunGame.Desktop/Library/Component/TransparentRect.cs index 2171994..827e579 100644 --- a/FunGame.Desktop/Library/Component/TransparentRect.cs +++ b/FunGame.Desktop/Library/Component/TransparentRect.cs @@ -1,4 +1,5 @@ using System.Drawing.Drawing2D; +using System.ComponentModel; namespace Milimoe.FunGame.Desktop.Library.Component { @@ -24,31 +25,37 @@ namespace Milimoe.FunGame.Desktop.Library.Component SetStyle(ControlStyles.Opaque, false); UpdateStyles(); } + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public new Color BackColor { get { return _backColor; } set { _backColor = value; Invalidate(); } } + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public ShapeBorderStyles ShapeBorderStyle { get { return _borderStyle; } set { _borderStyle = value; this.Invalidate(); } } + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public Color BorderColor { get { return _borderColor; } set { _borderColor = value; Invalidate(); } } + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public int Opacity { get { return _opacity; } set { _opacity = value; this.Invalidate(); } } + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public int Radius { get { return _radius; } set { _radius = value; this.Invalidate(); } } + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override Color ForeColor { get { return base.ForeColor; } diff --git a/FunGame.Desktop/UI/Login/ForgetPassword.cs b/FunGame.Desktop/UI/Login/ForgetPassword.cs index 1c1e699..9ea679b 100644 --- a/FunGame.Desktop/UI/Login/ForgetPassword.cs +++ b/FunGame.Desktop/UI/Login/ForgetPassword.cs @@ -1,5 +1,4 @@ -using Milimoe.FunGame.Core.Api.Utility; -using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Desktop.Controller; using Milimoe.FunGame.Desktop.Model; @@ -8,124 +7,134 @@ namespace Milimoe.FunGame.Desktop.UI { public partial class ForgetPassword { - private readonly LoginController LoginController; - public ForgetPassword() { InitializeComponent(); - LoginController = new(this); } - private void FindPassword_Click(object sender, EventArgs e) + private async void FindPassword_Click(object sender, EventArgs e) { - TaskUtility.NewTask(async () => + if (RunTime.Socket == null) return; + + string username = UsernameText.Text.Trim(); + string email = EmailText.Text.Trim(); + + if (!ValidateInput(username, email)) return; + + try { - if (RunTime.Socket != null) + if (!await RequestVerificationCodeAsync(username, email)) return; + + string newPassword = await GetNewPasswordAsync(username, email); + + if (!string.IsNullOrEmpty(newPassword) && await UpdatePasswordAsync(username, newPassword)) { - string username = ""; - string email = ""; - InvokeUpdateUI(() => - { - username = UsernameText.Text.Trim(); - email = EmailText.Text.Trim(); - }); - if (username == "" || email == "") - { - ShowMessage(ShowMessageType.Error, "账号或邮箱不能为空!"); - InvokeUpdateUI(() => UsernameText.Focus()); - return; - } - - string msg; - bool success = false; - - try - { - // 发送找回密码请求 - msg = await LoginController.ForgetPassword_CheckVerifyCodeAsync(username, email, ""); - - if (msg.Trim() != "") - { - // 如果返回一个信息,则停止找回密码 - ShowMessage(ShowMessageType.Error, msg); - } - else - { - while (!success) - { - string verifycode = ShowInputMessageCancel("请输入找回密码邮件中的6位数字验证码", "注册验证码", out MessageResult result); - if (result != MessageResult.Cancel) - { - if (verifycode.Trim() != "") - { - msg = await LoginController.ForgetPassword_CheckVerifyCodeAsync(username, email, verifycode); - if (msg.Trim() != "") - { - ShowMessage(ShowMessageType.Error, msg); - } - else - { - success = true; - break; - } - } - else - { - ShowMessage(ShowMessageType.Warning, "不能输入空值!"); - } - } - else break; - } - if (success) - { - while (true) - { - string newpass = ShowInputMessageCancel("请输入新密码", "设置新密码", out MessageResult result); - if (result != MessageResult.Cancel) - { - if (newpass.Trim() != "") - { - if (newpass.Length < 6 || newpass.Length > 15) // 字节范围 3~12 - { - ShowMessage(ShowMessageType.Error, "密码长度不符合要求:6~15个字符数"); - } - else - { - msg = await LoginController.ForgetPassword_UpdatePasswordAsync(username, newpass); - if (msg.Trim() != "") - { - ShowMessage(ShowMessageType.Error, msg); - } - else - { - ShowMessage(ShowMessageType.General, "密码更新成功!请您牢记新的密码。", "找回密码"); - break; - } - } - } - } - else - { - if (ShowMessage(ShowMessageType.OKCancel, "确定放弃设置新密码吗?", "找回密码") == MessageResult.OK) - { - success = false; - break; - } - } - } - } - if (success) - { - InvokeUpdateUI(Dispose); - } - } - } - catch (Exception ex) - { - RunTime.WritelnSystemInfo(ex.GetErrorInfo()); - } + Close(); } - }); + } + catch (Exception ex) + { + RunTime.WritelnSystemInfo(ex.GetErrorInfo()); + } + } + + private bool ValidateInput(string username, string email) + { + if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(email)) + { + ShowMessage(ShowMessageType.Error, "账号或邮箱不能为空!"); + InvokeUpdateUI(() => UsernameText.Focus()); + return false; + } + return true; + } + + private async Task RequestVerificationCodeAsync(string username, string email) + { + string msg = await LoginController.ForgetPassword_CheckVerifyCodeAsync(username, email, ""); + if (!string.IsNullOrEmpty(msg)) + { + ShowMessage(ShowMessageType.Error, msg); + return false; + } + return true; + } + + private async Task GetNewPasswordAsync(string username, string email) + { + bool success = false; + do + { + string verifycode = ShowInputMessageCancel("请输入找回密码邮件中的6位数字验证码", "注册验证码", out MessageResult result); + if (result == MessageResult.Cancel) + { + break; + } + + if (string.IsNullOrEmpty(verifycode)) + { + ShowMessage(ShowMessageType.Warning, "不能输入空值!"); + continue; + } + + string msg = await LoginController.ForgetPassword_CheckVerifyCodeAsync(username, email, verifycode); + if (!string.IsNullOrEmpty(msg)) + { + ShowMessage(ShowMessageType.Error, msg); + } + else + { + success = true; + } + } while (!success); + + if (!success) return ""; + + string newPassword; + success = false; + do + { + newPassword = ShowInputMessageCancel("请输入新密码", "设置新密码", out MessageResult result); + if (result == MessageResult.Cancel) + { + if (ShowMessage(ShowMessageType.OKCancel, "确定放弃设置新密码吗?", "找回密码") == MessageResult.OK) + { + break; + } + continue; + } + + if (string.IsNullOrEmpty(newPassword)) + { + continue; + } + + if (newPassword.Length < 6 || newPassword.Length > 15) + { + ShowMessage(ShowMessageType.Error, "密码长度不符合要求:6~15个字符数"); + continue; + } + success = true; + } while (!success); + + if (!success) return ""; + + return newPassword; + } + + private async Task UpdatePasswordAsync(string username, string newPassword) + { + string msg = await LoginController.ForgetPassword_UpdatePasswordAsync(username, newPassword); + if (!string.IsNullOrEmpty(msg)) + { + ShowMessage(ShowMessageType.Error, msg); + } + else + { + ShowMessage(ShowMessageType.General, "密码更新成功!请您牢记新的密码。", "找回密码"); + return true; + } + return false; } } } diff --git a/FunGame.Desktop/UI/Login/Login.cs b/FunGame.Desktop/UI/Login/Login.cs index 7d9083d..32f5a6a 100644 --- a/FunGame.Desktop/UI/Login/Login.cs +++ b/FunGame.Desktop/UI/Login/Login.cs @@ -1,5 +1,4 @@ -using Milimoe.FunGame.Core.Api.Utility; -using Milimoe.FunGame.Core.Library.Common.Event; +using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Desktop.Controller; @@ -28,18 +27,10 @@ namespace Milimoe.FunGame.Desktop.UI SucceedLogin += SucceedLoginEvent; } - private async Task Login_Handler() + private async Task Login_HandlerAsync(string username, string password) { try { - string username = UsernameText.Text.Trim(); - string password = PasswordText.Text.Trim(); - if (username == "" || password == "") - { - ShowMessage(ShowMessageType.Error, "账号或密码不能为空!"); - UsernameText.Focus(); - return false; - } return await LoginController.LoginAccountAsync(username, password); } catch (Exception e) @@ -49,11 +40,6 @@ namespace Milimoe.FunGame.Desktop.UI } } - /// - /// 打开注册界面 - /// - /// - /// private void RegButton_Click(object sender, EventArgs e) { OpenForm.SingleForm(FormType.Register, OpenFormType.Dialog); @@ -64,33 +50,51 @@ namespace Milimoe.FunGame.Desktop.UI ShowMessage(ShowMessageType.Tip, "与No.16对话即可获得快速登录秘钥,快去试试吧!"); } - private void GoToLogin_Click(object sender, EventArgs e) + private async void GoToLogin_Click(object sender, EventArgs e) { GoToLogin.Enabled = false; - bool result = false; - TaskUtility.NewTask(async () => + string username = UsernameText.Text.Trim(); + string password = PasswordText.Text.Trim(); + if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { - result = await Login_Handler(); - }).OnCompleted(() => + ShowMessage(ShowMessageType.Error, "账号或密码不能为空!"); + UsernameText.Focus(); + GoToLogin.Enabled = true; + return; + } + bool result = await Login_HandlerAsync(username, password); + if (result) { - if (result) InvokeUpdateUI(Dispose); - else GoToLogin.Enabled = true; - }); + Dispose(); + } + else + { + GoToLogin.Enabled = true; + } } + private void ForgetPassword_Click(object sender, EventArgs e) { OpenForm.SingleForm(FormType.ForgetPassword, OpenFormType.Dialog); UsernameText.Focus(); } - public void FailedLoginEvent(object sender, LoginEventArgs e) + private void FailedLoginEvent(object sender, LoginEventArgs e) { - GoToLogin.Enabled = true; + UpdateFailedLoginUI(); RunTime.Main?.OnFailedLoginEvent(sender, e); RunTime.PluginLoader?.OnFailedLoginEvent(sender, e); } + private void UpdateFailedLoginUI() + { + InvokeUpdateUI(() => + { + GoToLogin.Enabled = true; + }); + } + private void SucceedLoginEvent(object sender, LoginEventArgs e) { RunTime.Main?.OnSucceedLoginEvent(sender, e); diff --git a/FunGame.Desktop/UI/Main/Main.Designer.cs b/FunGame.Desktop/UI/Main/Main.Designer.cs index 9164667..4dd061a 100644 --- a/FunGame.Desktop/UI/Main/Main.Designer.cs +++ b/FunGame.Desktop/UI/Main/Main.Designer.cs @@ -514,7 +514,7 @@ namespace Milimoe.FunGame.Desktop.UI Copyright.Size = new Size(186, 23); Copyright.TabIndex = 97; Copyright.TabStop = true; - Copyright.Text = "©2024 Milimoe. 米粒的糖果屋"; + Copyright.Text = "©2022 Milimoe. 米粒的糖果屋"; Copyright.TextAlign = ContentAlignment.MiddleLeft; Copyright.UseCompatibleTextRendering = true; Copyright.LinkClicked += Copyright_LinkClicked; diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index bf02e8e..6201fd9 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -1,3 +1,4 @@ +using System.ComponentModel; using System.Diagnostics; using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; @@ -22,6 +23,7 @@ namespace Milimoe.FunGame.Desktop.UI * 属性 */ public int MaxRetryTimes { get; } = SocketSet.MaxRetryTimes; // 最大重试连接次数 + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public int CurrentRetryTimes { get; set; } = -1; // 当前重试连接次数 /** @@ -44,6 +46,7 @@ namespace Milimoe.FunGame.Desktop.UI /// public void Init() { + Copyright.Text = FunGameInfo.FunGame_CopyRight; RunTime.Main = this; SetButtonEnabled(false, ClientState.WaitConnect); SetRoomid(Usercfg.InRoom); // 房间号初始化 @@ -403,7 +406,9 @@ namespace Milimoe.FunGame.Desktop.UI /// private void WritelnGameInfo() { - WritelnGameInfo("\r\n"); + GameInfo.AppendText("\n"); + GameInfo.SelectionStart = GameInfo.Text.Length - 1; + GameInfo.ScrollToCaret(); } /// @@ -414,7 +419,9 @@ namespace Milimoe.FunGame.Desktop.UI { if (msg.Trim() != "") { - WriteGameInfo(msg + "\r\n"); + GameInfo.AppendText(msg + "\n"); + GameInfo.SelectionStart = GameInfo.Text.Length - 1; + GameInfo.ScrollToCaret(); } } @@ -426,12 +433,9 @@ namespace Milimoe.FunGame.Desktop.UI { if (msg.Trim() != "") { - InvokeUpdateUI(() => - { - GameInfo.AppendText(msg); - GameInfo.SelectionStart = GameInfo.Text.Length - 1; - GameInfo.ScrollToCaret(); - }); + GameInfo.AppendText(msg); + GameInfo.SelectionStart = GameInfo.Text.Length - 1; + GameInfo.ScrollToCaret(); } } @@ -489,16 +493,16 @@ namespace Milimoe.FunGame.Desktop.UI TaskUtility.NewTask(async () => { int PlayerCount = users.Count; - for (int i = 5; i > 0; i--) + for (int i = 10; i > 0; i--) { WritelnGameInfo("房间 [ " + room.Roomid + " (" + PlayerCount + "人" + RoomSet.GetTypeString(room.RoomType) + ") ] 的游戏将在" + i + "秒后开始..."); await Task.Delay(1000); } WritelnGameInfo("房间 [ " + room.Roomid + " (" + PlayerCount + "人" + RoomSet.GetTypeString(room.RoomType) + ") ] 的游戏正式开始!"); - if (RunTime.GameModuleLoader?.Modules.TryGetValue(room.GameModule, out GameModule? module) ?? false && module != null) + if (RunTime.GameModuleLoader?.Modules.ContainsKey(room.GameModule) ?? false) { - RunTime.Gaming = Core.Model.Gaming.StartGame(module, room, RunTime.Session.LoginUser, users, RunTime.GameModuleLoader); - Visible = !module.HideMain; // 隐藏主界面 + RunTime.Gaming = Core.Model.Gaming.StartGame(RunTime.GameModuleLoader[room.GameModule], room, RunTime.Session.LoginUser, users, RunTime.GameModuleLoader); + Visible = false; // 隐藏主界面 } else { @@ -525,8 +529,8 @@ namespace Milimoe.FunGame.Desktop.UI { // 恢复界面 Visible = true; - SetButtonEnabled(true, ClientState.InRoom); _InGame = false; + SetButtonEnabled(true, ClientState.InRoom); OnSucceedEndGameEvent(this, e); RunTime.PluginLoader?.OnSucceedEndGameEvent(this, e); } @@ -629,12 +633,12 @@ namespace Milimoe.FunGame.Desktop.UI /// /// 通过双击房间列表的房间号加入房间 /// - /// - private async Task JoinRoom(int selectedIndex) + /// + private async Task JoinRoom(int selectedindex) { - if (selectedIndex != -1 && RunTime.RoomList.Count > selectedIndex) + if (selectedindex != -1 && RunTime.RoomList.Count > selectedindex) { - string roomid = RunTime.RoomList.ListRoom[selectedIndex]?.Roomid ?? ""; + string roomid = RunTime.RoomList.ListRoom[selectedindex]?.Roomid ?? ""; return await JoinRoom_Handler(roomid); } return false; @@ -654,13 +658,26 @@ namespace Milimoe.FunGame.Desktop.UI { if (Usercfg.InRoom.Roomid == "-1") { - GameModule? gameModule = RunTime.GameModuleLoader?[RunTime.RoomList[roomid].GameModule]; - if (await MainController.GetRoomPlayerCountAsync(roomid) < gameModule?.MaxUsers) + if (await MainController.GetRoomPlayerCountAsync(roomid) < 8) { if (ShowMessage(ShowMessageType.YesNo, "已找到房间 -> [ " + roomid + " ]\n是否加入?", "已找到房间") == MessageResult.Yes) { Room r = GetRoom(roomid); - return await InvokeController_IntoRoom(r); + bool result = true; + if (r.Password.Trim() != "") + { + // 验证密码 + string inputPassword = ShowInputMessageCancel("请输入房间密码", "房间需要密码", out MessageResult messageResult); + if (messageResult == MessageResult.Cancel || inputPassword != r.Password) + { + ShowMessage(ShowMessageType.Error, "密码验证失败,拒绝加入!"); + result = false; + } + } + if (result) + { + return await InvokeController_IntoRoom(r); + } } return false; } @@ -890,27 +907,23 @@ namespace Milimoe.FunGame.Desktop.UI /// /// 创建房间的处理方法 /// - /// - /// - /// - /// - /// + /// + /// /// - private async Task CreateRoom_Handler(RoomType roomType, string gameModule, string gameMap, bool isRank, string password = "") + private async Task CreateRoom_Handler(RoomType RoomType, string GameModule, string GameMap, bool IsRank, string Password = "") { if (Usercfg.InRoom.Roomid != "-1") { ShowMessage(ShowMessageType.Warning, "已在房间中,无法创建房间。"); return; } - GameModule? module = RunTime.GameModuleLoader?[gameModule]; - if (module is null) + GameModule? mode = RunTime.GameModuleLoader?.Modules.Values.FirstOrDefault() ?? default; + if (mode is null) { ShowMessage(ShowMessageType.Error, ">> 缺少" + Config.FunGame_RoomType + "所需的模组,无法创建房间。"); return; } - string gameModuleServer = module.AssociatedServerModuleName; - Room room = await InvokeController_CreateRoom(roomType, gameModuleServer, gameMap, isRank, module.MaxUsers, password); + Room room = await InvokeController_CreateRoom(RoomType, GameModule, GameMap, IsRank, Password); if (MainController is not null && room.Roomid != "-1") { await MainController.UpdateRoomAsync(); @@ -1075,12 +1088,12 @@ namespace Milimoe.FunGame.Desktop.UI string modmap = ComboGameMap.SelectedItem?.ToString() ?? all; if (RunTime.GameModuleLoader is null || (modname != all && !RunTime.GameModuleLoader.Modules.ContainsKey(modname))) { - ShowMessage(ShowMessageType.Error, "模组未正确加载,无法创建房间。"); + ShowMessage(ShowMessageType.Error, ">> 模组未正确加载,无法创建房间。"); return; } if (RunTime.GameModuleLoader is null || (modmap != all && !RunTime.GameModuleLoader.Maps.ContainsKey(modmap))) { - ShowMessage(ShowMessageType.Error, "地图未正确加载,无法创建房间。"); + ShowMessage(ShowMessageType.Error, ">> 地图未正确加载,无法创建房间。"); return; } // 开始匹配 @@ -1127,12 +1140,12 @@ namespace Milimoe.FunGame.Desktop.UI } if (RunTime.GameModuleLoader is null || (modname != all && !RunTime.GameModuleLoader.Modules.ContainsKey(modname))) { - ShowMessage(ShowMessageType.Error, "模组未正确加载,无法创建房间。"); + ShowMessage(ShowMessageType.Error, ">> 模组未正确加载,无法创建房间。"); return; } if (RunTime.GameModuleLoader is null || (modmap != all && !RunTime.GameModuleLoader.Maps.ContainsKey(modmap))) { - ShowMessage(ShowMessageType.Error, "地图未正确加载,无法创建房间。"); + ShowMessage(ShowMessageType.Error, ">> 地图未正确加载,无法创建房间。"); return; } if (CheckHasPass.Checked) @@ -1152,25 +1165,21 @@ namespace Milimoe.FunGame.Desktop.UI /// /// /// - private void QuitRoom_Click(object sender, EventArgs e) + private async void QuitRoom_Click(object sender, EventArgs e) { string roomid = Usercfg.InRoom.Roomid; bool isMaster = Usercfg.InRoom.RoomMaster?.Id == Usercfg.LoginUser.Id; - bool result = false; - TaskUtility.NewTask(async () => + if (await InvokeController_QuitRoom(Usercfg.InRoom, isMaster)) { - if (await InvokeController_QuitRoom(Usercfg.InRoom, isMaster)) - { - WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 离开房间"); - WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + roomid + " ]"); - InvokeUpdateUI(InMain); - _ = MainController?.UpdateRoomAsync(); - result = true; - } - }).OnCompleted(() => + WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 离开房间"); + WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + roomid + " ]"); + InMain(); + _ = MainController?.UpdateRoomAsync(); + } + else { - if (!result) ShowMessage(ShowMessageType.Error, "无法退出房间!", "退出房间"); - }); + ShowMessage(ShowMessageType.Error, "无法退出房间!", "退出房间"); + } } /// @@ -1199,9 +1208,9 @@ namespace Milimoe.FunGame.Desktop.UI /// /// /// - private void QueryRoom_Click(object sender, EventArgs e) + private async void QueryRoom_Click(object sender, EventArgs e) { - TaskUtility.NewTask(async () => await JoinRoom(RoomText.Text)); + await JoinRoom(RoomText.Text); } /// @@ -1280,7 +1289,7 @@ namespace Milimoe.FunGame.Desktop.UI SetRoomTypeString(); ComboGameMap.Items.Clear(); ComboGameMap.Items.AddRange(Constant.SupportedGameMap(mod)); - ComboGameMap.SelectedItem = mod.DefaultMap; + ComboGameMap.SelectedIndex = 0; } } else @@ -1299,8 +1308,7 @@ namespace Milimoe.FunGame.Desktop.UI { if (RoomList.SelectedItem != null) { - int selected = RoomList.SelectedIndex; - TaskUtility.NewTask(async () => await JoinRoom(selected)); + TaskUtility.NewTask(async () => await JoinRoom(RoomList.SelectedIndex)); } } @@ -1347,13 +1355,13 @@ namespace Milimoe.FunGame.Desktop.UI /// /// /// - private void RoomText_KeyUp(object sender, KeyEventArgs e) + private async void RoomText_KeyUp(object sender, KeyEventArgs e) { RoomText.ForeColor = Color.Black; if (e.KeyCode.Equals(Keys.Enter)) { // 按下回车加入房间 - TaskUtility.NewTask(async () => await JoinRoom(RoomText.Text)); + await JoinRoom(RoomText.Text); } } @@ -1403,7 +1411,7 @@ namespace Milimoe.FunGame.Desktop.UI /// private void Copyright_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { - // Copyright 2023 milimoe + // Copyright 2022-Present milimoe Process.Start(new ProcessStartInfo("https://github.com/milimoe") { UseShellExecute = true }); } @@ -1562,9 +1570,12 @@ namespace Milimoe.FunGame.Desktop.UI /// private void SucceedCreateRoomEvent(object sender, RoomEventArgs e) { - WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 创建" + e.RoomTypeString + "房间"); - WritelnGameInfo(">> 创建" + e.RoomTypeString + "房间成功!房间号: " + e.RoomID); - ShowMessage(ShowMessageType.General, "创建" + e.RoomTypeString + "房间成功!\n房间号是 -> [ " + e.RoomID + " ]", "创建成功"); + InvokeUpdateUI(() => + { + WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 创建" + e.RoomTypeString + "房间"); + WritelnGameInfo(">> 创建" + e.RoomTypeString + "房间成功!房间号: " + e.RoomID); + ShowMessage(ShowMessageType.General, "创建" + e.RoomTypeString + "房间成功!\n房间号是 -> [ " + e.RoomID + " ]", "创建成功"); + }); } #endregion @@ -1633,20 +1644,13 @@ namespace Milimoe.FunGame.Desktop.UI { if (Usercfg.InRoom.Roomid != "-1") { - if (Usercfg.LoginUser.Id == Usercfg.InRoom.RoomMaster.Id) + TaskUtility.NewTask(async () => { - WritelnGameInfo(">> 房主无法使用此命令。"); - } - else - { - TaskUtility.NewTask(async () => + if (await MainController.SetReadyAsync(Usercfg.InRoom.Roomid)) { - if (await MainController.SetReadyAsync(Usercfg.InRoom.Roomid)) - { - await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已准备。"); - } - }); - } + await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已准备。"); + } + }); } else WritelnGameInfo(">> 不在房间中无法使用此命令。"); } @@ -1659,20 +1663,13 @@ namespace Milimoe.FunGame.Desktop.UI { if (Usercfg.InRoom.Roomid != "-1") { - if (Usercfg.LoginUser.Id == Usercfg.InRoom.RoomMaster.Id) + TaskUtility.NewTask(async () => { - WritelnGameInfo(">> 房主无法使用此命令。"); - } - else - { - TaskUtility.NewTask(async () => + if (await MainController.CancelReadyAsync(Usercfg.InRoom.Roomid)) { - if (await MainController.CancelReadyAsync(Usercfg.InRoom.Roomid)) - { - await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已取消准备。"); - } - }); - } + await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已取消准备。"); + } + }); } else WritelnGameInfo(">> 不在房间中无法使用此命令。"); } @@ -1821,7 +1818,7 @@ namespace Milimoe.FunGame.Desktop.UI OnBeforeConnectEvent(this, EventArgs); RunTime.PluginLoader?.OnBeforeConnectEvent(this, EventArgs); if (EventArgs.Cancel) return; - result = RunTime.Controller?.Connect(RunTime.Session.Server_Address, RunTime.Session.Server_Port) ?? result; + result = RunTime.Controller?.Connect(TransmittalType.Socket, RunTime.Session.Server_Address, RunTime.Session.Server_Port) ?? result; EventArgs.ConnectResult = result; }).OnCompleted(() => { @@ -1989,9 +1986,9 @@ namespace Milimoe.FunGame.Desktop.UI /// /// /// - public async Task InvokeController_CreateRoom(RoomType roomType, string gameModuleServer, string gameMap, bool isRank, int maxUsers,string password = "") + public async Task InvokeController_CreateRoom(RoomType RoomType, string GameModule, string GameMap, bool IsRank, string Password = "") { - RoomEventArgs EventArgs = new(roomType, password); + RoomEventArgs EventArgs = new(RoomType, Password); Room room = General.HallInstance; try @@ -2000,7 +1997,7 @@ namespace Milimoe.FunGame.Desktop.UI RunTime.PluginLoader?.OnBeforeCreateRoomEvent(this, EventArgs); if (EventArgs.Cancel) return room; - room = MainController is null ? room : await MainController.CreateRoomAsync(roomType, gameModuleServer, gameMap, isRank, maxUsers, password); + room = MainController is null ? room : await MainController.CreateRoomAsync(RoomType, GameModule, GameMap, IsRank, 8, Password); if (room.Roomid != "-1") { diff --git a/FunGame.Desktop/UI/Register/Register.cs b/FunGame.Desktop/UI/Register/Register.cs index 8c734fb..6c7e508 100644 --- a/FunGame.Desktop/UI/Register/Register.cs +++ b/FunGame.Desktop/UI/Register/Register.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Api.Utility; +using System.ComponentModel; +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; @@ -10,6 +11,7 @@ namespace Milimoe.FunGame.Desktop.UI { public partial class Register : BaseReg { + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public bool CheckReg { get; set; } = false; private readonly RegisterController RegController; @@ -28,77 +30,10 @@ namespace Milimoe.FunGame.Desktop.UI SucceedReg += SucceedRegEvent; } - private async Task Reg_Handler() + private async Task Reg_HandlerAsync(string username, string password, string email) { try { - string username = ""; - string password = ""; - string checkpassword = ""; - string email = ""; - InvokeUpdateUI(() => - { - username = UsernameText.Text.Trim(); - password = PasswordText.Text.Trim(); - checkpassword = CheckPasswordText.Text.Trim(); - email = EmailText.Text.Trim(); - }); - if (username != "") - { - if (NetworkUtility.IsUserName(username)) - { - int length = NetworkUtility.GetUserNameLength(username); - if (length >= 3 && length <= 12) // 字节范围 3~12 - { - if (password != checkpassword) - { - ShowMessage(ShowMessageType.Error, "两个密码不相同,请重新输入!"); - InvokeUpdateUI(() => CheckPasswordText.Focus()); - return false; - } - } - else - { - ShowMessage(ShowMessageType.Error, "账号名长度不符合要求:3~12个字符数(一个中文2个字符)"); - InvokeUpdateUI(() => UsernameText.Focus()); - return false; - } - } - else - { - ShowMessage(ShowMessageType.Error, "账号名不符合要求:不能包含特殊字符"); - InvokeUpdateUI(() => UsernameText.Focus()); - return false; - } - } - if (password != "") - { - int length = password.Length; - if (length < 6 || length > 15) // 字节范围 6~15 - { - ShowMessage(ShowMessageType.Error, "密码长度不符合要求:6~15个字符数"); - InvokeUpdateUI(() => PasswordText.Focus()); - return false; - } - } - if (username == "" || password == "" || checkpassword == "") - { - ShowMessage(ShowMessageType.Error, "请将账号和密码填写完整!"); - InvokeUpdateUI(() => UsernameText.Focus()); - return false; - } - if (email == "") - { - ShowMessage(ShowMessageType.Error, "邮箱不能为空!"); - InvokeUpdateUI(() => EmailText.Focus()); - return false; - } - if (!NetworkUtility.IsEmail(email)) - { - ShowMessage(ShowMessageType.Error, "这不是一个邮箱地址!"); - InvokeUpdateUI(() => EmailText.Focus()); - return false; - } return await RegController.RegAsync(username, password, email); } catch (Exception e) @@ -108,35 +43,98 @@ namespace Milimoe.FunGame.Desktop.UI } } - /// - /// 关闭窗口 - /// - /// - /// private void ExitButton_Click(object sender, EventArgs e) { Dispose(); } - private void SucceedRegEvent(object sender, GeneralEventArgs e) + private async void SucceedRegEvent(object sender, GeneralEventArgs e) { string username = ((RegisterEventArgs)e).Username; string password = ((RegisterEventArgs)e).Password; - TaskUtility.NewTask(async () => await LoginController.LoginAccountAsync(username, password, encrypt: false)); - if (RunTime.Login != null) + await LoginController.LoginAccountAsync(username, password, encrypt: false); + RunTime.Login?.Dispose(); + } + + private async void RegButton_Click(object sender, EventArgs e) + { + RegButton.Enabled = false; + + string username = UsernameText.Text.Trim(); + string password = PasswordText.Text.Trim(); + string checkpassword = CheckPasswordText.Text.Trim(); + string email = EmailText.Text.Trim(); + + if (!ValidateInput(username, password, checkpassword, email)) { - RunTime.Login.InvokeUpdateUI(RunTime.Login.Close); + RegButton.Enabled = true; + return; + } + + bool result = await Reg_HandlerAsync(username, password, email); + + if (!result) + { + RegButton.Enabled = true; + } + else + { + Dispose(); } } - private void RegButton_Click(object sender, EventArgs e) + private bool ValidateInput(string username, string password, string checkpassword, string email) { - RegButton.Enabled = false; - TaskUtility.NewTask(async () => + if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(checkpassword)) { - if (!await Reg_Handler()) InvokeUpdateUI(() => RegButton.Enabled = true); - else InvokeUpdateUI(Close); - }); + ShowMessage(ShowMessageType.Error, "请将账号和密码填写完整!"); + UsernameText.Focus(); + return false; + } + + if (string.IsNullOrEmpty(email)) + { + ShowMessage(ShowMessageType.Error, "邮箱不能为空!"); + EmailText.Focus(); + return false; + } + + if (!NetworkUtility.IsUserName(username)) + { + ShowMessage(ShowMessageType.Error, "账号名不符合要求:不能包含特殊字符"); + UsernameText.Focus(); + return false; + } + + int usernameLength = NetworkUtility.GetUserNameLength(username); + if (usernameLength < 3 || usernameLength > 12) + { + ShowMessage(ShowMessageType.Error, "账号名长度不符合要求:3~12个字符数(一个中文2个字符)"); + UsernameText.Focus(); + return false; + } + + if (password != checkpassword) + { + ShowMessage(ShowMessageType.Error, "两个密码不相同,请重新输入!"); + CheckPasswordText.Focus(); + return false; + } + + if (password.Length < 6 || password.Length > 15) + { + ShowMessage(ShowMessageType.Error, "密码长度不符合要求:6~15个字符数"); + PasswordText.Focus(); + return false; + } + + if (!NetworkUtility.IsEmail(email)) + { + ShowMessage(ShowMessageType.Error, "这不是一个邮箱地址!"); + EmailText.Focus(); + return false; + } + return true; } private void GoToLogin_Click(object sender, EventArgs e) diff --git a/FunGame.Desktop/Utility/OpenForm.cs b/FunGame.Desktop/Utility/OpenForm.cs index 8d47fc8..0811b09 100644 --- a/FunGame.Desktop/Utility/OpenForm.cs +++ b/FunGame.Desktop/Utility/OpenForm.cs @@ -8,7 +8,7 @@ namespace Milimoe.FunGame.Desktop.Utility { public class OpenForm { - public static List
Forms { get; } = new(); + public static List Forms { get; } = []; /// /// @@ -86,8 +86,9 @@ namespace Milimoe.FunGame.Desktop.Utility { try { - if (Singleton.Add(form)) + if (!Singleton.IsExist(form)) { + Singleton.AddOrUpdate(form); if (opentype == OpenFormType.Dialog) form.ShowDialog(); else form.Show(); } diff --git a/FunGame.Implement/FunGame.Implement.csproj b/FunGame.Implement/FunGame.Implement.csproj index b57b6cb..cca44b0 100644 --- a/FunGame.Implement/FunGame.Implement.csproj +++ b/FunGame.Implement/FunGame.Implement.csproj @@ -1,7 +1,7 @@  - net8.0 + net9.0 enable enable Milimoe @@ -25,7 +25,7 @@ - ..\..\FunGame.Core\bin\Debug\net8.0\FunGame.Core.dll + ..\..\FunGame.Core\bin\Debug\net9.0\FunGame.Core.dll