2020-11-30 14:00:09 +01:00
#nullable enable
using Content.Server.Administration ;
using Content.Shared.Administration ;
2021-02-01 16:49:43 -08:00
using Robust.Shared.Console ;
2020-11-30 14:00:09 +01:00
using Robust.Shared.GameObjects ;
using Robust.Shared.IoC ;
using Robust.Shared.Prototypes ;
namespace Content.Server.Commands
{
[AdminCommand(AdminFlags.Mapping)]
2021-02-01 16:49:43 -08:00
public class RemoveExtraComponents : IConsoleCommand
2020-11-30 14:00:09 +01:00
{
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}" ;
2021-02-01 16:49:43 -08:00
public void Execute ( IConsoleShell shell , string argStr , string [ ] args )
2020-11-30 14:00:09 +01:00
{
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 ) )
{
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"No entity prototype found with id {id}." ) ;
2020-11-30 14:00:09 +01:00
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 + + ;
}
}
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"Removed {components} components from {entities} entities{(id == null ? " . " : $" with id { id } ")}" ) ;
2020-11-30 14:00:09 +01:00
}
}
}