MethodBridgeGeneratorCommand.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using HybridCLR.Editor;
  2. using HybridCLR.Editor.ABI;
  3. using HybridCLR.Editor.Meta;
  4. using HybridCLR.Editor.MethodBridge;
  5. using HybridCLR.Editor.ReversePInvokeWrap;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using UnityEditor;
  14. using UnityEditor.Build;
  15. using UnityEngine;
  16. namespace HybridCLR.Editor.Commands
  17. {
  18. using Analyzer = HybridCLR.Editor.MethodBridge.Analyzer;
  19. public class MethodBridgeGeneratorCommand
  20. {
  21. public static void CleanIl2CppBuildCache()
  22. {
  23. string il2cppBuildCachePath = SettingsUtil.Il2CppBuildCacheDir;
  24. if (!Directory.Exists(il2cppBuildCachePath))
  25. {
  26. return;
  27. }
  28. Debug.Log($"clean il2cpp build cache:{il2cppBuildCachePath}");
  29. Directory.Delete(il2cppBuildCachePath, true);
  30. }
  31. private static void GenerateMethodBridgeCppFile(IReadOnlyCollection<GenericMethod> genericMethods, List<RawReversePInvokeMethodInfo> reversePInvokeMethods, string outputFile)
  32. {
  33. string templateCode = File.ReadAllText(outputFile, Encoding.UTF8);
  34. var g = new Generator(new Generator.Options()
  35. {
  36. TemplateCode = templateCode,
  37. OutputFile = outputFile,
  38. GenericMethods = genericMethods,
  39. ReversePInvokeMethods = reversePInvokeMethods,
  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 ReversePInvokeWrap.Analyzer(cache, hotUpdateDlls);
  71. reversePInvokeAnalyzer.Run();
  72. string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.cpp";
  73. GenerateMethodBridgeCppFile(methodBridgeAnalyzer.GenericMethods, reversePInvokeAnalyzer.ReversePInvokeMethods, outputFile);
  74. CleanIl2CppBuildCache();
  75. }
  76. }
  77. }