mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-20 19:19:39 +08:00
使用 WebAPIPlugin 接入模组控制器 (#38)
* WebAPI 现在可以添加插件了 * 添加 接收服务器控制台的输入 * 现在可以加载服务器插件 * 添加单例 SQLHelper 和 MailSender * SQL 服务守护 * 添加 wwwroot 支持静态访问 * 添加使用默认文档,以便可以访问 index.html
This commit is contained in:
parent
2f71d015b2
commit
c060c06d1c
1
.gitignore
vendored
1
.gitignore
vendored
@ -365,3 +365,4 @@ FodyWeavers.xsd
|
||||
#FunGame.Implement
|
||||
FunGame.Implement/*.cs
|
||||
FunGame.Implement/Implement/*.cs
|
||||
/FunGame.WebAPI/wwwroot/*
|
||||
|
@ -65,12 +65,21 @@ void StartServer()
|
||||
// 初始化命令菜单
|
||||
ServerHelper.InitOrderList();
|
||||
|
||||
// 创建全局SQLHelper
|
||||
Config.InitSQLHelper();
|
||||
|
||||
// 创建全局MailSender
|
||||
Config.InitMailSender();
|
||||
|
||||
// 读取游戏模组
|
||||
if (!Config.GetGameModuleList())
|
||||
{
|
||||
ServerHelper.WriteLine("服务器似乎未安装任何游戏模组,请检查是否正确安装它们。");
|
||||
}
|
||||
|
||||
// 读取Server插件
|
||||
Config.GetServerPlugins();
|
||||
|
||||
// 检查是否存在配置文件
|
||||
if (!INIHelper.ExistINIFile())
|
||||
{
|
||||
@ -86,9 +95,6 @@ void StartServer()
|
||||
}
|
||||
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
|
||||
|
||||
// 创建全局SQLHelper
|
||||
Config.InitSQLHelper();
|
||||
|
||||
// 使用Socket还是WebSocket
|
||||
bool useWebSocket = Config.UseWebSocket;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Milimoe.FunGame.Core.Interface.Base;
|
||||
using Milimoe.FunGame.Core.Library.Common.Addon;
|
||||
using Milimoe.FunGame.Server.Others;
|
||||
using Milimoe.FunGame.Server.Utility;
|
||||
|
||||
@ -47,6 +48,23 @@ namespace Milimoe.FunGame.Server.Model
|
||||
case OrderDictionary.Help:
|
||||
ShowHelp();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// 广播到插件
|
||||
if (Config.ServerPluginLoader != null)
|
||||
{
|
||||
foreach (ServerPlugin plugin in Config.ServerPluginLoader.Plugins.Values)
|
||||
{
|
||||
plugin.ProcessInput(order);
|
||||
}
|
||||
}
|
||||
if (Config.WebAPIPluginLoader != null)
|
||||
{
|
||||
foreach (WebAPIPlugin plugin in Config.WebAPIPluginLoader.Plugins.Values)
|
||||
{
|
||||
plugin.ProcessInput(order);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
|
@ -49,10 +49,11 @@ namespace Milimoe.FunGame.Server.Model
|
||||
Socket = socket;
|
||||
DataRequestController = new(this);
|
||||
IsDebugMode = isDebugMode;
|
||||
if (Config.SQLMode == SQLMode.MySQL) _sqlHelper = new MySQLHelper(this);
|
||||
else if (Config.SQLMode == SQLMode.SQLite) _sqlHelper = Config.SQLHelper;
|
||||
else ServerHelper.WriteLine("SQL 服务处于关闭状态", InvokeMessageType.Warning);
|
||||
_mailer = SmtpHelper.GetMailSender();
|
||||
if (Config.SQLMode != SQLMode.None)
|
||||
{
|
||||
_sqlHelper = Config.SQLHelper;
|
||||
}
|
||||
_mailer = Config.MailSender;
|
||||
}
|
||||
|
||||
public virtual async Task<bool> SocketMessageHandler(ISocketMessageProcessor socket, SocketObject obj)
|
||||
@ -282,7 +283,6 @@ namespace Milimoe.FunGame.Server.Model
|
||||
|
||||
public async Task Start()
|
||||
{
|
||||
TaskUtility.NewTask(CreatePeriodicalQuerier);
|
||||
await CreateStreamReader();
|
||||
}
|
||||
|
||||
@ -496,37 +496,10 @@ namespace Milimoe.FunGame.Server.Model
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task CreatePeriodicalQuerier()
|
||||
{
|
||||
await Task.Delay(20);
|
||||
ServerHelper.WriteLine("Creating: PeriodicalQuerier -> " + GetClientName() + " ...OK");
|
||||
while (Running)
|
||||
{
|
||||
// 每两小时触发一次SQL服务器的心跳查询,防止SQL服务器掉线
|
||||
try
|
||||
{
|
||||
await Task.Delay(2 * 1000 * 3600);
|
||||
SQLHelper?.ExecuteDataSet(UserQuery.Select_IsExistUsername(_username));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
RemoveUser();
|
||||
await Close();
|
||||
ServerHelper.WriteLine(GetClientName() + " Error -> Socket is Closed.");
|
||||
ServerHelper.WriteLine(GetClientName() + " Close -> StringStream is Closed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
SQLHelper?.Close();
|
||||
_sqlHelper = null;
|
||||
MailSender?.Dispose();
|
||||
_mailer = null;
|
||||
await Socket.CloseAsync();
|
||||
_running = false;
|
||||
Listener.ClientList.Remove(ClientName);
|
||||
|
@ -129,6 +129,16 @@ namespace Milimoe.FunGame.Server.Others
|
||||
/// </summary>
|
||||
public static GameModuleLoader? GameModuleLoader { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Server插件
|
||||
/// </summary>
|
||||
public static ServerPluginLoader? ServerPluginLoader { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Web API插件
|
||||
/// </summary>
|
||||
public static WebAPIPluginLoader? WebAPIPluginLoader { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 未Loadmodules时,此属性表示至少需要安装的模组
|
||||
/// </summary>
|
||||
@ -146,7 +156,13 @@ namespace Milimoe.FunGame.Server.Others
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 全局邮件发送器
|
||||
/// </summary>
|
||||
public static MailSender? MailSender => _MailSender;
|
||||
|
||||
private static SQLHelper? _SQLHelper;
|
||||
private static MailSender? _MailSender;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化数据库连接器
|
||||
@ -162,17 +178,13 @@ namespace Milimoe.FunGame.Server.Others
|
||||
_SQLHelper = new MySQLHelper("", false);
|
||||
if (((MySQLHelper)_SQLHelper).Connection != null)
|
||||
{
|
||||
SQLMode = _SQLHelper.Mode;
|
||||
ServerLogin();
|
||||
ClearRoomList();
|
||||
AfterCreateSQLService(_SQLHelper);
|
||||
}
|
||||
}
|
||||
else if (INIHelper.ReadINI("SQLite", "UseSQLite").Trim() == "true")
|
||||
{
|
||||
_SQLHelper = new SQLiteHelper();
|
||||
SQLMode = _SQLHelper.Mode;
|
||||
ServerLogin();
|
||||
ClearRoomList();
|
||||
AfterCreateSQLService(_SQLHelper);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -187,16 +199,39 @@ namespace Milimoe.FunGame.Server.Others
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化邮件发送器
|
||||
/// </summary>
|
||||
public static void InitMailSender()
|
||||
{
|
||||
try
|
||||
{
|
||||
_MailSender = SmtpHelper.GetMailSender();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
if (_MailSender != null)
|
||||
{
|
||||
Singleton.AddOrUpdate(_MailSender);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool GetGameModuleList()
|
||||
{
|
||||
List<string> supported = [];
|
||||
// 构建AddonController
|
||||
Hashtable delegates = [];
|
||||
Dictionary<string, object> delegates = [];
|
||||
delegates.Add("WriteLine", new Action<string>(msg => ServerHelper.WriteLine(msg, InvokeMessageType.GameModule)));
|
||||
delegates.Add("Error", new Action<Exception>(ServerHelper.Error));
|
||||
// 读取modules目录下的模组
|
||||
try
|
||||
{
|
||||
GameModuleLoader = GameModuleLoader.LoadGameModules(FunGameType, delegates);
|
||||
foreach (GameModuleServer module in GameModuleLoader.ModuleServers.Values)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool check = true;
|
||||
// 检查模组是否有相对应的地图
|
||||
@ -210,6 +245,16 @@ namespace Milimoe.FunGame.Server.Others
|
||||
supported.Add(module.Name);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e2)
|
||||
{
|
||||
ServerHelper.Error(e2);
|
||||
}
|
||||
// 设置全局
|
||||
GameModuleSupported = supported.Distinct().ToArray();
|
||||
foreach (string modename in GameModuleSupported)
|
||||
@ -220,6 +265,46 @@ namespace Milimoe.FunGame.Server.Others
|
||||
return GameModuleSupported.Length > 0;
|
||||
}
|
||||
|
||||
public static void GetServerPlugins()
|
||||
{
|
||||
Dictionary<string, object> delegates = [];
|
||||
delegates.Add("WriteLine", new Action<string>(msg => ServerHelper.WriteLine(msg, InvokeMessageType.Plugin)));
|
||||
delegates.Add("Error", new Action<Exception>(ServerHelper.Error));
|
||||
try
|
||||
{
|
||||
// 读取plugins目录下的插件
|
||||
ServerPluginLoader = ServerPluginLoader.LoadPlugins(delegates);
|
||||
foreach (ServerPlugin plugin in ServerPluginLoader.Plugins.Values)
|
||||
{
|
||||
ServerHelper.WriteLine("Loaded: " + plugin.Name, InvokeMessageType.Plugin);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void GetWebAPIPlugins()
|
||||
{
|
||||
Dictionary<string, object> delegates = [];
|
||||
delegates.Add("WriteLine", new Action<string>(msg => ServerHelper.WriteLine(msg, InvokeMessageType.Plugin)));
|
||||
delegates.Add("Error", new Action<Exception>(ServerHelper.Error));
|
||||
try
|
||||
{
|
||||
// 读取plugins目录下的插件
|
||||
WebAPIPluginLoader = WebAPIPluginLoader.LoadPlugins(delegates);
|
||||
foreach (WebAPIPlugin plugin in WebAPIPluginLoader.Plugins.Values)
|
||||
{
|
||||
ServerHelper.WriteLine("Loaded: " + plugin.Name, InvokeMessageType.Plugin);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 服务器启动登记
|
||||
/// </summary>
|
||||
@ -241,6 +326,30 @@ namespace Milimoe.FunGame.Server.Others
|
||||
SQLHelper.Execute(RoomQuery.Delete_Rooms());
|
||||
}
|
||||
}
|
||||
|
||||
public static void AfterCreateSQLService(SQLHelper sqlHelper)
|
||||
{
|
||||
SQLMode = sqlHelper.Mode;
|
||||
Singleton.AddOrUpdate(sqlHelper, true);
|
||||
ServerLogin();
|
||||
ClearRoomList();
|
||||
Task t = Task.Run(async () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// 每两小时触发一次SQL服务器的心跳查询,防止SQL服务器掉线
|
||||
try
|
||||
{
|
||||
await Task.Delay(2 * 1000 * 3600);
|
||||
SQLHelper?.ExecuteDataSet(ServerLoginLogs.Select_GetLastLoginTime());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ServerHelper.Error(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -59,8 +59,9 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
|
||||
public static void Error(Exception e)
|
||||
{
|
||||
Console.Write("\r" + GetPrefix(InvokeMessageType.Error) + e.Message + "\n" + e.StackTrace + "\n\r> ");
|
||||
Console.WriteLine("\r" + GetPrefix(InvokeMessageType.Error) + e.Message + "\n" + e.StackTrace);
|
||||
Console.ResetColor();
|
||||
Console.Write("\r> ");
|
||||
}
|
||||
|
||||
public static void Write(string msg, InvokeMessageType type = InvokeMessageType.System)
|
||||
@ -71,8 +72,9 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
|
||||
public static void WriteLine(string msg, InvokeMessageType type = InvokeMessageType.System)
|
||||
{
|
||||
if (msg.Trim() != "") Console.Write("\r" + GetPrefix(type) + msg + "\n\r> ");
|
||||
if (msg.Trim() != "") Console.WriteLine("\r" + GetPrefix(type) + msg);
|
||||
Console.ResetColor();
|
||||
Console.Write("\r> ");
|
||||
}
|
||||
|
||||
public static void Type()
|
||||
@ -211,7 +213,11 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
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);
|
||||
if (SmtpPort > 0)
|
||||
{
|
||||
ServerHelper.WriteLine("SMTP 服务已启动!");
|
||||
return new MailSender(SenderMailAddress, SenderName, SenderPassword, SmtpHost, SmtpPort, OpenSSL);
|
||||
}
|
||||
}
|
||||
}
|
||||
ServerHelper.WriteLine("SMTP 服务处于关闭状态", InvokeMessageType.Warning);
|
||||
|
@ -5,6 +5,8 @@ namespace Milimoe.FunGame.WebAPI.Architecture
|
||||
{
|
||||
public class RESTfulAPIListener : ISocketListener<RESTfulAPI>
|
||||
{
|
||||
public static RESTfulAPIListener? Instance { get; set; } = null;
|
||||
|
||||
public string Name => "RESTfulAPIListener";
|
||||
|
||||
public ConcurrentModelList<IServerModel> ClientList { get; } = [];
|
||||
|
@ -1,6 +1,5 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.WebAPI.Architecture;
|
||||
@ -22,7 +21,7 @@ namespace Milimoe.FunGame.WebAPI.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
RESTfulAPIListener? apiListener = Singleton.Get<RESTfulAPIListener>();
|
||||
RESTfulAPIListener? apiListener = RESTfulAPIListener.Instance;
|
||||
if (apiListener != null && apiListener.UserList.ContainsKey(username))
|
||||
{
|
||||
RESTfulAPIModel model = (RESTfulAPIModel)apiListener.UserList[username];
|
||||
|
@ -22,7 +22,7 @@ namespace Milimoe.FunGame.WebAPI.Controllers
|
||||
ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 通过 RESTful API 连接至服务器,正在登录 . . .", Core.Library.Constant.InvokeMessageType.Core);
|
||||
string username = loginModel.Username;
|
||||
string password = loginModel.Password;
|
||||
RESTfulAPIListener? apiListener = Singleton.Get<RESTfulAPIListener>();
|
||||
RESTfulAPIListener? apiListener = RESTfulAPIListener.Instance;
|
||||
if (apiListener != null)
|
||||
{
|
||||
// 移除旧模型
|
||||
|
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@ -45,4 +45,8 @@
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="wwwroot\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,13 +1,16 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Unicode;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Library.Common.Addon;
|
||||
using Milimoe.FunGame.Core.Library.Common.JsonConverter;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
@ -29,12 +32,24 @@ try
|
||||
// 初始化命令菜单
|
||||
ServerHelper.InitOrderList();
|
||||
|
||||
// 创建全局SQLHelper
|
||||
Config.InitSQLHelper();
|
||||
|
||||
// 创建全局MailSender
|
||||
Config.InitMailSender();
|
||||
|
||||
// 读取游戏模组
|
||||
if (!Config.GetGameModuleList())
|
||||
{
|
||||
ServerHelper.WriteLine("服务器似乎未安装任何游戏模组,请检查是否正确安装它们。");
|
||||
}
|
||||
|
||||
// 读取Server插件
|
||||
Config.GetServerPlugins();
|
||||
|
||||
// 读取Web API插件
|
||||
Config.GetWebAPIPlugins();
|
||||
|
||||
// 检查是否存在配置文件
|
||||
if (!INIHelper.ExistINIFile())
|
||||
{
|
||||
@ -51,18 +66,36 @@ try
|
||||
|
||||
// 创建单例
|
||||
RESTfulAPIListener apiListener = new();
|
||||
Singleton.Add(apiListener);
|
||||
RESTfulAPIListener.Instance = apiListener;
|
||||
|
||||
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。");
|
||||
|
||||
// 创建全局SQLHelper
|
||||
Config.InitSQLHelper();
|
||||
if (Config.ServerNotice != "")
|
||||
Console.WriteLine("\r \n********** 服务器公告 **********\n\n" + Config.ServerNotice + "\n");
|
||||
else
|
||||
Console.WriteLine("无法读取服务器公告");
|
||||
|
||||
ServerHelper.WriteLine("正在启动 Web API 监听 . . .");
|
||||
Console.WriteLine("\r ");
|
||||
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// 读取扩展控制器
|
||||
if (Config.WebAPIPluginLoader != null)
|
||||
{
|
||||
foreach (WebAPIPlugin plugin in Config.WebAPIPluginLoader.Plugins.Values)
|
||||
{
|
||||
Assembly? pluginAssembly = Assembly.GetAssembly(plugin.GetType());
|
||||
|
||||
if (pluginAssembly != null)
|
||||
{
|
||||
// 注册所有控制器
|
||||
builder.Services.AddControllers().PartManager.ApplicationParts.Add(new AssemblyPart(pluginAssembly));
|
||||
}
|
||||
}
|
||||
}
|
||||
// 添加 JSON 转换器
|
||||
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.WriteIndented = true;
|
||||
@ -146,6 +179,10 @@ try
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseDefaultFiles();
|
||||
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
@ -184,11 +221,6 @@ try
|
||||
// 开始监听连接
|
||||
listener.BannedList.AddRange(Config.ServerBannedList);
|
||||
|
||||
if (Config.ServerNotice != "")
|
||||
Console.WriteLine("\n\n********** 服务器公告 **********\n\n" + Config.ServerNotice + "\n");
|
||||
else
|
||||
Console.WriteLine("无法读取服务器公告");
|
||||
|
||||
Task order = Task.Factory.StartNew(GetConsoleOrder);
|
||||
|
||||
app.Run();
|
||||
|
Loading…
x
Reference in New Issue
Block a user