mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-21 19:49:36 +08:00
188 lines
7.7 KiB
C#
188 lines
7.7 KiB
C#
using System.Collections;
|
||
using Milimoe.FunGame.Core.Api.Utility;
|
||
using Milimoe.FunGame.Core.Entity;
|
||
using Milimoe.FunGame.Server.Others;
|
||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||
|
||
namespace Milimoe.FunGame.Server.Utility
|
||
{
|
||
public class ServerHelper
|
||
{
|
||
public static string GetPrefix()
|
||
{
|
||
DateTime now = DateTime.Now;
|
||
return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.ServerName + ":";
|
||
}
|
||
|
||
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("BannedList", INIHelper.ReadINI("Server", "BannedList"));
|
||
settings.Add("OfficialMail", INIHelper.ReadINI("ServerMail", "OfficialMail"));
|
||
settings.Add("SupportMail", INIHelper.ReadINI("ServerMail", "SupportMail"));
|
||
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"];
|
||
string? BannedList = (string?)settings["BannedList"];
|
||
|
||
if (Name != null) Config.ServerName = Name;
|
||
if (Password != null) Config.ServerPassword = Password;
|
||
if (Describe != null) Config.ServerDescription = Describe;
|
||
if (Notice != null) Config.ServerNotice = Notice;
|
||
if (Key != null) Config.ServerKey = Key;
|
||
if (BannedList != null) Config.ServerBannedList = BannedList;
|
||
|
||
string? OfficialMail = (string?)settings["OfficialMail"];
|
||
string? SupportMail = (string?)settings["SupportMail"];
|
||
|
||
if (OfficialMail != null) OfficialEmail.Email = OfficialMail;
|
||
if (SupportMail != null) OfficialEmail.SupportEmail = SupportMail;
|
||
|
||
int? Status = (int?)settings["Status"];
|
||
int? Port = (int?)settings["Port"];
|
||
int? MaxPlayer = (int?)settings["MaxPlayer"];
|
||
int? MaxConnectFailed = (int?)settings["MaxConnectFailed"];
|
||
|
||
if (Status != null) Config.ServerStatus = (int)Status;
|
||
if (Port != null) Config.ServerPort = (int)Port;
|
||
if (MaxPlayer != null) Config.MaxPlayers = (int)MaxPlayer;
|
||
if (MaxConnectFailed != null) Config.MaxConnectionFaileds = (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, "重启服务器");
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
public static string MakeClientName(string name, User? user = null)
|
||
{
|
||
if (user != null)
|
||
{
|
||
return "玩家 " + user.Username;
|
||
}
|
||
if (name != "") return "客户端(" + name + ")";
|
||
return "客户端";
|
||
}
|
||
}
|
||
|
||
public class SmtpHelper
|
||
{
|
||
public static string SenderMailAddress { get; set; } = "";
|
||
public static string SenderName { get; set; } = "";
|
||
public static string SenderPassword { get; set; } = "";
|
||
public static string SmtpHost { get; set; } = "";
|
||
public static int SmtpPort { get; set; } = 587;
|
||
public static bool OpenSSL { get; set; } = true;
|
||
|
||
public static MailSender? GetMailSender()
|
||
{
|
||
try
|
||
{
|
||
if (SenderMailAddress == "" && SenderName == "" && SenderPassword == "" && SmtpHost == "")
|
||
{
|
||
if (INIHelper.ExistINIFile())
|
||
{
|
||
if (bool.TryParse(INIHelper.ReadINI("Mailer", "UseMailSender").ToLower(), out bool use))
|
||
{
|
||
if (use)
|
||
{
|
||
SenderMailAddress = INIHelper.ReadINI("Mailer", "MailAddress");
|
||
SenderName = INIHelper.ReadINI("Mailer", "Name");
|
||
SenderPassword = INIHelper.ReadINI("Mailer", "Password");
|
||
SmtpHost = INIHelper.ReadINI("Mailer", "Host");
|
||
if (int.TryParse(INIHelper.ReadINI("Mailer", "Port"), out int Port))
|
||
SmtpPort = Port;
|
||
if (bool.TryParse(INIHelper.ReadINI("Mailer", "OpenSSL").ToLower(), out bool SSL))
|
||
OpenSSL = SSL;
|
||
if (SmtpPort > 0) return new MailSender(SenderMailAddress, SenderName, SenderPassword, SmtpHost, SmtpPort, OpenSSL);
|
||
}
|
||
}
|
||
ServerHelper.WriteLine("Smtp服务处于关闭状态");
|
||
return null;
|
||
}
|
||
throw new SmtpHelperException();
|
||
}
|
||
return new MailSender(SenderMailAddress, SenderName, SenderPassword, SmtpHost, SmtpPort, OpenSSL);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
ServerHelper.Error(e);
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
}
|