添加GetGameModeList和客户端模组验证

This commit is contained in:
milimoe 2023-11-27 21:46:13 +08:00
parent c5e8b9acce
commit 3cb2e4a75f
Signed by: milimoe
GPG Key ID: 05D280912DA6C69E
4 changed files with 197 additions and 29 deletions

View File

@ -171,15 +171,15 @@ namespace Milimoe.FunGame.Server.Controller
if (RequestData.Count >= 3) if (RequestData.Count >= 3)
{ {
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> CreateRoom"); ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> CreateRoom");
string roomtype_string = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomtype") ?? GameMode.All; string roomtype_string = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomtype") ?? RoomSet.All;
User user = DataRequest.GetHashtableJsonObject<User>(RequestData, "master") ?? Factory.GetUser(); User user = DataRequest.GetHashtableJsonObject<User>(RequestData, "master") ?? Factory.GetUser();
string password = DataRequest.GetHashtableJsonObject<string>(RequestData, "password") ?? ""; string password = DataRequest.GetHashtableJsonObject<string>(RequestData, "password") ?? "";
if (!string.IsNullOrWhiteSpace(roomtype_string) && user.Id != 0) if (!string.IsNullOrWhiteSpace(roomtype_string) && user.Id != 0)
{ {
RoomType roomtype = GameMode.GetRoomType(roomtype_string); RoomType roomtype = RoomSet.GetRoomType(roomtype_string);
string roomid = Verification.CreateVerifyCode(VerifyCodeType.MixVerifyCode, 7).ToUpper(); string roomid = Verification.CreateVerifyCode(VerifyCodeType.MixVerifyCode, 7).ToUpper();
SQLHelper.Execute(RoomQuery.Insert_CreateRoom(roomid, user.Id, roomtype, password ?? "")); SQLHelper.Execute(RoomQuery.Insert_CreateRoom(roomid, user.Id, roomtype, "", "", password ?? ""));
if (SQLHelper.Result == SQLResult.Success) if (SQLHelper.Result == SQLResult.Success)
{ {
ServerHelper.WriteLine("[CreateRoom] Master: " + user.Username + " RoomID: " + roomid); ServerHelper.WriteLine("[CreateRoom] Master: " + user.Username + " RoomID: " + roomid);
@ -272,7 +272,7 @@ namespace Milimoe.FunGame.Server.Controller
if (!iscancel) if (!iscancel)
{ {
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> MatchRoom : Start"); ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> MatchRoom : Start");
string roomtype_string = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomtype") ?? GameMode.All; string roomtype_string = DataRequest.GetHashtableJsonObject<string>(RequestData, "roomtype") ?? RoomSet.All;
User user = DataRequest.GetHashtableJsonObject<User>(RequestData, "matcher") ?? Factory.GetUser(); User user = DataRequest.GetHashtableJsonObject<User>(RequestData, "matcher") ?? Factory.GetUser();
Server.StartMatching(roomtype_string, user); Server.StartMatching(roomtype_string, user);
} }

View File

@ -1,5 +1,6 @@
using Milimoe.FunGame; using Milimoe.FunGame;
using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Common.Addon;
using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Server.Model; using Milimoe.FunGame.Server.Model;
@ -56,6 +57,12 @@ void StartServer()
// 初始化命令菜单 // 初始化命令菜单
ServerHelper.InitOrderList(); ServerHelper.InitOrderList();
// 读取游戏模组
if (!GetGameModeList())
{
ServerHelper.WriteLine("服务器似乎未安装任何游戏模组,请检查是否正确安装它们。");
}
// 检查是否存在配置文件 // 检查是否存在配置文件
if (!INIHelper.ExistINIFile()) if (!INIHelper.ExistINIFile())
{ {
@ -98,9 +105,10 @@ void StartServer()
clientip = socket.ClientIP; clientip = socket.ClientIP;
Config.ConnectingPlayerCount++; Config.ConnectingPlayerCount++;
// 开始处理客户端连接请求 // 开始处理客户端连接请求
if (Connect(socket, token, clientip)) bool isDebugMode = false;
if (Connect(socket, token, clientip, ref isDebugMode))
{ {
ServerModel ClientModel = new(ListeningSocket, socket, Running); ServerModel ClientModel = new(ListeningSocket, socket, Running, isDebugMode);
Task t = Task.Factory.StartNew(() => Task t = Task.Factory.StartNew(() =>
{ {
ClientModel.Start(); ClientModel.Start();
@ -144,7 +152,52 @@ void StartServer()
}); });
} }
bool Connect(ClientSocket socket, Guid token, string clientip) bool GetGameModeList()
{
// 同时读取Implement预设的模组和gamemods目录下的模组最后合成一个总的列表
List<string> supported = [];
GameModeLoader loader = GameModeLoader.LoadGameModes();
string[] mods = (string[]?)Implement.GetFunGameImplValue(InterfaceType.IGameModeSupported, InterfaceMethod.GameModeList, false) ?? [];
if (mods.Length > 0)
{
supported.AddRange(mods);
// 检查已安装的模组中是否在Implement预设的模组中存在
foreach (string mod in mods)
{
if (!loader.Modes.ContainsKey(mod))
{
ServerHelper.WriteLine("[GameMode] Load Failed: " + mod + " 不存在或未正确安装");
supported.Remove(mod);
}
}
}
// 检查模组是否有相对应的地图
foreach (GameMode mode in loader.Modes.Values)
{
string modename = mode.Name;
if (loader.Maps.ContainsKey(mode.Map))
{
supported.Add(modename);
}
else
{
ServerHelper.WriteLine("[GameMode] Load Failed: " + modename + " 没有找到相对应的地图,加载失败");
loader.Modes.Remove(modename);
supported.Remove(modename);
}
}
// 设置全局
Config.GameModeSupported = supported.Distinct().ToArray();
foreach (string modename in Config.GameModeSupported)
{
ServerHelper.WriteLine("[GameMode] Loaded: " + modename);
}
return Config.GameModeSupported.Length > 0;
}
bool Connect(ClientSocket socket, Guid token, string clientip, ref bool isDebugMode)
{ {
// 接收客户端消息 // 接收客户端消息
foreach (SocketObject read in socket.ReceiveArray()) foreach (SocketObject read in socket.ReceiveArray())
@ -167,11 +220,33 @@ bool Connect(ClientSocket socket, Guid token, string clientip)
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(read.SocketType) + "] " + ServerHelper.MakeClientName(socket.ClientIP)); ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(read.SocketType) + "] " + ServerHelper.MakeClientName(socket.ClientIP));
if (socket.Send(SocketMessageType.Connect, true, "", token, Config.ServerName, Config.ServerNotice) == SocketResult.Success) // 读取参数
// 参数1客户端的游戏模组列表没有服务器的需要拒绝
string[] modes = read.GetParam<string[]>(0) ?? [];
// 参数2客户端是否开启了开发者模式开启开发者模式部分功能不可用
isDebugMode = read.GetParam<bool>(1);
if (isDebugMode) ServerHelper.WriteLine("客户端已开启开发者模式");
string msg = "";
List<string> ClientDontHave = [];
string strDontHave = string.Join("\r\n", Config.GameModeSupported.Where(mode => !modes.Contains(mode)));
if (strDontHave != "")
{
strDontHave = "客户端缺少服务器所需的模组:" + strDontHave;
ServerHelper.WriteLine(strDontHave);
msg += strDontHave;
}
if (msg == "" && socket.Send(SocketMessageType.Connect, true, msg, token, Config.ServerName, Config.ServerNotice) == SocketResult.Success)
{ {
ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "已确认连接"); ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "已确认连接");
return true; return true;
} }
else if (msg != "" && socket.Send(SocketMessageType.Connect, false, msg) == SocketResult.Success)
{
ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "拒绝连接");
return false;
}
else else
{ {
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。"); ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");

View File

@ -30,6 +30,7 @@ namespace Milimoe.FunGame.Server.Model
} }
public MySQLHelper? SQLHelper { get; } public MySQLHelper? SQLHelper { get; }
public MailSender? MailSender { get; } public MailSender? MailSender { get; }
public bool IsDebugMode { get; }
/** /**
* Private * Private
@ -52,7 +53,7 @@ namespace Milimoe.FunGame.Server.Model
private long LogoutTime; private long LogoutTime;
private bool IsMatching; private bool IsMatching;
public ServerModel(ServerSocket server, ClientSocket socket, bool running) public ServerModel(ServerSocket server, ClientSocket socket, bool running, bool isDebugMode)
{ {
Server = server; Server = server;
_Socket = socket; _Socket = socket;
@ -394,7 +395,7 @@ namespace Milimoe.FunGame.Server.Model
{ {
if (IsMatching) if (IsMatching)
{ {
RoomType roomtype = GameMode.GetRoomType(roomtype_string); RoomType roomtype = RoomSet.GetRoomType(roomtype_string);
Room room = await MatchingRoom(roomtype, user); Room room = await MatchingRoom(roomtype, user);
if (IsMatching && Socket != null) if (IsMatching && Socket != null)
{ {

View File

@ -1,6 +1,7 @@
using System.Collections; using System.Collections;
using System.Text; using System.Text;
using Milimoe.FunGame.Core.Api.Transmittal; using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.SQLScript.Common; using Milimoe.FunGame.Core.Library.SQLScript.Common;
using Milimoe.FunGame.Core.Library.SQLScript.Entity; using Milimoe.FunGame.Core.Library.SQLScript.Entity;
@ -11,26 +12,106 @@ namespace Milimoe.FunGame.Server.Others
{ {
public static class Config public static class Config
{ {
public static string ServerName { get; set; } = "FunGame Server"; // 服务器名称 /// <summary>
public static int ServerPort { get; set; } = 22222; // 默认端口 /// 服务器名称
public static int ServerStatus { get; set; } = 1; // 默认状态1可连接 0不可连接 -1不可用 /// </summary>
public static string ServerNotice { get; set; } = ""; // 服务器的公告 public static string ServerName { get; set; } = "FunGame Server";
public static string ServerPassword { get; set; } = ""; // 服务器的密码
public static string ServerDescription { get; set; } = ""; // 服务器的描述 /// <summary>
public static string ServerKey { get; set; } = ""; // 注册社区服务器的Key /// 默认端口
public static string ServerBannedList { get; set; } = ""; // 禁止连接的黑名单 /// </summary>
public static int MaxPlayers { get; set; } = 20; // 最多接受连接的玩家数量 public static int ServerPort { get; set; } = 22222;
public static int MaxConnectionFaileds { get; set; } = 5; // 最大连接失败次数
public static int OnlinePlayerCount { get; set; } = 0; // 已连接的玩家数量 /// <summary>
public static int ConnectingPlayerCount { get; set; } = 0; // 正在连接的玩家数量 /// 默认状态1可连接 0不可连接 -1不可用
public static Encoding DefaultEncoding { get; } = General.DefaultEncoding; // 默认传输字符集 /// </summary>
public static FunGameInfo.FunGame FunGameType { get; } = FunGameInfo.FunGame.FunGame_Server; // FunGame Runtime public static int ServerStatus { get; set; } = 1;
public static Hashtable OrderList { get; } = new(); // 服务器指令列表
public static RoomList RoomList { get; } = new(); // 在线房间列表 /// <summary>
public static bool SQLMode { get; set; } = false; // 是否运行数据库模式 /// 服务器的公告
/// </summary>
public static string ServerNotice { get; set; } = "";
/// <summary>
/// 服务器的密码
/// </summary>
public static string ServerPassword { get; set; } = "";
/// <summary>
/// 服务器的描述
/// </summary>
public static string ServerDescription { get; set; } = "";
/// <summary>
/// 注册社区服务器的Key
/// </summary>
public static string ServerKey { get; set; } = "";
/// <summary>
/// 禁止连接的黑名单
/// </summary>
public static string ServerBannedList { get; set; } = "";
/// <summary>
/// 最多接受连接的玩家数量
/// </summary>
public static int MaxPlayers { get; set; } = 20;
/// <summary>
/// 最大连接失败次数
/// </summary>
public static int MaxConnectionFaileds { get; set; } = 5;
/// <summary>
/// 已连接的玩家数量
/// </summary>
public static int OnlinePlayerCount { get; set; } = 0;
/// <summary>
/// 正在连接的玩家数量
/// </summary>
public static int ConnectingPlayerCount { get; set; } = 0;
/// <summary>
/// 默认传输字符集
/// </summary>
public static Encoding DefaultEncoding { get; } = General.DefaultEncoding;
/// <summary>
/// FunGame Runtime
/// </summary>
public static FunGameInfo.FunGame FunGameType { get; } = FunGameInfo.FunGame.FunGame_Server;
/// <summary>
/// 服务器指令列表
/// </summary>
public static Hashtable OrderList { get; } = [];
/// <summary>
/// 在线房间列表
/// </summary>
public static RoomList RoomList { get; } = new();
/// <summary>
/// 是否运行数据库模式
/// </summary>
public static bool SQLMode { get; set; } = false;
/// <summary>
/// Server实际安装的模组
/// </summary>
public static GameModeLoader? GameModeLoader { get; set; }
/// <summary>
/// 未LoadGameMods时此属性表示至少需要安装的模组
/// </summary>
public static string[] GameModeSupported { get; set; } = [];
/// <summary>
/// 全局数据库连接器
/// </summary>
public static SQLHelper SQLHelper public static SQLHelper SQLHelper
{ {
// 全局数据库连接器
get get
{ {
if (_SQLHelper is null) throw new MySQLConfigException(); if (_SQLHelper is null) throw new MySQLConfigException();
@ -40,6 +121,9 @@ namespace Milimoe.FunGame.Server.Others
private static SQLHelper? _SQLHelper; private static SQLHelper? _SQLHelper;
/// <summary>
/// 初始化数据库连接器
/// </summary>
public static void InitSQLHelper() public static void InitSQLHelper()
{ {
try try
@ -58,6 +142,9 @@ namespace Milimoe.FunGame.Server.Others
} }
} }
/// <summary>
/// 服务器启动登记
/// </summary>
public static void ServerLogin() public static void ServerLogin()
{ {
if (SQLMode) if (SQLMode)
@ -66,16 +153,21 @@ namespace Milimoe.FunGame.Server.Others
} }
} }
/// <summary>
/// 重启服务器后,所有房间都会被删除
/// </summary>
public static void ClearRoomList() public static void ClearRoomList()
{ {
if (SQLMode) if (SQLMode)
{ {
// 重启服务器后,所有房间都会被删除
SQLHelper.Execute(RoomQuery.Delete_Rooms()); SQLHelper.Execute(RoomQuery.Delete_Rooms());
} }
} }
} }
/// <summary>
/// 此服务器的联系邮箱
/// </summary>
public static class OfficialEmail public static class OfficialEmail
{ {
public static string Email { get; set; } = ""; public static string Email { get; set; } = "";