From ec19c85d66a10bbd41c7fabfd9ea858fe659e747 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 21 May 2017 18:22:21 -0400 Subject: reimplement event deprecation warnings to fix "unknown mod" warnings --- src/StardewModdingAPI/Events/TimeEvents.cs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src/StardewModdingAPI/Events/TimeEvents.cs') diff --git a/src/StardewModdingAPI/Events/TimeEvents.cs b/src/StardewModdingAPI/Events/TimeEvents.cs index 3f06a46b..572898c7 100644 --- a/src/StardewModdingAPI/Events/TimeEvents.cs +++ b/src/StardewModdingAPI/Events/TimeEvents.cs @@ -1,6 +1,8 @@ using System; +using System.Diagnostics.CodeAnalysis; using StardewModdingAPI.Framework; +#pragma warning disable 618 // Suppress obsolete-symbol errors in this file. Since several events are marked obsolete, this produces unnecessary warnings. namespace StardewModdingAPI.Events { /// Events raised when the in-game date or time changes. @@ -12,6 +14,10 @@ namespace StardewModdingAPI.Events /// Manages deprecation warnings. private static DeprecationManager DeprecationManager; + /// The backing field for . + [SuppressMessage("ReSharper", "InconsistentNaming")] + private static event EventHandler _OnNewDay; + /********* ** Events @@ -33,7 +39,15 @@ namespace StardewModdingAPI.Events /// Raised when the player is transitioning to a new day and the game is performing its day update logic. This event is triggered twice: once after the game starts transitioning, and again after it finishes. [Obsolete("Use " + nameof(TimeEvents) + "." + nameof(TimeEvents.AfterDayStarted) + " or " + nameof(SaveEvents) + " instead")] - public static event EventHandler OnNewDay; + public static event EventHandler OnNewDay + { + add + { + TimeEvents.DeprecationManager.Warn($"{nameof(TimeEvents)}.{nameof(TimeEvents.OnNewDay)}", "1.6", DeprecationLevel.Info); + TimeEvents._OnNewDay += value; + } + remove => TimeEvents._OnNewDay -= value; + } /********* @@ -96,14 +110,7 @@ namespace StardewModdingAPI.Events /// Whether the game just started the transition (true) or finished it (false). internal static void InvokeOnNewDay(IMonitor monitor, int priorDay, int newDay, bool isTransitioning) { - if (TimeEvents.OnNewDay == null) - return; - - string name = $"{nameof(TimeEvents)}.{nameof(TimeEvents.OnNewDay)}"; - Delegate[] handlers = TimeEvents.OnNewDay.GetInvocationList(); - - TimeEvents.DeprecationManager.WarnForEvent(handlers, name, "1.6", DeprecationLevel.Info); - monitor.SafelyRaiseGenericEvent(name, handlers, null, new EventArgsNewDay(priorDay, newDay, isTransitioning)); + monitor.SafelyRaiseGenericEvent($"{nameof(TimeEvents)}.{nameof(TimeEvents.OnNewDay)}", TimeEvents._OnNewDay?.GetInvocationList(), null, new EventArgsNewDay(priorDay, newDay, isTransitioning)); } } } -- cgit From 798439ea34daf4999685ec8886cb295f1c4d43ba Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 29 May 2017 23:52:33 -0400 Subject: deprecate TimeEvents.DayOfMonthChanged, SeasonOfYearChanged, and YearOfGameChanged --- release-notes.md | 1 + src/StardewModdingAPI/Events/TimeEvents.cs | 50 ++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 6 deletions(-) (limited to 'src/StardewModdingAPI/Events/TimeEvents.cs') diff --git a/release-notes.md b/release-notes.md index 34c3e141..f7b594e1 100644 --- a/release-notes.md +++ b/release-notes.md @@ -35,6 +35,7 @@ For modders: * Fixed `smapi-crash.txt` being copied from the default log even if a different path is specified with `--log-path`. * Fixed the content API not matching XNB filenames with two dots (like `a.b.xnb`) if you don't specify the `.xnb` extension. * Fixed `debug` command output not printed to console. +* Deprecated `TimeEvents.DayOfMonthChanged`, `SeasonOfYearChanged`, and `YearOfGameChanged`. These don't do what most modders think they do and aren't very reliable, since they depend on the SMAPI/game lifecycle which can change. You should use `TimeEvents.AfterDayStarted` or `SaveEvents.BeforeSave` instead. ## 1.13.1 See [log](https://github.com/Pathoschild/SMAPI/compare/1.13...1.13.1). diff --git a/src/StardewModdingAPI/Events/TimeEvents.cs b/src/StardewModdingAPI/Events/TimeEvents.cs index 572898c7..5dadf567 100644 --- a/src/StardewModdingAPI/Events/TimeEvents.cs +++ b/src/StardewModdingAPI/Events/TimeEvents.cs @@ -18,6 +18,17 @@ namespace StardewModdingAPI.Events [SuppressMessage("ReSharper", "InconsistentNaming")] private static event EventHandler _OnNewDay; + /// The backing field for . + [SuppressMessage("ReSharper", "InconsistentNaming")] + private static event EventHandler _DayOfMonthChanged; + + /// The backing field for . + [SuppressMessage("ReSharper", "InconsistentNaming")] + private static event EventHandler _SeasonOfYearChanged; + + /// The backing field for . + [SuppressMessage("ReSharper", "InconsistentNaming")] + private static event EventHandler _YearOfGameChanged; /********* ** Events @@ -29,13 +40,40 @@ namespace StardewModdingAPI.Events public static event EventHandler TimeOfDayChanged; /// Raised after the day-of-month value changes, including when loading a save. This may happen before save; in most cases you should use instead. - public static event EventHandler DayOfMonthChanged; + [Obsolete("Use " + nameof(TimeEvents) + "." + nameof(TimeEvents.AfterDayStarted) + " or " + nameof(SaveEvents) + " instead")] + public static event EventHandler DayOfMonthChanged + { + add + { + TimeEvents.DeprecationManager.Warn($"{nameof(TimeEvents)}.{nameof(TimeEvents.DayOfMonthChanged)}", "1.14", DeprecationLevel.Info); + TimeEvents._DayOfMonthChanged += value; + } + remove => TimeEvents._DayOfMonthChanged -= value; + } /// Raised after the year value changes. - public static event EventHandler YearOfGameChanged; + [Obsolete("Use " + nameof(TimeEvents) + "." + nameof(TimeEvents.AfterDayStarted) + " or " + nameof(SaveEvents) + " instead")] + public static event EventHandler YearOfGameChanged + { + add + { + TimeEvents.DeprecationManager.Warn($"{nameof(TimeEvents)}.{nameof(TimeEvents.YearOfGameChanged)}", "1.14", DeprecationLevel.Info); + TimeEvents._YearOfGameChanged += value; + } + remove => TimeEvents._YearOfGameChanged -= value; + } /// Raised after the season value changes. - public static event EventHandler SeasonOfYearChanged; + [Obsolete("Use " + nameof(TimeEvents) + "." + nameof(TimeEvents.AfterDayStarted) + " or " + nameof(SaveEvents) + " instead")] + public static event EventHandler SeasonOfYearChanged + { + add + { + TimeEvents.DeprecationManager.Warn($"{nameof(TimeEvents)}.{nameof(TimeEvents.SeasonOfYearChanged)}", "1.14", DeprecationLevel.Info); + TimeEvents._SeasonOfYearChanged += value; + } + remove => TimeEvents._SeasonOfYearChanged -= value; + } /// Raised when the player is transitioning to a new day and the game is performing its day update logic. This event is triggered twice: once after the game starts transitioning, and again after it finishes. [Obsolete("Use " + nameof(TimeEvents) + "." + nameof(TimeEvents.AfterDayStarted) + " or " + nameof(SaveEvents) + " instead")] @@ -82,7 +120,7 @@ namespace StardewModdingAPI.Events /// The current day value. internal static void InvokeDayOfMonthChanged(IMonitor monitor, int priorDay, int newDay) { - monitor.SafelyRaiseGenericEvent($"{nameof(TimeEvents)}.{nameof(TimeEvents.DayOfMonthChanged)}", TimeEvents.DayOfMonthChanged?.GetInvocationList(), null, new EventArgsIntChanged(priorDay, newDay)); + monitor.SafelyRaiseGenericEvent($"{nameof(TimeEvents)}.{nameof(TimeEvents.DayOfMonthChanged)}", TimeEvents._DayOfMonthChanged?.GetInvocationList(), null, new EventArgsIntChanged(priorDay, newDay)); } /// Raise a event. @@ -91,7 +129,7 @@ namespace StardewModdingAPI.Events /// The current year value. internal static void InvokeYearOfGameChanged(IMonitor monitor, int priorYear, int newYear) { - monitor.SafelyRaiseGenericEvent($"{nameof(TimeEvents)}.{nameof(TimeEvents.YearOfGameChanged)}", TimeEvents.YearOfGameChanged?.GetInvocationList(), null, new EventArgsIntChanged(priorYear, newYear)); + monitor.SafelyRaiseGenericEvent($"{nameof(TimeEvents)}.{nameof(TimeEvents.YearOfGameChanged)}", TimeEvents._YearOfGameChanged?.GetInvocationList(), null, new EventArgsIntChanged(priorYear, newYear)); } /// Raise a event. @@ -100,7 +138,7 @@ namespace StardewModdingAPI.Events /// The current season name. internal static void InvokeSeasonOfYearChanged(IMonitor monitor, string priorSeason, string newSeason) { - monitor.SafelyRaiseGenericEvent($"{nameof(TimeEvents)}.{nameof(TimeEvents.SeasonOfYearChanged)}", TimeEvents.SeasonOfYearChanged?.GetInvocationList(), null, new EventArgsStringChanged(priorSeason, newSeason)); + monitor.SafelyRaiseGenericEvent($"{nameof(TimeEvents)}.{nameof(TimeEvents.SeasonOfYearChanged)}", TimeEvents._SeasonOfYearChanged?.GetInvocationList(), null, new EventArgsStringChanged(priorSeason, newSeason)); } /// Raise a event. -- cgit