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

View File

@ -7,7 +7,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ApplicationIcon>logo.ico</ApplicationIcon> <ApplicationIcon>logo.ico</ApplicationIcon>
<PackageIcon>logo.ico</PackageIcon> <PackageIcon>logo.ico</PackageIcon>
<BaseOutputPath>C:\milimoe\FunGame\bin</BaseOutputPath> <BaseOutputPath>C:\milimoe\FunGame\bin\Server</BaseOutputPath>
<Title>FunGameServer</Title> <Title>FunGameServer</Title>
<Company>Milimoe</Company> <Company>Milimoe</Company>
<Authors>Milimoe</Authors> <Authors>Milimoe</Authors>
@ -28,14 +28,18 @@
<Content Include="logo.ico" /> <Content Include="logo.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.30" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="FunGame.Core.Api"> <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>
<Private>True</Private> </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> </Reference>
</ItemGroup> </ItemGroup>

View File

@ -9,18 +9,115 @@ using FunGameServer.Models.Config;
using FunGameServer.Utils; using FunGameServer.Utils;
using static FunGame.Core.Api.Model.Enum.CommonEnums; using static FunGame.Core.Api.Model.Enum.CommonEnums;
Console.Title = Config.CONSOLE_TITLE;
bool Running = true; bool Running = true;
Socket? ServerSocket = null; Socket? ServerSocket = null;
string hostname = Config.SERVER_NAME; StartServer();
int port = Config.SERVER_PORT;
Console.Title = Config.CONSOLE_TITLE; while (Running)
Task t = Task.Factory.StartNew(() =>
{ {
string? order = "";
order = Console.ReadLine();
ServerHelper.Type();
if (order != null && !order.Equals("") && Running)
{
switch (order)
{
case "quit":
Running = false;
break;
case "help":
ServerHelper.WriteLine("Milimoe -> 帮助");
break;
case "restart":
if (ServerSocket == null)
{
ServerHelper.WriteLine("重启服务器");
StartServer();
}
else
ServerHelper.WriteLine("服务器正在运行,拒绝重启!");
break;
}
}
}
ServerHelper.WriteLine("服务器已关闭,按任意键退出程序。");
Console.ReadKey();
bool Read(Socket socket)
{
// 接收客户端消息
byte[] buffer = new byte[2048];
int length = socket.Receive(buffer);
if (length > 0)
{
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
string typestring = SocketHelper.GetTypeString(SocketHelper.GetType(msg));
msg = SocketHelper.GetMessage(msg);
if (typestring != SocketEnums.TYPE_UNKNOWN)
{
ServerHelper.WriteLine("[ 客户端(" + typestring + "] -> " + msg);
return true;
}
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符拒绝连接。");
return false;
}
else
ServerHelper.WriteLine("客户端没有回应。");
return false;
}
bool Send(Socket socket)
{
// 发送消息给客户端
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)
{
ServerHelper.WriteLine("[ 客户端 ] <- " + msg);
return true;
}
else
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
return false;
}
bool IsIP(string ip)
{
//判断是否为IP
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
bool IsEmail(string ip)
{
//判断是否为Email
return Regex.IsMatch(ip, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
}
void StartServer()
{
Task t = Task.Factory.StartNew(() =>
{
try 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服务器 // 连接MySQL服务器
if (!Config.DefaultDataHelper.Connect()) if (!Config.DefaultDataHelper.Connect())
{ {
@ -29,7 +126,7 @@ Task t = Task.Factory.StartNew(() =>
} }
// 创建IP地址终结点对象 // 创建IP地址终结点对象
IPEndPoint ip = new(IPAddress.Any, port); IPEndPoint ip = new(IPAddress.Any, Config.SERVER_PORT);
// 创建TCP Socket对象并绑定终结点 // 创建TCP Socket对象并绑定终结点
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
@ -37,16 +134,12 @@ Task t = Task.Factory.StartNew(() =>
// 开始监听连接 // 开始监听连接
ServerSocket.Listen(Config.MAX_PLAYERS); ServerSocket.Listen(Config.MAX_PLAYERS);
ServerHelper.WriteLine("服务器启动成功,端口号 " + port + " ,开始监听 . . ."); ServerHelper.WriteLine("服务器启动成功,端口号 " + Config.SERVER_PORT + " ,开始监听 . . .");
Task.Run(() => if (Config.SERVER_NOTICE != "")
{ ServerHelper.WriteLine("\n\n**********服务器公告**********\n\n" + Config.SERVER_NOTICE + "\n");
Config.ServerNotice = ServerHelper.GetServerNotice();
if (Config.ServerNotice != "")
ServerHelper.WriteLine("\n**********服务器公告**********\n" + Config.ServerNotice + "\n\n");
else else
ServerHelper.WriteLine("无法读取服务器公告"); ServerHelper.WriteLine("无法读取服务器公告");
});
while (Running) while (Running)
{ {
@ -97,75 +190,5 @@ Task t = Task.Factory.StartNew(() =>
} }
} }
}); });
while (Running)
{
string? order = "";
order = Console.ReadLine();
ServerHelper.Type();
if (order != null && !order.Equals("") && Running)
{
switch (order)
{
case "quit":
Running = false;
break;
}
}
}
ServerHelper.WriteLine("服务器已关闭,按任意键退出程序。");
Console.ReadKey();
bool Read(Socket socket)
{
// 接收客户端消息
byte[] buffer = new byte[2048];
int length = socket.Receive(buffer);
if (length > 0)
{
string msg = Config.DEFAULT_ENCODING.GetString(buffer, 0, length);
string typestring = SocketHelper.GetTypeString(SocketHelper.GetType(msg));
msg = SocketHelper.GetMessage(msg);
if (typestring != SocketEnums.TYPE_UNKNOWN)
{
ServerHelper.WriteLine("[ 客户端(" + typestring + "] -> " + msg);
return true;
}
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符拒绝连接。");
return false;
}
else
ServerHelper.WriteLine("客户端没有回应。");
return false;
}
bool Send(Socket socket)
{
// 发送消息给客户端
string msg = ">> 已连接至服务器 -> [ " + hostname + " ] 连接成功";
byte[] buffer = new byte[2048];
buffer = Config.DEFAULT_ENCODING.GetBytes(SocketHelper.MakeMessage((int)SocketEnums.Type.CheckLogin, msg));
if (socket.Send(buffer) > 0)
{
ServerHelper.WriteLine("[ 客户端 ] <- " + msg);
return true;
}
else
ServerHelper.WriteLine("无法传输数据,与客户端的连接可能丢失。");
return false;
}
bool IsIP(string ip)
{
//判断是否为IP
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
bool IsEmail(string ip)
{
//判断是否为Email
return Regex.IsMatch(ip, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
} }

View File

@ -13,18 +13,23 @@ namespace FunGameServer.Models.Config
{ {
public static class 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_PLAYERS = 20; // 最多接受连接的玩家数量
public static int MAX_CONNECTFAILED = 5; // 最大连接失败次数
public static int ONLINE_PLAYERS = 0; // 已连接的玩家数量 public static int ONLINE_PLAYERS = 0; // 已连接的玩家数量
public static int CONNECTING_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 Encoding DEFAULT_ENCODING = Encoding.UTF8; // 默认传输字符集
public static int MAX_CONNECTFAILED = 5; // 最大连接失败次数
public const string CONSOLE_TITLE = "FunGame Server"; // 控制台的标题 public const string CONSOLE_TITLE = "FunGame Server"; // 控制台的标题
public static string ServerNotice = ""; // 服务器的公告
public static AssemblyHelper DefaultAssemblyHelper = new AssemblyHelper(); public static AssemblyHelper DefaultAssemblyHelper = new();
public static DataHelper DefaultDataHelper = new DataHelper(); public static DataHelper DefaultDataHelper = new();
public static INIHelper DefaultINIHelper = new();
/// <summary> /// <summary>
/// string: 玩家标识ID /// string: 玩家标识ID

View File

@ -30,7 +30,7 @@ namespace FunGameServer.Utils
string[] DataSetting = GetConnection.Split(";"); string[] DataSetting = GetConnection.Split(";");
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8) 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 = new MySqlConnection(GetConnection);
msc.Open(); msc.Open();

View File

@ -1,5 +1,6 @@
using FunGameServer.Models.Config; using FunGameServer.Models.Config;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -31,17 +32,37 @@ namespace FunGameServer.Utils
Console.Write("\r> "); Console.Write("\r> ");
} }
public static string GetServerNotice() public static void GetServerSettings()
{ {
try 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) catch (Exception e)
{ {
ServerHelper.WriteLine(e.StackTrace); ServerHelper.WriteLine(e.StackTrace);
} }
return "";
} }
} }
} }