优化注册检查,添加BannedList,添加TXTHelper

This commit is contained in:
Mili 2023-03-12 11:54:21 +08:00
parent 42bd8b18a9
commit 62193971b4
9 changed files with 86 additions and 30 deletions

View File

@ -19,21 +19,37 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// </summary> /// </summary>
/// <param name="str"></param> /// <param name="str"></param>
/// <returns></returns> /// <returns></returns>
public static bool IsIP(string str) 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?)$");
{
//判断是否为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?)$");
}
/// <summary> /// <summary>
/// 判断字符串是否为邮箱地址 /// 判断字符串是否为邮箱地址
/// </summary> /// </summary>
/// <param name="str"></param> /// <param name="str"></param>
/// <returns></returns> /// <returns></returns>
public static bool IsEmail(string str) public static bool IsEmail(string str) => Regex.IsMatch(str, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
/// <summary>
/// 判断字符串是否是正常的用户名(只有中英文和数字)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsUserName(string str) => Regex.IsMatch(str, @"^[\u4e00-\u9fa5A-Za-z0-9]+$");
/// <summary>
/// 获取用户名长度
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int GetUserNameLength(string str)
{ {
//判断是否为Email int length = 0;
return Regex.IsMatch(str, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$"); 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;
} }
/// <summary> /// <summary>

View File

@ -1,5 +1,6 @@
using Milimoe.FunGame.Core.Library.Constant; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Api.Utility namespace Milimoe.FunGame.Core.Api.Utility
{ {
@ -34,8 +35,8 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <returns>读取到的值</returns> /// <returns>读取到的值</returns>
public static string ReadINI(string Section, string Key, string FileName = @"FunGame.ini") public static string ReadINI(string Section, string Key, string FileName = @"FunGame.ini")
{ {
char[] val = new char[2048]; char[] val = new char[General.StreamByteSize];
_ = GetPrivateProfileString(Section, Key, "", val, 2048, Environment.CurrentDirectory.ToString() + @"\" + FileName); _ = GetPrivateProfileString(Section, Key, "", val, General.StreamByteSize, Environment.CurrentDirectory.ToString() + @"\" + FileName);
string? read = new(val); string? read = new(val);
return read != null ? read.Trim('\0') : ""; return read != null ? read.Trim('\0') : "";
} }
@ -45,10 +46,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// </summary> /// </summary>
/// <param name="FileName">文件名缺省为FunGame.ini</param> /// <param name="FileName">文件名缺省为FunGame.ini</param>
/// <returns>是否存在</returns> /// <returns>是否存在</returns>
public static bool ExistINIFile(string FileName = @"FunGame.ini") public static bool ExistINIFile(string FileName = @"FunGame.ini") => File.Exists($@"{Environment.CurrentDirectory}\{FileName}");
{
return File.Exists(Environment.CurrentDirectory.ToString() + @"\" + FileName);
}
/// <summary> /// <summary>
/// 初始化ini模板文件 /// 初始化ini模板文件
@ -83,6 +81,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
WriteINI("Server", "Notice", "This is the FunGame Server's Notice."); WriteINI("Server", "Notice", "This is the FunGame Server's Notice.");
WriteINI("Server", "Key", ""); WriteINI("Server", "Key", "");
WriteINI("Server", "Status", "1"); WriteINI("Server", "Status", "1");
WriteINI("Server", "BannedList", "");
/** /**
* ServerMail * ServerMail
*/ */
@ -116,4 +115,33 @@ namespace Milimoe.FunGame.Core.Api.Utility
} }
} }
} }
public class TXTHelper
{
/// <summary>
/// 读取TXT文件内容
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="path">相对路径</param>
/// <returns>内容</returns>
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 "";
}
}
} }

View File

