Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UnityEditorPatch/InfoProviders/Editor/EditorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class EditorInfo
public string ContentLocation { get; init; }
public string RuntimeLocation { get; init; }
public string RoslynLocation { get; init; }
public string? DotNetSdkHostLocation { get; init; }
public string? DotNetSdkSharedLocation { get; init; }
public string[] SourceGeneratorLocations { get; init; }
}

Expand Down
43 changes: 41 additions & 2 deletions UnityEditorPatch/InfoProviders/Editor/EditorInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public static bool TryGet(UnityVersion version, OSPlatform platform, string look

var contentPath = UnityLocationUtility.GetContentPath(lookupPath);
var runtimePath = Path.Combine(contentPath, pathSpecification.RuntimePath);
var roslynPath = Path.Combine(contentPath, pathSpecification.RoslynLocation);
var roslynPath = ResolveRoslynPath(contentPath, pathSpecification.RoslynLocation);
var dotNetSdkHostPath = ResolveDotNetSdkSubdirectory(contentPath, roslynPath, "host");
var dotNetSdkSharedPath = ResolveDotNetSdkSubdirectory(contentPath, roslynPath, "shared");
var sourceGeneratorLocations = pathSpecification.SourceGeneratorLocations
.Select(location => Path.Combine(contentPath, location))
.Where(File.Exists).ToArray();
Expand All @@ -33,10 +35,47 @@ public static bool TryGet(UnityVersion version, OSPlatform platform, string look
RoslynLocation = roslynPath,
ContentLocation = contentPath,
RuntimeLocation = runtimePath,
DotNetSdkHostLocation = dotNetSdkHostPath,
DotNetSdkSharedLocation = dotNetSdkSharedPath,
IsPatched = Backup.IsBackupExist(contentPath),
SourceGeneratorLocations = sourceGeneratorLocations
};

return true;
}
}

static string ResolveRoslynPath(string contentPath, string preferredRelativePath)
{
var preferredPath = Path.Combine(contentPath, preferredRelativePath);
if (Directory.Exists(preferredPath))
{
return preferredPath;
}

var dotNetSdkPath = Path.Combine(contentPath, "DotNetSdk", "sdk");
if (!Directory.Exists(dotNetSdkPath))
{
return preferredPath;
}

var roslynCandidates = Directory.GetDirectories(dotNetSdkPath)
.Select(versionPath => Path.Combine(versionPath, "Roslyn", "bincore"))
.Where(Directory.Exists)
.OrderBy(path => path, StringComparer.OrdinalIgnoreCase)
.ToArray();

return roslynCandidates.LastOrDefault() ?? preferredPath;
}

private static string? ResolveDotNetSdkSubdirectory(string contentPath, string roslynPath, string subdirectory)
{
var dotNetSdkPath = Path.Combine(contentPath, "DotNetSdk");
if (!roslynPath.StartsWith(dotNetSdkPath, StringComparison.OrdinalIgnoreCase))
{
return null;
}

var path = Path.Combine(dotNetSdkPath, subdirectory);
return Directory.Exists(path) ? path : null;
}
}
26 changes: 25 additions & 1 deletion UnityEditorPatch/Interactors/Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static bool TryPerform(EditorInfo info)
Directory.CreateDirectory(backupPath);
FileSystemUtility.CopyDirectory(info.RoslynLocation, BackupLocation(backupPath, info.RoslynLocation, relativeTo: info.ContentLocation));
FileSystemUtility.CopyDirectory(info.RuntimeLocation, BackupLocation(backupPath, info.RuntimeLocation, relativeTo: info.ContentLocation));
BackupDirectoryIfExists(info.DotNetSdkHostLocation, backupPath, info.ContentLocation);
BackupDirectoryIfExists(info.DotNetSdkSharedLocation, backupPath, info.ContentLocation);

foreach (var sourceGeneratorLocation in info.SourceGeneratorLocations)
{
Expand Down Expand Up @@ -53,6 +55,8 @@ public static bool TryRestore(EditorInfo info)

FileSystemUtility.ReplaceDirectory(info.RoslynLocation, with: BackupLocation(backupPath, info.RoslynLocation, relativeTo: info.ContentLocation));
FileSystemUtility.ReplaceDirectory(info.RuntimeLocation, with: BackupLocation(backupPath, info.RuntimeLocation, relativeTo: info.ContentLocation));
RestoreDirectoryIfExists(info.DotNetSdkHostLocation, backupPath, info.ContentLocation);
RestoreDirectoryIfExists(info.DotNetSdkSharedLocation, backupPath, info.ContentLocation);

foreach (var sourceGeneratorLocation in info.SourceGeneratorLocations)
{
Expand All @@ -74,4 +78,24 @@ private static string BackupLocation(string backupLocation, string location, str
var relativeLocation = Path.GetRelativePath(relativeTo, location);
return Path.Combine(backupLocation, relativeLocation);
}
}

private static void BackupDirectoryIfExists(string? directory, string backupPath, string contentLocation)
{
if (string.IsNullOrEmpty(directory) || !Directory.Exists(directory))
{
return;
}

FileSystemUtility.CopyDirectory(directory, BackupLocation(backupPath, directory, relativeTo: contentLocation));
}

private static void RestoreDirectoryIfExists(string? directory, string backupPath, string contentLocation)
{
if (string.IsNullOrEmpty(directory))
{
return;
}

FileSystemUtility.ReplaceDirectory(directory, with: BackupLocation(backupPath, directory, relativeTo: contentLocation));
}
}
20 changes: 19 additions & 1 deletion UnityEditorPatch/Interactors/DotNetPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static bool TryPerform(SDKInfo sdkInfo, EditorInfo editorInfo)
{
FileSystemUtility.ReplaceDirectory(editorInfo.RuntimeLocation, with: sdkInfo.Location);
FileSystemUtility.ReplaceDirectory(editorInfo.RoslynLocation, with: sdkInfo.RoslynLocation);
CopyDotNetSdkRuntimeSupport(editorInfo.DotNetSdkHostLocation, sdkInfo.Location, "host");
CopyDotNetSdkRuntimeSupport(editorInfo.DotNetSdkSharedLocation, sdkInfo.Location, "shared");
}
catch (Exception)
{
Expand All @@ -20,4 +22,20 @@ public static bool TryPerform(SDKInfo sdkInfo, EditorInfo editorInfo)

return true;
}
}

private static void CopyDotNetSdkRuntimeSupport(string? targetDirectory, string sdkRoot, string subdirectory)
{
if (string.IsNullOrEmpty(targetDirectory))
{
return;
}

var sourceDirectory = Path.Combine(sdkRoot, subdirectory);
if (!Directory.Exists(sourceDirectory))
{
return;
}

FileSystemUtility.CopyDirectory(sourceDirectory, targetDirectory);
}
}