EventSystem.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. Editor,
  12. }
  13. public interface IObjectSystem
  14. {
  15. Type Type();
  16. void Set(object value);
  17. }
  18. public abstract class ObjectSystem<T> : IObjectSystem
  19. {
  20. private T value;
  21. protected T Get()
  22. {
  23. return value;
  24. }
  25. public void Set(object v)
  26. {
  27. this.value = (T)v;
  28. }
  29. public Type Type()
  30. {
  31. return typeof(T);
  32. }
  33. }
  34. public sealed class EventSystem
  35. {
  36. private Assembly hotfixAssembly;
  37. public Assembly HotfixAssembly
  38. {
  39. get
  40. {
  41. return this.hotfixAssembly;
  42. }
  43. set
  44. {
  45. this.hotfixAssembly = value;
  46. }
  47. }
  48. private readonly Dictionary<DLLType, Assembly> assemblies = new Dictionary<DLLType, Assembly>();
  49. private readonly Dictionary<EventIdType, List<IEventMethod>> allEvents = new Dictionary<EventIdType, List<IEventMethod>>();
  50. private readonly Dictionary<Type, IObjectSystem> disposerEvents = new Dictionary<Type, IObjectSystem>();
  51. private Queue<Disposer> updates = new Queue<Disposer>();
  52. private Queue<Disposer> updates2 = new Queue<Disposer>();
  53. private readonly Queue<Disposer> starts = new Queue<Disposer>();
  54. private Queue<Disposer> loaders = new Queue<Disposer>();
  55. private Queue<Disposer> loaders2 = new Queue<Disposer>();
  56. private Queue<Disposer> lateUpdates = new Queue<Disposer>();
  57. private Queue<Disposer> lateUpdates2 = new Queue<Disposer>();
  58. public void LoadHotfixDll()
  59. {
  60. #if ILRuntime
  61. DllHelper.LoadHotfixAssembly();
  62. #else
  63. this.HotfixAssembly = DllHelper.LoadHotfixAssembly();
  64. #endif
  65. this.Register();
  66. this.Load();
  67. }
  68. public void Add(DLLType dllType, Assembly assembly)
  69. {
  70. this.assemblies[dllType] = assembly;
  71. this.Register();
  72. }
  73. private void Register()
  74. {
  75. Type[] types = DllHelper.GetMonoTypes();
  76. foreach (Type type in types)
  77. {
  78. object[] attrs = type.GetCustomAttributes(typeof(ObjectEventAttribute), false);
  79. if (attrs.Length == 0)
  80. {
  81. continue;
  82. }
  83. object obj = Activator.CreateInstance(type);
  84. IObjectSystem objectSystem = obj as IObjectSystem;
  85. if (objectSystem == null)
  86. {
  87. Log.Error($"组件事件没有继承IObjectEvent: {type.Name}");
  88. continue;
  89. }
  90. this.disposerEvents[objectSystem.Type()] = objectSystem;
  91. }
  92. this.allEvents.Clear();
  93. foreach (Type type in types)
  94. {
  95. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  96. foreach (object attr in attrs)
  97. {
  98. EventAttribute aEventAttribute = (EventAttribute)attr;
  99. object obj = Activator.CreateInstance(type);
  100. if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
  101. {
  102. this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<IEventMethod>());
  103. }
  104. this.allEvents[(EventIdType)aEventAttribute.Type].Add(new IEventMonoMethod(obj));
  105. }
  106. }
  107. // hotfix dll
  108. Type[] hotfixTypes = DllHelper.GetHotfixTypes();
  109. foreach (Type type in hotfixTypes)
  110. {
  111. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  112. foreach (object attr in attrs)
  113. {
  114. EventAttribute aEventAttribute = (EventAttribute)attr;
  115. #if ILRuntime
  116. IEventMethod method = new IEventILMethod(type, "Run");
  117. #else
  118. object obj = Activator.CreateInstance(type);
  119. IEventMethod method = new IEventMonoMethod(obj);
  120. #endif
  121. if (!allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
  122. {
  123. allEvents.Add((EventIdType)aEventAttribute.Type, new List<IEventMethod>());
  124. }
  125. allEvents[(EventIdType)aEventAttribute.Type].Add(method);
  126. }
  127. }
  128. }
  129. public Assembly Get(DLLType dllType)
  130. {
  131. return this.assemblies[dllType];
  132. }
  133. public Assembly[] GetAll()
  134. {
  135. return this.assemblies.Values.ToArray();
  136. }
  137. public void Add(Disposer disposer)
  138. {
  139. IObjectSystem objectSystem;
  140. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  141. {
  142. return;
  143. }
  144. if (objectSystem is ILoad)
  145. {
  146. this.loaders.Enqueue(disposer);
  147. }
  148. if (objectSystem is IUpdate)
  149. {
  150. this.updates.Enqueue(disposer);
  151. }
  152. if (objectSystem is IStart)
  153. {
  154. this.starts.Enqueue(disposer);
  155. }
  156. }
  157. public void Awake(Disposer disposer)
  158. {
  159. this.Add(disposer);
  160. IObjectSystem objectSystem;
  161. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  162. {
  163. return;
  164. }
  165. IAwake iAwake = objectSystem as IAwake;
  166. if (iAwake == null)
  167. {
  168. return;
  169. }
  170. objectSystem.Set(disposer);
  171. iAwake.Awake();
  172. }
  173. public void Awake<P1>(Disposer disposer, P1 p1)
  174. {
  175. this.Add(disposer);
  176. IObjectSystem objectSystem;
  177. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  178. {
  179. return;
  180. }
  181. IAwake<P1> iAwake = objectSystem as IAwake<P1>;
  182. if (iAwake == null)
  183. {
  184. return;
  185. }
  186. objectSystem.Set(disposer);
  187. iAwake.Awake(p1);
  188. }
  189. public void Awake<P1, P2>(Disposer disposer, P1 p1, P2 p2)
  190. {
  191. this.Add(disposer);
  192. IObjectSystem objectSystem;
  193. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  194. {
  195. return;
  196. }
  197. IAwake<P1, P2> iAwake = objectSystem as IAwake<P1, P2>;
  198. if (iAwake == null)
  199. {
  200. return;
  201. }
  202. objectSystem.Set(disposer);
  203. iAwake.Awake(p1, p2);
  204. }
  205. public void Awake<P1, P2, P3>(Disposer disposer, P1 p1, P2 p2, P3 p3)
  206. {
  207. this.Add(disposer);
  208. IObjectSystem objectSystem;
  209. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  210. {
  211. return;
  212. }
  213. IAwake<P1, P2, P3> iAwake = objectSystem as IAwake<P1, P2, P3>;
  214. if (iAwake == null)
  215. {
  216. return;
  217. }
  218. objectSystem.Set(disposer);
  219. iAwake.Awake(p1, p2, p3);
  220. }
  221. public void Load()
  222. {
  223. while (this.loaders.Count > 0)
  224. {
  225. Disposer disposer = this.loaders.Dequeue();
  226. if (disposer.Id == 0)
  227. {
  228. continue;
  229. }
  230. IObjectSystem objectSystem;
  231. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  232. {
  233. continue;
  234. }
  235. this.loaders2.Enqueue(disposer);
  236. ILoad iLoad = objectSystem as ILoad;
  237. if (iLoad == null)
  238. {
  239. continue;
  240. }
  241. objectSystem.Set(disposer);
  242. try
  243. {
  244. iLoad.Load();
  245. }
  246. catch (Exception e)
  247. {
  248. Log.Error(e.ToString());
  249. }
  250. }
  251. ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
  252. }
  253. private void Start()
  254. {
  255. while (this.starts.Count > 0)
  256. {
  257. Disposer disposer = this.starts.Dequeue();
  258. IObjectSystem objectSystem;
  259. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  260. {
  261. continue;
  262. }
  263. IStart iStart = objectSystem as IStart;
  264. if (iStart == null)
  265. {
  266. continue;
  267. }
  268. objectSystem.Set(disposer);
  269. iStart.Start();
  270. }
  271. }
  272. public void Update()
  273. {
  274. this.Start();
  275. while (this.updates.Count > 0)
  276. {
  277. Disposer disposer = this.updates.Dequeue();
  278. if (disposer.Id == 0)
  279. {
  280. continue;
  281. }
  282. IObjectSystem objectSystem;
  283. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  284. {
  285. continue;
  286. }
  287. this.updates2.Enqueue(disposer);
  288. IUpdate iUpdate = objectSystem as IUpdate;
  289. if (iUpdate == null)
  290. {
  291. continue;
  292. }
  293. objectSystem.Set(disposer);
  294. try
  295. {
  296. iUpdate.Update();
  297. }
  298. catch (Exception e)
  299. {
  300. Log.Error(e.ToString());
  301. }
  302. }
  303. ObjectHelper.Swap(ref this.updates, ref this.updates2);
  304. }
  305. public void LateUpdate()
  306. {
  307. while (this.lateUpdates.Count > 0)
  308. {
  309. Disposer disposer = this.lateUpdates.Dequeue();
  310. if (disposer.Id == 0)
  311. {
  312. continue;
  313. }
  314. IObjectSystem objectSystem;
  315. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  316. {
  317. continue;
  318. }
  319. this.lateUpdates2.Enqueue(disposer);
  320. ILateUpdate iLateUpdate = objectSystem as ILateUpdate;
  321. if (iLateUpdate == null)
  322. {
  323. continue;
  324. }
  325. objectSystem.Set(disposer);
  326. try
  327. {
  328. iLateUpdate.LateUpdate();
  329. }
  330. catch (Exception e)
  331. {
  332. Log.Error(e.ToString());
  333. }
  334. }
  335. ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
  336. }
  337. public void Run(EventIdType type)
  338. {
  339. List<IEventMethod> iEvents;
  340. if (!this.allEvents.TryGetValue(type, out iEvents))
  341. {
  342. return;
  343. }
  344. foreach (IEventMethod iEvent in iEvents)
  345. {
  346. try
  347. {
  348. iEvent.Run();
  349. }
  350. catch (Exception e)
  351. {
  352. Log.Error(e.ToString());
  353. }
  354. }
  355. }
  356. public void Run<A>(EventIdType type, A a)
  357. {
  358. List<IEventMethod> iEvents;
  359. if (!this.allEvents.TryGetValue(type, out iEvents))
  360. {
  361. return;
  362. }
  363. foreach (IEventMethod iEvent in iEvents)
  364. {
  365. try
  366. {
  367. iEvent.Run(a);
  368. }
  369. catch (Exception e)
  370. {
  371. Log.Error(e.ToString());
  372. }
  373. }
  374. }
  375. public void Run<A, B>(EventIdType type, A a, B b)
  376. {
  377. List<IEventMethod> iEvents;
  378. if (!this.allEvents.TryGetValue(type, out iEvents))
  379. {
  380. return;
  381. }
  382. foreach (IEventMethod iEvent in iEvents)
  383. {
  384. try
  385. {
  386. iEvent.Run(a, b);
  387. }
  388. catch (Exception e)
  389. {
  390. Log.Error(e.ToString());
  391. }
  392. }
  393. }
  394. public void Run<A, B, C>(EventIdType type, A a, B b, C c)
  395. {
  396. List<IEventMethod> iEvents;
  397. if (!this.allEvents.TryGetValue(type, out iEvents))
  398. {
  399. return;
  400. }
  401. foreach (IEventMethod iEvent in iEvents)
  402. {
  403. try
  404. {
  405. iEvent.Run(a, b, c);
  406. }
  407. catch (Exception e)
  408. {
  409. Log.Error(e.ToString());
  410. }
  411. }
  412. }
  413. }
  414. }