| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using ETModel;
- namespace ETHotfix
- {
- public enum DLLType
- {
- Model,
- Hotfix,
- Editor,
- }
- public sealed class EventSystem
- {
- private readonly Dictionary<long, Entity> allComponents = new Dictionary<long, Entity>();
-
- private readonly List<Type> types = new List<Type>();
- private readonly Dictionary<string, List<object>> allEvents = new Dictionary<string, List<object>>();
- private readonly UnOrderMultiMap<Type, IAwakeSystem> awakeSystems = new UnOrderMultiMap<Type, IAwakeSystem>();
- private readonly UnOrderMultiMap<Type, IStartSystem> startSystems = new UnOrderMultiMap<Type, IStartSystem>();
- private readonly UnOrderMultiMap<Type, IDestroySystem> destroySystems = new UnOrderMultiMap<Type, IDestroySystem>();
- private readonly UnOrderMultiMap<Type, ILoadSystem> loadSystems = new UnOrderMultiMap<Type, ILoadSystem>();
- private readonly UnOrderMultiMap<Type, IUpdateSystem> updateSystems = new UnOrderMultiMap<Type, IUpdateSystem>();
- private readonly UnOrderMultiMap<Type, ILateUpdateSystem> lateUpdateSystems = new UnOrderMultiMap<Type, ILateUpdateSystem>();
- private readonly UnOrderMultiMap<Type, IChangeSystem> changeSystems = new UnOrderMultiMap<Type, IChangeSystem>();
-
- private readonly UnOrderMultiMap<Type, IDeserializeSystem> deserializeSystems = new UnOrderMultiMap<Type, IDeserializeSystem>();
- private Queue<long> updates = new Queue<long>();
- private Queue<long> updates2 = new Queue<long>();
-
- private readonly Queue<long> starts = new Queue<long>();
- private Queue<long> loaders = new Queue<long>();
- private Queue<long> loaders2 = new Queue<long>();
- private Queue<long> lateUpdates = new Queue<long>();
- private Queue<long> lateUpdates2 = new Queue<long>();
- public EventSystem()
- {
- this.types.Clear();
-
- List<Type> ts = ETModel.Game.Hotfix.GetHotfixTypes();
-
- foreach (Type type in ts)
- {
- // ILRuntime无法判断是否有Attribute
- //if (type.GetCustomAttributes(typeof (Attribute), false).Length == 0)
- //{
- // continue;
- //}
- this.types.Add(type);
- }
-
- foreach (Type type in types)
- {
- object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false);
- if (attrs.Length == 0)
- {
- continue;
- }
- object obj = Activator.CreateInstance(type);
- switch (obj)
- {
- case IAwakeSystem objectSystem:
- this.awakeSystems.Add(objectSystem.Type(), objectSystem);
- break;
- case IUpdateSystem updateSystem:
- this.updateSystems.Add(updateSystem.Type(), updateSystem);
- break;
- case ILateUpdateSystem lateUpdateSystem:
- this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem);
- break;
- case IStartSystem startSystem:
- this.startSystems.Add(startSystem.Type(), startSystem);
- break;
- case IDestroySystem destroySystem:
- this.destroySystems.Add(destroySystem.Type(), destroySystem);
- break;
- case ILoadSystem loadSystem:
- this.loadSystems.Add(loadSystem.Type(), loadSystem);
- break;
- case IChangeSystem changeSystem:
- this.changeSystems.Add(changeSystem.Type(), changeSystem);
- break;
- case IDeserializeSystem deserializeSystem:
- this.deserializeSystems.Add(deserializeSystem.Type(), deserializeSystem);
- break;
- }
- }
- 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);
- // hotfix的事件也要注册到mono层,hotfix可以订阅mono层的事件
- Action<List<object>> action = list => { Handle(iEvent, list); };
- ETModel.Game.EventSystem.RegisterEvent(aEventAttribute.Type, new EventProxy(action));
- }
- }
- this.Load();
- }
-
- public static void Handle(IEvent iEvent, List<object> param)
- {
- switch (param.Count)
- {
- case 0:
- iEvent.Handle();
- break;
- case 1:
- iEvent.Handle(param[0]);
- break;
- case 2:
- iEvent.Handle(param[0], param[1]);
- break;
- case 3:
- iEvent.Handle(param[0], param[1], param[2]);
- break;
- }
- }
- public void RegisterEvent(string eventId, IEvent e)
- {
- if (!this.allEvents.ContainsKey(eventId))
- {
- this.allEvents.Add(eventId, new List<object>());
- }
- this.allEvents[eventId].Add(e);
- }
-
- public List<Type> GetTypes()
- {
- return this.types;
- }
- public void RegisterSystem(Entity component, bool isRegister = true)
- {
- if (!isRegister)
- {
- this.Remove(component.InstanceId);
- return;
- }
- this.allComponents.Add(component.InstanceId, component);
-
- Type type = component.GetType();
- if (this.loadSystems.ContainsKey(type))
- {
- this.loaders.Enqueue(component.InstanceId);
- }
- if (this.updateSystems.ContainsKey(type))
- {
- this.updates.Enqueue(component.InstanceId);
- }
- if (this.startSystems.ContainsKey(type))
- {
- this.starts.Enqueue(component.InstanceId);
- }
- if (this.lateUpdateSystems.ContainsKey(type))
- {
- this.lateUpdates.Enqueue(component.InstanceId);
- }
- }
- public void Remove(long instanceId)
- {
- this.allComponents.Remove(instanceId);
- }
- public Entity Get(long instanceId)
- {
- Entity component = null;
- this.allComponents.TryGetValue(instanceId, out component);
- return component;
- }
-
- public bool IsRegister(long instanceId)
- {
- return this.allComponents.ContainsKey(instanceId);
- }
-
- public void Deserialize(Entity component)
- {
- List<IDeserializeSystem> iDeserializeSystems = this.deserializeSystems[component.GetType()];
- if (iDeserializeSystems == null)
- {
- return;
- }
- foreach (IDeserializeSystem deserializeSystem in iDeserializeSystems)
- {
- if (deserializeSystem == null)
- {
- continue;
- }
- try
- {
- deserializeSystem.Run(component);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Awake(Entity component)
- {
- List<IAwakeSystem> iAwakeSystems = this.awakeSystems[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<P1>(Entity component, P1 p1)
- {
- List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
- if (iAwakeSystems == null)
- {
- return;
- }
- foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
- {
- if (aAwakeSystem == null)
- {
- continue;
- }
-
- IAwake<P1> iAwake = aAwakeSystem as IAwake<P1>;
- if (iAwake == null)
- {
- continue;
- }
- try
- {
- iAwake.Run(component, p1);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Awake<P1, P2>(Entity component, P1 p1, P2 p2)
- {
- List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
- if (iAwakeSystems == null)
- {
- return;
- }
- foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
- {
- if (aAwakeSystem == null)
- {
- continue;
- }
-
- IAwake<P1, P2> iAwake = aAwakeSystem as IAwake<P1, P2>;
- if (iAwake == null)
- {
- continue;
- }
- try
- {
- iAwake.Run(component, p1, p2);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Awake<P1, P2, P3>(Entity component, P1 p1, P2 p2, P3 p3)
- {
- List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
- if (iAwakeSystems == null)
- {
- return;
- }
- foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
- {
- if (aAwakeSystem == null)
- {
- continue;
- }
- IAwake<P1, P2, P3> iAwake = aAwakeSystem as IAwake<P1, P2, P3>;
- if (iAwake == null)
- {
- continue;
- }
- try
- {
- iAwake.Run(component, p1, p2, p3);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Awake<P1, P2, P3, P4>(Entity component, P1 p1, P2 p2, P3 p3, P4 p4)
- {
- List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
- if (iAwakeSystems == null)
- {
- return;
- }
- foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
- {
- if (aAwakeSystem == null)
- {
- continue;
- }
- IAwake<P1, P2, P3, P4> iAwake = aAwakeSystem as IAwake<P1, P2, P3, P4>;
- if (iAwake == null)
- {
- continue;
- }
- try
- {
- iAwake.Run(component, p1, p2, p3, p4);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Change(Entity component)
- {
- List<IChangeSystem> iChangeSystems = this.changeSystems[component.GetType()];
- if (iChangeSystems == null)
- {
- return;
- }
- foreach (IChangeSystem iChangeSystem in iChangeSystems)
- {
- if (iChangeSystem == null)
- {
- continue;
- }
- try
- {
- iChangeSystem.Run(component);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Load()
- {
- while (this.loaders.Count > 0)
- {
- long instanceId = this.loaders.Dequeue();
- Entity component;
- if (!this.allComponents.TryGetValue(instanceId, out component))
- {
- continue;
- }
- if (component.IsDisposed)
- {
- continue;
- }
-
- List<ILoadSystem> iLoadSystems = this.loadSystems[component.GetType()];
- if (iLoadSystems == null)
- {
- continue;
- }
-
- this.loaders2.Enqueue(instanceId);
- foreach (ILoadSystem iLoadSystem in iLoadSystems)
- {
- try
- {
- iLoadSystem.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();
- Entity component;
- if (!this.allComponents.TryGetValue(instanceId, out component))
- {
- continue;
- }
- List<IStartSystem> iStartSystems = this.startSystems[component.GetType()];
- if (iStartSystems == null)
- {
- continue;
- }
-
- foreach (IStartSystem iStartSystem in iStartSystems)
- {
- try
- {
- iStartSystem.Run(component);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- }
- public void Destroy(Entity component)
- {
- List<IDestroySystem> iDestroySystems = this.destroySystems[component.GetType()];
- if (iDestroySystems == null)
- {
- return;
- }
- foreach (IDestroySystem iDestroySystem in iDestroySystems)
- {
- if (iDestroySystem == null)
- {
- continue;
- }
- try
- {
- iDestroySystem.Run(component);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Update()
- {
- this.Start();
-
- while (this.updates.Count > 0)
- {
- long instanceId = this.updates.Dequeue();
- Entity component;
- if (!this.allComponents.TryGetValue(instanceId, out component))
- {
- continue;
- }
- if (component.IsDisposed)
- {
- continue;
- }
-
- List<IUpdateSystem> iUpdateSystems = this.updateSystems[component.GetType()];
- if (iUpdateSystems == null)
- {
- continue;
- }
- this.updates2.Enqueue(instanceId);
- foreach (IUpdateSystem iUpdateSystem in iUpdateSystems)
- {
- try
- {
- iUpdateSystem.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();
- Entity component;
- if (!this.allComponents.TryGetValue(instanceId, out component))
- {
- continue;
- }
- if (component.IsDisposed)
- {
- continue;
- }
- List<ILateUpdateSystem> iLateUpdateSystems = this.lateUpdateSystems[component.GetType()];
- if (iLateUpdateSystems == null)
- {
- continue;
- }
- this.lateUpdates2.Enqueue(instanceId);
- foreach (ILateUpdateSystem iLateUpdateSystem in iLateUpdateSystems)
- {
- try
- {
- iLateUpdateSystem.Run(component);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
- }
- public void Run(string type)
- {
- List<object> iEvents;
- if (!this.allEvents.TryGetValue(type, out iEvents))
- {
- return;
- }
- foreach (AEvent aEvent in iEvents)
- {
- try
- {
- aEvent.Run();
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Run<A>(string type, A a)
- {
- List<object> iEvents;
- if (!this.allEvents.TryGetValue(type, out iEvents))
- {
- return;
- }
- foreach (AEvent<A> aEvent in iEvents)
- {
- try
- {
- aEvent?.Run(a);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Run<A, B>(string type, A a, B b)
- {
- List<object> iEvents;
- if (!this.allEvents.TryGetValue(type, out iEvents))
- {
- return;
- }
- foreach (AEvent<A, B> aEvent in iEvents)
- {
- try
- {
- aEvent.Run(a, b);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public void Run<A, B, C>(string type, A a, B b, C c)
- {
- List<object> iEvents;
- if (!this.allEvents.TryGetValue(type, out iEvents))
- {
- return;
- }
- foreach (AEvent<A, B, C> aEvent in iEvents)
- {
- try
- {
- aEvent.Run(a, b, c);
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- HashSet<Type> noParent = new HashSet<Type>();
- Dictionary<Type, int> typeCount = new Dictionary<Type, int>();
-
- HashSet<Type> noDomain = new HashSet<Type>();
-
- foreach (var kv in this.allComponents)
- {
- Type type = kv.Value.GetType();
- if (kv.Value.Parent == null)
- {
- noParent.Add(type);
- }
-
- if (kv.Value.Domain == null)
- {
- noDomain.Add(type);
- }
-
- if (typeCount.ContainsKey(type))
- {
- typeCount[type]++;
- }
- else
- {
- typeCount[type] = 1;
- }
- }
- sb.AppendLine("not set parent type: ");
- foreach (Type type in noParent)
- {
- sb.AppendLine($"\t{type.Name}");
- }
-
- sb.AppendLine("not set domain type: ");
- foreach (Type type in noDomain)
- {
- sb.AppendLine($"\t{type.Name}");
- }
- IOrderedEnumerable<KeyValuePair<Type, int>> orderByDescending = typeCount.OrderByDescending(s => s.Value);
-
- sb.AppendLine("Entity Count: ");
- foreach (var kv in orderByDescending)
- {
- if (kv.Value == 1)
- {
- continue;
- }
- sb.AppendLine($"\t{kv.Key.Name}: {kv.Value}");
- }
- return sb.ToString();
- }
- }
- }
|