mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-22 03:59:36 +08:00
结构调整
This commit is contained in:
parent
35f7c18a17
commit
309c2ab4a2
@ -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();
|
||||
|
||||
/// <summary>
|
||||
/// string: 玩家标识ID
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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("服务器遇到问题需要关闭,请重新启动服务器!");
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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, "重启服务器");
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
187
FunGameServer/Utils/Utility.cs
Normal file
187
FunGameServer/Utils/Utility.cs
Normal file
@ -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, "重启服务器");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user