using System; using System.Collections.Generic; using System.Reflection; namespace Base { [ObjectEvent] public class EventComponentEvent : ObjectEvent, ILoader, IAwake { public void Load() { this.GetValue().Load(); } public void Awake() { this.GetValue().Load(); } } /// /// 事件分发 /// public class EventComponent: Component { private Dictionary> allEvents; public void Load() { this.allEvents = new Dictionary>(); Assembly[] assemblies = Object.ObjectManager.GetAssemblies(); foreach (Assembly assembly in assemblies) { Type[] types = assembly.GetTypes(); 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(EventIdType type) { List 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 GameException($"event type: {type} is not IEvent"); } iEvent.Run(); } catch (Exception err) { Log.Error(err.ToString()); } } } public void Run(EventIdType type, A a) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { var iEvent = obj as IEvent; if (iEvent == null) { throw new GameException($"event type: {type} is not IEvent<{typeof (A).Name}>"); } iEvent.Run(a); } catch (Exception err) { Log.Error(err.ToString()); } } } public void Run(EventIdType type, A a, B b) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { var iEvent = obj as IEvent; if (iEvent == null) { throw new GameException($"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(EventIdType type, A a, B b, C c) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { var iEvent = obj as IEvent; if (iEvent == null) { throw new GameException($"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(EventIdType type, A a, B b, C c, D d) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { var iEvent = obj as IEvent; if (iEvent == null) { throw new GameException($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}>"); } iEvent.Run(a, b, c, d); } catch (Exception err) { Log.Error(err.ToString()); } } } public void Run(EventIdType type, A a, B b, C c, D d, E e) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { var iEvent = obj as IEvent; if (iEvent == null) { throw new GameException( $"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); } catch (Exception err) { Log.Error(err.ToString()); } } } public void Run(EventIdType type, A a, B b, C c, D d, E e,F f) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { var iEvent = obj as IEvent; if (iEvent == null) { throw new GameException( $"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); } catch (Exception err) { Log.Error(err.ToString()); } } } } }