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; } /// 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); // higher value = sort first return priorityCompare != 0 ? priorityCompare : this.RegistrationOrder.CompareTo(other.RegistrationOrder); } } }