|
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 | + protected bool SetupCompleted = false; |
| 18 | + |
| 19 | + // Mage singleton accessor |
15 | 20 | protected Mage Mage
|
16 | 21 | {
|
17 | 22 | get { return Mage.Instance; }
|
18 | 23 | }
|
19 | 24 |
|
| 25 | + // Contextualized logger |
20 | 26 | protected Logger Logger
|
21 | 27 | {
|
22 | 28 | get { return Mage.Logger(GetType().Name); }
|
23 | 29 | }
|
24 | 30 |
|
25 |
| - |
26 |
| - // |
| 31 | + // Static |
27 | 32 | protected virtual List<string> StaticTopics
|
28 | 33 | {
|
29 |
| - get { return null; } |
| 34 | + get { return new List<string> {}; } |
30 | 35 | }
|
31 | 36 |
|
| 37 | + // Static data container |
32 | 38 | public JToken StaticData;
|
33 | 39 |
|
| 40 | + // Static data setup |
| 41 | + // Note that topics are not tied to MAGE modules; they are |
| 42 | + // essentially global to the MAGE server instance. This is |
| 43 | + // simply a convenience function for |
34 | 44 | public void SetupStaticData(Action<Exception> cb)
|
35 | 45 | {
|
36 | 46 | Logger.Info("Setting up static data");
|
@@ -67,28 +77,46 @@ public void SetupStaticData(Action<Exception> cb)
|
67 | 77 | }
|
68 | 78 |
|
69 | 79 |
|
70 |
| - // |
71 |
| - protected virtual string CommandPrefix |
72 |
| - { |
73 |
| - get { return null; } |
74 |
| - } |
| 80 | + // The module name as defined on the remote MAGE server |
| 81 | + protected abstract string CommandPrefix { get; } |
75 | 82 |
|
76 |
| - protected virtual List<string> Commands |
| 83 | + // The list of available user commands on the remote MAGE server |
| 84 | + protected abstract List<string> Commands { get; } |
| 85 | + |
| 86 | + private Dictionary<string, MageCommandAction> commandHandlerActions; |
| 87 | + private Dictionary<string, MageCommandFunction> commandHandlerFuncs; |
| 88 | + |
| 89 | + private void AssertSetupCompleted() |
77 | 90 | {
|
78 |
| - get { return null; } |
| 91 | + if (SetupCompleted == false) |
| 92 | + { |
| 93 | + throw new Exception("This module was not setup: " + CommandPrefix); |
| 94 | + } |
79 | 95 | }
|
80 | 96 |
|
81 |
| - private Dictionary<string, Action<JObject, Action<Exception, JToken>>> commandHandlerActions; |
82 |
| - private Dictionary<string, Func<JObject, UserCommandStatus>> commandHandlerFuncs; |
| 97 | + private M GetCommand<M>(Dictionary<string, M> list, string commandName) { |
| 98 | + AssertSetupCompleted(); |
| 99 | + |
| 100 | + var command = list[commandName]; |
| 101 | + |
| 102 | + if (command == null) |
| 103 | + { |
| 104 | + throw new Exception("User command not found: " + CommandPrefix + "." + commandName); |
| 105 | + } |
| 106 | + |
| 107 | + return command; |
| 108 | + } |
83 | 109 |
|
84 | 110 | public void Command(string commandName, JObject arguments, Action<Exception, JToken> cb)
|
85 | 111 | {
|
86 |
| - commandHandlerActions[commandName](arguments, cb); |
| 112 | + var action = GetCommand(commandHandlerActions, commandName); |
| 113 | + action(arguments, cb); |
87 | 114 | }
|
88 | 115 |
|
89 | 116 | public UserCommandStatus Command(string commandName, JObject arguments)
|
90 | 117 | {
|
91 |
| - return commandHandlerFuncs[commandName](arguments); |
| 118 | + var action = GetCommand(commandHandlerFuncs, commandName); |
| 119 | + return action(arguments); |
92 | 120 | }
|
93 | 121 |
|
94 | 122 | private void RegisterCommand(string command)
|
@@ -126,17 +154,12 @@ public void SetupUsercommands(Action<Exception> cb)
|
126 | 154 | commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>();
|
127 | 155 | commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>();
|
128 | 156 |
|
129 |
| - if (Commands == null) |
130 |
| - { |
131 |
| - cb(null); |
132 |
| - return; |
133 |
| - } |
134 |
| - |
135 | 157 | foreach (string command in Commands)
|
136 | 158 | {
|
137 | 159 | RegisterCommand(command);
|
138 | 160 | }
|
139 | 161 |
|
| 162 | + SetupCompleted = true; |
140 | 163 | cb(null);
|
141 | 164 | }
|
142 | 165 | }
|
|
0 commit comments