From fc29fe918a89623544b011c76217aa1ea1975d00 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 15 Jun 2020 18:58:05 -0400 Subject: refactor & optimize event code a bit, drop old support for unknown event handlers --- src/SMAPI/Framework/Events/ManagedEventHandler.cs | 62 +++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/SMAPI/Framework/Events/ManagedEventHandler.cs (limited to 'src/SMAPI/Framework/Events/ManagedEventHandler.cs') 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); + } + } +} -- cgit