CodeLoader.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #else
  34. System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assBytes, pdbBytes);
  35. hotfixTypes = assembly.GetTypes();
  36. #endif
  37. #if ILRuntime
  38. this.start = new ILStaticMethod(appDomain, "ET.Entry", "Start", 0);
  39. #else
  40. this.start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  41. #endif
  42. }
  43. public void Start()
  44. {
  45. this.start.Run();
  46. }
  47. public Type[] GetHotfixTypes()
  48. {
  49. return this.hotfixTypes;
  50. }
  51. }
  52. }