123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using Packages.Rider.Editor.Util;
- using Unity.CodeEditor;
- using UnityEditor;
- using UnityEngine;
- using Debug = UnityEngine.Debug;
- namespace Packages.Rider.Editor
- {
- [InitializeOnLoad]
- public class RiderScriptEditor : IExternalCodeEditor
- {
- IDiscovery m_Discoverability;
- IGenerator m_ProjectGeneration;
- RiderInitializer m_Initiliazer = new RiderInitializer();
- static RiderScriptEditor()
- {
- try
- {
- var projectGeneration = new ProjectGeneration();
- var editor = new RiderScriptEditor(new Discovery(), projectGeneration);
- CodeEditor.Register(editor);
-
- var path = GetEditorRealPath(CodeEditor.CurrentEditorInstallation);
- if (IsRiderInstallation(path))
- {
- if (!FileSystemUtil.EditorPathExists(path))
- {
- var newEditor = editor.Installations.Last().Path;
- CodeEditor.SetExternalScriptEditor(newEditor);
- path = newEditor;
- }
-
- editor.CreateSolutionIfDoesntExist();
- if (ShouldLoadEditorPlugin(path))
- {
- editor.m_Initiliazer.Initialize(path);
- }
- InitProjectFilesWatcher();
- }
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- }
- }
- private static void InitProjectFilesWatcher()
- {
- var watcher = new FileSystemWatcher();
- watcher.Path = Directory.GetCurrentDirectory();
- watcher.NotifyFilter = NotifyFilters.LastWrite;
- watcher.Filter = "*.*";
-
- watcher.Changed += OnChanged;
- watcher.Created += OnChanged;
- watcher.EnableRaisingEvents = true;
-
- AppDomain.CurrentDomain.DomainUnload += (EventHandler) ((_, __) =>
- {
- watcher.Dispose();
- });
- }
- private static void OnChanged(object sender, FileSystemEventArgs e)
- {
- var extension = Path.GetExtension(e.FullPath);
- if (extension == ".sln" || extension == ".csproj")
- RiderScriptEditorData.instance.HasChanges = true;
- }
- private static string GetEditorRealPath(string path)
- {
- if (string.IsNullOrEmpty(path))
- {
- return path;
- }
- if (!FileSystemUtil.EditorPathExists(path))
- return path;
- if (SystemInfo.operatingSystemFamily != OperatingSystemFamily.Windows)
- {
- var realPath = FileSystemUtil.GetFinalPathName(path);
-
-
- if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.Linux)
- {
- if (new FileInfo(path).Name.ToLowerInvariant() == "rider" &&
- new FileInfo(realPath).Name.ToLowerInvariant() == "snap")
- {
- var snapInstallPath = "/snap/rider/current/bin/rider.sh";
- if (new FileInfo(snapInstallPath).Exists)
- return snapInstallPath;
- }
- }
-
-
- return realPath;
- }
- return path;
- }
- const string unity_generate_all = "unity_generate_all_csproj";
- public RiderScriptEditor(IDiscovery discovery, IGenerator projectGeneration)
- {
- m_Discoverability = discovery;
- m_ProjectGeneration = projectGeneration;
- }
- private static string[] defaultExtensions
- {
- get
- {
- var customExtensions = new[] {"json", "asmdef", "log"};
- return EditorSettings.projectGenerationBuiltinExtensions.Concat(EditorSettings.projectGenerationUserExtensions)
- .Concat(customExtensions).Distinct().ToArray();
- }
- }
- private static string[] HandledExtensions
- {
- get
- {
- return HandledExtensionsString.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries).Select(s => s.TrimStart('.', '*'))
- .ToArray();
- }
- }
- private static string HandledExtensionsString
- {
- get { return EditorPrefs.GetString("Rider_UserExtensions", string.Join(";", defaultExtensions));}
- set { EditorPrefs.SetString("Rider_UserExtensions", value); }
- }
-
- private static bool SupportsExtension(string path)
- {
- var extension = Path.GetExtension(path);
- if (string.IsNullOrEmpty(extension))
- return false;
- return HandledExtensions.Contains(extension.TrimStart('.'));
- }
- public void OnGUI()
- {
- var prevGenerate = EditorPrefs.GetBool(unity_generate_all, false);
- var generateAll = EditorGUILayout.Toggle("Generate all .csproj files.", prevGenerate);
- if (generateAll != prevGenerate)
- {
- EditorPrefs.SetBool(unity_generate_all, generateAll);
- }
-
- m_ProjectGeneration.GenerateAll(generateAll);
- if (ShouldLoadEditorPlugin(CurrentEditor))
- {
- HandledExtensionsString = EditorGUILayout.TextField(new GUIContent("Extensions handled: "), HandledExtensionsString);
- }
- }
- public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles,
- string[] importedFiles)
- {
- m_ProjectGeneration.SyncIfNeeded(addedFiles.Union(deletedFiles).Union(movedFiles).Union(movedFromFiles),
- importedFiles);
- }
- public void SyncAll()
- {
- AssetDatabase.Refresh();
- if (RiderScriptEditorData.instance.HasChanges)
- {
- m_ProjectGeneration.Sync();
- RiderScriptEditorData.instance.HasChanges = false;
- }
- }
- public void Initialize(string editorInstallationPath)
- {
- m_ProjectGeneration.Sync();
- }
- public bool OpenProject(string path, int line, int column)
- {
- if (path != "" && !SupportsExtension(path))
- {
- return false;
- }
-
- if (path == "" && SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX)
- {
-
- return OpenOSXApp(path, line, column);
- }
- if (!IsUnityScript(path))
- {
- var fastOpenResult = EditorPluginInterop.OpenFileDllImplementation(path, line, column);
- if (fastOpenResult)
- return true;
- }
- if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX)
- {
- return OpenOSXApp(path, line, column);
- }
- var solution = GetSolutionFile(path);
- solution = solution == "" ? "" : $"\"{solution}\"";
- var process = new Process
- {
- StartInfo = new ProcessStartInfo
- {
- FileName = CodeEditor.CurrentEditorInstallation,
- Arguments = $"{solution} -l {line} \"{path}\"",
- UseShellExecute = true,
- }
- };
- process.Start();
- return true;
- }
- private bool OpenOSXApp(string path, int line, int column)
- {
- var solution = GetSolutionFile(path);
- solution = solution == "" ? "" : $"\"{solution}\"";
- var pathArguments = path == "" ? "" : $"-l {line} \"{path}\"";
- var process = new Process
- {
- StartInfo = new ProcessStartInfo
- {
- FileName = "open",
- Arguments = $"-n \"{CodeEditor.CurrentEditorInstallation}\" --args {solution} {pathArguments}",
- CreateNoWindow = true,
- UseShellExecute = true,
- }
- };
- process.Start();
- return true;
- }
- private string GetSolutionFile(string path)
- {
- if (IsUnityScript(path))
- {
- return Path.Combine(GetBaseUnityDeveloperFolder(), "Projects/CSharp/Unity.CSharpProjects.gen.sln");
- }
- var solutionFile = m_ProjectGeneration.SolutionFile();
- if (File.Exists(solutionFile))
- {
- return solutionFile;
- }
- return "";
- }
- static bool IsUnityScript(string path)
- {
- if (UnityEditor.Unsupported.IsDeveloperBuild())
- {
- var baseFolder = GetBaseUnityDeveloperFolder().Replace("\\", "/");
- var lowerPath = path.ToLowerInvariant().Replace("\\", "/");
- if (lowerPath.Contains((baseFolder + "/Runtime").ToLowerInvariant())
- || lowerPath.Contains((baseFolder + "/Editor").ToLowerInvariant()))
- {
- return true;
- }
- }
- return false;
- }
- static string GetBaseUnityDeveloperFolder()
- {
- return Directory.GetParent(EditorApplication.applicationPath).Parent.Parent.FullName;
- }
- public bool TryGetInstallationForPath(string editorPath, out CodeEditor.Installation installation)
- {
- if (FileSystemUtil.EditorPathExists(editorPath) && IsRiderInstallation(editorPath))
- {
- var info = new RiderPathLocator.RiderInfo(editorPath, false);
- installation = new CodeEditor.Installation
- {
- Name = info.Presentation,
- Path = info.Path
- };
- return true;
- }
- installation = default;
- return false;
- }
- public static bool IsRiderInstallation(string path)
- {
- if (string.IsNullOrEmpty(path))
- {
- return false;
- }
- var fileInfo = new FileInfo(path);
- var filename = fileInfo.Name.ToLowerInvariant();
- return filename.StartsWith("rider", StringComparison.Ordinal);
- }
- public static string CurrentEditor
- => EditorPrefs.GetString("kScriptsDefaultApp");
- public static bool ShouldLoadEditorPlugin(string path)
- {
- var ver = RiderPathLocator.GetBuildNumber(path);
- if (!Version.TryParse(ver, out var version))
- return false;
- return version >= new Version("191.7141.156");
- }
- public CodeEditor.Installation[] Installations => m_Discoverability.PathCallback();
- public void CreateSolutionIfDoesntExist()
- {
- if (!m_ProjectGeneration.HasSolutionBeenGenerated())
- {
- m_ProjectGeneration.Sync();
- }
- }
- }
- }
|