FunGame-Core/Controller/BaseAddonController.cs
milimoe c51b7c50fa
统一命名 并禁止GameModuleServer调用DataRequest (#81)
* 统一命名 并禁止GameModuleServer调用DataRequest

* 添加了GameModuleDepend (依赖集合) 用于整合Maps Characters Items Skills
2024-08-01 20:40:25 +08:00

71 lines
2.4 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 System.Collections;
using Milimoe.FunGame.Core.Interface.Addons;
using Milimoe.FunGame.Core.Library.Common.Addon;
namespace Milimoe.FunGame.Core.Controller
{
/// <summary>
/// 这是通用的控制器,仅提供基本功能
/// </summary>
/// <typeparam name="T">Addon的类型如<see cref="GameModule"/>或者<see cref="Plugin"/></typeparam>
public class BaseAddonController<T> where T : IAddon
{
/// <summary>
/// 控制器的本体
/// </summary>
public T Addon { get; }
/// <summary>
/// 输出系统消息
/// </summary>
protected Action<string> MaskMethod_WriteLine { get; set; }
/// <summary>
/// 输出错误消息
/// </summary>
protected Action<Exception> MaskMethod_Error { get; set; }
/// <summary>
/// 输出系统消息
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public void WriteLine(string msg) => MaskMethod_WriteLine(msg);
/// <summary>
/// 输出错误消息
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public void Error(Exception e) => MaskMethod_Error(e);
/// <summary>
/// 新建一个BaseAddonController
/// </summary>
/// <param name="addon"></param>
/// <param name="delegates"></param>
public BaseAddonController(IAddon addon, Hashtable delegates)
{
Addon = (T)addon;
if (delegates.ContainsKey("WriteLine")) MaskMethod_WriteLine = delegates["WriteLine"] != null ? (Action<string>)delegates["WriteLine"]! : new(DefaultPrint);
if (delegates.ContainsKey("Error")) MaskMethod_Error = delegates["Error"] != null ? (Action<Exception>)delegates["Error"]! : new(DefaultPrint);
MaskMethod_WriteLine ??= new(DefaultPrint);
MaskMethod_Error ??= new(DefaultPrint);
}
/// <summary>
/// 默认的输出错误消息方法
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private void DefaultPrint(string msg) => Console.Write("\r" + msg + "\n\r> ");
/// <summary>
/// 输出错误消息
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
private void DefaultPrint(Exception e) => DefaultPrint(e.ToString());
}
}