From fce8b058b07e3253d6be2a452bf38be4b355d811 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 25 Jan 2024 15:56:49 +0100 Subject: [PATCH] Improve DesignStorage slightly. --- Glamourer/Api/GlamourerIpc.Apply.cs | 3 +- Glamourer/Automation/AutoDesignManager.cs | 3 +- Glamourer/Designs/DesignColors.cs | 29 +++++--------- Glamourer/Designs/DesignManager.cs | 47 +++++++++++------------ Glamourer/Designs/DesignStorage.cs | 14 ++++++- Glamourer/Services/CommandService.cs | 2 +- Glamourer/Services/IGamePathParser.cs | 6 --- 7 files changed, 48 insertions(+), 56 deletions(-) delete mode 100644 Glamourer/Services/IGamePathParser.cs diff --git a/Glamourer/Api/GlamourerIpc.Apply.cs b/Glamourer/Api/GlamourerIpc.Apply.cs index 08ccff4..c2d68aa 100644 --- a/Glamourer/Api/GlamourerIpc.Apply.cs +++ b/Glamourer/Api/GlamourerIpc.Apply.cs @@ -1,7 +1,6 @@ using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Plugin; using Glamourer.Designs; -using Glamourer.Events; using Glamourer.Interop.Structs; using Glamourer.State; using Penumbra.Api.Helpers; @@ -138,5 +137,5 @@ public partial class GlamourerIpc } private void ApplyDesignByGuid(Guid identifier, IEnumerable actors, uint lockCode) - => ApplyDesign(_designManager.Designs.FirstOrDefault(x => x.Identifier == identifier), actors, DesignConverter.Version, lockCode); + => ApplyDesign(_designManager.Designs.ByIdentifier(identifier), actors, DesignConverter.Version, lockCode); } diff --git a/Glamourer/Automation/AutoDesignManager.cs b/Glamourer/Automation/AutoDesignManager.cs index 72b8daf..2e77c01 100644 --- a/Glamourer/Automation/AutoDesignManager.cs +++ b/Glamourer/Automation/AutoDesignManager.cs @@ -480,8 +480,7 @@ public class AutoDesignManager : ISavable, IReadOnlyList, IDispos return null; } - design = _designs.Designs.FirstOrDefault(d => d.Identifier == guid); - if (design == null) + if (!_designs.Designs.TryGetValue(guid, out design)) { Glamourer.Messager.NotificationMessage( $"Error parsing automatically applied design for set {setName}: The specified design {guid} does not exist.", diff --git a/Glamourer/Designs/DesignColors.cs b/Glamourer/Designs/DesignColors.cs index 4026a98..bd192be 100644 --- a/Glamourer/Designs/DesignColors.cs +++ b/Glamourer/Designs/DesignColors.cs @@ -11,21 +11,10 @@ using OtterGui.Classes; namespace Glamourer.Designs; -public class DesignColorUi +public class DesignColorUi(DesignColors colors, Configuration config) { - private readonly DesignColors _colors; - private readonly DesignManager _designs; - private readonly Configuration _config; - private string _newName = string.Empty; - public DesignColorUi(DesignColors colors, DesignManager designs, Configuration config) - { - _colors = colors; - _designs = designs; - _config = config; - } - public void Draw() { using var table = ImRaii.Table("designColors", 3, ImGuiTableFlags.RowBg); @@ -44,7 +33,7 @@ public class DesignColorUi ImGui.TableNextColumn(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Recycle.ToIconString(), buttonSize, - "Revert the color used for missing design colors to its default.", _colors.MissingColor == DesignColors.MissingColorDefault, + "Revert the color used for missing design colors to its default.", colors.MissingColor == DesignColors.MissingColorDefault, true)) { changeString = DesignColors.MissingColorName; @@ -52,7 +41,7 @@ public class DesignColorUi } ImGui.TableNextColumn(); - if (DrawColorButton(DesignColors.MissingColorName, _colors.MissingColor, out var newColor)) + if (DrawColorButton(DesignColors.MissingColorName, colors.MissingColor, out var newColor)) { changeString = DesignColors.MissingColorName; changeValue = newColor; @@ -64,12 +53,12 @@ public class DesignColorUi ImGuiUtil.HoverTooltip("This color is used when the color specified in a design is not available."); - var disabled = !_config.DeleteDesignModifier.IsActive(); + var disabled = !config.DeleteDesignModifier.IsActive(); var tt = "Delete this color. This does not remove it from designs using it."; if (disabled) - tt += $"\nHold {_config.DeleteDesignModifier} to delete."; + tt += $"\nHold {config.DeleteDesignModifier} to delete."; - foreach (var ((name, color), idx) in _colors.WithIndex()) + foreach (var ((name, color), idx) in colors.WithIndex()) { using var id = ImRaii.PushId(idx); ImGui.TableNextColumn(); @@ -97,7 +86,7 @@ public class DesignColorUi ? ("Specify a name for a new color first.", true) : _newName is DesignColors.MissingColorName or DesignColors.AutomaticName ? ($"You can not use the name {DesignColors.MissingColorName} or {DesignColors.AutomaticName}, choose a different one.", true) - : _colors.ContainsKey(_newName) + : colors.ContainsKey(_newName) ? ($"The color {_newName} already exists, please choose a different name.", true) : ($"Add a new color {_newName} to your list.", false); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), buttonSize, tt, disabled, true)) @@ -119,9 +108,9 @@ public class DesignColorUi if (changeString.Length > 0) { if (!changeValue.HasValue) - _colors.DeleteColor(changeString); + colors.DeleteColor(changeString); else - _colors.SetColor(changeString, changeValue.Value); + colors.SetColor(changeString, changeValue.Value); } } diff --git a/Glamourer/Designs/DesignManager.cs b/Glamourer/Designs/DesignManager.cs index 3b3b43d..c3ff2ac 100644 --- a/Glamourer/Designs/DesignManager.cs +++ b/Glamourer/Designs/DesignManager.cs @@ -21,16 +21,14 @@ public class DesignManager private readonly HumanModelList _humans; private readonly SaveService _saveService; private readonly DesignChanged _event; - private readonly DesignStorage _designs; private readonly Dictionary _undoStore = []; - public IReadOnlyList Designs - => _designs; + public DesignStorage Designs { get; } public DesignManager(SaveService saveService, ItemManager items, CustomizeService customizations, DesignChanged @event, HumanModelList humans, DesignStorage storage, DesignLinkLoader designLinkLoader, Configuration config) { - _designs = storage; + Designs = storage; _config = config; _saveService = saveService; _items = items; @@ -55,7 +53,7 @@ public class DesignManager _items.ItemData.Awaiter.Wait(); var stopwatch = Stopwatch.StartNew(); - _designs.Clear(); + Designs.Clear(); var skipped = 0; ThreadLocal> designs = new(() => [], true); Parallel.ForEach(_saveService.FileNames.Designs(), (f, _) => @@ -79,15 +77,15 @@ public class DesignManager { if (design.Identifier.ToString() != Path.GetFileNameWithoutExtension(path)) invalidNames.Add((design, path)); - if (_designs.Any(d => d.Identifier == design.Identifier)) + if (Designs.Contains(design.Identifier)) { Glamourer.Log.Error($"Could not load design, skipped: Identifier {design.Identifier} was not unique."); ++skipped; continue; } - design.Index = _designs.Count; - _designs.Add(design); + design.Index = Designs.Count; + Designs.Add(design); } var failed = MoveInvalidNames(invalidNames); @@ -96,7 +94,7 @@ public class DesignManager $"Moved {invalidNames.Count - failed} designs to correct names.{(failed > 0 ? $" Failed to move {failed} designs to correct names." : string.Empty)}"); Glamourer.Log.Information( - $"Loaded {_designs.Count} designs in {stopwatch.ElapsedMilliseconds} ms.{(skipped > 0 ? $" Skipped loading {skipped} designs due to errors." : string.Empty)}"); + $"Loaded {Designs.Count} designs in {stopwatch.ElapsedMilliseconds} ms.{(skipped > 0 ? $" Skipped loading {skipped} designs due to errors." : string.Empty)}"); _event.Invoke(DesignChanged.Type.ReloadedAll, null!, null); } @@ -118,9 +116,9 @@ public class DesignManager LastEdit = DateTimeOffset.UtcNow, Identifier = CreateNewGuid(), Name = actualName, - Index = _designs.Count, + Index = Designs.Count, }; - _designs.Add(design); + Designs.Add(design); Glamourer.Log.Debug($"Added new design {design.Identifier}."); _saveService.ImmediateSave(design); _event.Invoke(DesignChanged.Type.Created, design, path); @@ -137,10 +135,10 @@ public class DesignManager LastEdit = DateTimeOffset.UtcNow, Identifier = CreateNewGuid(), Name = actualName, - Index = _designs.Count, + Index = Designs.Count, }; - _designs.Add(design); + Designs.Add(design); Glamourer.Log.Debug($"Added new design {design.Identifier} by cloning Temporary Design."); _saveService.ImmediateSave(design); _event.Invoke(DesignChanged.Type.Created, design, path); @@ -157,9 +155,9 @@ public class DesignManager LastEdit = DateTimeOffset.UtcNow, Identifier = CreateNewGuid(), Name = actualName, - Index = _designs.Count, + Index = Designs.Count, }; - _designs.Add(design); + Designs.Add(design); Glamourer.Log.Debug( $"Added new design {design.Identifier} by cloning {clone.Identifier.ToString()}."); _saveService.ImmediateSave(design); @@ -170,9 +168,9 @@ public class DesignManager /// Delete a design. public void Delete(Design design) { - foreach (var d in _designs.Skip(design.Index + 1)) + foreach (var d in Designs.Skip(design.Index + 1)) --d.Index; - _designs.RemoveAt(design.Index); + Designs.RemoveAt(design.Index); _saveService.ImmediateDelete(design); _event.Invoke(DesignChanged.Type.Deleted, design, null); } @@ -591,7 +589,7 @@ public class DesignManager var errors = 0; var skips = 0; var successes = 0; - var oldDesigns = _designs.ToList(); + var oldDesigns = Designs.ToList(); try { var text = File.ReadAllText(_saveService.FileNames.MigrationDesignFile); @@ -697,7 +695,7 @@ public class DesignManager while (true) { var guid = Guid.NewGuid(); - if (_designs.All(d => d.Identifier != guid)) + if (!Designs.Contains(guid)) return guid; } } @@ -709,11 +707,11 @@ public class DesignManager /// private bool Add(Design design, string? message) { - if (_designs.Any(d => d == design || d.Identifier == design.Identifier)) + if (Designs.Any(d => d == design || d.Identifier == design.Identifier)) return false; - design.Index = _designs.Count; - _designs.Add(design); + design.Index = Designs.Count; + Designs.Add(design); if (!message.IsNullOrEmpty()) Glamourer.Log.Debug(message); _saveService.ImmediateSave(design); @@ -740,9 +738,10 @@ public class DesignManager } /// Change a mainhand weapon and either fix or apply appropriate offhand and potentially gauntlets. - private bool ChangeMainhandPeriphery(Design design, EquipItem currentMain, EquipItem currentOff, EquipItem newMain, out EquipItem? newOff, out EquipItem? newGauntlets) + private bool ChangeMainhandPeriphery(Design design, EquipItem currentMain, EquipItem currentOff, EquipItem newMain, out EquipItem? newOff, + out EquipItem? newGauntlets) { - newOff = null; + newOff = null; newGauntlets = null; if (newMain.Type != currentMain.Type) { diff --git a/Glamourer/Designs/DesignStorage.cs b/Glamourer/Designs/DesignStorage.cs index 297f3be..a87415c 100644 --- a/Glamourer/Designs/DesignStorage.cs +++ b/Glamourer/Designs/DesignStorage.cs @@ -3,4 +3,16 @@ namespace Glamourer.Designs; public class DesignStorage : List, IService -{} +{ + public bool TryGetValue(Guid identifier, [NotNullWhen(true)] out Design? design) + { + design = ByIdentifier(identifier); + return design != null; + } + + public Design? ByIdentifier(Guid identifier) + => this.FirstOrDefault(d => d.Identifier == identifier); + + public bool Contains(Guid identifier) + => ByIdentifier(identifier) != null; +} diff --git a/Glamourer/Services/CommandService.cs b/Glamourer/Services/CommandService.cs index 41a2052..e300ab4 100644 --- a/Glamourer/Services/CommandService.cs +++ b/Glamourer/Services/CommandService.cs @@ -587,7 +587,7 @@ public class CommandService : IDisposable if (Guid.TryParse(argument, out var guid)) { - design = _designManager.Designs.FirstOrDefault(d => d.Identifier == guid); + design = _designManager.Designs.ByIdentifier(guid); } else { diff --git a/Glamourer/Services/IGamePathParser.cs b/Glamourer/Services/IGamePathParser.cs deleted file mode 100644 index 28364d1..0000000 --- a/Glamourer/Services/IGamePathParser.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Glamourer.Services -{ - internal interface IGamePathParser - { - } -} \ No newline at end of file