EditorPluginInterop.cs 5.1 KB

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