Skip to content
Snippets Groups Projects
Commit 71efe192 authored by Andreia Gaita's avatar Andreia Gaita
Browse files

Move ApplicationManager to its own file

parent 2a767d8e
No related branches found
No related tags found
No related merge requests found
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.Events;
namespace GitHub.Unity
{
class ApplicationManager : IApplicationManager
{
private static readonly ILogging logger = Logging.GetLogger<ApplicationManager>();
private const string QuitActionFieldName = "editorApplicationQuit";
private CancellationTokenSource cancellationTokenSource;
private FieldInfo quitActionField;
private const BindingFlags quitActionBindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
private Tasks taskRunner;
public ApplicationManager(MainThreadSynchronizationContext syncCtx)
{
ThreadUtils.SetMainThread();
SynchronizationContext.SetSynchronizationContext(syncCtx);
cancellationTokenSource = new CancellationTokenSource();
EditorApplicationQuit = (UnityAction)Delegate.Combine(EditorApplicationQuit, new UnityAction(OnShutdown));
EditorApplication.playmodeStateChanged += () =>
{
if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
{
OnShutdown();
}
};
Platform = new Platform(Environment, FileSystem);
GitObjectFactory = new GitObjectFactory(Environment, GitEnvironment);
ProcessManager = new ProcessManager(Environment, GitEnvironment, CancellationToken);
Platform.Initialize(ProcessManager);
CredentialManager = Platform.CredentialManager;
TaskResultDispatcher = new TaskResultDispatcher();
ApiClientFactory.Instance = new ApiClientFactory(new AppConfiguration(), CredentialManager);
LocalSettings = new LocalSettings(Environment);
UserSettings = new UserSettings(Environment, ApplicationInfo.ApplicationName);
SystemSettings = new SystemSettings(Environment, ApplicationInfo.ApplicationName);
taskRunner = new Tasks(syncCtx, cancellationTokenSource.Token);
}
private void InitializeEnvironment()
{
NPathFileSystemProvider.Current = new FileSystem();
Environment = new DefaultEnvironment();
// figure out where we are
Environment.ExtensionInstallPath = DetermineInstallationPath();
// figure out where the project is
var assetsPath = Application.dataPath.ToNPath();
var projectPath = assetsPath.Parent;
Environment.UnityAssetsPath = assetsPath.ToString(SlashMode.Forward);
Environment.UnityProjectPath = projectPath.ToString(SlashMode.Forward);
// figure out where the repository root is
GitClient = new GitClient(projectPath);
Environment.Repository = GitClient.GetRepository();
// Make sure CurrentDirectory always returns the repository root, so all
// file system path calculations use it as a base
FileSystem = new FileSystem(Environment.Repository.LocalPath);
NPathFileSystemProvider.Current = FileSystem;
}
// for unit testing
public ApplicationManager(IEnvironment environment, IFileSystem fileSystem,
IPlatform platform, IProcessManager processManager, ITaskResultDispatcher taskResultDispatcher)
{
Environment = environment;
FileSystem = fileSystem;
NPathFileSystemProvider.Current = FileSystem;
Platform = platform;
ProcessManager = processManager;
TaskResultDispatcher = taskResultDispatcher;
}
public void Run()
{
Utility.Initialize();
DetermineInstallationPath();
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(() =>
{
try
{
Environment.GitExecutablePath = DetermineGitInstallationPath();
Environment.Repository = GitClient.GetRepository();
}
catch (Exception ex)
{
logger.Error(ex);
throw;
}
})
.ContinueWith(_ =>
{
taskRunner.Run();
Utility.Run();
ProjectWindowInterface.Initialize();
Window.Initialize();
}, scheduler);
}
private void OnShutdown()
{
taskRunner.Shutdown();
cancellationTokenSource.Cancel();
}
private UnityAction EditorApplicationQuit
{
get
{
SecureQuitActionField();
return (UnityAction)quitActionField.GetValue(null);
}
set
{
SecureQuitActionField();
quitActionField.SetValue(null, value);
}
}
private void SecureQuitActionField()
{
if (quitActionField == null)
{
quitActionField = typeof(EditorApplication).GetField(QuitActionFieldName, quitActionBindingFlags);
if (quitActionField == null)
{
throw new InvalidOperationException("Unable to reflect EditorApplication." + QuitActionFieldName);
}
}
}
private string DetermineInstallationPath()
{
// Juggling to find out where we got installed
var shim = ScriptableObject.CreateInstance<RunLocationShim>();
var script = MonoScript.FromScriptableObject(shim);
string ret = String.Empty;
if (script != null)
{
var scriptPath = AssetDatabase.GetAssetPath(script).ToNPath();
ret = scriptPath.Parent.ToString(SlashMode.Forward);
}
ScriptableObject.DestroyImmediate(shim);
return ret;
}
private string DetermineGitInstallationPath()
{
var cachedGitInstallPath = SystemSettings.Get("GitInstallPath");
// Root paths
if (string.IsNullOrEmpty(cachedGitInstallPath) || !cachedGitInstallPath.ToNPath().Exists())
{
return GitEnvironment.FindGitInstallationPath(ProcessManager).Result;
}
else
{
return cachedGitInstallPath;
}
}
public CancellationToken CancellationToken { get { return cancellationTokenSource.Token; } }
private IEnvironment environment;
public IEnvironment Environment
{
get
{
// if this is called while still null, it's because Unity wants
// to render something and we need to load icons, and that runs
// before EntryPoint. Do an early initialization
if (environment == null)
InitializeEnvironment();
return environment;
}
set { environment = value; }
}
public IFileSystem FileSystem { get; private set; }
public IPlatform Platform { get; private set; }
public IGitEnvironment GitEnvironment { get { return Platform.GitEnvironment; } }
public IProcessManager ProcessManager { get; private set; }
public ICredentialManager CredentialManager { get; private set; }
public IGitClient GitClient { get; private set; }
public ITaskResultDispatcher TaskResultDispatcher { get; private set; }
public ISettings SystemSettings { get; private set; }
public ISettings LocalSettings { get; private set; }
public ISettings UserSettings { get; private set; }
public GitObjectFactory GitObjectFactory { get; private set; }
}
}
\ No newline at end of file
using GitHub.Unity;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.Events;
namespace GitHub.Unity
{
......@@ -116,208 +111,4 @@ namespace GitHub.Unity
public static bool Initialized { get; private set; }
}
class ApplicationManager : IApplicationManager
{
private static readonly ILogging logger = Logging.GetLogger<ApplicationManager>();
private const string QuitActionFieldName = "editorApplicationQuit";
private CancellationTokenSource cancellationTokenSource;
private FieldInfo quitActionField;
private const BindingFlags quitActionBindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
private Tasks taskRunner;
public ApplicationManager(MainThreadSynchronizationContext syncCtx)
{
ThreadUtils.SetMainThread();
SynchronizationContext.SetSynchronizationContext(syncCtx);
cancellationTokenSource = new CancellationTokenSource();
EditorApplicationQuit = (UnityAction)Delegate.Combine(EditorApplicationQuit, new UnityAction(OnShutdown));
EditorApplication.playmodeStateChanged += () =>
{
if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
{
OnShutdown();
}
};
Platform = new Platform(Environment, FileSystem);
GitObjectFactory = new GitObjectFactory(Environment, GitEnvironment);
ProcessManager = new ProcessManager(Environment, GitEnvironment, CancellationToken);
Platform.Initialize(ProcessManager);
CredentialManager = Platform.CredentialManager;
TaskResultDispatcher = new TaskResultDispatcher();
ApiClientFactory.Instance = new ApiClientFactory(new AppConfiguration(), CredentialManager);
LocalSettings = new LocalSettings(Environment);
UserSettings = new UserSettings(Environment, ApplicationInfo.ApplicationName);
SystemSettings = new SystemSettings(Environment, ApplicationInfo.ApplicationName);
taskRunner = new Tasks(syncCtx, cancellationTokenSource.Token);
}
private void InitializeEnvironment()
{
NPathFileSystemProvider.Current = new FileSystem();
Environment = new DefaultEnvironment();
// figure out where we are
Environment.ExtensionInstallPath = DetermineInstallationPath();
// figure out where the project is
var assetsPath = Application.dataPath.ToNPath();
var projectPath = assetsPath.Parent;
Environment.UnityAssetsPath = assetsPath.ToString(SlashMode.Forward);
Environment.UnityProjectPath = projectPath.ToString(SlashMode.Forward);
// figure out where the repository root is
GitClient = new GitClient(projectPath);
Environment.Repository = GitClient.GetRepository();
// Make sure CurrentDirectory always returns the repository root, so all
// file system path calculations use it as a base
FileSystem = new FileSystem(Environment.Repository.LocalPath);
NPathFileSystemProvider.Current = FileSystem;
}
// for unit testing
public ApplicationManager(IEnvironment environment, IFileSystem fileSystem,
IPlatform platform, IProcessManager processManager, ITaskResultDispatcher taskResultDispatcher)
{
Environment = environment;
FileSystem = fileSystem;
NPathFileSystemProvider.Current = FileSystem;
Platform = platform;
ProcessManager = processManager;
TaskResultDispatcher = taskResultDispatcher;
}
public void Run()
{
Utility.Initialize();
DetermineInstallationPath();
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(() =>
{
try
{
Environment.GitExecutablePath = DetermineGitInstallationPath();
Environment.Repository = GitClient.GetRepository();
}
catch (Exception ex)
{
logger.Error(ex);
throw;
}
})
.ContinueWith(_ =>
{
taskRunner.Run();
Utility.Run();
ProjectWindowInterface.Initialize();
Window.Initialize();
}, scheduler);
}
private void OnShutdown()
{
taskRunner.Shutdown();
cancellationTokenSource.Cancel();
}
private UnityAction EditorApplicationQuit
{
get
{
SecureQuitActionField();
return (UnityAction)quitActionField.GetValue(null);
}
set
{
SecureQuitActionField();
quitActionField.SetValue(null, value);
}
}
private void SecureQuitActionField()
{
if (quitActionField == null)
{
quitActionField = typeof(EditorApplication).GetField(QuitActionFieldName, quitActionBindingFlags);
if (quitActionField == null)
{
throw new InvalidOperationException("Unable to reflect EditorApplication." + QuitActionFieldName);
}
}
}
private string DetermineInstallationPath()
{
// Juggling to find out where we got installed
var shim = ScriptableObject.CreateInstance<RunLocationShim>();
var script = MonoScript.FromScriptableObject(shim);
string ret = String.Empty;
if (script != null)
{
var scriptPath = AssetDatabase.GetAssetPath(script).ToNPath();
ret = scriptPath.Parent.ToString(SlashMode.Forward);
}
ScriptableObject.DestroyImmediate(shim);
return ret;
}
private string DetermineGitInstallationPath()
{
var cachedGitInstallPath = SystemSettings.Get("GitInstallPath");
// Root paths
if (string.IsNullOrEmpty(cachedGitInstallPath) || !cachedGitInstallPath.ToNPath().Exists())
{
return GitEnvironment.FindGitInstallationPath(ProcessManager).Result;
}
else
{
return cachedGitInstallPath;
}
}
public CancellationToken CancellationToken { get { return cancellationTokenSource.Token; } }
private IEnvironment environment;
public IEnvironment Environment
{
get
{
// if this is called while still null, it's because Unity wants
// to render something and we need to load icons, and that runs
// before EntryPoint. Do an early initialization
if (environment == null)
InitializeEnvironment();
return environment;
}
set { environment = value; }
}
public IFileSystem FileSystem { get; private set; }
public IPlatform Platform { get; private set; }
public IGitEnvironment GitEnvironment { get { return Platform.GitEnvironment; } }
public IProcessManager ProcessManager { get; private set; }
public ICredentialManager CredentialManager { get; private set; }
public IGitClient GitClient { get; private set; }
public ITaskResultDispatcher TaskResultDispatcher { get; private set; }
public ISettings SystemSettings { get; private set; }
public ISettings LocalSettings { get; private set; }
public ISettings UserSettings { get; private set; }
public GitObjectFactory GitObjectFactory { get; private set; }
}
}
......@@ -57,6 +57,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationManager.cs" />
<Compile Include="Extensions\ActionExtensions.cs" />
<Compile Include="IO\LockOutputProcessor.cs" />
<Compile Include="Logging\FileLogAdapter.cs" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment