1 module commands.uda;
2 
3 alias CommandType = string delegate(string[]);
4 
5 struct CmdTableEntry {
6     CommandType cmd;
7     string desc;
8     int minArgs;
9     int maxArgs;
10 }
11 
12 struct CommandNamespace {
13     string name;
14 }
15 
16 struct Command {
17     string name;
18     string desc = "";
19     int minArgs = 0;
20     int maxArgs = 0;
21 }
22 
23 mixin template RegisterModule(T) {
24     mixin("__gshared T " ~ T.stringof ~ "Singleton;");
25     shared static this() {
26         import commands : gCommandInterpreter;
27         import std.traits;
28         mixin(T.stringof ~ "Singleton = new T();");
29         static foreach(m; __traits(allMembers, T)) {{
30             static if (hasUDA!(__traits(getMember, T, m), CommandNamespace)) {
31                 static if (hasUDA!(__traits(getMember, T, m), Command)) {
32                     enum namespace = getUDAs!(__traits(getMember, T, m), CommandNamespace)[0].name;
33                     enum cmdName = getUDAs!(__traits(getMember, T, m), Command)[0].name; 
34                     pragma(msg, "command " ~ cmdName ~ ", namespace: " ~ namespace);
35                     gCommandInterpreter.registerCommand(namespace, getUDAs!(__traits(getMember, T, m), Command), mixin("&" ~ T.stringof ~ "Singleton." ~ m));
36                 }
37             } else {
38                 static if (hasUDA!(__traits(getMember, T, m), Command)) {
39                     enum cmdName = getUDAs!(__traits(getMember, T, m), Command)[0].name; 
40                     pragma(msg, "command " ~ cmdName ~ ", namespace: global");
41                     gCommandInterpreter.registerCommand(getUDAs!(__traits(getMember, T, m), Command), mixin("&" ~ T.stringof ~ "Singleton." ~ m));
42                 }
43             }
44         }}
45     }
46 }