using System;
using System.Collections.Generic;
using Base;
namespace Model
{
///
/// 事件分发,可以将事件分发到IL和Mono层,性能较EventComponent要差
///
[EntityEvent(EntityEventId.ILEventComponent)]
public class ILEventComponent : Component
{
private Dictionary> allEvents;
private void Awake()
{
this.Load();
}
private void Load()
{
this.allEvents = new Dictionary>();
Type[] types = DllHelper.GetBaseTypes();
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
foreach (object attr in attrs)
{
EventAttribute aEventAttribute = (EventAttribute)attr;
IInstanceMethod method = new MonoInstanceMethod(type, "Run");
if (!this.allEvents.ContainsKey(aEventAttribute.Type))
{
this.allEvents.Add(aEventAttribute.Type, new List());
}
this.allEvents[aEventAttribute.Type].Add(method);
}
}
types = DllHelper.GetHotfixTypes();
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
foreach (object attr in attrs)
{
EventAttribute aEventAttribute = (EventAttribute)attr;
IInstanceMethod method = new ILInstanceMethod(type, "Run");
if (!this.allEvents.ContainsKey(aEventAttribute.Type))
{
this.allEvents.Add(aEventAttribute.Type, new List());
}
this.allEvents[aEventAttribute.Type].Add(method);
}
}
}
public void Run(int type, params object[] param)
{
List iEvents = null;
if (!this.allEvents.TryGetValue(type, out iEvents))
{
return;
}
foreach (IInstanceMethod obj in iEvents)
{
try
{
obj.Run(param);
}
catch (Exception err)
{
Log.Error(err.ToString());
}
}
}
}
}