Fix duplicate command error (#4507)

This commit is contained in:
Leon Friedrich
2023-10-22 08:18:50 +11:00
committed by GitHub
parent ea152366e3
commit 7feede0d95
3 changed files with 33 additions and 13 deletions

View File

@@ -164,26 +164,39 @@ namespace Robust.Server.Console
var counter = 0;
var toolshedCommands = _toolshed.DefaultEnvironment.AllCommands().ToArray();
message.Commands = new MsgConCmdReg.Command[AvailableCommands.Count + toolshedCommands.Length];
message.Commands = new List<MsgConCmdReg.Command>(AvailableCommands.Count + toolshedCommands.Length);
var commands = new HashSet<string>();
foreach (var command in AvailableCommands.Values)
{
message.Commands[counter++] = new MsgConCmdReg.Command
if (!commands.Add(command.Command))
{
Sawmill.Error($"Duplicate command: {command.Command}");
continue;
}
message.Commands.Add(new MsgConCmdReg.Command
{
Name = command.Command,
Description = command.Description,
Help = command.Help
};
});
}
foreach (var spec in toolshedCommands)
{
message.Commands[counter++] = new MsgConCmdReg.Command
var name = spec.FullName();
if (!commands.Add(name))
{
Name = spec.FullName(),
Sawmill.Warning($"Duplicate toolshed command: {name}");
continue;
}
message.Commands.Add(new MsgConCmdReg.Command
{
Name = name,
Description = spec.Cmd.Description(spec.SubCommand),
Help = spec.Cmd.GetHelp(spec.SubCommand)
};
});
}
NetManager.ServerSendMessage(message, senderConnection);

View File

@@ -32,6 +32,9 @@ namespace Robust.Shared.Console
private readonly CommandBuffer _commandBuffer = new CommandBuffer();
// TODO add Initialize() method.
protected ISawmill Sawmill => LogManager.GetSawmill(SawmillName);
/// <inheritdoc />
public bool IsServer { get; }

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Lidgren.Network;
using Robust.Shared.Serialization;
@@ -10,7 +11,7 @@ namespace Robust.Shared.Network.Messages
{
public override MsgGroups MsgGroup => MsgGroups.String;
public Command[] Commands { get; set; }
public List<Command> Commands { get; set; }
public sealed class Command
{
@@ -22,24 +23,27 @@ namespace Robust.Shared.Network.Messages
public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer serializer)
{
var cmdCount = buffer.ReadUInt16();
Commands = new Command[cmdCount];
Commands = new (cmdCount);
for (var i = 0; i < cmdCount; i++)
{
Commands[i] = new Command()
Commands.Add(new Command()
{
Name = buffer.ReadString(),
Description = buffer.ReadString(),
Help = buffer.ReadString()
};
});
}
}
public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer serializer)
{
if(Commands == null) // client leaves comands as null to request from server
Commands = new Command[0];
if (Commands == null) // client leaves comands as null to request from server
{
buffer.Write((UInt16)0);
return;
}
buffer.Write((UInt16)Commands.Length);
buffer.Write((UInt16)Commands.Count);
foreach (var command in Commands)
{
buffer.Write(command.Name);