Skip to content

Commit

Permalink
Address nits.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Kurochi51 committed Feb 21, 2024
1 parent def7e7b commit 6382bbd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
1 change: 1 addition & 0 deletions PetScale/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
60 changes: 16 additions & 44 deletions PetScale/PetScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BattleChara>, (Pointer<Character> character, bool petSet)> activePetDictionary = new();
private readonly Dictionary<string, (float smallScale, float mediumScale, float largeScale)> petSizeMap = new();
private readonly Dictionary<string, (float smallScale, float mediumScale, float largeScale)> 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<PetRow, PetModel> 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<string> players { get; } = new(101); // 100 players + Others entry
public bool requestedCache { get; set; } = true;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions PetScale/PetScale.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Authors></Authors>
<Company></Company>
<Version>1.0.0.0</Version>
<Version>1.0.1.0</Version>
<Description>Customize pet scale.</Description>
<Copyright></Copyright>
<PackageProjectUrl>https://github.com/Kurochi51/PetScale</PackageProjectUrl>
Expand All @@ -15,7 +15,7 @@
<TargetFramework>net7.0-windows</TargetFramework>
<Platforms>x64</Platforms>
<Nullable>enable</Nullable>
<LangVersion>11</LangVersion>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand Down
34 changes: 26 additions & 8 deletions PetScale/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public sealed class ConfigWindow : Window, IDisposable
private readonly IPluginLog log;
private readonly Dictionary<PetSize, string> 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;
Expand Down Expand Up @@ -83,7 +83,7 @@ public override void OnOpen()

public override void OnClose()
{
Save();
Save(save: true);
}

public override void Draw()
Expand Down Expand Up @@ -188,7 +188,7 @@ private unsafe void DisplayEntries()
clipper.Destroy();
if (itemRemoved)
{
Save();
Save(save: true);
}
}

Expand Down Expand Up @@ -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()
Expand All @@ -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));
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down

0 comments on commit 6382bbd

Please sign in to comment.