2020-11-29 03:31:56 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-06-27 05:40:03 +02:00
|
|
|
using System.Linq;
|
2020-11-29 03:31:56 +01:00
|
|
|
using Content.Shared.Administration;
|
2021-02-01 16:49:43 -08:00
|
|
|
using Robust.Shared.Console;
|
2020-11-29 03:31:56 +01:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Administration.Commands
|
2020-11-29 03:31:56 +01:00
|
|
|
{
|
|
|
|
|
[AdminCommand(AdminFlags.Mapping)]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class FindEntitiesWithComponents : IConsoleCommand
|
2020-11-29 03:31:56 +01:00
|
|
|
{
|
|
|
|
|
public string Command => "findentitieswithcomponents";
|
|
|
|
|
public string Description => "Finds entities with all of the specified components.";
|
|
|
|
|
public string Help => $"{Command} <componentName1> <componentName2>...";
|
|
|
|
|
|
2021-02-01 16:49:43 -08:00
|
|
|
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
2020-11-29 03:31:56 +01:00
|
|
|
{
|
|
|
|
|
if (args.Length == 0)
|
|
|
|
|
{
|
2021-02-01 16:49:43 -08:00
|
|
|
shell.WriteLine($"Invalid amount of arguments: {args.Length}.\n{Help}");
|
2020-11-29 03:31:56 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var components = new List<Type>();
|
|
|
|
|
var componentFactory = IoCManager.Resolve<IComponentFactory>();
|
|
|
|
|
var invalidArgs = new List<string>();
|
|
|
|
|
|
|
|
|
|
foreach (var arg in args)
|
|
|
|
|
{
|
|
|
|
|
if (!componentFactory.TryGetRegistration(arg, out var registration))
|
|
|
|
|
{
|
|
|
|
|
invalidArgs.Add(arg);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
components.Add(registration.Type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (invalidArgs.Count > 0)
|
|
|
|
|
{
|
2021-02-01 16:49:43 -08:00
|
|
|
shell.WriteLine($"No component found for component names: {string.Join(", ", invalidArgs)}");
|
2020-11-29 03:31:56 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var entityManager = IoCManager.Resolve<IEntityManager>();
|
|
|
|
|
var entityIds = new HashSet<string>();
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
var entitiesWithComponents = components.Select(c => entityManager.GetAllComponents(c).Select(x => x.Owner)).ToArray();
|
|
|
|
|
var entitiesWithAllComponents = entitiesWithComponents.Skip(1).Aggregate(new HashSet<EntityUid>(entitiesWithComponents.First()), (h, e) => { h.IntersectWith(e); return h; });
|
2021-06-27 05:40:03 +02:00
|
|
|
|
|
|
|
|
foreach (var entity in entitiesWithAllComponents)
|
2020-11-29 03:31:56 +01:00
|
|
|
{
|
2021-12-05 18:09:01 +01:00
|
|
|
if (entityManager.GetComponent<MetaDataComponent>(entity).EntityPrototype is not { } prototypeId)
|
2020-11-29 03:31:56 +01:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
entityIds.Add(prototypeId.ID);
|
2020-11-29 03:31:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entityIds.Count == 0)
|
|
|
|
|
{
|
2021-02-01 16:49:43 -08:00
|
|
|
shell.WriteLine($"No entities found with components {string.Join(", ", args)}.");
|
2020-11-29 03:31:56 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 16:49:43 -08:00
|
|
|
shell.WriteLine($"{entityIds.Count} entities found:\n{string.Join("\n", entityIds)}");
|
2020-11-29 03:31:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|