65 lines
3.0 KiB
PowerShell
65 lines
3.0 KiB
PowerShell
param([switch]$NoBackup)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$Project = Split-Path $PSScriptRoot -Parent
|
|
$AssetRoot = Join-Path $Project 'docs\asset'
|
|
$NewRoot = Get-ChildItem -LiteralPath $AssetRoot -Directory |
|
|
Where-Object { @(Get-ChildItem -LiteralPath $_.FullName -Recurse -File -Filter '*.glb').Count -gt 0 } |
|
|
Select-Object -First 1
|
|
if ($null -eq $NewRoot) { throw 'Could not find the new GLB asset folder.' }
|
|
|
|
$ScaledRoot = Join-Path $AssetRoot "$($NewRoot.Name)_project_scale_fbx\models"
|
|
$ReferenceDirs = @((Join-Path $Project 'assets\Models'), (Join-Path $Project 'assets\Model'))
|
|
$SpecialReferences = @{
|
|
'BulletTrail' = 'BulletTrail'
|
|
'SM_Item_Bullet_01' = 'bullet'
|
|
'SM_Tile_Cube_Water_0' = 'SM_Tile_Cube_Water_01'
|
|
'test' = 'test'
|
|
'WEP_98k' = 'Wep_Rifle_98k'
|
|
'WEP_M4A1 copy' = 'Wep_Rifle_1'
|
|
'WEP_Sniper_1' = 'Wep_Sniper_01'
|
|
}
|
|
|
|
function Normalize-ModelName([string]$Name) {
|
|
return (($Name.ToLowerInvariant().ToCharArray() | Where-Object { [char]::IsLetterOrDigit($_) }) -join '')
|
|
}
|
|
|
|
$References = @{}
|
|
foreach ($Directory in $ReferenceDirs) {
|
|
Get-ChildItem -LiteralPath $Directory -Recurse -File -Filter '*.fbx' | ForEach-Object {
|
|
$References[(Normalize-ModelName $_.BaseName)] = $_
|
|
}
|
|
}
|
|
|
|
$Replacements = foreach ($Glb in Get-ChildItem -LiteralPath $NewRoot.FullName -Recurse -File -Filter '*.glb' | Sort-Object FullName) {
|
|
$ReferenceName = if ($SpecialReferences.ContainsKey($Glb.BaseName)) { $SpecialReferences[$Glb.BaseName] } else { $Glb.BaseName }
|
|
$Target = $References[(Normalize-ModelName $ReferenceName)]
|
|
$Relative = $Glb.FullName.Substring($NewRoot.FullName.Length).TrimStart('\')
|
|
$Source = Join-Path $ScaledRoot ([IO.Path]::ChangeExtension($Relative, '.fbx'))
|
|
if ($null -eq $Target) { throw "No project target for: $($Glb.Name)" }
|
|
if (-not (Test-Path -LiteralPath $Source)) { throw "Scaled FBX missing: $Source" }
|
|
[PSCustomObject]@{ Source = $Source; Target = $Target.FullName; Reference = $Target.Name }
|
|
}
|
|
|
|
if (@($Replacements).Count -ne 113) { throw "Expected 113 replacements; found $(@($Replacements).Count)." }
|
|
if (@($Replacements.Target | Select-Object -Unique).Count -ne @($Replacements).Count) { throw 'Multiple new models map to the same project target.' }
|
|
|
|
if (-not $NoBackup) {
|
|
$BackupRoot = Join-Path $AssetRoot ('project_model_fbx_backup_' + (Get-Date -Format 'yyyyMMdd_HHmmss'))
|
|
New-Item -ItemType Directory -Path $BackupRoot | Out-Null
|
|
foreach ($Item in $Replacements) {
|
|
$RelativeTarget = $Item.Target.Substring($Project.Length).TrimStart('\')
|
|
$Backup = Join-Path $BackupRoot $RelativeTarget
|
|
New-Item -ItemType Directory -Force -Path (Split-Path $Backup -Parent) | Out-Null
|
|
Copy-Item -LiteralPath $Item.Target -Destination $Backup
|
|
}
|
|
}
|
|
foreach ($Item in $Replacements) {
|
|
Copy-Item -LiteralPath $Item.Source -Destination $Item.Target -Force
|
|
}
|
|
if ($BackupRoot) {
|
|
$Replacements | Export-Csv -LiteralPath (Join-Path $BackupRoot 'replacement_manifest.csv') -NoTypeInformation -Encoding UTF8
|
|
}
|
|
Write-Output "Replaced $(@($Replacements).Count) FBX files."
|