using System.ComponentModel; using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32.SafeHandles; namespace Agc.Unity.Attach; public static class ProjectIdentity { public static string Normalize(string projectPath) { if (string.IsNullOrWhiteSpace(projectPath) || projectPath.Contains('\0') || !Path.IsPathFullyQualified(projectPath)) throw new HostException("UnityProjectInvalid", "An absolute Unity project path is required."); var supplied = new DirectoryInfo(WithoutDevicePrefix(Path.GetFullPath(projectPath))); if (!supplied.Exists || IsLink(supplied)) throw new HostException("UnityProjectInvalid", "The Unity project root must be an existing ordinary directory."); var root = Canonical(projectPath); var directory = new DirectoryInfo(root); if (!directory.Exists || IsLink(directory)) throw new HostException("UnityProjectInvalid", "The Unity project root must be an existing ordinary directory."); foreach (var child in new[] { "Assets", "Packages", "ProjectSettings" }) { var info = new DirectoryInfo(Path.Combine(root, child)); if (!info.Exists || IsLink(info)) throw new HostException("UnityProjectInvalid", "The Unity project directories must be ordinary directories."); } var version = new FileInfo(Path.Combine(root, "ProjectSettings", "ProjectVersion.txt")); if (!version.Exists || IsLink(version)) throw new HostException("UnityProjectInvalid", "ProjectVersion.txt must be an existing ordinary file."); return root; } public static bool Same(string? first, string? second) { if (string.IsNullOrWhiteSpace(first) || string.IsNullOrWhiteSpace(second)) return false; try { return string.Equals(Canonical(first), Canonical(second), OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); } catch (Exception error) when (error is ArgumentException or NotSupportedException or IOException or Win32Exception or UnauthorizedAccessException) { return false; } } private static bool IsLink(FileSystemInfo info) => (info.Attributes & FileAttributes.ReparsePoint) != 0 || info.LinkTarget != null; internal static string Canonical(string path) { var full = Path.TrimEndingDirectorySeparator(Path.GetFullPath(WithoutDevicePrefix(path))); if (OperatingSystem.IsWindows() && Directory.Exists(full)) { // A directory handle resolves 8.3 aliases and junction ancestors to the // same physical identity used by Rust canonicalize before any attach. using var handle = CreateFileW(full, 0, 7, IntPtr.Zero, 3, 0x02000000, IntPtr.Zero); if (handle.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); var buffer = new StringBuilder(512); var length = GetFinalPathNameByHandleW(handle, buffer, (uint)buffer.Capacity, 0); if (length == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); if (length >= buffer.Capacity) { if (length > 32768) throw new PathTooLongException(); buffer = new StringBuilder((int)length + 1); length = GetFinalPathNameByHandleW(handle, buffer, (uint)buffer.Capacity, 0); if (length == 0 || length >= buffer.Capacity) throw new Win32Exception(Marshal.GetLastWin32Error()); } full = WithoutDevicePrefix(buffer.ToString()); } return Path.TrimEndingDirectorySeparator(full); } private static string WithoutDevicePrefix(string path) { if (!OperatingSystem.IsWindows()) return path; if (path.StartsWith(@"\\?\UNC\", StringComparison.OrdinalIgnoreCase)) return @"\\" + path[8..]; return path.StartsWith(@"\\?\", StringComparison.Ordinal) ? path[4..] : path; } [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern SafeFileHandle CreateFileW(string path, uint access, uint share, IntPtr security, uint creation, uint flags, IntPtr template); [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern uint GetFinalPathNameByHandleW(SafeFileHandle file, StringBuilder path, uint length, uint flags); }