Make IEntity.GetAllComponents no longer return duplicates.

Remove the Instances method that did that instead.
This commit is contained in:
Pieter-Jan Briers
2019-08-11 01:22:59 +02:00
parent 768fed2421
commit 896758e176
9 changed files with 32 additions and 53 deletions

View File

@@ -223,7 +223,7 @@ namespace Robust.Client.Console.Commands
console.AddLine($"{entity.Uid}: {entity.Prototype.ID}/{entity.Name}");
console.AddLine($"init/del/lmt: {entity.Initialized}/{entity.Deleted}/{entity.LastModifiedTick}");
foreach (var component in entity.GetAllComponents().Distinct())
foreach (var component in entity.GetAllComponents())
{
console.AddLine(component.ToString());
if (component is IComponentDebug debug)

View File

@@ -120,7 +120,7 @@ namespace Robust.Client.ViewVariables.Instances
_tabs.SetTabTitle(TabClientComponents, "Client Components");
// See engine#636 for why the Distinct() call.
var componentList = _entity.GetAllComponents().Distinct().OrderBy(c => c.GetType().ToString());
var componentList = _entity.GetAllComponents().OrderBy(c => c.GetType().ToString());
foreach (var component in componentList)
{
var button = new Button {Text = component.GetType().ToString(), TextAlign = Button.AlignMode.Left};

View File

@@ -482,7 +482,7 @@ namespace Robust.Server.Maps
var components = new YamlSequenceNode();
// See engine#636 for why the Distinct() call.
foreach (var component in entity.GetAllComponents().Distinct())
foreach (var component in entity.GetAllComponents())
{
var compMapping = new YamlMappingNode();
CurrentWritingComponent = component.Name;

View File

@@ -20,7 +20,7 @@ namespace Robust.Server.ViewVariables.Traits
{
var list = new List<ViewVariablesBlobEntityComponents.Entry>();
// See engine#636 for why the Distinct() call.
foreach (var component in _entity.GetAllComponents().Distinct())
foreach (var component in _entity.GetAllComponents())
{
var type = component.GetType();
list.Add(new ViewVariablesBlobEntityComponents.Entry

View File

@@ -339,22 +339,12 @@ namespace Robust.Shared.GameObjects
/// <inheritdoc />
public IEnumerable<IComponent> GetComponents(EntityUid uid)
{
foreach (var kvTypeDict in _dictComponents.Values)
{
if (kvTypeDict.TryGetValue(uid, out var comp))
yield return comp;
}
}
/// <inheritdoc />
public IEnumerable<IComponent> GetComponentInstances(EntityUid uid)
{
var results = new List<IComponent>();
foreach (var kvTypeDict in _dictComponents.Values)
{
if (kvTypeDict.TryGetValue(uid, out var comp) && !results.Contains(comp))
if (kvTypeDict.TryGetValue(uid, out var comp) && !results.Contains(comp) && !comp.Deleted)
{
results.Add(comp);
}
@@ -366,7 +356,17 @@ namespace Robust.Shared.GameObjects
/// <inheritdoc />
public IEnumerable<T> GetComponents<T>(EntityUid uid)
{
return GetComponents(uid).OfType<T>();
var results = new List<T>();
foreach (var kvTypeDict in _dictComponents.Values)
{
if (kvTypeDict.TryGetValue(uid, out var comp) && comp is T t && !results.Contains(t) && !comp.Deleted)
{
results.Add(t);
}
}
return results;
}
/// <inheritdoc />

View File

@@ -122,10 +122,10 @@ namespace Robust.Shared.GameObjects
public void InitializeComponents()
{
// Initialize() can modify the collection of components.
var components = EntityManager.ComponentManager.GetComponentInstances(Uid).ToList();
for (int i = 0; i < components.Count; i++)
var components = EntityManager.ComponentManager.GetComponents(Uid);
foreach (var t in components)
{
var comp = (Component)components[i];
var comp = (Component) t;
if (comp != null && !comp.Initialized)
comp.Initialize();
}
@@ -138,10 +138,10 @@ namespace Robust.Shared.GameObjects
{
// Startup() can modify _components
// TODO: This code can only handle additions to the list. Is there a better way?
var components = EntityManager.ComponentManager.GetComponentInstances(Uid).ToList();
for (int i = 0; i < components.Count; i++)
var components = EntityManager.ComponentManager.GetComponents(Uid);
foreach (var t in components)
{
var comp = (Component)components[i];
var comp = (Component)t;
if (comp != null && comp.Initialized && !comp.Running && !comp.Deleted)
comp.Startup();
}
@@ -162,7 +162,7 @@ namespace Robust.Shared.GameObjects
/// <inheritdoc />
public void SendMessage(IComponent owner, ComponentMessage message)
{
var components = EntityManager.ComponentManager.GetComponentInstances(Uid);
var components = EntityManager.ComponentManager.GetComponents(Uid);
foreach (var component in components)
{
if (owner != component)
@@ -205,7 +205,7 @@ namespace Robust.Shared.GameObjects
}
else
{
foreach (var component in EntityManager.ComponentManager.GetComponentInstances(Uid))
foreach (var component in EntityManager.ComponentManager.GetComponents(Uid))
{
component.HandleMessage(compMsg, compChannel);
}
@@ -352,19 +352,13 @@ namespace Robust.Shared.GameObjects
/// <inheritdoc />
public IEnumerable<IComponent> GetAllComponents()
{
return EntityManager.ComponentManager.GetComponents(Uid).Where(comp => !comp.Deleted);
}
/// <inheritdoc />
public IEnumerable<IComponent> GetComponentInstances()
{
return EntityManager.ComponentManager.GetComponentInstances(Uid).Where(comp => !comp.Deleted);
return EntityManager.ComponentManager.GetComponents(Uid);
}
/// <inheritdoc />
public IEnumerable<T> GetAllComponents<T>()
{
return GetAllComponents().OfType<T>();
return EntityManager.ComponentManager.GetComponents<T>(Uid);
}
#endregion Components
@@ -381,7 +375,7 @@ namespace Robust.Shared.GameObjects
internal void HandleEntityState(EntityState curState, EntityState nextState)
{
_compStateWork.Clear();
if(curState?.ComponentChanges != null)
{
foreach (var compChange in curState.ComponentChanges)
@@ -448,7 +442,7 @@ namespace Robust.Shared.GameObjects
public EntityState GetEntityState(GameTick fromTick)
{
var compStates = GetComponentStates(fromTick);
var addedComponents = EntityManager.ComponentManager.GetNetComponents(Uid)
.Where(c => c.CreationTick >= fromTick && !c.Deleted)
.Select(c => ComponentChanged.Added(c.NetID.Value, c.Name));
@@ -472,7 +466,7 @@ namespace Robust.Shared.GameObjects
/// <returns></returns>
private List<ComponentState> GetComponentStates(GameTick fromTick)
{
return GetComponentInstances()
return GetAllComponents()
.Where(c => c.NetID != null && c.NetSyncEnabled && c.LastModifiedTick >= fromTick)
.Select(component => component.GetComponentState())
.ToList();

View File

@@ -173,13 +173,6 @@ namespace Robust.Shared.Interfaces.GameObjects
/// <returns>All component types on the Entity.</returns>
IEnumerable<IComponent> GetComponents(EntityUid uid);
/// <summary>
/// Returns all component instances on an entity. This does not use the
/// component reference system. Use this for serialization.
/// </summary>
/// <returns>An enumerable of component instances on the entity.</returns>
IEnumerable<IComponent> GetComponentInstances(EntityUid uid);
/// <summary>
/// Returns ALL component type instances that are assignable to the specified type.
/// A single component instance can have multiple component type instances.

View File

@@ -164,20 +164,12 @@ namespace Robust.Shared.Interfaces.GameObjects
void Delete();
/// <summary>
/// Returns all components on the entity. This uses the reference system,
/// so a component instance can be duplicated over multiple interfaces.
/// Returns all components on the entity.
/// </summary>
/// <returns>An enumerable of components on the entity.</returns>
IEnumerable<IComponent> GetAllComponents();
/// <summary>
/// Returns all component instances on an entity. This does not use the
/// component reference system. Use this for serialization.
/// </summary>
/// <returns>An enumerable of component instances on the entity.</returns>
IEnumerable<IComponent> GetComponentInstances();
/// <summary>
/// Returns all components that are assignable to <typeparamref name="T"/>.
/// This does not go by component references.
/// </summary>

View File

@@ -226,7 +226,7 @@ namespace Robust.UnitTesting.Shared.GameObjects
manager.AddComponent(entity, component);
// Act
var result = manager.GetComponentInstances(entity.Uid);
var result = manager.GetComponents(entity.Uid);
// Assert
var list = result.ToList();
@@ -235,7 +235,7 @@ namespace Robust.UnitTesting.Shared.GameObjects
}
#endregion
private static IComponentManager ManagerFactory(out IEntityManager entityManager)
{
var dependencies = new DependencyCollection();