1
0

RemoveExtraComponents.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Content.Shared.Administration;
  2. using Robust.Shared.Console;
  3. using Robust.Shared.Prototypes;
  4. namespace Content.Server.Administration.Commands
  5. {
  6. [AdminCommand(AdminFlags.Mapping)]
  7. public sealed class RemoveExtraComponents : IConsoleCommand
  8. {
  9. public string Command => "removeextracomponents";
  10. 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.";
  11. public string Help => $"{Command} <entityId> / {Command}";
  12. public void Execute(IConsoleShell shell, string argStr, string[] args)
  13. {
  14. var id = args.Length == 0 ? null : string.Join(" ", args);
  15. var entityManager = IoCManager.Resolve<IEntityManager>();
  16. var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
  17. var fac = IoCManager.Resolve<IComponentFactory>();
  18. EntityPrototype? prototype = null;
  19. var checkPrototype = !string.IsNullOrEmpty(id);
  20. if (checkPrototype && !prototypeManager.TryIndex(id!, out prototype))
  21. {
  22. shell.WriteError($"Can't find entity prototype with id \"{id}\"!");
  23. return;
  24. }
  25. var entities = 0;
  26. var components = 0;
  27. foreach (var entity in entityManager.GetEntities())
  28. {
  29. var metaData = entityManager.GetComponent<MetaDataComponent>(entity);
  30. if (checkPrototype && metaData.EntityPrototype != prototype || metaData.EntityPrototype == null)
  31. {
  32. continue;
  33. }
  34. var modified = false;
  35. foreach (var component in entityManager.GetComponents(entity))
  36. {
  37. if (metaData.EntityPrototype.Components.ContainsKey(fac.GetComponentName(component.GetType())))
  38. continue;
  39. entityManager.RemoveComponent(entity, component);
  40. components++;
  41. modified = true;
  42. }
  43. if (modified)
  44. entities++;
  45. }
  46. shell.WriteLine($"Removed {components} components from {entities} entities{(id == null ? "." : $" with id {id}")}");
  47. }
  48. }
  49. }