FunGame-Core/Api/Utility/ServerPluginLoader.cs
milimoe 59253948cb
为服务器添加插件 (#94)
* 添加 WebAPIPlugin 和 Loader

* 添加 接收服务器控制台的输入

* 添加 ServerPlugin,添加控制器

* 更新 SocketObejct
2024-10-15 20:13:48 +08:00

55 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Milimoe.FunGame.Core.Library.Common.Addon;
using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Api.Utility
{
public class ServerPluginLoader
{
/// <summary>
/// 已读取的插件列表
/// <para>key 是 <see cref="ServerPlugin.Name"/></para>
/// </summary>
public Dictionary<string, ServerPlugin> Plugins { get; } = [];
/// <summary>
/// 已加载的插件DLL名称对应的路径
/// </summary>
public static Dictionary<string, string> PluginFilePaths => new(AddonManager.PluginFilePaths);
private ServerPluginLoader()
{
}
/// <summary>
/// 构建一个插件读取器并读取插件
/// </summary>
/// <param name="delegates">用于构建 <see cref="Controller.BaseAddonController{T}"/></param>
/// <param name="otherobjs">其他需要传入给插件初始化的对象</param>
/// <returns></returns>
public static ServerPluginLoader LoadPlugins(Dictionary<string, object> delegates, params object[] otherobjs)
{
ServerPluginLoader loader = new();
AddonManager.LoadServerPlugins(loader.Plugins, delegates, otherobjs);
foreach (ServerPlugin plugin in loader.Plugins.Values.ToList())
{
// 如果插件加载后需要执行代码请重写AfterLoad方法
plugin.AfterLoad(loader);
}
return loader;
}
public ServerPlugin this[string name]
{
get
{
return Plugins[name];
}
set
{
Plugins.TryAdd(name, value);
}
}
}
}