From 309c2ab4a2ff8e3425d63942a8137b1bd8b93135 Mon Sep 17 00:00:00 2001 From: Mili Date: Sun, 18 Sep 2022 17:20:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGameServer/Models/Config/Config.cs | 5 +- .../{Sockets => ServerCore}/ClientSocket.cs | 25 ++- FunGameServer/{ => ServerCore}/Main.cs | 11 +- FunGameServer/Utils/DataHelper.cs | 64 ------ FunGameServer/Utils/ServerHelper.cs | 79 -------- FunGameServer/Utils/SocketHelper.cs | 34 ---- FunGameServer/Utils/Utility.cs | 187 ++++++++++++++++++ 7 files changed, 218 insertions(+), 187 deletions(-) rename FunGameServer/{Sockets => ServerCore}/ClientSocket.cs (92%) rename FunGameServer/{ => ServerCore}/Main.cs (96%) delete mode 100644 FunGameServer/Utils/DataHelper.cs delete mode 100644 FunGameServer/Utils/ServerHelper.cs delete mode 100644 FunGameServer/Utils/SocketHelper.cs create mode 100644 FunGameServer/Utils/Utility.cs diff --git a/FunGameServer/Models/Config/Config.cs b/FunGameServer/Models/Config/Config.cs index f84f57c..cc8a4cc 100644 --- a/FunGameServer/Models/Config/Config.cs +++ b/FunGameServer/Models/Config/Config.cs @@ -30,10 +30,7 @@ namespace FunGameServer.Models.Config public static FunGameEnums.FunGame FunGameType = FunGameEnums.FunGame.FunGame_Server; public static Hashtable OrderList = new(); - - public static AssemblyHelper DefaultAssemblyHelper = new(); - public static DataHelper DefaultDataHelper = new(); - public static INIHelper DefaultINIHelper = new(); + public static ReflectionHelper ReflectionHelper = new(); /// /// string: 玩家标识ID diff --git a/FunGameServer/Sockets/ClientSocket.cs b/FunGameServer/ServerCore/ClientSocket.cs similarity index 92% rename from FunGameServer/Sockets/ClientSocket.cs rename to FunGameServer/ServerCore/ClientSocket.cs index a1ed900..1df6c0d 100644 --- a/FunGameServer/Sockets/ClientSocket.cs +++ b/FunGameServer/ServerCore/ClientSocket.cs @@ -14,7 +14,7 @@ using System.Net; using FunGame.Core.Api.Model.Enum; using MySqlX.XDevAPI.Common; -namespace FunGameServer.Sockets +namespace FunGameServer.ServerCore { public class ClientSocket { @@ -199,4 +199,27 @@ namespace FunGameServer.Sockets } } } + + public class SocketHelper + { + public static int GetType(string msg) + { + int index = msg.IndexOf(';') - 1; + if (index > 0) + return Convert.ToInt32(msg[..index]); + else + return Convert.ToInt32(msg[..1]); + } + + public static string GetMessage(string msg) + { + int index = msg.IndexOf(';') + 1; + return msg[index..]; + } + + public static string MakeMessage(int type, string msg) + { + return type + ";" + msg; + } + } } diff --git a/FunGameServer/Main.cs b/FunGameServer/ServerCore/Main.cs similarity index 96% rename from FunGameServer/Main.cs rename to FunGameServer/ServerCore/Main.cs index 2c16c24..fc76b78 100644 --- a/FunGameServer/Main.cs +++ b/FunGameServer/ServerCore/Main.cs @@ -3,11 +3,12 @@ using System.Net; using System.Text; using System.Text.RegularExpressions; using System; -using FunGameServer.Sockets; using System.Net.WebSockets; using FunGameServer.Models.Config; using FunGameServer.Utils; using FunGame.Core.Api.Model.Enum; +using FunGame.Core.Api.Util; +using FunGameServer.ServerCore; Console.Title = Config.SERVER_NAME; Console.WriteLine(FunGameEnums.GetInfo(Config.FunGameType)); @@ -61,10 +62,10 @@ void StartServer() ServerHelper.InitOrderList(); // 检查是否存在配置文件 - if (!Config.DefaultINIHelper.ExistINIFile()) + if (!INIHelper.ExistINIFile()) { ServerHelper.WriteLine("未检测到配置文件,将自动创建配置文件 . . ."); - Config.DefaultINIHelper.InitServerConfigs(); + INIHelper.Init(Config.FunGameType); ServerHelper.WriteLine("配置文件FunGame.ini创建成功,请修改该配置文件,然后重启服务器。"); ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。"); return; @@ -75,10 +76,10 @@ void StartServer() Console.Title = Config.SERVER_NAME + " - FunGame Server Port: " + Config.SERVER_PORT; } - Config.DefaultDataHelper.Close(); + DataHelper.Close(); // 连接MySQL服务器 - if (!Config.DefaultDataHelper.Connect()) + if (!DataHelper.Connect()) { Running = false; throw new Exception("服务器遇到问题需要关闭,请重新启动服务器!"); diff --git a/FunGameServer/Utils/DataHelper.cs b/FunGameServer/Utils/DataHelper.cs deleted file mode 100644 index b0ad685..0000000 --- a/FunGameServer/Utils/DataHelper.cs +++ /dev/null @@ -1,64 +0,0 @@ -using FunGame.Core.Api.Model.Entity; -using FunGame.Core.Api.Model.Enum; -using FunGameServer.Models.Config; -using MySql.Data.MySqlClient; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FunGameServer.Utils -{ - public class DataHelper - { - private string? GetConnection = ""; - private MySqlConnection? msc = null; - - public DataHelper() - { - - } - - public bool Connect() - { - try - { - GetConnection = (string?)Config.DefaultAssemblyHelper.GetFunGameCoreValue((int)InterfaceType.ServerInterface, (int)InterfaceMethod.DBConnection); - if (GetConnection != null) - { - string[] DataSetting = GetConnection.Split(";"); - if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8) - { - ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]); - } - msc = new MySqlConnection(GetConnection); - msc.Open(); - if (msc.State == System.Data.ConnectionState.Open) - { - ServerHelper.WriteLine("Connected: MySQL服务器连接成功"); - return true; - } - } - else - { - throw new Exception("MySQL服务启动失败:无法找到MySQL配置文件。"); - } - } - catch (Exception e) - { - ServerHelper.Error(e); - } - return false; - } - - public void Close() - { - if (msc != null && msc.State == System.Data.ConnectionState.Open) - { - msc.Close(); - } - msc = null; - } - } -} diff --git a/FunGameServer/Utils/ServerHelper.cs b/FunGameServer/Utils/ServerHelper.cs deleted file mode 100644 index f5f6ce7..0000000 --- a/FunGameServer/Utils/ServerHelper.cs +++ /dev/null @@ -1,79 +0,0 @@ -using FunGame.Core.Api.Model.Enum; -using FunGameServer.Models.Config; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; - -namespace FunGameServer.Utils -{ - public class ServerHelper - { - public static string GetPrefix() - { - DateTime now = System.DateTime.Now; - return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.SERVER_NAME + ":"; - } - - public static void Error(Exception e) - { - Console.Write("\r" + GetPrefix() + e.Message + "\n" + e.StackTrace + "\n\r> "); - } - - public static void WriteLine(string? msg) - { - Console.Write("\r" + GetPrefix() + msg + "\n\r> "); - } - - public static void Type() - { - Console.Write("\r> "); - } - - public static void GetServerSettings() - { - try - { - Hashtable? settings = (Hashtable?)Config.DefaultAssemblyHelper.GetFunGameCoreValue((int)InterfaceType.ServerInterface, (int)InterfaceMethod.GetServerSettings); - if (settings != null) - { - string? Name = (string?)settings["Name"]; - string? Password = (string?)settings["Password"]; - string? Describe = (string?)settings["Describe"]; - string? Notice = (string?)settings["Notice"]; - string? Key = (string?)settings["Key"]; - if (Name != null) Config.SERVER_NAME = Name; - if (Password != null) Config.SERVER_PASSWORD = Password; - if (Describe != null) Config.SERVER_DESCRIBE = Describe; - if (Notice != null) Config.SERVER_NOTICE = Notice; - if (Key != null) Config.SERVER_KEY = Key; - int? Status = (int?)settings["Status"]; - int? Port = (int?)settings["Port"]; - int? MaxPlayer = (int?)settings["MaxPlayer"]; - int? MaxConnectFailed = (int?)settings["MaxConnectFailed"]; - if (Status != null) Config.SERVER_STATUS = (int)Status; - if (Port != null) Config.SERVER_PORT = (int)Port; - if (MaxPlayer != null) Config.MAX_PLAYERS = (int)MaxPlayer; - if (MaxConnectFailed != null) Config.MAX_CONNECTFAILED = (int)MaxConnectFailed; - } - } - catch (Exception e) - { - ServerHelper.WriteLine(e.StackTrace); - } - } - - public static void InitOrderList() - { - Config.OrderList.Clear(); - Config.OrderList.Add(OrderDictionary.Help, "Milimoe -> 帮助"); - Config.OrderList.Add(OrderDictionary.Quit, "关闭服务器"); - Config.OrderList.Add(OrderDictionary.Exit, "关闭服务器"); - Config.OrderList.Add(OrderDictionary.Close, "关闭服务器"); - Config.OrderList.Add(OrderDictionary.Restart, "重启服务器"); - } - } -} diff --git a/FunGameServer/Utils/SocketHelper.cs b/FunGameServer/Utils/SocketHelper.cs deleted file mode 100644 index 997cb19..0000000 --- a/FunGameServer/Utils/SocketHelper.cs +++ /dev/null @@ -1,34 +0,0 @@ -using FunGameServer.Models.Config; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.NetworkInformation; -using System.Text; -using System.Threading.Tasks; -using FunGame.Core.Api.Model.Enum; - -namespace FunGameServer.Utils -{ - public class SocketHelper - { - public static int GetType(string msg) - { - int index = msg.IndexOf(';') - 1; - if (index > 0) - return Convert.ToInt32(msg[..index]); - else - return Convert.ToInt32(msg[..1]); - } - - public static string GetMessage(string msg) - { - int index = msg.IndexOf(';') + 1; - return msg[index..]; - } - - public static string MakeMessage(int type, string msg) - { - return type + ";" + msg; - } - } -} diff --git a/FunGameServer/Utils/Utility.cs b/FunGameServer/Utils/Utility.cs new file mode 100644 index 0000000..d0d8059 --- /dev/null +++ b/FunGameServer/Utils/Utility.cs @@ -0,0 +1,187 @@ +using FunGame.Core.Api.Model.Enum; +using FunGame.Core.Api.Util; +using FunGameServer.Models.Config; +using MySql.Data.MySqlClient; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace FunGameServer.Utils +{ + public class Utility : FunGame.Core.Api.Util.Utility + { + + } + + public class DataHelper + { + private static MySqlConnection? msc = null; + + public static string Name { get; set; } = ""; + public static string DataSource { get; set; } = ""; + public static string Port { get; set; } = ""; + public static string DataBase { get; set; } = ""; + public static string User { get; set; } = ""; + public static string Password { get; set; } = ""; + public static string GetConnection { get; set; } = ""; + + private static string GetConnectProperties() + { + if (INIHelper.ExistINIFile()) + { + DataSource = INIHelper.ReadINI("MySQL", "DBServer"); + Port = INIHelper.ReadINI("MySQL", "DBPort"); + DataBase = INIHelper.ReadINI("MySQL", "DBName"); + User = INIHelper.ReadINI("MySQL", "DBUser"); + Password = INIHelper.ReadINI("MySQL", "DBPassword"); + return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;"; + } + else ServerHelper.Error(new Exception("找不到配置文件。")); + return ""; + } + + private static string GetConnectProperties(string datasource, string port, string database, string user, string password) + { + DataSource = datasource; + Port = port; + DataBase = database; + User = user; + Password = password; + return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;"; + } + + public static bool Connect(object[]? objs = null) + { + try + { + if (objs != null && objs.Length == 5) + { + GetConnection = GetConnectProperties((string)objs[0], (string)objs[1], (string)objs[2], (string)objs[3], (string)objs[4]); + } + else GetConnection = GetConnectProperties(); + if (GetConnection != null) + { + string[] DataSetting = GetConnection.Split(";"); + if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8) + { + ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]); + } + msc = new MySqlConnection(GetConnection); + msc.Open(); + if (msc.State == System.Data.ConnectionState.Open) + { + ServerHelper.WriteLine("Connected: MySQL服务器连接成功"); + return true; + } + } + else + { + throw new Exception("MySQL服务启动失败:无法找到MySQL配置文件。"); + } + } + catch (Exception e) + { + ServerHelper.Error(e); + } + return false; + } + + public static void Close() + { + if (msc != null && msc.State == System.Data.ConnectionState.Open) + { + msc.Close(); + } + msc = null; + } + } + + public class ServerHelper + { + public static string GetPrefix() + { + DateTime now = System.DateTime.Now; + return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.SERVER_NAME + ":"; + } + + public static void Error(Exception e) + { + Console.Write("\r" + GetPrefix() + e.Message + "\n" + e.StackTrace + "\n\r> "); + } + + public static void WriteLine(string? msg) + { + Console.Write("\r" + GetPrefix() + msg + "\n\r> "); + } + + public static void Type() + { + Console.Write("\r> "); + } + + private static Hashtable GetServerSettingHashtable() + { + Hashtable settings = new(); + if (INIHelper.ExistINIFile()) + { + settings.Add("Name", INIHelper.ReadINI("Server", "Name")); + settings.Add("Password", INIHelper.ReadINI("Server", "Password")); + settings.Add("Describe", INIHelper.ReadINI("Server", "Describe")); + settings.Add("Notice", INIHelper.ReadINI("Server", "Notice")); + settings.Add("Key", INIHelper.ReadINI("Server", "Key")); + settings.Add("Status", Convert.ToInt32(INIHelper.ReadINI("Server", "Status"))); + settings.Add("Port", Convert.ToInt32(INIHelper.ReadINI("Socket", "Port"))); + settings.Add("MaxPlayer", Convert.ToInt32(INIHelper.ReadINI("Socket", "MaxPlayer"))); + settings.Add("MaxConnectFailed", Convert.ToInt32(INIHelper.ReadINI("Socket", "MaxConnectFailed"))); + } + return settings; + } + + public static void GetServerSettings() + { + try + { + Hashtable settings = GetServerSettingHashtable(); + if (settings != null) + { + string? Name = (string?)settings["Name"]; + string? Password = (string?)settings["Password"]; + string? Describe = (string?)settings["Describe"]; + string? Notice = (string?)settings["Notice"]; + string? Key = (string?)settings["Key"]; + if (Name != null) Config.SERVER_NAME = Name; + if (Password != null) Config.SERVER_PASSWORD = Password; + if (Describe != null) Config.SERVER_DESCRIBE = Describe; + if (Notice != null) Config.SERVER_NOTICE = Notice; + if (Key != null) Config.SERVER_KEY = Key; + int? Status = (int?)settings["Status"]; + int? Port = (int?)settings["Port"]; + int? MaxPlayer = (int?)settings["MaxPlayer"]; + int? MaxConnectFailed = (int?)settings["MaxConnectFailed"]; + if (Status != null) Config.SERVER_STATUS = (int)Status; + if (Port != null) Config.SERVER_PORT = (int)Port; + if (MaxPlayer != null) Config.MAX_PLAYERS = (int)MaxPlayer; + if (MaxConnectFailed != null) Config.MAX_CONNECTFAILED = (int)MaxConnectFailed; + } + } + catch (Exception e) + { + ServerHelper.WriteLine(e.StackTrace); + } + } + + public static void InitOrderList() + { + Config.OrderList.Clear(); + Config.OrderList.Add(OrderDictionary.Help, "Milimoe -> 帮助"); + Config.OrderList.Add(OrderDictionary.Quit, "关闭服务器"); + Config.OrderList.Add(OrderDictionary.Exit, "关闭服务器"); + Config.OrderList.Add(OrderDictionary.Close, "关闭服务器"); + Config.OrderList.Add(OrderDictionary.Restart, "重启服务器"); + } + } +}