2021-12-05 18:09:01 +01:00
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 ;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Administration.Commands
2020-11-30 14:00:09 +01:00
{
[AdminCommand(AdminFlags.Mapping)]
2022-02-16 00:23:23 -07:00
public sealed 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 > ( ) ;
2021-05-10 20:36:16 +02:00
EntityPrototype ? prototype = null ;
var checkPrototype = ! string . IsNullOrEmpty ( id ) ;
2020-11-30 14:00:09 +01:00
2021-05-10 20:36:16 +02:00
if ( checkPrototype & & ! prototypeManager . TryIndex ( id ! , out prototype ) )
2020-11-30 14:00:09 +01:00
{
2021-05-10 20:36:16 +02:00
shell . WriteError ( $"Can't find entity prototype with id \" { id } \ "!" ) ;
return ;
2020-11-30 14:00:09 +01:00
}
var entities = 0 ;
var components = 0 ;
2021-05-10 20:36:16 +02:00
foreach ( var entity in entityManager . GetEntities ( ) )
2020-11-30 14:00:09 +01:00
{
2021-12-05 18:09:01 +01:00
var metaData = entityManager . GetComponent < MetaDataComponent > ( entity ) ;
if ( checkPrototype & & metaData . EntityPrototype ! = prototype | | metaData . EntityPrototype = = null )
2020-11-30 14:00:09 +01:00
{
continue ;
}
var modified = false ;
2021-12-05 18:09:01 +01:00
foreach ( var component in entityManager . GetComponents ( entity ) )
2020-11-30 14:00:09 +01:00
{
2021-12-05 18:09:01 +01:00
if ( metaData . EntityPrototype . Components . ContainsKey ( component . Name ) )
2021-05-10 20:36:16 +02:00
continue ;
2021-12-03 15:53:09 +01:00
entityManager . RemoveComponent ( entity , component ) ;
2021-05-10 20:36:16 +02:00
components + + ;
2020-11-30 14:00:09 +01:00
2021-05-10 20:36:16 +02:00
modified = true ;
2020-11-30 14:00:09 +01:00
}
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
}
}
}