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

@@ -146,8 +146,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (map.Package != null)
{
selectedDirectory = writableDirectories.Find(k => k.Folder.Contains(map.Package.Name));
selectedDirectory ??= writableDirectories.Find(k => Directory.GetDirectories(k.Folder.Name).Any(f => f.Contains(map.Package.Name)));
selectedDirectory = writableDirectories.FirstOrDefault(k => k.Folder.Contains(map.Package.Name));
selectedDirectory ??= writableDirectories.FirstOrDefault(k => Directory.GetDirectories(k.Folder.Name).Any(f => f.Contains(map.Package.Name)));
}
// Prioritize MapClassification.User directories over system directories

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
void SelectTab(bool reverse)
{
palette.CurrentQueue = Array.Find(queues, q => q.Enabled);
palette.CurrentQueue = queues.FirstOrDefault(q => q.Enabled);
// When a tab is selected, scroll to the top because the current row position may be invalid for the new tab
palette.ScrollToTop();

View File

@@ -212,7 +212,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
var localClient = orderManager.LocalClient;
var localPlayer = localClient == null ? null : Array.Find(world.Players, player => player.ClientIndex == localClient.Index);
var localPlayer = localClient == null ? null : world.Players.FirstOrDefault(player => player.ClientIndex == localClient.Index);
bool LocalPlayerCanKick() => localClient != null
&& (Game.IsHost || ((!orderManager.LocalClient.IsObserver) && localPlayer.WinState == WinState.Undefined));
bool CanClientBeKicked(Session.Client client, Func<bool> isVoteKick) =>

View File

@@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var downloadYaml = MiniYaml.Load(modFileSystem, content.Downloads, null);
modFileSystem.UnmountAll();
var download = downloadYaml.Find(n => n.Key == content.QuickDownload);
var download = downloadYaml.FirstOrDefault(n => n.Key == content.QuickDownload);
if (download == null)
throw new InvalidOperationException($"Mod QuickDownload `{content.QuickDownload}` definition not found.");

View File

@@ -274,7 +274,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var botTypes = map.PlayerActorInfo.TraitInfos<IBotInfo>().Select(t => t.Type);
var options = new Dictionary<string, IEnumerable<DropDownOption>>();
var botController = orderManager.LobbyInfo.Clients.Find(c => c.IsAdmin);
var botController = orderManager.LobbyInfo.Clients.FirstOrDefault(c => c.IsAdmin);
if (orderManager.LobbyInfo.Slots.Values.Any(s => s.AllowBots))
{
var botOptions = new List<DropDownOption>()

View File

@@ -74,7 +74,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
foreach (var b in map.PlayerActorInfo.TraitInfos<IBotInfo>())
{
var botController = orderManager.LobbyInfo.Clients.Find(c => c.IsAdmin);
var botController = orderManager.LobbyInfo.Clients.FirstOrDefault(c => c.IsAdmin);
bots.Add(new SlotDropDownOption(b.Name,
$"slot_bot {slot.PlayerReference} {botController.Index} {b.Type}",
() => client != null && client.Bot == b.Type));
@@ -266,7 +266,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
static void ClearPlayerSpawnPoint(OrderManager orderManager, MapPreviewWidget mapPreview, MapPreview preview, MouseInput mi)
{
var selectedSpawn = DetermineSelectedSpawnPoint(mapPreview, preview, mi);
if (Game.IsHost || orderManager.LobbyInfo.Clients.Find(cc => cc.SpawnPoint == selectedSpawn) == orderManager.LocalClient)
if (Game.IsHost || orderManager.LobbyInfo.Clients.FirstOrDefault(cc => cc.SpawnPoint == selectedSpawn) == orderManager.LocalClient)
orderManager.IssueOrder(Order.Command($"clear_spawn {selectedSpawn}"));
}

View File

@@ -356,7 +356,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
gameModeDropdown.GetText = () =>
{
var item = categories.Find(m => m.Category == category);
var item = categories.FirstOrDefault(m => m.Category == category);
if (item == default((string, int)))
item.Category = TranslationProvider.GetString(NoMatches);

View File

@@ -696,7 +696,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
void SelectFirstVisibleReplay()
{
SelectReplay(replays.Find(r => replayState[r].Visible));
SelectReplay(replays.FirstOrDefault(r => replayState[r].Visible));
}
void SelectReplay(ReplayMetadata replay)

View File

@@ -103,7 +103,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
videoVolumeSlider.OnChange += x => Game.Sound.VideoVolume = x;
var devices = Game.Sound.AvailableDevices();
soundDevice = Array.Find(devices, d => d.Device == ss.Device) ?? devices[0];
soundDevice = devices.FirstOrDefault(d => d.Device == ss.Device) ?? devices[0];
var audioDeviceDropdown = panel.Get<DropDownButtonWidget>("AUDIO_DEVICE");
audioDeviceDropdown.OnMouseDown = _ => ShowAudioDeviceDropdown(audioDeviceDropdown, devices, scrollPanel);