101 lines
5.9 KiB
PowerShell
101 lines
5.9 KiB
PowerShell
<#
|
|
Repairs regenerated package GUIDs without changing package versions or game assets.
|
|
Dry-run by default. Close/reload Unity after applying so its asset database refreshes.
|
|
Official registry archives are verified against the installed package fingerprint.
|
|
Only Library/PackageCache .meta files are restored; overwritten files are backed up.
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[string]$ProjectPath = (Split-Path $PSScriptRoot -Parent),
|
|
[string]$EditorPath = 'C:/Program Files/Unity/Hub/Editor/6000.4.8f1/Editor',
|
|
[string]$RepairDirectory,
|
|
[switch]$Apply
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
$projectRoot = (Resolve-Path -LiteralPath $ProjectPath).Path
|
|
$packageRoot = (Resolve-Path -LiteralPath (Join-Path $projectRoot 'Library/PackageCache')).Path
|
|
$bundledRoot = Join-Path $EditorPath 'Data/Resources/PackageManager/BuiltInPackages'
|
|
if (!$RepairDirectory) {
|
|
$RepairDirectory = Join-Path ([IO.Path]::GetTempPath()) ('KernelPanicPackageRepair-' + [guid]::NewGuid().ToString('N'))
|
|
}
|
|
$repairRoot = [IO.Path]::GetFullPath($RepairDirectory)
|
|
New-Item -ItemType Directory -Path $repairRoot -Force | Out-Null
|
|
Write-Output "Recovery/download directory: $repairRoot"
|
|
|
|
function ChildPath([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"
|
|
}
|
|
return $path
|
|
}
|
|
|
|
$changes = [Collections.Generic.List[object]]::new()
|
|
foreach ($cached in Get-ChildItem -LiteralPath $packageRoot -Directory) {
|
|
$manifestPath = Join-Path $cached.FullName 'package.json'
|
|
if (!(Test-Path -LiteralPath $manifestPath)) { continue }
|
|
$package = Get-Content -LiteralPath $manifestPath -Raw | ConvertFrom-Json
|
|
# Packages with no regenerated metadata need no download or restoration.
|
|
$suspect = @(Get-ChildItem -LiteralPath $cached.FullName -Recurse -File -Filter '*.meta' |
|
|
Where-Object { $_.Length -lt 80 -and (Get-Content -LiteralPath $_.FullName -Raw) -match '^fileFormatVersion: 2\r?\nguid: [a-f0-9]{32}\s*$' })
|
|
if (!$suspect.Count) { continue }
|
|
|
|
$source = Join-Path $bundledRoot $package.name
|
|
$bundledManifest = Join-Path $source 'package.json'
|
|
$isMatchingBundle = (Test-Path -LiteralPath $bundledManifest) -and
|
|
((Get-Content -LiteralPath $bundledManifest -Raw | ConvertFrom-Json).version -eq $package.version)
|
|
if (!$isMatchingBundle) {
|
|
if ($package.name -notmatch '^com\.unity\.[a-z0-9.-]+$' -or $package.version -notmatch '^[a-zA-Z0-9.+-]+$') {
|
|
throw "Unsupported package identity: $($package.name) $($package.version)"
|
|
}
|
|
if ($package._fingerprint -notmatch '^[a-f0-9]{40}$') { throw "No verifiable archive fingerprint: $($package.name)" }
|
|
$archive = ChildPath $repairRoot "$($package.name)-$($package.version).tgz"
|
|
if (!(Test-Path -LiteralPath $archive)) {
|
|
$url = "https://download.packages.unity.com/$($package.name)/-/$($package.name)-$($package.version).tgz"
|
|
& curl.exe --fail --silent --show-error --location --retry 2 --connect-timeout 15 --max-time 180 $url --output $archive
|
|
if ($LASTEXITCODE) { throw "Package download failed: $($package.name)" }
|
|
}
|
|
if ((Get-FileHash -LiteralPath $archive -Algorithm SHA1).Hash -ne $package._fingerprint) {
|
|
throw "Archive checksum mismatch: $($package.name)"
|
|
}
|
|
$source = ChildPath $repairRoot "$($package.name)-$($package.version)"
|
|
if (!(Test-Path -LiteralPath (Join-Path $source 'package.json'))) {
|
|
$members = & tar -tzf $archive
|
|
if ($LASTEXITCODE) { throw "Cannot read archive: $archive" }
|
|
foreach ($member in $members) {
|
|
if ($member -notmatch '^package(/|$)' -or $member -match '(^|/)\.\.(/|$)' -or $member.Contains('\')) {
|
|
throw "Unsafe archive member: $member"
|
|
}
|
|
}
|
|
New-Item -ItemType Directory -Path $source -Force | Out-Null
|
|
& tar -xzf $archive -C $source --strip-components 1
|
|
if ($LASTEXITCODE) { throw "Package extraction failed: $archive" }
|
|
}
|
|
$verified = Get-Content -LiteralPath (Join-Path $source 'package.json') -Raw | ConvertFrom-Json
|
|
if ($verified.name -ne $package.name -or $verified.version -ne $package.version) { throw 'Package identity mismatch' }
|
|
}
|
|
|
|
$before = $changes.Count
|
|
foreach ($original in Get-ChildItem -LiteralPath $source -Recurse -File -Filter '*.meta') {
|
|
$relative = $original.FullName.Substring($source.Length).TrimStart('\','/')
|
|
$target = ChildPath $cached.FullName $relative
|
|
# Do not create stale metadata for absent package content (e.g. excluded samples).
|
|
if (!(Test-Path -LiteralPath $target.Substring(0, $target.Length - 5))) { continue }
|
|
if ((Test-Path -LiteralPath $target) -and
|
|
(Get-FileHash -LiteralPath $target).Hash -eq (Get-FileHash -LiteralPath $original.FullName).Hash) { continue }
|
|
$changes.Add([pscustomobject]@{ Source=$original.FullName; Target=$target; Backup=(ChildPath $repairRoot ('backup/' + $cached.Name + '/' + $relative)) })
|
|
}
|
|
Write-Output "$($package.name)@$($package.version): $($changes.Count - $before) metadata repairs"
|
|
}
|
|
|
|
if (!$changes.Count) { Write-Output 'Metadata verified: no repairs needed.'; return }
|
|
if (!$Apply) { Write-Output "Dry run: $($changes.Count) changes. Run again with -Apply and the same -RepairDirectory to restore."; return }
|
|
foreach ($change in $changes) {
|
|
if (Test-Path -LiteralPath $change.Target) {
|
|
New-Item -ItemType Directory -Path (Split-Path $change.Backup -Parent) -Force | Out-Null
|
|
Copy-Item -LiteralPath $change.Target -Destination $change.Backup -Force
|
|
}
|
|
Copy-Item -LiteralPath $change.Source -Destination $change.Target -Force
|
|
}
|
|
Write-Output "Restored $($changes.Count) metadata files. Backups: $repairRoot/backup. Package manifests and source code unchanged."
|