From 6382bbdfa1cf4255377bef13b056a15af10f297f Mon Sep 17 00:00:00 2001 From: Kurochi51 Date: Wed, 21 Feb 2024 16:13:06 +0200 Subject: [PATCH] Address nits. Add button to delete user entries. Changed `ConfigWindow.Save` to accomodate the use of the now deleted `InitData`. Changed all instances of `Enum.TryParse` to `Enum.IsDefined`. --- PetScale/GlobalSuppressions.cs | 1 + PetScale/PetScale.cs | 60 +++++++++----------------------- PetScale/PetScale.csproj | 4 +-- PetScale/Windows/ConfigWindow.cs | 34 +++++++++++++----- 4 files changed, 45 insertions(+), 54 deletions(-) diff --git a/PetScale/GlobalSuppressions.cs b/PetScale/GlobalSuppressions.cs index b28548d..001acab 100644 --- a/PetScale/GlobalSuppressions.cs +++ b/PetScale/GlobalSuppressions.cs @@ -9,3 +9,4 @@ [assembly: SuppressMessage("Design", "MA0038:Make method static (deprecated, use CA1822 instead)", Justification = "Obsolete", Scope = "member", Target = "~M:PetScale.Helpers.Utilities.SetScale(FFXIVClientStructs.FFXIV.Client.Game.Character.BattleChara*,System.Single)")] [assembly: SuppressMessage("Design", "MA0041:Make property static (deprecated, use CA1822 instead)", Justification = "Obsolete", Scope = "member", Target = "~P:PetScale.PetScale.BattleCharaSpan")] [assembly: SuppressMessage("Design", "MA0016:Prefer using collection abstraction instead of implementation", Justification = "Not meant for public consumption", Scope = "member", Target = "~P:PetScale.Windows.ConfigWindow.petMap")] +[assembly: SuppressMessage("Performance", "MA0066:Hash table unfriendly type is used in a hash table", Justification = "None", Scope = "member", Target = "~F:PetScale.PetScale.activePetDictionary")] diff --git a/PetScale/PetScale.cs b/PetScale/PetScale.cs index f8a404b..500b4d1 100644 --- a/PetScale/PetScale.cs +++ b/PetScale/PetScale.cs @@ -37,14 +37,22 @@ public sealed class PetScale : IDalamudPlugin private readonly IClientState clientState; private readonly Utilities utilities; - private readonly CultureInfo cultureInfo = CultureInfo.InvariantCulture; private readonly StringComparison ordinalComparison = StringComparison.Ordinal; private readonly Dictionary, (Pointer character, bool petSet)> activePetDictionary = new(); - private readonly Dictionary petSizeMap = new(); + private readonly Dictionary petSizeMap = new(StringComparer.OrdinalIgnoreCase); private readonly Stopwatch stopwatch = new(); private readonly TimeSpan dictionaryExpirationTime = TimeSpan.FromMilliseconds(500); // used via .TotalMilliseconds private const string Others = "Other players"; + private readonly Dictionary petModelMap = new() + { + { PetRow.Bahamut, PetModel.Bahamut }, + { PetRow.Phoenix, PetModel.Phoenix }, + { PetRow.Ifrit, PetModel.Ifrit }, + { PetRow.Titan, PetModel.Titan }, + { PetRow.Garuda, PetModel.Garuda }, + }; + public WindowSystem WindowSystem { get; } = new("PetScale"); public Queue players { get; } = new(101); // 100 players + Others entry public bool requestedCache { get; set; } = true; @@ -93,33 +101,7 @@ public PetScale(DalamudPluginInterface _pluginInterface, stopwatch.Start(); _ = Task.Run(InitSheet); - InitData(); - } - - private void InitData() - { - if (config.PetData.Count is 0) - { - return; - } - var tempEnumerable = config.PetData.Where(item => item.CharacterName.Equals("Other players", StringComparison.Ordinal)); - if (tempEnumerable.Count() is not 0) - { - var tempList = tempEnumerable.ToList(); - tempList.AddRange(config.PetData.Except(tempList).OrderBy(item => item.CharacterName, StringComparer.Ordinal).ThenBy(item => item.PetID.ToString(), StringComparer.Ordinal).ToList()); - if (tempList.Count == config.PetData.Count && config.PetData.ToHashSet().SetEquals(tempList)) - { - config.PetData = tempList; - } - var otherEntry = tempList.Last(item => item.CharacterName.Equals("Other players", StringComparison.Ordinal)); - lastIndexOfOthers = tempList.LastIndexOf(otherEntry); - } - else - { - var orderedList = config.PetData.OrderBy(item => item.CharacterName, StringComparer.Ordinal).ThenBy(item => item.PetID.ToString(), StringComparer.Ordinal).ToList(); - config.PetData = orderedList; - lastIndexOfOthers = -1; - } + ConfigWindow.Save(save: false); } private void TerritoryChanged(ushort obj) @@ -137,7 +119,7 @@ private void InitSheet() ConfigWindow.petMap.Add(nameof(PetModel.AllPets), PetModel.AllPets); foreach (var pet in petSheet) { - if (!Enum.TryParse(typeof(PetRow), pet.RowId.ToString(cultureInfo), out var row) || row is not PetRow) + if (!Enum.IsDefined((PetRow)pet.RowId)) { continue; } @@ -147,18 +129,7 @@ private void InitSheet() continue; } petSizeMap.Add(pet.Name, scales); - var name = row.ToString(); - if (name.IsNullOrWhitespace()) - { - log.Debug("Invalid PetRow {name}", row); - continue; - } - if (!Enum.TryParse(typeof(PetModel), name, out var model) || model is not PetModel modelValue) - { - log.Debug("PetRow {name} couldn't map onto PetModel", name); - continue; - } - ConfigWindow.petMap.Add(pet.Name, modelValue); + ConfigWindow.petMap.Add(pet.Name, petModelMap[(PetRow)pet.RowId]); } foreach (var entry in petSizeMap) { @@ -230,7 +201,7 @@ private unsafe void PopulateDictionary() { continue; } - if (!Enum.TryParse(typeof(PetModel), chara.Value->Character.CharacterData.ModelCharaId.ToString(cultureInfo), out _)) + if (!Enum.IsDefined(typeof(PetModel), chara.Value->Character.CharacterData.ModelCharaId)) { continue; } @@ -289,7 +260,8 @@ private unsafe void ParseDictionary(PlayerCharacter player) private unsafe bool ParseStruct(BattleChara* pet, string characterName, string petName, int modelId, bool isLocalPlayer) { var petSet = false; - if (!Enum.TryParse(typeof(PetModel), modelId.ToString(cultureInfo), out var model) || model is not PetModel modelType) + var modelType = (PetModel)modelId; + if (!Enum.IsDefined(modelType)) { return petSet; } diff --git a/PetScale/PetScale.csproj b/PetScale/PetScale.csproj index a105557..67e2af3 100644 --- a/PetScale/PetScale.csproj +++ b/PetScale/PetScale.csproj @@ -3,7 +3,7 @@ - 1.0.0.0 + 1.0.1.0 Customize pet scale. https://github.com/Kurochi51/PetScale @@ -15,7 +15,7 @@ net7.0-windows x64 enable - 11 + latest true false false diff --git a/PetScale/Windows/ConfigWindow.cs b/PetScale/Windows/ConfigWindow.cs index 92d50ed..6b14012 100644 --- a/PetScale/Windows/ConfigWindow.cs +++ b/PetScale/Windows/ConfigWindow.cs @@ -31,9 +31,9 @@ public sealed class ConfigWindow : Window, IDisposable private readonly IPluginLog log; private readonly Dictionary sizeMap = new() { - { PetSize.SmallModelScale, "Small" }, - { PetSize.MediumModelScale, "Medium" }, - { PetSize.LargeModelScale, "Large" }, + { PetSize.SmallModelScale, "Small" }, + { PetSize.MediumModelScale, "Medium"}, + { PetSize.LargeModelScale, "Large" }, }; private readonly string buttonIcon; private readonly CancellationTokenSource cts; @@ -83,7 +83,7 @@ public override void OnOpen() public override void OnClose() { - Save(); + Save(save: true); } public override void Draw() @@ -188,7 +188,7 @@ private unsafe void DisplayEntries() clipper.Destroy(); if (itemRemoved) { - Save(); + Save(save: true); } } @@ -335,7 +335,7 @@ private void CheckPossibleEntry() log.Debug("Entry {name} with {pet} at {size} got changed.", checkPet.CharacterName, petSelection, checkPet.PetSize); pluginInterface.UiBuilder.AddNotification(entry + sizeMap[petData[index].PetSize]); } - Save(); + Save(save: true); } private void DrawBottomButtons() @@ -347,6 +347,12 @@ private void DrawBottomButtons() { RequestCache(newCache: true); } + ImGui.SameLine(); + if (ImGui.Button("Clear All Entries")) + { + petData.Clear(); + Save(save: true); + } ImGui.SetCursorPos(originPos); ImGui.SetCursorPosX(ImGui.GetWindowContentRegionMax().X - ImGui.CalcTextSize("Close").X - 10f); ImGui.SetCursorPosY(ImGui.GetWindowContentRegionMax().Y - ImGui.GetFrameHeight() - (3f * ImGuiHelpers.GlobalScale) + (ImGui.GetScrollY() * 2)); @@ -388,8 +394,17 @@ private void ResizeIfNeeded() } } - private void Save() + public void Save(bool save) { + if (petData.Count is 0) + { + plugin.lastIndexOfOthers = -1; + if (save) + { + config.Save(pluginInterface); + } + return; + } var tempEnumerable = petData.Where(item => item.CharacterName.Equals("Other players", StringComparison.Ordinal)); if (tempEnumerable.Count() is not 0) { @@ -408,7 +423,10 @@ private void Save() config.PetData = orderedList; plugin.lastIndexOfOthers = -1; } - config.Save(pluginInterface); + if (save) + { + config.Save(pluginInterface); + } } private async void QueueColumnWidthChange(IFontHandle handle, ILockedImFont lockedFont)