EventSystem.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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(ObjectSystemAttribute), 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. if (objectSystem is ILateUpdate)
  157. {
  158. this.lateUpdates.Enqueue(disposer);
  159. }
  160. }
  161. public void Awake(Disposer disposer)
  162. {
  163. this.Add(disposer);
  164. IObjectSystem objectSystem;
  165. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  166. {
  167. return;
  168. }
  169. IAwake iAwake = objectSystem as IAwake;
  170. if (iAwake == null)
  171. {
  172. return;
  173. }
  174. objectSystem.Set(disposer);
  175. iAwake.Awake();
  176. }
  177. public void Awake<P1>(Disposer disposer, P1 p1)
  178. {
  179. this.Add(disposer);
  180. IObjectSystem objectSystem;
  181. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  182. {
  183. return;
  184. }
  185. IAwake<P1> iAwake = objectSystem as IAwake<P1>;
  186. if (iAwake == null)
  187. {
  188. return;
  189. }
  190. objectSystem.Set(disposer);
  191. iAwake.Awake(p1);
  192. }
  193. public void Awake<P1, P2>(Disposer disposer, P1 p1, P2 p2)
  194. {
  195. this.Add(disposer);
  196. IObjectSystem objectSystem;
  197. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  198. {
  199. return;
  200. }
  201. IAwake<P1, P2> iAwake = objectSystem as IAwake<P1, P2>;
  202. if (iAwake == null)
  203. {
  204. return;
  205. }
  206. objectSystem.Set(disposer);
  207. iAwake.Awake(p1, p2);
  208. }
  209. public void Awake<P1, P2, P3>(Disposer disposer, P1 p1, P2 p2, P3 p3)
  210. {
  211. this.Add(disposer);
  212. IObjectSystem objectSystem;
  213. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  214. {
  215. return;
  216. }
  217. IAwake<P1, P2, P3> iAwake = objectSystem as IAwake<P1, P2, P3>;
  218. if (iAwake == null)
  219. {
  220. return;
  221. }
  222. objectSystem.Set(disposer);
  223. iAwake.Awake(p1, p2, p3);
  224. }
  225. public void Load()
  226. {
  227. while (this.loaders.Count > 0)
  228. {
  229. Disposer disposer = this.loaders.Dequeue();
  230. if (disposer.Id == 0)
  231. {
  232. continue;
  233. }
  234. IObjectSystem objectSystem;
  235. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  236. {
  237. continue;
  238. }
  239. this.loaders2.Enqueue(disposer);
  240. ILoad iLoad = objectSystem as ILoad;
  241. if (iLoad == null)
  242. {
  243. continue;
  244. }
  245. objectSystem.Set(disposer);
  246. try
  247. {
  248. iLoad.Load();
  249. }
  250. catch (Exception e)
  251. {
  252. Log.Error(e.ToString());
  253. }
  254. }
  255. ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
  256. }
  257. private void Start()
  258. {
  259. while (this.starts.Count > 0)
  260. {
  261. Disposer disposer = this.starts.Dequeue();
  262. IObjectSystem objectSystem;
  263. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  264. {
  265. continue;
  266. }
  267. IStart iStart = objectSystem as IStart;
  268. if (iStart == null)
  269. {
  270. continue;
  271. }
  272. objectSystem.Set(disposer);
  273. iStart.Start();
  274. }
  275. }
  276. public void Update()
  277. {
  278. this.Start();
  279. while (this.updates.Count > 0)
  280. {
  281. Disposer disposer = this.updates.Dequeue();
  282. if (disposer.Id == 0)
  283. {
  284. continue;
  285. }
  286. IObjectSystem objectSystem;
  287. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  288. {
  289. continue;
  290. }
  291. this.updates2.Enqueue(disposer);
  292. IUpdate iUpdate = objectSystem as IUpdate;
  293. if (iUpdate == null)
  294. {
  295. continue;
  296. }
  297. objectSystem.Set(disposer);
  298. try
  299. {
  300. iUpdate.Update();
  301. }
  302. catch (Exception e)
  303. {
  304. Log.Error(e.ToString());
  305. }
  306. }
  307. ObjectHelper.Swap(ref this.updates, ref this.updates2);
  308. }
  309. public void LateUpdate()
  310. {
  311. while (this.lateUpdates.Count > 0)
  312. {
  313. Disposer disposer = this.lateUpdates.Dequeue();
  314. if (disposer.Id == 0)
  315. {
  316. continue;
  317. }
  318. IObjectSystem objectSystem;
  319. if (!this.disposerEvents.TryGetValue(disposer.GetType(), out objectSystem))
  320. {
  321. continue;
  322. }
  323. this.lateUpdates2.Enqueue(disposer);
  324. ILateUpdate iLateUpdate = objectSystem as ILateUpdate;
  325. if (iLateUpdate == null)
  326. {
  327. continue;
  328. }
  329. objectSystem.Set(disposer);
  330. try
  331. {
  332. iLateUpdate.LateUpdate();
  333. }
  334. catch (Exception e)
  335. {
  336. Log.Error(e.ToString());
  337. }
  338. }
  339. ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
  340. }
  341. public void Run(EventIdType type)
  342. {
  343. List<IEventMethod> iEvents;
  344. if (!this.allEvents.TryGetValue(type, out iEvents))
  345. {
  346. return;
  347. }
  348. foreach (IEventMethod iEvent in iEvents)
  349. {
  350. try
  351. {
  352. iEvent.Run();
  353. }
  354. catch (Exception e)
  355. {
  356. Log.Error(e.ToString());
  357. }
  358. }
  359. }
  360. public void Run<A>(EventIdType type, A a)
  361. {
  362. List<IEventMethod> iEvents;
  363. if (!this.allEvents.TryGetValue(type, out iEvents))
  364. {
  365. return;
  366. }
  367. foreach (IEventMethod iEvent in iEvents)
  368. {
  369. try
  370. {
  371. iEvent.Run(a);
  372. }
  373. catch (Exception e)
  374. {
  375. Log.Error(e.ToString());
  376. }
  377. }
  378. }
  379. public void Run<A, B>(EventIdType type, A a, B b)
  380. {
  381. List<IEventMethod> iEvents;
  382. if (!this.allEvents.TryGetValue(type, out iEvents))
  383. {
  384. return;
  385. }
  386. foreach (IEventMethod iEvent in iEvents)
  387. {
  388. try
  389. {
  390. iEvent.Run(a, b);
  391. }
  392. catch (Exception e)
  393. {
  394. Log.Error(e.ToString());
  395. }
  396. }
  397. }
  398. public void Run<A, B, C>(EventIdType type, A a, B b, C c)
  399. {
  400. List<IEventMethod> iEvents;
  401. if (!this.allEvents.TryGetValue(type, out iEvents))
  402. {
  403. return;
  404. }
  405. foreach (IEventMethod iEvent in iEvents)
  406. {
  407. try
  408. {
  409. iEvent.Run(a, b, c);
  410. }
  411. catch (Exception e)
  412. {
  413. Log.Error(e.ToString());
  414. }
  415. }
  416. }
  417. }
  418. }