Skip to content

Commit 29a7a12

Browse files
committed
update restore script to use platform-specific paths
1 parent 01d5608 commit 29a7a12

File tree

1 file changed

+68
-20
lines changed

1 file changed

+68
-20
lines changed

UnityProject/Assets/Editor/UnityBuilderAction/RestoreScript.cs

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,89 @@
33
using System.IO;
44
using System.Linq;
55
using UnityEditor;
6-
using UnityEditor.Build.Reporting;
76

87
namespace UnityBuilderAction
98
{
109
public static class RestoreScript
1110
{
12-
private static readonly string UnityEditorPath = "/opt/unity/Editor";
11+
private static readonly Dictionary<string, string> UnityEditorPath = new()
12+
{
13+
["linux"] = "/opt/unity/Editor",
14+
["windows"] = @"C:\UnityEditor\2022.3.9f1\Editor",
15+
["macOS"] = "/Applications/Unity/Hub/Editor/2022.3.9f1/Unity.app/Contents/MacOS"
16+
};
1317
private static readonly string LocalCopyTargetPath = "UnityEditor";
18+
19+
private static readonly string[] Secrets = {};
1420

1521
public static void Restore()
1622
{
17-
void RecursiveCopy(DirectoryInfo from, DirectoryInfo to)
18-
{
19-
Directory.CreateDirectory(to.FullName);
20-
21-
foreach (string entry in Directory.GetFiles(from.FullName))
22-
{
23-
var fileInfo = new FileInfo(entry);
24-
File.Copy(fileInfo.FullName, Path.Combine(to.FullName, fileInfo.Name));
25-
}
26-
27-
foreach (string entry in Directory.GetDirectories(from.FullName))
28-
{
29-
var fromSubdir = new DirectoryInfo(entry);
30-
var toSubdir = new DirectoryInfo(Path.Combine(to.FullName, fromSubdir.Name));
31-
RecursiveCopy(fromSubdir, toSubdir);
32-
}
33-
}
23+
// Gather values from args
24+
Dictionary<string, string> options = GetValidatedOptions();
25+
var hostPlatform = options["hostPlatform"];
3426

35-
var assembliesFrom = new DirectoryInfo(Path.Combine(UnityEditorPath, "Data", "Managed"));
27+
var assembliesFrom = new DirectoryInfo(Path.Combine(UnityEditorPath[hostPlatform], "Data", "Managed"));
3628
var assembliesTo = new DirectoryInfo(Path.Combine(Path.GetFullPath(LocalCopyTargetPath), "Data", "Managed"));
3729

3830
RecursiveCopy(assembliesFrom, assembliesTo);
3931

4032
EditorApplication.Exit(0);
4133
}
34+
35+
private static void RecursiveCopy(DirectoryInfo from, DirectoryInfo to)
36+
{
37+
Directory.CreateDirectory(to.FullName);
38+
39+
foreach (string entry in Directory.GetFiles(from.FullName))
40+
{
41+
var fileInfo = new FileInfo(entry);
42+
File.Copy(fileInfo.FullName, Path.Combine(to.FullName, fileInfo.Name));
43+
}
44+
45+
foreach (string entry in Directory.GetDirectories(from.FullName))
46+
{
47+
var fromSubdir = new DirectoryInfo(entry);
48+
var toSubdir = new DirectoryInfo(Path.Combine(to.FullName, fromSubdir.Name));
49+
RecursiveCopy(fromSubdir, toSubdir);
50+
}
51+
}
52+
53+
private static Dictionary<string, string> GetValidatedOptions()
54+
{
55+
ParseCommandLineArguments(out Dictionary<string, string> validatedOptions);
56+
57+
if (!validatedOptions.TryGetValue("hostPlatform", out string _))
58+
{
59+
Console.WriteLine("Missing argument -hostPlatform");
60+
EditorApplication.Exit(120);
61+
}
62+
63+
return validatedOptions;
64+
}
65+
66+
private static void ParseCommandLineArguments(out Dictionary<string, string> providedArguments)
67+
{
68+
providedArguments = new Dictionary<string, string>();
69+
string[] args = Environment.GetCommandLineArgs();
70+
71+
// Extract flags with optional values
72+
for (int current = 0, next = 1; current < args.Length; current++, next++)
73+
{
74+
// Parse flag
75+
bool isFlag = args[current].StartsWith("-");
76+
if (!isFlag) continue;
77+
string flag = args[current].TrimStart('-');
78+
79+
// Parse optional value
80+
bool flagHasValue = next < args.Length && !args[next].StartsWith("-");
81+
string value = flagHasValue ? args[next].TrimStart('-') : "";
82+
bool secret = Secrets.Contains(flag);
83+
string displayValue = secret ? "*HIDDEN*" : "\"" + value + "\"";
84+
85+
// Assign
86+
Console.WriteLine($"Found flag \"{flag}\" with value {displayValue}.");
87+
providedArguments.Add(flag, value);
88+
}
89+
}
4290
}
4391
}

0 commit comments

Comments
 (0)