|
8 | 8 |
|
9 | 9 | namespace Wizcorp.MageSDK.MageClient |
10 | 10 | { |
11 | | - |
12 | | - public class Module<T> : Singleton<T> where T : class, new() |
| 11 | + using MageCommandAction = Action<JObject, Action<Exception, JToken>>; |
| 12 | + using MageCommandFunction = Func<JObject, UserCommandStatus>; |
| 13 | + |
| 14 | + public abstract class Module<T> : Singleton<T> where T : class, new() |
13 | 15 | { |
14 | | - // |
| 16 | + // Tells us if the module was set up |
| 17 | + private bool _SetupCompleted = false; |
| 18 | + public bool SetupCompleted { |
| 19 | + get { return _SetupCompleted; } |
| 20 | + private set { _SetupCompleted = value; } |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + // Mage singleton accessor |
15 | 25 | protected Mage Mage |
16 | 26 | { |
17 | 27 | get { return Mage.Instance; } |
18 | 28 | } |
19 | 29 |
|
| 30 | + // Contextualized logger |
20 | 31 | protected Logger Logger |
21 | 32 | { |
22 | 33 | get { return Mage.Logger(GetType().Name); } |
23 | 34 | } |
24 | 35 |
|
25 | | - |
26 | | - // |
| 36 | + // List of static topics to load during setup |
27 | 37 | protected virtual List<string> StaticTopics |
28 | 38 | { |
29 | 39 | get { return null; } |
30 | 40 | } |
31 | 41 |
|
| 42 | + // Static data container |
32 | 43 | public JToken StaticData; |
33 | 44 |
|
| 45 | + // Static data setup |
| 46 | + // |
| 47 | + // Note that topics are not tied to MAGE modules; they are |
| 48 | + // essentially global to the MAGE server instance. This is |
| 49 | + // simply a convenience function for loading data at setup time. |
34 | 50 | public void SetupStaticData(Action<Exception> cb) |
35 | 51 | { |
36 | 52 | Logger.Info("Setting up static data"); |
@@ -67,28 +83,50 @@ public void SetupStaticData(Action<Exception> cb) |
67 | 83 | } |
68 | 84 |
|
69 | 85 |
|
70 | | - // |
71 | | - protected virtual string CommandPrefix |
72 | | - { |
73 | | - get { return null; } |
74 | | - } |
| 86 | + // The module name as defined on the remote MAGE server |
| 87 | + protected abstract string CommandPrefix { get; } |
| 88 | + |
| 89 | + // The list of available user commands on the remote MAGE server |
| 90 | + protected abstract List<string> Commands { get; } |
75 | 91 |
|
76 | | - protected virtual List<string> Commands |
| 92 | + private Dictionary<string, MageCommandAction> commandHandlerActions; |
| 93 | + private Dictionary<string, MageCommandFunction> commandHandlerFuncs; |
| 94 | + |
| 95 | + private void AssertSetupCompleted() |
77 | 96 | { |
78 | | - get { return null; } |
| 97 | + if (SetupCompleted == false) |
| 98 | + { |
| 99 | + throw new Exception("Module was not setup: " + CommandPrefix); |
| 100 | + } |
79 | 101 | } |
80 | 102 |
|
81 | | - private Dictionary<string, Action<JObject, Action<Exception, JToken>>> commandHandlerActions; |
82 | | - private Dictionary<string, Func<JObject, UserCommandStatus>> commandHandlerFuncs; |
| 103 | + private M GetCommand<M>(Dictionary<string, M> list, string commandName) { |
| 104 | + AssertSetupCompleted(); |
| 105 | + |
| 106 | + if (list == null) { |
| 107 | + throw new Exception("Module does not define any user commands: " + CommandPrefix); |
| 108 | + } |
| 109 | + |
| 110 | + var command = list[commandName]; |
| 111 | + |
| 112 | + if (command == null) |
| 113 | + { |
| 114 | + throw new Exception("User command not found: " + CommandPrefix + "." + commandName); |
| 115 | + } |
| 116 | + |
| 117 | + return command; |
| 118 | + } |
83 | 119 |
|
84 | 120 | public void Command(string commandName, JObject arguments, Action<Exception, JToken> cb) |
85 | 121 | { |
86 | | - commandHandlerActions[commandName](arguments, cb); |
| 122 | + var action = GetCommand(commandHandlerActions, commandName); |
| 123 | + action(arguments, cb); |
87 | 124 | } |
88 | 125 |
|
89 | 126 | public UserCommandStatus Command(string commandName, JObject arguments) |
90 | 127 | { |
91 | | - return commandHandlerFuncs[commandName](arguments); |
| 128 | + var action = GetCommand(commandHandlerFuncs, commandName); |
| 129 | + return action(arguments); |
92 | 130 | } |
93 | 131 |
|
94 | 132 | private void RegisterCommand(string command) |
@@ -123,20 +161,21 @@ public void SetupUsercommands(Action<Exception> cb) |
123 | 161 | { |
124 | 162 | Logger.Info("Setting up usercommands"); |
125 | 163 |
|
126 | | - commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>(); |
127 | | - commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>(); |
128 | | - |
129 | | - if (Commands == null) |
130 | | - { |
| 164 | + if (Commands == null) { |
| 165 | + SetupCompleted = true; |
131 | 166 | cb(null); |
132 | 167 | return; |
133 | 168 | } |
134 | 169 |
|
| 170 | + commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>(); |
| 171 | + commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>(); |
| 172 | + |
135 | 173 | foreach (string command in Commands) |
136 | 174 | { |
137 | 175 | RegisterCommand(command); |
138 | 176 | } |
139 | 177 |
|
| 178 | + SetupCompleted = true; |
140 | 179 | cb(null); |
141 | 180 | } |
142 | 181 | } |
|
0 commit comments