Skip to content

Commit 1c2e088

Browse files
committed
refactor
1 parent 9a4b7c5 commit 1c2e088

File tree

4 files changed

+57
-45
lines changed

4 files changed

+57
-45
lines changed

Plugins/CSharpCompilerSettings/CompilerInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public bool IsValid
1515
get { return !string.IsNullOrEmpty(Path) && !string.IsNullOrEmpty(RuntimePath); }
1616
}
1717

18-
private CompilerInfo(string packageId, string path, string runtimePath)
18+
public CompilerInfo(string packageId, string path, string runtimePath)
1919
{
2020
PackageId = packageId;
2121
Path = path;
@@ -44,9 +44,9 @@ public static CompilerInfo GetInstalledInfo(string packageId)
4444
return new CompilerInfo(packageId, "", "");
4545
}
4646

47-
public void Setup(ProcessStartInfo psi, string responseFile)
47+
public void Setup(ProcessStartInfo psi, string responseFile, RuntimePlatform platform)
4848
{
49-
if (Application.platform == RuntimePlatform.WindowsEditor
49+
if (platform == RuntimePlatform.WindowsEditor
5050
&& Path.EndsWith(".exe"))
5151
{
5252
psi.FileName = System.IO.Path.GetFullPath(Path);

Plugins/CSharpCompilerSettings/Core.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212

1313
namespace Coffee.CSharpCompilerSettings
1414
{
15-
[InitializeOnLoad]
1615
internal static class Core
1716
{
18-
private static bool IsGlobal { get; }
17+
private static bool IsGlobal { get; set; }
1918

2019
public static string GetAssemblyName(string asmdefPath)
2120
{
@@ -142,6 +141,9 @@ public static string ModifyResponseFile(CscSettingsAsset setting, string assembl
142141

143142
private static void ChangeCompilerProcess(object compiler, object scriptAssembly, CscSettingsAsset setting)
144143
{
144+
if (IsDevelopAssembly)
145+
return;
146+
145147
var tProgram = Type.GetType("UnityEditor.Utils.Program, UnityEditor");
146148
var tScriptCompilerBase = Type.GetType("UnityEditor.Scripting.Compilers.ScriptCompilerBase, UnityEditor");
147149
var fiProcess = tScriptCompilerBase.GetField("process", BindingFlags.NonPublic | BindingFlags.Instance);
@@ -183,7 +185,7 @@ private static void ChangeCompilerProcess(object compiler, object scriptAssembly
183185
}
184186

185187
// Change exe file path.
186-
compilerInfo.Setup(psi, responseFile);
188+
compilerInfo.Setup(psi, responseFile, Application.platform);
187189
}
188190

189191
// Modify response file.
@@ -254,13 +256,15 @@ public static void OnAssemblyCompilationStarted(string name)
254256
}
255257
}
256258

257-
static Core()
259+
static bool IsDevelopAssembly
258260
{
259-
// For development assemblies: Do nothing.
260-
var coreAssemblyName = typeof(Core).Assembly.GetName().Name;
261-
if (coreAssemblyName == "CSharpCompilerSettings_") return;
261+
get { return typeof(Core).Assembly.GetName().Name == "CSharpCompilerSettings_"; }
262+
}
262263

263-
IsGlobal = coreAssemblyName == "CSharpCompilerSettings";
264+
[InitializeOnLoadMethod]
265+
public static void Initialize()
266+
{
267+
IsGlobal = typeof(Core).Assembly.GetName().Name == "CSharpCompilerSettings" || IsDevelopAssembly;
264268

265269
// Setup logger.
266270
if (IsGlobal)

Plugins/CSharpCompilerSettings/RuntimeInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private RuntimeInfo(string packageId, string path)
1818
public static RuntimeInfo GetInstalledDotnetInfo(string version)
1919
{
2020
var packageId = "dotnet-runtime-" + version;
21-
var url = GetDotnetDownloadUrl(version);
21+
var url = GetDotnetDownloadUrl(version, Application.platform);
2222
var installPath = Utils.InstallPackage(packageId, url);
2323
if (string.IsNullOrEmpty(installPath)) return new RuntimeInfo(packageId, "");
2424

@@ -28,12 +28,12 @@ public static RuntimeInfo GetInstalledDotnetInfo(string version)
2828
return new RuntimeInfo(packageId, dotnetPath);
2929
}
3030

31-
private static string GetDotnetDownloadUrl(string version)
31+
public static string GetDotnetDownloadUrl(string version, RuntimePlatform platform)
3232
{
3333
const string pattern = "https://dotnetcli.azureedge.net/dotnet/Runtime/{0}/dotnet-runtime-{0}-{1}-x64.{2}";
3434

3535
// todo: replace hardcoded 3.1.8 with maximum available version
36-
switch (Application.platform)
36+
switch (platform)
3737
{
3838
case RuntimePlatform.WindowsEditor:
3939
return string.Format(pattern, version, "win", "zip");

Plugins/CSharpCompilerSettings/Utils.cs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public static void RequestCompilation(string assemblyName = null)
5454
}
5555

5656
var allScripts = editorCompilation.Get("allScripts") as Dictionary<string, string>;
57+
if (allScripts == null) return;
58+
5759
var assemblyFilename = assemblyName + ".dll";
5860
var path = allScripts.FirstOrDefault(x => x.Value == assemblyFilename).Key;
5961
if (string.IsNullOrEmpty(path)) return;
@@ -155,58 +157,64 @@ public static string DownloadFile(string url)
155157
{
156158
ServicePointManager.ServerCertificateValidationCallback = cb;
157159

158-
Logger.LogInfo("Download {0} (alternative)", url);
159-
160160
// NOTE: In .Net Framework 3.5, TSL1.2 is not supported.
161161
// So, download the file on command line instead.
162-
switch (Application.platform)
163-
{
164-
case RuntimePlatform.WindowsEditor:
165-
ExecuteCommand("PowerShell.exe", string.Format("curl -O {0} {1}", downloadPath, url));
166-
break;
167-
case RuntimePlatform.OSXEditor:
168-
ExecuteCommand("curl", string.Format("-o {0} -L {1}", downloadPath, url));
169-
break;
170-
case RuntimePlatform.LinuxEditor:
171-
ExecuteCommand("wget", string.Format("-O {0} {1}", downloadPath, url));
172-
break;
173-
}
162+
Logger.LogInfo("Download {0} (alternative)", url);
163+
var args = GetDownloadCommand(url, downloadPath, Application.platform);
164+
ExecuteCommand(args[0], args[1]);
174165
}
175166

176167
return downloadPath;
177168
}
178169

179-
/// <summary>
180-
/// Extract archive file.
181-
/// </summary>
182-
/// <param name="archivePath">Archive file path</param>
183-
/// <param name="extractTo">Extraction path</param>
170+
public static string[] GetDownloadCommand(string url, string downloadPath, RuntimePlatform platform)
171+
{
172+
switch (platform)
173+
{
174+
case RuntimePlatform.WindowsEditor:
175+
return new []{"PowerShell.exe", string.Format("curl -O {0} {1}", downloadPath, url)};
176+
case RuntimePlatform.OSXEditor:
177+
return new []{"curl", string.Format("-o {0} -L {1}", downloadPath, url)};
178+
case RuntimePlatform.LinuxEditor:
179+
return new []{"wget", string.Format("-O {0} {1}", downloadPath, url)};
180+
181+
default:
182+
throw new NotSupportedException($"{Application.platform} is not supported");
183+
}
184+
}
185+
186+
// Extract archive file.
184187
public static void ExtractArchive(string archivePath, string extractTo)
185188
{
186189
Logger.LogInfo("Extract archive {0} to {1}", archivePath, extractTo);
187-
var contentsPath = EditorApplication.applicationContentsPath;
188-
var args = string.Format("x {0} -o{1}", archivePath, extractTo);
189-
190-
Directory.CreateDirectory(Path.GetDirectoryName(extractTo));
190+
var args = GetExtractArchiveCommand(archivePath, extractTo, Application.platform);
191+
ExecuteCommand(args[0], args[1]);
192+
}
191193

192-
switch (Application.platform)
194+
public static string[] GetExtractArchiveCommand(string archivePath, string extractTo, RuntimePlatform platform)
195+
{
196+
var contentsPath = EditorApplication.applicationContentsPath;
197+
switch (platform)
193198
{
194199
case RuntimePlatform.WindowsEditor:
195-
ExecuteCommand(PathCombine(contentsPath, "Tools", "7z.exe"), args);
196-
break;
200+
Directory.CreateDirectory(Path.GetDirectoryName(extractTo));
201+
return new[] {PathCombine(contentsPath, "Tools", "7z.exe"), string.Format("x {0} -o{1}", archivePath, extractTo)};
197202
case RuntimePlatform.OSXEditor:
198203
case RuntimePlatform.LinuxEditor:
199204
if (archivePath.EndsWith("tar.gz"))
200205
{
201-
// 7za doesn't preserve permission but tar does
202206
Directory.CreateDirectory(extractTo);
203-
ExecuteCommand("tar", string.Format("-pzxf {0} -C {1}", archivePath, extractTo));
207+
return new[] {"tar", string.Format("-pzxf {0} -C {1}", archivePath, extractTo)};
204208
}
205209
else
206-
ExecuteCommand(PathCombine(contentsPath, "Tools", "7za"), args);
207-
208-
break;
210+
{
211+
Directory.CreateDirectory(Path.GetDirectoryName(extractTo));
212+
return new[] {PathCombine(contentsPath, "Tools", "7za"), string.Format("x {0} -o{1}", archivePath, extractTo)};
213+
}
214+
default:
215+
throw new NotSupportedException($"{Application.platform} is not supported");
209216
}
217+
210218
}
211219

212220
/// <summary>

0 commit comments

Comments
 (0)