Merge pull request #7231 from RoosterDragon/linq-checks
Checked LINQ queries and collections for inefficiencies.
This commit is contained in:
@@ -21,8 +21,8 @@ namespace OpenRA.Mods.Common.Lint
|
||||
foreach (var actorInfo in map.Rules.Actors.Where(a => !a.Key.StartsWith("^")))
|
||||
try
|
||||
{
|
||||
var traits = actorInfo.Value.TraitsInConstructOrder().ToArray();
|
||||
if (traits.Length == 0)
|
||||
var hasTraits = actorInfo.Value.TraitsInConstructOrder().Any();
|
||||
if (!hasTraits)
|
||||
emitWarning("Actor {0} has no traits. Is this intended?".F(actorInfo.Key));
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -47,8 +47,8 @@ namespace OpenRA.Mods.Common.Orders
|
||||
if (mi.Button == MouseButton.Right)
|
||||
world.CancelInputMode();
|
||||
|
||||
var ret = InnerOrder(world, xy, mi).ToList();
|
||||
if (ret.Count > 0)
|
||||
var ret = InnerOrder(world, xy, mi).ToArray();
|
||||
if (ret.Length > 0)
|
||||
world.CancelInputMode();
|
||||
|
||||
return ret;
|
||||
|
||||
@@ -203,14 +203,14 @@ namespace OpenRA.Mods.Common.SpriteLoaders
|
||||
|
||||
public static void Write(Stream s, Size size, IEnumerable<byte[]> frames)
|
||||
{
|
||||
var compressedFrames = frames.Select(f => Format80.Encode(f)).ToArray();
|
||||
var compressedFrames = frames.Select(f => Format80.Encode(f)).ToList();
|
||||
|
||||
// note: end-of-file and all-zeroes headers
|
||||
var dataOffset = 14 + (compressedFrames.Length + 2) * 8;
|
||||
var dataOffset = 14 + (compressedFrames.Count + 2) * 8;
|
||||
|
||||
using (var bw = new BinaryWriter(s))
|
||||
{
|
||||
bw.Write((ushort)compressedFrames.Length);
|
||||
bw.Write((ushort)compressedFrames.Count);
|
||||
bw.Write((ushort)0);
|
||||
bw.Write((ushort)0);
|
||||
bw.Write((ushort)size.Width);
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public readonly CargoInfo Info;
|
||||
readonly Actor self;
|
||||
readonly List<Actor> cargo = new List<Actor>();
|
||||
readonly Stack<Actor> cargo = new Stack<Actor>();
|
||||
readonly HashSet<Actor> reserves = new HashSet<Actor>();
|
||||
readonly Lazy<IFacing> facing;
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
if (init.Contains<RuntimeCargoInit>())
|
||||
{
|
||||
cargo = init.Get<RuntimeCargoInit, Actor[]>().ToList();
|
||||
cargo = new Stack<Actor>(init.Get<RuntimeCargoInit, Actor[]>());
|
||||
totalWeight = cargo.Sum(c => GetWeight(c));
|
||||
}
|
||||
else if (init.Contains<CargoInit>())
|
||||
@@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var unit = self.World.CreateActor(false, u.ToLowerInvariant(),
|
||||
new TypeDictionary { new OwnerInit(self.Owner) });
|
||||
|
||||
cargo.Add(unit);
|
||||
cargo.Push(unit);
|
||||
}
|
||||
|
||||
totalWeight = cargo.Sum(c => GetWeight(c));
|
||||
@@ -81,7 +81,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var unit = self.World.CreateActor(false, u.ToLowerInvariant(),
|
||||
new TypeDictionary { new OwnerInit(self.Owner) });
|
||||
|
||||
cargo.Add(unit);
|
||||
cargo.Push(unit);
|
||||
}
|
||||
|
||||
totalWeight = cargo.Sum(c => GetWeight(c));
|
||||
@@ -185,13 +185,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public bool HasSpace(int weight) { return totalWeight + reservedWeight + weight <= Info.MaxWeight; }
|
||||
public bool IsEmpty(Actor self) { return cargo.Count == 0; }
|
||||
|
||||
public Actor Peek(Actor self) { return cargo[0]; }
|
||||
public Actor Peek(Actor self) { return cargo.Peek(); }
|
||||
|
||||
public Actor Unload(Actor self)
|
||||
{
|
||||
var a = cargo[0];
|
||||
var a = cargo.Pop();
|
||||
|
||||
cargo.RemoveAt(0);
|
||||
totalWeight -= GetWeight(a);
|
||||
|
||||
SetPassengerFacing(a);
|
||||
@@ -248,7 +247,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public void Load(Actor self, Actor a)
|
||||
{
|
||||
cargo.Add(a);
|
||||
cargo.Push(a);
|
||||
var w = GetWeight(a);
|
||||
totalWeight += w;
|
||||
if (reserves.Contains(a))
|
||||
|
||||
@@ -103,8 +103,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
CPos? ChooseEmptyCellNear(Actor a, string unit)
|
||||
{
|
||||
var possibleCells = GetSuitableCells(a.Location, unit).Where(c => !usedCells.Contains(c)).ToArray();
|
||||
if (possibleCells.Length == 0)
|
||||
var possibleCells = GetSuitableCells(a.Location, unit).Where(c => !usedCells.Contains(c)).ToList();
|
||||
if (possibleCells.Count == 0)
|
||||
return null;
|
||||
|
||||
return possibleCells.Random(self.World.SharedRandom);
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
dudesValue /= 100;
|
||||
|
||||
var eligibleLocations = FootprintUtils.Tiles(self).ToList();
|
||||
var actorTypes = info.ActorTypes.Select(a => new { Name = a, Cost = self.World.Map.Rules.Actors[a].Traits.Get<ValuedInfo>().Cost }).ToArray();
|
||||
var actorTypes = info.ActorTypes.Select(a => new { Name = a, Cost = self.World.Map.Rules.Actors[a].Traits.Get<ValuedInfo>().Cost }).ToList();
|
||||
|
||||
while (eligibleLocations.Count > 0 && actorTypes.Any(a => a.Cost <= dudesValue))
|
||||
{
|
||||
|
||||
@@ -29,9 +29,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public void WorldLoaded(World world, WorldRenderer wr)
|
||||
{
|
||||
domainIndexes = new Dictionary<uint, MovementClassDomainIndex>();
|
||||
var movementClasses = new HashSet<uint>(
|
||||
var movementClasses =
|
||||
world.Map.Rules.Actors.Where(ai => ai.Value.Traits.Contains<MobileInfo>())
|
||||
.Select(ai => (uint)ai.Value.Traits.Get<MobileInfo>().GetMovementClass(world.TileSet)));
|
||||
.Select(ai => (uint)ai.Value.Traits.Get<MobileInfo>().GetMovementClass(world.TileSet)).Distinct();
|
||||
|
||||
foreach (var mc in movementClasses)
|
||||
domainIndexes[mc] = new MovementClassDomainIndex(world, mc);
|
||||
@@ -45,7 +45,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
/// Regenerate the domain index for a group of cells
|
||||
public void UpdateCells(World world, IEnumerable<CPos> cells)
|
||||
{
|
||||
var dirty = new HashSet<CPos>(cells);
|
||||
var dirty = cells.ToHashSet();
|
||||
foreach (var index in domainIndexes)
|
||||
index.Value.UpdateCells(world, dirty);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public void WorldLoaded(World world, WorldRenderer wr)
|
||||
{
|
||||
var spawns = world.Map.GetSpawnPoints().ToList();
|
||||
var spawns = world.Map.GetSpawnPoints();
|
||||
var taken = world.LobbyInfo.Clients.Where(c => c.SpawnPoint != 0 && c.Slot != null)
|
||||
.Select(c => spawns[c.SpawnPoint - 1]).ToList();
|
||||
var available = spawns.Except(taken).ToList();
|
||||
@@ -85,4 +85,4 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return sp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
}
|
||||
|
||||
// Add map smudges
|
||||
foreach (var s in w.Map.Smudges.Value.Where(s => smudges.Keys.Contains(s.Type)))
|
||||
foreach (var s in w.Map.Smudges.Value.Where(s => smudges.ContainsKey(s.Type)))
|
||||
{
|
||||
var smudge = new Smudge
|
||||
{
|
||||
|
||||
@@ -187,8 +187,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
|
||||
var wps = file.GetSection("Waypoints")
|
||||
.Where(kv => Exts.ParseIntegerInvariant(kv.Value) > 0)
|
||||
.Select(kv => Pair.New(Exts.ParseIntegerInvariant(kv.Key),
|
||||
LocationFromMapOffset(Exts.ParseIntegerInvariant(kv.Value), mapSize)))
|
||||
.ToArray();
|
||||
LocationFromMapOffset(Exts.ParseIntegerInvariant(kv.Value), mapSize)));
|
||||
|
||||
// Add waypoint actors
|
||||
foreach (var kv in wps)
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
readonly ContainerWidget chatTemplate;
|
||||
readonly TextFieldWidget chatText;
|
||||
|
||||
readonly List<INotifyChat> chatTraits;
|
||||
readonly INotifyChat[] chatTraits;
|
||||
|
||||
readonly TabCompletionLogic tabCompletion = new TabCompletionLogic();
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
this.orderManager = orderManager;
|
||||
this.modRules = modRules;
|
||||
|
||||
chatTraits = world.WorldActor.TraitsImplementing<INotifyChat>().ToList();
|
||||
chatTraits = world.WorldActor.TraitsImplementing<INotifyChat>().ToArray();
|
||||
|
||||
var players = world.Players.Where(p => p != world.LocalPlayer && !p.NonCombatant && !p.IsBot);
|
||||
disableTeamChat = world.LocalPlayer == null || world.LobbyInfo.IsSinglePlayer || !players.Any(p => p.IsAlliedWith(world.LocalPlayer));
|
||||
|
||||
@@ -459,10 +459,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
var techLevel = optionsBin.GetOrNull<DropDownButtonWidget>("TECHLEVEL_DROPDOWNBUTTON");
|
||||
if (techLevel != null)
|
||||
{
|
||||
var techTraits = modRules.Actors["player"].Traits.WithInterface<ProvidesTechPrerequisiteInfo>().ToArray();
|
||||
techLevel.IsVisible = () => techTraits.Length > 0;
|
||||
optionsBin.GetOrNull<LabelWidget>("TECHLEVEL_DESC").IsVisible = () => techTraits.Length > 0;
|
||||
techLevel.IsDisabled = () => Map.Status != MapStatus.Available || Map.Map.Options.TechLevel != null || configurationDisabled() || techTraits.Length <= 1;
|
||||
var techTraits = modRules.Actors["player"].Traits.WithInterface<ProvidesTechPrerequisiteInfo>().ToList();
|
||||
techLevel.IsVisible = () => techTraits.Count > 0;
|
||||
optionsBin.GetOrNull<LabelWidget>("TECHLEVEL_DESC").IsVisible = () => techTraits.Count > 0;
|
||||
techLevel.IsDisabled = () => Map.Status != MapStatus.Available || Map.Map.Options.TechLevel != null || configurationDisabled() || techTraits.Count <= 1;
|
||||
techLevel.GetText = () => Map.Status != MapStatus.Available || Map.Map.Options.TechLevel != null ? "Not Available" : "{0}".F(orderManager.LobbyInfo.GlobalSettings.TechLevel);
|
||||
techLevel.OnMouseDown = _ =>
|
||||
{
|
||||
|
||||
@@ -156,7 +156,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
var spawns = preview.SpawnPoints;
|
||||
return lobbyInfo.Clients
|
||||
.Where(c => (c.SpawnPoint - 1 >= 0) && (c.SpawnPoint - 1 < spawns.Count))
|
||||
.Where(c => (c.SpawnPoint - 1 >= 0) && (c.SpawnPoint - 1 < spawns.Length))
|
||||
.ToDictionary(c => spawns[c.SpawnPoint - 1], c => new SpawnOccupant(c));
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
var spawns = preview.SpawnPoints;
|
||||
return players
|
||||
.Where(c => (c.SpawnPoint - 1 >= 0) && (c.SpawnPoint - 1 < spawns.Count))
|
||||
.Where(c => (c.SpawnPoint - 1 >= 0) && (c.SpawnPoint - 1 < spawns.Length))
|
||||
.ToDictionary(c => spawns[c.SpawnPoint - 1], c => new SpawnOccupant(c));
|
||||
}
|
||||
|
||||
@@ -430,7 +430,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
var dropdown = parent.Get<DropDownButtonWidget>("SPAWN");
|
||||
dropdown.IsDisabled = () => s.LockSpawn || orderManager.LocalClient.IsReady;
|
||||
dropdown.OnMouseDown = _ => ShowSpawnDropDown(dropdown, c, orderManager, Enumerable.Range(0, map.SpawnPoints.Count + 1).Except(orderManager.LobbyInfo.Clients.Where(client => client != c && client.SpawnPoint != 0).Select(client => client.SpawnPoint)));
|
||||
dropdown.OnMouseDown = _ =>
|
||||
{
|
||||
var spawnPoints = Enumerable.Range(0, map.SpawnPoints.Length + 1).Except(
|
||||
orderManager.LobbyInfo.Clients.Where(
|
||||
client => client != c && client.SpawnPoint != 0).Select(client => client.SpawnPoint));
|
||||
ShowSpawnDropDown(dropdown, c, orderManager, spawnPoints);
|
||||
};
|
||||
dropdown.GetText = () => (c.SpawnPoint == 0) ? "-" : Convert.ToChar('A' - 1 + c.SpawnPoint).ToString();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
string selectedUid;
|
||||
|
||||
// May be a subset of available maps if a mode filter is active
|
||||
List<string> visibleMaps;
|
||||
string[] visibleMaps;
|
||||
|
||||
ScrollPanelWidget scrollpanel;
|
||||
ScrollItemWidget itemTemplate;
|
||||
@@ -103,7 +103,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
selectedUid = uid;
|
||||
scrollpanel.ScrollToItem(uid, smooth: true);
|
||||
};
|
||||
randomMapButton.IsDisabled = () => visibleMaps == null || visibleMaps.Count == 0;
|
||||
randomMapButton.IsDisabled = () => visibleMaps == null || visibleMaps.Length == 0;
|
||||
}
|
||||
|
||||
EnumerateMaps(onSelect, filter);
|
||||
@@ -158,7 +158,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
scrollpanel.AddChild(item);
|
||||
}
|
||||
|
||||
visibleMaps = maps.Select(m => m.Uid).ToList();
|
||||
visibleMaps = maps.Select(m => m.Uid).ToArray();
|
||||
if (visibleMaps.Contains(selectedUid))
|
||||
scrollpanel.ScrollToItem(selectedUid);
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
var ddb = panel.GetOrNull<DropDownButtonWidget>("FLT_MAPNAME_DROPDOWNBUTTON");
|
||||
if (ddb != null)
|
||||
{
|
||||
var options = new HashSet<string>(replays.Select(r => r.GameInfo.MapTitle), StringComparer.OrdinalIgnoreCase).ToList();
|
||||
var options = replays.Select(r => r.GameInfo.MapTitle).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
||||
options.Sort(StringComparer.OrdinalIgnoreCase);
|
||||
options.Insert(0, null); // no filter
|
||||
|
||||
@@ -231,7 +231,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
var ddb = panel.GetOrNull<DropDownButtonWidget>("FLT_PLAYER_DROPDOWNBUTTON");
|
||||
if (ddb != null)
|
||||
{
|
||||
var options = new HashSet<string>(replays.SelectMany(r => r.GameInfo.Players.Select(p => p.Name)), StringComparer.OrdinalIgnoreCase).ToList();
|
||||
var options = replays.SelectMany(r => r.GameInfo.Players.Select(p => p.Name)).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
||||
options.Sort(StringComparer.OrdinalIgnoreCase);
|
||||
options.Insert(0, null); // no filter
|
||||
|
||||
@@ -295,7 +295,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
ddb.IsDisabled = () => string.IsNullOrEmpty(filter.PlayerName);
|
||||
|
||||
var options = new HashSet<string>(replays.SelectMany(r => r.GameInfo.Players.Select(p => p.FactionName).Where(n => !string.IsNullOrEmpty(n))), StringComparer.OrdinalIgnoreCase).ToList();
|
||||
var options = replays.SelectMany(r => r.GameInfo.Players.Select(p => p.FactionName).Where(n => !string.IsNullOrEmpty(n))).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
||||
options.Sort(StringComparer.OrdinalIgnoreCase);
|
||||
options.Insert(0, null); // no filter
|
||||
|
||||
|
||||
@@ -281,7 +281,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
Hotkey = ks.GetProductionHotkey(DisplayedIconCount),
|
||||
Sprite = icon.Image,
|
||||
Pos = new float2(rect.Location),
|
||||
Queued = CurrentQueue.AllQueued().Where(a => a.Item == item.Name).ToList(),
|
||||
Queued = CurrentQueue.AllQueued().Where(a => a.Item == item.Name).ToList()
|
||||
};
|
||||
|
||||
icons.Add(rect, pi);
|
||||
|
||||
@@ -196,7 +196,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
var allQueues = a.World.ActorsWithTrait<ProductionQueue>()
|
||||
.Where(p => p.Actor.Owner == p.Actor.World.LocalPlayer && p.Actor.IsInWorld && p.Trait.Enabled)
|
||||
.Select(p => p.Trait).ToArray();
|
||||
.Select(p => p.Trait).ToList();
|
||||
|
||||
foreach (var g in Groups.Values)
|
||||
g.Update(allQueues);
|
||||
|
||||
Reference in New Issue
Block a user