diff --git a/FunGame.Core/Api/Utility/General.cs b/FunGame.Core/Api/Utility/General.cs index eee4658..f71120e 100644 --- a/FunGame.Core/Api/Utility/General.cs +++ b/FunGame.Core/Api/Utility/General.cs @@ -19,21 +19,37 @@ namespace Milimoe.FunGame.Core.Api.Utility /// /// /// - public static bool IsIP(string str) - { - //判断是否为IP - return Regex.IsMatch(str, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); - } + public static bool IsIP(string str) => Regex.IsMatch(str, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); /// /// 判断字符串是否为邮箱地址 /// /// /// - public static bool IsEmail(string str) + public static bool IsEmail(string str) => Regex.IsMatch(str, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$"); + + /// + /// 判断字符串是否是正常的用户名(只有中英文和数字) + /// + /// + /// + public static bool IsUserName(string str) => Regex.IsMatch(str, @"^[\u4e00-\u9fa5A-Za-z0-9]+$"); + + /// + /// 获取用户名长度 + /// + /// + /// + public static int GetUserNameLength(string str) { - //判断是否为Email - return Regex.IsMatch(str, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$"); + int length = 0; + for (int i = 0; i < str.Length; i++) + { + char c = str[i]; + if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9') length++; + else length += 2; + } + return length; } /// diff --git a/FunGame.Core/Api/Utility/INIHelper.cs b/FunGame.Core/Api/Utility/TextReader.cs similarity index 77% rename from FunGame.Core/Api/Utility/INIHelper.cs rename to FunGame.Core/Api/Utility/TextReader.cs index 3119a71..7e75ab0 100644 --- a/FunGame.Core/Api/Utility/INIHelper.cs +++ b/FunGame.Core/Api/Utility/TextReader.cs @@ -1,5 +1,6 @@ -using Milimoe.FunGame.Core.Library.Constant; +using System.IO; using System.Runtime.InteropServices; +using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Api.Utility { @@ -34,8 +35,8 @@ namespace Milimoe.FunGame.Core.Api.Utility /// 读取到的值 public static string ReadINI(string Section, string Key, string FileName = @"FunGame.ini") { - char[] val = new char[2048]; - _ = GetPrivateProfileString(Section, Key, "", val, 2048, Environment.CurrentDirectory.ToString() + @"\" + FileName); + char[] val = new char[General.StreamByteSize]; + _ = GetPrivateProfileString(Section, Key, "", val, General.StreamByteSize, Environment.CurrentDirectory.ToString() + @"\" + FileName); string? read = new(val); return read != null ? read.Trim('\0') : ""; } @@ -45,10 +46,7 @@ namespace Milimoe.FunGame.Core.Api.Utility /// /// 文件名,缺省为FunGame.ini /// 是否存在 - public static bool ExistINIFile(string FileName = @"FunGame.ini") - { - return File.Exists(Environment.CurrentDirectory.ToString() + @"\" + FileName); - } + public static bool ExistINIFile(string FileName = @"FunGame.ini") => File.Exists($@"{Environment.CurrentDirectory}\{FileName}"); /// /// 初始化ini模板文件 @@ -83,6 +81,7 @@ namespace Milimoe.FunGame.Core.Api.Utility WriteINI("Server", "Notice", "This is the FunGame Server's Notice."); WriteINI("Server", "Key", ""); WriteINI("Server", "Status", "1"); + WriteINI("Server", "BannedList", ""); /** * ServerMail */ @@ -116,4 +115,33 @@ namespace Milimoe.FunGame.Core.Api.Utility } } } + + public class TXTHelper + { + /// + /// 读取TXT文件内容 + /// + /// 文件名 + /// 相对路径 + /// 内容 + public static string ReadTXT(string filename, string path = "") + { + if (path.Trim() != "") path = Path.Combine(path, filename); + else path = $@"{Environment.CurrentDirectory}\{filename}"; + if (File.Exists(path)) + { + string s = ""; + // 创建一个 StreamReader 的实例来读取文件 + using StreamReader sr = new(path); + string? line; + // 从文件读取并显示行,直到文件的末尾 + while ((line = sr.ReadLine()) != null) + { + s += line + " "; + } + return s; + } + return ""; + } + } } diff --git a/FunGame.Core/Interface/Base/ISocket.cs b/FunGame.Core/Interface/Base/ISocket.cs index 1ef1d44..c64a26f 100644 --- a/FunGame.Core/Interface/Base/ISocket.cs +++ b/FunGame.Core/Interface/Base/ISocket.cs @@ -5,7 +5,7 @@ namespace Milimoe.FunGame.Core.Interface.Base public interface ISocket { public System.Net.Sockets.Socket Instance { get; } - public int Runtime { get; } + public SocketRuntimeType Runtime { get; } public Guid Token { get; } public string ServerIP { get; } public int ServerPort { get; } diff --git a/FunGame.Core/Library/Common/Network/ClientSocket.cs b/FunGame.Core/Library/Common/Network/ClientSocket.cs index 97f8d15..7c3b70e 100644 --- a/FunGame.Core/Library/Common/Network/ClientSocket.cs +++ b/FunGame.Core/Library/Common/Network/ClientSocket.cs @@ -7,7 +7,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public class ClientSocket : IClientSocket { public System.Net.Sockets.Socket Instance { get; } - public int Runtime { get; } = (int)SocketRuntimeType.Server; + public SocketRuntimeType Runtime => SocketRuntimeType.Server; public Guid Token { get; } = Guid.Empty; public string ServerIP { get; } = ""; public int ServerPort { get; } = 0; diff --git a/FunGame.Core/Library/Common/Network/ServerSocket.cs b/FunGame.Core/Library/Common/Network/ServerSocket.cs index 08748d2..e66b876 100644 --- a/FunGame.Core/Library/Common/Network/ServerSocket.cs +++ b/FunGame.Core/Library/Common/Network/ServerSocket.cs @@ -8,7 +8,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public class ServerSocket : ISocket { public System.Net.Sockets.Socket Instance { get; } - public int Runtime { get; } = (int)SocketRuntimeType.Server; + public SocketRuntimeType Runtime => SocketRuntimeType.Server; public Guid Token { get; } = Guid.Empty; public string ServerIP { get; } = ""; public int ServerPort { get; } = 0; @@ -16,7 +16,9 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public string ServerNotice { get; } = ""; public bool Connected => Instance != null && Instance.Connected; public List GetUsersList => OnlineUsers.GetList(); + public List BannedList { get; } = new(); public int UsersCount => OnlineUsers.Count; + public int BannedCount => BannedList.Count; private readonly ThreadManager OnlineUsers; diff --git a/FunGame.Core/Library/Common/Network/Socket.cs b/FunGame.Core/Library/Common/Network/Socket.cs index a9b14cc..6a85115 100644 --- a/FunGame.Core/Library/Common/Network/Socket.cs +++ b/FunGame.Core/Library/Common/Network/Socket.cs @@ -7,7 +7,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public class Socket : IClientSocket, ISocketHeartBeat { public System.Net.Sockets.Socket Instance { get; } - public int Runtime { get; } = (int)SocketRuntimeType.Client; + public SocketRuntimeType Runtime => SocketRuntimeType.Client; public Guid Token { get; set; } = Guid.Empty; public string ServerIP { get; } = ""; public int ServerPort { get; } = 0; diff --git a/FunGame.Core/Library/Constant/General.cs b/FunGame.Core/Library/Constant/General.cs index baaa29c..68ddddb 100644 --- a/FunGame.Core/Library/Constant/General.cs +++ b/FunGame.Core/Library/Constant/General.cs @@ -16,5 +16,6 @@ namespace Milimoe.FunGame.Core.Library.Constant public const int MaxTask_4C4G = 40; public const int SocketByteSize = 2048; + public const int StreamByteSize = 2048; } } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 5a9e090..9143f7f 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -64,8 +64,8 @@ namespace Milimoe.FunGame.Desktop.UI break; } } + if (Config.FunGame_isAutoConnect) RunTime.Connector?.GetServerConnection(); }); - if (Config.FunGame_isAutoConnect) RunTime.Connector?.GetServerConnection(); } /// @@ -661,10 +661,10 @@ namespace Milimoe.FunGame.Desktop.UI { // 向消息队列发送消息 string text = TalkText.Text; - if (!text.Trim().Equals("") && !TalkText.ForeColor.Equals(Color.DarkGray)) + if (text.Trim() != "" && !TalkText.ForeColor.Equals(Color.DarkGray)) { - string msg = ""; - if (Usercfg.LoginUserName.Equals("")) + string msg; + if (Usercfg.LoginUserName == "") { msg = ":> " + text; } @@ -673,7 +673,7 @@ namespace Milimoe.FunGame.Desktop.UI msg = DateTimeUtility.GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + text; } WritelnGameInfo(msg); - if (!SwitchTalkMessage(text)) + if (Usercfg.LoginUser != null && !SwitchTalkMessage(text)) { MainController?.Chat(msg); } diff --git a/FunGame.Desktop/UI/Register/Register.cs b/FunGame.Desktop/UI/Register/Register.cs index a131147..1f1b7e4 100644 --- a/FunGame.Desktop/UI/Register/Register.cs +++ b/FunGame.Desktop/UI/Register/Register.cs @@ -44,19 +44,28 @@ namespace Milimoe.FunGame.Desktop.UI string email = EmailText.Text.Trim(); if (username != "") { - int length = General.DefaultEncoding.GetBytes(username).Length; - if (length >= 3 && length <= 12) // 字节范围 3~12 + if (NetworkUtility.IsUserName(username)) { - if (password != checkpassword) + int length = NetworkUtility.GetUserNameLength(username); + if (length >= 3 && length <= 12) // 字节范围 3~12 { - ShowMessage.ErrorMessage("两个密码不相同,请重新输入!"); - CheckPasswordText.Focus(); + if (password != checkpassword) + { + ShowMessage.ErrorMessage("两个密码不相同,请重新输入!"); + CheckPasswordText.Focus(); + return false; + } + } + else + { + ShowMessage.ErrorMessage("账号名长度不符合要求:最多6个中文字符或12个英文字符"); + UsernameText.Focus(); return false; } } else { - ShowMessage.ErrorMessage("账号名长度不符合要求:2~6个字符数"); + ShowMessage.ErrorMessage("账号名不符合要求:不能包含特殊字符"); UsernameText.Focus(); return false; }