EditorPluginInterop.cs 4.3 KB

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