diff --git a/FunGame.Core/FunGame.Core.csproj b/FunGame.Core/FunGame.Core.csproj
index 64b1b7d..32a2dab 100644
--- a/FunGame.Core/FunGame.Core.csproj
+++ b/FunGame.Core/FunGame.Core.csproj
@@ -4,7 +4,7 @@
net6.0
enable
enable
- C:\milimoe\FunGame\bin
+ C:\milimoe\FunGame\bin\Server
FunGameServer
Milimoe
$(AssemblyName)
@@ -22,13 +22,9 @@
embedded
-
-
-
-
- ..\..\FunGame\bin\Debug\net6.0\FunGame.Core.Api.dll
+ ..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.Api.dll
diff --git a/FunGameServer/FunGameServer.csproj b/FunGameServer/FunGameServer.csproj
index 8799b1e..2365b79 100644
--- a/FunGameServer/FunGameServer.csproj
+++ b/FunGameServer/FunGameServer.csproj
@@ -7,7 +7,7 @@
enable
logo.ico
logo.ico
- C:\milimoe\FunGame\bin
+ C:\milimoe\FunGame\bin\Server
FunGameServer
Milimoe
Milimoe
@@ -28,14 +28,18 @@
-
-
-
-
- ..\..\FunGame\bin\Debug\net6.0\FunGame.Core.Api.dll
- True
+ ..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.Api.dll
+
+
+ ..\..\FunGame\bin\Server\Debug\net6.0\MySql.Data.dll
+
+
+ ..\..\FunGame\bin\Server\Debug\net6.0\System.Configuration.ConfigurationManager.dll
+
+
+ ..\..\FunGame\bin\Server\Debug\net6.0\System.Security.Permissions.dll
diff --git a/FunGameServer/Main.cs b/FunGameServer/Main.cs
index 8723c6d..236e4f9 100644
--- a/FunGameServer/Main.cs
+++ b/FunGameServer/Main.cs
@@ -9,95 +9,12 @@ using FunGameServer.Models.Config;
using FunGameServer.Utils;
using static FunGame.Core.Api.Model.Enum.CommonEnums;
+Console.Title = Config.CONSOLE_TITLE;
+
bool Running = true;
Socket? ServerSocket = null;
-string hostname = Config.SERVER_NAME;
-int port = Config.SERVER_PORT;
-
-Console.Title = Config.CONSOLE_TITLE;
-
-Task t = Task.Factory.StartNew(() =>
-{
- try
- {
- // 连接MySQL服务器
- if (!Config.DefaultDataHelper.Connect())
- {
- Running = false;
- throw new Exception("服务器遇到问题需要关闭,请重新启动服务器!");
- }
-
- // 创建IP地址终结点对象
- IPEndPoint ip = new(IPAddress.Any, port);
-
- // 创建TCP Socket对象并绑定终结点
- ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- ServerSocket.Bind(ip);
-
- // 开始监听连接
- ServerSocket.Listen(Config.MAX_PLAYERS);
- ServerHelper.WriteLine("服务器启动成功,端口号 " + port + " ,开始监听 . . .");
-
- Task.Run(() =>
- {
- Config.ServerNotice = ServerHelper.GetServerNotice();
- if (Config.ServerNotice != "")
- ServerHelper.WriteLine("\n**********服务器公告**********\n" + Config.ServerNotice + "\n\n");
- else
- ServerHelper.WriteLine("无法读取服务器公告");
- });
-
- while (Running)
- {
- Socket socket;
- try
- {
- socket = ServerSocket.Accept();
- IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint;
- if (clientIP != null)
- ServerHelper.WriteLine("客户端" + clientIP.ToString() + "连接 . . .");
- else
- ServerHelper.WriteLine("未知地点客户端连接 . . .");
- if (Read(socket) && Send(socket))
- Task.Factory.StartNew(() =>
- {
- new ClientSocket(socket, Running).Start();
- });
- else
- if (clientIP != null)
- ServerHelper.WriteLine("客户端" + clientIP.ToString() + "连接失败。");
- else
- ServerHelper.WriteLine("客户端连接失败。");
- }
- catch (Exception e)
- {
- ServerHelper.WriteLine("客户端断开连接!\n" + e.StackTrace);
- }
- }
- }
- catch (Exception e)
- {
- if (e.Message.Equals("服务器遇到问题需要关闭,请重新启动服务器!"))
- {
- if (ServerSocket != null)
- {
- ServerSocket.Close();
- ServerSocket = null;
- }
- }
- ServerHelper.Error(e);
- }
- finally
- {
- if (ServerSocket != null)
- {
- ServerSocket.Close();
- ServerSocket = null;
- }
- }
-
-});
+StartServer();
while (Running)
{
@@ -111,6 +28,18 @@ while (Running)
case "quit":
Running = false;
break;
+ case "help":
+ ServerHelper.WriteLine("Milimoe -> 帮助");
+ break;
+ case "restart":
+ if (ServerSocket == null)
+ {
+ ServerHelper.WriteLine("重启服务器");
+ StartServer();
+ }
+ else
+ ServerHelper.WriteLine("服务器正在运行,拒绝重启!");
+ break;
}
}
}
@@ -145,7 +74,7 @@ bool Read(Socket socket)
bool Send(Socket socket)
{
// 发送消息给客户端
- string msg = ">> 已连接至服务器 -> [ " + hostname + " ] 连接成功";
+ string msg = ">> 已连接至服务器 -> [ " + Config.SERVER_NAME + " ] 连接成功";
byte[] buffer = new byte[2048];
buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketEnums.Type.CheckLogin, msg));
if (socket.Send(buffer) > 0)
@@ -168,4 +97,98 @@ bool IsEmail(string ip)
{
//判断是否为Email
return Regex.IsMatch(ip, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
+}
+
+void StartServer()
+{
+ Task t = Task.Factory.StartNew(() =>
+ {
+ try
+ {
+ ServerHelper.WriteLine("正在读取配置文件并初始化服务 . . .");
+ // 检查是否存在配置文件
+ if (!Config.DefaultINIHelper.ExistINIFile())
+ {
+ ServerHelper.WriteLine("未检测到配置文件,将自动创建配置文件 . . .");
+ Config.DefaultINIHelper.Init();
+ ServerHelper.WriteLine("配置文件FunGame.ini创建成功,请修改该配置文件,然后重启服务器。");
+ ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
+ return;
+ }
+ else
+ ServerHelper.GetServerSettings();
+
+ // 连接MySQL服务器
+ if (!Config.DefaultDataHelper.Connect())
+ {
+ Running = false;
+ throw new Exception("服务器遇到问题需要关闭,请重新启动服务器!");
+ }
+
+ // 创建IP地址终结点对象
+ IPEndPoint ip = new(IPAddress.Any, Config.SERVER_PORT);
+
+ // 创建TCP Socket对象并绑定终结点
+ ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ ServerSocket.Bind(ip);
+
+ // 开始监听连接
+ ServerSocket.Listen(Config.MAX_PLAYERS);
+ ServerHelper.WriteLine("服务器启动成功,端口号 " + Config.SERVER_PORT + " ,开始监听 . . .");
+
+ if (Config.SERVER_NOTICE != "")
+ ServerHelper.WriteLine("\n\n**********服务器公告**********\n\n" + Config.SERVER_NOTICE + "\n");
+ else
+ ServerHelper.WriteLine("无法读取服务器公告");
+
+ while (Running)
+ {
+ Socket socket;
+ try
+ {
+ socket = ServerSocket.Accept();
+ IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint;
+ if (clientIP != null)
+ ServerHelper.WriteLine("客户端" + clientIP.ToString() + "连接 . . .");
+ else
+ ServerHelper.WriteLine("未知地点客户端连接 . . .");
+ if (Read(socket) && Send(socket))
+ Task.Factory.StartNew(() =>
+ {
+ new ClientSocket(socket, Running).Start();
+ });
+ else
+ if (clientIP != null)
+ ServerHelper.WriteLine("客户端" + clientIP.ToString() + "连接失败。");
+ else
+ ServerHelper.WriteLine("客户端连接失败。");
+ }
+ catch (Exception e)
+ {
+ ServerHelper.WriteLine("客户端断开连接!\n" + e.StackTrace);
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ if (e.Message.Equals("服务器遇到问题需要关闭,请重新启动服务器!"))
+ {
+ if (ServerSocket != null)
+ {
+ ServerSocket.Close();
+ ServerSocket = null;
+ }
+ }
+ ServerHelper.Error(e);
+ }
+ finally
+ {
+ if (ServerSocket != null)
+ {
+ ServerSocket.Close();
+ ServerSocket = null;
+ }
+ }
+
+ });
}
\ No newline at end of file
diff --git a/FunGameServer/Models/Config/Config.cs b/FunGameServer/Models/Config/Config.cs
index 210bc15..6a8e334 100644
--- a/FunGameServer/Models/Config/Config.cs
+++ b/FunGameServer/Models/Config/Config.cs
@@ -13,18 +13,23 @@ namespace FunGameServer.Models.Config
{
public static class Config
{
+ public static string SERVER_NAME = "FunGame Server"; // 服务器名称
+ public static int SERVER_PORT = 22222; // 默认端口
+ public static int SERVER_STATUS = 1; // 默认状态:1可连接 0不可连接 -1不可用
+ public static string SERVER_NOTICE = ""; // 服务器的公告
+ public static string SERVER_PASSWORD = ""; // 服务器的密码
+ public static string SERVER_DESCRIBE = ""; // 服务器的描述
+ public static string SERVER_KEY = ""; // 注册社区服务器的Key
public static int MAX_PLAYERS = 20; // 最多接受连接的玩家数量
+ public static int MAX_CONNECTFAILED = 5; // 最大连接失败次数
public static int ONLINE_PLAYERS = 0; // 已连接的玩家数量
public static int CONNECTING_PLAYERS = 0; // 正在连接的玩家数量
- public static string SERVER_NAME = "米粒的糖果屋"; // 服务器名称
- public static int SERVER_PORT = 22222; // 默认端口
public static Encoding DEFAULT_ENCODING = Encoding.UTF8; // 默认传输字符集
- public static int MAX_CONNECTFAILED = 5; // 最大连接失败次数
public const string CONSOLE_TITLE = "FunGame Server"; // 控制台的标题
- public static string ServerNotice = ""; // 服务器的公告
- public static AssemblyHelper DefaultAssemblyHelper = new AssemblyHelper();
- public static DataHelper DefaultDataHelper = new DataHelper();
+ public static AssemblyHelper DefaultAssemblyHelper = new();
+ public static DataHelper DefaultDataHelper = new();
+ public static INIHelper DefaultINIHelper = new();
///
/// string: 玩家标识ID
diff --git a/FunGameServer/Utils/DataHelper.cs b/FunGameServer/Utils/DataHelper.cs
index 57d57f2..8984cb1 100644
--- a/FunGameServer/Utils/DataHelper.cs
+++ b/FunGameServer/Utils/DataHelper.cs
@@ -17,7 +17,7 @@ namespace FunGameServer.Utils
public DataHelper()
{
-
+
}
public bool Connect()
@@ -30,7 +30,7 @@ namespace FunGameServer.Utils
string[] DataSetting = GetConnection.Split(";");
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
{
- ServerHelper.WriteLine("Connecting: " + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
+ ServerHelper.WriteLine("Connect -> MySQL:\\\\" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
}
msc = new MySqlConnection(GetConnection);
msc.Open();
diff --git a/FunGameServer/Utils/ServerHelper.cs b/FunGameServer/Utils/ServerHelper.cs
index 9421f95..74719e1 100644
--- a/FunGameServer/Utils/ServerHelper.cs
+++ b/FunGameServer/Utils/ServerHelper.cs
@@ -1,5 +1,6 @@
using FunGameServer.Models.Config;
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -31,17 +32,37 @@ namespace FunGameServer.Utils
Console.Write("\r> ");
}
- public static string GetServerNotice()
+ 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);
}
- return "";
}
}
}