使用INIhelper读取配置文件

This commit is contained in:
Mili 2022-09-06 00:13:35 +08:00
parent e4fe75aa11
commit 0d66815b39
6 changed files with 160 additions and 111 deletions

View File

@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>C:\milimoe\FunGame\bin</BaseOutputPath>
<BaseOutputPath>C:\milimoe\FunGame\bin\Server</BaseOutputPath>
<Title>FunGameServer</Title>
<Authors>Milimoe</Authors>
<Product>$(AssemblyName)</Product>
@ -22,13 +22,9 @@
<DebugType>embedded</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.30" />
</ItemGroup>
<ItemGroup>
<Reference Include="FunGame.Core.Api">
<HintPath>..\..\FunGame\bin\Debug\net6.0\FunGame.Core.Api.dll</HintPath>
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.Api.dll</HintPath>
</Reference>
</ItemGroup>

View File

@ -7,7 +7,7 @@
<Nullable>enable</Nullable>
<ApplicationIcon>logo.ico</ApplicationIcon>
<PackageIcon>logo.ico</PackageIcon>
<BaseOutputPath>C:\milimoe\FunGame\bin</BaseOutputPath>
<BaseOutputPath>C:\milimoe\FunGame\bin\Server</BaseOutputPath>
<Title>FunGameServer</Title>
<Company>Milimoe</Company>
<Authors>Milimoe</Authors>
@ -28,14 +28,18 @@
<Content Include="logo.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.30" />
</ItemGroup>
<ItemGroup>
<Reference Include="FunGame.Core.Api">
<HintPath>..\..\FunGame\bin\Debug\net6.0\FunGame.Core.Api.dll</HintPath>
<Private>True</Private>
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\FunGame.Core.Api.dll</HintPath>
</Reference>
<Reference Include="MySql.Data">
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\MySql.Data.dll</HintPath>
</Reference>
<Reference Include="System.Configuration.ConfigurationManager">
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\System.Configuration.ConfigurationManager.dll</HintPath>
</Reference>
<Reference Include="System.Security.Permissions">
<HintPath>..\..\FunGame\bin\Server\Debug\net6.0\System.Security.Permissions.dll</HintPath>
</Reference>
</ItemGroup>

View File

@ -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;
}
}
});
}

View File

@ -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();
/// <summary>
/// string: 玩家标识ID

View File

@ -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();

View File

@ -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 "";
}
}
}