From f1322f0a76c878052e475cb0c8ea2b2b1a31576c Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Wed, 3 Aug 2022 19:25:33 +0800 Subject: [PATCH] Update GameEvent for MemPool Update GameEvent for MemPool --- Assets/TEngine/Runtime/Event/GameEvent.cs | 606 ++++++++++++++++++ .../TEngine/Runtime/Event/GameEvent.cs.meta | 11 + Assets/TEngine/Runtime/Event/GameEventMgr.cs | 351 ++-------- 3 files changed, 681 insertions(+), 287 deletions(-) create mode 100644 Assets/TEngine/Runtime/Event/GameEvent.cs create mode 100644 Assets/TEngine/Runtime/Event/GameEvent.cs.meta diff --git a/Assets/TEngine/Runtime/Event/GameEvent.cs b/Assets/TEngine/Runtime/Event/GameEvent.cs new file mode 100644 index 00000000..221d0a8b --- /dev/null +++ b/Assets/TEngine/Runtime/Event/GameEvent.cs @@ -0,0 +1,606 @@ +using System; +using System.Collections.Generic; + +namespace TEngine +{ + #region EventInfo + internal interface IEventInfo + { + void Free(); + } + + public class EventInfo : IEventInfo, IMemPoolObject + { + public Action actions; + public EventInfo() + { + + } + + public EventInfo(Action action) + { + actions += action; + } + + public void Init() + { + actions = null; + } + + public void Destroy() + { + actions = null; + } + + public void Free() + { + GameMemPool.Free(this); + } + } + + + public class EventInfo : IEventInfo, IMemPoolObject + { + public Action actions; + + public EventInfo() + { + + } + + public EventInfo(Action action) + { + actions += action; + } + + public void Init() + { + actions = null; + } + + public void Destroy() + { + actions = null; + } + + public void Free() + { + GameMemPool>.Free(this); + } + } + + public class EventInfo : IEventInfo, IMemPoolObject + { + public Action actions; + + public EventInfo() + { + + } + + public EventInfo(Action action) + { + actions += action; + } + + public void Init() + { + actions = null; + } + + public void Destroy() + { + actions = null; + } + + public void Free() + { + GameMemPool>.Free(this); + } + } + + public class EventInfo : IEventInfo, IMemPoolObject + { + public Action actions; + + public EventInfo() + { + + } + + public EventInfo(Action action) + { + actions += action; + } + + public void Init() + { + actions = null; + } + + public void Destroy() + { + actions = null; + } + + public void Free() + { + GameMemPool>.Free(this); + } + } + #endregion + + public class GameEvent : IMemPoolObject + { + public void Init() + { + + } + + public void Destroy() + { + Clear(); + } + + /// + /// Dictionary Key->Int.32 Value->EventInfo,调用频率高建议使用int事件,减少字典内String的哈希碰撞 + /// + private Dictionary _eventDic = new Dictionary(); + + /// + /// Dictionary Key->string Value->EventInfo,调用频率不高的时候可以使用 + /// + private Dictionary m_eventStrDic = new Dictionary(); + + #region AddEventListener + public void AddEventListener(int eventId, Action action) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo!= null) + { + eventInfo.actions += action; + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = GameMemPool>.Alloc(); + eventInfo.actions += action; + _eventDic.Add(eventId, eventInfo); + } + } + + public void AddEventListener(int eventId, Action action) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions += action; + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = GameMemPool>.Alloc(); + eventInfo.actions += action; + _eventDic.Add(eventId, eventInfo); + } + } + + public void AddEventListener(int eventId, Action action) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions += action; + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = GameMemPool>.Alloc(); + eventInfo.actions += action; + _eventDic.Add(eventId, eventInfo); + } + } + + public void AddEventListener(int eventId, Action action) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions += action; + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = GameMemPool.Alloc(); + eventInfo.actions += action; + _eventDic.Add(eventId, eventInfo); + } + } + #endregion + + #region RemoveEventListener + public void RemoveEventListener(int eventId, Action action) + { + if (action == null) + { + return; + } + + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions -= action; + } + else + { + throw new Exception("The Same GameEventId RemoveEventListener Need Same Args"); + } + } + } + + public void RemoveEventListener(int eventId, Action action) + { + if (action == null) + { + return; + } + + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions -= action; + } + else + { + throw new Exception("The Same GameEventId RemoveEventListener Need Same Args"); + } + } + } + + public void RemoveEventListener(int eventId, Action action) + { + if (action == null) + { + return; + } + + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions -= action; + } + else + { + throw new Exception("The Same GameEventId RemoveEventListener Need Same Args"); + } + } + } + + public void RemoveEventListener(int eventId, Action action) + { + if (action == null) + { + return; + } + + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions -= action; + } + else + { + throw new Exception("The Same GameEventId RemoveEventListener Need Same Args"); + } + } + } + #endregion + + #region Send + public void Send(int eventId, T info) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions.Invoke(info); + } + } + } + + public void Send(int eventId, T info, U info2) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions.Invoke(info, info2); + } + } + } + + public void Send(int eventId, T info, U info2, W info3) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions.Invoke(info, info2, info3); + } + } + } + + /// + /// 事件触发 无参 + /// + /// + public void Send(int eventId) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions.Invoke(); + } + } + } + #endregion + + #region StringEvent + #region AddEventListener + public void AddEventListener(string eventId, Action action) + { + if (m_eventStrDic.ContainsKey(eventId)) + { + var eventInfo = m_eventStrDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions += action; + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = GameMemPool>.Alloc(); + eventInfo.actions += action; + m_eventStrDic.Add(eventId, eventInfo); + } + } + + public void AddEventListener(string eventId, Action action) + { + if (m_eventStrDic.ContainsKey(eventId)) + { + var eventInfo = m_eventStrDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions += action; + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = GameMemPool>.Alloc(); + eventInfo.actions += action; + m_eventStrDic.Add(eventId, eventInfo); + } + } + + public void AddEventListener(string eventId, Action action) + { + if (m_eventStrDic.ContainsKey(eventId)) + { + var eventInfo = m_eventStrDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions += action; + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = GameMemPool>.Alloc(); + eventInfo.actions += action; + m_eventStrDic.Add(eventId, eventInfo); + } + } + + public void AddEventListener(string eventId, Action action) + { + if (m_eventStrDic.ContainsKey(eventId)) + { + var eventInfo = m_eventStrDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.actions += action; + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = GameMemPool.Alloc(); + eventInfo.actions += action; + m_eventStrDic.Add(eventId, eventInfo); + } + } + #endregion + + #region RemoveEventListener + public void RemoveEventListener(string eventId, Action action) + { + if (action == null) + { + return; + } + + if (m_eventStrDic.ContainsKey(eventId)) + { + (m_eventStrDic[eventId] as EventInfo).actions -= action; + } + } + + public void RemoveEventListener(string eventId, Action action) + { + if (action == null) + { + return; + } + + if (m_eventStrDic.ContainsKey(eventId)) + { + (m_eventStrDic[eventId] as EventInfo).actions -= action; + } + } + + public void RemoveEventListener(string eventId, Action action) + { + if (action == null) + { + return; + } + + if (m_eventStrDic.ContainsKey(eventId)) + { + (m_eventStrDic[eventId] as EventInfo).actions -= action; + } + } + + public void RemoveEventListener(string eventId, Action action) + { + if (action == null) + { + return; + } + + if (m_eventStrDic.ContainsKey(eventId)) + { + (m_eventStrDic[eventId] as EventInfo).actions -= action; + } + } + #endregion + + #region Send + public void Send(string eventId, T info) + { + if (m_eventStrDic.ContainsKey(eventId)) + { + var eventInfo = (m_eventStrDic[eventId] as EventInfo); + if (eventInfo != null) + { + eventInfo.actions.Invoke(info); + } + } + } + + public void Send(string eventId, T info, U info2) + { + if (m_eventStrDic.ContainsKey(eventId)) + { + var eventInfo = (m_eventStrDic[eventId] as EventInfo); + if (eventInfo != null) + { + eventInfo.actions.Invoke(info, info2); + } + } + } + + public void Send(string eventId, T info, U info2, W info3) + { + if (m_eventStrDic.ContainsKey(eventId)) + { + var eventInfo = (m_eventStrDic[eventId] as EventInfo); + if (eventInfo != null) + { + eventInfo.actions.Invoke(info, info2, info3); + } + } + } + + public void Send(string eventId) + { + if (m_eventStrDic.ContainsKey(eventId)) + { + var eventInfo = (m_eventStrDic[eventId] as EventInfo); + if (eventInfo != null) + { + eventInfo.actions.Invoke(); + } + } + } + #endregion + #endregion + + #region Clear + public void Clear() + { + var etr = _eventDic.GetEnumerator(); + while (etr.MoveNext()) + { + var eventInfo = etr.Current.Value; + eventInfo.Free(); + } + etr.Dispose(); + + var etrStr = m_eventStrDic.GetEnumerator(); + while (etrStr.MoveNext()) + { + var eventInfo = etrStr.Current.Value; + eventInfo.Free(); + } + etrStr.Dispose(); + + _eventDic.Clear(); + m_eventStrDic.Clear(); + } + #endregion + } + +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/Event/GameEvent.cs.meta b/Assets/TEngine/Runtime/Event/GameEvent.cs.meta new file mode 100644 index 00000000..b8163b74 --- /dev/null +++ b/Assets/TEngine/Runtime/Event/GameEvent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8bb87835faef0ad4f9bf016b5df511f0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Event/GameEventMgr.cs b/Assets/TEngine/Runtime/Event/GameEventMgr.cs index 5417a78f..6d67c087 100644 --- a/Assets/TEngine/Runtime/Event/GameEventMgr.cs +++ b/Assets/TEngine/Runtime/Event/GameEventMgr.cs @@ -1,381 +1,159 @@ using System; -using System.Collections.Generic; -using TEngine; namespace TEngine { - #region EventInfo - internal interface IEventInfo - { - - } - - public class EventInfo : IEventInfo - { - public Action actions; - - public EventInfo(Action action) - { - actions += action; - } - } - - - public class EventInfo : IEventInfo - { - public Action actions; - - public EventInfo(Action action) - { - actions += action; - } - } - - public class EventInfo : IEventInfo - { - public Action actions; - - public EventInfo(Action action) - { - actions += action; - } - } - - public class EventInfo : IEventInfo - { - public Action actions; - - public EventInfo(Action action) - { - actions += action; - } - } - #endregion /// /// 总观察者 - 总事件中心系统 /// public class GameEventMgr : TSingleton { - /// - /// Dictionary Key->Int.32 Value->EventInfo,调用频率高建议使用int事件,减少字典内String的哈希碰撞 - /// - private Dictionary m_eventDic = new Dictionary(); + private GameEvent _gameEvent; - /// - /// Dictionary Key->string Value->EventInfo,调用频率不高的时候可以使用 - /// - private Dictionary m_eventStrDic = new Dictionary(); + public GameEventMgr() + { + _gameEvent = GameMemPool.Alloc(); + } + + public override void Release() + { + GameMemPool.Free(_gameEvent); + } + + public override void Active() + { + base.Active(); + } #region AddEventListener - public void AddEventListener(int eventid, Action action) + public void AddEventListener(int eventId, Action action) { - if (m_eventDic.ContainsKey(eventid)) - { - (m_eventDic[eventid] as EventInfo).actions += action; - } - else - { - m_eventDic.Add(eventid, new EventInfo(action)); - } + _gameEvent.AddEventListener(eventId, action); } - public void AddEventListener(int eventid, Action action) + public void AddEventListener(int eventId, Action action) { - if (m_eventDic.ContainsKey(eventid)) - { - (m_eventDic[eventid] as EventInfo).actions += action; - } - else - { - m_eventDic.Add(eventid, new EventInfo(action)); - } + _gameEvent.AddEventListener(eventId, action); } - public void AddEventListener(int eventid, Action action) + public void AddEventListener(int eventId, Action action) { - if (m_eventDic.ContainsKey(eventid)) - { - (m_eventDic[eventid] as EventInfo).actions += action; - } - else - { - m_eventDic.Add(eventid, new EventInfo(action)); - } + _gameEvent.AddEventListener(eventId, action); } - public void AddEventListener(int eventid, Action action) + public void AddEventListener(int eventId, Action action) { - if (m_eventDic.ContainsKey(eventid)) - { - (m_eventDic[eventid] as EventInfo).actions += action; - } - else - { - m_eventDic.Add(eventid, new EventInfo(action)); - } + _gameEvent.AddEventListener(eventId, action); } #endregion #region RemoveEventListener - public void RemoveEventListener(int eventid, Action action) + public void RemoveEventListener(int eventId, Action action) { - if (action == null) - { - return; - } - - if (m_eventDic.ContainsKey(eventid)) - { - (m_eventDic[eventid] as EventInfo).actions -= action; - } + _gameEvent.RemoveEventListener(eventId, action); } - public void RemoveEventListener(int eventid, Action action) + public void RemoveEventListener(int eventId, Action action) { - if (action == null) - { - return; - } - - if (m_eventDic.ContainsKey(eventid)) - { - (m_eventDic[eventid] as EventInfo).actions -= action; - } + _gameEvent.RemoveEventListener(eventId, action); } - public void RemoveEventListener(int eventid, Action action) + public void RemoveEventListener(int eventId, Action action) { - if (action == null) - { - return; - } - - if (m_eventDic.ContainsKey(eventid)) - { - (m_eventDic[eventid] as EventInfo).actions -= action; - } + _gameEvent.RemoveEventListener(eventId, action); } - public void RemoveEventListener(int eventid, Action action) + public void RemoveEventListener(int eventId, Action action) { - if (action == null) - { - return; - } - - if (m_eventDic.ContainsKey(eventid)) - { - (m_eventDic[eventid] as EventInfo).actions -= action; - } + _gameEvent.RemoveEventListener(eventId, action); } #endregion #region Send - public void Send(int eventid, T info) + public void Send(int eventId, T info) { - if (m_eventDic.ContainsKey(eventid)) - { - var eventInfo = (m_eventDic[eventid] as EventInfo); - if (eventInfo != null) - { - eventInfo.actions.Invoke(info); - } - } + _gameEvent.Send(eventId, info); } - public void Send(int eventid, T info, U info2) + public void Send(int eventId, T info, U info2) { - if (m_eventDic.ContainsKey(eventid)) - { - var eventInfo = (m_eventDic[eventid] as EventInfo); - if (eventInfo != null) - { - eventInfo.actions.Invoke(info, info2); - } - } + _gameEvent.Send(eventId, info, info2); } - public void Send(int eventid, T info, U info2, W info3) + public void Send(int eventId, T info, U info2, W info3) { - if (m_eventDic.ContainsKey(eventid)) - { - var eventInfo = (m_eventDic[eventid] as EventInfo); - if (eventInfo != null) - { - eventInfo.actions.Invoke(info, info2, info3); - } - } + _gameEvent.Send(eventId, info, info2, info3); } - /// - /// 事件触发 无参 - /// - /// - public void Send(int eventid) + public void Send(int eventId) { - if (m_eventDic.ContainsKey(eventid)) - { - var eventInfo = (m_eventDic[eventid] as EventInfo); - if (eventInfo != null) - { - eventInfo.actions.Invoke(); - } - } + _gameEvent.Send(eventId); } #endregion #region StringEvent #region AddEventListener - public void AddEventListener(string eventid, Action action) + public void AddEventListener(string eventId, Action action) { - if (m_eventStrDic.ContainsKey(eventid)) - { - (m_eventStrDic[eventid] as EventInfo).actions += action; - } - else - { - m_eventStrDic.Add(eventid, new EventInfo(action)); - } + _gameEvent.AddEventListener(eventId, action); } - public void AddEventListener(string eventid, Action action) + public void AddEventListener(string eventId, Action action) { - if (m_eventStrDic.ContainsKey(eventid)) - { - (m_eventStrDic[eventid] as EventInfo).actions += action; - } - else - { - m_eventStrDic.Add(eventid, new EventInfo(action)); - } + _gameEvent.AddEventListener(eventId, action); } - public void AddEventListener(string eventid, Action action) + public void AddEventListener(string eventId, Action action) { - if (m_eventStrDic.ContainsKey(eventid)) - { - (m_eventStrDic[eventid] as EventInfo).actions += action; - } - else - { - m_eventStrDic.Add(eventid, new EventInfo(action)); - } + _gameEvent.AddEventListener(eventId, action); } - public void AddEventListener(string eventid, Action action) + public void AddEventListener(string eventId, Action action) { - if (m_eventStrDic.ContainsKey(eventid)) - { - (m_eventStrDic[eventid] as EventInfo).actions += action; - } - else - { - m_eventStrDic.Add(eventid, new EventInfo(action)); - } + _gameEvent.AddEventListener(eventId, action); } #endregion #region RemoveEventListener - public void RemoveEventListener(string eventid, Action action) + public void RemoveEventListener(string eventId, Action action) { - if (action == null) - { - return; - } - - if (m_eventStrDic.ContainsKey(eventid)) - { - (m_eventStrDic[eventid] as EventInfo).actions -= action; - } + _gameEvent.RemoveEventListener(eventId, action); } - public void RemoveEventListener(string eventid, Action action) + public void RemoveEventListener(string eventId, Action action) { - if (action == null) - { - return; - } - - if (m_eventStrDic.ContainsKey(eventid)) - { - (m_eventStrDic[eventid] as EventInfo).actions -= action; - } + _gameEvent.RemoveEventListener(eventId, action); } - public void RemoveEventListener(string eventid, Action action) + public void RemoveEventListener(string eventId, Action action) { - if (action == null) - { - return; - } - - if (m_eventStrDic.ContainsKey(eventid)) - { - (m_eventStrDic[eventid] as EventInfo).actions -= action; - } + _gameEvent.RemoveEventListener(eventId, action); } - public void RemoveEventListener(string eventid, Action action) + public void RemoveEventListener(string eventId, Action action) { - if (action == null) - { - return; - } - - if (m_eventStrDic.ContainsKey(eventid)) - { - (m_eventStrDic[eventid] as EventInfo).actions -= action; - } + _gameEvent.RemoveEventListener(eventId, action); } #endregion #region Send - public void Send(string eventid, T info) + public void Send(string eventId, T info) { - if (m_eventStrDic.ContainsKey(eventid)) - { - var eventInfo = (m_eventStrDic[eventid] as EventInfo); - if (eventInfo != null) - { - eventInfo.actions.Invoke(info); - } - } + _gameEvent.Send(eventId, info); } - public void Send(string eventid, T info, U info2) + public void Send(string eventId, T info, U info2) { - if (m_eventStrDic.ContainsKey(eventid)) - { - var eventInfo = (m_eventStrDic[eventid] as EventInfo); - if (eventInfo != null) - { - eventInfo.actions.Invoke(info, info2); - } - } + _gameEvent.Send(eventId, info, info2); } - public void Send(string eventid, T info, U info2, W info3) + public void Send(string eventId, T info, U info2, W info3) { - if (m_eventStrDic.ContainsKey(eventid)) - { - var eventInfo = (m_eventStrDic[eventid] as EventInfo); - if (eventInfo != null) - { - eventInfo.actions.Invoke(info, info2, info3); - } - } + _gameEvent.Send(eventId, info, info2, info3); } - public void Send(string eventid) + public void Send(string eventId) { - if (m_eventStrDic.ContainsKey(eventid)) - { - var eventInfo = (m_eventStrDic[eventid] as EventInfo); - if (eventInfo != null) - { - eventInfo.actions.Invoke(); - } - } + _gameEvent.Send(eventId); } #endregion #endregion @@ -383,8 +161,7 @@ namespace TEngine #region Clear public void Clear() { - m_eventDic.Clear(); - m_eventStrDic.Clear(); + _gameEvent.Clear(); } #endregion }