-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathPluginSettings.cs
397 lines (330 loc) · 14.5 KB
/
PluginSettings.cs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Diagnostics;
using JetBrains.Diagnostics.Internal;
using JetBrains.Lifetimes;
using UnityEditor;
using UnityEngine;
namespace JetBrains.Rider.Unity.Editor
{
public interface IPluginSettings
{
OperatingSystemFamilyRider OperatingSystemFamilyRider { get; }
}
public enum AssemblyReloadSettings
{
RecompileAndContinuePlaying = 0,
RecompileAfterFinishedPlaying = 1,
StopPlayingAndRecompile = 2
}
public class PluginSettings : IPluginSettings
{
static PluginSettings()
{
SystemInfoRiderPlugin.Init();
}
public static readonly string ProductName = PlayerSettings.productName;
public static readonly string CompanyName = PlayerSettings.companyName;
private static readonly ILog ourLogger = Log.GetLog<PluginSettings>();
public static LoggingLevel SelectedLoggingLevel
{
get => (LoggingLevel) EditorPrefs.GetInt("Rider_SelectedLoggingLevel", 0);
set
{
EditorPrefs.SetInt("Rider_SelectedLoggingLevel", (int) value);
InitLog();
}
}
public static void InitLog()
{
if (SelectedLoggingLevel > LoggingLevel.OFF)
Log.DefaultFactory = Log.CreateFileLogFactory(Lifetime.Eternal, PluginEntryPoint.LogPath, true, SelectedLoggingLevel);
else
Log.DefaultFactory = new SingletonLogFactory(NullLog.Instance); // use profiler in Unity - this is faster than leaving TextWriterLogFactory with LoggingLevel OFF
}
public static string[] GetInstalledNetFrameworks()
{
if (SystemInfoRiderPlugin.operatingSystemFamily != OperatingSystemFamilyRider.Windows)
throw new InvalidOperationException("GetTargetFrameworkVersionWindowsMono2 is designed for Windows only");
var programFiles86 = Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ??
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
if (string.IsNullOrEmpty(programFiles86))
programFiles86 = @"C:\Program Files (x86)";
var referenceAssembliesPaths = new[]
{
Path.Combine(programFiles86, @"Reference Assemblies\Microsoft\Framework\.NETFramework"),
Path.Combine(programFiles86, @"Reference Assemblies\Microsoft\Framework") //RIDER-42873
}.Select(s => new DirectoryInfo(s)).Where(a=>a.Exists).ToArray();
if (!referenceAssembliesPaths.Any())
return new string[0];
var availableVersions = referenceAssembliesPaths
.SelectMany(a=>a.GetDirectories("v*"))
.Select(a => a.Name.Substring(1))
.Where(v => InvokeIfValidVersion(v, s => { }))
.Where(v=>new Version(v) >= new Version("3.5"))
.ToArray();
return availableVersions;
}
private static bool InvokeIfValidVersion(string input, Action<string> action)
{
try
{
// ReSharper disable once ObjectCreationAsStatement
new Version(input); // mono 2.6 doesn't support Version.TryParse
action(input);
return true;
}
catch (ArgumentException)
{
} // can't put loggin here because ot fire on every symbol
catch (FormatException)
{
}
return false;
}
public static bool OverrideTargetFrameworkVersion
{
get { return EditorPrefs.GetBool("Rider_OverrideTargetFrameworkVersion", false); }
private set { EditorPrefs.SetBool("Rider_OverrideTargetFrameworkVersion", value);; }
}
public static AssemblyReloadSettings AssemblyReloadSettings
{
get
{
if (UnityUtils.UnityVersion >= new Version(2018, 2))
return AssemblyReloadSettings.RecompileAndContinuePlaying;
return (AssemblyReloadSettings) EditorPrefs.GetInt("Rider_AssemblyReloadSettings", (int) AssemblyReloadSettings.RecompileAndContinuePlaying);
}
set { EditorPrefs.SetInt("Rider_AssemblyReloadSettings", (int) value);; }
}
public static bool UseLatestRiderFromToolbox
{
get { return EditorPrefs.GetBool("UseLatestRiderFromToolbox", true); }
set { EditorPrefs.SetBool("UseLatestRiderFromToolbox", value); }
}
private static string TargetFrameworkVersionDefault = "4.6";
public static string TargetFrameworkVersion
{
get { return EditorPrefs.GetString("Rider_TargetFrameworkVersion", TargetFrameworkVersionDefault); }
private set { InvokeIfValidVersion(value, val => { EditorPrefs.SetString("Rider_TargetFrameworkVersion", val); }); }
}
public static bool OverrideTargetFrameworkVersionOldMono
{
get { return EditorPrefs.GetBool("Rider_OverrideTargetFrameworkVersionOldMono", false); }
private set { EditorPrefs.SetBool("Rider_OverrideTargetFrameworkVersionOldMono", value);; }
}
private static string TargetFrameworkVersionOldMonoDefault = "3.5";
public static string TargetFrameworkVersionOldMono
{
get { return EditorPrefs.GetString("Rider_TargetFrameworkVersionOldMono", TargetFrameworkVersionOldMonoDefault); }
private set { InvokeIfValidVersion(value, val => { EditorPrefs.SetString("Rider_TargetFrameworkVersionOldMono", val); }); }
}
public static bool OverrideLangVersion
{
get { return EditorPrefs.GetBool("Rider_OverrideLangVersion", false); }
private set { EditorPrefs.SetBool("Rider_OverrideLangVersion", value);; }
}
public static string LangVersion
{
get { return EditorPrefs.GetString("Rider_LangVersion", "4"); }
private set { EditorPrefs.SetString("Rider_LangVersion", value); }
}
public static bool RiderInitializedOnce
{
get { return EditorPrefs.GetBool("RiderInitializedOnce", false); }
set { EditorPrefs.SetBool("RiderInitializedOnce", value); }
}
public static bool LogEventsCollectorEnabled
{
get { return EditorPrefs.GetBool("Rider_LogEventsCollectorEnabled", true); }
private set { EditorPrefs.SetBool("Rider_LogEventsCollectorEnabled", value); }
}
/// <summary>
/// Preferences menu layout
/// </summary>
/// <remarks>
/// Contains all 3 toggles: Enable/Disable; Debug On/Off; Writing Launch File On/Off
/// </remarks>
[PreferenceItem("Rider")]
private static void RiderPreferencesItem()
{
EditorGUIUtility.labelWidth = 200f;
EditorGUILayout.BeginVertical();
var alternatives = RiderPathLocator.GetAllFoundInfos(SystemInfoRiderPlugin.operatingSystemFamily);
if (alternatives.Any()) // from known locations
{
var paths = alternatives.Select(a => a.Path).ToList();
var externalEditor = EditorPrefsWrapper.ExternalScriptEditor;
var alts = alternatives.Select(s => s.Presentation).ToList();
if (!paths.Contains(externalEditor))
{
paths.Add(externalEditor);
alts.Add(externalEditor);
}
var index = paths.IndexOf(externalEditor);
var result = paths[EditorGUILayout.Popup("Rider build:", index == -1 ? 0 : index, alts.ToArray())];
EditorPrefsWrapper.ExternalScriptEditor = result;
}
if (PluginEntryPoint.IsRiderDefaultEditor() && !RiderPathProvider.RiderPathExist(EditorPrefsWrapper.ExternalScriptEditor, SystemInfoRiderPlugin.operatingSystemFamily))
{
EditorGUILayout.HelpBox($"Rider is selected as preferred ExternalEditor, but doesn't exist on disk {EditorPrefsWrapper.ExternalScriptEditor}", MessageType.Warning);
}
UseLatestRiderFromToolbox = EditorGUILayout.Toggle(new GUIContent("Update Rider to latest version"), UseLatestRiderFromToolbox);
GUILayout.BeginVertical();
LogEventsCollectorEnabled = EditorGUILayout.Toggle(new GUIContent("Pass Console to Rider:"), LogEventsCollectorEnabled);
if (UnityUtils.ScriptingRuntime > 0)
{
OverrideTargetFrameworkVersion = EditorGUILayout.Toggle(new GUIContent("Override TargetFrameworkVersion:"), OverrideTargetFrameworkVersion);
if (OverrideTargetFrameworkVersion)
{
var help = @"TargetFramework >= 4.6 is recommended.";
TargetFrameworkVersion =
EditorGUILayout.TextField(
new GUIContent("For Active profile NET 4.6",
help), TargetFrameworkVersion);
EditorGUILayout.HelpBox(help, MessageType.None);
}
}
else
{
OverrideTargetFrameworkVersionOldMono = EditorGUILayout.Toggle(new GUIContent("Override TargetFrameworkVersion:"), OverrideTargetFrameworkVersionOldMono);
if (OverrideTargetFrameworkVersionOldMono)
{
var helpOldMono = @"TargetFramework = 3.5 is recommended.
- With 4.5 Rider may show ambiguous references in UniRx.";
TargetFrameworkVersionOldMono =
EditorGUILayout.TextField(
new GUIContent("For Active profile NET 3.5:",
helpOldMono), TargetFrameworkVersionOldMono);
EditorGUILayout.HelpBox(helpOldMono, MessageType.None);
}
}
// Unity 2018.1 doesn't require installed dotnet framework, it references everything from Unity installation
if (SystemInfoRiderPlugin.operatingSystemFamily == OperatingSystemFamilyRider.Windows && UnityUtils.UnityVersion < new Version(2018, 1))
{
var detectedDotnetText = string.Empty;
var installedFrameworks = GetInstalledNetFrameworks();
if (installedFrameworks.Any())
detectedDotnetText = installedFrameworks.OrderBy(v => new Version(v)).Aggregate((a, b) => a+"; "+b);
EditorGUILayout.HelpBox($"Installed dotnet versions: {detectedDotnetText}", MessageType.None);
}
GUILayout.EndVertical();
OverrideLangVersion = EditorGUILayout.Toggle(new GUIContent("Override LangVersion:"), OverrideLangVersion);
if (OverrideLangVersion)
{
var workaroundUrl = "https://gist.github.com/van800/875ce55eaf88d65b105d010d7b38a8d4";
var workaroundText = "Use this <color=#0000FF>workaround</color> if overriding doesn't work.";
var helpLangVersion = @"Avoid overriding, unless there is no particular need.";
LangVersion =
EditorGUILayout.TextField(
new GUIContent("LangVersion:",
helpLangVersion), LangVersion);
LinkButton(caption: workaroundText, url: workaroundUrl);
EditorGUILayout.HelpBox(helpLangVersion, MessageType.None);
}
GUILayout.Label("");
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Log file:");
var previous = GUI.enabled;
GUI.enabled = previous && SelectedLoggingLevel != LoggingLevel.OFF;
var button = GUILayout.Button(new GUIContent("Open log"));
if (button)
{
//UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(PluginEntryPoint.LogPath, 0);
// works much faster than the commented code, when Rider is already started
PluginEntryPoint.OpenAssetHandler.OnOpenedAsset(PluginEntryPoint.LogPath, 0, 0);
}
GUI.enabled = previous;
GUILayout.EndHorizontal();
var loggingMsg =
@"Sets the amount of Rider Debug output. If you are about to report an issue, please select Verbose logging level and attach Unity console output to the issue.";
SelectedLoggingLevel =
(LoggingLevel) EditorGUILayout.EnumPopup(new GUIContent("Logging Level:", loggingMsg),
SelectedLoggingLevel);
EditorGUILayout.HelpBox(loggingMsg, MessageType.None);
if (UnityUtils.UnityVersion < new Version(2018, 2))
{
EditorGUI.BeginChangeCheck();
AssemblyReloadSettings = (AssemblyReloadSettings) EditorGUILayout.EnumPopup("Script Changes during Playing:", AssemblyReloadSettings);
if (EditorGUI.EndChangeCheck())
{
if (AssemblyReloadSettings == AssemblyReloadSettings.RecompileAfterFinishedPlaying && EditorApplication.isPlaying)
{
ourLogger.Info("LockReloadAssemblies");
EditorApplication.LockReloadAssemblies();
}
else
{
ourLogger.Info("UnlockReloadAssemblies");
EditorApplication.UnlockReloadAssemblies();
}
}
}
var githubRepo = "https://github.com/JetBrains/resharper-unity";
var caption = $"<color=#0000FF>{githubRepo}</color>";
LinkButton(caption: caption, url: githubRepo);
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
var version = Assembly.GetExecutingAssembly().GetName().Version;
GUILayout.Label("Plugin version: " + version, new GUIStyle()
{
normal = new GUIStyleState()
{
textColor = new Color(0, 0, 0, .6f),
},
margin = new RectOffset(4, 4, 4, 4),
});
GUILayout.EndHorizontal();
// left for testing purposes
/* if (GUILayout.Button("reset RiderInitializedOnce = false"))
{
RiderInitializedOnce = false;
}*/
EditorGUILayout.EndVertical();
}
private static void LinkButton(string caption, string url)
{
var style = GUI.skin.label;
style.richText = true;
var bClicked = GUILayout.Button(caption, style);
var rect = GUILayoutUtility.GetLastRect();
rect.width = style.CalcSize(new GUIContent(caption)).x;
EditorGUIUtility.AddCursorRect(rect, MouseCursor.Link);
if (bClicked)
Application.OpenURL(url);
}
public OperatingSystemFamilyRider OperatingSystemFamilyRider => SystemInfoRiderPlugin.operatingSystemFamily;
internal static class SystemInfoRiderPlugin
{
public static void Init()
{
}
// This call on Linux is extremely slow, so cache it
private static readonly string ourOperatingSystem = SystemInfo.operatingSystem;
// Do not rename. Explicitly disabled for consistency/compatibility with future Unity API
// ReSharper disable once InconsistentNaming
public static OperatingSystemFamilyRider operatingSystemFamily
{
get
{
if (ourOperatingSystem.StartsWith("Mac", StringComparison.InvariantCultureIgnoreCase))
{
return OperatingSystemFamilyRider.MacOSX;
}
if (ourOperatingSystem.StartsWith("Win", StringComparison.InvariantCultureIgnoreCase))
{
return OperatingSystemFamilyRider.Windows;
}
if (ourOperatingSystem.StartsWith("Lin", StringComparison.InvariantCultureIgnoreCase))
{
return OperatingSystemFamilyRider.Linux;
}
return OperatingSystemFamilyRider.Other;
}
}
}
}
}