EventSystem.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace Model
  6. {
  7. public enum DLLType
  8. {
  9. Model,
  10. Hotfix,
  11. }
  12. public interface IObjectSystem
  13. {
  14. Type Type();
  15. void Set(object value);
  16. }
  17. public abstract class ObjectSystem<T> : IObjectSystem
  18. {
  19. private T value;
  20. protected T Get()
  21. {
  22. return value;
  23. }
  24. public void Set(object v)
  25. {
  26. this.value = (T)v;
  27. }
  28. public Type Type()
  29. {
  30. return typeof(T);
  31. }
  32. }
  33. public sealed class EventSystem
  34. {
  35. private readonly Dictionary<DLLType, Assembly> assemblies = new Dictionary<DLLType, Assembly>();
  36. private readonly Dictionary<EventIdType, List<object>> allEvents = new Dictionary<EventIdType, List<object>>();
  37. private readonly Dictionary<Type, IObjectSystem> disposerEvents = new Dictionary<Type, IObjectSystem>();
  38. private Queue<Disposer> updates = new Queue<Disposer>();
  39. private Queue<Disposer> updates2 = new Queue<Disposer>();
  40. private readonly Queue<Disposer> starts = new Queue<Disposer>();
  41. private Queue<Disposer> loaders = new Queue<Disposer>();
  42. private Queue<Disposer> loaders2 = new Queue<Disposer>();
  43. private readonly HashSet<Disposer> unique = new HashSet<Disposer>();
  44. public void Add(DLLType dllType, Assembly assembly)
  45. {
  46. this.assemblies[dllType] = assembly;
  47. this.disposerEvents.Clear();
  48. Type[] types = DllHelper.GetMonoTypes();
  49. foreach (Type type in types)
  50. {
  51. object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false);
  52. if (attrs.Length == 0)
  53. {
  54. continue;
  55. }
  56. object obj = Activator.CreateInstance(type);
  57. IObjectSystem objectSystem = obj as IObjectSystem;
  58. if (objectSystem == null)
  59. {
  60. Log.Error($"组件事件没有继承IObjectEvent: {type.Name}");
  61. continue;
  62. }
  63. this.disposerEvents[objectSystem.Type()] = objectSystem;
  64. }
  65. allEvents.Clear();
  66. foreach (Type type in types)
  67. {
  68. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  69. foreach (object attr in attrs)
  70. {
  71. EventAttribute aEventAttribute = (EventAttribute)attr;
  72. object obj = Activator.CreateInstance(type);
  73. if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
  74. {
  75. this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<object>());
  76. }
  77. this.allEvents[(EventIdType)aEventAttribute.Type].Add(obj);
  78. }
  79. }
  80. this.Load();
  81. }
  82. public Assembly Get(DLLType dllType)
  83. {
  84. return this.assemblies[dllType];
  85. }
  86. public Assembly[] GetAll()
  87. {
  88. return this.assemblies.Values.ToArray();
  89. }
  90. public void Add(Disposer disposer)
  91. {
  92. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
  93. {
  94. return;
  95. }
  96. if (objectEvent is ILoad)
  97. {
  98. this.loaders.Enqueue(disposer);
  99. }
  100. if (objectEvent is IUpdate)
  101. {
  102. this.updates.Enqueue(disposer);
  103. }
  104. if (objectEvent is IStart)
  105. {
  106. this.starts.Enqueue(disposer);
  107. }
  108. }
  109. public void Awake(Disposer disposer)
  110. {
  111. this.Add(disposer);
  112. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
  113. {
  114. return;
  115. }
  116. IAwake iAwake = objectEvent as IAwake;
  117. if (iAwake == null)
  118. {
  119. return;
  120. }
  121. objectEvent.Set(disposer);
  122. iAwake.Awake();
  123. }
  124. public void Awake<P1>(Disposer disposer, P1 p1)
  125. {
  126. this.Add(disposer);
  127. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
  128. {
  129. throw new Exception($"{disposer.GetType().Name} not found awake1");
  130. }
  131. IAwake<P1> iAwake = objectEvent as IAwake<P1>;
  132. if (iAwake == null)
  133. {
  134. throw new Exception($"{disposer.GetType().Name} not found awake1");
  135. }
  136. objectEvent.Set(disposer);
  137. iAwake.Awake(p1);
  138. }
  139. public void Awake<P1, P2>(Disposer disposer, P1 p1, P2 p2)
  140. {
  141. this.Add(disposer);
  142. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
  143. {
  144. throw new Exception($"{disposer.GetType().Name} not found awake2");
  145. }
  146. IAwake<P1, P2> iAwake = objectEvent as IAwake<P1, P2>;
  147. if (iAwake == null)
  148. {
  149. throw new Exception($"{disposer.GetType().Name} not found awake2");
  150. }
  151. objectEvent.Set(disposer);
  152. iAwake.Awake(p1, p2);
  153. }
  154. public void Awake<P1, P2, P3>(Disposer disposer, P1 p1, P2 p2, P3 p3)
  155. {
  156. this.Add(disposer);
  157. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
  158. {
  159. throw new Exception($"{disposer.GetType().Name} not found awake3");
  160. }
  161. IAwake<P1, P2, P3> iAwake = objectEvent as IAwake<P1, P2, P3>;
  162. if (iAwake == null)
  163. {
  164. throw new Exception($"{disposer.GetType().Name} not found awake3");
  165. }
  166. objectEvent.Set(disposer);
  167. iAwake.Awake(p1, p2, p3);
  168. }
  169. public void Load()
  170. {
  171. unique.Clear();
  172. while (this.loaders.Count > 0)
  173. {
  174. Disposer disposer = this.loaders.Dequeue();
  175. if (disposer.Id == 0)
  176. {
  177. continue;
  178. }
  179. if (!this.unique.Add(disposer))
  180. {
  181. continue;
  182. }
  183. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
  184. {
  185. continue;
  186. }
  187. this.loaders2.Enqueue(disposer);
  188. ILoad iLoad = objectEvent as ILoad;
  189. if (iLoad == null)
  190. {
  191. continue;
  192. }
  193. objectEvent.Set(disposer);
  194. try
  195. {
  196. iLoad.Load();
  197. }
  198. catch (Exception e)
  199. {
  200. Log.Error(e.ToString());
  201. }
  202. }
  203. ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
  204. }
  205. private void Start()
  206. {
  207. unique.Clear();
  208. while (this.starts.Count > 0)
  209. {
  210. Disposer disposer = this.starts.Dequeue();
  211. if (!this.unique.Add(disposer))
  212. {
  213. continue;
  214. }
  215. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
  216. {
  217. continue;
  218. }
  219. IStart iStart = objectEvent as IStart;
  220. if (iStart == null)
  221. {
  222. continue;
  223. }
  224. objectEvent.Set(disposer);
  225. iStart.Start();
  226. }
  227. }
  228. public void Update()
  229. {
  230. this.Start();
  231. unique.Clear();
  232. while (this.updates.Count > 0)
  233. {
  234. Disposer disposer = this.updates.Dequeue();
  235. if (disposer.Id == 0)
  236. {
  237. continue;
  238. }
  239. if (!this.unique.Add(disposer))
  240. {
  241. continue;
  242. }
  243. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out IObjectSystem objectEvent))
  244. {
  245. continue;
  246. }
  247. this.updates2.Enqueue(disposer);
  248. IUpdate iUpdate = objectEvent as IUpdate;
  249. if (iUpdate == null)
  250. {
  251. continue;
  252. }
  253. objectEvent.Set(disposer);
  254. try
  255. {
  256. iUpdate.Update();
  257. }
  258. catch (Exception e)
  259. {
  260. Log.Error(e.ToString());
  261. }
  262. }
  263. ObjectHelper.Swap(ref this.updates, ref this.updates2);
  264. }
  265. public void Run(EventIdType type)
  266. {
  267. List<object> iEvents;
  268. if (!this.allEvents.TryGetValue(type, out iEvents))
  269. {
  270. return;
  271. }
  272. foreach (object obj in iEvents)
  273. {
  274. try
  275. {
  276. IEvent iEvent = (IEvent)obj;
  277. iEvent.Run();
  278. }
  279. catch (Exception e)
  280. {
  281. Log.Error(e.ToString());
  282. }
  283. }
  284. }
  285. public void Run<A>(EventIdType type, A a)
  286. {
  287. List<object> iEvents;
  288. if (!this.allEvents.TryGetValue(type, out iEvents))
  289. {
  290. return;
  291. }
  292. foreach (object obj in iEvents)
  293. {
  294. try
  295. {
  296. IEvent<A> iEvent = (IEvent<A>)obj;
  297. iEvent.Run(a);
  298. }
  299. catch (Exception err)
  300. {
  301. Log.Error(err.ToString());
  302. }
  303. }
  304. }
  305. public void Run<A, B>(EventIdType type, A a, B b)
  306. {
  307. List<object> iEvents;
  308. if (!this.allEvents.TryGetValue(type, out iEvents))
  309. {
  310. return;
  311. }
  312. foreach (object obj in iEvents)
  313. {
  314. try
  315. {
  316. IEvent<A, B> iEvent = (IEvent<A, B>)obj;
  317. iEvent.Run(a, b);
  318. }
  319. catch (Exception err)
  320. {
  321. Log.Error(err.ToString());
  322. }
  323. }
  324. }
  325. public void Run<A, B, C>(EventIdType type, A a, B b, C c)
  326. {
  327. List<object> iEvents;
  328. if (!this.allEvents.TryGetValue(type, out iEvents))
  329. {
  330. return;
  331. }
  332. foreach (object obj in iEvents)
  333. {
  334. try
  335. {
  336. IEvent<A, B, C> iEvent = (IEvent<A, B, C>)obj;
  337. iEvent.Run(a, b, c);
  338. }
  339. catch (Exception err)
  340. {
  341. Log.Error(err.ToString());
  342. }
  343. }
  344. }
  345. }
  346. }