EditorPluginInterop.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using Debug = UnityEngine.Debug;
  7. namespace Packages.Rider.Editor
  8. {
  9. public static class EditorPluginInterop
  10. {
  11. private static string ourEntryPointTypeName = "JetBrains.Rider.Unity.Editor.PluginEntryPoint";
  12. private static Assembly ourEditorPluginAssembly;
  13. public static Assembly EditorPluginAssembly
  14. {
  15. get
  16. {
  17. if (ourEditorPluginAssembly != null)
  18. return ourEditorPluginAssembly;
  19. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  20. ourEditorPluginAssembly = assemblies.FirstOrDefault(a => a.GetName().Name.Equals("JetBrains.Rider.Unity.Editor.Plugin.Full.Repacked"));
  21. return ourEditorPluginAssembly;
  22. }
  23. }
  24. private static void DisableSyncSolutionOnceCallBack()
  25. {
  26. // RiderScriptableSingleton.Instance.CsprojProcessedOnce = true;
  27. // Otherwise EditorPlugin regenerates all on every AppDomain reload
  28. var assembly = EditorPluginAssembly;
  29. if (assembly == null) return;
  30. var type = assembly.GetType("JetBrains.Rider.Unity.Editor.Utils.RiderScriptableSingleton");
  31. if (type == null) return;
  32. var baseType = type.BaseType;
  33. if (baseType == null) return;
  34. var instance = baseType.GetProperty("Instance");
  35. if (instance == null) return;
  36. var instanceVal = instance.GetValue(null);
  37. var member = type.GetProperty("CsprojProcessedOnce");
  38. if (member==null) return;
  39. member.SetValue(instanceVal, true);
  40. }
  41. public static string LogPath
  42. {
  43. get
  44. {
  45. try
  46. {
  47. var assembly = EditorPluginAssembly;
  48. if (assembly == null) return null;
  49. var type = assembly.GetType(ourEntryPointTypeName);
  50. if (type == null) return null;
  51. var field = type.GetField("LogPath", BindingFlags.NonPublic | BindingFlags.Static);
  52. if (field == null) return null;
  53. return field.GetValue(null) as string;
  54. }
  55. catch (Exception)
  56. {
  57. Debug.Log("Unable to do OpenFile to Rider from dll, fallback to com.unity.ide.rider implementation.");
  58. }
  59. return null;
  60. }
  61. }
  62. public static bool OpenFileDllImplementation(string path, int line, int column)
  63. {
  64. var openResult = false;
  65. // reflection for fast OpenFileLineCol, when Rider is started and protocol connection is established
  66. try
  67. {
  68. var assembly = EditorPluginAssembly;
  69. if (assembly == null) return false;
  70. var type = assembly.GetType(ourEntryPointTypeName);
  71. if (type == null) return false;
  72. var field = type.GetField("OpenAssetHandler", BindingFlags.NonPublic | BindingFlags.Static);
  73. if (field == null) return false;
  74. var handlerInstance = field.GetValue(null);
  75. var method = handlerInstance.GetType()
  76. .GetMethod("OnOpenedAsset", new[] {typeof(string), typeof(int), typeof(int)});
  77. if (method == null) return false;
  78. var assetFilePath = path;
  79. if (!string.IsNullOrEmpty(path))
  80. assetFilePath = Path.GetFullPath(path);
  81. openResult = (bool) method.Invoke(handlerInstance, new object[] {assetFilePath, line, column});
  82. }
  83. catch (Exception e)
  84. {
  85. Debug.Log("Unable to do OpenFile to Rider from dll, fallback to com.unity.ide.rider implementation.");
  86. Debug.LogException(e);
  87. }
  88. return openResult;
  89. }
  90. public static bool EditorPluginIsLoadedFromAssets(Assembly assembly)
  91. {
  92. if (assembly == null)
  93. return false;
  94. var location = assembly.Location;
  95. var currentDir = Directory.GetCurrentDirectory();
  96. return location.StartsWith(currentDir, StringComparison.InvariantCultureIgnoreCase);
  97. }
  98. internal static void InitEntryPoint(Assembly assembly)
  99. {
  100. try
  101. {
  102. if (Version.TryParse(RiderScriptEditorData.instance.currentEditorVersion, out var version))
  103. {
  104. if (version.Major < 192)
  105. DisableSyncSolutionOnceCallBack(); // is require for Rider prior to 2019.2
  106. }
  107. else
  108. DisableSyncSolutionOnceCallBack();
  109. var type = assembly.GetType("JetBrains.Rider.Unity.Editor.AfterUnity56.EntryPoint");
  110. if (type == null)
  111. type = assembly.GetType("JetBrains.Rider.Unity.Editor.UnitTesting.EntryPoint"); // oldRider
  112. RuntimeHelpers.RunClassConstructor(type.TypeHandle);
  113. }
  114. catch (TypeInitializationException ex)
  115. {
  116. Debug.LogException(ex);
  117. if (ex.InnerException != null)
  118. Debug.LogException(ex.InnerException);
  119. }
  120. }
  121. }
  122. }