Skip to content

Move protocol to background thread #1827

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: net203
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
}
4 changes: 2 additions & 2 deletions unity/EditorPlugin/NonUnity/RiderProtocolController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class RiderProtocolController
public readonly SocketWire.Server Wire;
private static readonly ILog ourLogger = Log.GetLog<RiderProtocolController>();

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}");
Expand Down
84 changes: 51 additions & 33 deletions unity/EditorPlugin/PluginEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private static void CreateProtocolAndAdvise(Lifetime lifetime, List<ProtocolInst
{
try
{
var dispatcher = MainThreadDispatcher.Instance;
var dispatcher = SingleThreadScheduler.RunOnSeparateThread(lifetime, "protocol");
var riderProtocolController = new RiderProtocolController(dispatcher, lifetime);
list.Add(new ProtocolInstance(riderProtocolController.Wire.Port, solutionName));

Expand All @@ -294,7 +294,7 @@ private static void CreateProtocolAndAdvise(Lifetime lifetime, List<ProtocolInst
var identities = new Identities(IdKind.Server);

MainThreadDispatcher.AssertThread();
var protocol = new Protocol("UnityEditorPlugin" + solutionName, serializers, identities, MainThreadDispatcher.Instance, riderProtocolController.Wire, lifetime);
var protocol = new Protocol("UnityEditorPlugin" + solutionName, serializers, identities, dispatcher, riderProtocolController.Wire, lifetime);
riderProtocolController.Wire.Connected.WhenTrue(lifetime, connectionLifetime =>
{
ourLogger.Log(LoggingLevel.VERBOSE, "Create UnityModel and advise for new sessions...");
Expand All @@ -303,23 +303,26 @@ private static void CreateProtocolAndAdvise(Lifetime lifetime, List<ProtocolInst
AdviseEditorState(model);
OnModelInitialization(new UnityModelAndLifetime(model, connectionLifetime));
AdviseRefresh(model);
InitEditorLogPath(model);

model.UnityProcessId.SetValue(Process.GetCurrentProcess().Id);
model.UnityApplicationData.SetValue(new UnityApplicationData(
EditorApplication.applicationPath,
EditorApplication.applicationContentsPath, UnityUtils.UnityApplicationVersion));
model.ScriptingRuntime.SetValue(UnityUtils.ScriptingRuntime);

if (UnityUtils.UnityVersion >= 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);
Expand All @@ -335,11 +338,14 @@ private static void CreateProtocolAndAdvise(Lifetime lifetime, List<ProtocolInst

private static void GetBuildLocation(EditorPluginModel model)
{
MainThreadDispatcher.Instance.Queue(() =>
{
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)
Expand Down Expand Up @@ -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<UnityEditorState>();

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;
});
}

Expand All @@ -453,13 +467,16 @@ private static void AdviseRefresh(EditorPluginModel model)
var refreshTask = new RdTask<Unit>();
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");
Expand All @@ -474,7 +491,7 @@ void SendResult()
ourLogger.Verbose("Refresh: RequestScriptReload");
UnityEditorInternal.InternalEditorUtility.RequestScriptReload();
}

ourLogger.Verbose("Refresh: SyncSolution Started");
UnityUtils.SyncSolution();
}
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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;
}
}
Expand Down
13 changes: 13 additions & 0 deletions unity/EditorPlugin/PluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginSettings>();

public static LoggingLevel SelectedLoggingLevel
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down