using System; using System.Collections.Generic; namespace Model { [EntityEvent(EntityEventId.EventComponent)] public class EventComponent : Component { private Dictionary> allEvents; private void Awake() { this.Load(); } private void Load() { this.allEvents = new Dictionary>(); Type[] types = DllHelper.GetAllTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false); foreach (object attr in attrs) { EventAttribute aEventAttribute = (EventAttribute)attr; object obj = Activator.CreateInstance(type); if (!this.allEvents.ContainsKey(aEventAttribute.Type)) { this.allEvents.Add(aEventAttribute.Type, new List()); } this.allEvents[aEventAttribute.Type].Add(obj); } } } public void Run(int type) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { IEvent iEvent = (IEvent)obj; iEvent.Run(); } catch (Exception e) { Log.Error(e.ToString()); } } } public void Run(int type, A a) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { IEvent iEvent = (IEvent)obj; iEvent.Run(a); } catch (Exception err) { Log.Error(err.ToString()); } } } public void Run(int type, A a, B b) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { IEvent iEvent = (IEvent)obj; iEvent.Run(a, b); } catch (Exception err) { Log.Error(err.ToString()); } } } public void Run(int type, A a, B b, C c) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { IEvent iEvent = (IEvent)obj; iEvent.Run(a, b, c); } catch (Exception err) { Log.Error(err.ToString()); } } } } }