using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; using Common.Base; using Common.Logger; namespace Model { public class EventComponent: Component, IAssemblyLoader where AttributeType : AEventAttribute { private Dictionary> allEvents; public void Load(Assembly assembly) { this.allEvents = new Dictionary>(); ServerType serverType = World.Instance.Options.ServerType; Type[] types = assembly.GetTypes(); foreach (Type t in types) { object[] attrs = t.GetCustomAttributes(typeof (AttributeType), false); if (attrs.Length == 0) { continue; } AEventAttribute aEventAttribute = (AEventAttribute) attrs[0]; if (!aEventAttribute.Contains(serverType)) { continue; } object obj = Activator.CreateInstance(t); if (obj == null) { throw new Exception(string.Format("event not inherit IEvent or IEventAsync interface: {0}", obj.GetType().FullName)); } if (!this.allEvents.ContainsKey(aEventAttribute.Type)) { this.allEvents.Add(aEventAttribute.Type, new List()); } this.allEvents[aEventAttribute.Type].Add(obj); } } public async Task RunAsync(EventType type) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { AEvent iEvent = obj as AEvent; if (iEvent == null) { throw new GameException(string.Format("event type: {0} is not IEvent", type)); } iEvent.Run(); await iEvent.RunAsync(); } catch (Exception err) { Log.Debug(err.ToString()); } } } public async Task RunAsync(EventType type, A a) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { AEvent iEvent = obj as AEvent; if (iEvent == null) { throw new GameException(string.Format("event type: {0} is not IEvent<{1}>", type, typeof(A).Name)); } iEvent.Run(a); await iEvent.RunAsync(a); } catch (Exception err) { Log.Debug(err.ToString()); } } } public async Task RunAsync(EventType type, A a, B b) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { AEvent iEvent = obj as AEvent; if (iEvent == null) { throw new GameException(string.Format("event type: {0} is not IEvent<{1}, {2}>", type, typeof(A).Name, typeof(B).Name)); } iEvent.Run(a, b); await iEvent.RunAsync(a, b); } catch (Exception err) { Log.Debug(err.ToString()); } } } public async Task RunAsync(EventType type, A a, B b, C c) { List iEvents = null; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (object obj in iEvents) { try { AEvent iEvent = obj as AEvent; if (iEvent == null) { throw new GameException(string.Format("event type: {0} is not IEvent<{1}, {2}, {3}>", type, typeof(A).Name, typeof(B).Name, typeof(C).Name)); } iEvent.Run(a, b, c); await iEvent.RunAsync(a, b, c); } catch (Exception err) { Log.Debug(err.ToString()); } } } public async Task RunAsync(EventType 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 { AEvent iEvent = obj as AEvent; if (iEvent == null) { throw new GameException(string.Format("event type: {0} is not IEvent<{1}, {2}, {3}, {4}>", type, typeof(A).Name, typeof(B).Name, typeof(C).Name, typeof(D).Name)); } iEvent.Run(a, b, c, d); await iEvent.RunAsync(a, b, c, d); } catch (Exception err) { Log.Debug(err.ToString()); } } } public async Task RunAsync(EventType 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 { AEvent iEvent = obj as AEvent; if (iEvent == null) { throw new GameException(string.Format("event type: {0} is not IEvent<{1}, {2}, {3}, {4}, {5}>", type, typeof(A).Name, typeof(B).Name, typeof(C).Name, typeof(D).Name, typeof(E).Name)); } iEvent.Run(a, b, c, d, e); await iEvent.RunAsync(a, b, c, d, e); } catch (Exception err) { Log.Debug(err.ToString()); } } } } }