From 66272cbe46b848c64859cd0ab8cca6032c307137 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 13 Apr 2022 23:40:47 -0400 Subject: [PATCH] fix false-positive deprecation notice (#837) --- src/SMAPI/Utilities/PerScreen.cs | 43 +++++++++++++++++++------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/SMAPI/Utilities/PerScreen.cs b/src/SMAPI/Utilities/PerScreen.cs index 799ff63b..ba4f3325 100644 --- a/src/SMAPI/Utilities/PerScreen.cs +++ b/src/SMAPI/Utilities/PerScreen.cs @@ -40,27 +40,12 @@ namespace StardewModdingAPI.Utilities /// Construct an instance. /// Limitation with nullable reference types: when the underlying type is nullable, this sets the default value to null regardless of whether you marked the type parameter nullable. To avoid that, set the default value with the 'createNewState' argument instead. public PerScreen() - : this(null!) { } + : this(null, nullExpected: true) { } /// Construct an instance. /// Create the initial state for a screen. public PerScreen(Func createNewState) - { - // ReSharper disable once ConditionIsAlwaysTrueOrFalse -- required for backwards compatibility - if (createNewState is null) - { - SCore.DeprecationManager.Warn( - SCore.DeprecationManager.GetSourceNameFromStack(), - $"calling the {nameof(PerScreen)} constructor with null", - "3.14.0", - DeprecationLevel.Notice - ); - - createNewState = (() => default!); - } - - this.CreateNewState = createNewState; - } + : this(createNewState, nullExpected: false) { } /// Get all active values by screen ID. This doesn't initialize the value for a screen ID if it's not created yet. public IEnumerable> GetActiveValues() @@ -98,6 +83,30 @@ namespace StardewModdingAPI.Utilities /********* ** Private methods *********/ + /// Construct an instance. + /// Create the initial state for a screen. + /// Whether a null value is expected. + /// This constructor only exists to maintain backwards compatibility. In SMAPI 4.0.0, the overload that passes nullExpected: false should throw an exception instead. + private PerScreen(Func? createNewState, bool nullExpected) + { + if (createNewState is null) + { + createNewState = (() => default!); + + if (!nullExpected) + { + SCore.DeprecationManager.Warn( + SCore.DeprecationManager.GetSourceNameFromStack(), + $"calling the {nameof(PerScreen)} constructor with null", + "3.14.0", + DeprecationLevel.Notice + ); + } + } + + this.CreateNewState = createNewState; + } + /// Remove screens which are no longer active. private void RemoveDeadScreens() {