/
commandRegistry.c
111 lines (100 loc) · 3.51 KB
/
commandRegistry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//*****************************************************************************
// Copyright (c) 2018 - Allen Cummings, RealmsMUD, All rights reserved. See
// the accompanying LICENSE file for details.
//*****************************************************************************
private string PlayerCommands = "/lib/commands/player/";
private string WizardCommands = "/lib/commands/wizard/";
private string CannedEmotes = "/lib/commands/soul.c";
private string BaseCommand = "lib/commands/baseCommand.c";
private string Wizard = "lib/realizations/wizard.c";
private mapping commands = ([]);
private string *wizCommands = ({});
private int IsInitialized = 0;
/////////////////////////////////////////////////////////////////////////////
private nomask void registerPlayerCommands()
{
string *commandFiles = get_dir(PlayerCommands);
if(sizeof(commandFiles))
{
foreach(string command in commandFiles)
{
string fullyQualifiedFile = sprintf("%s/%s", PlayerCommands, command);
if(file_size(fullyQualifiedFile))
{
object commandObj = load_object(fullyQualifiedFile);
if(commandObj && (member(inherit_list(commandObj), BaseCommand) > -1))
{
commands[commandObj->commandRegExp()] = fullyQualifiedFile;
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
private nomask void registerWizardCommands()
{
string *commandFiles = get_dir(WizardCommands);
if (sizeof(commandFiles))
{
foreach(string command in commandFiles)
{
string fullyQualifiedFile = sprintf("%s/%s", WizardCommands, command);
if (file_size(fullyQualifiedFile))
{
object commandObj = load_object(fullyQualifiedFile);
if (commandObj && (member(inherit_list(commandObj), BaseCommand) > -1) &&
commandObj->commandRegExp())
{
commands[commandObj->commandRegExp()] = fullyQualifiedFile;
wizCommands += ({ commandObj->commandRegExp() });
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
public nomask int isInitialized()
{
return IsInitialized;
}
/////////////////////////////////////////////////////////////////////////////
public nomask void reset(int arg)
{
if (!arg)
{
commands = ([]);
registerPlayerCommands();
registerWizardCommands();
IsInitialized = 1;
}
}
/////////////////////////////////////////////////////////////////////////////
public nomask int executeCommand(string passedCommand, object initiator)
{
int ret = 0;
string *commandList = m_indices(commands);
if (member(inherit_list(initiator), Wizard) < 0)
{
commandList -= wizCommands;
}
if(sizeof(commandList))
{
if (sizeof(regexp(({ passedCommand }), "^('|:|=)[^ ]")))
{
passedCommand = passedCommand[0..0] + " " + passedCommand[1..];
}
foreach(string command in commandList)
{
if(sizeof(regexp(({ passedCommand }), command)))
{
object commandObj = load_object(commands[command]);
if(commandObj)
{
ret = commandObj->execute(passedCommand, initiator);
}
break;
}
}
}
return ret;
}