-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHooks.cs
More file actions
80 lines (66 loc) · 1.83 KB
/
Hooks.cs
File metadata and controls
80 lines (66 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
namespace Pluton.Core
{
using System;
using System.Reactive.Subjects;
using System.Collections.Generic;
using PluginLoaders;
public class Hook : CountedInstance
{
~Hook()
{
if (hook != null)
hook.Dispose();
}
public Hook(string method, Action<object[]> callback)
{
if (Hooks.GetInstance().HookNames.Contains(method))
hook = Hooks.GetInstance().Subjects[method].Subscribe(callback);
else
throw new Exception($"Can't find the hook '{method}' to subscribe to.");
Name = method;
}
public string Name;
public IDisposable hook;
}
public class Hooks : Singleton<Hooks>, ISingleton
{
public void Initialize()
{
}
public static bool Loaded = false;
public List<string> HookNames = new List<string>();
internal Dictionary<string, Subject<object[]>> Subjects = new Dictionary<string, Subject<object[]>>()
{
{ "On_AllPluginLoaded", new Subject<object[]>() },
{ "On_PluginLoaded", new Subject<object[]>() }
};
public Dictionary<string, Subject<object[]>> CreateOrUpdateSubjects()
{
for (int i = 0; i < HookNames.Count; i++) {
string hookName = HookNames[i];
if (!Subjects.ContainsKey(hookName))
Subjects.Add(hookName, new Subject<object[]>());
}
return Subjects;
}
public static void OnNext(string hook, params object[] args)
{
if (Loaded)
Instance.Subjects[hook].OnNext(args);
else
Console.WriteLine($"[Hooks] Not calling method: {hook}, because Hooks is not initialized yet.");
}
public static Hook Subscribe(string hookname, Action<object[]> callback)
{
return new Hook(hookname, callback);
}
public static Hook Subscribe(string hookname, BasePlugin plugin)
{
return new Hook(hookname, args => plugin.Invoke(hookname, args));
}
public static void On_PluginLoaded(BasePlugin plugin)
{
OnNext("On_PluginLoaded", plugin);
}
}
}