using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Interface.Addons;
using Milimoe.FunGame.Core.Library.Common.Addon;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Controller
{
///
/// 这是通用的控制器,仅提供基本功能
///
/// Addon的类型,如 或者 / /
public class BaseAddonController where T : IAddon
{
///
/// 控制器的本体
///
public T Addon { get; }
///
/// 输出系统消息
///
protected Action MaskMethod_WriteLine { get; set; }
///
/// 输出错误消息
///
protected Action MaskMethod_Error { get; set; }
///
/// 输出系统消息
///
///
///
///
///
public void WriteLine(string msg, LogLevel level = LogLevel.Info, bool useLevel = true) => MaskMethod_WriteLine(Addon.Name, msg, level, useLevel);
///
/// 输出错误消息
///
///
///
public void Error(Exception e) => MaskMethod_Error(e);
///
/// JSON 工具类对象
///
public JsonTool JSON { get; } = new();
///
/// 新建一个BaseAddonController
///
///
///
public BaseAddonController(IAddon addon, Dictionary delegates)
{
Addon = (T)addon;
if (delegates.TryGetValue("WriteLine", out object? value)) MaskMethod_WriteLine = value != null ? (Action)value : new(DefaultPrint);
if (delegates.TryGetValue("Error", out value)) MaskMethod_Error = value != null ? (Action)value : new(DefaultPrint);
MaskMethod_WriteLine ??= new(DefaultPrint);
MaskMethod_Error ??= new(DefaultPrint);
}
///
/// 默认的输出错误消息方法
///
///
///
///
///
///
private void DefaultPrint(string name, string msg, LogLevel level = LogLevel.Info, bool useLevel = true)
{
DateTime now = DateTime.Now;
Console.Write("\r" + now.AddMilliseconds(-now.Millisecond).ToString() + $" {CommonSet.GetLogLevelPrefix(level)}/[Addon] {Addon.Name}:\n\r> ");
}
///
/// 输出错误消息
///
///
///
private void DefaultPrint(Exception e) => DefaultPrint(Addon.Name, e.ToString(), LogLevel.Error);
}
}