MethodBridgeGeneratorCommand.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using HybridCLR.Editor;
  2. using HybridCLR.Editor.ABI;
  3. using HybridCLR.Editor.Meta;
  4. using HybridCLR.Editor.MethodBridge;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using UnityEditor;
  13. using UnityEditor.Build;
  14. using UnityEngine;
  15. namespace HybridCLR.Editor.Commands
  16. {
  17. using Analyzer = HybridCLR.Editor.MethodBridge.Analyzer;
  18. public class MethodBridgeGeneratorCommand
  19. {
  20. public static void CleanIl2CppBuildCache()
  21. {
  22. string il2cppBuildCachePath = SettingsUtil.Il2CppBuildCacheDir;
  23. if (!Directory.Exists(il2cppBuildCachePath))
  24. {
  25. return;
  26. }
  27. Debug.Log($"clean il2cpp build cache:{il2cppBuildCachePath}");
  28. Directory.Delete(il2cppBuildCachePath, true);
  29. }
  30. private static void GenerateMethodBridgeCppFile(IReadOnlyCollection<GenericMethod> genericMethods, List<RawMonoPInvokeCallbackMethodInfo> reversePInvokeMethods, IReadOnlyCollection<CallNativeMethodSignatureInfo> calliMethodSignatures, string tempFile, string outputFile)
  31. {
  32. string templateCode = File.ReadAllText(tempFile, Encoding.UTF8);
  33. var g = new Generator(new Generator.Options()
  34. {
  35. TemplateCode = templateCode,
  36. OutputFile = outputFile,
  37. GenericMethods = genericMethods,
  38. ReversePInvokeMethods = reversePInvokeMethods,
  39. CalliMethodSignatures = calliMethodSignatures,
  40. Development = EditorUserBuildSettings.development,
  41. });
  42. g.Generate();
  43. Debug.LogFormat("[MethodBridgeGeneratorCommand] output:{0}", outputFile);
  44. }
  45. [MenuItem("HybridCLR/Generate/MethodBridgeAndReversePInvokeWrapper", priority = 101)]
  46. public static void GenerateMethodBridgeAndReversePInvokeWrapper()
  47. {
  48. BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  49. GenerateMethodBridgeAndReversePInvokeWrapper(target);
  50. }
  51. public static void GenerateMethodBridgeAndReversePInvokeWrapper(BuildTarget target)
  52. {
  53. string aotDllDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  54. List<string> aotAssemblyNames = Directory.Exists(aotDllDir) ?
  55. Directory.GetFiles(aotDllDir, "*.dll", SearchOption.TopDirectoryOnly).Select(Path.GetFileNameWithoutExtension).ToList()
  56. : new List<string>();
  57. if (aotAssemblyNames.Count == 0)
  58. {
  59. throw new Exception($"no aot assembly found. please run `HybridCLR/Generate/All` or `HybridCLR/Generate/AotDlls` to generate aot dlls before runing `HybridCLR/Generate/MethodBridge`");
  60. }
  61. AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateAOTAssemblyResolver(target), aotAssemblyNames);
  62. var methodBridgeAnalyzer = new Analyzer(new Analyzer.Options
  63. {
  64. MaxIterationCount = Math.Min(20, SettingsUtil.HybridCLRSettings.maxMethodBridgeGenericIteration),
  65. Collector = collector,
  66. });
  67. methodBridgeAnalyzer.Run();
  68. List<string> hotUpdateDlls = SettingsUtil.HotUpdateAssemblyNamesExcludePreserved;
  69. var cache = new AssemblyCache(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotUpdateDlls));
  70. var reversePInvokeAnalyzer = new MonoPInvokeCallbackAnalyzer(cache, hotUpdateDlls);
  71. reversePInvokeAnalyzer.Run();
  72. var calliAnalyzer = new CalliAnalyzer(cache, hotUpdateDlls);
  73. calliAnalyzer.Run();
  74. var pinvokeAnalyzer = new PInvokeAnalyzer(cache, hotUpdateDlls);
  75. pinvokeAnalyzer.Run();
  76. var callPInvokeMethodSignatures = pinvokeAnalyzer.PInvokeMethodSignatures;
  77. string templateFile = $"{SettingsUtil.TemplatePathInPackage}/MethodBridge.cpp.tpl";
  78. string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.cpp";
  79. var callNativeMethodSignatures = calliAnalyzer.CalliMethodSignatures.Concat(pinvokeAnalyzer.PInvokeMethodSignatures).ToList();
  80. GenerateMethodBridgeCppFile(methodBridgeAnalyzer.GenericMethods, reversePInvokeAnalyzer.ReversePInvokeMethods, callNativeMethodSignatures, templateFile, outputFile);
  81. CleanIl2CppBuildCache();
  82. }
  83. }
  84. }