Update RemoveExtraComponents command to not use IEntityQuery.

This commit is contained in:
Vera Aguilera Puerto
2021-05-10 20:36:16 +02:00
parent 2b10396a83
commit 3e0eb22921

View File

@@ -20,29 +20,21 @@ namespace Content.Server.Commands
var entityManager = IoCManager.Resolve<IEntityManager>(); var entityManager = IoCManager.Resolve<IEntityManager>();
var prototypeManager = IoCManager.Resolve<IPrototypeManager>(); var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
IEntityQuery query; EntityPrototype? prototype = null;
var checkPrototype = !string.IsNullOrEmpty(id);
if (id == null) if (checkPrototype && !prototypeManager.TryIndex(id!, out prototype))
{ {
query = new AllEntityQuery(); shell.WriteError($"Can't find entity prototype with id \"{id}\"!");
}
else
{
if (!prototypeManager.TryIndex(id, out EntityPrototype? prototype))
{
shell.WriteLine($"No entity prototype found with id {id}.");
return; return;
} }
query = new PredicateEntityQuery(e => e.Prototype == prototype);
}
var entities = 0; var entities = 0;
var components = 0; var components = 0;
foreach (var entity in entityManager.GetEntities(query)) foreach (var entity in entityManager.GetEntities())
{ {
if (entity.Prototype == null) if (checkPrototype && entity.Prototype != prototype || entity.Prototype == null)
{ {
continue; continue;
} }
@@ -51,20 +43,18 @@ namespace Content.Server.Commands
foreach (var component in entity.GetAllComponents()) foreach (var component in entity.GetAllComponents())
{ {
if (!entity.Prototype.Components.ContainsKey(component.Name)) if (entity.Prototype.Components.ContainsKey(component.Name))
{ continue;
entityManager.ComponentManager.RemoveComponent(entity.Uid, component); entityManager.ComponentManager.RemoveComponent(entity.Uid, component);
components++; components++;
modified = true; modified = true;
} }
}
if (modified) if (modified)
{
entities++; entities++;
} }
}
shell.WriteLine($"Removed {components} components from {entities} entities{(id == null ? "." : $" with id {id}")}"); shell.WriteLine($"Removed {components} components from {entities} entities{(id == null ? "." : $" with id {id}")}");
} }