using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace ETModel { public enum DLLType { Model, Hotfix, Editor, } public sealed class EventSystem { private readonly Dictionary allComponents = new Dictionary(); private readonly Dictionary assemblies = new Dictionary(); private readonly Dictionary> allEvents = new Dictionary>(); private readonly UnOrderMultiMap awakeEvents = new UnOrderMultiMap(); private readonly UnOrderMultiMap startEvents = new UnOrderMultiMap(); private readonly UnOrderMultiMap destroyEvents = new UnOrderMultiMap(); private readonly UnOrderMultiMap loadEvents = new UnOrderMultiMap(); private readonly UnOrderMultiMap updateEvents = new UnOrderMultiMap(); private readonly UnOrderMultiMap lateUpdateEvents = new UnOrderMultiMap(); private Queue updates = new Queue(); private Queue updates2 = new Queue(); private readonly Queue starts = new Queue(); private Queue loaders = new Queue(); private Queue loaders2 = new Queue(); private Queue lateUpdates = new Queue(); private Queue lateUpdates2 = new Queue(); public void Add(DLLType dllType, Assembly assembly) { this.assemblies[dllType] = assembly; this.awakeEvents.Clear(); this.lateUpdateEvents.Clear(); this.updateEvents.Clear(); this.startEvents.Clear(); this.loadEvents.Clear(); Type[] types = DllHelper.GetMonoTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false); if (attrs.Length == 0) { continue; } object obj = Activator.CreateInstance(type); IAwakeSystem objectSystem = obj as IAwakeSystem; if (objectSystem != null) { this.awakeEvents.Add(objectSystem.Type(), objectSystem); } IUpdateSystem updateSystem = obj as IUpdateSystem; if (updateSystem != null) { this.updateEvents.Add(updateSystem.Type(), updateSystem); } ILateUpdateSystem lateUpdateSystem = obj as ILateUpdateSystem; if (lateUpdateSystem != null) { this.lateUpdateEvents.Add(lateUpdateSystem.Type(), lateUpdateSystem); } IStartSystem startSystem = obj as IStartSystem; if (startSystem != null) { this.startEvents.Add(startSystem.Type(), startSystem); } IDestroySystem destroySystem = obj as IDestroySystem; if (destroySystem != null) { this.destroyEvents.Add(destroySystem.Type(), destroySystem); } ILoadSystem loadSystem = obj as ILoadSystem; if (loadSystem != null) { this.loadEvents.Add(loadSystem.Type(), loadSystem); } } this.allEvents.Clear(); 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); IEvent iEvent = obj as IEvent; if (iEvent == null) { Log.Error($"{obj.GetType().Name} 没有继承IEvent"); } this.RegisterEvent(aEventAttribute.Type, iEvent); } } this.Load(); } public void RegisterEvent(string eventId, IEvent e) { if (!this.allEvents.ContainsKey(eventId)) { this.allEvents.Add(eventId, new List()); } this.allEvents[eventId].Add(e); } public Assembly Get(DLLType dllType) { return this.assemblies[dllType]; } public Assembly[] GetAll() { return this.assemblies.Values.ToArray(); } public void Add(Component component) { this.allComponents.Add(component.InstanceId, component); Type type = component.GetType(); if (this.loadEvents.ContainsKey(type)) { this.loaders.Enqueue(component.InstanceId); } if (this.updateEvents.ContainsKey(type)) { this.updates.Enqueue(component.InstanceId); } if (this.startEvents.ContainsKey(type)) { this.starts.Enqueue(component.InstanceId); } if (this.lateUpdateEvents.ContainsKey(type)) { this.lateUpdates.Enqueue(component.InstanceId); } } public void Remove(long id) { this.allComponents.Remove(id); } public Component Get(long id) { Component component = null; this.allComponents.TryGetValue(id, out component); return component; } public void Awake(Component component) { List iAwakeSystems = this.awakeEvents[component.GetType()]; if (iAwakeSystems == null) { return; } foreach (IAwakeSystem aAwakeSystem in iAwakeSystems) { if (aAwakeSystem == null) { continue; } IAwake iAwake = aAwakeSystem as IAwake; if (iAwake == null) { continue; } try { iAwake.Run(component); } catch (Exception e) { Log.Error(e); } } } public void Awake(Component component, P1 p1) { List iAwakeSystems = this.awakeEvents[component.GetType()]; if (iAwakeSystems == null) { return; } foreach (IAwakeSystem aAwakeSystem in iAwakeSystems) { if (aAwakeSystem == null) { continue; } IAwake iAwake = aAwakeSystem as IAwake; if (iAwake == null) { continue; } try { iAwake.Run(component, p1); } catch (Exception e) { Log.Error(e); } } } public void Awake(Component component, P1 p1, P2 p2) { List iAwakeSystems = this.awakeEvents[component.GetType()]; if (iAwakeSystems == null) { return; } foreach (IAwakeSystem aAwakeSystem in iAwakeSystems) { if (aAwakeSystem == null) { continue; } IAwake iAwake = aAwakeSystem as IAwake; if (iAwake == null) { continue; } try { iAwake.Run(component, p1, p2); } catch (Exception e) { Log.Error(e); } } } public void Awake(Component component, P1 p1, P2 p2, P3 p3) { List iAwakeSystems = this.awakeEvents[component.GetType()]; if (iAwakeSystems == null) { return; } foreach (IAwakeSystem aAwakeSystem in iAwakeSystems) { if (aAwakeSystem == null) { continue; } IAwake iAwake = aAwakeSystem as IAwake; if (iAwake == null) { continue; } try { iAwake.Run(component, p1, p2, p3); } catch (Exception e) { Log.Error(e); } } } public void Load() { while (this.loaders.Count > 0) { long instanceId = this.loaders.Dequeue(); Component component; if (!this.allComponents.TryGetValue(instanceId, out component)) { continue; } if (component.IsDisposed) { continue; } List aLoadSystems = this.loadEvents[component.GetType()]; if (aLoadSystems == null) { continue; } this.loaders2.Enqueue(instanceId); foreach (ILoadSystem aLoadSystem in aLoadSystems) { try { aLoadSystem.Run(component); } catch (Exception e) { Log.Error(e); } } } ObjectHelper.Swap(ref this.loaders, ref this.loaders2); } private void Start() { while (this.starts.Count > 0) { long instanceId = this.starts.Dequeue(); Component component; if (!this.allComponents.TryGetValue(instanceId, out component)) { continue; } List aStartSystems = this.startEvents[component.GetType()]; if (aStartSystems == null) { continue; } foreach (IStartSystem aStartSystem in aStartSystems) { try { aStartSystem.Run(component); } catch (Exception e) { Log.Error(e); } } } } public void Destroy(Component component) { List iDestroySystems = this.destroyEvents[component.GetType()]; if (iDestroySystems == null) { return; } foreach (IDestroySystem aDestroySystem in iDestroySystems) { if (aDestroySystem == null) { continue; } try { aDestroySystem.Run(component); } catch (Exception e) { Log.Error(e); } } } public void Update() { this.Start(); while (this.updates.Count > 0) { long instanceId = this.updates.Dequeue(); Component component; if (!this.allComponents.TryGetValue(instanceId, out component)) { continue; } if (component.IsDisposed) { continue; } List aUpdateSystems = this.updateEvents[component.GetType()]; if (aUpdateSystems == null) { continue; } this.updates2.Enqueue(instanceId); foreach (IUpdateSystem aUpdateSystem in aUpdateSystems) { try { aUpdateSystem.Run(component); } catch (Exception e) { Log.Error(e); } } } ObjectHelper.Swap(ref this.updates, ref this.updates2); } public void LateUpdate() { while (this.lateUpdates.Count > 0) { long instanceId = this.lateUpdates.Dequeue(); Component component; if (!this.allComponents.TryGetValue(instanceId, out component)) { continue; } if (component.IsDisposed) { continue; } List aLateUpdateSystems = this.lateUpdateEvents[component.GetType()]; if (aLateUpdateSystems == null) { continue; } this.lateUpdates2.Enqueue(instanceId); foreach (ILateUpdateSystem aLateUpdateSystem in aLateUpdateSystems) { try { aLateUpdateSystem.Run(component); } catch (Exception e) { Log.Error(e); } } } ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2); } public void Run(string type) { List iEvents; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (IEvent iEvent in iEvents) { try { iEvent?.Handle(); } catch (Exception e) { Log.Error(e); } } } public void Run(string type, A a) { List iEvents; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (IEvent iEvent in iEvents) { try { iEvent?.Handle(a); } catch (Exception e) { Log.Error(e); } } } public void Run(string type, A a, B b) { List iEvents; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (IEvent iEvent in iEvents) { try { iEvent?.Handle(a, b); } catch (Exception e) { Log.Error(e); } } } public void Run(string type, A a, B b, C c) { List iEvents; if (!this.allEvents.TryGetValue(type, out iEvents)) { return; } foreach (IEvent iEvent in iEvents) { try { iEvent?.Handle(a, b, c); } catch (Exception e) { Log.Error(e); } } } } }