2023-08-12 01:43:24 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2023-10-20 19:31:12 +08:00
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
|
namespace BITKit
|
|
|
|
|
{
|
|
|
|
|
/// <summary>泛型事件,简单的事件接口</summary>
|
|
|
|
|
public interface IGenericEvent<TKey>
|
|
|
|
|
{
|
|
|
|
|
void AddListener<T>(Action<T> action);
|
|
|
|
|
void Invoke<T>(T value);
|
|
|
|
|
void Invoke<T>() where T : new();
|
|
|
|
|
void RemoveListener<T>(Action<T> action);
|
|
|
|
|
void AddListener<T>(TKey key, Action<T> action);
|
|
|
|
|
void Invoke<T>(TKey key, T value);
|
|
|
|
|
void RemoveListener<T>(TKey key, Action<T> action);
|
2023-11-15 23:54:54 +08:00
|
|
|
|
|
|
|
|
|
void SetDirect(int key, object value);
|
|
|
|
|
void GetDirect(int key, out object value);
|
|
|
|
|
void GetDirect<T>(int key,out T value);
|
2023-08-12 01:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>泛型数据库,主要使用方式为Get和Set</summary>
|
|
|
|
|
public interface IDatabase
|
|
|
|
|
{
|
|
|
|
|
T Get<T>(string key = Constant.System.Internal);
|
|
|
|
|
void Set<T>(T value);
|
|
|
|
|
void Set<T>(string key = Constant.System.Internal, T value = default);
|
|
|
|
|
}
|
|
|
|
|
public class GenericEvent : IGenericEvent<string>, IDatabase, IDiagnostics
|
|
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
|
private readonly Dictionary<int,object> _directDirection = new();
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<string, List<object>> events = new();
|
|
|
|
|
private readonly Dictionary<string, object> dictionary = new();
|
2023-08-12 01:43:24 +08:00
|
|
|
|
public const string defaultEventName = nameof(GenericEvent);
|
|
|
|
|
public void AddListener<T>(Action<T> action) =>
|
|
|
|
|
AddListener<T>(defaultEventName, action);
|
|
|
|
|
public void Invoke<T>(T value) =>
|
|
|
|
|
Invoke<T>(defaultEventName, value);
|
|
|
|
|
public void RemoveListener<T>(Action<T> action) =>
|
|
|
|
|
RemoveListener<T>(defaultEventName, action);
|
|
|
|
|
public void AddListener<T>(string key, Action<T> action)
|
|
|
|
|
{
|
|
|
|
|
key = key.GetType<T>();
|
|
|
|
|
var list = events.Get(key);
|
|
|
|
|
list.Add(action);
|
|
|
|
|
}
|
2023-10-20 19:31:12 +08:00
|
|
|
|
|
|
|
|
|
public void AddListenerDirect(string key, Action<object> action)
|
|
|
|
|
{
|
|
|
|
|
events.Get(key).Add(action);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
|
public void Invoke<T>(string key, T value)
|
|
|
|
|
{
|
|
|
|
|
key = key.GetType<T>();
|
|
|
|
|
var list = events.Get(key);
|
|
|
|
|
dictionary.Set(key, value);
|
|
|
|
|
list.ToArray().ForEach(x =>
|
|
|
|
|
{
|
|
|
|
|
if (x is Action<T> action)
|
|
|
|
|
{
|
|
|
|
|
action.Invoke(value);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-10-20 19:31:12 +08:00
|
|
|
|
public void InvokeDirect(string key,object value)
|
|
|
|
|
{
|
|
|
|
|
foreach (var x in events.Get(key))
|
|
|
|
|
{
|
|
|
|
|
if (x is Action<object> action)
|
|
|
|
|
{
|
|
|
|
|
//Console.WriteLine(action.Method);
|
|
|
|
|
action.Invoke(value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"事件{key}的监听器{x}不是Action<object>类型");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// list.ToArray().ForEach(x =>
|
|
|
|
|
// {
|
|
|
|
|
// if (x is Action<object> action)
|
|
|
|
|
// {
|
|
|
|
|
// action.Invoke(value);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// Console.WriteLine(x);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
|
public void Invoke(string typeName, object value)
|
|
|
|
|
{
|
|
|
|
|
var key = $"{typeName}.{defaultEventName}";
|
|
|
|
|
var list = events.Get(key);
|
|
|
|
|
//BIT4Log.Log<GenericEvent>($"{key}\t@\t{list.Count}");
|
|
|
|
|
foreach (var item in list.ToArray())
|
|
|
|
|
{
|
|
|
|
|
//BIT4Log.Log<GenericEvent>($"event:{item}");
|
|
|
|
|
item.GetType().GetMethod("Invoke").Invoke(item, new object[] { value });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public T Get<T>(string key = Constant.System.Internal)
|
|
|
|
|
{
|
|
|
|
|
key = key.GetType<T>();
|
|
|
|
|
var value = dictionary.Get(key);
|
|
|
|
|
if (value is T t)
|
|
|
|
|
{
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
public object Get(string key = Constant.System.Internal)
|
|
|
|
|
{
|
|
|
|
|
return dictionary[key];
|
|
|
|
|
}
|
|
|
|
|
public void Set<T>(T value) => dictionary.Set(Constant.System.Internal.GetType<T>(), value);
|
|
|
|
|
public void Set<T>(string key, T value) => dictionary.Set(key.GetType<T>(), value);
|
2023-08-23 01:59:40 +08:00
|
|
|
|
public void Set(Type type, object value) => dictionary.Set($"{type.FullName}.{Constant.System.Internal}", value);
|
2023-10-20 19:31:12 +08:00
|
|
|
|
public void SetDirect(string key, object value) => dictionary.Set(key, value);
|
2023-08-12 01:43:24 +08:00
|
|
|
|
public void RemoveListener<T>(string key, Action<T> action)
|
|
|
|
|
{
|
|
|
|
|
key = key.GetType<T>();
|
|
|
|
|
var list = events.Get(key);
|
|
|
|
|
list.TryRemove(action);
|
|
|
|
|
}
|
2023-11-15 23:54:54 +08:00
|
|
|
|
|
|
|
|
|
public void SetDirect(int key, object value)
|
|
|
|
|
{
|
|
|
|
|
_directDirection.Set(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetDirect(int key,out object value)
|
|
|
|
|
{
|
|
|
|
|
value = _directDirection.Get(key);
|
|
|
|
|
}
|
|
|
|
|
public void GetDirect<T>(int key,out T value)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
value = (T) _directDirection.Get(key);
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidCastException)
|
|
|
|
|
{
|
|
|
|
|
value = default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
|
public void DebugAllValues()
|
|
|
|
|
{
|
|
|
|
|
StringBuilder stringBuilder = new();
|
|
|
|
|
stringBuilder.AppendLine($"Event Count:{events.Count}");
|
|
|
|
|
foreach (var x in events)
|
|
|
|
|
{
|
|
|
|
|
stringBuilder.AppendLine(x.ToString());
|
|
|
|
|
}
|
|
|
|
|
BIT4Log.Log<GenericEvent>(stringBuilder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetName() => nameof(GenericEvent);
|
|
|
|
|
|
|
|
|
|
public object GetDiagnostics()
|
|
|
|
|
{
|
|
|
|
|
StringBuilder stringBuilder = new("——————Dictionary");
|
|
|
|
|
foreach (var x in dictionary)
|
|
|
|
|
{
|
|
|
|
|
stringBuilder.AppendLine(x.ToString());
|
|
|
|
|
}
|
|
|
|
|
stringBuilder.AppendLine("——————Events");
|
|
|
|
|
foreach (var x in events)
|
|
|
|
|
{
|
|
|
|
|
stringBuilder.AppendLine($"{x.Key}\t{x.Value.Count}");
|
|
|
|
|
}
|
|
|
|
|
return stringBuilder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Invoke<T>() where T : new() => Invoke(new T());
|
2023-10-20 19:31:12 +08:00
|
|
|
|
|
|
|
|
|
public bool TryGetObjectDirect(string key, out object value)
|
|
|
|
|
{
|
|
|
|
|
return dictionary.TryGetValue(key, out value);
|
|
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|