From de879ffc1b60122ba349b5ecd113ed64b4ee07f7 Mon Sep 17 00:00:00 2001 From: Ivan Shakhov Date: Mon, 31 Aug 2020 14:14:33 +0200 Subject: [PATCH] move protocol to background thread --- .../UnitTesting/CompiledAssembliesTracker.cs | 23 ++--- .../NonUnity/RiderProtocolController.cs | 4 +- unity/EditorPlugin/PluginEntryPoint.cs | 84 +++++++++++-------- unity/EditorPlugin/PluginSettings.cs | 13 +++ 4 files changed, 79 insertions(+), 45 deletions(-) diff --git a/unity/EditorPlugin/AfterUnity56/UnitTesting/CompiledAssembliesTracker.cs b/unity/EditorPlugin/AfterUnity56/UnitTesting/CompiledAssembliesTracker.cs index 85caa9030e..b3e5311a46 100644 --- a/unity/EditorPlugin/AfterUnity56/UnitTesting/CompiledAssembliesTracker.cs +++ b/unity/EditorPlugin/AfterUnity56/UnitTesting/CompiledAssembliesTracker.cs @@ -31,19 +31,22 @@ void OnCompilationFinished(string assemblyPath, CompilerMessage[] messages) private static void UpdateAssemblies() { - ourCompiledAssemblyPaths.Clear(); - - var projectPath = Directory.GetParent(Application.dataPath).FullName; - var compiledAssemblies = CompilationPipeline.GetAssemblies().Select(a => + MainThreadDispatcher.Instance.Queue(() => { - ourCompiledAssemblyPaths.Add(a.outputPath); + ourCompiledAssemblyPaths.Clear(); + + var projectPath = Directory.GetParent(Application.dataPath).FullName; + var compiledAssemblies = CompilationPipeline.GetAssemblies().Select(a => + { + ourCompiledAssemblyPaths.Add(a.outputPath); - var fullOutputPath = Path.Combine(projectPath, a.outputPath); - return new CompiledAssembly(a.name, fullOutputPath); - }) - .ToList(); + var fullOutputPath = Path.Combine(projectPath, a.outputPath); + return new CompiledAssembly(a.name, fullOutputPath); + }) + .ToList(); - ourModel.CompiledAssemblies(compiledAssemblies); + ourModel.CompiledAssemblies(compiledAssemblies); + }); } } } \ No newline at end of file diff --git a/unity/EditorPlugin/NonUnity/RiderProtocolController.cs b/unity/EditorPlugin/NonUnity/RiderProtocolController.cs index b211a45ea6..54000586d8 100644 --- a/unity/EditorPlugin/NonUnity/RiderProtocolController.cs +++ b/unity/EditorPlugin/NonUnity/RiderProtocolController.cs @@ -15,13 +15,13 @@ public class RiderProtocolController public readonly SocketWire.Server Wire; private static readonly ILog ourLogger = Log.GetLog(); - public RiderProtocolController(IScheduler mainThreadScheduler, Lifetime lifetime) + public RiderProtocolController(IScheduler scheduler, Lifetime lifetime) { try { ourLogger.Verbose("Start ControllerTask..."); - Wire = new SocketWire.Server(lifetime, mainThreadScheduler, null, "UnityServer"); + Wire = new SocketWire.Server(lifetime, scheduler, null, "UnityServer"); Wire.BackwardsCompatibleWireFormat = true; ourLogger.Verbose($"Created SocketWire with port = {Wire.Port}"); diff --git a/unity/EditorPlugin/PluginEntryPoint.cs b/unity/EditorPlugin/PluginEntryPoint.cs index 849a53f923..5af14bf2dc 100644 --- a/unity/EditorPlugin/PluginEntryPoint.cs +++ b/unity/EditorPlugin/PluginEntryPoint.cs @@ -282,7 +282,7 @@ private static void CreateProtocolAndAdvise(Lifetime lifetime, List { ourLogger.Log(LoggingLevel.VERBOSE, "Create UnityModel and advise for new sessions..."); @@ -303,7 +303,6 @@ private static void CreateProtocolAndAdvise(Lifetime lifetime, List= new Version(2018, 2)) - model.ScriptCompilationDuringPlay.Set(EditorPrefsWrapper.ScriptChangesDuringPlayOptions); - else - model.ScriptCompilationDuringPlay.Set((int)PluginSettings.AssemblyReloadSettings); + MainThreadDispatcher.Instance.Queue(() => + { + if (UnityUtils.UnityVersion >= new Version(2018, 2)) + model.ScriptCompilationDuringPlay.Set(EditorPrefsWrapper.ScriptChangesDuringPlayOptions); + else + model.ScriptCompilationDuringPlay.Set((int)PluginSettings.AssemblyReloadSettings); + }); AdviseShowPreferences(model, connectionLifetime, ourLogger); AdviseGenerateUISchema(model); AdviseExitUnity(model); GetBuildLocation(model); + InitEditorLogPath(model); ourLogger.Verbose("UnityModel initialized."); var pair = new ModelWithLifetime(model, connectionLifetime); @@ -335,11 +338,14 @@ private static void CreateProtocolAndAdvise(Lifetime lifetime, List + { var path = EditorUserBuildSettings.GetBuildLocation(EditorUserBuildSettings.selectedStandaloneTarget); if (PluginSettings.SystemInfoRiderPlugin.operatingSystemFamily == OperatingSystemFamilyRider.MacOSX) - path = Path.Combine(Path.Combine(Path.Combine(path, "Contents"), "MacOS"), PlayerSettings.productName); + path = Path.Combine(Path.Combine(Path.Combine(path, "Contents"), "MacOS"), PluginSettings.ProductName); if (!string.IsNullOrEmpty(path) && File.Exists(path)) - model.BuildLocation.Value = path; + model.BuildLocation.Value = path; + }); } private static void AdviseGenerateUISchema(EditorPluginModel model) @@ -425,24 +431,32 @@ private static void AdviseShowPreferences(EditorPluginModel model, Lifetime conn private static void AdviseEditorState(EditorPluginModel modelValue) { - modelValue.GetUnityEditorState.Set(rdVoid => + + modelValue.GetUnityEditorState.Set((l, unit) => { - if (EditorApplication.isPaused) - { - return UnityEditorState.Pause; - } + var refreshTask = new RdTask(); - if (EditorApplication.isPlaying) + MainThreadDispatcher.Instance.Queue(() => { - return UnityEditorState.Play; - } + if (EditorApplication.isPaused) + { + refreshTask.Set(UnityEditorState.Pause); + } - if (EditorApplication.isCompiling || EditorApplication.isUpdating) - { - return UnityEditorState.Refresh; - } + if (EditorApplication.isPlaying) + { + refreshTask.Set(UnityEditorState.Play); + } + + if (EditorApplication.isCompiling || EditorApplication.isUpdating) + { + refreshTask.Set(UnityEditorState.Refresh); + } - return UnityEditorState.Idle; + refreshTask.Set(UnityEditorState.Idle); + }); + + return refreshTask; }); } @@ -453,13 +467,16 @@ private static void AdviseRefresh(EditorPluginModel model) var refreshTask = new RdTask(); void SendResult() { - if (!EditorApplication.isCompiling) + MainThreadDispatcher.Instance.Queue(() => { - // ReSharper disable once DelegateSubtraction - EditorApplication.update -= SendResult; - ourLogger.Verbose("Refresh: SyncSolution Completed"); - refreshTask.Set(Unit.Instance); - } + if (!EditorApplication.isCompiling) + { + // ReSharper disable once DelegateSubtraction + EditorApplication.update -= SendResult; + ourLogger.Verbose("Refresh: SyncSolution Completed"); + refreshTask.Set(Unit.Instance); + } + }); } ourLogger.Verbose("Refresh: SyncSolution Enqueue"); @@ -474,7 +491,7 @@ void SendResult() ourLogger.Verbose("Refresh: RequestScriptReload"); UnityEditorInternal.InternalEditorUtility.RequestScriptReload(); } - + ourLogger.Verbose("Refresh: SyncSolution Started"); UnityUtils.SyncSolution(); } @@ -585,9 +602,8 @@ private static void InitEditorLogPath(EditorPluginModel editorPluginModel) editorLogpath = Path.Combine(localAppData, @"Unity\Editor\Editor.log"); var userProfile = Environment.GetEnvironmentVariable("USERPROFILE"); if (!string.IsNullOrEmpty(userProfile)) - playerLogPath = Path.Combine( - Path.Combine(Path.Combine(Path.Combine(userProfile, @"AppData\LocalLow"), PlayerSettings.companyName), - PlayerSettings.productName),"output_log.txt"); + playerLogPath = Path.Combine(Path.Combine(Path.Combine(Path.Combine(userProfile, @"AppData\LocalLow"), PluginSettings.CompanyName), + PluginSettings.ProductName), "output_log.txt"); break; } case OperatingSystemFamilyRider.MacOSX: @@ -598,6 +614,7 @@ private static void InitEditorLogPath(EditorPluginModel editorPluginModel) editorLogpath = Path.Combine(home, "Library/Logs/Unity/Editor.log"); playerLogPath = Path.Combine(home, "Library/Logs/Unity/Player.log"); } + break; } case OperatingSystemFamilyRider.Linux: @@ -606,8 +623,9 @@ private static void InitEditorLogPath(EditorPluginModel editorPluginModel) if (!string.IsNullOrEmpty(home)) { editorLogpath = Path.Combine(home, ".config/unity3d/Editor.log"); - playerLogPath = Path.Combine(home, $".config/unity3d/{PlayerSettings.companyName}/{PlayerSettings.productName}/Player.log"); + playerLogPath = Path.Combine(home, $".config/unity3d/{PluginSettings.CompanyName}/{PluginSettings.ProductName}/Player.log"); } + break; } } diff --git a/unity/EditorPlugin/PluginSettings.cs b/unity/EditorPlugin/PluginSettings.cs index de2b6b15b3..50bd898498 100644 --- a/unity/EditorPlugin/PluginSettings.cs +++ b/unity/EditorPlugin/PluginSettings.cs @@ -24,6 +24,14 @@ public enum AssemblyReloadSettings 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(); public static LoggingLevel SelectedLoggingLevel @@ -38,6 +46,7 @@ public static LoggingLevel SelectedLoggingLevel public static void InitLog() { + if (SelectedLoggingLevel > LoggingLevel.OFF) Log.DefaultFactory = Log.CreateFileLogFactory(Lifetime.Eternal, PluginEntryPoint.LogPath, true, SelectedLoggingLevel); else @@ -352,6 +361,10 @@ private static void LinkButton(string caption, string url) 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;