Skip to content

Commit a24ea03

Browse files
committed
Added command exception logs to the server console, added commands for holiday features, fixed commands with more than one argument failing with "MISSING_ARGS"
1 parent 34f3c6e commit a24ea03

8 files changed

Lines changed: 71 additions & 80 deletions

File tree

LabExtended/Commands/Custom/Holidays/HolidaysCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class HolidaysCommand : CommandBase, IServerSideCommand
2020
/// Sets the active status of the Hubert skybox.
2121
/// </summary>
2222
/// <param name="status">true to activate the Hubert skybox; otherwise, false.</param>
23-
[CommandOverload("hubertskybox", "Sets the Hubert skybox status.")]
23+
[CommandOverload("hubert skybox", "Sets the Hubert skybox status.", null)]
2424
public void HubertSkybox(
2525
[CommandParameter("Status", "The status of the skybox to set.")] bool status)
2626
{
@@ -34,7 +34,7 @@ public void HubertSkybox(
3434
/// </summary>
3535
/// <remarks>Use this method to create and register a Hubert Moon entity. The method outputs a
3636
/// confirmation message including the unique network identifier of the spawned instance.</remarks>
37-
[CommandOverload("hubertmoon", "Spawns a Hubert Moon instance.")]
37+
[CommandOverload("hubert moon", "Spawns a Hubert Moon instance.", null)]
3838
public void HubertMoon()
3939
{
4040
var instance = ExMap.SpawnHubertMoon();

LabExtended/Commands/Parameters/CommandParameter.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,19 @@ public CommandParameter(ParameterInfo parameterInfo)
107107

108108
if (optionsAttribute.ParserType != null)
109109
{
110-
if (!CommandParameterParserUtils.Parsers.TryGetFirst(p => p.Value.GetType() == optionsAttribute.ParserType, out var parserInstance))
110+
if (CommandParameterParserUtils.TryGetParser(optionsAttribute.ParserType, out var parserInstance))
111111
{
112-
ApiLog.Error("LabExtended", $"Parser &3{optionsAttribute.ParserType.FullName}&r has not been registered!");
112+
Parsers[parserInstance] = optionsAttribute.ParserProperty ?? string.Empty;
113113
continue;
114114
}
115115

116-
Parsers[parserInstance.Value] = optionsAttribute.ParserProperty ?? string.Empty;
116+
if (CommandParameterParserUtils.Parsers.TryGetFirst(p => p.Value.GetType() == optionsAttribute.ParserType, out var parserPair))
117+
{
118+
Parsers[parserPair.Value] = optionsAttribute.ParserProperty ?? string.Empty;
119+
continue;
120+
}
121+
122+
ApiLog.Error("LabExtended", $"Parser &3{optionsAttribute.ParserType.FullName}&r has not been registered!");
117123
}
118124
}
119125
}

LabExtended/Commands/Parameters/CommandParameterParserUtils.cs

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,35 @@ public static string[] CompileParameters(this IEnumerable<CommandParameter> para
192192
/// <param name="parserResult">The parsing result of the parser.</param>
193193
/// <typeparam name="T">The type to parse.</typeparam>
194194
/// <returns>true if the parser was found, otherwise false</returns>
195-
public static bool TryParse<T>(CommandContext context, string value, out CommandParameterParserResult parserResult)
195+
public static bool TryParse<T>(this CommandContext context, string value, out CommandParameterParserResult parserResult)
196196
{
197+
if (context is null)
198+
throw new ArgumentNullException(nameof(context));
199+
200+
if (string.IsNullOrEmpty(value))
201+
throw new ArgumentNullException(nameof(value));
202+
197203
if (!TryGetParser(typeof(T), out var parser))
198204
{
199-
parserResult = default;
200-
return false;
205+
parserResult = new(false, null, $"Missing parser for type '{typeof(T).Name}'", null, null);
201206
}
207+
else
208+
{
209+
var token = StringToken.Instance.NewToken<StringToken>();
210+
var tokens = ListPool<ICommandToken>.Shared.Rent();
202211

203-
var token = StringToken.Instance.NewToken<StringToken>();
204-
var tokens = ListPool<ICommandToken>.Shared.Rent();
205-
206-
tokens.Add(token);
212+
tokens.Add(token);
207213

208-
token.Value = value;
214+
token.Value = value;
209215

210-
parserResult = parser.Parse(tokens, token, 0, context, NullParameter);
211-
212-
token.ReturnToken();
213-
214-
ListPool<ICommandToken>.Shared.Return(tokens);
215-
return true;
216+
parserResult = parser.Parse(tokens, token, 0, context, NullParameter);
217+
218+
token.ReturnToken();
219+
220+
ListPool<ICommandToken>.Shared.Return(tokens);
221+
}
222+
223+
return parserResult.Success;
216224
}
217225

218226
/// <summary>
@@ -243,68 +251,42 @@ public static CommandParameterParserResult ParseParameters(CommandContext contex
243251
if (index < context.Tokens.Count)
244252
{
245253
var parameterToken = context.Tokens[index];
246-
247-
var parserFound = false;
248-
var parserResult = default(CommandParameterParserResult);
249-
250-
var defaultParserResult = default(CommandParameterParserResult);
254+
var parserResult = new CommandParameterParserResult(false, null, null, null, null!);
251255

252256
foreach (var pair in parameter.Parsers)
253257
{
254258
if (!pair.Key.AcceptsToken(parameterToken))
255259
continue;
256260

257-
var parameterResult = pair.Key.Parse(context.Tokens, parameterToken, index, context, parameter);
258-
259-
if (!parameterResult.Success)
260-
{
261-
if (pair.Key == parameter.Type.Parser)
262-
defaultParserResult = parameterResult;
263-
264-
parserResult = parameterResult;
265-
continue;
266-
}
267-
else
268-
{
269-
parserFound = true;
270-
parserResult = parameterResult;
261+
parserResult = pair.Key.Parse(context.Tokens, parameterToken, index, context, parameter);
271262

263+
if (parserResult.Success)
272264
break;
273-
}
274265
}
275266

276-
if (parserFound)
267+
if (parserResult.Success)
277268
{
278-
if (parserResult.Success)
279-
{
280-
string? argumentError = null;
269+
string? argumentError = null;
281270

282-
foreach (var restriction in parameter.Restrictions)
271+
foreach (var restriction in parameter.Restrictions)
272+
{
273+
if (!restriction.IsValid(parserResult.Value!, context, parameter, out var error))
283274
{
284-
if (!restriction.IsValid(parserResult.Value!, context, parameter, out var error))
285-
{
286-
argumentError = error;
287-
break;
288-
}
275+
argumentError = error;
276+
break;
289277
}
278+
}
290279

291-
if (argumentError != null)
292-
{
293-
parserResults.Add(new(false, null, argumentError, parameter, null!));
280+
if (argumentError != null)
281+
{
282+
parserResults.Add(new(false, null, argumentError, parameter, null!));
294283

295-
index++;
296-
return;
297-
}
284+
index++;
285+
return;
298286
}
299-
300-
parserResults.Add(parserResult);
301-
index++;
302-
}
303-
else
304-
{
305-
parserResults.Add(defaultParserResult);
306-
index++;
307287
}
288+
289+
parserResults.Add(parserResult);
308290
}
309291
else
310292
{

LabExtended/Commands/Runners/AsyncCommandRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void Start()
5151
}).ContinueWithOnMain(x =>
5252
{
5353
if (x.Exception != null)
54-
ApiLog.Error("Command API", x.Exception);
54+
ApiLog.Error("CommandManager", x.Exception);
5555

5656
isFinished = true;
5757
isInProgress = false;

LabExtended/Commands/Runners/ContinuableCommandRunner.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
using LabExtended.Core;
1010
using LabExtended.Utilities.Update;
1111

12-
using MEC;
13-
1412
using NorthwoodLib.Pools;
1513

1614
using UnityEngine;
@@ -73,9 +71,9 @@ public bool ShouldContinue(CommandExecutingEventArgs args, ExPlayer sender)
7371
}
7472
catch (Exception ex)
7573
{
76-
ApiLog.Error("Command API", ex);
74+
ApiLog.Error("CommandManager", ex);
7775

78-
newContext.Response = new(false, false, false, null, newContext.FormatExceptionResponse(ex));
76+
newContext.Response = new(false, false, false, null!, newContext.FormatExceptionResponse(ex));
7977
}
8078

8179
HandleResponse(newContext);
@@ -135,7 +133,9 @@ void FinishCommand()
135133
}
136134
catch (Exception ex)
137135
{
138-
ctx.Response = new(false, false, false, null, ctx.FormatExceptionResponse(ex));
136+
ctx.Response = new(false, false, false, null!, ctx.FormatExceptionResponse(ex));
137+
138+
ApiLog.Error("CommandManager", ex);
139139
}
140140

141141
FinishCommand();
@@ -175,7 +175,7 @@ private void HandleResponse(CommandContext ctx)
175175

176176
private void Start()
177177
{
178-
command.RemainingTime = command.CommandData.TimeOut ?? 0f;
178+
command!.RemainingTime = command.CommandData.TimeOut ?? 0f;
179179

180180
if (command.RemainingTime > 0f && !eventStatus)
181181
{

LabExtended/Commands/Runners/InputCommandRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public bool ShouldContinue(CommandExecutingEventArgs ev, ExPlayer sender)
5656
var args = ListPool<string>.Shared.Rent(ev.Arguments.Array);
5757
var response = inputCommandRunner.context.Response;
5858

59-
inputCommandRunner.context.Response = null;
59+
inputCommandRunner.context.Response = null!;
6060

6161
inputCommandRunner.context.Args = args;
6262
inputCommandRunner.context.Line = line;

LabExtended/Commands/Runners/RegularCommandRunner.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using LabExtended.Commands.Interfaces;
66
using LabExtended.Commands.Utilities;
7+
using LabExtended.Core;
78

89
namespace LabExtended.Commands.Runners;
910

@@ -81,7 +82,9 @@ void FinishCommand()
8182
}
8283
catch (Exception ex)
8384
{
84-
ctx.Response = new(false, false, false, null, ctx.FormatExceptionResponse(ex));
85+
ctx.Response = new(false, false, false, null!, ctx.FormatExceptionResponse(ex));
86+
87+
ApiLog.Error("CommandManager", ex);
8588
}
8689

8790
FinishCommand();

LabExtended/Commands/Utilities/CommandResponseFormatter.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal static bool WriteResponse(this CommandContext ctx, out ContinuableComma
3737
{
3838
if (ctx.Type is CommandType.Console or CommandType.RemoteAdmin)
3939
{
40-
ctx.Sender.SendRemoteAdminMessage(ctx.FormatCommandResponse(), ctx.Response is { IsSuccess: true }, true, ctx.Command.Name);
40+
ctx.Sender.SendRemoteAdminMessage(ctx.FormatCommandResponse(), ctx.Response is { IsSuccess: true }, true, ctx.Command.Name.ToUpperInvariant());
4141
}
4242
else
4343
{
@@ -51,7 +51,7 @@ internal static bool WriteResponse(this CommandContext ctx, out ContinuableComma
5151
}
5252
}
5353

54-
continuableCommand = null;
54+
continuableCommand = null!;
5555
return false;
5656
}
5757

@@ -74,7 +74,7 @@ internal static string FormatCommandResponse(this CommandContext ctx)
7474
if (ctx.Type is CommandType.Client)
7575
{
7676
x.Append("[");
77-
x.Append(ctx.Command.Name);
77+
x.Append(ctx.Command.Name.ToUpperInvariant());
7878
x.Append("] ");
7979
}
8080

@@ -89,7 +89,7 @@ internal static string FormatExceptionResponse(this CommandContext ctx, Exceptio
8989
if (ctx.Type is CommandType.Client)
9090
{
9191
x.Append("[");
92-
x.Append(ctx.Command.Name);
92+
x.Append(ctx.Command.Name.ToUpperInvariant());
9393
x.Append("] ");
9494
}
9595

@@ -105,7 +105,7 @@ internal static string FormatMissingPermissionsFailure(string requiredPermission
105105
if (type is CommandType.Console)
106106
{
107107
x.Append("[");
108-
x.Append(commandName);
108+
x.Append(commandName.ToUpperInvariant());
109109
x.Append("] ");
110110
}
111111

@@ -131,7 +131,7 @@ internal static string FormatMissingArgumentsFailure(this CommandContext ctx)
131131
if (ctx.Type is CommandType.Client)
132132
{
133133
x.Append("[");
134-
x.Append(ctx.Command.Name);
134+
x.Append(ctx.Command.Name.ToUpperInvariant());
135135
x.Append("] ");
136136
}
137137

@@ -174,7 +174,7 @@ internal static string FormatInvalidArgumentsFailure(this CommandContext ctx, Li
174174
if (ctx.Type is CommandType.Client)
175175
{
176176
x.Append("[");
177-
x.Append(ctx.Command.Name);
177+
x.Append(ctx.Command.Name.ToUpperInvariant());
178178
x.Append("] ");
179179
}
180180

@@ -225,7 +225,7 @@ internal static string FormatTokenParserFailure(this CommandContext ctx)
225225
if (ctx.Type is CommandType.Client)
226226
{
227227
x.Append("[");
228-
x.Append(ctx.Command.Name);
228+
x.Append(ctx.Command.Name.ToUpperInvariant());
229229
x.Append("] ");
230230
}
231231

0 commit comments

Comments
 (0)