ILRuntimeCLRBinding.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System;
  5. using System.Text;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using ILRuntime.Runtime.Enviorment;
  9. using ETModel;
  10. [System.Reflection.Obfuscation(Exclude = true)]
  11. public class ILRuntimeCLRBinding
  12. {
  13. [MenuItem("Tools/ILRuntime/Generate CLR Binding Code")]
  14. static void GenerateCLRBinding()
  15. {
  16. List<Type> types = new List<Type>();
  17. types.Add(typeof(int));
  18. types.Add(typeof(float));
  19. types.Add(typeof(long));
  20. types.Add(typeof(object));
  21. types.Add(typeof(string));
  22. types.Add(typeof(Array));
  23. types.Add(typeof(Vector2));
  24. types.Add(typeof(Vector3));
  25. types.Add(typeof(Quaternion));
  26. types.Add(typeof(GameObject));
  27. types.Add(typeof(UnityEngine.Object));
  28. types.Add(typeof(Transform));
  29. types.Add(typeof(RectTransform));
  30. types.Add(typeof(Time));
  31. types.Add(typeof(Debug));
  32. //所有DLL内的类型的真实C#类型都是ILTypeInstance
  33. types.Add(typeof(List<ILRuntime.Runtime.Intepreter.ILTypeInstance>));
  34. ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(types, "Assets/ThirdParty/ILRuntime/Generated");
  35. }
  36. [MenuItem("Tools/ILRuntime/Generate CLR Binding Code by Analysis")]
  37. static void GenerateCLRBindingByAnalysis()
  38. {
  39. //用新的分析热更dll调用引用来生成绑定代码
  40. ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain();
  41. using (System.IO.FileStream fs = new System.IO.FileStream("Assets/Res/Code/Hotfix.dll.bytes", System.IO.FileMode.Open, System.IO.FileAccess.Read))
  42. {
  43. domain.LoadAssembly(fs);
  44. }
  45. //Crossbind Adapter is needed to generate the correct binding code
  46. InitILRuntime(domain);
  47. ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, "Assets/ThirdParty/ILRuntime/Generated");
  48. }
  49. static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain domain)
  50. {
  51. //这里需要注册所有热更DLL中用到的跨域继承Adapter,否则无法正确抓取引用
  52. // 注册适配器
  53. Assembly assembly = typeof(Init).Assembly;
  54. foreach (Type type in assembly.GetTypes())
  55. {
  56. object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
  57. if (attrs.Length == 0)
  58. {
  59. continue;
  60. }
  61. object obj = Activator.CreateInstance(type);
  62. CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
  63. if (adaptor == null)
  64. {
  65. continue;
  66. }
  67. domain.RegisterCrossBindingAdaptor(adaptor);
  68. }
  69. }
  70. }
  71. #endif