@ -5,7 +5,7 @@ namespace Milimoe.FunGame.Core.Interface.Base
public interface ISocket public interface ISocket
{ {
public System.Net.Sockets.Socket Instance { get; } public System.Net.Sockets.Socket Instance { get; }
public int Runtime { get; } public SocketRuntimeType Runtime { get; }
public Guid Token { get; } public Guid Token { get; }
public string ServerIP { get; } public string ServerIP { get; }
public int ServerPort { get; } public int ServerPort { get; }

View File

@ -7,7 +7,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public class ClientSocket : IClientSocket public class ClientSocket : IClientSocket
{ {
public System.Net.Sockets.Socket Instance { get; } 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 Guid Token { get; } = Guid.Empty;
public string ServerIP { get; } = ""; public string ServerIP { get; } = "";
public int ServerPort { get; } = 0; public int ServerPort { get; } = 0;

View File

@ -8,7 +8,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public class ServerSocket : ISocket public class ServerSocket : ISocket
{ {
public System.Net.Sockets.Socket Instance { get; } 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 Guid Token { get; } = Guid.Empty;
public string ServerIP { get; } = ""; public string ServerIP { get; } = "";
public int ServerPort { get; } = 0; public int ServerPort { get; } = 0;
@ -16,7 +16,9 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public string ServerNotice { get; } = ""; public string ServerNotice { get; } = "";
public bool Connected => Instance != null && Instance.Connected; public bool Connected => Instance != null && Instance.Connected;
public List<BaseModel> GetUsersList => OnlineUsers.GetList(); public List<BaseModel> GetUsersList => OnlineUsers.GetList();
public List<string> BannedList { get; } = new();
public int UsersCount => OnlineUsers.Count; public int UsersCount => OnlineUsers.Count;
public int BannedCount => BannedList.Count;
private readonly ThreadManager OnlineUsers; private readonly ThreadManager OnlineUsers;

View File

@ -7,7 +7,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public class Socket : IClientSocket, ISocketHeartBeat public class Socket : IClientSocket, ISocketHeartBeat
{ {
public System.Net.Sockets.Socket Instance { get; } 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 Guid Token { get; set; } = Guid.Empty;
public string ServerIP { get; } = ""; public string ServerIP { get; } = "";
public int ServerPort { get; } = 0; public int ServerPort { get; } = 0;

View File

@ -16,5 +16,6 @@ namespace Milimoe.FunGame.Core.Library.Constant
public const int MaxTask_4C4G = 40; public const int MaxTask_4C4G = 40;
public const int SocketByteSize = 2048; public const int SocketByteSize = 2048;
public const int StreamByteSize = 2048;
} }
} }

View File

@ -64,8 +64,8 @@ namespace Milimoe.FunGame.Desktop.UI
break; break;
} }
} }
if (Config.FunGame_isAutoConnect) RunTime.Connector?.GetServerConnection();
}); });
if (Config.FunGame_isAutoConnect) RunTime.Connector?.GetServerConnection();
} }
/// <summary> /// <summary>
@ -661,10 +661,10 @@ namespace Milimoe.FunGame.Desktop.UI
{ {
// 向消息队列发送消息 // 向消息队列发送消息
string text = TalkText.Text; string text = TalkText.Text;
if (!text.Trim().Equals("") && !TalkText.ForeColor.Equals(Color.DarkGray)) if (text.Trim() != "" && !TalkText.ForeColor.Equals(Color.DarkGray))
{ {
string msg = ""; string msg;
if (Usercfg.LoginUserName.Equals("")) if (Usercfg.LoginUserName == "")
{ {
msg = ":> " + text; msg = ":> " + text;
} }
@ -673,7 +673,7 @@ namespace Milimoe.FunGame.Desktop.UI
msg = DateTimeUtility.GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + text; msg = DateTimeUtility.GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " + text;
} }
WritelnGameInfo(msg); WritelnGameInfo(msg);
if (!SwitchTalkMessage(text)) if (Usercfg.LoginUser != null && !SwitchTalkMessage(text))
{ {
MainController?.Chat(msg); MainController?.Chat(msg);
} }

View File

@ -44,19 +44,28 @@ namespace Milimoe.FunGame.Desktop.UI
string email = EmailText.Text.Trim(); string email = EmailText.Text.Trim();
if (username != "") if (username != "")
{ {
int length = General.DefaultEncoding.GetBytes(username).Length; if (NetworkUtility.IsUserName(username))
if (length >= 3 && length <= 12) // 字节范围 3~12
{ {
if (password != checkpassword) int length = NetworkUtility.GetUserNameLength(username);
if (length >= 3 && length <= 12) // 字节范围 3~12
{ {
ShowMessage.ErrorMessage("两个密码不相同,请重新输入!"); if (password != checkpassword)
CheckPasswordText.Focus(); {
ShowMessage.ErrorMessage("两个密码不相同,请重新输入!");
CheckPasswordText.Focus();
return false;
}
}
else
{
ShowMessage.ErrorMessage("账号名长度不符合要求最多6个中文字符或12个英文字符");
UsernameText.Focus();
return false; return false;
} }
} }
else else
{ {
ShowMessage.ErrorMessage("账号名长度不符合要求2~6个字符数"); ShowMessage.ErrorMessage("账号名不符合要求:不能包含特殊字符");
UsernameText.Focus(); UsernameText.Focus();
return false; return false;
} }