Skip to content

Commit a95ce2b

Browse files
committed
Throw if module is not setup or cmd missing
- Transform CommandPrefix and Commands into abstracts; this should help provide a more comprehensive experience to newcomers - Track whether a module was setup or not, and throw an exception when you try to call a command on a non-setup module - Throw an exception if a user command is not found Fixes #63
1 parent e641979 commit a95ce2b

File tree

1 file changed

+60
-21
lines changed

1 file changed

+60
-21
lines changed

Mage/Module.cs

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,45 @@
88

99
namespace Wizcorp.MageSDK.MageClient
1010
{
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()
1315
{
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
1525
protected Mage Mage
1626
{
1727
get { return Mage.Instance; }
1828
}
1929

30+
// Contextualized logger
2031
protected Logger Logger
2132
{
2233
get { return Mage.Logger(GetType().Name); }
2334
}
2435

25-
26-
//
36+
// List of static topics to load during setup
2737
protected virtual List<string> StaticTopics
2838
{
2939
get { return null; }
3040
}
3141

42+
// Static data container
3243
public JToken StaticData;
3344

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.
3450
public void SetupStaticData(Action<Exception> cb)
3551
{
3652
Logger.Info("Setting up static data");
@@ -67,28 +83,50 @@ public void SetupStaticData(Action<Exception> cb)
6783
}
6884

6985

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; }
7591

76-
protected virtual List<string> Commands
92+
private Dictionary<string, MageCommandAction> commandHandlerActions;
93+
private Dictionary<string, MageCommandFunction> commandHandlerFuncs;
94+
95+
private void AssertSetupCompleted()
7796
{
78-
get { return null; }
97+
if (SetupCompleted == false)
98+
{
99+
throw new Exception("Module was not setup: " + CommandPrefix);
100+
}
79101
}
80102

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+
}
83119

84120
public void Command(string commandName, JObject arguments, Action<Exception, JToken> cb)
85121
{
86-
commandHandlerActions[commandName](arguments, cb);
122+
var action = GetCommand(commandHandlerActions, commandName);
123+
action(arguments, cb);
87124
}
88125

89126
public UserCommandStatus Command(string commandName, JObject arguments)
90127
{
91-
return commandHandlerFuncs[commandName](arguments);
128+
var action = GetCommand(commandHandlerFuncs, commandName);
129+
return action(arguments);
92130
}
93131

94132
private void RegisterCommand(string command)
@@ -123,20 +161,21 @@ public void SetupUsercommands(Action<Exception> cb)
123161
{
124162
Logger.Info("Setting up usercommands");
125163

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;
131166
cb(null);
132167
return;
133168
}
134169

170+
commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>();
171+
commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>();
172+
135173
foreach (string command in Commands)
136174
{
137175
RegisterCommand(command);
138176
}
139177

178+
SetupCompleted = true;
140179
cb(null);
141180
}
142181
}

0 commit comments

Comments
 (0)