From 7c60cb342b445816254b360c74466afb72f7582c Mon Sep 17 00:00:00 2001 From: PaulNonatomic Date: Sat, 7 Oct 2023 05:24:23 +0100 Subject: [PATCH] Added support to add projects to Unity Hub, but it requires a specific version of Hub at present so this needs more work. --- App/Program.cs | 3 ++ App/Unity/UnityHub.cs | 97 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 App/Unity/UnityHub.cs diff --git a/App/Program.cs b/App/Program.cs index f1aac4c..c10a716 100644 --- a/App/Program.cs +++ b/App/Program.cs @@ -14,6 +14,7 @@ public class Program private static UnityCli _untiyCli; private static QuickStartProject _project; private static UserSettings _userSettings; + private static UnityHub _unityHub; static async Task Main(string[] args) { @@ -21,6 +22,7 @@ static async Task Main(string[] args) _git = new Git(); _untiyCli = new UnityCli(); + _unityHub = new UnityHub(); _project = new QuickStartProject(); _userSettings = new UserSettings(); await _userSettings.LoadSettings(); @@ -59,6 +61,7 @@ await Parser.Default.ParseArguments(args).WithParsedAsync(async options if(createdLocalRepo) await _git.CreateRemoteRepo(_project, _userSettings); var createdUnityProject = await _untiyCli.CreateUnityProject(_project, unityVersion, installPath); + if (createdUnityProject) await _unityHub.AddProjectToHub(_project, _project.ProjectName, _project.ProjectPath, unityVersion); if(createdUnityProject) await _untiyCli.OpenUnityProject(_project, unityVersion, installPath); if(createdUnityProject) Output.WriteSuccessWithTick("Complete"); diff --git a/App/Unity/UnityHub.cs b/App/Unity/UnityHub.cs new file mode 100644 index 0000000..c700666 --- /dev/null +++ b/App/Unity/UnityHub.cs @@ -0,0 +1,97 @@ +using System.Diagnostics; +using Newtonsoft.Json.Linq; +using UnityQuickStart.App.IO; +using UnityQuickStart.App.Project; + +namespace UnityQuickStart.App.Unity; + +public class UnityHub +{ + public async Task AddProjectToHub(QuickStartProject project, string projectName, string projectPath, string version) + { + var addToHub = UserInput.GetYesNo("Would you like to add this project to Unity Hub:"); + if(!addToHub) Output.WriteSuccessWithTick("Skipped adding project to Hub"); + + var unityHubPath = string.Empty; + var isHubOpen = IsUnityHubOpen(out unityHubPath); + var closedHub = isHubOpen && await CloseUnityHub(); + + //get projects + var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + var projectJsonPath = Path.Combine(appData, "UnityHub", "projects-v1.json"); + var projectJsonTxt = await File.ReadAllTextAsync(projectJsonPath); + var projectsJson = JObject.Parse(projectJsonTxt); + + var directoryInfo = new DirectoryInfo(projectPath); + var newProject = new JObject + { + ["title"] = projectName, + ["lastModified"] = 1700000000000, + ["isCustomEditor"] = false, + ["path"] = projectPath, + ["containingFolderPath"] = directoryInfo.Parent?.FullName, + ["version"] = version + }; + projectsJson["data"]![$@"{projectPath}"] = newProject; + + //write projects + await File.WriteAllTextAsync(projectJsonPath, projectsJson.ToString()); + + if (closedHub) + { + await OpenHub(unityHubPath); + } + } + + private async Task OpenHub(string fileName) + { + Process.Start(new ProcessStartInfo() + { + FileName = fileName, + RedirectStandardInput = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }); + } + + private bool IsUnityHubOpen(out string unityHubPath) + { + unityHubPath = string.Empty; + var processes = Process.GetProcessesByName("Unity Hub"); + + if (processes.Length > 0) + { + unityHubPath = processes[0].MainModule.FileName; + return true; + } + + return false; + } + + private async Task CloseUnityHub() + { + const string processMsg = "Closing Unity Hub"; + const string fileName = "taskkill"; + const string args = "/F /IM \"Unity Hub.exe\""; + var success = false; + + await ProcessExecutor.ExecuteProcess(fileName,args, processMsg, + (output) => + { + success = true; + Output.WriteSuccessWithTick($"Ok close Hub"); + + //wait to ensure hub to close + Task.Delay(2000); + }, + (error) => + { + success = false; + Output.WriteError($"Failed to close Hub: {error}"); + }); + + return success; + } +} \ No newline at end of file