-
Notifications
You must be signed in to change notification settings - Fork 18
/
patch.c
176 lines (159 loc) · 5.07 KB
/
patch.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//*****************************************************************************
// Copyright (c) 2018 - Allen Cummings, RealmsMUD, All rights reserved. See
// the accompanying LICENSE file for details.
//*****************************************************************************
inherit "/lib/commands/baseCommand.c";
/////////////////////////////////////////////////////////////////////////////
public nomask void reset(int arg)
{
if (!arg)
{
addCommandTemplate("patch [-t ##Target##] [-f ##Function##] [-v ##Values##] [.*]");
}
}
/////////////////////////////////////////////////////////////////////////////
private int sortArray(mixed a, mixed b)
{
string compA;
string compB;
if (mappingp(a) && mappingp(b))
{
compA = this_object()->convertDataToString(a);
compB = this_object()->convertDataToString(b);
}
else
{
compA = to_string(a);
compB = to_string(b);
}
return compA > compB;
}
/////////////////////////////////////////////////////////////////////////////
private string convertDataToString(mixed data)
{
string ret = "";
if (objectp(data))
{
ret += program_name(data);
}
else if (pointerp(data) && sizeof(data))
{
ret += "({ ";
data = sort_array(data, "sortArray");
foreach(mixed element in data)
{
ret += convertDataToString(element) + ", ";
}
ret += "})";
}
else if (mappingp(data))
{
ret += "([ ";
mixed *indices = sort_array(m_indices(data), "sortArray");
foreach(mixed index in indices)
{
ret += convertDataToString(index) + ": " + convertDataToString(data[index]) + ", ";
}
ret += "])";
}
else
{
ret += to_string(data);
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private object getPatchTarget(string command, object initiator)
{
object target = 0;
if (sizeof(regexp(({ command }), "-t [^-]+")))
{
string targetString = regreplace(command, ".*-t ([^-]+)( .*|$)", "\\1", 1);
target = present(targetString, initiator) ||
present(targetString, environment(initiator)) ||
find_object(targetString);
if (!target)
{
string targetPath = initiator->hasReadAccess(targetString);
if (targetPath && (file_size(targetPath) > 0))
{
catch (target = load_object(targetPath));
}
}
}
return target;
}
/////////////////////////////////////////////////////////////////////////////
private string getPatchFunction(string command, object initiator)
{
string methodName = 0;
if (sizeof(regexp(({ command }), "-f [A-Za-z_0-9]+")))
{
methodName = regreplace(command, ".*-f ([A-Za-z_0-9]+).*", "\\1", 1);
}
return methodName;
}
/////////////////////////////////////////////////////////////////////////////
private mixed *getPatchParameters(string command, object initiator)
{
mixed *parameters = 0;
if (sizeof(regexp(({ command }), "-v [^-]+")))
{
mixed *paramList = regexplode(regreplace(command, ".*-v ([^-]+)( .*|$)", "\\1", 1),
", *") - ({ ",", ", " });
parameters = ({ });
foreach(mixed parameter in paramList)
{
if (to_object(parameter))
{
parameters += ({ to_object(parameter) });
}
else if (sizeof(regexp(({ parameter }), "[0-9]*\\.[0-9]+")))
{
parameters += ({ to_float(parameter) });
}
else if (to_int(parameter))
{
parameters += ({ to_int(parameter) });
}
else
{
parameters += ({ to_string(parameter) });
}
}
}
return parameters;
}
/////////////////////////////////////////////////////////////////////////////
public nomask int execute(string command, object initiator)
{
int ret = 0;
if (canExecuteCommand(command) && initiator->hasExecuteAccess("patch"))
{
object target = getPatchTarget(command, initiator);
string methodName = getPatchFunction(command, initiator);
mixed *parameters = getPatchParameters(command, initiator);
if (objectp(target))
{
if (methodName && function_exists(methodName, target))
{
ret = 1;
tell_object(initiator, sprintf("Function '%s' found in %O.\n",
methodName, target));
mixed output = apply(#'call_other, target, methodName, parameters);
tell_object(initiator, sprintf("Returned: %s\n",
convertDataToString(output)));
}
else
{
notify_fail(sprintf("Function '%s' does not exist in %O.\n",
to_string(methodName), target));
}
}
else
{
notify_fail(sprintf("Couldn't find object '%O'.\n", target));
}
}
return ret;
}