|
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 |
| - get { return null; } |
| 39 | + get { return new List<string> {}; } |
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,46 @@ 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; } |
75 | 88 |
|
76 |
| - protected virtual List<string> Commands |
| 89 | + // The list of available user commands on the remote MAGE server |
| 90 | + protected abstract List<string> Commands { get; } |
| 91 | + |
| 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("This 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 | + var command = list[commandName]; |
| 107 | + |
| 108 | + if (command == null) |
| 109 | + { |
| 110 | + throw new Exception("User command not found: " + CommandPrefix + "." + commandName); |
| 111 | + } |
| 112 | + |
| 113 | + return command; |
| 114 | + } |
83 | 115 |
|
84 | 116 | public void Command(string commandName, JObject arguments, Action<Exception, JToken> cb)
|
85 | 117 | {
|
86 |
| - commandHandlerActions[commandName](arguments, cb); |
| 118 | + var action = GetCommand(commandHandlerActions, commandName); |
| 119 | + action(arguments, cb); |
87 | 120 | }
|
88 | 121 |
|
89 | 122 | public UserCommandStatus Command(string commandName, JObject arguments)
|
90 | 123 | {
|
91 |
| - return commandHandlerFuncs[commandName](arguments); |
| 124 | + var action = GetCommand(commandHandlerFuncs, commandName); |
| 125 | + return action(arguments); |
92 | 126 | }
|
93 | 127 |
|
94 | 128 | private void RegisterCommand(string command)
|
@@ -126,17 +160,12 @@ public void SetupUsercommands(Action<Exception> cb)
|
126 | 160 | commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>();
|
127 | 161 | commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>();
|
128 | 162 |
|
129 |
| - if (Commands == null) |
130 |
| - { |
131 |
| - cb(null); |
132 |
| - return; |
133 |
| - } |
134 |
| - |
135 | 163 | foreach (string command in Commands)
|
136 | 164 | {
|
137 | 165 | RegisterCommand(command);
|
138 | 166 | }
|
139 | 167 |
|
| 168 | + SetupCompleted = true; |
140 | 169 | cb(null);
|
141 | 170 | }
|
142 | 171 | }
|
|
0 commit comments