<# Backs up stale generated import/compiler caches after package GUID repair. Dry-run by default. Save and close this project's Unity editor before -Apply. Preserves PackageCache, Assets, Packages, ProjectSettings, UserSettings and saves. #> [CmdletBinding()] param( [string]$ProjectPath = (Split-Path $PSScriptRoot -Parent), [switch]$Apply ) $ErrorActionPreference = 'Stop' $projectRoot = (Resolve-Path -LiteralPath $ProjectPath).Path.TrimEnd('\','/') foreach ($required in @('Assets', 'Packages', 'ProjectSettings/ProjectVersion.txt')) { if (!(Test-Path -LiteralPath (Join-Path $projectRoot $required))) { throw "Not a Unity project: $projectRoot" } } function CheckedChild([string]$root, [string]$relative) { $path = [IO.Path]::GetFullPath((Join-Path $root $relative)) if (!$path.StartsWith($root.TrimEnd('\','/') + [IO.Path]::DirectorySeparatorChar, [StringComparison]::OrdinalIgnoreCase)) { throw "Path escapes intended directory: $path" } if ((Test-Path -LiteralPath $path) -and ((Get-Item -LiteralPath $path -Force).Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "Refusing to move through a junction/symlink: $path" } return $path } function AssertEditorClosed { foreach ($process in Get-CimInstance Win32_Process -Filter "Name = 'Unity.exe'") { if (!$process.CommandLine) { throw "Cannot verify Unity process $($process.ProcessId). Close Unity before applying." } if ($process.CommandLine -match '(?i)(?:^|\s)"?-projectPath"?\s+(?:"(?[^"]+)"|(?\S+))') { $argument = if ($Matches.quoted) { $Matches.quoted } else { $Matches.plain } $openProject = [IO.Path]::GetFullPath($argument).TrimEnd('\','/') if ($openProject -eq $projectRoot) { throw "Save and close this project's Unity editor first (process $($process.ProcessId)). No cache files moved." } } } } $library = CheckedChild $projectRoot 'Library' if (!(Test-Path -LiteralPath $library -PathType Container)) { Write-Output 'No Library directory; Unity will perform a fresh import.' return } # Explicit allowlist: never move all of Library, especially its repaired PackageCache. $names = @('SourceAssetDB', 'SourceAssetDB-lock', 'ArtifactDB', 'ArtifactDB-lock', 'Artifacts', 'TempArtifacts', 'Bee', 'ScriptAssemblies', 'ScriptMapper') $targets = @($names | ForEach-Object { $candidate = CheckedChild $library $_ if (Test-Path -LiteralPath $candidate) { $candidate } }) foreach ($target in $targets) { Write-Output "Generated cache: $target" } if (!$targets.Count) { Write-Output 'No stale cache targets found.'; return } if (!$Apply) { Write-Output "Dry run: $($targets.Count) cache entries. Save and close Unity, then run with -Apply." return } AssertEditorClosed $logs = CheckedChild $projectRoot 'Logs' $backup = CheckedChild $logs ('ImportCacheBackup-' + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $backup | Out-Null Write-Output "Backup directory: $backup" # All paths were resolved and bounded above before moving any directory. foreach ($target in $targets) { $destination = CheckedChild $backup ([IO.Path]::GetFileName($target)) Move-Item -LiteralPath $target -Destination $destination } Write-Output "Backed up $($targets.Count) generated cache entries. Nothing deleted." Write-Output 'Reopen Unity and allow the import/compilation to finish before entering Play Mode.' Write-Output "PackageCache and project content preserved. Recovery copies remain in $backup."