46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Reflection;
|
|
|
|
public class ModManager {
|
|
private readonly Dictionary<string, Delegate> _api = [];
|
|
private readonly Dictionary<string, Delegate> _hooks = [];
|
|
public delegate bool OnCallback(params object[] args);
|
|
private readonly Dictionary<string, List<OnCallback>> _callbackList = [];
|
|
|
|
public ReadOnlyDictionary<string, Delegate> GetAPIs() => new(_api);
|
|
public ReadOnlyDictionary<string, Delegate> GetHooks() => new(_hooks);
|
|
|
|
public ModManager RegistryFunc<TDelegate>(string name, TDelegate handler, bool isHook = false)
|
|
where TDelegate : Delegate {
|
|
MethodInfo _ = handler.Method;
|
|
Dictionary<string, Delegate> dict = isHook ? _hooks : _api;
|
|
dict.Add(name, handler);
|
|
return this;
|
|
}
|
|
|
|
public bool AddHook(string name, OnCallback handler) {
|
|
if (_hooks.ContainsKey(name)) {
|
|
if (!_callbackList.TryGetValue(name, out var list)) {
|
|
list = [];
|
|
_callbackList[name] = list;
|
|
}
|
|
list.Add(handler);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool InvokeFunc<TDelegate>(string name, params object[] args)
|
|
where TDelegate : Delegate {
|
|
List<object> res = [];
|
|
if (_callbackList.TryGetValue(name, out var callback)) {
|
|
foreach (var func in callback) {
|
|
func?.Invoke(args);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|