ReversePInvokeWrapperGeneratorCommand.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using HybridCLR.Editor.ABI;
  2. using HybridCLR.Editor.Link;
  3. using HybridCLR.Editor.Meta;
  4. using HybridCLR.Editor.ReversePInvokeWrap;
  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 UnityEngine;
  14. namespace HybridCLR.Editor.Commands
  15. {
  16. public static class ReversePInvokeWrapperGeneratorCommand
  17. {
  18. [MenuItem("HybridCLR/Generate/ReversePInvokeWrapper", priority = 103)]
  19. public static void CompileAndGenerateReversePInvokeWrapper()
  20. {
  21. BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  22. CompileDllCommand.CompileDll(target);
  23. GenerateReversePInvokeWrapper(target);
  24. }
  25. public static void GenerateReversePInvokeWrapper(BuildTarget target)
  26. {
  27. List<string> hotUpdateDlls = SettingsUtil.HotUpdateAssemblyNames;
  28. using (var cache = new AssemblyCache(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotUpdateDlls)))
  29. {
  30. var analyzer = new ReversePInvokeWrap.Analyzer(cache, hotUpdateDlls);
  31. analyzer.Run();
  32. string templateCode = File.ReadAllText($"{SettingsUtil.TemplatePathInPackage}/ReversePInvokeMethodStub.cpp");
  33. foreach (PlatformABI abi in Enum.GetValues(typeof(PlatformABI)))
  34. {
  35. string outputFile = $"{SettingsUtil.GeneratedCppDir}/ReversePInvokeMethodStub_{abi}.cpp";
  36. List<ABIReversePInvokeMethodInfo> methods = analyzer.BuildABIMethods(abi);
  37. Debug.Log($"GenerateReversePInvokeWrapper. abi:{abi} wraperCount:{methods.Sum(m => m.Count)} output:{outputFile}");
  38. var generator = new Generator();
  39. generator.Generate(templateCode, abi, methods, outputFile);
  40. }
  41. }
  42. MethodBridgeGeneratorCommand.CleanIl2CppBuildCache();
  43. }
  44. }
  45. }