| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.Collections.Generic;
- namespace Model
- {
- [ObjectEvent]
- public class EventComponentEvent : ObjectEvent<EventComponent>, IAwake, ILoad
- {
- public void Awake()
- {
- this.Get().Awake();
- }
- public void Load()
- {
- this.Get().Load();
- }
- }
-
- public class EventComponent : Component
- {
- private Dictionary<EventIdType, List<object>> allEvents;
- public void Awake()
- {
- this.Load();
- }
- public void Load()
- {
- this.allEvents = new Dictionary<EventIdType, List<object>>();
- Type[] types = DllHelper.GetMonoTypes();
- 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((EventIdType)aEventAttribute.Type))
- {
- this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<object>());
- }
- this.allEvents[(EventIdType)aEventAttribute.Type].Add(obj);
- }
- }
- }
- public void Run(EventIdType type)
- {
- List<object> iEvents;
- 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<A>(EventIdType type, A a)
- {
- List<object> iEvents;
- if (!this.allEvents.TryGetValue(type, out iEvents))
- {
- return;
- }
- foreach (object obj in iEvents)
- {
- try
- {
- IEvent<A> iEvent = (IEvent<A>)obj;
- iEvent.Run(a);
- }
- catch (Exception err)
- {
- Log.Error(err.ToString());
- }
- }
- }
- public void Run<A, B>(EventIdType type, A a, B b)
- {
- List<object> iEvents;
- if (!this.allEvents.TryGetValue(type, out iEvents))
- {
- return;
- }
- foreach (object obj in iEvents)
- {
- try
- {
- IEvent<A, B> iEvent = (IEvent<A, B>)obj;
- iEvent.Run(a, b);
- }
- catch (Exception err)
- {
- Log.Error(err.ToString());
- }
- }
- }
- public void Run<A, B, C>(EventIdType type, A a, B b, C c)
- {
- List<object> iEvents;
- if (!this.allEvents.TryGetValue(type, out iEvents))
- {
- return;
- }
- foreach (object obj in iEvents)
- {
- try
- {
- IEvent<A, B, C> iEvent = (IEvent<A, B, C>)obj;
- iEvent.Run(a, b, c);
- }
- catch (Exception err)
- {
- Log.Error(err.ToString());
- }
- }
- }
- }
- }
|