Skip to content

Commit

Permalink
Use C# 12 collection initialization.
Browse files Browse the repository at this point in the history
Use C# 12 primary constructor for `Utilities`.
Swap to NET 8.
Switch to `GeneratedSheets2` for new schema.
  • Loading branch information
Kurochi51 committed Mar 12, 2024
1 parent e57e44a commit 4ad3561
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion PetScale/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PetScale;
public class Configuration : IPluginConfiguration
{
public int Version { get; set; } = 0;
public IList<PetStruct> PetData { get; set; } = new List<PetStruct>();
public IList<PetStruct> PetData { get; set; } = [];


public void Save(DalamudPluginInterface pi) => pi.SavePluginConfig(this);
Expand Down
2 changes: 2 additions & 0 deletions PetScale/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
[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")]
[assembly: SuppressMessage("Design", "MA0038:Make method static (deprecated, use CA1822 instead)", Justification = "Obsolete", Scope = "member", Target = "~M:PetScale.Helpers.Utilities.CachePlayerList(System.UInt32,System.Collections.Generic.Queue{System.String},System.Span{FFXIVClientStructs.Interop.Pointer{FFXIVClientStructs.FFXIV.Client.Game.Character.BattleChara}})")]
[assembly: SuppressMessage("Design", "MA0038:Make method static (deprecated, use CA1822 instead)", Justification = "Obsolete", Scope = "member", Target = "~M:PetScale.Helpers.Utilities.PetVisible(FFXIVClientStructs.FFXIV.Client.Game.Character.BattleChara*)~System.Boolean")]
25 changes: 14 additions & 11 deletions PetScale/Helpers/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@

namespace PetScale.Helpers;

public class Utilities
public class Utilities(IDataManager _dataManager, IPluginLog _pluginLog)
{
private readonly IDataManager dataManager;
private readonly IPluginLog log;

public Utilities(IDataManager _dataManager, IPluginLog _pluginLog)
{
dataManager = _dataManager;
log = _pluginLog;
}
private readonly IDataManager dataManager = _dataManager;
private readonly IPluginLog log = _pluginLog;

/// <summary>
/// Attempt to retrieve an <see cref="ExcelSheet{T}"/>, optionally in a specific <paramref name="language"/>.
Expand Down Expand Up @@ -52,8 +46,8 @@ public unsafe void SetScale(BattleChara* pet, float scale)
{
return;
}
pet->Character.CharacterData.ModelScale = scale;
pet->Character.GameObject.Scale = scale;
pet->Character.CharacterData.ModelScale = scale;
var drawObject = pet->Character.GameObject.GetDrawObject();
if (drawObject is not null)
{
Expand All @@ -75,7 +69,7 @@ public static unsafe void ToggleVisibility(FFXIVClientStructs.FFXIV.Client.Game.
*ActorDrawState(actor) ^= DrawState.Invisibility;
}

public static unsafe void CachePlayerList(uint playerObjectId, Queue<string> queue, Span<Pointer<BattleChara>> CharacterSpan)
public unsafe void CachePlayerList(uint playerObjectId, Queue<string> queue, Span<Pointer<BattleChara>> CharacterSpan)
{
foreach (var chara in CharacterSpan)
{
Expand All @@ -93,4 +87,13 @@ public static unsafe void CachePlayerList(uint playerObjectId, Queue<string> que
}
}
}

public unsafe bool PetVisible(BattleChara* pet)
{
if (pet is null || pet->Character.GameObject.GetDrawObject() is null)
{
return false;
}
return pet->Character.GameObject.GetDrawObject()->IsVisible;
}
}
14 changes: 8 additions & 6 deletions PetScale/PetScale.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Linq;
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;
using System.Collections.Generic;

Expand All @@ -11,7 +10,7 @@
using Dalamud.Game.Command;
using Dalamud.Plugin.Services;
using Dalamud.Interface.Windowing;
using Lumina.Excel.GeneratedSheets;
using Lumina.Excel.GeneratedSheets2;
using Dalamud.Game.ClientState.Objects.Enums;
using Dalamud.Game.ClientState.Objects.SubKinds;
using BattleChara = FFXIVClientStructs.FFXIV.Client.Game.Character.BattleChara;
Expand All @@ -38,7 +37,7 @@ public sealed class PetScale : IDalamudPlugin
public readonly IClientState clientState;

private readonly StringComparison ordinalComparison = StringComparison.Ordinal;
private readonly Dictionary<Pointer<BattleChara>, (Pointer<Character> character, bool petSet)> activePetDictionary = new();
private readonly Dictionary<Pointer<BattleChara>, (Pointer<Character> character, bool petSet)> activePetDictionary = [];
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
Expand Down Expand Up @@ -85,6 +84,7 @@ public PetScale(DalamudPluginInterface _pluginInterface,
#if DEBUG
DevWindow = new DevWindow(log, pluginInterface);
WindowSystem.AddWindow(DevWindow);
dictionaryExpirationTime = TimeSpan.FromMilliseconds(20);
#endif

WindowSystem.AddWindow(ConfigWindow);
Expand Down Expand Up @@ -261,7 +261,9 @@ private unsafe void ParseDictionary(PlayerCharacter player)
continue;
}
#if DEBUG
DevWindow.Print(petName + ": " + pet->Character.CharacterData.ModelCharaId + " owned by " + characterName);
DevWindow.Print(petName + ": " + pet->Character.CharacterData.ModelCharaId + " owned by " + characterName + " size " + pet->Character.GameObject.Scale);
DevWindow.Print("Visibility: " + pet->Character.GameObject.GetDrawObject()->IsVisible);
DevWindow.Print("RenderFlags: " + pet->Character.GameObject.RenderFlags);
#endif
if (ParseStruct(pet, characterName, petName, pet->Character.CharacterData.ModelCharaId, character->GameObject.ObjectID == player.ObjectId))
{
Expand Down Expand Up @@ -331,7 +333,7 @@ private void RefreshCache(string playerName, uint playerObjectId)
players.Clear();
players.Enqueue(playerName);
players.Enqueue(Others);
Utilities.CachePlayerList(playerObjectId, players, BattleCharaSpan);
utilities.CachePlayerList(playerObjectId, players, BattleCharaSpan);
}

private unsafe void SetScale(BattleChara* pet, PetStruct userData, string petName)
Expand All @@ -350,7 +352,7 @@ private unsafe void SetScale(BattleChara* pet, PetStruct userData, string petNam
private unsafe void DevWindowThings()
{
DevWindow.IsOpen = true;
DevWindow.Print("Actor pair count: " + activePetDictionary.Count.ToString(cultureInfo));
DevWindow.Print("Actor pair count: " + activePetDictionary.Count.ToString());
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions PetScale/PetScale.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<Platforms>x64</Platforms>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand All @@ -39,7 +39,7 @@

<ItemGroup>
<PackageReference Include="DalamudPackager" Version="2.1.12" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.141">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.145">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 1 addition & 3 deletions PetScale/Structs/PetStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ public PetStruct()
}

public readonly bool IsDefault()
{
return CharacterName.IsNullOrWhitespace() || CharacterName.Equals("Default", System.StringComparison.Ordinal);
}
=> CharacterName.IsNullOrWhitespace() || CharacterName.Equals("Default", System.StringComparison.Ordinal);
}
2 changes: 1 addition & 1 deletion PetScale/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public void Save(bool save)
if (tempEnumerable.Count() is not 0)
{
var tempList = tempEnumerable.ToList();
tempList.AddRange(petData.Except(tempList).OrderBy(item => item.CharacterName, StringComparer.Ordinal).ThenBy(item => item.PetID.ToString(), StringComparer.Ordinal).ToList());
tempList.AddRange([.. petData.Except(tempList).OrderBy(item => item.CharacterName, StringComparer.Ordinal).ThenBy(item => item.PetID.ToString(), StringComparer.Ordinal)]);
if (tempList.Count == petData.Count && petData.ToHashSet().SetEquals(tempList))
{
config.PetData = tempList;
Expand Down
2 changes: 1 addition & 1 deletion PetScale/Windows/DevWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace PetScale.Windows;

public class DevWindow : Window
{
private static readonly List<string> PrintLines = new();
private static readonly List<string> PrintLines = [];
private static readonly Queue<Pointer<GameObject>> RedrawObjects = new();
#pragma warning disable S4487
private readonly IPluginLog log;
Expand Down
8 changes: 4 additions & 4 deletions PetScale/packages.lock.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": 1,
"dependencies": {
"net7.0-windows7.0": {
"net8.0-windows7.0": {
"DalamudPackager": {
"type": "Direct",
"requested": "[2.1.12, )",
Expand All @@ -10,9 +10,9 @@
},
"Meziantou.Analyzer": {
"type": "Direct",
"requested": "[2.0.141, )",
"resolved": "2.0.141",
"contentHash": "SnQIdjQeJnc/3xmKSNvIAFHAPwV+LTXuf4QesfdiNrntnL5oCoBIKAn6194JeMj/nWoCSwRnWJybTsZAS2u51Q=="
"requested": "[2.0.145, )",
"resolved": "2.0.145",
"contentHash": "BPgr0uG1JpLaDE/dIDK04cngtwzcwNEa3MJROJ+HjOal4E5EBjfTsyPOr/1yyjB2FTCmg2yIAqGteDqSykJwng=="
}
}
}
Expand Down

0 comments on commit 4ad3561

Please sign in to comment.