using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Reflection; public class ModManager { private readonly Dictionary _api = []; private readonly Dictionary _hooks = []; public delegate bool OnCallback(params object[] args); private readonly Dictionary> _callbackList = []; public ReadOnlyDictionary GetAPIs() => new(_api); public ReadOnlyDictionary GetHooks() => new(_hooks); public ModManager RegistryFunc(string name, TDelegate handler, bool isHook = false) where TDelegate : Delegate { MethodInfo _ = handler.Method; Dictionary 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(string name, params object[] args) where TDelegate : Delegate { List res = []; if (_callbackList.TryGetValue(name, out var callback)) { foreach (var func in callback) { func?.Invoke(args); } } return true; } }