ILRuntimeComponent.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using ILRuntime.CLR.Method;
  5. using ILRuntime.Runtime.Enviorment;
  6. using UnityEngine;
  7. namespace Model
  8. {
  9. [EntityEvent(typeof(ILRuntimeComponent))]
  10. public class ILRuntimeComponent : Component
  11. {
  12. private ILRuntime.Runtime.Enviorment.AppDomain appDomain;
  13. private readonly object[] param0 = new object[0];
  14. private IMethod start;
  15. private void Awake()
  16. {
  17. appDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  18. GameObject code = (GameObject)Resources.Load("Code");
  19. byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
  20. byte[] mdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes;
  21. using (MemoryStream fs = new MemoryStream(assBytes))
  22. using (MemoryStream p = new MemoryStream(mdbBytes))
  23. {
  24. appDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
  25. }
  26. Assembly assembly = Game.EntityEventManager.GetAssembly("Model");
  27. foreach (Type type in assembly.GetTypes())
  28. {
  29. object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
  30. if (attrs.Length == 0)
  31. {
  32. continue;
  33. }
  34. object obj = Activator.CreateInstance(type);
  35. CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
  36. if (adaptor == null)
  37. {
  38. continue;
  39. }
  40. appDomain.RegisterCrossBindingAdaptor(adaptor);
  41. }
  42. this.start = appDomain.LoadedTypes["Hotfix.HotfixEntry"].GetMethod("Start", 0);
  43. appDomain.Invoke(this.start, null, param0);
  44. }
  45. public override void Dispose()
  46. {
  47. if (this.Id == 0)
  48. {
  49. return;
  50. }
  51. base.Dispose();
  52. }
  53. }
  54. }