130 lines
4.1 KiB
C#
130 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace BITKit
|
|
{
|
|
public static class Data
|
|
{
|
|
[ExcuteOnStop]
|
|
public static void Reload()
|
|
{
|
|
dictionary.Clear();
|
|
events.Clear();
|
|
}
|
|
public static readonly Dictionary<string, object> Objects = new();
|
|
public static readonly ConcurrentDictionary<string, List<object>> rawEvents = new();
|
|
static readonly ConcurrentDictionary<string, object> dictionary = new();
|
|
static readonly ConcurrentDictionary<string, List<object>> events = new();
|
|
public static void AddListener<T>(Action<T> action, bool autoInvoke = false) =>
|
|
AddListener<T>(Constant.System.Internal, action, autoInvoke);
|
|
public static void RemoveListender<T>(Action<T> action) =>
|
|
RemoveListender<T>(Constant.System.Internal, action);
|
|
public static void Set<T>(T value) =>
|
|
Set<T>(Constant.System.Internal, value);
|
|
public static void AddListener<T>(string key, Action<T> action, bool autoInvoke = false)
|
|
{
|
|
rawEvents.AddOrUpdate(key, x => new List<object>() { action }, (x, y) =>
|
|
{
|
|
y.Add(action);
|
|
return y;
|
|
});
|
|
key = Generic<T>.GetVariable(key);
|
|
events.Get(key).Add(action);
|
|
if (autoInvoke)
|
|
{
|
|
if (dictionary.TryGetValue(key, out var value))
|
|
{
|
|
if (value is T t)
|
|
{
|
|
action.Invoke(t);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public static void RemoveListender<T>(string key, Action<T> action)
|
|
{
|
|
var list = rawEvents.GetOrAdd(key, x => new());
|
|
list.TryRemove(action);
|
|
key = Generic<T>.GetVariable(key);
|
|
events.Get(key).Remove(action);
|
|
}
|
|
|
|
public static void Set<T>(string key, Func<T> factory)
|
|
{
|
|
key = Generic<T>.GetVariable(key);
|
|
dictionary.AddOrUpdate(key,factory, AddOrUpdate);
|
|
return;
|
|
Func<T> AddOrUpdate(string _key,object current)
|
|
{
|
|
return factory;
|
|
}
|
|
}
|
|
public static void Set<T>(string key, T value)
|
|
{
|
|
key = Generic<T>.GetVariable(key);
|
|
if (dictionary.ContainsKey(key))
|
|
{
|
|
dictionary.AddOrUpdate(key, (x) => value, (x, y) => value);
|
|
}
|
|
else
|
|
{
|
|
dictionary.GetOrAdd(key, value);
|
|
}
|
|
|
|
var list = events.Get(key);
|
|
list.ToArray().ForEach(x =>
|
|
{
|
|
var action = x as Action<T>;
|
|
action.Invoke(value);
|
|
});
|
|
}
|
|
public static bool TryGetValue<T>(string key, out T value)
|
|
{
|
|
key = Generic<T>.GetVariable(key);
|
|
if (dictionary.TryGetValue(key, out var _value))
|
|
{
|
|
if (_value is T t)
|
|
{
|
|
value = t;
|
|
return true;
|
|
}
|
|
}
|
|
value = default;
|
|
return false;
|
|
}
|
|
public static T Get<T>(string key = Constant.System.Internal)
|
|
{
|
|
key = Generic<T>.GetVariable(key);
|
|
if (dictionary.TryGetValue(key, out var value))
|
|
{
|
|
switch (value)
|
|
{
|
|
case T t:
|
|
return t;
|
|
case Func<T> func:
|
|
return func.Invoke();
|
|
}
|
|
}
|
|
return default;
|
|
}
|
|
public static void Clear()
|
|
{
|
|
dictionary.Clear();
|
|
}
|
|
public static void Clear<T>()
|
|
{
|
|
ConcurrentDictionary<string, object> copy = new(dictionary);
|
|
object output;
|
|
foreach (var item in copy)
|
|
{
|
|
if (item.Value is T)
|
|
{
|
|
dictionary.Remove(item.Key, out output);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
} |