BITKit/Packages/Runtime/Utility/Processor.cs

45 lines
1.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit
{
/// <summary>加工系统.处理系统,加工一个数据后返回,处理一个数据后返回</summary>
public interface IProcessor
{
T GetContext<T>(string key, T value);
void AddProcessor<T>(string key, Func<T, T> func);
void RemoveProcessor<T>(string key, Func<T, T> func);
T GetContext<T>(T value = default);
void AddProcessor<T>(Func<T, T> func);
void RemoveProcessor<T>(Func<T, T> func);
}
public class Processor : IProcessor
{
public Dictionary<string, List<object>> dictionary = new();
public T GetContext<T>(T value = default) => GetContext<T>(key, value);
public void AddProcessor<T>(Func<T, T> func) => AddProcessor<T>(key, func);
public void RemoveProcessor<T>(Func<T, T> func) => RemoveProcessor<T>(key, func);
string key => nameof(Processor);
public T GetContext<T>(string key, T value)
{
key = string.Empty.GetType<T>();
dictionary.Get(key).ForEach(x =>
{
var func = x as Func<T, T>;
value = func.Invoke(value);
});
return value;
}
public void AddProcessor<T>(string key, Func<T, T> func)
{
key = string.Empty.GetType<T>();
dictionary.Get(key).Add(func);
}
public void RemoveProcessor<T>(string key, Func<T, T> func)
{
key = string.Empty.GetType<T>();
dictionary.Get(key).Remove(func);
}
}
}