summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Events/ManagedEventHandler.cs
blob: 28e88be02ccdcfca5e7904c24b540e6ce1507c2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using StardewModdingAPI.Events;

namespace StardewModdingAPI.Framework.Events
{
    /// <summary>An event handler wrapper which tracks metadata about an event handler.</summary>
    /// <typeparam name="TEventArgs">The event arguments type.</typeparam>
    internal class ManagedEventHandler<TEventArgs> : IComparable
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The event handler method.</summary>
        public EventHandler<TEventArgs> Handler { get; }

        /// <summary>The order in which the event handler was registered, relative to other handlers for this event.</summary>
        public int RegistrationOrder { get; }

        /// <summary>The event handler priority, relative to other handlers for this event.</summary>
        public EventPriority Priority { get; }

        /// <summary>The mod which registered the handler.</summary>
        public IModMetadata SourceMod { get; set; }


        /*********
        ** Accessors
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="handler">The event handler method.</param>
        /// <param name="registrationOrder">The order in which the event handler was registered, relative to other handlers for this event.</param>
        /// <param name="priority">The event handler priority, relative to other handlers for this event.</param>
        /// <param name="sourceMod">The mod which registered the handler.</param>
        public ManagedEventHandler(EventHandler<TEventArgs> handler, int registrationOrder, EventPriority priority, IModMetadata sourceMod)
        {
            this.Handler = handler;
            this.RegistrationOrder = registrationOrder;
            this.Priority = priority;
            this.SourceMod = sourceMod;
        }

        /// <inheritdoc />
        public int CompareTo(object obj)
        {
            if (!(obj is ManagedEventHandler<TEventArgs> 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);
        }
    }
}