CodeLoader.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #define ILRuntime
  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 Type[] hotfixTypes;
  17. private CodeLoader()
  18. {
  19. }
  20. public void Start()
  21. {
  22. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  23. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  24. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  25. #if ILRuntime
  26. ILRuntime.Runtime.Enviorment.AppDomain appDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  27. System.IO.MemoryStream assStream = new System.IO.MemoryStream(assBytes);
  28. System.IO.MemoryStream pdbStream = new System.IO.MemoryStream(pdbBytes);
  29. appDomain.LoadAssembly(assStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
  30. ILHelper.InitILRuntime(appDomain);
  31. this.hotfixTypes = Type.EmptyTypes;
  32. this.hotfixTypes = appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray();
  33. IStaticMethod 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. IStaticMethod start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  38. #endif
  39. start.Run();
  40. }
  41. public Type[] GetHotfixTypes()
  42. {
  43. return this.hotfixTypes;
  44. }
  45. }
  46. }