From 3cb2e4a75f94b545046416cb30d470457cea5050 Mon Sep 17 00:00:00 2001 From: milimoe Date: Mon, 27 Nov 2023 21:46:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0GetGameModeList=E5=92=8C?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=A8=A1=E7=BB=84=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/DataRequestController.cs | 8 +- FunGame.Server/Main.cs | 83 ++++++++++- FunGame.Server/Models/ServerModel.cs | 5 +- FunGame.Server/Others/Config.cs | 130 +++++++++++++++--- 4 files changed, 197 insertions(+), 29 deletions(-) diff --git a/FunGame.Server/Controllers/DataRequestController.cs b/FunGame.Server/Controllers/DataRequestController.cs index a76610c..92bcc85 100644 --- a/FunGame.Server/Controllers/DataRequestController.cs +++ b/FunGame.Server/Controllers/DataRequestController.cs @@ -171,15 +171,15 @@ namespace Milimoe.FunGame.Server.Controller if (RequestData.Count >= 3) { ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> CreateRoom"); - string roomtype_string = DataRequest.GetHashtableJsonObject(RequestData, "roomtype") ?? GameMode.All; + string roomtype_string = DataRequest.GetHashtableJsonObject(RequestData, "roomtype") ?? RoomSet.All; User user = DataRequest.GetHashtableJsonObject(RequestData, "master") ?? Factory.GetUser(); string password = DataRequest.GetHashtableJsonObject(RequestData, "password") ?? ""; 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(); - 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) { ServerHelper.WriteLine("[CreateRoom] Master: " + user.Username + " RoomID: " + roomid); @@ -272,7 +272,7 @@ namespace Milimoe.FunGame.Server.Controller if (!iscancel) { ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(SocketMessageType.DataRequest) + "] " + Server.GetClientName() + " -> MatchRoom : Start"); - string roomtype_string = DataRequest.GetHashtableJsonObject(RequestData, "roomtype") ?? GameMode.All; + string roomtype_string = DataRequest.GetHashtableJsonObject(RequestData, "roomtype") ?? RoomSet.All; User user = DataRequest.GetHashtableJsonObject(RequestData, "matcher") ?? Factory.GetUser(); Server.StartMatching(roomtype_string, user); } diff --git a/FunGame.Server/Main.cs b/FunGame.Server/Main.cs index 1af1d25..ab34cbc 100644 --- a/FunGame.Server/Main.cs +++ b/FunGame.Server/Main.cs @@ -1,5 +1,6 @@ using Milimoe.FunGame; 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.Constant; using Milimoe.FunGame.Server.Model; @@ -56,6 +57,12 @@ void StartServer() // 初始化命令菜单 ServerHelper.InitOrderList(); + // 读取游戏模组 + if (!GetGameModeList()) + { + ServerHelper.WriteLine("服务器似乎未安装任何游戏模组,请检查是否正确安装它们。"); + } + // 检查是否存在配置文件 if (!INIHelper.ExistINIFile()) { @@ -98,9 +105,10 @@ void StartServer() clientip = socket.ClientIP; 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(() => { ClientModel.Start(); @@ -144,7 +152,52 @@ void StartServer() }); } -bool Connect(ClientSocket socket, Guid token, string clientip) +bool GetGameModeList() +{ + // 同时读取Implement预设的模组和gamemods目录下的模组,最后合成一个总的列表 + List 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()) @@ -167,11 +220,33 @@ bool Connect(ClientSocket socket, Guid token, string 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(0) ?? []; + // 参数2:客户端是否开启了开发者模式,开启开发者模式部分功能不可用 + isDebugMode = read.GetParam(1); + if (isDebugMode) ServerHelper.WriteLine("客户端已开启开发者模式"); + + string msg = ""; + List 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) + " <- " + "已确认连接"); return true; } + else if (msg != "" && socket.Send(SocketMessageType.Connect, false, msg) == SocketResult.Success) + { + ServerHelper.WriteLine(ServerHelper.MakeClientName(socket.ClientIP) + " <- " + "拒绝连接"); + return false; + } else { ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。"); diff --git a/FunGame.Server/Models/ServerModel.cs b/FunGame.Server/Models/ServerModel.cs index d67342b..f2fc128 100644 --- a/FunGame.Server/Models/ServerModel.cs +++ b/FunGame.Server/Models/ServerModel.cs @@ -30,6 +30,7 @@ namespace Milimoe.FunGame.Server.Model } public MySQLHelper? SQLHelper { get; } public MailSender? MailSender { get; } + public bool IsDebugMode { get; } /** * Private @@ -52,7 +53,7 @@ namespace Milimoe.FunGame.Server.Model private long LogoutTime; private bool IsMatching; - public ServerModel(ServerSocket server, ClientSocket socket, bool running) + public ServerModel(ServerSocket server, ClientSocket socket, bool running, bool isDebugMode) { Server = server; _Socket = socket; @@ -394,7 +395,7 @@ namespace Milimoe.FunGame.Server.Model { if (IsMatching) { - RoomType roomtype = GameMode.GetRoomType(roomtype_string); + RoomType roomtype = RoomSet.GetRoomType(roomtype_string); Room room = await MatchingRoom(roomtype, user); if (IsMatching && Socket != null) { diff --git a/FunGame.Server/Others/Config.cs b/FunGame.Server/Others/Config.cs index 7cdb9e3..b74ae36 100644 --- a/FunGame.Server/Others/Config.cs +++ b/FunGame.Server/Others/Config.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Text; using Milimoe.FunGame.Core.Api.Transmittal; +using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.SQLScript.Common; using Milimoe.FunGame.Core.Library.SQLScript.Entity; @@ -11,26 +12,106 @@ namespace Milimoe.FunGame.Server.Others { public static class Config { - public static string ServerName { get; set; } = "FunGame Server"; // 服务器名称 - public static int ServerPort { get; set; } = 22222; // 默认端口 - public static int ServerStatus { get; set; } = 1; // 默认状态:1可连接 0不可连接 -1不可用 - public static string ServerNotice { get; set; } = ""; // 服务器的公告 - public static string ServerPassword { get; set; } = ""; // 服务器的密码 - public static string ServerDescription { get; set; } = ""; // 服务器的描述 - public static string ServerKey { get; set; } = ""; // 注册社区服务器的Key - public static string ServerBannedList { get; set; } = ""; // 禁止连接的黑名单 - public static int MaxPlayers { get; set; } = 20; // 最多接受连接的玩家数量 - public static int MaxConnectionFaileds { get; set; } = 5; // 最大连接失败次数 - public static int OnlinePlayerCount { get; set; } = 0; // 已连接的玩家数量 - public static int ConnectingPlayerCount { get; set; } = 0; // 正在连接的玩家数量 - public static Encoding DefaultEncoding { get; } = General.DefaultEncoding; // 默认传输字符集 - public static FunGameInfo.FunGame FunGameType { get; } = FunGameInfo.FunGame.FunGame_Server; // FunGame Runtime - public static Hashtable OrderList { get; } = new(); // 服务器指令列表 - public static RoomList RoomList { get; } = new(); // 在线房间列表 - public static bool SQLMode { get; set; } = false; // 是否运行数据库模式 + /// + /// 服务器名称 + /// + public static string ServerName { get; set; } = "FunGame Server"; + + /// + /// 默认端口 + /// + public static int ServerPort { get; set; } = 22222; + + /// + /// 默认状态:1可连接 0不可连接 -1不可用 + /// + public static int ServerStatus { get; set; } = 1; + + /// + /// 服务器的公告 + /// + public static string ServerNotice { get; set; } = ""; + + /// + /// 服务器的密码 + /// + public static string ServerPassword { get; set; } = ""; + + /// + /// 服务器的描述 + /// + public static string ServerDescription { get; set; } = ""; + + /// + /// 注册社区服务器的Key + /// + public static string ServerKey { get; set; } = ""; + + /// + /// 禁止连接的黑名单 + /// + public static string ServerBannedList { get; set; } = ""; + + /// + /// 最多接受连接的玩家数量 + /// + public static int MaxPlayers { get; set; } = 20; + + /// + /// 最大连接失败次数 + /// + public static int MaxConnectionFaileds { get; set; } = 5; + + /// + /// 已连接的玩家数量 + /// + public static int OnlinePlayerCount { get; set; } = 0; + + /// + /// 正在连接的玩家数量 + /// + public static int ConnectingPlayerCount { get; set; } = 0; + + /// + /// 默认传输字符集 + /// + public static Encoding DefaultEncoding { get; } = General.DefaultEncoding; + + /// + /// FunGame Runtime + /// + public static FunGameInfo.FunGame FunGameType { get; } = FunGameInfo.FunGame.FunGame_Server; + + /// + /// 服务器指令列表 + /// + public static Hashtable OrderList { get; } = []; + + /// + /// 在线房间列表 + /// + public static RoomList RoomList { get; } = new(); + + /// + /// 是否运行数据库模式 + /// + public static bool SQLMode { get; set; } = false; + + /// + /// Server实际安装的模组 + /// + public static GameModeLoader? GameModeLoader { get; set; } + + /// + /// 未LoadGameMods时,此属性表示至少需要安装的模组 + /// + public static string[] GameModeSupported { get; set; } = []; + + /// + /// 全局数据库连接器 + /// public static SQLHelper SQLHelper { - // 全局数据库连接器 get { if (_SQLHelper is null) throw new MySQLConfigException(); @@ -40,6 +121,9 @@ namespace Milimoe.FunGame.Server.Others private static SQLHelper? _SQLHelper; + /// + /// 初始化数据库连接器 + /// public static void InitSQLHelper() { try @@ -58,6 +142,9 @@ namespace Milimoe.FunGame.Server.Others } } + /// + /// 服务器启动登记 + /// public static void ServerLogin() { if (SQLMode) @@ -66,16 +153,21 @@ namespace Milimoe.FunGame.Server.Others } } + /// + /// 重启服务器后,所有房间都会被删除 + /// public static void ClearRoomList() { if (SQLMode) { - // 重启服务器后,所有房间都会被删除 SQLHelper.Execute(RoomQuery.Delete_Rooms()); } } } + /// + /// 此服务器的联系邮箱 + /// public static class OfficialEmail { public static string Email { get; set; } = "";