-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityHub.cs
More file actions
97 lines (82 loc) · 2.49 KB
/
Copy pathUnityHub.cs
File metadata and controls
97 lines (82 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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<bool> 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;
}
}