Files
tbd-station-14/Content.Server/Commands/RemoveExtraComponents.cs
Acruid 8b5d66050a Console Unify API Changes (#3059)
* Remove unused IChatCommand.

* Lots of refactoring into a shared context.

* Removed ICommonSession from server concmd Execute.

* Added argStr parameter to concmd execute.

* The execute function of client concmds now returns void, use the new shell.RemoteExecuteCommand function to forward commands.

* Finally move shells and commands into shared.

* Console commands can now be registered directly without a class in a shared context.

* Engine API Changes.

* Repair rebase damage.

* Update Submodule.
2021-02-01 16:49:43 -08:00

75 lines
2.5 KiB
C#

#nullable enable
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Server.Interfaces.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Server.Commands
{
[AdminCommand(AdminFlags.Mapping)]
public class RemoveExtraComponents : IConsoleCommand
{
public string Command => "removeextracomponents";
public string Description => "Removes all components from all entities of the specified id if that component is not in its prototype.\nIf no id is specified, it matches all entities.";
public string Help => $"{Command} <entityId> / {Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var id = args.Length == 0 ? null : string.Join(" ", args);
var entityManager = IoCManager.Resolve<IEntityManager>();
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
IEntityQuery query;
if (id == null)
{
query = new AllEntityQuery();
}
else
{
if (!prototypeManager.TryIndex(id, out EntityPrototype prototype))
{
shell.WriteLine($"No entity prototype found with id {id}.");
return;
}
query = new PredicateEntityQuery(e => e.Prototype == prototype);
}
var entities = 0;
var components = 0;
foreach (var entity in entityManager.GetEntities(query))
{
if (entity.Prototype == null)
{
continue;
}
var modified = false;
foreach (var component in entity.GetAllComponents())
{
if (!entity.Prototype.Components.ContainsKey(component.Name))
{
entityManager.ComponentManager.RemoveComponent(entity.Uid, component);
components++;
modified = true;
}
}
if (modified)
{
entities++;
}
}
shell.WriteLine($"Removed {components} components from {entities} entities{(id == null ? "." : $" with id {id}")}");
}
}
}