From 2dc20be5f711c50cc80de024c8a6afb96c04d8a1 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 16 Apr 2022 11:10:13 -0400 Subject: use specified nullability in reflection API (#837) --- src/SMAPI/Framework/ContentManagers/BaseContentManager.cs | 2 +- src/SMAPI/Framework/InternalExtensions.cs | 2 +- src/SMAPI/Framework/ModRegistry.cs | 2 +- src/SMAPI/Framework/Reflection/ReflectedField.cs | 6 +++--- src/SMAPI/Framework/Reflection/ReflectedMethod.cs | 4 ++-- src/SMAPI/Framework/Reflection/ReflectedProperty.cs | 12 ++++++------ src/SMAPI/Framework/SMultiplayer.cs | 8 ++++---- 7 files changed, 18 insertions(+), 18 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index 613df023..b2e3ec0f 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -98,7 +98,7 @@ namespace StardewModdingAPI.Framework.ContentManagers this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations; // get asset data - this.BaseDisposableReferences = reflection.GetField>(this, "disposableAssets").GetValue() + this.BaseDisposableReferences = reflection.GetField?>(this, "disposableAssets").GetValue() ?? throw new InvalidOperationException("Can't initialize content manager: the required 'disposableAssets' field wasn't found."); } diff --git a/src/SMAPI/Framework/InternalExtensions.cs b/src/SMAPI/Framework/InternalExtensions.cs index fd8cc86f..580651f3 100644 --- a/src/SMAPI/Framework/InternalExtensions.cs +++ b/src/SMAPI/Framework/InternalExtensions.cs @@ -160,7 +160,7 @@ namespace StardewModdingAPI.Framework /// The reflection helper with which to access private fields. public static bool IsOpen(this SpriteBatch spriteBatch, Reflector reflection) { - return reflection.GetField(spriteBatch, "_beginCalled")!.GetValue(); + return reflection.GetField(spriteBatch, "_beginCalled").GetValue(); } } } diff --git a/src/SMAPI/Framework/ModRegistry.cs b/src/SMAPI/Framework/ModRegistry.cs index 449dccb1..1ae5643f 100644 --- a/src/SMAPI/Framework/ModRegistry.cs +++ b/src/SMAPI/Framework/ModRegistry.cs @@ -35,7 +35,7 @@ namespace StardewModdingAPI.Framework this.Mods.Add(metadata); } - /// Track a mod's assembly for use via . + /// Track a mod's assembly for use via . /// The mod metadata. /// The mod assembly. public void TrackAssemblies(IModMetadata metadata, Assembly modAssembly) diff --git a/src/SMAPI/Framework/Reflection/ReflectedField.cs b/src/SMAPI/Framework/Reflection/ReflectedField.cs index 197a246e..a97ca3f0 100644 --- a/src/SMAPI/Framework/Reflection/ReflectedField.cs +++ b/src/SMAPI/Framework/Reflection/ReflectedField.cs @@ -56,11 +56,11 @@ namespace StardewModdingAPI.Framework.Reflection } /// - public TValue? GetValue() + public TValue GetValue() { try { - return (TValue?)this.FieldInfo.GetValue(this.Parent); + return (TValue)this.FieldInfo.GetValue(this.Parent)!; } catch (InvalidCastException) { @@ -73,7 +73,7 @@ namespace StardewModdingAPI.Framework.Reflection } /// - public void SetValue(TValue? value) + public void SetValue(TValue value) { try { diff --git a/src/SMAPI/Framework/Reflection/ReflectedMethod.cs b/src/SMAPI/Framework/Reflection/ReflectedMethod.cs index c245e843..a607141e 100644 --- a/src/SMAPI/Framework/Reflection/ReflectedMethod.cs +++ b/src/SMAPI/Framework/Reflection/ReflectedMethod.cs @@ -55,7 +55,7 @@ namespace StardewModdingAPI.Framework.Reflection } /// - public TValue? Invoke(params object?[] arguments) + public TValue Invoke(params object?[] arguments) { // invoke method object? result; @@ -75,7 +75,7 @@ namespace StardewModdingAPI.Framework.Reflection // cast return value try { - return (TValue?)result; + return (TValue)result!; } catch (InvalidCastException) { diff --git a/src/SMAPI/Framework/Reflection/ReflectedProperty.cs b/src/SMAPI/Framework/Reflection/ReflectedProperty.cs index 638953a3..72e701d1 100644 --- a/src/SMAPI/Framework/Reflection/ReflectedProperty.cs +++ b/src/SMAPI/Framework/Reflection/ReflectedProperty.cs @@ -14,10 +14,10 @@ namespace StardewModdingAPI.Framework.Reflection private readonly string DisplayName; /// The underlying property getter. - private readonly Func? GetMethod; + private readonly Func? GetMethod; /// The underlying property setter. - private readonly Action? SetMethod; + private readonly Action? SetMethod; /********* @@ -56,13 +56,13 @@ namespace StardewModdingAPI.Framework.Reflection this.PropertyInfo = property; if (this.PropertyInfo.GetMethod != null) - this.GetMethod = (Func)Delegate.CreateDelegate(typeof(Func), obj, this.PropertyInfo.GetMethod); + this.GetMethod = (Func)Delegate.CreateDelegate(typeof(Func), obj, this.PropertyInfo.GetMethod); if (this.PropertyInfo.SetMethod != null) - this.SetMethod = (Action)Delegate.CreateDelegate(typeof(Action), obj, this.PropertyInfo.SetMethod); + this.SetMethod = (Action)Delegate.CreateDelegate(typeof(Action), obj, this.PropertyInfo.SetMethod); } /// - public TValue? GetValue() + public TValue GetValue() { if (this.GetMethod == null) throw new InvalidOperationException($"The {this.DisplayName} property has no get method."); @@ -82,7 +82,7 @@ namespace StardewModdingAPI.Framework.Reflection } /// - public void SetValue(TValue? value) + public void SetValue(TValue value) { if (this.SetMethod == null) throw new InvalidOperationException($"The {this.DisplayName} property has no set method."); diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs index c0e8ee81..e41e7edc 100644 --- a/src/SMAPI/Framework/SMultiplayer.cs +++ b/src/SMAPI/Framework/SMultiplayer.cs @@ -113,13 +113,13 @@ namespace StardewModdingAPI.Framework { case LidgrenClient: { - string address = this.Reflection.GetField(client, "address").GetValue() ?? throw new InvalidOperationException("Can't initialize base networking client: no valid address found."); + string address = this.Reflection.GetField(client, "address").GetValue() ?? throw new InvalidOperationException("Can't initialize base networking client: no valid address found."); return new SLidgrenClient(address, this.OnClientProcessingMessage, this.OnClientSendingMessage); } case GalaxyNetClient: { - GalaxyID address = this.Reflection.GetField(client, "lobbyId").GetValue() ?? throw new InvalidOperationException("Can't initialize GOG networking client: no valid address found."); + GalaxyID address = this.Reflection.GetField(client, "lobbyId").GetValue() ?? throw new InvalidOperationException("Can't initialize GOG networking client: no valid address found."); return new SGalaxyNetClient(address, this.OnClientProcessingMessage, this.OnClientSendingMessage); } @@ -137,13 +137,13 @@ namespace StardewModdingAPI.Framework { case LidgrenServer: { - IGameServer gameServer = this.Reflection.GetField(server, "gameServer").GetValue() ?? throw new InvalidOperationException("Can't initialize base networking client: the required 'gameServer' field wasn't found."); + IGameServer gameServer = this.Reflection.GetField(server, "gameServer").GetValue() ?? throw new InvalidOperationException("Can't initialize base networking client: the required 'gameServer' field wasn't found."); return new SLidgrenServer(gameServer, this, this.OnServerProcessingMessage); } case GalaxyNetServer: { - IGameServer gameServer = this.Reflection.GetField(server, "gameServer").GetValue() ?? throw new InvalidOperationException("Can't initialize GOG networking client: the required 'gameServer' field wasn't found."); + IGameServer gameServer = this.Reflection.GetField(server, "gameServer").GetValue() ?? throw new InvalidOperationException("Can't initialize GOG networking client: the required 'gameServer' field wasn't found."); return new SGalaxyNetServer(gameServer, this, this.OnServerProcessingMessage); } -- cgit