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.

This commit is contained in:
high 2011-12-30 20:32:03 -05:00
parent bf50a82cbd
commit 6437d2a977
3 changed files with 83 additions and 6 deletions

View file

@ -58,12 +58,9 @@ namespace TShockAPI
public float Type { get; set; }
public float EditType { get; set; }
}
public static event EventHandler<TileEditEventArgs> TileEdit;
public static HandlerList<TileEditEventArgs> TileEdit = new HandlerList<TileEditEventArgs>();
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

79
TShockAPI/HandlerList.cs Normal file
View file

@ -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<EventArgs>
{
}
public class HandlerList<T> where T : EventArgs
{
protected class HandlerObject
{
public EventHandler<T> Handler { get; set; }
public HandlerPriority Priority { get; set; }
public bool GetHandled { get; set; }
}
protected object HandlerLock = new object();
protected List<HandlerObject> Handlers { get; set; }
public HandlerList()
{
Handlers = new List<HandlerObject>();
}
/// <summary>
/// Register a handler
/// </summary>
/// <param name="handler">Delegate to be called</param>
/// <param name="priority">Priority of the delegate</param>
/// <param name="gethandled">Should the handler receive a call even if it has been handled</param>
public void Register(EventHandler<T> 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<T> handler)
{
lock (HandlerLock)
{
Handlers.RemoveAll(h => h.Handler.Equals(handler));
}
}
public void Invoke(object sender, T e)
{
List<HandlerObject> handlers;
lock (HandlerLock)
{
//Copy the list for invoking as to not keep it locked during the invokes
handlers = new List<HandlerObject>(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,
}
}

View file

@ -96,6 +96,7 @@
<Compile Include="Extensions\RandomExt.cs" />
<Compile Include="Extensions\StringExt.cs" />
<Compile Include="GeoIPCountry.cs" />
<Compile Include="HandlerList.cs" />
<Compile Include="IPackable.cs" />
<Compile Include="Commands.cs" />
<Compile Include="ConfigFile.cs" />
@ -183,7 +184,7 @@
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" />
<UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.