Introduce FirstOrDefault extensions method for Array.Find and List.Find.

This allows the LINQ spelling to be used, but benefits from the performance improvement of the specific methods for these classes that provide the same result.
This commit is contained in:
RoosterDragon
2023-11-17 09:49:27 +00:00
committed by Gustas
parent acca837142
commit e6914f707a
54 changed files with 83 additions and 89 deletions

View File

@@ -109,14 +109,14 @@ namespace OpenRA
.Where(f => !requireSelectable || f.Selectable)
.ToList();
var selected = selectableFactions.Find(f => f.InternalName == factionName)
var selected = selectableFactions.FirstOrDefault(f => f.InternalName == factionName)
?? selectableFactions.Random(playerRandom);
// Don't loop infinite
for (var i = 0; i <= 10 && selected.RandomFactionMembers.Count > 0; i++)
{
var faction = selected.RandomFactionMembers.Random(playerRandom);
selected = selectableFactions.Find(f => f.InternalName == faction);
selected = selectableFactions.FirstOrDefault(f => f.InternalName == faction);
if (selected == null)
throw new YamlException($"Unknown faction: {faction}");
@@ -179,7 +179,7 @@ namespace OpenRA
else
{
// Map player
ClientIndex = world.LobbyInfo.Clients.Find(c => c.IsAdmin)?.Index ?? 0; // Owned by the host (TODO: fix this)
ClientIndex = world.LobbyInfo.Clients.FirstOrDefault(c => c.IsAdmin)?.Index ?? 0; // Owned by the host (TODO: fix this)
Color = pr.Color;
PlayerName = pr.Name;
NonCombatant = pr.NonCombatant;