From 7ab274696c97020ead0bd944d346bef039df4e3d Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Sun, 30 Oct 2022 11:02:49 +0100 Subject: [PATCH] Enable nullable --- Pal.Client/Configuration.cs | 6 +++--- Pal.Client/LocalState.cs | 9 +++++--- Pal.Client/Marker.cs | 8 +++---- Pal.Client/Pal.Client.csproj | 3 ++- Pal.Client/Plugin.cs | 16 +++++++------- Pal.Client/RemoteApi.cs | 8 +++---- Pal.Client/Service.cs | 2 +- Pal.Client/WindowSystemExtensions.cs | 2 +- Pal.Client/Windows/ConfigWindow.cs | 29 ++++++++++++++------------ Pal.Client/Windows/StatisticsWindow.cs | 9 ++++++-- 10 files changed, 52 insertions(+), 40 deletions(-) diff --git a/Pal.Client/Configuration.cs b/Pal.Client/Configuration.cs index d3d2477..88147b5 100644 --- a/Pal.Client/Configuration.cs +++ b/Pal.Client/Configuration.cs @@ -10,8 +10,8 @@ namespace Pal.Client #region Saved configuration values public bool FirstUse { get; set; } = true; public EMode Mode { get; set; } = EMode.Offline; - public string DebugAccountId { get; set; } - public string AccountId { get; set; } + public string? DebugAccountId { get; set; } + public string? AccountId { get; set; } public bool ShowTraps { get; set; } = true; public Vector4 TrapColor { get; set; } = new Vector4(1, 0, 0, 0.4f); @@ -27,7 +27,7 @@ namespace Pal.Client #endregion public delegate void OnSaved(); - public event OnSaved Saved; + public event OnSaved? Saved; public void Save() { diff --git a/Pal.Client/LocalState.cs b/Pal.Client/LocalState.cs index 312fd2b..3f0c7aa 100644 --- a/Pal.Client/LocalState.cs +++ b/Pal.Client/LocalState.cs @@ -29,7 +29,7 @@ namespace Pal.Client Markers = new ConcurrentBag(Markers.Where(x => x.Seen)); } - public static LocalState Load(uint territoryType) + public static LocalState? Load(uint territoryType) { string path = GetSaveLocation(territoryType); if (!File.Exists(path)) @@ -46,12 +46,15 @@ namespace Pal.Client // v1 only had a list of markers, not a JSON object as root localState = new LocalState(territoryType) { - Markers = new ConcurrentBag(JsonSerializer.Deserialize>(content, _jsonSerializerOptions)), + Markers = new ConcurrentBag(JsonSerializer.Deserialize>(content, _jsonSerializerOptions) ?? new()), }; } else { var save = JsonSerializer.Deserialize(content, _jsonSerializerOptions); + if (save == null) + return null; + localState = new LocalState(territoryType) { Markers = new ConcurrentBag(save.Markers), @@ -85,7 +88,7 @@ namespace Pal.Client { foreach (ETerritoryType territory in typeof(ETerritoryType).GetEnumValues()) { - LocalState localState = Load((ushort)territory); + LocalState? localState = Load((ushort)territory); if (localState != null) localState.Save(); } diff --git a/Pal.Client/Marker.cs b/Pal.Client/Marker.cs index a7a71c7..867cb8c 100644 --- a/Pal.Client/Marker.cs +++ b/Pal.Client/Marker.cs @@ -16,7 +16,7 @@ namespace Pal.Client public bool RemoteSeen { get; set; } = false; [JsonIgnore] - public Element SplatoonElement { get; set; } + public Element? SplatoonElement { get; set; } public Marker(EType type, Vector3 position) { @@ -29,17 +29,17 @@ namespace Pal.Client return HashCode.Combine(Type, (int)Position.X, (int)Position.Y, (int)Position.Z); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is Marker otherMarker && Type == otherMarker.Type && (int)Position.X == (int)otherMarker.Position.X && (int)Position.Y == (int)otherMarker.Position.Y && (int)Position.Z == (int)otherMarker.Position.Z; } - public static bool operator ==(Marker a, object b) + public static bool operator ==(Marker? a, object? b) { return Equals(a, b); } - public static bool operator !=(Marker a, object b) + public static bool operator !=(Marker? a, object? b) { return !Equals(a, b); } diff --git a/Pal.Client/Pal.Client.csproj b/Pal.Client/Pal.Client.csproj index 4cce4a1..623ce5f 100644 --- a/Pal.Client/Pal.Client.csproj +++ b/Pal.Client/Pal.Client.csproj @@ -3,7 +3,8 @@ net6.0-windows 9.0 - 1.9.0.0 + 1.10.0.0 + enable diff --git a/Pal.Client/Plugin.cs b/Pal.Client/Plugin.cs index 0af2afc..16d8ad0 100644 --- a/Pal.Client/Plugin.cs +++ b/Pal.Client/Plugin.cs @@ -47,7 +47,7 @@ namespace Pal.Client public SyncState TerritorySyncState { get; set; } public PomanderState PomanderOfSight { get; set; } = PomanderState.Inactive; public PomanderState PomanderOfIntuition { get; set; } = PomanderState.Inactive; - public string DebugMessage { get; set; } + public string? DebugMessage { get; set; } public string Name => "Palace Pal"; @@ -58,7 +58,7 @@ namespace Pal.Client pluginInterface.Create(); Service.Plugin = this; - Service.Configuration = (Configuration)pluginInterface.GetPluginConfig() ?? pluginInterface.Create(); + Service.Configuration = (Configuration?)pluginInterface.GetPluginConfig() ?? pluginInterface.Create()!; var agreementWindow = pluginInterface.Create(); if (agreementWindow is not null) @@ -94,7 +94,7 @@ namespace Pal.Client public void OnOpenConfigUi() { - Window configWindow; + Window? configWindow; if (Service.Configuration.FirstUse) configWindow = Service.WindowSystem.GetWindow(); else @@ -282,7 +282,7 @@ namespace Pal.Client foreach (var visibleMarker in visibleMarkers) { - Marker knownMarker = currentFloorMarkers.SingleOrDefault(x => x == visibleMarker); + Marker? knownMarker = currentFloorMarkers.SingleOrDefault(x => x == visibleMarker); if (knownMarker != null) { if (!knownMarker.Seen) @@ -462,7 +462,7 @@ namespace Pal.Client var (success, floorStatistics) = await Service.RemoteApi.FetchStatistics(); if (success) { - var statisticsWindow = Service.WindowSystem.GetWindow(); + var statisticsWindow = Service.WindowSystem.GetWindow()!; statisticsWindow.SetFloorData(floorStatistics); statisticsWindow.IsOpen = true; } @@ -490,7 +490,7 @@ namespace Pal.Client { foreach (var downloadedMarker in downloadedMarkers) { - Marker seenMarker = currentFloor.Markers.SingleOrDefault(x => x == downloadedMarker); + Marker? seenMarker = currentFloor.Markers.SingleOrDefault(x => x == downloadedMarker); if (seenMarker != null) { seenMarker.RemoteSeen = true; @@ -517,7 +517,7 @@ namespace Pal.Client List result = new(); for (int i = 246; i < Service.ObjectTable.Length; i++) { - GameObject obj = Service.ObjectTable[i]; + GameObject? obj = Service.ObjectTable[i]; if (obj == null) continue; @@ -584,7 +584,7 @@ namespace Pal.Client private string GetLocalizedString(uint id) { - return Service.DataManager.GetExcelSheet().GetRow(id).Text?.ToString() ?? "Unknown"; + return Service.DataManager.GetExcelSheet()?.GetRow(id)?.Text?.ToString() ?? "Unknown"; } public enum SyncState diff --git a/Pal.Client/RemoteApi.cs b/Pal.Client/RemoteApi.cs index d093cdb..523d0d6 100644 --- a/Pal.Client/RemoteApi.cs +++ b/Pal.Client/RemoteApi.cs @@ -19,8 +19,8 @@ namespace Pal.Client #else private const string remoteUrl = "https://pal.μ.tv"; #endif - private GrpcChannel _channel; - private LoginReply _lastLoginReply; + private GrpcChannel? _channel; + private LoginReply? _lastLoginReply; private async Task Connect(CancellationToken cancellationToken, bool retry = true) { @@ -43,9 +43,9 @@ namespace Pal.Client var accountClient = new AccountService.AccountServiceClient(_channel); #if DEBUG - string accountId = Service.Configuration.DebugAccountId; + string? accountId = Service.Configuration.DebugAccountId; #else - string accountId = Service.Configuration.AccountId; + string? accountId = Service.Configuration.AccountId; #endif if (string.IsNullOrEmpty(accountId)) { diff --git a/Pal.Client/Service.cs b/Pal.Client/Service.cs index 1f375ab..2805836 100644 --- a/Pal.Client/Service.cs +++ b/Pal.Client/Service.cs @@ -14,7 +14,7 @@ namespace Pal.Client public class Service { [PluginService] public static DalamudPluginInterface PluginInterface { get; private set; } = null!; - [PluginService] public static ClientState ClientState { get; set; } = null; + [PluginService] public static ClientState ClientState { get; set; } = null!; [PluginService] public static ChatGui Chat { get; private set; } = null!; [PluginService] public static ObjectTable ObjectTable { get; private set; } = null!; [PluginService] public static Framework Framework { get; set; } = null!; diff --git a/Pal.Client/WindowSystemExtensions.cs b/Pal.Client/WindowSystemExtensions.cs index 65c41dd..504d3ab 100644 --- a/Pal.Client/WindowSystemExtensions.cs +++ b/Pal.Client/WindowSystemExtensions.cs @@ -5,7 +5,7 @@ namespace Pal.Client { internal static class WindowSystemExtensions { - public static T GetWindow(this WindowSystem windowSystem) + public static T? GetWindow(this WindowSystem windowSystem) where T : Window { return windowSystem.Windows.Select(w => w as T).FirstOrDefault(w => w != null); diff --git a/Pal.Client/Windows/ConfigWindow.cs b/Pal.Client/Windows/ConfigWindow.cs index e9e2ac4..99a116d 100644 --- a/Pal.Client/Windows/ConfigWindow.cs +++ b/Pal.Client/Windows/ConfigWindow.cs @@ -23,11 +23,11 @@ namespace Pal.Client.Windows private Vector4 _silverCofferColor; private bool _fillSilverCoffers; - private string _connectionText; + private string? _connectionText; public ConfigWindow() : base("Palace Pal###PalPalaceConfig") { - var version = typeof(Plugin).Assembly.GetName().Version.ToString(2); + var version = typeof(Plugin).Assembly.GetName().Version!.ToString(2); WindowName = $"Palace Pal v{version}###PalPalaceConfig"; Size = new Vector2(500, 400); SizeCondition = ImGuiCond.FirstUseEver; @@ -153,17 +153,17 @@ namespace Pal.Client.Windows { if (_showTraps) { - int traps = currentFloor.Markers.Count(x => x != null && x.Type == Marker.EType.Trap); + int traps = currentFloor.Markers.Count(x => x.Type == Marker.EType.Trap); ImGui.Text($"{traps} known trap{(traps == 1 ? "" : "s")}"); } if (_showHoard) { - int hoardCoffers = currentFloor.Markers.Count(x => x != null && x.Type == Marker.EType.Hoard); + int hoardCoffers = currentFloor.Markers.Count(x => x.Type == Marker.EType.Hoard); ImGui.Text($"{hoardCoffers} known hoard coffer{(hoardCoffers == 1 ? "" : "s")}"); } if (_showSilverCoffers) { - int silverCoffers = plugin.EphemeralMarkers.Count(x => x != null && x.Type == Marker.EType.SilverCoffer); + int silverCoffers = plugin.EphemeralMarkers.Count(x => x.Type == Marker.EType.SilverCoffer); ImGui.Text($"{silverCoffers} silver coffer{(silverCoffers == 1 ? "" : "s")} visible on current floor"); } @@ -184,16 +184,19 @@ namespace Pal.Client.Windows { try { - var pos = Service.ClientState.LocalPlayer.Position; - var elements = new List + Vector3? pos = Service.ClientState.LocalPlayer?.Position; + if (pos != null) { - Plugin.CreateSplatoonElement(Marker.EType.Trap, pos, _trapColor), - Plugin.CreateSplatoonElement(Marker.EType.Hoard, pos, _hoardColor), - }; + var elements = new List + { + Plugin.CreateSplatoonElement(Marker.EType.Trap, pos.Value, _trapColor), + Plugin.CreateSplatoonElement(Marker.EType.Hoard, pos.Value, _hoardColor), + }; - if (!Splatoon.AddDynamicElements("PalacePal.Test", elements.ToArray(), new long[] { Environment.TickCount64 + 10000 })) - { - Service.Chat.PrintError("Could not draw markers :("); + if (!Splatoon.AddDynamicElements("PalacePal.Test", elements.ToArray(), new long[] { Environment.TickCount64 + 10000 })) + { + Service.Chat.PrintError("Could not draw markers :("); + } } } catch (Exception) diff --git a/Pal.Client/Windows/StatisticsWindow.cs b/Pal.Client/Windows/StatisticsWindow.cs index 5b963ba..cef527c 100644 --- a/Pal.Client/Windows/StatisticsWindow.cs +++ b/Pal.Client/Windows/StatisticsWindow.cs @@ -24,7 +24,7 @@ namespace Pal.Client.Windows foreach (ETerritoryType territory in typeof(ETerritoryType).GetEnumValues()) { - _territoryStatistics[territory] = new TerritoryStatistics { TerritoryName = territory.ToString() }; + _territoryStatistics[territory] = new TerritoryStatistics(territory.ToString()); } } @@ -70,7 +70,7 @@ namespace Pal.Client.Windows foreach (var floor in floorStatistics) { - if (_territoryStatistics.TryGetValue((ETerritoryType)floor.TerritoryType, out TerritoryStatistics territoryStatistics)) + if (_territoryStatistics.TryGetValue((ETerritoryType)floor.TerritoryType, out TerritoryStatistics? territoryStatistics)) { territoryStatistics.TrapCount = floor.TrapCount; territoryStatistics.HoardCofferCount = floor.HoardCount; @@ -83,6 +83,11 @@ namespace Pal.Client.Windows public string TerritoryName { get; set; } public uint? TrapCount { get; set; } public uint? HoardCofferCount { get; set; } + + public TerritoryStatistics(string territoryName) + { + TerritoryName = territoryName; + } } } }