説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

EditorPluginInterop.cs 5.1KB

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