| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- namespace Model
- {
- public enum DLLType
- {
- Model,
- Hotfix,
- }
- public interface IObjectSystem
- {
- Type Type();
- void Set(object value);
- }
- public abstract class ObjectSystem<T> : IObjectSystem
- {
- private T value;
- protected T Get()
- {
- return value;
- }
- public void Set(object v)
- {
- this.value = (T)v;
- }
- public Type Type()
- {
- return typeof(T);
- }
- }
- public sealed class EventSystem
- {
- private readonly Dictionary<DLLType, Assembly> assemblies = new Dictionary<DLLType, Assembly>();
- private readonly Dictionary<EventIdType, List<object>> allEvents = new Dictionary<EventIdType, List<object>>();
- private readonly Dictionary<Type, IObjectSystem> disposerEvents = new Dictionary<Type, IObjectSystem>();
- private Queue<Disposer> updates = new Queue<Disposer>();
- private Queue<Disposer> updates2 = new Queue<Disposer>();
- private readonly Queue<Disposer> starts = new Queue<Disposer>();
- private Queue<Disposer> loaders = new Queue<Disposer>();
- private Queue<Disposer> loaders2 = new Queue<Disposer>();
- private readonly HashSet<Disposer> unique = new HashSet<Disposer>();
- public void Add(DLLType dllType, Assembly assembly)
- {
- this.assemblies[dllType] = assembly;
- this.disposerEvents.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);
- IObjectSystem objectSystem = obj as IObjectSystem;
- if (objectSystem == null)
- {
- Log.Error($"组件事件没有继承IObjectEvent: {type.Name}");
- continue;
- }
- this.disposerEvents[objectSystem.Type()] = objectSystem;
- }
- 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);
- if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
- {
- this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<object>());
- }
- this.allEvents[(EventIdType)aEventAttribute.Type].Add(obj);
- }
- }
- this.Load();
- }
- public Assembly Get(DLLType dllType)
- {
- return this.assemblies[dllType];
- }
- public Assembly[] GetAll()
- {
- return this.assemblies.Values.ToArray();
- }
- public void Add(Disposer disposer)
- {
- if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
- {
- return;
- }
- if (objectEvent is ILoad)
- {
- this.loaders.Enqueue(disposer);
- }
- if (objectEvent is IUpdate)
- {
- this.updates.Enqueue(disposer);
- }
- if (objectEvent is IStart)
- {
- this.starts.Enqueue(disposer);
- }
- }
- public void Awake(Disposer disposer)
- {
- this.Add(disposer);
- if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
- {
- return;
- }
- IAwake iAwake = objectEvent as IAwake;
- if (iAwake == null)
- {
- return;
- }
- objectEvent.Set(disposer);
- iAwake.Awake();
- }
- public void Awake<P1>(Disposer disposer, P1 p1)
- {
- this.Add(disposer);
- if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
- {
- throw new Exception($"{disposer.GetType().Name} not found awake1");
- }
- IAwake<P1> iAwake = objectEvent as IAwake<P1>;
- if (iAwake == null)
- {
- throw new Exception($"{disposer.GetType().Name} not found awake1");
- }
- objectEvent.Set(disposer);
- iAwake.Awake(p1);
- }
- public void Awake<P1, P2>(Disposer disposer, P1 p1, P2 p2)
- {
- this.Add(disposer);
- if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
- {
- throw new Exception($"{disposer.GetType().Name} not found awake2");
- }
- IAwake<P1, P2> iAwake = objectEvent as IAwake<P1, P2>;
- if (iAwake == null)
- {
- throw new Exception($"{disposer.GetType().Name} not found awake2");
- }
- objectEvent.Set(disposer);
- iAwake.Awake(p1, p2);
- }
- public void Awake<P1, P2, P3>(Disposer disposer, P1 p1, P2 p2, P3 p3)
- {
- this.Add(disposer);
- if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
- {
- throw new Exception($"{disposer.GetType().Name} not found awake3");
- }
- IAwake<P1, P2, P3> iAwake = objectEvent as IAwake<P1, P2, P3>;
- if (iAwake == null)
- {
- throw new Exception($"{disposer.GetType().Name} not found awake3");
- }
- objectEvent.Set(disposer);
- iAwake.Awake(p1, p2, p3);
- }
- public void Load()
- {
- unique.Clear();
- while (this.loaders.Count > 0)
- {
- Disposer disposer = this.loaders.Dequeue();
- if (disposer.IsDisposed)
- {
- continue;
- }
- if (!this.unique.Add(disposer))
- {
- continue;
- }
- if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
- {
- continue;
- }
- this.loaders2.Enqueue(disposer);
- ILoad iLoad = objectEvent as ILoad;
- if (iLoad == null)
- {
- continue;
- }
- objectEvent.Set(disposer);
- try
- {
- iLoad.Load();
- }
- catch (Exception e)
- {
- Log.Error(e.ToString());
- }
- }
- ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
- }
- private void Start()
- {
- unique.Clear();
- while (this.starts.Count > 0)
- {
- Disposer disposer = this.starts.Dequeue();
- if (!this.unique.Add(disposer))
- {
- continue;
- }
- if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
- {
- continue;
- }
- IStart iStart = objectEvent as IStart;
- if (iStart == null)
- {
- continue;
- }
- objectEvent.Set(disposer);
- iStart.Start();
- }
- }
- public void Update()
- {
- this.Start();
- unique.Clear();
- while (this.updates.Count > 0)
- {
- Disposer disposer = this.updates.Dequeue();
- if (disposer.IsDisposed)
- {
- continue;
- }
- if (!this.unique.Add(disposer))
- {
- continue;
- }
- if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
- {
- continue;
- }
- this.updates2.Enqueue(disposer);
- IUpdate iUpdate = objectEvent as IUpdate;
- if (iUpdate == null)
- {
- continue;
- }
- objectEvent.Set(disposer);
- try
- {
- iUpdate.Update();
- }
- catch (Exception e)
- {
- Log.Error(e.ToString());
- }
- }
-
- ObjectHelper.Swap(ref this.updates, ref this.updates2);
- }
- 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());
- }
- }
- }
- }
- }
|