Files
OpenRA/OpenRA.Mods.Common/ActorIndex.cs
RoosterDragon c18d10c846 Fix ActorIndex when dealing with multiple trait instances.
The intended check was "has any trait", but TraitOrDefault throws if there is more than one. Adjust this check so it doesn't throw in the face of multiple trait instances.

Resolves a regression introduced in 63de527d9e0a90e2f055dc302dacca855092ebfa.
2024-03-16 11:21:23 +02:00

153 lines
4.3 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.Mods.Common
{
/// <summary>
/// Maintains an index of actors in the world.
/// </summary>
public abstract class ActorIndex : IDisposable
{
readonly World world;
readonly HashSet<Actor> actors = new();
public IReadOnlyCollection<Actor> Actors => actors;
ActorIndex(World world, IEnumerable<Actor> initialActorsToIndex)
{
this.world = world;
world.ActorAdded += AddActor;
world.ActorRemoved += RemoveActor;
actors.UnionWith(initialActorsToIndex);
}
protected abstract bool ShouldIndexActor(Actor actor);
void AddActor(Actor actor)
{
if (ShouldIndexActor(actor))
actors.Add(actor);
}
void RemoveActor(Actor actor)
{
actors.Remove(actor);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
world.ActorAdded -= AddActor;
world.ActorRemoved -= RemoveActor;
}
}
// No OwnerAndTrait class is provided. As the world can provide actors by trait anyway,
// an additional filter on owner isn't sufficiently selective to justify the overhead of manging the index.
// Whereas a filter with actor names is much more selective, so OwnerAndNames is worthwhile.
/// <summary>
/// Maintains an index of actors in the world that
/// are owned by a given <see cref="Player"/>
/// and have one of the given <see cref="ActorInfo.Name"/>.
/// </summary>
public sealed class OwnerAndNames : ActorIndex
{
readonly HashSet<string> names;
readonly Player owner;
public OwnerAndNames(World world, IReadOnlyCollection<string> names, Player owner)
: base(world, ActorsToIndex(world, names.ToHashSet(), owner))
{
this.names = names.ToHashSet();
this.owner = owner;
}
static IEnumerable<Actor> ActorsToIndex(World world, HashSet<string> names, Player owner)
{
return world.Actors.Where(a => a.Owner == owner && names.Contains(a.Info.Name));
}
protected override bool ShouldIndexActor(Actor actor)
{
return actor.Owner == owner && names.Contains(actor.Info.Name);
}
}
/// <summary>
/// Maintains an index of actors in the world that
/// have one of the given <see cref="ActorInfo.Name"/>
/// and have the trait of type <typeparamref name="T"/>.
/// </summary>
public sealed class NamesAndTrait<T> : ActorIndex
{
readonly HashSet<string> names;
public NamesAndTrait(World world, IReadOnlyCollection<string> names)
: base(world, ActorsToIndex(world, names.ToHashSet()))
{
this.names = names.ToHashSet();
}
static IEnumerable<Actor> ActorsToIndex(World world, HashSet<string> names)
{
return world.ActorsHavingTrait<T>().Where(a => names.Contains(a.Info.Name));
}
protected override bool ShouldIndexActor(Actor actor)
{
return names.Contains(actor.Info.Name) && actor.TraitsImplementing<T>().Any();
}
}
/// <summary>
/// Maintains an index of actors in the world that
/// are owned by a given <see cref="Player"/>,
/// have one of the given <see cref="ActorInfo.Name"/>
/// and have the trait of type <typeparamref name="T"/>.
/// </summary>
public sealed class OwnerAndNamesAndTrait<T> : ActorIndex
{
readonly HashSet<string> names;
readonly Player owner;
public OwnerAndNamesAndTrait(World world, IReadOnlyCollection<string> names, Player owner)
: base(world, ActorsToIndex(world, names.ToHashSet(), owner))
{
this.names = names.ToHashSet();
this.owner = owner;
}
static IEnumerable<Actor> ActorsToIndex(World world, HashSet<string> names, Player owner)
{
return world.ActorsHavingTrait<T>().Where(a => a.Owner == owner && names.Contains(a.Info.Name));
}
protected override bool ShouldIndexActor(Actor actor)
{
return actor.Owner == owner && names.Contains(actor.Info.Name) && actor.TraitsImplementing<T>().Any();
}
}
}
}