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