This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
PluginManager.cs
112 lines (97 loc) · 2.95 KB
/
PluginManager.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
namespace Oxide
{
/// <summary>
/// Keeps track and interfaces with all loaded plugins
/// </summary>
public class PluginManager
{
private HashSet<Plugin> allplugins;
private Dictionary<string, HashSet<Plugin>> hooks;
public PluginManager()
{
allplugins = new HashSet<Plugin>();
hooks = new Dictionary<string, HashSet<Plugin>>();
}
/// <summary>
/// Adds a plugin to this manager
/// </summary>
/// <param name="plugin"></param>
public void AddPlugin(Plugin plugin)
{
// Add to all plugins
allplugins.Add(plugin);
// Register all hooks
foreach (string hookname in plugin.GetHooks())
{
HashSet<Plugin> set;
if (!hooks.TryGetValue(hookname, out set))
{
set = new HashSet<Plugin>();
hooks.Add(hookname, set);
}
set.Add(plugin);
}
}
/// <summary>
/// Removes a plugin from this manager
/// </summary>
/// <param name="plugin"></param>
public void RemovePlugin(Plugin plugin)
{
// Remove from all plugins
allplugins.Remove(plugin);
// Unregister any hooks
foreach (var pair in hooks)
pair.Value.Remove(plugin);
}
/// <summary>
/// Calls a hook on all plugins
/// </summary>
/// <param name="hookname"></param>
/// <param name="args"></param>
public object Call(string hookname, object[] args)
{
// Locate the plugin set
HashSet<Plugin> set;
if (!hooks.TryGetValue(hookname, out set)) return null;
// Loop each plugin
foreach (Plugin plugin in set.ToArray())
{
// Make the call
object result = plugin.Call(hookname, args);
// Is there a return value?
if (result != null) return result;
}
// No return value
return null;
}
/// <summary>
/// Returns an enumerable for all loaded plugins
/// </summary>
/// <returns></returns>
public IEnumerable<Plugin> GetPlugins()
{
return allplugins;
}
/// <summary>
/// Returns a specific plugin
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Plugin this[string name]
{
get
{
// Try and find it
foreach (Plugin plugin in allplugins)
if (plugin.Name == name)
return plugin;
// Not found
return null;
}
}
}
}