Use FirstOrDefault instead of manual iteration.

This commit is contained in:
Ottermandias 2025-10-24 15:21:57 +02:00
parent 349b62e549
commit 90cffb1759
2 changed files with 89 additions and 95 deletions

2
Luna

@ -1 +1 @@
Subproject commit 78216203f4570a6194fce9422204d8abb536c828 Subproject commit 8fc11b993d8504982acd95b8d39e6ee7046c1347

View file

@ -1,94 +1,88 @@
using OtterGui.Classes; using OtterGui.Classes;
using OtterGui.Widgets; using OtterGui.Widgets;
namespace Penumbra.Mods.Manager; namespace Penumbra.Mods.Manager;
public class ModCombo(Func<IReadOnlyList<Mod>> generator) : FilterComboCache<Mod>(generator, MouseWheelType.None, Penumbra.Log) public class ModCombo(Func<IReadOnlyList<Mod>> generator) : FilterComboCache<Mod>(generator, MouseWheelType.None, Penumbra.Log)
{ {
protected override bool IsVisible(int globalIndex, LowerString filter) protected override bool IsVisible(int globalIndex, LowerString filter)
=> Items[globalIndex].Name.Contains(filter); => Items[globalIndex].Name.Contains(filter);
protected override string ToString(Mod obj) protected override string ToString(Mod obj)
=> obj.Name; => obj.Name;
} }
public class ModStorage : IReadOnlyList<Mod> public class ModStorage : IReadOnlyList<Mod>
{ {
/// <summary> The actual list of mods. </summary> /// <summary> The actual list of mods. </summary>
protected readonly List<Mod> Mods = []; protected readonly List<Mod> Mods = [];
public int Count public int Count
=> Mods.Count; => Mods.Count;
public Mod this[int idx] public Mod this[int idx]
=> Mods[idx]; => Mods[idx];
public Mod this[Index idx] public Mod this[Index idx]
=> Mods[idx]; => Mods[idx];
public IEnumerator<Mod> GetEnumerator() public IEnumerator<Mod> GetEnumerator()
=> Mods.GetEnumerator(); => Mods.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator(); => GetEnumerator();
/// <summary> /// <summary>
/// Try to obtain a mod by its directory name (unique identifier). /// Try to obtain a mod by its directory name (unique identifier).
/// </summary> /// </summary>
public bool TryGetMod(string identifier, [NotNullWhen(true)] out Mod? mod) public bool TryGetMod(string identifier, [NotNullWhen(true)] out Mod? mod)
{ {
foreach (var m in Mods) mod = this.FirstOrDefault(m => string.Equals(m.Identifier, identifier, StringComparison.OrdinalIgnoreCase));
{ return mod is not null;
if (string.Equals(m.Identifier, identifier, StringComparison.OrdinalIgnoreCase)) }
{
mod = m; /// <summary>
return true; /// Try to obtain a mod by its directory name (unique identifier, preferred),
} /// or the first mod of the given name if no directory fits.
} /// </summary>
public bool TryGetMod(string identifier, string modName, [NotNullWhen(true)] out Mod? mod)
mod = null; {
return false; if (modName.Length is 0)
} return TryGetMod(identifier, out mod);
/// <summary> mod = null;
/// Try to obtain a mod by its directory name (unique identifier, preferred), foreach (var m in Mods)
/// or the first mod of the given name if no directory fits. {
/// </summary> if (string.Equals(m.Identifier, identifier, StringComparison.OrdinalIgnoreCase))
public bool TryGetMod(string identifier, string modName, [NotNullWhen(true)] out Mod? mod) {
{ mod = m;
mod = null; return true;
foreach (var m in Mods) }
{
if (string.Equals(m.Identifier, identifier, StringComparison.OrdinalIgnoreCase)) if (m.Name == modName)
{ mod ??= m;
mod = m; }
return true;
} return mod != null;
}
if (m.Name == modName)
mod ??= m; /// <summary>
} /// An easily accessible set of new mods.
/// Mods are added when they are created or imported.
return mod != null; /// Mods are removed when they are deleted or when they are toggled in any collection.
} /// Also gets cleared on mod rediscovery.
/// </summary>
/// <summary> private readonly HashSet<Mod> _newMods = [];
/// An easily accessible set of new mods.
/// Mods are added when they are created or imported. public bool IsNew(Mod mod)
/// Mods are removed when they are deleted or when they are toggled in any collection. => _newMods.Contains(mod);
/// Also gets cleared on mod rediscovery.
/// </summary> public void SetNew(Mod mod)
private readonly HashSet<Mod> _newMods = []; => _newMods.Add(mod);
public bool IsNew(Mod mod) public void SetKnown(Mod mod)
=> _newMods.Contains(mod); => _newMods.Remove(mod);
public void SetNew(Mod mod) public void ClearNewMods()
=> _newMods.Add(mod); => _newMods.Clear();
}
public void SetKnown(Mod mod)
=> _newMods.Remove(mod);
public void ClearNewMods()
=> _newMods.Clear();
}