-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatCommandAPI.cs
More file actions
306 lines (266 loc) · 10.7 KB
/
Copy pathChatCommandAPI.cs
File metadata and controls
306 lines (266 loc) · 10.7 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ChatCommandAPI.Commands;
using ChatCommandAPI.ServerCommands;
using ChatCommandAPI.Utils;
using HarmonyLib;
using Color = UnityEngine.Color;
using Help = ChatCommandAPI.Commands.Help;
namespace ChatCommandAPI;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInDependency(LethalModUtils.MyPluginInfo.PLUGIN_GUID)]
[BepInDependency("taffyko.NiceChat", BepInDependency.DependencyFlags.SoftDependency)]
public class ChatCommandAPI : BaseUnityPlugin
{
internal const string COMMAND_NAME_REGEX = @"\S+";
internal readonly Dictionary<string, Command> commands = [];
internal readonly Dictionary<string, ServerCommand> serverCommands = [];
private ConfigEntry<string> commandPrefix = null!;
private ConfigEntry<bool> compatibilityModeEnabled = null!;
private ConfigEntry<Color> defaultMessageColor = null!;
private ConfigEntry<string> serverCommandPrefix = null!;
private ConfigEntry<bool> serverCommandsEnabled = null!;
private ConfigEntry<string> serverWelcomeMessage = null!;
public static ChatCommandAPI Instance { get; private set; } = null!;
internal static new ManualLogSource Logger { get; private set; } = null!;
internal static Harmony? Harmony { get; set; }
public System.Drawing.Color DefaultMessageColor => defaultMessageColor.Value.ToSystem();
public char CommandPrefix => commandPrefix.Value[0];
public bool CompatibilityModeEnabled => compatibilityModeEnabled.Value;
public bool ServerCommandsEnabled => serverCommandsEnabled.Value;
public char ServerCommandPrefix => serverCommandPrefix.Value[0];
public string ServerWelcomeMessage =>
string.Format(serverWelcomeMessage.Value.Trim(), ServerCommandPrefix);
/// <summary>
/// A list of all registered commands and their primary commands (avoids duplicates)
/// </summary>
public IReadOnlyDictionary<string, Command> Commands => commands;
/// <summary>
/// A list of all registered server commands and their primary commands (avoids duplicates)
/// </summary>
public IReadOnlyDictionary<string, ServerCommand> ServerCommands => serverCommands;
private void Awake()
{
const string SECTION_GENERAL = "General";
const string SECTION_CLIENT = "Client";
const string SECTION_SERVER = "Server";
Logger = base.Logger;
Instance = this;
defaultMessageColor = Config.Bind(
SECTION_GENERAL,
nameof(DefaultMessageColor),
System.Drawing.Color.Aqua.ToUnity(),
"The default color to use for chat responses"
);
commandPrefix = Config.Bind(
SECTION_CLIENT,
nameof(CommandPrefix),
'/'.ToString(),
"The prefix to use for client-side commands"
);
compatibilityModeEnabled = Config.Bind(
SECTION_CLIENT,
nameof(CompatibilityModeEnabled),
false,
"Allows non-API chat commands to run (may cause the command you run to be sent in chat)"
);
serverCommandsEnabled = Config.Bind(
SECTION_SERVER,
nameof(ServerCommandsEnabled),
false,
"Enables or disables server commands"
);
serverCommandPrefix = Config.Bind(
SECTION_SERVER,
nameof(ServerCommandPrefix),
'!'.ToString(),
"The prefix to use for server-side commands"
);
serverWelcomeMessage = Config.Bind(
SECTION_SERVER,
nameof(ServerWelcomeMessage),
"This server supports chat commands.\nType {0}help for more information",
$"A welcome message that is displayed to any player that joins (clear to disable). {{0}} is replaced with {nameof(ServerCommandPrefix)}"
);
Config.SettingChanged += verifyConfigValues;
verifyConfigValues(null!, null!);
_ = new Help();
_ = new Clear();
_ = new ServerCommands.Help();
#if DEBUG
Logger.LogWarning(
"You are running a development version of this mod, which contains some additional commands for testing."
);
Logger.LogWarning("If you downloaded this mod from an official source, please report this");
_ = new ExampleCommand();
_ = new ExampleErrorCommand();
_ = new ExampleExceptionCommand();
_ = new ExampleTeleport();
_ = new ExampleDisableJump();
_ = new ExampleAdd();
_ = new ExampleIsPlayerDead();
_ = new ExampleShipLoot();
_ = new ExampleWhisper();
#endif
Patch();
Logger.LogInfo($"{MyPluginInfo.PLUGIN_GUID} v{MyPluginInfo.PLUGIN_VERSION} has loaded!");
return;
void Patch()
{
Harmony ??= new Harmony(MyPluginInfo.PLUGIN_GUID);
Logger.LogDebug("Patching...");
Harmony.PatchAll();
Logger.LogDebug("Finished patching!");
}
void verifyConfigValues(object args, object sender)
{
switch (commandPrefix.Value.Length)
{
case 1:
break;
case <= 0:
Logger.LogWarning(
$"Invalid {nameof(CommandPrefix)} value '{CommandPrefix}', value expected..."
);
commandPrefix.Value = (string)commandPrefix.DefaultValue;
goto serverCommandPrefix;
default:
Logger.LogWarning(
$"{nameof(CommandPrefix)} '{commandPrefix.Value}' contains more than one character, only the first one will be used"
);
break;
}
if (!char.IsSymbol(CommandPrefix) && !char.IsPunctuation(CommandPrefix))
{
Logger.LogWarning(
$"Invalid {nameof(CommandPrefix)} value '{CommandPrefix}', symbol expected..."
);
commandPrefix.Value = (string)commandPrefix.DefaultValue;
}
serverCommandPrefix:
switch (serverCommandPrefix.Value.Length)
{
case 1:
break;
case <= 0:
Logger.LogWarning(
$"Invalid {nameof(ServerCommandPrefix)} value '{ServerCommandPrefix}', symbol expected..."
);
serverCommandPrefix.Value = (string)serverCommandPrefix.DefaultValue;
goto compare;
default:
Logger.LogWarning(
$"{nameof(ServerCommandPrefix)} '{serverCommandPrefix.Value}' contains more than one character, only the first one will be used"
);
break;
}
if (!char.IsSymbol(ServerCommandPrefix) && !char.IsPunctuation(ServerCommandPrefix))
{
Logger.LogWarning(
$"Invalid {nameof(ServerCommandPrefix)} value '{ServerCommandPrefix}', symbol expected..."
);
serverCommandPrefix.Value = (string)serverCommandPrefix.DefaultValue;
}
compare:
if (ServerCommandPrefix == CommandPrefix)
Logger.LogWarning(
$"{nameof(ServerCommandPrefix)} is the same as {nameof(CommandPrefix)}, this configuration will prioritize client commands and is not supported"
);
}
}
public static bool TryParseCommand(
char prefix,
string cmdline,
out string name,
out string args
)
{
name = null!;
args = null!;
var match = Regex.Match(
cmdline,
$"^{Regex.Escape(prefix.ToString())}({COMMAND_NAME_REGEX})(?: (.*))?$"
);
if (!match.Success)
return false;
var _args = match.Groups[2];
name = match.Groups[1].Value;
args = _args.Success ? _args.Value : string.Empty;
return true;
}
public bool TryParseCommand(string cmdline, out string name, out string args)
{
return TryParseCommand(CommandPrefix, cmdline, out name, out args);
}
public bool TryParseServerCommand(string cmdline, out string name, out string args)
{
return TryParseCommand(ServerCommandPrefix, cmdline, out name, out args);
}
public bool TryGetCommand(string name, out Command command, out string primaryName)
{
primaryName = name = name.ToLowerInvariant();
var commands = Commands;
if (commands.TryGetValue(name, out command))
return true;
foreach (var kvp in commands)
if (
string.Equals(kvp.Value.FullName, name, StringComparison.InvariantCultureIgnoreCase)
)
{
primaryName = kvp.Key;
command = kvp.Value;
return true;
}
foreach (var kvp in commands)
if (string.Equals(kvp.Value.Command, name, StringComparison.InvariantCultureIgnoreCase))
{
primaryName = kvp.Key;
command = kvp.Value;
return true;
}
foreach (var kvp in commands)
if (kvp.Value.Aliases.Contains(name, StringComparer.InvariantCultureIgnoreCase))
{
primaryName = kvp.Key;
command = kvp.Value;
return true;
}
return false;
}
public bool TryGetServerCommand(string name, out ServerCommand command, out string primaryName)
{
primaryName = name = name.ToLowerInvariant();
var commands = ServerCommands;
if (commands.TryGetValue(name, out command))
return true;
foreach (var kvp in commands)
if (
string.Equals(kvp.Value.FullName, name, StringComparison.InvariantCultureIgnoreCase)
)
{
primaryName = kvp.Key;
command = kvp.Value;
return true;
}
foreach (var kvp in commands)
if (string.Equals(kvp.Value.Command, name, StringComparison.InvariantCultureIgnoreCase))
{
primaryName = kvp.Key;
command = kvp.Value;
return true;
}
foreach (var kvp in commands)
if (kvp.Value.Aliases.Contains(name, StringComparer.InvariantCultureIgnoreCase))
{
primaryName = kvp.Key;
command = kvp.Value;
return true;
}
return false;
}
}