Skip to content

Commit 0851e02

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 mage#63
1 parent e641979 commit 0851e02

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

Mage/Module.cs

Lines changed: 52 additions & 23 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
{
29-
get { return null; }
39+
get { return new List<string> {}; }
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,46 @@ 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; }
7588

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()
7796
{
78-
get { return null; }
97+
if (SetupCompleted == false)
98+
{
99+
throw new Exception("This 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+
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+
}
83115

84116
public void Command(string commandName, JObject arguments, Action<Exception, JToken> cb)
85117
{
86-
commandHandlerActions[commandName](arguments, cb);
118+
var action = GetCommand(commandHandlerActions, commandName);
119+
action(arguments, cb);
87120
}
88121

89122
public UserCommandStatus Command(string commandName, JObject arguments)
90123
{
91-
return commandHandlerFuncs[commandName](arguments);
124+
var action = GetCommand(commandHandlerFuncs, commandName);
125+
return action(arguments);
92126
}
93127

94128
private void RegisterCommand(string command)
@@ -126,17 +160,12 @@ public void SetupUsercommands(Action<Exception> cb)
126160
commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>();
127161
commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>();
128162

129-
if (Commands == null)
130-
{
131-
cb(null);
132-
return;
133-
}
134-
135163
foreach (string command in Commands)
136164
{
137165
RegisterCommand(command);
138166
}
139167

168+
SetupCompleted = true;
140169
cb(null);
141170
}
142171
}

0 commit comments

Comments
 (0)