update: 更新到godot4.4.1,并大量重构代码
This commit is contained in:
45
Scripts/Mods/ModManager.cs
Normal file
45
Scripts/Mods/ModManager.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user