CodeLoader.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #define ILRuntime1
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. #if ILRuntime
  6. using System.Linq;
  7. #endif
  8. namespace ET
  9. {
  10. public class CodeLoader
  11. {
  12. public static CodeLoader Instance = new CodeLoader();
  13. public Action Update { get; set; }
  14. public Action LateUpdate { get; set; }
  15. public Action OnApplicationQuit { get; set; }
  16. private readonly IStaticMethod start;
  17. private readonly Type[] hotfixTypes;
  18. private CodeLoader()
  19. {
  20. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  21. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  22. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  23. #if ILRuntime
  24. ILRuntime.Runtime.Enviorment.AppDomain appDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  25. using (System.IO.MemoryStream assStream = new System.IO.MemoryStream(assBytes))
  26. using (System.IO.MemoryStream pdbStream = new System.IO.MemoryStream(pdbBytes))
  27. {
  28. appDomain.LoadAssembly(assStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
  29. }
  30. ILHelper.InitILRuntime(appDomain);
  31. this.hotfixTypes = Type.EmptyTypes;
  32. this.hotfixTypes = appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray();
  33. this.start = new ILStaticMethod(appDomain, "ET.Entry", "Start", 0);
  34. #else
  35. System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assBytes, pdbBytes);
  36. hotfixTypes = assembly.GetTypes();
  37. this.start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  38. #endif
  39. }
  40. public void Start()
  41. {
  42. this.start.Run();
  43. }
  44. public Type[] GetHotfixTypes()
  45. {
  46. return this.hotfixTypes;
  47. }
  48. }
  49. }