From 0df159e73ba9511167781281b19ff5cd009fff5c Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 20 Jan 2019 12:22:30 +0000 Subject: [PATCH] Cache world INotifySelection traits in Selection. --- OpenRA.Game/Selection.cs | 14 +++++++++++--- OpenRA.Game/World.cs | 4 +++- OpenRA.Mods.Cnc/Traits/ConyardChronoReturn.cs | 2 +- OpenRA.Mods.Common/Activities/Transform.cs | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/OpenRA.Game/Selection.cs b/OpenRA.Game/Selection.cs index e2152c319f..0122dce5e4 100644 --- a/OpenRA.Game/Selection.cs +++ b/OpenRA.Game/Selection.cs @@ -21,7 +21,14 @@ namespace OpenRA { public int Hash { get; private set; } public IEnumerable Actors { get { return actors; } } + readonly HashSet actors = new HashSet(); + readonly INotifySelection[] worldNotifySelection; + + internal Selection(IEnumerable worldNotifySelection) + { + this.worldNotifySelection = worldNotifySelection.ToArray(); + } void UpdateHash() { @@ -31,14 +38,15 @@ namespace OpenRA Hash += 1; } - public void Add(World w, Actor a) + public void Add(Actor a) { actors.Add(a); UpdateHash(); foreach (var sel in a.TraitsImplementing()) sel.Selected(a); - foreach (var ns in w.WorldActor.TraitsImplementing()) + + foreach (var ns in worldNotifySelection) ns.SelectionChanged(); } @@ -77,7 +85,7 @@ namespace OpenRA foreach (var sel in a.TraitsImplementing()) sel.Selected(a); - foreach (var ns in world.WorldActor.TraitsImplementing()) + foreach (var ns in worldNotifySelection) ns.SelectionChanged(); if (world.IsGameOver) diff --git a/OpenRA.Game/World.cs b/OpenRA.Game/World.cs index 83fa5b13ee..9ccf7f6d26 100644 --- a/OpenRA.Game/World.cs +++ b/OpenRA.Game/World.cs @@ -142,7 +142,7 @@ namespace OpenRA } } - public readonly Selection Selection = new Selection(); + public readonly Selection Selection; public void CancelInputMode() { OrderGenerator = new UnitOrderGenerator(); } @@ -179,6 +179,8 @@ namespace OpenRA ActorMap = WorldActor.Trait(); ScreenMap = WorldActor.Trait(); + Selection = new Selection(WorldActor.TraitsImplementing()); + // Add players foreach (var cmp in WorldActor.TraitsImplementing()) cmp.CreatePlayers(this); diff --git a/OpenRA.Mods.Cnc/Traits/ConyardChronoReturn.cs b/OpenRA.Mods.Cnc/Traits/ConyardChronoReturn.cs index ef60f07eac..58604a311c 100644 --- a/OpenRA.Mods.Cnc/Traits/ConyardChronoReturn.cs +++ b/OpenRA.Mods.Cnc/Traits/ConyardChronoReturn.cs @@ -193,7 +193,7 @@ namespace OpenRA.Mods.Cnc.Traits nt.AfterTransform(a); if (selected) - self.World.Selection.Add(self.World, a); + self.World.Selection.Add(a); if (controlgroup.HasValue) self.World.Selection.AddToControlGroup(a, controlgroup.Value); diff --git a/OpenRA.Mods.Common/Activities/Transform.cs b/OpenRA.Mods.Common/Activities/Transform.cs index 03ab901344..42b47d286d 100644 --- a/OpenRA.Mods.Common/Activities/Transform.cs +++ b/OpenRA.Mods.Common/Activities/Transform.cs @@ -135,7 +135,7 @@ namespace OpenRA.Mods.Common.Activities self.ReplacedByActor = a; if (selected) - w.Selection.Add(w, a); + w.Selection.Add(a); if (controlgroup.HasValue) w.Selection.AddToControlGroup(a, controlgroup.Value);