ObjectEvents.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using Base;
  8. #if !SERVER
  9. using ILRuntime.CLR.Method;
  10. using ILRuntime.CLR.TypeSystem;
  11. using ILRuntime.Runtime.Enviorment;
  12. using UnityEngine;
  13. #endif
  14. namespace Model
  15. {
  16. [Flags]
  17. public enum EntityEventType
  18. {
  19. Awake = 1,
  20. Awake1 = 2,
  21. Awake2 = 4,
  22. Awake3 = 8,
  23. Update = 16,
  24. Load = 32,
  25. LateUpdate = 64
  26. }
  27. public class EntityTypeInfo
  28. {
  29. private readonly Dictionary<EntityEventType, IStaticMethod> infos = new Dictionary<EntityEventType, IStaticMethod>();
  30. public void Add(EntityEventType type, IStaticMethod methodInfo)
  31. {
  32. try
  33. {
  34. this.infos.Add(type, methodInfo);
  35. }
  36. catch (Exception e)
  37. {
  38. throw new Exception($"Add EntityEventType MethodInfo Error: {type}", e);
  39. }
  40. }
  41. public IStaticMethod Get(EntityEventType type)
  42. {
  43. IStaticMethod methodInfo;
  44. this.infos.TryGetValue(type, out methodInfo);
  45. return methodInfo;
  46. }
  47. public EntityEventType[] GetEntityEventTypes()
  48. {
  49. return this.infos.Keys.ToArray();
  50. }
  51. public override string ToString()
  52. {
  53. StringBuilder sb = new StringBuilder();
  54. foreach (EntityEventType disposerEventType in this.infos.Keys.ToArray())
  55. {
  56. sb.Append($"{disposerEventType} {this.infos[disposerEventType].Name} ");
  57. }
  58. return sb.ToString();
  59. }
  60. }
  61. public sealed class ObjectEvents
  62. {
  63. private static ObjectEvents instance;
  64. private readonly Dictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();
  65. private readonly Dictionary<EntityEventType, HashSet<Disposer>> disposers = new Dictionary<EntityEventType, HashSet<Disposer>>();
  66. private readonly HashSet<Disposer> addDisposers = new HashSet<Disposer>();
  67. private readonly HashSet<Disposer> removeDisposers = new HashSet<Disposer>();
  68. private Dictionary<int, EntityTypeInfo> eventInfo;
  69. private Dictionary<Type, int> typeToEntityEventId;
  70. #if !SERVER
  71. private ILRuntime.Runtime.Enviorment.AppDomain appDomain;
  72. #endif
  73. public ObjectEvents()
  74. {
  75. foreach (EntityEventType t in Enum.GetValues(typeof (EntityEventType)))
  76. {
  77. this.disposers.Add(t, new HashSet<Disposer>());
  78. }
  79. }
  80. public static ObjectEvents Instance
  81. {
  82. get
  83. {
  84. return instance ?? (instance = new ObjectEvents());
  85. }
  86. }
  87. public void Reset()
  88. {
  89. instance = null;
  90. }
  91. public void Register(string name, Assembly assembly)
  92. {
  93. this.assemblies[name] = assembly;
  94. LoadAssemblyInfo();
  95. this.Load();
  96. }
  97. public void LoadAssemblyInfo()
  98. {
  99. this.eventInfo = new Dictionary<int, EntityTypeInfo>();
  100. this.typeToEntityEventId = new Dictionary<Type, int>();
  101. Type[] types = DllHelper.GetMonoTypes();
  102. List<string> allEntityType = Enum.GetNames(typeof(EntityEventType)).ToList();
  103. foreach (Type type in types)
  104. {
  105. object[] attrs = type.GetCustomAttributes(typeof(EntityEventAttribute), true);
  106. if (attrs.Length == 0)
  107. {
  108. continue;
  109. }
  110. EntityEventAttribute entityEventAttribute = attrs[0] as EntityEventAttribute;
  111. int entityEventId = entityEventAttribute.ClassType;
  112. this.typeToEntityEventId[type] = entityEventId;
  113. if (!this.eventInfo.ContainsKey(entityEventId))
  114. {
  115. this.eventInfo.Add(entityEventId, new EntityTypeInfo());
  116. }
  117. MethodInfo[] methodInfos = type.GetMethods(
  118. BindingFlags.Instance | BindingFlags.Static | BindingFlags.InvokeMethod |
  119. BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly);
  120. foreach (MethodInfo methodInfo in methodInfos)
  121. {
  122. int n = methodInfo.GetParameters().Length;
  123. if (methodInfo.IsStatic)
  124. {
  125. --n;
  126. }
  127. string sn = n > 0 ? $"{methodInfo.Name}{n}" : methodInfo.Name;
  128. if (!allEntityType.Contains(sn))
  129. {
  130. continue;
  131. }
  132. EntityEventType t = EnumHelper.FromString<EntityEventType>(sn);
  133. this.eventInfo[entityEventId].Add(t, new MonoStaticMethod(methodInfo));
  134. }
  135. }
  136. #if !SERVER
  137. if (this.appDomain == null)
  138. {
  139. return;
  140. }
  141. IType[] ilTypes = this.appDomain.LoadedTypes.Values.ToArray();
  142. foreach (IType itype in ilTypes)
  143. {
  144. Type type = itype.ReflectionType;
  145. object[] attrs = type.GetCustomAttributes(typeof(EntityEventAttribute), true);
  146. if (attrs.Length == 0)
  147. {
  148. continue;
  149. }
  150. EntityEventAttribute entityEventAttribute = attrs[0] as EntityEventAttribute;
  151. int entityEventId = entityEventAttribute.ClassType;
  152. if (!this.eventInfo.ContainsKey(entityEventId))
  153. {
  154. this.eventInfo.Add(entityEventId, new EntityTypeInfo());
  155. }
  156. foreach (IMethod methodInfo in itype.GetMethods())
  157. {
  158. int n = methodInfo.ParameterCount;
  159. if (methodInfo.IsStatic)
  160. {
  161. --n;
  162. }
  163. string sn = n > 0 ? $"{methodInfo.Name}{n}" : methodInfo.Name;
  164. if (!allEntityType.Contains(sn))
  165. {
  166. continue;
  167. }
  168. EntityEventType t = EnumHelper.FromString<EntityEventType>(sn);
  169. this.eventInfo[entityEventId].Add(t, new ILStaticMethod(methodInfo, n));
  170. }
  171. }
  172. #endif
  173. }
  174. #if !SERVER
  175. public void RegisterILRuntime()
  176. {
  177. appDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  178. ILRuntime.Runtime.Generated.CLRBindings.Initialize(appDomain);
  179. GameObject code = (GameObject)Resources.Load("Code");
  180. byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
  181. byte[] mdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes;
  182. using (MemoryStream fs = new MemoryStream(assBytes))
  183. using (MemoryStream p = new MemoryStream(mdbBytes))
  184. {
  185. appDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
  186. }
  187. }
  188. public void RegisterILAdapter()
  189. {
  190. Assembly assembly = ObjectEvents.Instance.GetAssembly("Model");
  191. foreach (Type type in assembly.GetTypes())
  192. {
  193. object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
  194. if (attrs.Length == 0)
  195. {
  196. continue;
  197. }
  198. object obj = Activator.CreateInstance(type);
  199. CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
  200. if (adaptor == null)
  201. {
  202. continue;
  203. }
  204. appDomain.RegisterCrossBindingAdaptor(adaptor);
  205. }
  206. }
  207. public ILRuntime.Runtime.Enviorment.AppDomain AppDomain
  208. {
  209. get
  210. {
  211. return this.appDomain;
  212. }
  213. }
  214. #endif
  215. private int GetEntityEventIdByType(Type type)
  216. {
  217. int entityEventId = 0;
  218. this.typeToEntityEventId.TryGetValue(type, out entityEventId);
  219. return entityEventId;
  220. }
  221. public void Add(Disposer disposer)
  222. {
  223. this.addDisposers.Add(disposer);
  224. }
  225. public void Remove(Disposer disposer)
  226. {
  227. this.removeDisposers.Add(disposer);
  228. }
  229. private void UpdateAddDisposer()
  230. {
  231. foreach (Disposer disposer in this.addDisposers)
  232. {
  233. EntityTypeInfo entityTypeInfo;
  234. if (!this.eventInfo.TryGetValue(this.GetEntityEventIdByType(disposer.GetType()), out entityTypeInfo))
  235. {
  236. continue;
  237. }
  238. foreach (EntityEventType disposerEvent2Type in entityTypeInfo.GetEntityEventTypes())
  239. {
  240. this.disposers[disposerEvent2Type].Add(disposer);
  241. }
  242. }
  243. this.addDisposers.Clear();
  244. }
  245. private void UpdateRemoveDisposer()
  246. {
  247. foreach (Disposer disposer in this.removeDisposers)
  248. {
  249. EntityTypeInfo entityTypeInfo;
  250. if (!this.eventInfo.TryGetValue(this.GetEntityEventIdByType(disposer.GetType()), out entityTypeInfo))
  251. {
  252. continue;
  253. }
  254. foreach (EntityEventType disposerEvent2Type in entityTypeInfo.GetEntityEventTypes())
  255. {
  256. this.disposers[disposerEvent2Type].Remove(disposer);
  257. }
  258. }
  259. this.removeDisposers.Clear();
  260. }
  261. public Assembly GetAssembly(string name)
  262. {
  263. return this.assemblies[name];
  264. }
  265. public Assembly[] GetAssemblies()
  266. {
  267. return this.assemblies.Values.ToArray();
  268. }
  269. private void Load()
  270. {
  271. HashSet<Disposer> list;
  272. if (!this.disposers.TryGetValue(EntityEventType.Load, out list))
  273. {
  274. return;
  275. }
  276. foreach (Disposer disposer in list)
  277. {
  278. EntityTypeInfo entityTypeInfo = this.eventInfo[this.GetEntityEventIdByType(disposer.GetType())];
  279. entityTypeInfo.Get(EntityEventType.Load).Run(disposer);
  280. }
  281. }
  282. public void Awake(Disposer disposer)
  283. {
  284. EntityTypeInfo entityTypeInfo;
  285. if (!this.eventInfo.TryGetValue(this.GetEntityEventIdByType(disposer.GetType()), out entityTypeInfo))
  286. {
  287. return;
  288. }
  289. entityTypeInfo.Get(EntityEventType.Awake)?.Run(disposer);
  290. }
  291. public void Awake(Disposer disposer, object p1)
  292. {
  293. EntityTypeInfo entityTypeInfo;
  294. if (!this.eventInfo.TryGetValue(this.GetEntityEventIdByType(disposer.GetType()), out entityTypeInfo))
  295. {
  296. return;
  297. }
  298. entityTypeInfo.Get(EntityEventType.Awake1)?.Run(disposer, p1);
  299. }
  300. public void Awake(Disposer disposer, object p1, object p2)
  301. {
  302. EntityTypeInfo entityTypeInfo;
  303. if (!this.eventInfo.TryGetValue(this.GetEntityEventIdByType(disposer.GetType()), out entityTypeInfo))
  304. {
  305. return;
  306. }
  307. entityTypeInfo.Get(EntityEventType.Awake2)?.Run(disposer, p1, p2);
  308. }
  309. public void Awake(Disposer disposer, object p1, object p2, object p3)
  310. {
  311. EntityTypeInfo entityTypeInfo;
  312. if (!this.eventInfo.TryGetValue(this.GetEntityEventIdByType(disposer.GetType()), out entityTypeInfo))
  313. {
  314. return;
  315. }
  316. entityTypeInfo.Get(EntityEventType.Awake3)?.Run(disposer, p1, p2, p3);
  317. }
  318. public void Update()
  319. {
  320. UpdateAddDisposer();
  321. UpdateRemoveDisposer();
  322. HashSet<Disposer> list;
  323. if (!this.disposers.TryGetValue(EntityEventType.Update, out list))
  324. {
  325. return;
  326. }
  327. foreach (Disposer disposer in list)
  328. {
  329. try
  330. {
  331. if (this.removeDisposers.Contains(disposer))
  332. {
  333. continue;
  334. }
  335. EntityTypeInfo entityTypeInfo = this.eventInfo[this.GetEntityEventIdByType(disposer.GetType())];
  336. entityTypeInfo.Get(EntityEventType.Update).Run(disposer);
  337. }
  338. catch (Exception e)
  339. {
  340. Log.Error(e.ToString());
  341. }
  342. }
  343. }
  344. public void LateUpdate()
  345. {
  346. HashSet<Disposer> list;
  347. if (!this.disposers.TryGetValue(EntityEventType.LateUpdate, out list))
  348. {
  349. return;
  350. }
  351. foreach (Disposer disposer in list)
  352. {
  353. try
  354. {
  355. EntityTypeInfo entityTypeInfo = this.eventInfo[this.GetEntityEventIdByType(disposer.GetType())];
  356. entityTypeInfo.Get(EntityEventType.LateUpdate).Run(disposer);
  357. }
  358. catch (Exception e)
  359. {
  360. Log.Error(e.ToString());
  361. }
  362. }
  363. }
  364. }
  365. }