CodeLoader.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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;
  14. public Action LateUpdate;
  15. public Action OnApplicationQuit;
  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. System.IO.MemoryStream assStream = new System.IO.MemoryStream(assBytes);
  26. System.IO.MemoryStream pdbStream = new System.IO.MemoryStream(pdbBytes);
  27. appDomain.LoadAssembly(assStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
  28. ILHelper.InitILRuntime(appDomain);
  29. this.hotfixTypes = Type.EmptyTypes;
  30. this.hotfixTypes = appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray();
  31. this.start = new ILStaticMethod(appDomain, "ET.Entry", "Start", 0);
  32. #else
  33. System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assBytes, pdbBytes);
  34. hotfixTypes = assembly.GetTypes();
  35. this.start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  36. #endif
  37. }
  38. public void Start()
  39. {
  40. this.start.Run();
  41. }
  42. public Type[] GetHotfixTypes()
  43. {
  44. return this.hotfixTypes;
  45. }
  46. }
  47. }