|
|
@@ -1,17 +1,16 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
-using System.Reflection;
|
|
|
using Base;
|
|
|
|
|
|
namespace Model
|
|
|
{
|
|
|
/// <summary>
|
|
|
- /// 事件分发
|
|
|
+ /// 事件分发,可以将事件分发到IL和Mono层,因为是用反射实现的,所以性能较Server用的EventComponent要差
|
|
|
/// </summary>
|
|
|
[EntityEvent(EntityEventId.EventComponent)]
|
|
|
- public class EventComponent: Component
|
|
|
+ public class EventComponent : Component
|
|
|
{
|
|
|
- private Dictionary<int, List<object>> allEvents;
|
|
|
+ private Dictionary<int, List<IInstanceMethod>> allEvents;
|
|
|
|
|
|
private void Awake()
|
|
|
{
|
|
|
@@ -20,203 +19,56 @@ namespace Model
|
|
|
|
|
|
private void Load()
|
|
|
{
|
|
|
- this.allEvents = new Dictionary<int, List<object>>();
|
|
|
+ this.allEvents = new Dictionary<int, List<IInstanceMethod>>();
|
|
|
|
|
|
- Type[] types = DllHelper.GetBaseTypes();
|
|
|
+ Type[] types = DllHelper.GetMonoTypes();
|
|
|
foreach (Type type in types)
|
|
|
{
|
|
|
- object[] attrs = type.GetCustomAttributes(typeof (EventAttribute), false);
|
|
|
+ object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
|
|
|
|
|
|
foreach (object attr in attrs)
|
|
|
{
|
|
|
- EventAttribute aEventAttribute = (EventAttribute) attr;
|
|
|
-
|
|
|
- object obj = Activator.CreateInstance(type);
|
|
|
+ EventAttribute aEventAttribute = (EventAttribute)attr;
|
|
|
+ IInstanceMethod method = new MonoInstanceMethod(type, "Run");
|
|
|
if (!this.allEvents.ContainsKey(aEventAttribute.Type))
|
|
|
{
|
|
|
- this.allEvents.Add(aEventAttribute.Type, new List<object>());
|
|
|
- }
|
|
|
- this.allEvents[aEventAttribute.Type].Add(obj);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Run(int type)
|
|
|
- {
|
|
|
- List<object> iEvents = null;
|
|
|
- if (!this.allEvents.TryGetValue(type, out iEvents))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (object obj in iEvents)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- IEvent iEvent = obj as IEvent;
|
|
|
- if (iEvent == null)
|
|
|
- {
|
|
|
- throw new Exception($"event type: {type} is not IEvent");
|
|
|
- }
|
|
|
- iEvent.Run();
|
|
|
- }
|
|
|
- catch (Exception err)
|
|
|
- {
|
|
|
- Log.Error(err.ToString());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Run<A>(int type, A a)
|
|
|
- {
|
|
|
- List<object> iEvents = null;
|
|
|
- if (!this.allEvents.TryGetValue(type, out iEvents))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (object obj in iEvents)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- var iEvent = obj as IEvent<A>;
|
|
|
- if (iEvent == null)
|
|
|
- {
|
|
|
- throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}>");
|
|
|
- }
|
|
|
- iEvent.Run(a);
|
|
|
- }
|
|
|
- catch (Exception err)
|
|
|
- {
|
|
|
- Log.Error(err.ToString());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Run<A, B>(int type, A a, B b)
|
|
|
- {
|
|
|
- List<object> iEvents = null;
|
|
|
- if (!this.allEvents.TryGetValue(type, out iEvents))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (object obj in iEvents)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- var iEvent = obj as IEvent<A, B>;
|
|
|
- if (iEvent == null)
|
|
|
- {
|
|
|
- throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}>");
|
|
|
- }
|
|
|
- iEvent.Run(a, b);
|
|
|
- }
|
|
|
- catch (Exception err)
|
|
|
- {
|
|
|
- Log.Error(err.ToString());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Run<A, B, C>(int type, A a, B b, C c)
|
|
|
- {
|
|
|
- List<object> iEvents = null;
|
|
|
- if (!this.allEvents.TryGetValue(type, out iEvents))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (object obj in iEvents)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- var iEvent = obj as IEvent<A, B, C>;
|
|
|
- if (iEvent == null)
|
|
|
- {
|
|
|
- throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}>");
|
|
|
- }
|
|
|
- iEvent.Run(a, b, c);
|
|
|
- }
|
|
|
- catch (Exception err)
|
|
|
- {
|
|
|
- Log.Error(err.ToString());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Run<A, B, C, D>(int type, A a, B b, C c, D d)
|
|
|
- {
|
|
|
- List<object> iEvents = null;
|
|
|
- if (!this.allEvents.TryGetValue(type, out iEvents))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach (object obj in iEvents)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- var iEvent = obj as IEvent<A, B, C, D>;
|
|
|
- if (iEvent == null)
|
|
|
- {
|
|
|
- throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}>");
|
|
|
+ this.allEvents.Add(aEventAttribute.Type, new List<IInstanceMethod>());
|
|
|
}
|
|
|
- iEvent.Run(a, b, c, d);
|
|
|
- }
|
|
|
- catch (Exception err)
|
|
|
- {
|
|
|
- Log.Error(err.ToString());
|
|
|
+ this.allEvents[aEventAttribute.Type].Add(method);
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- public void Run<A, B, C, D, E>(int type, A a, B b, C c, D d, E e)
|
|
|
- {
|
|
|
- List<object> iEvents = null;
|
|
|
- if (!this.allEvents.TryGetValue(type, out iEvents))
|
|
|
+ types = DllHelper.GetHotfixTypes();
|
|
|
+ foreach (Type type in types)
|
|
|
{
|
|
|
- return;
|
|
|
- }
|
|
|
+ object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
|
|
|
|
|
|
- foreach (object obj in iEvents)
|
|
|
- {
|
|
|
- try
|
|
|
+ foreach (object attr in attrs)
|
|
|
{
|
|
|
- var iEvent = obj as IEvent<A, B, C, D, E>;
|
|
|
- if (iEvent == null)
|
|
|
+ EventAttribute aEventAttribute = (EventAttribute)attr;
|
|
|
+ IInstanceMethod method = new ILInstanceMethod(type, "Run");
|
|
|
+ if (!this.allEvents.ContainsKey(aEventAttribute.Type))
|
|
|
{
|
|
|
- throw new Exception(
|
|
|
- $"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}, {typeof (E).Name}>");
|
|
|
+ this.allEvents.Add(aEventAttribute.Type, new List<IInstanceMethod>());
|
|
|
}
|
|
|
- iEvent.Run(a, b, c, d, e);
|
|
|
- }
|
|
|
- catch (Exception err)
|
|
|
- {
|
|
|
- Log.Error(err.ToString());
|
|
|
+ this.allEvents[aEventAttribute.Type].Add(method);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void Run<A, B, C, D, E, F>(int type, A a, B b, C c, D d, E e, F f)
|
|
|
+ public void Run(int type, params object[] param)
|
|
|
{
|
|
|
- List<object> iEvents = null;
|
|
|
+ List<IInstanceMethod> iEvents = null;
|
|
|
if (!this.allEvents.TryGetValue(type, out iEvents))
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- foreach (object obj in iEvents)
|
|
|
+ foreach (IInstanceMethod obj in iEvents)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- var iEvent = obj as IEvent<A, B, C, D, E, F>;
|
|
|
- if (iEvent == null)
|
|
|
- {
|
|
|
- throw new Exception(
|
|
|
- $"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}, {typeof (E).Name}>");
|
|
|
- }
|
|
|
- iEvent.Run(a, b, c, d, e, f);
|
|
|
+ obj.Run(param);
|
|
|
}
|
|
|
catch (Exception err)
|
|
|
{
|