-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnityHub.cs
More file actions
191 lines (160 loc) · 5.97 KB
/
UnityHub.cs
File metadata and controls
191 lines (160 loc) · 5.97 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System.Runtime.InteropServices;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using EditorInfo = (string Version, string Path);
internal class UnityHub(PlatformSupport platformSupport)
{
private readonly Lazy<string> hubPathCache = new(() => platformSupport.FindHubInstallPath() ?? throw new UserException(
"Unity Hub not found. If it is installed in a custom location, configure the UNITY_HUB_PATH environment variable."));
// It would seem more efficient to store the editors in a Dictionary by version, but it's possible
// to install multiple editors with the same version (e.g. Intel and Silicon on macOS).
private List<EditorInfo>? editorsCache;
/// <summary>
/// Returns the path to the Unity Hub executable.
/// </summary>
public string GetHubPath() => hubPathCache.Value;
/// <summary>
/// Returns the path to the editor executable on the current platform.
/// </summary>
public string GetEditorPath(string version)
{
// Fast: try the default install location first.
string? editorPathDefault = platformSupport.FindDefaultEditorPath(version);
if (editorPathDefault != null)
return editorPathDefault;
// Fallback: query Unity Hub for custom installation locations.
var editors = ListInstalledEditors();
string? editorPathHub = editors.FirstOrDefault(p => p.Version == version).Path;
if (editorPathHub == null)
throw new UserException($"Unity version {version} is not installed.");
// On macOS, the Unity Hub returns a path to the app bundle (Unity.app), but we need the binary within.
return platformSupport.GetUnityExecutablePath(editorPathHub);
}
public IEnumerable<string> GetRecentProjects(bool favoriteOnly = false)
{
string configDir = platformSupport.UnityHubConfigDirectory;
string projectsFile = Path.Combine(configDir, "projects-v1.json");
try
{
string json = File.ReadAllText(projectsFile);
var root = JsonNode.Parse(json);
var data = root?["data"]?.AsObject()!;
var projects = new List<(string path, long lastModified, bool isFavorite)>();
foreach ((string projectPath, JsonNode? value) in data)
{
var project = value?.AsObject()!;
long? lastModified = project["lastModified"]?.GetValue<long>();
if (lastModified.HasValue)
{
bool isFavorite = project["isFavorite"]?.GetValue<bool>() ?? false;
projects.Add((projectPath, lastModified.Value, isFavorite));
}
}
var filteredProjects = favoriteOnly
? projects.Where(p => p.isFavorite)
: projects;
return filteredProjects
.OrderByDescending(p => p.lastModified)
.Select(p => p.path);
}
catch
{
return [];
}
}
public void InstallEditorChecked(
string version,
string? changeset,
IProcessRunner processRunner,
string[]? additionalArgs = null)
{
if (IsEditorInstalled(version))
return;
InstallEditor(version, changeset, processRunner, additionalArgs ?? []);
}
public void InstallEditor(string version, string? changeset, IProcessRunner processRunner, string[] additionalArgs)
{
WriteStatusUpdate($"Installing Unity version {version} {changeset}");
if (changeset == null)
{
WriteStatusUpdate("Changeset not provided, fetching from Unity API");
changeset = UnityReleaseApi.FetchChangesetAsync(version).Result;
}
string args = $"--headless install --version {version} --changeset {changeset}";
args = ConfigurePlatformArgs(args);
if (additionalArgs.Length > 0)
args += " " + string.Join(" ", additionalArgs);
var process = processRunner.Run(new ProcessStartInfo(
hubPathCache.Value,
platformSupport.FormatHubArgs(args)) { RedirectStandardError = true });
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new UserException($"Failed to install Unity {version}. (Exit code: {process.ExitCode})");
}
// Invalidate the cache after installing a new editor
editorsCache = null;
}
public List<EditorInfo> ListInstalledEditors()
{
if (editorsCache != null)
return editorsCache;
string args = platformSupport.FormatHubArgs("--headless editors --installed");
ProcessStartInfo startInfo = new(hubPathCache.Value, args)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
};
Process process = ProcessRunner.Default.Run(startInfo);
string output = process.CaptureOutput().output;
// There's a bug in some older Unity Hub version where the exit code is non-zero, but the output works.
// So, don't do any validation, just attempt to parse.
// The paths in this output specify the Unity.app directory on macOS, not the executable within.
editorsCache = ParseEditorsOutput(output);
return editorsCache;
}
internal static List<EditorInfo> ParseEditorsOutput(string output)
{
var matches = Regex.Matches(output, @"(\S+).*? installed at (.+)");
return matches.Select(m => new EditorInfo(m.Groups[1].Value, m.Groups[2].Value)).ToList();
}
private bool IsEditorInstalled(string version)
{
// Fast path: check default install location first
if (platformSupport.FindDefaultEditorPath(version) != null)
return true;
// Fallback: query Unity Hub for custom installation locations
try
{
var editors = ListInstalledEditors();
return editors.Any(s => s.Version == version);
}
catch
{
return false;
}
}
private static string ConfigurePlatformArgs(string args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
string? arch = RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "x86_64",
Architecture.Arm64 => "arm64",
// Unsupported architecture will probably cause the hub installation to fail,
// but better we try and let the user discover that and report a bug than abort.
_ => null,
};
if (arch != null)
return args + $" --architecture {arch}";
}
return args;
}
private static void WriteStatusUpdate(string message)
{
// This output is colored to differentiate it from the verbose Unity Hub output, which can be noisy.
// However, we do want the progress feedback that the Hub provides.
AnsiConsole.MarkupLine($"[cyan]{message}...[/]");
}
}