From 79181012ee01e93c1af7c4bf8bd1a3a717274ded Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 14 Jun 2020 10:55:52 -0400 Subject: [PATCH 01/11] tweak build files --- build/common.targets | 1 + src/SMAPI.Installer/SMAPI.Installer.csproj | 8 +- .../SMAPI.ModBuildConfig.Analyzer.csproj | 4 - .../SMAPI.ModBuildConfig.csproj | 25 ++---- src/SMAPI.ModBuildConfig/build/smapi.targets | 56 +++---------- .../SMAPI.Mods.ConsoleCommands.csproj | 47 +++-------- .../SMAPI.Mods.SaveBackup.csproj | 16 +--- .../SMAPI.Toolkit.CoreInterfaces.csproj | 6 +- src/SMAPI.Toolkit/ModToolkit.cs | 3 - src/SMAPI.Toolkit/Properties/AssemblyInfo.cs | 4 + src/SMAPI.Toolkit/SMAPI.Toolkit.csproj | 7 +- src/SMAPI/SMAPI.csproj | 81 +++++-------------- 12 files changed, 56 insertions(+), 202 deletions(-) create mode 100644 src/SMAPI.Toolkit/Properties/AssemblyInfo.cs diff --git a/build/common.targets b/build/common.targets index 41bea8af..ddfbd71a 100644 --- a/build/common.targets +++ b/build/common.targets @@ -7,6 +7,7 @@ 3.5.0 SMAPI + latest $(AssemblySearchPaths);{GAC} $(DefineConstants);SMAPI_FOR_WINDOWS diff --git a/src/SMAPI.Installer/SMAPI.Installer.csproj b/src/SMAPI.Installer/SMAPI.Installer.csproj index 79e19d89..44ed3bd1 100644 --- a/src/SMAPI.Installer/SMAPI.Installer.csproj +++ b/src/SMAPI.Installer/SMAPI.Installer.csproj @@ -1,11 +1,8 @@  - - SMAPI.Installer StardewModdingAPI.Installer The SMAPI installer for players. net45 - latest Exe x86 false @@ -16,13 +13,10 @@ - - PreserveNewest - + - diff --git a/src/SMAPI.ModBuildConfig.Analyzer/SMAPI.ModBuildConfig.Analyzer.csproj b/src/SMAPI.ModBuildConfig.Analyzer/SMAPI.ModBuildConfig.Analyzer.csproj index 3659e25a..0d109b83 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer/SMAPI.ModBuildConfig.Analyzer.csproj +++ b/src/SMAPI.ModBuildConfig.Analyzer/SMAPI.ModBuildConfig.Analyzer.csproj @@ -1,11 +1,8 @@  - - SMAPI.ModBuildConfig.Analyzer StardewModdingAPI.ModBuildConfig.Analyzer 3.0.0 netstandard2.0 - latest false bin latest @@ -19,5 +16,4 @@ - diff --git a/src/SMAPI.ModBuildConfig/SMAPI.ModBuildConfig.csproj b/src/SMAPI.ModBuildConfig/SMAPI.ModBuildConfig.csproj index ccbd9a85..5061b01b 100644 --- a/src/SMAPI.ModBuildConfig/SMAPI.ModBuildConfig.csproj +++ b/src/SMAPI.ModBuildConfig/SMAPI.ModBuildConfig.csproj @@ -1,24 +1,12 @@  - - SMAPI.ModBuildConfig StardewModdingAPI.ModBuildConfig - 3.0.0 + 3.1.0 net45 - latest x86 false - - - - - - - - - @@ -28,19 +16,16 @@ - - mod-package.md - + - - PreserveNewest - + + + - diff --git a/src/SMAPI.ModBuildConfig/build/smapi.targets b/src/SMAPI.ModBuildConfig/build/smapi.targets index 5ca9f032..bfee3b33 100644 --- a/src/SMAPI.ModBuildConfig/build/smapi.targets +++ b/src/SMAPI.ModBuildConfig/build/smapi.targets @@ -38,58 +38,26 @@ **********************************************--> - - $(GamePath)\$(GameExecutableName).exe - $(CopyModReferencesToBuildOutput) - - - $(GamePath)\StardewValley.GameData.dll - $(CopyModReferencesToBuildOutput) - - - $(GamePath)\StardewModdingAPI.exe - $(CopyModReferencesToBuildOutput) - - - $(GamePath)\smapi-internal\SMAPI.Toolkit.CoreInterfaces.dll - $(CopyModReferencesToBuildOutput) - - - $(GamePath)\xTile.dll - $(CopyModReferencesToBuildOutput) - - - $(GamePath)\smapi-internal\0Harmony.dll - $(CopyModReferencesToBuildOutput) - + + + + + + - - $(CopyModReferencesToBuildOutput) - - - $(CopyModReferencesToBuildOutput) - - - $(CopyModReferencesToBuildOutput) - - - $(CopyModReferencesToBuildOutput) - - - $(GamePath)\Netcode.dll - $(CopyModReferencesToBuildOutput) - + + + + + - - $(GamePath)\MonoGame.Framework.dll - $(CopyModReferencesToBuildOutput) - + diff --git a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj index 526d406b..1e3208de 100644 --- a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj +++ b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj @@ -1,72 +1,45 @@  - ConsoleCommands StardewModdingAPI.Mods.ConsoleCommands net45 - latest false x86 - - False - + - - $(GamePath)\$(GameExecutableName).exe - False - - - $(GamePath)\StardewValley.GameData.dll - False - + + - - $(GamePath)\Netcode.dll - False - - - False - - - False - - - False - - - False - + + + + + - - $(GamePath)\MonoGame.Framework.dll - False - + - - PreserveNewest - + - diff --git a/src/SMAPI.Mods.SaveBackup/SMAPI.Mods.SaveBackup.csproj b/src/SMAPI.Mods.SaveBackup/SMAPI.Mods.SaveBackup.csproj index 970ccea8..98a3f0cc 100644 --- a/src/SMAPI.Mods.SaveBackup/SMAPI.Mods.SaveBackup.csproj +++ b/src/SMAPI.Mods.SaveBackup/SMAPI.Mods.SaveBackup.csproj @@ -1,34 +1,24 @@  - SaveBackup StardewModdingAPI.Mods.SaveBackup net45 - latest false x86 - - False - + - - $(GamePath)\$(GameExecutableName).exe - False - + - - PreserveNewest - + - diff --git a/src/SMAPI.Toolkit.CoreInterfaces/SMAPI.Toolkit.CoreInterfaces.csproj b/src/SMAPI.Toolkit.CoreInterfaces/SMAPI.Toolkit.CoreInterfaces.csproj index accc9175..2bddc46a 100644 --- a/src/SMAPI.Toolkit.CoreInterfaces/SMAPI.Toolkit.CoreInterfaces.csproj +++ b/src/SMAPI.Toolkit.CoreInterfaces/SMAPI.Toolkit.CoreInterfaces.csproj @@ -1,15 +1,11 @@  - - SMAPI.Toolkit.CoreInterfaces StardewModdingAPI Provides toolkit interfaces which are available to SMAPI mods. net4.5;netstandard2.0 - latest - bin\$(Configuration)\$(TargetFramework)\SMAPI.Toolkit.CoreInterfaces.xml + true x86 - diff --git a/src/SMAPI.Toolkit/ModToolkit.cs b/src/SMAPI.Toolkit/ModToolkit.cs index 80b14659..08fe0fed 100644 --- a/src/SMAPI.Toolkit/ModToolkit.cs +++ b/src/SMAPI.Toolkit/ModToolkit.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Runtime.CompilerServices; using System.Threading.Tasks; using Newtonsoft.Json; using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; @@ -11,8 +10,6 @@ using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.ModScanning; using StardewModdingAPI.Toolkit.Serialization; -[assembly: InternalsVisibleTo("StardewModdingAPI")] -[assembly: InternalsVisibleTo("SMAPI.Web")] namespace StardewModdingAPI.Toolkit { /// A convenience wrapper for the various tools. diff --git a/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs b/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..233e680b --- /dev/null +++ b/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("StardewModdingAPI")] +[assembly: InternalsVisibleTo("SMAPI.Web")] diff --git a/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj b/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj index 4e6918ad..71ea0f12 100644 --- a/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj +++ b/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj @@ -1,14 +1,10 @@  - - SMAPI.Toolkit StardewModdingAPI.Toolkit A library which encapsulates mod-handling logic for mod managers and tools. Not intended for use by mods. net4.5;netstandard2.0 - latest - bin\$(Configuration)\$(TargetFramework)\SMAPI.Toolkit.xml + true x86 - StardewModdingAPI.Toolkit @@ -24,5 +20,4 @@ - diff --git a/src/SMAPI/SMAPI.csproj b/src/SMAPI/SMAPI.csproj index d36d7b4c..02f1763f 100644 --- a/src/SMAPI/SMAPI.csproj +++ b/src/SMAPI/SMAPI.csproj @@ -1,14 +1,12 @@  - StardewModdingAPI StardewModdingAPI The modding API for Stardew Valley. net45 - latest x86 Exe - bin\$(Configuration)\StardewModdingAPI.xml + true false true icon.ico @@ -23,54 +21,24 @@ - - $(GamePath)\$(GameExecutableName).exe - False - - - $(GamePath)\StardewValley.GameData.dll - False - - - True - - - True - - - $(GamePath)\GalaxyCSharp.dll - False - - - $(GamePath)\Lidgren.Network.dll - False - - - $(GamePath)\xTile.dll - False - + + + + + + + - - $(GamePath)\Netcode.dll - False - - - False - - - False - - - False - - - False - + + + + + @@ -78,10 +46,7 @@ - - $(GamePath)\MonoGame.Framework.dll - False - + @@ -92,22 +57,12 @@ - - PreserveNewest - - - SMAPI.metadata.json - PreserveNewest - - - PreserveNewest - - - PreserveNewest - + + + + - From 2d19095169019a1cb07da5802dd83fb13550a051 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 14 Jun 2020 11:29:07 -0400 Subject: [PATCH 02/11] add support for using a custom Harmony build (#711) --- .gitignore | 4 ++-- docs/technical/smapi.md | 33 ++++++++++++++++++++------------- src/SMAPI/SMAPI.csproj | 3 ++- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 5450a2f5..02522716 100644 --- a/.gitignore +++ b/.gitignore @@ -30,8 +30,8 @@ _ReSharper*/ # sensitive files appsettings.Development.json -# AWS generated files -src/SMAPI.Web.LegacyRedirects/aws-beanstalk-tools-defaults.json +# generated build files +build/0Harmony.* # Azure generated files src/SMAPI.Web/Properties/PublishProfiles/*.pubxml diff --git a/docs/technical/smapi.md b/docs/technical/smapi.md index c9d5c07e..ca8a9c70 100644 --- a/docs/technical/smapi.md +++ b/docs/technical/smapi.md @@ -15,6 +15,7 @@ This document is about SMAPI itself; see also [mod build package](mod-package.md * [Compiling from source](#compiling-from-source) * [Debugging a local build](#debugging-a-local-build) * [Preparing a release](#preparing-a-release) + * [Using a custom Harmony build](#using-a-custom-harmony-build) * [Release notes](#release-notes) ## Customisation @@ -60,21 +61,18 @@ flag | purpose ## For SMAPI developers ### Compiling from source -Using an official SMAPI release is recommended for most users. +Using an official SMAPI release is recommended for most users, but you can compile from source +directly if needed. There are no special steps (just open the project and compile), but SMAPI often +uses the latest C# syntax. You may need the latest version of your IDE to compile it. -SMAPI often uses the latest C# syntax. You may need the latest version of -[Visual Studio](https://www.visualstudio.com/vs/community/) on Windows, -[MonoDevelop](https://www.monodevelop.com/) on Linux, -[Visual Studio for Mac](https://www.visualstudio.com/vs/visual-studio-mac/), or an equivalent IDE -to compile it. It uses build configuration derived from the -[crossplatform mod config](https://smapi.io/package/readme) to detect your current OS automatically -and load the correct references. Compile output will be placed in a `bin` folder at the root of the -git repository. +SMAPI uses build configuration derived from the [crossplatform mod config](https://smapi.io/package/readme) +to detect your current OS automatically and load the correct references. Compile output will be +placed in a `bin` folder at the root of the Git repository. ### Debugging a local build Rebuilding the solution in debug mode will copy the SMAPI files into your game folder. Starting the `SMAPI` project with debugging from Visual Studio (on Mac or Windows) will launch SMAPI with -the debugger attached, so you can intercept errors and step through the code being executed. This +the debugger attached, so you can intercept errors and step through the code being executed. That doesn't work in MonoDevelop on Linux, unfortunately. ### Preparing a release @@ -87,9 +85,9 @@ on the wiki for the first-time setup. build type | format | example :--------- | :----------------------- | :------ - dev build | `-alpha.` | `3.0-alpha.20171230` - prerelease | `-beta.` | `3.0-beta.2` - release | `` | `3.0` + dev build | `-alpha.` | `3.0.0-alpha.20171230` + prerelease | `-beta.` | `3.0.0-beta.2` + release | `` | `3.0.0` 2. In Windows: 1. Rebuild the solution in Release mode. @@ -103,5 +101,14 @@ on the wiki for the first-time setup. 3. Rename the folders to `SMAPI installer` and `SMAPI installer for developers`. 4. Zip the two folders. +### Using a custom Harmony build +The official SMAPI releases include [a custom build of Harmony](https://github.com/Pathoschild/Harmony), +but compiling from source will use the official build. To use a custom build, put `0Harmony.dll` in +the `build` folder and it'll be referenced automatically. + +Note that Harmony merges its dependencies into `0Harmony.dll` when compiled in release mode. To use +a debug build of Harmony, you'll need to manually copy those dependencies into your game's +`smapi-internal` folder. + ## Release notes See [release notes](../release-notes.md). diff --git a/src/SMAPI/SMAPI.csproj b/src/SMAPI/SMAPI.csproj index 02f1763f..443d5baa 100644 --- a/src/SMAPI/SMAPI.csproj +++ b/src/SMAPI/SMAPI.csproj @@ -14,13 +14,14 @@ - + + From ff7b9a0251484bfb9737f9c6c05637f63efa9551 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 14 Jun 2020 23:30:35 -0400 Subject: [PATCH 03/11] update TMXTile --- src/SMAPI/SMAPI.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SMAPI/SMAPI.csproj b/src/SMAPI/SMAPI.csproj index 443d5baa..603b6fb5 100644 --- a/src/SMAPI/SMAPI.csproj +++ b/src/SMAPI/SMAPI.csproj @@ -17,7 +17,7 @@ - + From b395e92faae0a197a2d1c2e10e835e38fcc6502c Mon Sep 17 00:00:00 2001 From: Chase W Date: Mon, 15 Jun 2020 15:28:03 -0400 Subject: [PATCH 04/11] Implemented event priority attribute --- src/SMAPI/Events/EventPriority.cs | 29 ++++++++++++++ src/SMAPI/Events/EventPriorityAttribute.cs | 29 ++++++++++++++ src/SMAPI/Framework/Events/ManagedEvent.cs | 45 +++++++++++++++++----- 3 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 src/SMAPI/Events/EventPriority.cs create mode 100644 src/SMAPI/Events/EventPriorityAttribute.cs diff --git a/src/SMAPI/Events/EventPriority.cs b/src/SMAPI/Events/EventPriority.cs new file mode 100644 index 00000000..17f5fbb7 --- /dev/null +++ b/src/SMAPI/Events/EventPriority.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Events +{ + /// + /// Event priority for method handlers. + /// + public enum EventPriority + { + /// + /// Low priority. + /// + Low = 3, + + /// + /// Normal priority. This is the default. + /// + Normal = 2, + + /// + /// High priority. + /// + High = 1, + } +} diff --git a/src/SMAPI/Events/EventPriorityAttribute.cs b/src/SMAPI/Events/EventPriorityAttribute.cs new file mode 100644 index 00000000..c5683931 --- /dev/null +++ b/src/SMAPI/Events/EventPriorityAttribute.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Events +{ + /// + /// An attribute for controlling event priority of an event handler. + /// + [AttributeUsage(AttributeTargets.Method)] + public class EventPriorityAttribute : System.Attribute + { + /// + /// The priority for the method marked by this attribute. + /// + public EventPriority Priority { get; } + + /// + /// Constructor. + /// + /// The priority for method marked by this attribute. + public EventPriorityAttribute( EventPriority priority ) + { + this.Priority = priority; + } + } +} diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index 118b73ac..172b25c0 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -1,6 +1,9 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Reflection; +using StardewModdingAPI.Events; using StardewModdingAPI.Framework.PerformanceMonitoring; namespace StardewModdingAPI.Framework.Events @@ -13,7 +16,7 @@ namespace StardewModdingAPI.Framework.Events ** Fields *********/ /// The underlying event. - private event EventHandler Event; + private IList> EventHandlers = new List>(); /// Writes messages to the log. private readonly IMonitor Monitor; @@ -77,23 +80,23 @@ namespace StardewModdingAPI.Framework.Events /// The mod which added the event handler. public void Add(EventHandler handler, IModMetadata mod) { - this.Event += handler; - this.AddTracking(mod, handler, this.Event?.GetInvocationList().Cast>()); + this.EventHandlers.Add(handler); + this.AddTracking(mod, handler, this.EventHandlers); } /// Remove an event handler. /// The event handler. public void Remove(EventHandler handler) { - this.Event -= handler; - this.RemoveTracking(handler, this.Event?.GetInvocationList().Cast>()); + this.EventHandlers.Remove(handler); + this.RemoveTracking(handler, this.EventHandlers); } /// Raise the event and notify all handlers. /// The event arguments to pass. public void Raise(TEventArgs args) { - if (this.Event == null) + if (this.EventHandlers.Count == 0) return; @@ -118,7 +121,7 @@ namespace StardewModdingAPI.Framework.Events /// A lambda which returns true if the event should be raised for the given mod. public void RaiseForMods(TEventArgs args, Func match) { - if (this.Event == null) + if (this.EventHandlers.Count == 0) return; foreach (EventHandler handler in this.CachedInvocationList) @@ -154,6 +157,30 @@ namespace StardewModdingAPI.Framework.Events : mod.DisplayName; } + /// + /// Get the event priority of an event handler. + /// + /// The event handler to get the priority of. + /// The event priority of the event handler. + private EventPriority GetPriorityOfHandler(EventHandler handler) + { + CustomAttributeData attr = handler.Method.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EventPriorityAttribute)); + if (attr == null) + return EventPriority.Normal; + return (EventPriority) attr.ConstructorArguments[0].Value; + } + + /// + /// Sort an invocation list by its priority. + /// + /// The invocation list. + /// An array of the event handlers sorted by their priority. + private EventHandler[] GetCachedInvocationList(IEnumerable> invocationList ) + { + EventHandler[] handlers = invocationList?.ToArray() ?? new EventHandler[0]; + return handlers.OrderBy((h1) => this.GetPriorityOfHandler(h1)).ToArray(); + } + /// Track an event handler. /// The mod which added the handler. /// The event handler. @@ -161,7 +188,7 @@ namespace StardewModdingAPI.Framework.Events protected void AddTracking(IModMetadata mod, EventHandler handler, IEnumerable> invocationList) { this.SourceMods[handler] = mod; - this.CachedInvocationList = invocationList?.ToArray() ?? new EventHandler[0]; + this.CachedInvocationList = this.GetCachedInvocationList(invocationList); } /// Remove tracking for an event handler. @@ -169,7 +196,7 @@ namespace StardewModdingAPI.Framework.Events /// The updated event invocation list. protected void RemoveTracking(EventHandler handler, IEnumerable> invocationList) { - this.CachedInvocationList = invocationList?.ToArray() ?? new EventHandler[0]; + this.CachedInvocationList = this.GetCachedInvocationList(invocationList); if (!this.CachedInvocationList.Contains(handler)) // don't remove if there's still a reference to the removed handler (e.g. it was added twice and removed once) this.SourceMods.Remove(handler); } From fc29fe918a89623544b011c76217aa1ea1975d00 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 15 Jun 2020 18:58:05 -0400 Subject: [PATCH 05/11] refactor & optimize event code a bit, drop old support for unknown event handlers --- src/SMAPI/Events/EventPriority.cs | 24 +-- src/SMAPI/Events/EventPriorityAttribute.cs | 31 ++-- src/SMAPI/Framework/Events/EventManager.cs | 5 +- src/SMAPI/Framework/Events/ManagedEvent.cs | 138 ++++++------------ .../Framework/Events/ManagedEventHandler.cs | 62 ++++++++ .../Framework/Events/ModDisplayEvents.cs | 20 +-- .../Framework/Events/ModGameLoopEvents.cs | 28 ++-- src/SMAPI/Framework/Events/ModInputEvents.cs | 8 +- .../Framework/Events/ModMultiplayerEvents.cs | 6 +- src/SMAPI/Framework/Events/ModPlayerEvents.cs | 6 +- .../Framework/Events/ModSpecialisedEvents.cs | 6 +- src/SMAPI/Framework/Events/ModWorldEvents.cs | 8 +- src/SMAPI/Framework/SCore.cs | 2 +- 13 files changed, 166 insertions(+), 178 deletions(-) create mode 100644 src/SMAPI/Framework/Events/ManagedEventHandler.cs diff --git a/src/SMAPI/Events/EventPriority.cs b/src/SMAPI/Events/EventPriority.cs index 17f5fbb7..e1fb00ac 100644 --- a/src/SMAPI/Events/EventPriority.cs +++ b/src/SMAPI/Events/EventPriority.cs @@ -1,29 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace StardewModdingAPI.Events { - /// - /// Event priority for method handlers. - /// + /// The event priorities for method handlers. public enum EventPriority { - /// - /// Low priority. - /// + /// Low priority. Low = 3, - /// - /// Normal priority. This is the default. - /// + /// The default priority. Normal = 2, - /// - /// High priority. - /// - High = 1, + /// High priority. + High = 1 } } diff --git a/src/SMAPI/Events/EventPriorityAttribute.cs b/src/SMAPI/Events/EventPriorityAttribute.cs index c5683931..207e7862 100644 --- a/src/SMAPI/Events/EventPriorityAttribute.cs +++ b/src/SMAPI/Events/EventPriorityAttribute.cs @@ -1,27 +1,24 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace StardewModdingAPI.Events { - /// - /// An attribute for controlling event priority of an event handler. - /// + /// An attribute which specifies the priority for an event handler. [AttributeUsage(AttributeTargets.Method)] - public class EventPriorityAttribute : System.Attribute + public class EventPriorityAttribute : Attribute { - /// - /// The priority for the method marked by this attribute. - /// - public EventPriority Priority { get; } + /********* + ** Accessors + *********/ + /// The event handler priority, relative to other handlers across all mods registered for this event. + internal EventPriority Priority { get; } - /// - /// Constructor. - /// - /// The priority for method marked by this attribute. - public EventPriorityAttribute( EventPriority priority ) + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The event handler priority, relative to other handlers across all mods registered for this event. Higher-priority handlers are notified before lower-priority handlers. + public EventPriorityAttribute(EventPriority priority) { this.Priority = priority; } diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index a9dfda97..538fde59 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -174,15 +174,14 @@ namespace StardewModdingAPI.Framework.Events ** Public methods *********/ /// Construct an instance. - /// Writes messages to the log. /// The mod registry with which to identify mods. /// Tracks performance metrics. - public EventManager(IMonitor monitor, ModRegistry modRegistry, PerformanceMonitor performanceMonitor) + public EventManager(ModRegistry modRegistry, PerformanceMonitor performanceMonitor) { // create shortcut initializers ManagedEvent ManageEventOf(string typeName, string eventName, bool isPerformanceCritical = false) { - return new ManagedEvent($"{typeName}.{eventName}", monitor, modRegistry, performanceMonitor, isPerformanceCritical); + return new ManagedEvent($"{typeName}.{eventName}", modRegistry, performanceMonitor, isPerformanceCritical); } // init events (new) diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index 172b25c0..b0f0ae71 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -15,24 +14,24 @@ namespace StardewModdingAPI.Framework.Events /********* ** Fields *********/ - /// The underlying event. - private IList> EventHandlers = new List>(); - - /// Writes messages to the log. - private readonly IMonitor Monitor; + /// The underlying event handlers. + private readonly List> EventHandlers = new List>(); /// The mod registry with which to identify mods. protected readonly ModRegistry ModRegistry; - /// The display names for the mods which added each delegate. - private readonly IDictionary, IModMetadata> SourceMods = new Dictionary, IModMetadata>(); - - /// The cached invocation list. - private EventHandler[] CachedInvocationList; - /// Tracks performance metrics. private readonly PerformanceMonitor PerformanceMonitor; + /// The total number of event handlers registered for this events, regardless of whether they're still registered. + private int RegistrationIndex; + + /// Whether any registered event handlers have a custom priority value. + private bool HasCustomPriorities; + + /// Whether event handlers should be sorted before the next invocation. + private bool NeedsSort; + /********* ** Accessors @@ -49,14 +48,12 @@ namespace StardewModdingAPI.Framework.Events *********/ /// Construct an instance. /// A human-readable name for the event. - /// Writes messages to the log. /// The mod registry with which to identify mods. /// Tracks performance metrics. /// Whether the event is typically called at least once per second. - public ManagedEvent(string eventName, IMonitor monitor, ModRegistry modRegistry, PerformanceMonitor performanceMonitor, bool isPerformanceCritical = false) + public ManagedEvent(string eventName, ModRegistry modRegistry, PerformanceMonitor performanceMonitor, bool isPerformanceCritical = false) { this.EventName = eventName; - this.Monitor = monitor; this.ModRegistry = modRegistry; this.PerformanceMonitor = performanceMonitor; this.IsPerformanceCritical = isPerformanceCritical; @@ -65,14 +62,7 @@ namespace StardewModdingAPI.Framework.Events /// Get whether anything is listening to the event. public bool HasListeners() { - return this.CachedInvocationList?.Length > 0; - } - - /// Add an event handler. - /// The event handler. - public void Add(EventHandler handler) - { - this.Add(handler, this.ModRegistry.GetFromStack()); + return this.EventHandlers.Count > 0; } /// Add an event handler. @@ -80,33 +70,46 @@ namespace StardewModdingAPI.Framework.Events /// The mod which added the event handler. public void Add(EventHandler handler, IModMetadata mod) { - this.EventHandlers.Add(handler); - this.AddTracking(mod, handler, this.EventHandlers); + EventPriority priority = handler.Method.GetCustomAttribute()?.Priority ?? EventPriority.Normal; + var managedHandler = new ManagedEventHandler(handler, this.RegistrationIndex++, priority, mod); + + this.EventHandlers.Add(managedHandler); + this.HasCustomPriorities = this.HasCustomPriorities || managedHandler.HasCustomPriority(); + + if (this.HasCustomPriorities) + this.NeedsSort = true; } /// Remove an event handler. /// The event handler. public void Remove(EventHandler handler) { - this.EventHandlers.Remove(handler); - this.RemoveTracking(handler, this.EventHandlers); + this.EventHandlers.RemoveAll(p => p.Handler == handler); + this.HasCustomPriorities = this.HasCustomPriorities && this.EventHandlers.Any(p => p.HasCustomPriority()); } /// Raise the event and notify all handlers. /// The event arguments to pass. public void Raise(TEventArgs args) { + // sort event handlers by priority + // (This is done here to avoid repeatedly sorting when handlers are added/removed.) + if (this.NeedsSort) + { + this.NeedsSort = false; + this.EventHandlers.Sort(); + } + + // raise if (this.EventHandlers.Count == 0) return; - - this.PerformanceMonitor.Track(this.EventName, () => { - foreach (EventHandler handler in this.CachedInvocationList) + foreach (ManagedEventHandler handler in this.EventHandlers) { try { - this.PerformanceMonitor.Track(this.EventName, this.GetModNameForPerformanceCounters(handler), () => handler.Invoke(null, args)); + this.PerformanceMonitor.Track(this.EventName, this.GetModNameForPerformanceCounters(handler), () => handler.Handler.Invoke(null, args)); } catch (Exception ex) { @@ -124,13 +127,13 @@ namespace StardewModdingAPI.Framework.Events if (this.EventHandlers.Count == 0) return; - foreach (EventHandler handler in this.CachedInvocationList) + foreach (ManagedEventHandler handler in this.EventHandlers) { - if (match(this.GetSourceMod(handler))) + if (match(handler.SourceMod)) { try { - handler.Invoke(null, args); + handler.Handler.Invoke(null, args); } catch (Exception ex) { @@ -146,80 +149,21 @@ namespace StardewModdingAPI.Framework.Events *********/ /// Get the mod name for a given event handler to display in performance monitoring reports. /// The event handler. - private string GetModNameForPerformanceCounters(EventHandler handler) + private string GetModNameForPerformanceCounters(ManagedEventHandler handler) { - IModMetadata mod = this.GetSourceMod(handler); - if (mod == null) - return Constants.GamePerformanceCounterName; + IModMetadata mod = handler.SourceMod; return mod.HasManifest() ? mod.Manifest.UniqueID : mod.DisplayName; } - /// - /// Get the event priority of an event handler. - /// - /// The event handler to get the priority of. - /// The event priority of the event handler. - private EventPriority GetPriorityOfHandler(EventHandler handler) - { - CustomAttributeData attr = handler.Method.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EventPriorityAttribute)); - if (attr == null) - return EventPriority.Normal; - return (EventPriority) attr.ConstructorArguments[0].Value; - } - - /// - /// Sort an invocation list by its priority. - /// - /// The invocation list. - /// An array of the event handlers sorted by their priority. - private EventHandler[] GetCachedInvocationList(IEnumerable> invocationList ) - { - EventHandler[] handlers = invocationList?.ToArray() ?? new EventHandler[0]; - return handlers.OrderBy((h1) => this.GetPriorityOfHandler(h1)).ToArray(); - } - - /// Track an event handler. - /// The mod which added the handler. - /// The event handler. - /// The updated event invocation list. - protected void AddTracking(IModMetadata mod, EventHandler handler, IEnumerable> invocationList) - { - this.SourceMods[handler] = mod; - this.CachedInvocationList = this.GetCachedInvocationList(invocationList); - } - - /// Remove tracking for an event handler. - /// The event handler. - /// The updated event invocation list. - protected void RemoveTracking(EventHandler handler, IEnumerable> invocationList) - { - this.CachedInvocationList = this.GetCachedInvocationList(invocationList); - if (!this.CachedInvocationList.Contains(handler)) // don't remove if there's still a reference to the removed handler (e.g. it was added twice and removed once) - this.SourceMods.Remove(handler); - } - - /// Get the mod which registered the given event handler, if available. - /// The event handler. - protected IModMetadata GetSourceMod(EventHandler handler) - { - return this.SourceMods.TryGetValue(handler, out IModMetadata mod) - ? mod - : null; - } - /// Log an exception from an event handler. /// The event handler instance. /// The exception that was raised. - protected void LogError(EventHandler handler, Exception ex) + protected void LogError(ManagedEventHandler handler, Exception ex) { - IModMetadata mod = this.GetSourceMod(handler); - if (mod != null) - mod.LogAsMod($"This mod failed in the {this.EventName} event. Technical details: \n{ex.GetLogSummary()}", LogLevel.Error); - else - this.Monitor.Log($"A mod failed in the {this.EventName} event. Technical details: \n{ex.GetLogSummary()}", LogLevel.Error); + handler.SourceMod.LogAsMod($"This mod failed in the {this.EventName} event. Technical details: \n{ex.GetLogSummary()}", LogLevel.Error); } } } diff --git a/src/SMAPI/Framework/Events/ManagedEventHandler.cs b/src/SMAPI/Framework/Events/ManagedEventHandler.cs new file mode 100644 index 00000000..87591f63 --- /dev/null +++ b/src/SMAPI/Framework/Events/ManagedEventHandler.cs @@ -0,0 +1,62 @@ +using System; +using StardewModdingAPI.Events; + +namespace StardewModdingAPI.Framework.Events +{ + /// An event handler wrapper which tracks metadata about an event handler. + /// The event arguments type. + internal class ManagedEventHandler : IComparable + { + /********* + ** Accessors + *********/ + /// The event handler method. + public EventHandler Handler { get; } + + /// The order in which the event handler was registered, relative to other handlers for this event. + public int RegistrationOrder { get; } + + /// The event handler priority, relative to other handlers for this event. + public EventPriority Priority { get; } + + /// The mod which registered the handler. + public IModMetadata SourceMod { get; set; } + + + /********* + ** Accessors + *********/ + /// Construct an instance. + /// The event handler method. + /// The order in which the event handler was registered, relative to other handlers for this event. + /// The event handler priority, relative to other handlers for this event. + /// The mod which registered the handler. + public ManagedEventHandler(EventHandler handler, int registrationOrder, EventPriority priority, IModMetadata sourceMod) + { + this.Handler = handler; + this.RegistrationOrder = registrationOrder; + this.Priority = priority; + this.SourceMod = sourceMod; + } + + /// Get whether the event handler has a custom priority value. + public bool HasCustomPriority() + { + return this.Priority != EventPriority.Normal; + } + + /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. + /// An object to compare with this instance. + /// is not the same type as this instance. + public int CompareTo(object obj) + { + if (!(obj is ManagedEventHandler other)) + throw new ArgumentException("Can't compare to an unrelated object type."); + + int priorityCompare = this.Priority.CompareTo(other.Priority); + return priorityCompare != 0 + ? priorityCompare + : this.RegistrationOrder.CompareTo(other.RegistrationOrder); + } + } +} diff --git a/src/SMAPI/Framework/Events/ModDisplayEvents.cs b/src/SMAPI/Framework/Events/ModDisplayEvents.cs index e383eec6..54d40dee 100644 --- a/src/SMAPI/Framework/Events/ModDisplayEvents.cs +++ b/src/SMAPI/Framework/Events/ModDisplayEvents.cs @@ -13,70 +13,70 @@ namespace StardewModdingAPI.Framework.Events /// Raised after a game menu is opened, closed, or replaced. public event EventHandler MenuChanged { - add => this.EventManager.MenuChanged.Add(value); + add => this.EventManager.MenuChanged.Add(value, this.Mod); remove => this.EventManager.MenuChanged.Remove(value); } /// Raised before the game draws anything to the screen in a draw tick, as soon as the sprite batch is opened. The sprite batch may be closed and reopened multiple times after this event is called, but it's only raised once per draw tick. This event isn't useful for drawing to the screen, since the game will draw over it. public event EventHandler Rendering { - add => this.EventManager.Rendering.Add(value); + add => this.EventManager.Rendering.Add(value, this.Mod); remove => this.EventManager.Rendering.Remove(value); } /// Raised after the game draws to the sprite patch in a draw tick, just before the final sprite batch is rendered to the screen. Since the game may open/close the sprite batch multiple times in a draw tick, the sprite batch may not contain everything being drawn and some things may already be rendered to the screen. Content drawn to the sprite batch at this point will be drawn over all vanilla content (including menus, HUD, and cursor). public event EventHandler Rendered { - add => this.EventManager.Rendered.Add(value); + add => this.EventManager.Rendered.Add(value, this.Mod); remove => this.EventManager.Rendered.Remove(value); } /// Raised before the game world is drawn to the screen. This event isn't useful for drawing to the screen, since the game will draw over it. public event EventHandler RenderingWorld { - add => this.EventManager.RenderingWorld.Add(value); + add => this.EventManager.RenderingWorld.Add(value, this.Mod); remove => this.EventManager.RenderingWorld.Remove(value); } /// Raised after the game world is drawn to the sprite patch, before it's rendered to the screen. Content drawn to the sprite batch at this point will be drawn over the world, but under any active menu, HUD elements, or cursor. public event EventHandler RenderedWorld { - add => this.EventManager.RenderedWorld.Add(value); + add => this.EventManager.RenderedWorld.Add(value, this.Mod); remove => this.EventManager.RenderedWorld.Remove(value); } /// When a menu is open ( isn't null), raised before that menu is drawn to the screen. This includes the game's internal menus like the title screen. Content drawn to the sprite batch at this point will appear under the menu. public event EventHandler RenderingActiveMenu { - add => this.EventManager.RenderingActiveMenu.Add(value); + add => this.EventManager.RenderingActiveMenu.Add(value, this.Mod); remove => this.EventManager.RenderingActiveMenu.Remove(value); } /// When a menu is open ( isn't null), raised after that menu is drawn to the sprite batch but before it's rendered to the screen. Content drawn to the sprite batch at this point will appear over the menu and menu cursor. public event EventHandler RenderedActiveMenu { - add => this.EventManager.RenderedActiveMenu.Add(value); + add => this.EventManager.RenderedActiveMenu.Add(value, this.Mod); remove => this.EventManager.RenderedActiveMenu.Remove(value); } /// Raised before drawing the HUD (item toolbar, clock, etc) to the screen. The vanilla HUD may be hidden at this point (e.g. because a menu is open). Content drawn to the sprite batch at this point will appear under the HUD. public event EventHandler RenderingHud { - add => this.EventManager.RenderingHud.Add(value); + add => this.EventManager.RenderingHud.Add(value, this.Mod); remove => this.EventManager.RenderingHud.Remove(value); } /// Raised after drawing the HUD (item toolbar, clock, etc) to the sprite batch, but before it's rendered to the screen. The vanilla HUD may be hidden at this point (e.g. because a menu is open). Content drawn to the sprite batch at this point will appear over the HUD. public event EventHandler RenderedHud { - add => this.EventManager.RenderedHud.Add(value); + add => this.EventManager.RenderedHud.Add(value, this.Mod); remove => this.EventManager.RenderedHud.Remove(value); } /// Raised after the game window is resized. public event EventHandler WindowResized { - add => this.EventManager.WindowResized.Add(value); + add => this.EventManager.WindowResized.Add(value, this.Mod); remove => this.EventManager.WindowResized.Remove(value); } diff --git a/src/SMAPI/Framework/Events/ModGameLoopEvents.cs b/src/SMAPI/Framework/Events/ModGameLoopEvents.cs index c15460fa..a0119bf8 100644 --- a/src/SMAPI/Framework/Events/ModGameLoopEvents.cs +++ b/src/SMAPI/Framework/Events/ModGameLoopEvents.cs @@ -12,84 +12,84 @@ namespace StardewModdingAPI.Framework.Events /// Raised after the game is launched, right before the first update tick. public event EventHandler GameLaunched { - add => this.EventManager.GameLaunched.Add(value); + add => this.EventManager.GameLaunched.Add(value, this.Mod); remove => this.EventManager.GameLaunched.Remove(value); } /// Raised before the game performs its overall update tick (≈60 times per second). public event EventHandler UpdateTicking { - add => this.EventManager.UpdateTicking.Add(value); + add => this.EventManager.UpdateTicking.Add(value, this.Mod); remove => this.EventManager.UpdateTicking.Remove(value); } /// Raised after the game performs its overall update tick (≈60 times per second). public event EventHandler UpdateTicked { - add => this.EventManager.UpdateTicked.Add(value); + add => this.EventManager.UpdateTicked.Add(value, this.Mod); remove => this.EventManager.UpdateTicked.Remove(value); } /// Raised once per second before the game state is updated. public event EventHandler OneSecondUpdateTicking { - add => this.EventManager.OneSecondUpdateTicking.Add(value); + add => this.EventManager.OneSecondUpdateTicking.Add(value, this.Mod); remove => this.EventManager.OneSecondUpdateTicking.Remove(value); } /// Raised once per second after the game state is updated. public event EventHandler OneSecondUpdateTicked { - add => this.EventManager.OneSecondUpdateTicked.Add(value); + add => this.EventManager.OneSecondUpdateTicked.Add(value, this.Mod); remove => this.EventManager.OneSecondUpdateTicked.Remove(value); } /// Raised before the game creates a new save file. public event EventHandler SaveCreating { - add => this.EventManager.SaveCreating.Add(value); + add => this.EventManager.SaveCreating.Add(value, this.Mod); remove => this.EventManager.SaveCreating.Remove(value); } /// Raised after the game finishes creating the save file. public event EventHandler SaveCreated { - add => this.EventManager.SaveCreated.Add(value); + add => this.EventManager.SaveCreated.Add(value, this.Mod); remove => this.EventManager.SaveCreated.Remove(value); } /// Raised before the game begins writes data to the save file. public event EventHandler Saving { - add => this.EventManager.Saving.Add(value); + add => this.EventManager.Saving.Add(value, this.Mod); remove => this.EventManager.Saving.Remove(value); } /// Raised after the game finishes writing data to the save file. public event EventHandler Saved { - add => this.EventManager.Saved.Add(value); + add => this.EventManager.Saved.Add(value, this.Mod); remove => this.EventManager.Saved.Remove(value); } /// Raised after the player loads a save slot and the world is initialized. public event EventHandler SaveLoaded { - add => this.EventManager.SaveLoaded.Add(value); + add => this.EventManager.SaveLoaded.Add(value, this.Mod); remove => this.EventManager.SaveLoaded.Remove(value); } /// Raised after the game begins a new day (including when the player loads a save). public event EventHandler DayStarted { - add => this.EventManager.DayStarted.Add(value); + add => this.EventManager.DayStarted.Add(value, this.Mod); remove => this.EventManager.DayStarted.Remove(value); } /// Raised before the game ends the current day. This happens before it starts setting up the next day and before . public event EventHandler DayEnding { - add => this.EventManager.DayEnding.Add(value); + add => this.EventManager.DayEnding.Add(value, this.Mod); remove => this.EventManager.DayEnding.Remove(value); } @@ -97,14 +97,14 @@ namespace StardewModdingAPI.Framework.Events public event EventHandler TimeChanged { - add => this.EventManager.TimeChanged.Add(value); + add => this.EventManager.TimeChanged.Add(value, this.Mod); remove => this.EventManager.TimeChanged.Remove(value); } /// Raised after the game returns to the title screen. public event EventHandler ReturnedToTitle { - add => this.EventManager.ReturnedToTitle.Add(value); + add => this.EventManager.ReturnedToTitle.Add(value, this.Mod); remove => this.EventManager.ReturnedToTitle.Remove(value); } diff --git a/src/SMAPI/Framework/Events/ModInputEvents.cs b/src/SMAPI/Framework/Events/ModInputEvents.cs index 6a4298b4..ab26ab3e 100644 --- a/src/SMAPI/Framework/Events/ModInputEvents.cs +++ b/src/SMAPI/Framework/Events/ModInputEvents.cs @@ -12,28 +12,28 @@ namespace StardewModdingAPI.Framework.Events /// Raised after the player presses a button on the keyboard, controller, or mouse. public event EventHandler ButtonPressed { - add => this.EventManager.ButtonPressed.Add(value); + add => this.EventManager.ButtonPressed.Add(value, this.Mod); remove => this.EventManager.ButtonPressed.Remove(value); } /// Raised after the player releases a button on the keyboard, controller, or mouse. public event EventHandler ButtonReleased { - add => this.EventManager.ButtonReleased.Add(value); + add => this.EventManager.ButtonReleased.Add(value, this.Mod); remove => this.EventManager.ButtonReleased.Remove(value); } /// Raised after the player moves the in-game cursor. public event EventHandler CursorMoved { - add => this.EventManager.CursorMoved.Add(value); + add => this.EventManager.CursorMoved.Add(value, this.Mod); remove => this.EventManager.CursorMoved.Remove(value); } /// Raised after the player scrolls the mouse wheel. public event EventHandler MouseWheelScrolled { - add => this.EventManager.MouseWheelScrolled.Add(value); + add => this.EventManager.MouseWheelScrolled.Add(value, this.Mod); remove => this.EventManager.MouseWheelScrolled.Remove(value); } diff --git a/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs b/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs index 152c4e0c..2006b2b5 100644 --- a/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs +++ b/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs @@ -12,21 +12,21 @@ namespace StardewModdingAPI.Framework.Events /// Raised after the mod context for a peer is received. This happens before the game approves the connection, so the player doesn't yet exist in the game. This is the earliest point where messages can be sent to the peer via SMAPI. public event EventHandler PeerContextReceived { - add => this.EventManager.PeerContextReceived.Add(value); + add => this.EventManager.PeerContextReceived.Add(value, this.Mod); remove => this.EventManager.PeerContextReceived.Remove(value); } /// Raised after a mod message is received over the network. public event EventHandler ModMessageReceived { - add => this.EventManager.ModMessageReceived.Add(value); + add => this.EventManager.ModMessageReceived.Add(value, this.Mod); remove => this.EventManager.ModMessageReceived.Remove(value); } /// Raised after the connection with a peer is severed. public event EventHandler PeerDisconnected { - add => this.EventManager.PeerDisconnected.Add(value); + add => this.EventManager.PeerDisconnected.Add(value, this.Mod); remove => this.EventManager.PeerDisconnected.Remove(value); } diff --git a/src/SMAPI/Framework/Events/ModPlayerEvents.cs b/src/SMAPI/Framework/Events/ModPlayerEvents.cs index ca7cfd96..240beb8d 100644 --- a/src/SMAPI/Framework/Events/ModPlayerEvents.cs +++ b/src/SMAPI/Framework/Events/ModPlayerEvents.cs @@ -12,21 +12,21 @@ namespace StardewModdingAPI.Framework.Events /// Raised after items are added or removed to a player's inventory. NOTE: this event is currently only raised for the local player. public event EventHandler InventoryChanged { - add => this.EventManager.InventoryChanged.Add(value); + add => this.EventManager.InventoryChanged.Add(value, this.Mod); remove => this.EventManager.InventoryChanged.Remove(value); } /// Raised after a player skill level changes. This happens as soon as they level up, not when the game notifies the player after their character goes to bed. NOTE: this event is currently only raised for the local player. public event EventHandler LevelChanged { - add => this.EventManager.LevelChanged.Add(value); + add => this.EventManager.LevelChanged.Add(value, this.Mod); remove => this.EventManager.LevelChanged.Remove(value); } /// Raised after a player warps to a new location. NOTE: this event is currently only raised for the local player. public event EventHandler Warped { - add => this.EventManager.Warped.Add(value); + add => this.EventManager.Warped.Add(value, this.Mod); remove => this.EventManager.Warped.Remove(value); } diff --git a/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs b/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs index 9388bdb2..1d6788e1 100644 --- a/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs +++ b/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs @@ -12,21 +12,21 @@ namespace StardewModdingAPI.Framework.Events /// Raised when the low-level stage in the game's loading process has changed. This is an advanced event for mods which need to run code at specific points in the loading process. The available stages or when they happen might change without warning in future versions (e.g. due to changes in the game's load process), so mods using this event are more likely to break or have bugs. Most mods should use instead. public event EventHandler LoadStageChanged { - add => this.EventManager.LoadStageChanged.Add(value); + add => this.EventManager.LoadStageChanged.Add(value, this.Mod); remove => this.EventManager.LoadStageChanged.Remove(value); } /// Raised before the game state is updated (≈60 times per second), regardless of normal SMAPI validation. This event is not thread-safe and may be invoked while game logic is running asynchronously. Changes to game state in this method may crash the game or corrupt an in-progress save. Do not use this event unless you're fully aware of the context in which your code will be run. Mods using this event will trigger a stability warning in the SMAPI console. public event EventHandler UnvalidatedUpdateTicking { - add => this.EventManager.UnvalidatedUpdateTicking.Add(value); + add => this.EventManager.UnvalidatedUpdateTicking.Add(value, this.Mod); remove => this.EventManager.UnvalidatedUpdateTicking.Remove(value); } /// Raised after the game state is updated (≈60 times per second), regardless of normal SMAPI validation. This event is not thread-safe and may be invoked while game logic is running asynchronously. Changes to game state in this method may crash the game or corrupt an in-progress save. Do not use this event unless you're fully aware of the context in which your code will be run. Mods using this event will trigger a stability warning in the SMAPI console. public event EventHandler UnvalidatedUpdateTicked { - add => this.EventManager.UnvalidatedUpdateTicked.Add(value); + add => this.EventManager.UnvalidatedUpdateTicked.Add(value, this.Mod); remove => this.EventManager.UnvalidatedUpdateTicked.Remove(value); } diff --git a/src/SMAPI/Framework/Events/ModWorldEvents.cs b/src/SMAPI/Framework/Events/ModWorldEvents.cs index 2ae69669..21b1b664 100644 --- a/src/SMAPI/Framework/Events/ModWorldEvents.cs +++ b/src/SMAPI/Framework/Events/ModWorldEvents.cs @@ -40,28 +40,28 @@ namespace StardewModdingAPI.Framework.Events /// Raised after NPCs are added or removed in a location. public event EventHandler NpcListChanged { - add => this.EventManager.NpcListChanged.Add(value); + add => this.EventManager.NpcListChanged.Add(value, this.Mod); remove => this.EventManager.NpcListChanged.Remove(value); } /// Raised after objects are added or removed in a location. public event EventHandler ObjectListChanged { - add => this.EventManager.ObjectListChanged.Add(value); + add => this.EventManager.ObjectListChanged.Add(value, this.Mod); remove => this.EventManager.ObjectListChanged.Remove(value); } /// Raised after items are added or removed from a chest. public event EventHandler ChestInventoryChanged { - add => this.EventManager.ChestInventoryChanged.Add(value); + add => this.EventManager.ChestInventoryChanged.Add(value, this.Mod); remove => this.EventManager.ChestInventoryChanged.Remove(value); } /// Raised after terrain features (like floors and trees) are added or removed in a location. public event EventHandler TerrainFeatureListChanged { - add => this.EventManager.TerrainFeatureListChanged.Add(value); + add => this.EventManager.TerrainFeatureListChanged.Add(value, this.Mod); remove => this.EventManager.TerrainFeatureListChanged.Remove(value); } diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index de9c955d..c6e69d4e 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -172,7 +172,7 @@ namespace StardewModdingAPI.Framework this.MonitorForGame = this.GetSecondaryMonitor("game"); SCore.PerformanceMonitor = new PerformanceMonitor(this.Monitor); - this.EventManager = new EventManager(this.Monitor, this.ModRegistry, SCore.PerformanceMonitor); + this.EventManager = new EventManager(this.ModRegistry, SCore.PerformanceMonitor); SCore.PerformanceMonitor.InitializePerformanceCounterCollections(this.EventManager); SCore.DeprecationManager = new DeprecationManager(this.Monitor, this.ModRegistry); From da95a906bf8e812ddcd99a90a4d49942f02f5623 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 15 Jun 2020 18:59:05 -0400 Subject: [PATCH 06/11] increase event priority range This can be used in cases where more granular priority is needed. --- src/SMAPI/Events/EventPriority.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SMAPI/Events/EventPriority.cs b/src/SMAPI/Events/EventPriority.cs index e1fb00ac..1efb4e2a 100644 --- a/src/SMAPI/Events/EventPriority.cs +++ b/src/SMAPI/Events/EventPriority.cs @@ -4,12 +4,12 @@ namespace StardewModdingAPI.Events public enum EventPriority { /// Low priority. - Low = 3, + Low = -1000, /// The default priority. - Normal = 2, + Normal = 0, /// High priority. - High = 1 + High = 1000 } } From 02e7318d2b99d311a328746b23a359364575f0c5 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 15 Jun 2020 19:08:02 -0400 Subject: [PATCH 07/11] merge inconsistent event raise methods --- src/SMAPI/Framework/Events/ManagedEvent.cs | 38 ++++++---------------- src/SMAPI/Framework/SGame.cs | 2 +- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index b0f0ae71..b37fb376 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -90,8 +90,13 @@ namespace StardewModdingAPI.Framework.Events /// Raise the event and notify all handlers. /// The event arguments to pass. - public void Raise(TEventArgs args) + /// A lambda which returns true if the event should be raised for the given mod. + public void Raise(TEventArgs args, Func match = null) { + // skip if no handlers + if (this.EventHandlers.Count == 0) + return; + // sort event handlers by priority // (This is done here to avoid repeatedly sorting when handlers are added/removed.) if (this.NeedsSort) @@ -100,13 +105,14 @@ namespace StardewModdingAPI.Framework.Events this.EventHandlers.Sort(); } - // raise - if (this.EventHandlers.Count == 0) - return; + // raise event this.PerformanceMonitor.Track(this.EventName, () => { foreach (ManagedEventHandler handler in this.EventHandlers) { + if (match != null && !match(handler.SourceMod)) + continue; + try { this.PerformanceMonitor.Track(this.EventName, this.GetModNameForPerformanceCounters(handler), () => handler.Handler.Invoke(null, args)); @@ -119,30 +125,6 @@ namespace StardewModdingAPI.Framework.Events }); } - /// Raise the event and notify all handlers. - /// The event arguments to pass. - /// A lambda which returns true if the event should be raised for the given mod. - public void RaiseForMods(TEventArgs args, Func match) - { - if (this.EventHandlers.Count == 0) - return; - - foreach (ManagedEventHandler handler in this.EventHandlers) - { - if (match(handler.SourceMod)) - { - try - { - handler.Handler.Invoke(null, args); - } - catch (Exception ex) - { - this.LogError(handler, ex); - } - } - } - } - /********* ** Private methods diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 2a30b595..23358afb 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -240,7 +240,7 @@ namespace StardewModdingAPI.Framework modIDs.Remove(message.FromModID); // don't send a broadcast back to the sender // raise events - this.Events.ModMessageReceived.RaiseForMods(new ModMessageReceivedEventArgs(message), mod => mod != null && modIDs.Contains(mod.Manifest.UniqueID)); + this.Events.ModMessageReceived.Raise(new ModMessageReceivedEventArgs(message), mod => mod != null && modIDs.Contains(mod.Manifest.UniqueID)); } /// A callback invoked when custom content is removed from the save data to avoid a crash. From 6d1cd7d9b884bddd00675dfdca9f63dc7db1bd1f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 15 Jun 2020 22:14:17 -0400 Subject: [PATCH 08/11] fix merge, update release notes --- docs/release-notes.md | 2 ++ src/SMAPI/Framework/Events/ModMultiplayerEvents.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index 6c9a9649..dd87c1fc 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -18,6 +18,7 @@ * For modders: * Migrated to Harmony 2.0 (see [_migrate to Harmony 2.0_](https://stardewvalleywiki.com/Modding:Migrate_to_Harmony_2.0) for more info). + * Added [event priorities](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Events#Custom_priority) (thanks to spacechase0!). * Added [update subkeys](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Update_checks#Update_subkeys). * Added `Multiplayer.PeerConnected` event. * Added ability to override update keys from the compatibility list. @@ -29,6 +30,7 @@ * Fixed `helper.Reflection` blocking access to game methods/properties that were extended by SMAPI. * Fixed asset propagation for Gil's portraits. * Fixed `.pdb` files ignored for error stack traces for mods rewritten by SMAPI. + * Fixed `ModMessageReceived` event handlers not tracked for performance monitoring. * For SMAPI developers: * Eliminated MongoDB storage in the web services, which complicated the code unnecessarily. The app still uses an abstract interface for storage, so we can wrap a distributed cache in the future if needed. diff --git a/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs b/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs index 64cf0355..2f9b9482 100644 --- a/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs +++ b/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs @@ -19,7 +19,7 @@ namespace StardewModdingAPI.Framework.Events /// Raised after a peer connection is approved by the game. public event EventHandler PeerConnected { - add => this.EventManager.PeerConnected.Add(value); + add => this.EventManager.PeerConnected.Add(value, this.Mod); remove => this.EventManager.PeerConnected.Remove(value); } From dcd2c647a2abd836e8ee20f8ddad6568c9b4fbf2 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 15 Jun 2020 22:17:32 -0400 Subject: [PATCH 09/11] temporarily restore Harmony 1.x support with compile flag (#711) --- docs/release-notes.md | 10 +- docs/technical/smapi.md | 1 + .../Commands/HarmonySummaryCommand.cs | 2 + .../RewriteFacades/AccessToolsFacade.cs | 2 + .../RewriteFacades/HarmonyInstanceFacade.cs | 2 + .../RewriteFacades/HarmonyMethodFacade.cs | 2 + .../Rewriters/Harmony1AssemblyRewriter.cs | 2 + src/SMAPI/Framework/Patching/GamePatcher.cs | 8 ++ src/SMAPI/Framework/Patching/IHarmonyPatch.cs | 8 ++ src/SMAPI/Framework/Patching/PatchHelper.cs | 36 +++++++ src/SMAPI/Framework/SCore.cs | 2 + src/SMAPI/Metadata/InstructionMetadata.cs | 6 ++ src/SMAPI/Patches/DialogueErrorPatch.cs | 94 ++++++++++++++++++- src/SMAPI/Patches/EventErrorPatch.cs | 48 +++++++++- src/SMAPI/Patches/LoadContextPatch.cs | 8 ++ src/SMAPI/Patches/LoadErrorPatch.cs | 8 ++ src/SMAPI/Patches/ObjectErrorPatch.cs | 50 +++++++++- src/SMAPI/Patches/ScheduleErrorPatch.cs | 50 +++++++++- src/SMAPI/SMAPI.csproj | 2 +- 19 files changed, 328 insertions(+), 13 deletions(-) create mode 100644 src/SMAPI/Framework/Patching/PatchHelper.cs diff --git a/docs/release-notes.md b/docs/release-notes.md index dd87c1fc..c47ee835 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,7 +1,12 @@ ← [README](README.md) # Release notes -## Upcoming released +## Upcoming release + 1 +* For modders: + * Migrated to Harmony 2.0 (see [_migrate to Harmony 2.0_](https://stardewvalleywiki.com/Modding:Migrate_to_Harmony_2.0) for more info). + * Added `harmony_summary` console command which lists all current Harmony patches, optionally with a search filter. + +## Upcoming release * For players: * Mod warnings are now listed alphabetically. * MacOS files starting with `._` are now ignored and can no longer cause skipped mods. @@ -17,12 +22,10 @@ * Internal changes to improve performance and reliability. * For modders: - * Migrated to Harmony 2.0 (see [_migrate to Harmony 2.0_](https://stardewvalleywiki.com/Modding:Migrate_to_Harmony_2.0) for more info). * Added [event priorities](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Events#Custom_priority) (thanks to spacechase0!). * Added [update subkeys](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Update_checks#Update_subkeys). * Added `Multiplayer.PeerConnected` event. * Added ability to override update keys from the compatibility list. - * Added `harmony_summary` console command which lists all current Harmony patches, optionally with a search filter. * Harmony mods which use the `[HarmonyPatch(type)]` attribute now work crossplatform. Previously SMAPI couldn't rewrite types in custom attributes for compatibility. * Improved mod rewriting for compatibility: * Fixed rewriting types in custom attributes. @@ -33,6 +36,7 @@ * Fixed `ModMessageReceived` event handlers not tracked for performance monitoring. * For SMAPI developers: + * Added support for bundling a custom Harmony build for upcoming use. * Eliminated MongoDB storage in the web services, which complicated the code unnecessarily. The app still uses an abstract interface for storage, so we can wrap a distributed cache in the future if needed. * Overhauled update checks to simplify individual clients, centralize common logic, and enable upcoming features. * Merged the separate legacy redirects app on AWS into the main app on Azure. diff --git a/docs/technical/smapi.md b/docs/technical/smapi.md index ca8a9c70..3b2d6e56 100644 --- a/docs/technical/smapi.md +++ b/docs/technical/smapi.md @@ -58,6 +58,7 @@ SMAPI uses a small number of conditional compilation constants, which you can se flag | purpose ---- | ------- `SMAPI_FOR_WINDOWS` | Whether SMAPI is being compiled on Windows for players on Windows. Set automatically in `crossplatform.targets`. +`HARMONY_2` | Whether to enable experimental Harmony 2.0 support. Existing Harmony 1._x_ mods will be rewritten automatically for compatibility. ## For SMAPI developers ### Compiling from source diff --git a/src/SMAPI/Framework/Commands/HarmonySummaryCommand.cs b/src/SMAPI/Framework/Commands/HarmonySummaryCommand.cs index 08233feb..8c20fbdd 100644 --- a/src/SMAPI/Framework/Commands/HarmonySummaryCommand.cs +++ b/src/SMAPI/Framework/Commands/HarmonySummaryCommand.cs @@ -1,3 +1,4 @@ +#if HARMONY_2 using System; using System.Collections.Generic; using System.Linq; @@ -166,3 +167,4 @@ namespace StardewModdingAPI.Framework.Commands } } } +#endif diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/AccessToolsFacade.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/AccessToolsFacade.cs index 8e4320b3..102f3364 100644 --- a/src/SMAPI/Framework/ModLoading/RewriteFacades/AccessToolsFacade.cs +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/AccessToolsFacade.cs @@ -1,3 +1,4 @@ +#if HARMONY_2 using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -40,3 +41,4 @@ namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades } } } +#endif diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs index 54b91679..ad6d5e4f 100644 --- a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs @@ -1,3 +1,4 @@ +#if HARMONY_2 using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -80,3 +81,4 @@ namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades } } } +#endif diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyMethodFacade.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyMethodFacade.cs index 44c97401..f3975558 100644 --- a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyMethodFacade.cs +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyMethodFacade.cs @@ -1,3 +1,4 @@ +#if HARMONY_2 using System; using System.Diagnostics.CodeAnalysis; using System.Reflection; @@ -43,3 +44,4 @@ namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades } } } +#endif diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs index 8fed170a..b30d686e 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs @@ -1,3 +1,4 @@ +#if HARMONY_2 using System; using HarmonyLib; using Mono.Cecil; @@ -125,3 +126,4 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters } } } +#endif diff --git a/src/SMAPI/Framework/Patching/GamePatcher.cs b/src/SMAPI/Framework/Patching/GamePatcher.cs index dcad285a..82d7b9c8 100644 --- a/src/SMAPI/Framework/Patching/GamePatcher.cs +++ b/src/SMAPI/Framework/Patching/GamePatcher.cs @@ -1,5 +1,9 @@ using System; +#if HARMONY_2 using HarmonyLib; +#else +using Harmony; +#endif namespace StardewModdingAPI.Framework.Patching { @@ -27,7 +31,11 @@ namespace StardewModdingAPI.Framework.Patching /// The patches to apply. public void Apply(params IHarmonyPatch[] patches) { +#if HARMONY_2 Harmony harmony = new Harmony("SMAPI"); +#else + HarmonyInstance harmony = HarmonyInstance.Create("SMAPI"); +#endif foreach (IHarmonyPatch patch in patches) { try diff --git a/src/SMAPI/Framework/Patching/IHarmonyPatch.cs b/src/SMAPI/Framework/Patching/IHarmonyPatch.cs index 7d5eb3d4..922243fa 100644 --- a/src/SMAPI/Framework/Patching/IHarmonyPatch.cs +++ b/src/SMAPI/Framework/Patching/IHarmonyPatch.cs @@ -1,4 +1,8 @@ +#if HARMONY_2 using HarmonyLib; +#else +using Harmony; +#endif namespace StardewModdingAPI.Framework.Patching { @@ -10,6 +14,10 @@ namespace StardewModdingAPI.Framework.Patching /// Apply the Harmony patch. /// The Harmony instance. +#if HARMONY_2 void Apply(Harmony harmony); +#else + void Apply(HarmonyInstance harmony); +#endif } } diff --git a/src/SMAPI/Framework/Patching/PatchHelper.cs b/src/SMAPI/Framework/Patching/PatchHelper.cs new file mode 100644 index 00000000..d1aa0185 --- /dev/null +++ b/src/SMAPI/Framework/Patching/PatchHelper.cs @@ -0,0 +1,36 @@ +#if !HARMONY_2 +using System; +using System.Collections.Generic; + +namespace StardewModdingAPI.Framework.Patching +{ + /// Provides generic methods for implementing Harmony patches. + internal class PatchHelper + { + /********* + ** Fields + *********/ + /// The interception keys currently being intercepted. + private static readonly HashSet InterceptingKeys = new HashSet(StringComparer.OrdinalIgnoreCase); + + + /********* + ** Public methods + *********/ + /// Track a method that will be intercepted. + /// The intercept key. + /// Returns false if the method was already marked for interception, else true. + public static bool StartIntercept(string key) + { + return PatchHelper.InterceptingKeys.Add(key); + } + + /// Track a method as no longer being intercepted. + /// The intercept key. + public static void StopIntercept(string key) + { + PatchHelper.InterceptingKeys.Remove(key); + } + } +} +#endif diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 530b6754..1a2c97f4 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -511,7 +511,9 @@ namespace StardewModdingAPI.Framework this.Monitor.Log("Type 'help' for help, or 'help ' for a command's usage", LogLevel.Info); this.GameInstance.CommandManager .Add(new HelpCommand(this.GameInstance.CommandManager), this.Monitor) +#if HARMONY_2 .Add(new HarmonySummaryCommand(), this.Monitor) +#endif .Add(new ReloadI18nCommand(this.ReloadTranslations), this.Monitor); // start handling command line input diff --git a/src/SMAPI/Metadata/InstructionMetadata.cs b/src/SMAPI/Metadata/InstructionMetadata.cs index 89430a11..79d7a7a8 100644 --- a/src/SMAPI/Metadata/InstructionMetadata.cs +++ b/src/SMAPI/Metadata/InstructionMetadata.cs @@ -38,8 +38,10 @@ namespace StardewModdingAPI.Metadata // rewrite for Stardew Valley 1.3 yield return new StaticFieldToConstantRewriter(typeof(Game1), "tileSize", Game1.tileSize); +#if HARMONY_2 // rewrite for SMAPI 3.6 (Harmony 1.x => 2.0 update) yield return new Harmony1AssemblyRewriter(); +#endif /**** ** detect mod issues @@ -51,7 +53,11 @@ namespace StardewModdingAPI.Metadata /**** ** detect code which may impact game stability ****/ +#if HARMONY_2 yield return new TypeFinder(typeof(HarmonyLib.Harmony).FullName, InstructionHandleResult.DetectedGamePatch); +#else + yield return new TypeFinder(typeof(Harmony.HarmonyInstance).FullName, InstructionHandleResult.DetectedGamePatch); +#endif yield return new TypeFinder("System.Runtime.CompilerServices.CallSite", InstructionHandleResult.DetectedDynamic); yield return new FieldFinder(typeof(SaveGame).FullName, nameof(SaveGame.serializer), InstructionHandleResult.DetectedSaveSerializer); yield return new FieldFinder(typeof(SaveGame).FullName, nameof(SaveGame.farmerSerializer), InstructionHandleResult.DetectedSaveSerializer); diff --git a/src/SMAPI/Patches/DialogueErrorPatch.cs b/src/SMAPI/Patches/DialogueErrorPatch.cs index cddf29d6..8043eda3 100644 --- a/src/SMAPI/Patches/DialogueErrorPatch.cs +++ b/src/SMAPI/Patches/DialogueErrorPatch.cs @@ -1,11 +1,16 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using HarmonyLib; -using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.Patching; using StardewModdingAPI.Framework.Reflection; using StardewValley; +#if HARMONY_2 +using HarmonyLib; +using StardewModdingAPI.Framework; +#else +using System.Reflection; +using Harmony; +#endif namespace StardewModdingAPI.Patches { @@ -47,6 +52,7 @@ namespace StardewModdingAPI.Patches /// Apply the Harmony patch. /// The Harmony instance. +#if HARMONY_2 public void Apply(Harmony harmony) { harmony.Patch( @@ -58,11 +64,24 @@ namespace StardewModdingAPI.Patches finalizer: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Finalize_NPC_CurrentDialogue)) ); } - +#else + public void Apply(HarmonyInstance harmony) + { + harmony.Patch( + original: AccessTools.Constructor(typeof(Dialogue), new[] { typeof(string), typeof(NPC) }), + prefix: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Before_Dialogue_Constructor)) + ); + harmony.Patch( + original: AccessTools.Property(typeof(NPC), nameof(NPC.CurrentDialogue)).GetMethod, + prefix: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Before_NPC_CurrentDialogue)) + ); + } +#endif /********* ** Private methods *********/ +#if HARMONY_2 /// The method to call after the Dialogue constructor. /// The instance being patched. /// The dialogue being parsed. @@ -102,5 +121,74 @@ namespace StardewModdingAPI.Patches return null; } +#else + + /// The method to call instead of the Dialogue constructor. + /// The instance being patched. + /// The dialogue being parsed. + /// The NPC for which the dialogue is being parsed. + /// Returns whether to execute the original method. + private static bool Before_Dialogue_Constructor(Dialogue __instance, string masterDialogue, NPC speaker) + { + // get private members + bool nameArraysTranslated = DialogueErrorPatch.Reflection.GetField(typeof(Dialogue), "nameArraysTranslated").GetValue(); + IReflectedMethod translateArraysOfStrings = DialogueErrorPatch.Reflection.GetMethod(typeof(Dialogue), "TranslateArraysOfStrings"); + IReflectedMethod parseDialogueString = DialogueErrorPatch.Reflection.GetMethod(__instance, "parseDialogueString"); + IReflectedMethod checkForSpecialDialogueAttributes = DialogueErrorPatch.Reflection.GetMethod(__instance, "checkForSpecialDialogueAttributes"); + IReflectedField> dialogues = DialogueErrorPatch.Reflection.GetField>(__instance, "dialogues"); + + // replicate base constructor + if (dialogues.GetValue() == null) + dialogues.SetValue(new List()); + + // duplicate code with try..catch + try + { + if (!nameArraysTranslated) + translateArraysOfStrings.Invoke(); + __instance.speaker = speaker; + parseDialogueString.Invoke(masterDialogue); + checkForSpecialDialogueAttributes.Invoke(); + } + catch (Exception baseEx) when (baseEx.InnerException is TargetInvocationException invocationEx && invocationEx.InnerException is Exception ex) + { + string name = !string.IsNullOrWhiteSpace(speaker?.Name) ? speaker.Name : null; + DialogueErrorPatch.MonitorForGame.Log($"Failed parsing dialogue string{(name != null ? $" for {name}" : "")}:\n{masterDialogue}\n{ex}", LogLevel.Error); + + parseDialogueString.Invoke("..."); + checkForSpecialDialogueAttributes.Invoke(); + } + + return false; + } + + /// The method to call instead of . + /// The instance being patched. + /// The return value of the original method. + /// The method being wrapped. + /// Returns whether to execute the original method. + private static bool Before_NPC_CurrentDialogue(NPC __instance, ref Stack __result, MethodInfo __originalMethod) + { + const string key = nameof(Before_NPC_CurrentDialogue); + if (!PatchHelper.StartIntercept(key)) + return true; + + try + { + __result = (Stack)__originalMethod.Invoke(__instance, new object[0]); + return false; + } + catch (TargetInvocationException ex) + { + DialogueErrorPatch.MonitorForGame.Log($"Failed loading current dialogue for NPC {__instance.Name}:\n{ex.InnerException ?? ex}", LogLevel.Error); + __result = new Stack(); + return false; + } + finally + { + PatchHelper.StopIntercept(key); + } + } +#endif } } diff --git a/src/SMAPI/Patches/EventErrorPatch.cs b/src/SMAPI/Patches/EventErrorPatch.cs index de9dea29..4dbb25f3 100644 --- a/src/SMAPI/Patches/EventErrorPatch.cs +++ b/src/SMAPI/Patches/EventErrorPatch.cs @@ -1,6 +1,11 @@ -using System; using System.Diagnostics.CodeAnalysis; +#if HARMONY_2 +using System; using HarmonyLib; +#else +using System.Reflection; +using Harmony; +#endif using StardewModdingAPI.Framework.Patching; using StardewValley; @@ -38,6 +43,7 @@ namespace StardewModdingAPI.Patches /// Apply the Harmony patch. /// The Harmony instance. +#if HARMONY_2 public void Apply(Harmony harmony) { harmony.Patch( @@ -45,11 +51,21 @@ namespace StardewModdingAPI.Patches finalizer: new HarmonyMethod(this.GetType(), nameof(EventErrorPatch.Finalize_GameLocation_CheckEventPrecondition)) ); } +#else + public void Apply(HarmonyInstance harmony) + { + harmony.Patch( + original: AccessTools.Method(typeof(GameLocation), "checkEventPrecondition"), + prefix: new HarmonyMethod(this.GetType(), nameof(EventErrorPatch.Before_GameLocation_CheckEventPrecondition)) + ); + } +#endif /********* ** Private methods *********/ +#if HARMONY_2 /// The method to call instead of the GameLocation.CheckEventPrecondition. /// The return value of the original method. /// The precondition to be parsed. @@ -65,5 +81,35 @@ namespace StardewModdingAPI.Patches return null; } +#else + /// The method to call instead of the GameLocation.CheckEventPrecondition. + /// The instance being patched. + /// The return value of the original method. + /// The precondition to be parsed. + /// The method being wrapped. + /// Returns whether to execute the original method. + private static bool Before_GameLocation_CheckEventPrecondition(GameLocation __instance, ref int __result, string precondition, MethodInfo __originalMethod) + { + const string key = nameof(Before_GameLocation_CheckEventPrecondition); + if (!PatchHelper.StartIntercept(key)) + return true; + + try + { + __result = (int)__originalMethod.Invoke(__instance, new object[] { precondition }); + return false; + } + catch (TargetInvocationException ex) + { + __result = -1; + EventErrorPatch.MonitorForGame.Log($"Failed parsing event precondition ({precondition}):\n{ex.InnerException}", LogLevel.Error); + return false; + } + finally + { + PatchHelper.StopIntercept(key); + } + } +#endif } } diff --git a/src/SMAPI/Patches/LoadContextPatch.cs b/src/SMAPI/Patches/LoadContextPatch.cs index 9c707676..768ddd6b 100644 --- a/src/SMAPI/Patches/LoadContextPatch.cs +++ b/src/SMAPI/Patches/LoadContextPatch.cs @@ -1,6 +1,10 @@ using System; using System.Diagnostics.CodeAnalysis; +#if HARMONY_2 using HarmonyLib; +#else +using Harmony; +#endif using StardewModdingAPI.Enums; using StardewModdingAPI.Framework.Patching; using StardewModdingAPI.Framework.Reflection; @@ -47,7 +51,11 @@ namespace StardewModdingAPI.Patches /// Apply the Harmony patch. /// The Harmony instance. +#if HARMONY_2 public void Apply(Harmony harmony) +#else + public void Apply(HarmonyInstance harmony) +#endif { // detect CreatedBasicInfo harmony.Patch( diff --git a/src/SMAPI/Patches/LoadErrorPatch.cs b/src/SMAPI/Patches/LoadErrorPatch.cs index f8ad6693..5e67b169 100644 --- a/src/SMAPI/Patches/LoadErrorPatch.cs +++ b/src/SMAPI/Patches/LoadErrorPatch.cs @@ -2,7 +2,11 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +#if HARMONY_2 using HarmonyLib; +#else +using Harmony; +#endif using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Patching; using StardewValley; @@ -49,7 +53,11 @@ namespace StardewModdingAPI.Patches /// Apply the Harmony patch. /// The Harmony instance. +#if HARMONY_2 public void Apply(Harmony harmony) +#else + public void Apply(HarmonyInstance harmony) +#endif { harmony.Patch( original: AccessTools.Method(typeof(SaveGame), nameof(SaveGame.loadDataToLocations)), diff --git a/src/SMAPI/Patches/ObjectErrorPatch.cs b/src/SMAPI/Patches/ObjectErrorPatch.cs index 189a14a0..4edcc64e 100644 --- a/src/SMAPI/Patches/ObjectErrorPatch.cs +++ b/src/SMAPI/Patches/ObjectErrorPatch.cs @@ -1,11 +1,16 @@ -using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using HarmonyLib; using StardewModdingAPI.Framework.Patching; using StardewValley; using StardewValley.Menus; using SObject = StardewValley.Object; +#if HARMONY_2 +using System; +using HarmonyLib; +#else +using System.Reflection; +using Harmony; +#endif namespace StardewModdingAPI.Patches { @@ -27,7 +32,11 @@ namespace StardewModdingAPI.Patches *********/ /// Apply the Harmony patch. /// The Harmony instance. +#if HARMONY_2 public void Apply(Harmony harmony) +#else + public void Apply(HarmonyInstance harmony) +#endif { // object.getDescription harmony.Patch( @@ -38,7 +47,11 @@ namespace StardewModdingAPI.Patches // object.getDisplayName harmony.Patch( original: AccessTools.Method(typeof(SObject), "loadDisplayName"), +#if HARMONY_2 finalizer: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Finalize_Object_loadDisplayName)) +#else + prefix: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Before_Object_loadDisplayName)) +#endif ); // IClickableMenu.drawToolTip @@ -68,6 +81,7 @@ namespace StardewModdingAPI.Patches return true; } +#if HARMONY_2 /// The method to call after . /// The patched method's return value. /// The exception thrown by the wrapped method, if any. @@ -82,6 +96,38 @@ namespace StardewModdingAPI.Patches return __exception; } +#else + /// The method to call instead of . + /// The instance being patched. + /// The patched method's return value. + /// The method being wrapped. + /// Returns whether to execute the original method. + private static bool Before_Object_loadDisplayName(SObject __instance, ref string __result, MethodInfo __originalMethod) + { + const string key = nameof(Before_Object_loadDisplayName); + if (!PatchHelper.StartIntercept(key)) + return true; + + try + { + __result = (string)__originalMethod.Invoke(__instance, new object[0]); + return false; + } + catch (TargetInvocationException ex) when (ex.InnerException is KeyNotFoundException) + { + __result = "???"; + return false; + } + catch + { + return true; + } + finally + { + PatchHelper.StopIntercept(key); + } + } +#endif /// The method to call instead of . /// The item for which to draw a tooltip. diff --git a/src/SMAPI/Patches/ScheduleErrorPatch.cs b/src/SMAPI/Patches/ScheduleErrorPatch.cs index df6ffab3..cc2238b0 100644 --- a/src/SMAPI/Patches/ScheduleErrorPatch.cs +++ b/src/SMAPI/Patches/ScheduleErrorPatch.cs @@ -1,10 +1,15 @@ -using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using HarmonyLib; -using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.Patching; using StardewValley; +#if HARMONY_2 +using System; +using HarmonyLib; +using StardewModdingAPI.Framework; +#else +using System.Reflection; +using Harmony; +#endif namespace StardewModdingAPI.Patches { @@ -40,11 +45,19 @@ namespace StardewModdingAPI.Patches /// Apply the Harmony patch. /// The Harmony instance. +#if HARMONY_2 public void Apply(Harmony harmony) +#else + public void Apply(HarmonyInstance harmony) +#endif { harmony.Patch( original: AccessTools.Method(typeof(NPC), "parseMasterSchedule"), +#if HARMONY_2 finalizer: new HarmonyMethod(this.GetType(), nameof(ScheduleErrorPatch.Finalize_NPC_parseMasterSchedule)) +#else + prefix: new HarmonyMethod(this.GetType(), nameof(ScheduleErrorPatch.Before_NPC_parseMasterSchedule)) +#endif ); } @@ -52,6 +65,7 @@ namespace StardewModdingAPI.Patches /********* ** Private methods *********/ +#if HARMONY_2 /// The method to call instead of . /// The raw schedule data to parse. /// The instance being patched. @@ -68,5 +82,35 @@ namespace StardewModdingAPI.Patches return null; } +#else + /// The method to call instead of . + /// The raw schedule data to parse. + /// The instance being patched. + /// The patched method's return value. + /// The method being wrapped. + /// Returns whether to execute the original method. + private static bool Before_NPC_parseMasterSchedule(string rawData, NPC __instance, ref Dictionary __result, MethodInfo __originalMethod) + { + const string key = nameof(Before_NPC_parseMasterSchedule); + if (!PatchHelper.StartIntercept(key)) + return true; + + try + { + __result = (Dictionary)__originalMethod.Invoke(__instance, new object[] { rawData }); + return false; + } + catch (TargetInvocationException ex) + { + ScheduleErrorPatch.MonitorForGame.Log($"Failed parsing schedule for NPC {__instance.Name}:\n{rawData}\n{ex.InnerException ?? ex}", LogLevel.Error); + __result = new Dictionary(); + return false; + } + finally + { + PatchHelper.StopIntercept(key); + } + } +#endif } } diff --git a/src/SMAPI/SMAPI.csproj b/src/SMAPI/SMAPI.csproj index 603b6fb5..c17de6d0 100644 --- a/src/SMAPI/SMAPI.csproj +++ b/src/SMAPI/SMAPI.csproj @@ -14,7 +14,7 @@ - + From f63f14c70369541311bb5034894409a5170d56e9 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 16 Jun 2020 18:53:29 -0400 Subject: [PATCH 10/11] fix typo --- src/SMAPI/Framework/SCore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 1a2c97f4..2794002c 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -994,7 +994,7 @@ namespace StardewModdingAPI.Framework } catch (SAssemblyLoadFailedException ex) { - errorReasonPhrase = $"it DLL couldn't be loaded: {ex.Message}"; + errorReasonPhrase = $"its DLL couldn't be loaded: {ex.Message}"; return false; } catch (Exception ex) From c41b92f721bc61f3dd21e56f86557d7cb185197a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 16 Jun 2020 20:14:27 -0400 Subject: [PATCH 11/11] improve new event code This commit... * debounces the has-custom-priorities check; * fixes collection-modified-during-enumeration errors if an event handler is added or removed while the event is being raised; * fixes Remove(handler) removing all instances of the handler instead of the last one. --- src/SMAPI/Framework/Events/ManagedEvent.cs | 56 +++++++++++-------- .../Framework/Events/ManagedEventHandler.cs | 6 -- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index b37fb376..08ac1131 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -14,23 +14,23 @@ namespace StardewModdingAPI.Framework.Events /********* ** Fields *********/ - /// The underlying event handlers. - private readonly List> EventHandlers = new List>(); - /// The mod registry with which to identify mods. protected readonly ModRegistry ModRegistry; /// Tracks performance metrics. private readonly PerformanceMonitor PerformanceMonitor; + /// The underlying event handlers. + private readonly List> Handlers = new List>(); + + /// A cached snapshot of , or null to rebuild it next raise. + private ManagedEventHandler[] CachedHandlers = new ManagedEventHandler[0]; + /// The total number of event handlers registered for this events, regardless of whether they're still registered. private int RegistrationIndex; - /// Whether any registered event handlers have a custom priority value. - private bool HasCustomPriorities; - - /// Whether event handlers should be sorted before the next invocation. - private bool NeedsSort; + /// Whether new handlers were added since the last raise. + private bool HasNewHandlers; /********* @@ -62,7 +62,7 @@ namespace StardewModdingAPI.Framework.Events /// Get whether anything is listening to the event. public bool HasListeners() { - return this.EventHandlers.Count > 0; + return this.Handlers.Count > 0; } /// Add an event handler. @@ -73,19 +73,25 @@ namespace StardewModdingAPI.Framework.Events EventPriority priority = handler.Method.GetCustomAttribute()?.Priority ?? EventPriority.Normal; var managedHandler = new ManagedEventHandler(handler, this.RegistrationIndex++, priority, mod); - this.EventHandlers.Add(managedHandler); - this.HasCustomPriorities = this.HasCustomPriorities || managedHandler.HasCustomPriority(); - - if (this.HasCustomPriorities) - this.NeedsSort = true; + this.Handlers.Add(managedHandler); + this.CachedHandlers = null; + this.HasNewHandlers = true; } /// Remove an event handler. /// The event handler. public void Remove(EventHandler handler) { - this.EventHandlers.RemoveAll(p => p.Handler == handler); - this.HasCustomPriorities = this.HasCustomPriorities && this.EventHandlers.Any(p => p.HasCustomPriority()); + // match C# events: if a handler is listed multiple times, remove the last one added + for (int i = this.Handlers.Count - 1; i >= 0; i--) + { + if (this.Handlers[i].Handler != handler) + continue; + + this.Handlers.RemoveAt(i); + this.CachedHandlers = null; + break; + } } /// Raise the event and notify all handlers. @@ -94,21 +100,25 @@ namespace StardewModdingAPI.Framework.Events public void Raise(TEventArgs args, Func match = null) { // skip if no handlers - if (this.EventHandlers.Count == 0) + if (this.Handlers.Count == 0) return; - // sort event handlers by priority - // (This is done here to avoid repeatedly sorting when handlers are added/removed.) - if (this.NeedsSort) + // update cached data + // (This is debounced here to avoid repeatedly sorting when handlers are added/removed, + // and keeping a separate cached list allows changes during enumeration.) + if (this.CachedHandlers == null) { - this.NeedsSort = false; - this.EventHandlers.Sort(); + if (this.HasNewHandlers && this.Handlers.Any(p => p.Priority != EventPriority.Normal)) + this.Handlers.Sort(); + + this.CachedHandlers = this.Handlers.ToArray(); + this.HasNewHandlers = false; } // raise event this.PerformanceMonitor.Track(this.EventName, () => { - foreach (ManagedEventHandler handler in this.EventHandlers) + foreach (ManagedEventHandler handler in this.CachedHandlers) { if (match != null && !match(handler.SourceMod)) continue; diff --git a/src/SMAPI/Framework/Events/ManagedEventHandler.cs b/src/SMAPI/Framework/Events/ManagedEventHandler.cs index 87591f63..cf470c1e 100644 --- a/src/SMAPI/Framework/Events/ManagedEventHandler.cs +++ b/src/SMAPI/Framework/Events/ManagedEventHandler.cs @@ -39,12 +39,6 @@ namespace StardewModdingAPI.Framework.Events this.SourceMod = sourceMod; } - /// Get whether the event handler has a custom priority value. - public bool HasCustomPriority() - { - return this.Priority != EventPriority.Normal; - } - /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. /// An object to compare with this instance. /// is not the same type as this instance.