From 6437d2a9774239f092d4c05db6c93875b05d51d6 Mon Sep 17 00:00:00 2001 From: high Date: Fri, 30 Dec 2011 20:32:03 -0500 Subject: [PATCH] Added HandlerList.cs so that we can have prioritized event handlers also so you can easily register a handler and not have to put 'if (e.Handled) return;' at the top. --- TShockAPI/GetDataHandlers.cs | 7 +--- TShockAPI/HandlerList.cs | 79 ++++++++++++++++++++++++++++++++++++ TShockAPI/TShockAPI.csproj | 3 +- 3 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 TShockAPI/HandlerList.cs diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index f18dca77..7c9b2dbd 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -58,12 +58,9 @@ namespace TShockAPI public float Type { get; set; } public float EditType { get; set; } } - public static event EventHandler TileEdit; + public static HandlerList TileEdit = new HandlerList(); public static bool OnTileEdit(float x, float y, float type, float editType) { - if (TileEdit == null) - return false; - var args = new TileEditEventArgs { X = x, @@ -71,7 +68,7 @@ namespace TShockAPI Type = type, EditType = editType }; - TileEdit(null, args); + TileEdit.Invoke(null, args); return args.Handled; } #endregion diff --git a/TShockAPI/HandlerList.cs b/TShockAPI/HandlerList.cs new file mode 100644 index 00000000..f411efc5 --- /dev/null +++ b/TShockAPI/HandlerList.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; + +namespace TShockAPI +{ + public class HandlerList : HandlerList + { + } + public class HandlerList where T : EventArgs + { + protected class HandlerObject + { + public EventHandler Handler { get; set; } + public HandlerPriority Priority { get; set; } + public bool GetHandled { get; set; } + } + + protected object HandlerLock = new object(); + protected List Handlers { get; set; } + public HandlerList() + { + Handlers = new List(); + } + + /// + /// Register a handler + /// + /// Delegate to be called + /// Priority of the delegate + /// Should the handler receive a call even if it has been handled + public void Register(EventHandler handler, HandlerPriority priority = HandlerPriority.Normal, bool gethandled = false) + { + lock (HandlerLock) + { + Handlers.Add(new HandlerObject { Handler = handler, Priority = priority, GetHandled = gethandled }); + Handlers = Handlers.OrderBy(h => (int)h.Priority).ToList(); + } + } + + public void UnRegister(EventHandler handler) + { + lock (HandlerLock) + { + Handlers.RemoveAll(h => h.Handler.Equals(handler)); + } + } + + public void Invoke(object sender, T e) + { + List handlers; + lock (HandlerLock) + { + //Copy the list for invoking as to not keep it locked during the invokes + handlers = new List(Handlers); + } + + var hargs = e as HandledEventArgs; + for (int i = 0; i < handlers.Count; i++) + { + if (hargs == null || !hargs.Handled || (hargs.Handled && handlers[i].GetHandled)) + { + handlers[i].Handler(sender, e); + } + } + } + } + + public enum HandlerPriority + { + Highest = 1, + High = 2, + Normal = 3, + Low = 4, + Lowest = 5, + } +} diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index 6149414f..717b35e4 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -96,6 +96,7 @@ + @@ -183,7 +184,7 @@ - +