From 2bfcfee75346c972e0a1887ad81aba2b4d1a1987 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Sat, 11 Feb 2023 21:10:45 +0100 Subject: [PATCH] Code cleanup bits --- Pal.Client/Configuration.cs | 10 ++--- Pal.Client/Extensions/ChatExtensions.cs | 11 +++--- Pal.Client/Extensions/GuidExtensions.cs | 13 ++++++ Pal.Client/LocalState.cs | 3 +- Pal.Client/Net/GrpcLogger.cs | 2 +- Pal.Client/Net/JwtClaims.cs | 2 +- Pal.Client/Net/RemoteApi.AccountService.cs | 4 +- Pal.Client/Net/RemoteApi.PalaceService.cs | 4 +- Pal.Client/Net/RemoteApi.Utils.cs | 13 +++--- Pal.Client/Net/RemoteApi.cs | 5 ++- Pal.Client/Palace Pal.json | 2 +- Pal.Client/Plugin.cs | 46 +++++++++++----------- Pal.Client/Rendering/MarkerConfig.cs | 8 ++-- Pal.Client/Rendering/SimpleRenderer.cs | 32 +++++++-------- Pal.Client/Rendering/SplatoonRenderer.cs | 16 ++++---- Pal.Client/Scheduled/QueuedImport.cs | 14 +++---- Pal.Client/Scheduled/QueuedSyncResponse.cs | 8 ++-- Pal.Client/Service.cs | 8 ++-- Pal.Client/Windows/ConfigWindow.cs | 16 ++++---- Pal.Client/Windows/StatisticsWindow.cs | 2 +- Pal.Common/ExportConfig.cs | 2 +- Pal.Common/PalaceMath.cs | 2 +- 22 files changed, 120 insertions(+), 103 deletions(-) create mode 100644 Pal.Client/Extensions/GuidExtensions.cs diff --git a/Pal.Client/Configuration.cs b/Pal.Client/Configuration.cs index af3133a..b1a88a7 100644 --- a/Pal.Client/Configuration.cs +++ b/Pal.Client/Configuration.cs @@ -38,15 +38,15 @@ namespace Pal.Client public List ImportHistory { get; set; } = new(); public bool ShowTraps { get; set; } = true; - public Vector4 TrapColor { get; set; } = new Vector4(1, 0, 0, 0.4f); + public Vector4 TrapColor { get; set; } = new(1, 0, 0, 0.4f); public bool OnlyVisibleTrapsAfterPomander { get; set; } = true; public bool ShowHoard { get; set; } = true; - public Vector4 HoardColor { get; set; } = new Vector4(0, 1, 1, 0.4f); + public Vector4 HoardColor { get; set; } = new(0, 1, 1, 0.4f); public bool OnlyVisibleHoardAfterPomander { get; set; } = true; - public bool ShowSilverCoffers { get; set; } = false; - public Vector4 SilverCofferColor { get; set; } = new Vector4(1, 1, 1, 0.4f); + public bool ShowSilverCoffers { get; set; } + public Vector4 SilverCofferColor { get; set; } = new(1, 1, 1, 0.4f); public bool FillSilverCoffers { get; set; } = true; /// @@ -120,7 +120,7 @@ namespace Pal.Client // Only notify offline users - we can just re-download the backup markers from the server seamlessly. if (Mode == EMode.Offline && changedAnyFile) { - new TickScheduler(delegate + _ = new TickScheduler(delegate { Service.Chat.PalError("Due to a bug, some coffers were accidentally saved as traps. To fix the related display issue, locally cached data was cleaned up."); Service.Chat.PrintError($"If you have any backup tools installed, please restore the contents of '{Service.PluginInterface.GetPluginConfigDirectory()}' to any backup from February 2, 2023 or before."); diff --git a/Pal.Client/Extensions/ChatExtensions.cs b/Pal.Client/Extensions/ChatExtensions.cs index 6d7eeda..31b39ef 100644 --- a/Pal.Client/Extensions/ChatExtensions.cs +++ b/Pal.Client/Extensions/ChatExtensions.cs @@ -1,10 +1,11 @@ using Dalamud.Game.Gui; using Pal.Client.Properties; -namespace Pal.Client.Extensions; - -public static class ChatExtensions +namespace Pal.Client.Extensions { - public static void PalError(this ChatGui chat, string e) - => chat.PrintError($"[{Localization.Palace_Pal}] {e}"); + public static class ChatExtensions + { + public static void PalError(this ChatGui chat, string e) + => chat.PrintError($"[{Localization.Palace_Pal}] {e}"); + } } diff --git a/Pal.Client/Extensions/GuidExtensions.cs b/Pal.Client/Extensions/GuidExtensions.cs new file mode 100644 index 0000000..b80a6bb --- /dev/null +++ b/Pal.Client/Extensions/GuidExtensions.cs @@ -0,0 +1,13 @@ +using System; + +namespace Pal.Client.Extensions +{ + public static class GuidExtensions + { + public static string ToPartialId(this Guid g, int length = 13) + => g.ToString().ToPartialId(); + + public static string ToPartialId(this string s, int length = 13) + => s.PadRight(length + 1).Substring(0, length); + } +} diff --git a/Pal.Client/LocalState.cs b/Pal.Client/LocalState.cs index 4cc8b38..4044768 100644 --- a/Pal.Client/LocalState.cs +++ b/Pal.Client/LocalState.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; +using Pal.Client.Extensions; namespace Pal.Client { @@ -72,7 +73,7 @@ namespace Pal.Client if (version <= 3) { foreach (var marker in localState.Markers) - marker.RemoteSeenOn = marker.RemoteSeenOn.Select(x => x.PadRight(14).Substring(0, 13)).ToList(); + marker.RemoteSeenOn = marker.RemoteSeenOn.Select(x => x.ToPartialId()).ToList(); } if (version < CurrentVersion) diff --git a/Pal.Client/Net/GrpcLogger.cs b/Pal.Client/Net/GrpcLogger.cs index 07f0ccf..9c12149 100644 --- a/Pal.Client/Net/GrpcLogger.cs +++ b/Pal.Client/Net/GrpcLogger.cs @@ -65,7 +65,7 @@ namespace Pal.Client.Net private class NullScope : IDisposable { - public static NullScope Instance { get; } = new NullScope(); + public static NullScope Instance { get; } = new(); private NullScope() { diff --git a/Pal.Client/Net/JwtClaims.cs b/Pal.Client/Net/JwtClaims.cs index 538d87d..3247bba 100644 --- a/Pal.Client/Net/JwtClaims.cs +++ b/Pal.Client/Net/JwtClaims.cs @@ -48,7 +48,7 @@ namespace Pal.Client.Net internal class JwtRoleConverter : JsonConverter> { - public override List? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override List Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.String) return new List { reader.GetString() ?? throw new JsonException("no value present") }; diff --git a/Pal.Client/Net/RemoteApi.AccountService.cs b/Pal.Client/Net/RemoteApi.AccountService.cs index dba9bec..f0fd67c 100644 --- a/Pal.Client/Net/RemoteApi.AccountService.cs +++ b/Pal.Client/Net/RemoteApi.AccountService.cs @@ -88,7 +88,7 @@ namespace Pal.Client.Net var account = Account; if (account != null) { - account.CachedRoles = _loginInfo.Claims?.Roles?.ToList() ?? new List(); + account.CachedRoles = _loginInfo.Claims?.Roles.ToList() ?? new List(); Service.Configuration.Save(); } } @@ -156,7 +156,7 @@ namespace Pal.Client.Net { IsLoggedIn = true; AuthToken = authToken; - Claims = JwtClaims.FromAuthToken(authToken!); + Claims = JwtClaims.FromAuthToken(authToken); } else IsLoggedIn = false; diff --git a/Pal.Client/Net/RemoteApi.PalaceService.cs b/Pal.Client/Net/RemoteApi.PalaceService.cs index cd38321..cee5337 100644 --- a/Pal.Client/Net/RemoteApi.PalaceService.cs +++ b/Pal.Client/Net/RemoteApi.PalaceService.cs @@ -18,7 +18,7 @@ namespace Pal.Client.Net var palaceClient = new PalaceService.PalaceServiceClient(_channel); var downloadReply = await palaceClient.DownloadFloorsAsync(new DownloadFloorsRequest { TerritoryType = territoryId }, headers: AuthorizedHeaders(), cancellationToken: cancellationToken); - return (downloadReply.Success, downloadReply.Objects.Select(o => CreateMarkerFromNetworkObject(o)).ToList()); + return (downloadReply.Success, downloadReply.Objects.Select(CreateMarkerFromNetworkObject).ToList()); } public async Task<(bool, List)> UploadMarker(ushort territoryType, IList markers, CancellationToken cancellationToken = default) @@ -42,7 +42,7 @@ namespace Pal.Client.Net Z = m.Position.Z })); var uploadReply = await palaceClient.UploadFloorsAsync(uploadRequest, headers: AuthorizedHeaders(), cancellationToken: cancellationToken); - return (uploadReply.Success, uploadReply.Objects.Select(o => CreateMarkerFromNetworkObject(o)).ToList()); + return (uploadReply.Success, uploadReply.Objects.Select(CreateMarkerFromNetworkObject).ToList()); } public async Task MarkAsSeen(ushort territoryType, IList markers, CancellationToken cancellationToken = default) diff --git a/Pal.Client/Net/RemoteApi.Utils.cs b/Pal.Client/Net/RemoteApi.Utils.cs index fb3c965..7dff878 100644 --- a/Pal.Client/Net/RemoteApi.Utils.cs +++ b/Pal.Client/Net/RemoteApi.Utils.cs @@ -1,4 +1,5 @@ -using Dalamud.Logging; +using System; +using Dalamud.Logging; using Grpc.Core; using System.Net.Security; using System.Security.Cryptography.X509Certificates; @@ -7,14 +8,14 @@ namespace Pal.Client.Net { internal partial class RemoteApi { - private Metadata UnauthorizedHeaders() => new Metadata + private Metadata UnauthorizedHeaders() => new() { { "User-Agent", _userAgent }, }; - private Metadata AuthorizedHeaders() => new Metadata + private Metadata AuthorizedHeaders() => new() { - { "Authorization", $"Bearer {_loginInfo?.AuthToken}" }, + { "Authorization", $"Bearer {_loginInfo.AuthToken}" }, { "User-Agent", _userAgent }, }; @@ -34,7 +35,9 @@ namespace Pal.Client.Net return null; var bytes = new byte[manifestResourceStream.Length]; - manifestResourceStream.Read(bytes, 0, bytes.Length); + int read = manifestResourceStream.Read(bytes, 0, bytes.Length); + if (read != bytes.Length) + throw new InvalidOperationException(); var certificate = new X509Certificate2(bytes, pass, X509KeyStorageFlags.DefaultKeySet); PluginLog.Debug($"Using client certificate {certificate.GetCertHashString()}"); diff --git a/Pal.Client/Net/RemoteApi.cs b/Pal.Client/Net/RemoteApi.cs index 713c771..a25afbf 100644 --- a/Pal.Client/Net/RemoteApi.cs +++ b/Pal.Client/Net/RemoteApi.cs @@ -2,6 +2,7 @@ using Grpc.Net.Client; using Microsoft.Extensions.Logging; using System; +using Pal.Client.Extensions; namespace Pal.Client.Net { @@ -18,7 +19,7 @@ namespace Pal.Client.Net private GrpcChannel? _channel; private LoginInfo _loginInfo = new(null); - private bool _warnedAboutUpgrade = false; + private bool _warnedAboutUpgrade; public Configuration.AccountInfo? Account { @@ -34,7 +35,7 @@ namespace Pal.Client.Net public Guid? AccountId => Account?.Id; - public string? PartialAccountId => Account?.Id?.ToString()?.PadRight(14).Substring(0, 13); + public string? PartialAccountId => Account?.Id?.ToPartialId(); private string FormattedPartialAccountId => PartialAccountId ?? "[no account id]"; diff --git a/Pal.Client/Palace Pal.json b/Pal.Client/Palace Pal.json index a252711..77ce336 100644 --- a/Pal.Client/Palace Pal.json +++ b/Pal.Client/Palace Pal.json @@ -2,7 +2,7 @@ "Name": "Palace Pal", "Author": "Liza Carvelli", "Punchline": "Shows possible trap & hoard coffer locations in Palace of the Dead & Heaven on High.", - "Description": "Shows possible trap & hoard coffer locations in Palace of the Dead & Heaven on High. Requires Splatoon to be installed.", + "Description": "Shows possible trap & hoard coffer locations in Palace of the Dead & Heaven on High.\n\nThe default configuration requires Splatoon to be installed. If you do not wish to install Splatoon, you can switch to the experimental 'Simple' renderer in the configuration.", "RepoUrl": "https://github.com/carvelli/PalacePal", "IconUrl": "https://raw.githubusercontent.com/carvelli/Dalamud-Plugins/master/dist/Palace Pal.png", "Tags": [ "potd", "palace", "hoh", "splatoon" ] diff --git a/Pal.Client/Plugin.cs b/Pal.Client/Plugin.cs index 6e3075c..4e5e893 100644 --- a/Pal.Client/Plugin.cs +++ b/Pal.Client/Plugin.cs @@ -39,10 +39,10 @@ namespace Pal.Client internal ConcurrentDictionary FloorMarkers { get; } = new(); internal ConcurrentBag EphemeralMarkers { get; set; } = new(); internal ushort LastTerritory { get; set; } - public SyncState TerritorySyncState { get; set; } - public PomanderState PomanderOfSight { get; set; } = PomanderState.Inactive; - public PomanderState PomanderOfIntuition { get; set; } = PomanderState.Inactive; - public string? DebugMessage { get; set; } + internal SyncState TerritorySyncState { get; set; } + internal PomanderState PomanderOfSight { get; private set; } = PomanderState.Inactive; + internal PomanderState PomanderOfIntuition { get; private set; } = PomanderState.Inactive; + internal string? DebugMessage { get; set; } internal Queue EarlyEventQueue { get; } = new(); internal Queue LateEventQueue { get; } = new(); internal ConcurrentQueue NextUpdateObjects { get; } = new(); @@ -98,7 +98,7 @@ namespace Pal.Client } pluginInterface.UiBuilder.Draw += Draw; - pluginInterface.UiBuilder.OpenConfigUi += OnOpenConfigUi; + pluginInterface.UiBuilder.OpenConfigUi += OpenConfigUi; pluginInterface.LanguageChanged += LanguageChanged; Service.Framework.Update += OnFrameworkUpdate; Service.Chat.ChatMessage += OnChatMessage; @@ -110,7 +110,7 @@ namespace Pal.Client ReloadLanguageStrings(); } - public void OnOpenConfigUi() + private void OpenConfigUi() { Window? configWindow; if (Service.Configuration.FirstUse) @@ -162,7 +162,7 @@ namespace Pal.Client break; case "near": - DebugNearest(m => true); + DebugNearest(_ => true); break; case "tnear": @@ -191,7 +191,7 @@ namespace Pal.Client Service.CommandManager.RemoveHandler("/pal"); Service.PluginInterface.UiBuilder.Draw -= Draw; - Service.PluginInterface.UiBuilder.OpenConfigUi -= OnOpenConfigUi; + Service.PluginInterface.UiBuilder.OpenConfigUi -= OpenConfigUi; Service.PluginInterface.LanguageChanged -= LanguageChanged; Service.Framework.Update -= OnFrameworkUpdate; Service.Chat.ChatMessage -= OnChatMessage; @@ -246,14 +246,12 @@ namespace Pal.Client { PomanderOfIntuition = PomanderState.FoundOnCurrentFloor; } - else - return; } private void LanguageChanged(string langcode) { Localization.Culture = new CultureInfo(langcode); - Service.WindowSystem?.Windows.OfType().Each(w => w.LanguageChanged()); + Service.WindowSystem.Windows.OfType().Each(w => w.LanguageChanged()); } private void OnFrameworkUpdate(Framework framework) @@ -267,7 +265,7 @@ namespace Pal.Client bool saveMarkers = false; while (EarlyEventQueue.TryDequeue(out IQueueOnFrameworkThread? queued)) - queued?.Run(this, ref recreateLayout, ref saveMarkers); + queued.Run(this, ref recreateLayout, ref saveMarkers); if (LastTerritory != Service.ClientState.TerritoryType) { @@ -294,7 +292,7 @@ namespace Pal.Client } while (LateEventQueue.TryDequeue(out IQueueOnFrameworkThread? queued)) - queued?.Run(this, ref recreateLayout, ref saveMarkers); + queued.Run(this, ref recreateLayout, ref saveMarkers); var currentFloor = GetFloorMarkers(LastTerritory); @@ -334,7 +332,7 @@ namespace Pal.Client // This requires you to have seen a trap/hoard marker once per floor to synchronize this for older local states, // markers discovered afterwards are automatically marked seen. - if (partialAccountId != null && knownMarker.NetworkId != null && !knownMarker.RemoteSeenRequested && !knownMarker.RemoteSeenOn.Contains(partialAccountId)) + if (partialAccountId != null && knownMarker is { NetworkId: { }, RemoteSeenRequested: false } && !knownMarker.RemoteSeenOn.Contains(partialAccountId)) updateSeenMarkers = true; continue; @@ -372,7 +370,7 @@ namespace Pal.Client if (updateSeenMarkers && partialAccountId != null) { - var markersToUpdate = currentFloorMarkers.Where(x => x.Seen && x.NetworkId != null && !x.RemoteSeenRequested && !x.RemoteSeenOn.Contains(partialAccountId)).ToList(); + var markersToUpdate = currentFloorMarkers.Where(x => x is { Seen: true, NetworkId: { }, RemoteSeenRequested: false } && !x.RemoteSeenOn.Contains(partialAccountId)).ToList(); foreach (var marker in markersToUpdate) marker.RemoteSeenRequested = true; Task.Run(async () => await SyncSeenMarkersForTerritory(LastTerritory, markersToUpdate)); @@ -401,7 +399,7 @@ namespace Pal.Client List elements = new(); foreach (var marker in currentFloorMarkers) { - if (marker.Seen || config.Mode == Configuration.EMode.Online || (marker.WasImported && marker.Imports.Count > 0)) + if (marker.Seen || config.Mode == Configuration.EMode.Online || marker is { WasImported: true, Imports.Count: > 0 }) { if (marker.Type == Marker.EType.Trap && config.ShowTraps) { @@ -583,12 +581,12 @@ namespace Pal.Client var nearbyMarkers = state.Markers .Where(m => predicate(m)) .Where(m => m.RenderElement != null && m.RenderElement.Color != ColorInvisible) - .Select(m => new { m = m, distance = (playerPosition - m.Position)?.Length() ?? float.MaxValue }) + .Select(m => new { m, distance = (playerPosition - m.Position)?.Length() ?? float.MaxValue }) .OrderBy(m => m.distance) .Take(5) .ToList(); foreach (var nearbyMarker in nearbyMarkers) - Service.Chat.Print($"{nearbyMarker.distance:F2} - {nearbyMarker.m.Type} {nearbyMarker.m.NetworkId?.ToString()?.Substring(0, 8)} - {nearbyMarker.m.Position}"); + Service.Chat.Print($"{nearbyMarker.distance:F2} - {nearbyMarker.m.Type} {nearbyMarker.m.NetworkId?.ToPartialId(length: 8)} - {nearbyMarker.m.Position}"); } #endregion @@ -690,12 +688,12 @@ namespace Pal.Client private class LocalizedChatMessages { - public string MapRevealed { get; set; } = "???"; //"The map for this floor has been revealed!"; - public string AllTrapsRemoved { get; set; } = "???"; // "All the traps on this floor have disappeared!"; - public string HoardOnCurrentFloor { get; set; } = "???"; // "You sense the Accursed Hoard calling you..."; - public string HoardNotOnCurrentFloor { get; set; } = "???"; // "You do not sense the call of the Accursed Hoard on this floor..."; - public string HoardCofferOpened { get; set; } = "???"; // "You discover a piece of the Accursed Hoard!"; - public Regex FloorChanged { get; set; } = new Regex(@"This isn't a game message, but will be replaced"); // new Regex(@"^Floor (\d+)$"); + public string MapRevealed { get; init; } = "???"; //"The map for this floor has been revealed!"; + public string AllTrapsRemoved { get; init; } = "???"; // "All the traps on this floor have disappeared!"; + public string HoardOnCurrentFloor { get; init; } = "???"; // "You sense the Accursed Hoard calling you..."; + public string HoardNotOnCurrentFloor { get; init; } = "???"; // "You do not sense the call of the Accursed Hoard on this floor..."; + public string HoardCofferOpened { get; init; } = "???"; // "You discover a piece of the Accursed Hoard!"; + public Regex FloorChanged { get; init; } = new(@"This isn't a game message, but will be replaced"); // new Regex(@"^Floor (\d+)$"); } } } diff --git a/Pal.Client/Rendering/MarkerConfig.cs b/Pal.Client/Rendering/MarkerConfig.cs index 6b465f1..58d3642 100644 --- a/Pal.Client/Rendering/MarkerConfig.cs +++ b/Pal.Client/Rendering/MarkerConfig.cs @@ -4,17 +4,17 @@ namespace Pal.Client.Rendering { internal class MarkerConfig { - - private readonly static Dictionary _markerConfig = new Dictionary + private static readonly MarkerConfig EmptyConfig = new(); + private static readonly Dictionary MarkerConfigs = new() { { Marker.EType.Trap, new MarkerConfig { Radius = 1.7f } }, { Marker.EType.Hoard, new MarkerConfig { Radius = 1.7f, OffsetY = -0.03f } }, { Marker.EType.SilverCoffer, new MarkerConfig { Radius = 1f } }, }; - public float OffsetY { get; set; } = 0; + public float OffsetY { get; set; } public float Radius { get; set; } = 0.25f; - public static MarkerConfig ForType(Marker.EType type) => _markerConfig[type] ?? new MarkerConfig(); + public static MarkerConfig ForType(Marker.EType type) => MarkerConfigs.GetValueOrDefault(type, EmptyConfig); } } diff --git a/Pal.Client/Rendering/SimpleRenderer.cs b/Pal.Client/Rendering/SimpleRenderer.cs index 8b7c26f..c2c047b 100644 --- a/Pal.Client/Rendering/SimpleRenderer.cs +++ b/Pal.Client/Rendering/SimpleRenderer.cs @@ -22,11 +22,11 @@ namespace Pal.Client.Rendering /// internal class SimpleRenderer : IRenderer, IDisposable { - private ConcurrentDictionary layers = new(); + private readonly ConcurrentDictionary _layers = new(); public void SetLayer(ELayer layer, IReadOnlyList elements) { - layers[layer] = new SimpleLayer + _layers[layer] = new SimpleLayer { TerritoryType = Service.ClientState.TerritoryType, Elements = elements.Cast().ToList() @@ -35,7 +35,7 @@ namespace Pal.Client.Rendering public void ResetLayer(ELayer layer) { - if (layers.Remove(layer, out var l)) + if (_layers.Remove(layer, out var l)) l.Dispose(); } @@ -54,7 +54,7 @@ namespace Pal.Client.Rendering public void DrawLayers() { - if (layers.Count == 0) + if (_layers.Count == 0) return; ImGuiHelpers.ForceNextWindowMainViewport(); @@ -65,10 +65,10 @@ namespace Pal.Client.Rendering { ushort territoryType = Service.ClientState.TerritoryType; - foreach (var layer in layers.Values.Where(l => l.TerritoryType == territoryType)) + foreach (var layer in _layers.Values.Where(l => l.TerritoryType == territoryType)) layer.Draw(); - foreach (var key in layers.Where(l => l.Value.TerritoryType != territoryType).Select(l => l.Key).ToList()) + foreach (var key in _layers.Where(l => l.Value.TerritoryType != territoryType).Select(l => l.Key).ToList()) ResetLayer(key); ImGui.End(); @@ -78,14 +78,14 @@ namespace Pal.Client.Rendering public void Dispose() { - foreach (var l in layers.Values) + foreach (var l in _layers.Values) l.Dispose(); } public class SimpleLayer : IDisposable { public required ushort TerritoryType { get; init; } - public required IReadOnlyList Elements { get; set; } + public required IReadOnlyList Elements { get; init; } public void Draw() { @@ -102,14 +102,14 @@ namespace Pal.Client.Rendering public class SimpleElement : IRenderElement { - private const int segmentCount = 20; + private const int SegmentCount = 20; public bool IsValid { get; set; } = true; - public required Marker.EType Type { get; set; } - public required Vector3 Position { get; set; } + public required Marker.EType Type { get; init; } + public required Vector3 Position { get; init; } public required uint Color { get; set; } - public required float Radius { get; set; } - public required bool Fill { get; set; } + public required float Radius { get; init; } + public required bool Fill { get; init; } public void Draw() { @@ -136,12 +136,12 @@ namespace Pal.Client.Rendering } bool onScreen = false; - for (int index = 0; index < 2 * segmentCount; ++index) + for (int index = 0; index < 2 * SegmentCount; ++index) { onScreen |= Service.GameGui.WorldToScreen(new Vector3( - Position.X + Radius * (float)Math.Sin(Math.PI / segmentCount * index), + Position.X + Radius * (float)Math.Sin(Math.PI / SegmentCount * index), Position.Y, - Position.Z + Radius * (float)Math.Cos(Math.PI / segmentCount * index)), + Position.Z + Radius * (float)Math.Cos(Math.PI / SegmentCount * index)), out Vector2 vector2); ImGui.GetWindowDrawList().PathLineTo(vector2); diff --git a/Pal.Client/Rendering/SplatoonRenderer.cs b/Pal.Client/Rendering/SplatoonRenderer.cs index c4969e3..2c07578 100644 --- a/Pal.Client/Rendering/SplatoonRenderer.cs +++ b/Pal.Client/Rendering/SplatoonRenderer.cs @@ -18,8 +18,8 @@ namespace Pal.Client.Rendering { internal class SplatoonRenderer : IRenderer, IDrawDebugItems, IDisposable { - private const long ON_TERRITORY_CHANGE = -2; - private bool IsDisposed { get; set; } = false; + private const long OnTerritoryChange = -2; + private bool IsDisposed { get; set; } public SplatoonRenderer(DalamudPluginInterface pluginInterface, IDalamudPlugin plugin) { @@ -29,11 +29,11 @@ namespace Pal.Client.Rendering public void SetLayer(ELayer layer, IReadOnlyList elements) { // we need to delay this, as the current framework update could be before splatoon's, in which case it would immediately delete the layout - new TickScheduler(delegate + _ = new TickScheduler(delegate { try { - Splatoon.AddDynamicElements(ToLayerName(layer), elements.Cast().Select(x => x.Delegate).ToArray(), new long[] { Environment.TickCount64 + 60 * 60 * 1000, ON_TERRITORY_CHANGE }); + Splatoon.AddDynamicElements(ToLayerName(layer), elements.Cast().Select(x => x.Delegate).ToArray(), new[] { Environment.TickCount64 + 60 * 60 * 1000, OnTerritoryChange }); } catch (Exception e) { @@ -91,7 +91,7 @@ namespace Pal.Client.Rendering CreateElement(Marker.EType.Hoard, pos.Value, ImGui.ColorConvertFloat4ToU32(hoardColor)), }; - if (!Splatoon.AddDynamicElements("PalacePal.Test", elements.Cast().Select(x => x.Delegate).ToArray(), new long[] { Environment.TickCount64 + 10000 })) + if (!Splatoon.AddDynamicElements("PalacePal.Test", elements.Cast().Select(x => x.Delegate).ToArray(), new[] { Environment.TickCount64 + 10000 })) { Service.Chat.PrintError("Could not draw markers :("); } @@ -134,17 +134,17 @@ namespace Pal.Client.Rendering public class SplatoonElement : IRenderElement { - private readonly SplatoonRenderer renderer; + private readonly SplatoonRenderer _renderer; public SplatoonElement(SplatoonRenderer renderer, Element element) { - this.renderer = renderer; + _renderer = renderer; Delegate = element; } public Element Delegate { get; } - public bool IsValid => !renderer.IsDisposed && Delegate.IsValid(); + public bool IsValid => !_renderer.IsDisposed && Delegate.IsValid(); public uint Color { get => Delegate.color; diff --git a/Pal.Client/Scheduled/QueuedImport.cs b/Pal.Client/Scheduled/QueuedImport.cs index 3bc649e..890de80 100644 --- a/Pal.Client/Scheduled/QueuedImport.cs +++ b/Pal.Client/Scheduled/QueuedImport.cs @@ -15,13 +15,13 @@ namespace Pal.Client.Scheduled { private readonly ExportRoot _export; private Guid _exportId; - private int importedTraps; - private int importedHoardCoffers; + private int _importedTraps; + private int _importedHoardCoffers; public QueuedImport(string sourcePath) { - using (var input = File.OpenRead(sourcePath)) - _export = ExportRoot.Parser.ParseFrom(input); + using var input = File.OpenRead(sourcePath); + _export = ExportRoot.Parser.ParseFrom(input); } public void Run(Plugin plugin, ref bool recreateLayout, ref bool saveMarkers) @@ -58,7 +58,7 @@ namespace Pal.Client.Scheduled recreateLayout = true; saveMarkers = true; - Service.Chat.Print(string.Format(Localization.ImportCompleteStatistics, importedTraps, importedHoardCoffers)); + Service.Chat.Print(string.Format(Localization.ImportCompleteStatistics, _importedTraps, _importedHoardCoffers)); } catch (Exception e) { @@ -103,9 +103,9 @@ namespace Pal.Client.Scheduled localMarker = remoteMarker; if (localMarker.Type == Marker.EType.Trap) - importedTraps++; + _importedTraps++; else if (localMarker.Type == Marker.EType.Hoard) - importedHoardCoffers++; + _importedHoardCoffers++; } remoteMarker.Imports.Add(_exportId); diff --git a/Pal.Client/Scheduled/QueuedSyncResponse.cs b/Pal.Client/Scheduled/QueuedSyncResponse.cs index a2539a7..871ed47 100644 --- a/Pal.Client/Scheduled/QueuedSyncResponse.cs +++ b/Pal.Client/Scheduled/QueuedSyncResponse.cs @@ -9,10 +9,10 @@ namespace Pal.Client.Scheduled { internal class QueuedSyncResponse : IQueueOnFrameworkThread { - public SyncType Type { get; set; } - public ushort TerritoryType { get; set; } - public bool Success { get; set; } - public List Markers { get; set; } = new(); + public required SyncType Type { get; init; } + public required ushort TerritoryType { get; init; } + public required bool Success { get; init; } + public required List Markers { get; init; } public void Run(Plugin plugin, ref bool recreateLayout, ref bool saveMarkers) { diff --git a/Pal.Client/Service.cs b/Pal.Client/Service.cs index bb04800..0b0d90d 100644 --- a/Pal.Client/Service.cs +++ b/Pal.Client/Service.cs @@ -24,10 +24,10 @@ namespace Pal.Client [PluginService] public static DataManager DataManager { get; set; } = null!; [PluginService] public static GameGui GameGui { get; set; } = null!; - public static Plugin Plugin { get; set; } = null!; - public static WindowSystem WindowSystem { get; set; } = new(typeof(Service).AssemblyQualifiedName); - internal static RemoteApi RemoteApi { get; set; } = new RemoteApi(); - public static Configuration Configuration { get; set; } = null!; + internal static Plugin Plugin { get; set; } = null!; + internal static WindowSystem WindowSystem { get; } = new(typeof(Service).AssemblyQualifiedName); + internal static RemoteApi RemoteApi { get; } = new(); + internal static Configuration Configuration { get; set; } = null!; internal static Hooks Hooks { get; set; } = null!; } } diff --git a/Pal.Client/Windows/ConfigWindow.cs b/Pal.Client/Windows/ConfigWindow.cs index 614123e..efd79c6 100644 --- a/Pal.Client/Windows/ConfigWindow.cs +++ b/Pal.Client/Windows/ConfigWindow.cs @@ -22,7 +22,7 @@ using Pal.Client.Properties; namespace Pal.Client.Windows { - internal class ConfigWindow : Window + internal class ConfigWindow : Window, ILanguageChanged { private const string WindowId = "###PalPalaceConfig"; private int _mode; @@ -43,8 +43,8 @@ namespace Pal.Client.Windows private string _saveExportPath = string.Empty; private string? _openImportDialogStartPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); private string? _saveExportDialogStartPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); - private FileDialogManager _importDialog; - private FileDialogManager _exportDialog; + private readonly FileDialogManager _importDialog; + private readonly FileDialogManager _exportDialog; public ConfigWindow() : base(WindowId) { @@ -358,7 +358,7 @@ namespace Pal.Client.Windows /// /// None of the default BeginTabItem methods allow using flags without making the tab have a close button for some reason. /// - private unsafe static bool BeginTabItemEx(string label, ImGuiTabItemFlags flags) + private static unsafe bool BeginTabItemEx(string label, ImGuiTabItemFlags flags) { int labelLength = Encoding.UTF8.GetByteCount(label); byte* labelPtr = stackalloc byte[labelLength + 1]; @@ -392,17 +392,17 @@ namespace Pal.Client.Windows }); } - internal void DoImport(string sourcePath) + private void DoImport(string sourcePath) { Service.Plugin.EarlyEventQueue.Enqueue(new QueuedImport(sourcePath)); } - internal void UndoImport(Guid importId) + private void UndoImport(Guid importId) { Service.Plugin.EarlyEventQueue.Enqueue(new QueuedUndoImport(importId)); } - internal void DoExport(string destinationPath) + private void DoExport(string destinationPath) { Task.Run(async () => { @@ -411,7 +411,7 @@ namespace Pal.Client.Windows (bool success, ExportRoot export) = await Service.RemoteApi.DoExport(); if (success) { - using var output = File.Create(destinationPath); + await using var output = File.Create(destinationPath); export.WriteTo(output); Service.Chat.Print($"Export saved as {destinationPath}."); diff --git a/Pal.Client/Windows/StatisticsWindow.cs b/Pal.Client/Windows/StatisticsWindow.cs index e9545d8..7aed58e 100644 --- a/Pal.Client/Windows/StatisticsWindow.cs +++ b/Pal.Client/Windows/StatisticsWindow.cs @@ -17,7 +17,7 @@ namespace Pal.Client.Windows internal class StatisticsWindow : Window, ILanguageChanged { private const string WindowId = "###PalacePalStats"; - private SortedDictionary _territoryStatistics = new(); + private readonly SortedDictionary _territoryStatistics = new(); public StatisticsWindow() : base(WindowId) { diff --git a/Pal.Common/ExportConfig.cs b/Pal.Common/ExportConfig.cs index 4df0263..e664ee2 100644 --- a/Pal.Common/ExportConfig.cs +++ b/Pal.Common/ExportConfig.cs @@ -8,6 +8,6 @@ namespace Pal.Common { public static class ExportConfig { - public static int ExportVersion { get; } = 1; + public static int ExportVersion => 1; } } diff --git a/Pal.Common/PalaceMath.cs b/Pal.Common/PalaceMath.cs index 3f685f0..19c890c 100644 --- a/Pal.Common/PalaceMath.cs +++ b/Pal.Common/PalaceMath.cs @@ -4,7 +4,7 @@ namespace Pal.Common { public class PalaceMath { - private static readonly Vector3 ScaleFactor = new Vector3(5); + private static readonly Vector3 ScaleFactor = new(5); public static bool IsNearlySamePosition(Vector3 a, Vector3 b) {