260 lines
8.2 KiB
C#
260 lines
8.2 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Diagnostics;
|
|||
|
|
|||
|
namespace Thousandto.CoreSDK.Event
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 事件系统管理器
|
|||
|
/// </summary>
|
|||
|
public class SDKEventManager
|
|||
|
{
|
|||
|
#region //静态变量
|
|||
|
|
|||
|
//静态变量
|
|||
|
protected static SDKEventManager _instance = null;
|
|||
|
|
|||
|
//静态属性
|
|||
|
public static SDKEventManager SharedInstance
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_instance == null)
|
|||
|
{
|
|||
|
_instance = new SDKEventManager();
|
|||
|
}
|
|||
|
return _instance;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region //成员变量
|
|||
|
protected Dictionary<int, List<EventSystemHandler>> _events = null;
|
|||
|
#endregion
|
|||
|
|
|||
|
#region//构造函数
|
|||
|
private SDKEventManager()
|
|||
|
{
|
|||
|
_events = new Dictionary<int, List<EventSystemHandler>>(2048);
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region//公共接口
|
|||
|
/// <summary>
|
|||
|
/// 注册事件函数调用。
|
|||
|
/// </summary>
|
|||
|
/// <returns>返回 True 表明回调添加成功,返回 False 表明回调添加失败。</returns>
|
|||
|
public bool RegFixEventHandle(int eventType, EventSystemHandler eventHandler)
|
|||
|
{
|
|||
|
//一次只能注册一个回调函数,防止函数重复注册
|
|||
|
if (eventHandler == null)
|
|||
|
{
|
|||
|
Trace.Fail(string.Format("RegFixEventHandle:eventType={0},eventHandler=null", eventType));
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
List<EventSystemHandler> list;
|
|||
|
if (!_events.TryGetValue(eventType, out list))
|
|||
|
{
|
|||
|
list = new List<EventSystemHandler>();
|
|||
|
_events[eventType] = list;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (IndexOf(list, eventHandler) >= 0)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
list.Add(eventHandler);
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 取消某个事件下的函数调用注册。
|
|||
|
/// </summary>
|
|||
|
public void UnRegFixEventHandle(int eventType, EventSystemHandler eventHandler)
|
|||
|
{
|
|||
|
if (eventHandler == null)
|
|||
|
return;
|
|||
|
|
|||
|
List<EventSystemHandler> list;
|
|||
|
if (_events.TryGetValue(eventType, out list))
|
|||
|
{
|
|||
|
int idx = IndexOf(list, eventHandler);
|
|||
|
if (idx >= 0)
|
|||
|
{
|
|||
|
list.RemoveAt(idx);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 删除某个事件类型。
|
|||
|
/// </summary>
|
|||
|
public void ClearFixEvent(int eventType)
|
|||
|
{
|
|||
|
List<EventSystemHandler> list; //不是引用!
|
|||
|
if (_events.TryGetValue(eventType, out list))
|
|||
|
{
|
|||
|
list.Clear();
|
|||
|
_events.Remove(eventType);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 清除所有的事件。
|
|||
|
/// </summary>
|
|||
|
public void ClearAllFixEvent()
|
|||
|
{
|
|||
|
_events.Clear();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 发送事件。
|
|||
|
/// </summary>
|
|||
|
public void PushFixEvent(int eventType)
|
|||
|
{
|
|||
|
PushFixEvent(null, eventType, null);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 发送事件。
|
|||
|
/// </summary>
|
|||
|
public void PushFixEvent(int eventType, object sender)
|
|||
|
{
|
|||
|
PushFixEvent(null, eventType, sender);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 发送事件。
|
|||
|
/// </summary>
|
|||
|
public void PushFixEvent(object target, int eventType, object sender)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
List<EventSystemHandler> list;
|
|||
|
if (_events.TryGetValue(eventType, out list))
|
|||
|
{
|
|||
|
if (list != null && list.Count > 0)
|
|||
|
{
|
|||
|
if (target == null)
|
|||
|
{//执行方法链里的所有方法
|
|||
|
for (int i = 0; i < list.Count; i++)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
list[i](sender);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Trace.Fail(ex.Message, ex.StackTrace);
|
|||
|
list.RemoveAt(i);
|
|||
|
i--;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
for (int i = 0; i < list.Count; i++)
|
|||
|
{
|
|||
|
if (list[i].Target == target)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
list[i](sender);
|
|||
|
break;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Trace.Fail(ex.Message, ex.StackTrace);
|
|||
|
list.RemoveAt(i);
|
|||
|
i--;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Trace.Fail(ex.Message, ex.StackTrace);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查事件定义是否有效
|
|||
|
/// </summary>
|
|||
|
/// <param name="type"></param>
|
|||
|
public void CheckEventDefineIsValid(Type type)
|
|||
|
{
|
|||
|
//获取所有常量定义
|
|||
|
var fields = type.GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
|
|||
|
//定义一个常量定义的数组
|
|||
|
int[] arrEventValues = new int[fields.Length];
|
|||
|
//针对每个事件类都有一个基础值,定义为EID_BASE的值
|
|||
|
int baseID = -1;
|
|||
|
for (int i = 0; i < fields.Length; i++)
|
|||
|
{
|
|||
|
arrEventValues[i] = (int)fields[i].GetValue(null);
|
|||
|
if (baseID == -1 && fields[i].Name == "EID_BASE")
|
|||
|
{
|
|||
|
baseID = arrEventValues[i];
|
|||
|
}
|
|||
|
}
|
|||
|
//输出当前类的定义的数量
|
|||
|
Trace.Fail("当前类["+ type.Name + "]定义的事件数:" + fields.Length);
|
|||
|
//判断是否重复
|
|||
|
bool bDuplication = false;
|
|||
|
for (int i = 0; i < arrEventValues.Length; i++)
|
|||
|
{
|
|||
|
for (int j = 0; j < arrEventValues.Length; j++)
|
|||
|
{
|
|||
|
if (i != j && arrEventValues[i] == arrEventValues[j])
|
|||
|
{
|
|||
|
if (baseID > 0)
|
|||
|
{
|
|||
|
Trace.Fail("当前事件定义重复: EID_BASE + " + (arrEventValues[i] - baseID));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Trace.Fail("当前事件定义重复: " + arrEventValues[i]);
|
|||
|
}
|
|||
|
bDuplication = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
//如果有有重复,那么就触发异常
|
|||
|
if (bDuplication)
|
|||
|
{
|
|||
|
throw new Exception("当前类[" + type.Name + "]事件定义异常!!");
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region//私有函数
|
|||
|
|
|||
|
//从列表中获取Handler
|
|||
|
private int IndexOf(List<EventSystemHandler> list, EventSystemHandler handler)
|
|||
|
{
|
|||
|
for (int i = 0; i < list.Count; i++)
|
|||
|
{
|
|||
|
if (list[i].Equals(handler))
|
|||
|
{
|
|||
|
return i;
|
|||
|
}
|
|||
|
}
|
|||
|
return -1;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
|
|||
|
//事件系统的处理句柄委托
|
|||
|
public delegate void EventSystemHandler(object sender);
|
|||
|
}
|