Files
youlegames/codes/games/client/Projects/Spine/scripts/build_spine_data.ps1
2026-04-09 17:31:46 +08:00

99 lines
3.9 KiB
PowerShell

# Spine resource data generation script
# Scans assets/spine/ directory and generates:
# generated/spine_assets.js - resource name list
# generated/spine_data.js - .json/.atlas text content embedded (file:// CORS fix)
$ErrorActionPreference = "Stop"
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$projectDir = Split-Path -Parent $scriptDir
$spineDir = Join-Path $projectDir "assets\spine"
$outDir = Join-Path $projectDir "generated"
$assetsOut = Join-Path $outDir "spine_assets.js"
$dataOut = Join-Path $outDir "spine_data.js"
if (-not (Test-Path $outDir)) {
New-Item -ItemType Directory -Path $outDir | Out-Null
}
if (-not (Test-Path $spineDir)) {
Write-Host "[WARN] Spine assets directory not found: $spineDir — generating empty files" -ForegroundColor Yellow
$jsonFiles = @()
} else {
$jsonFiles = Get-ChildItem $spineDir -Filter "*.json" | Sort-Object Name
}
if ($jsonFiles.Count -eq 0) {
Write-Host "[INFO] No .json files found — generating empty placeholder files" -ForegroundColor Yellow
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($assetsOut, "// Spine resource list (auto-generated, do not edit manually)`ngameabc_face.spineAssets = [];`n", $utf8NoBom)
[System.IO.File]::WriteAllText($dataOut, "// Spine text data (auto-generated, do not edit manually)`ngameabc_face.spineTextData = {};`n", $utf8NoBom)
Write-Host "[OK] $assetsOut (empty)" -ForegroundColor Green
Write-Host "[OK] $dataOut (empty)" -ForegroundColor Green
exit 0
}
$names = @()
foreach ($f in $jsonFiles) {
$names += $f.BaseName
}
Write-Host "Found $($names.Count) Spine resource(s): $($names -join ', ')" -ForegroundColor Cyan
# ==================== spine_assets.js ====================
$assetsSB = [System.Text.StringBuilder]::new()
[void]$assetsSB.AppendLine('// Spine resource list (auto-generated, do not edit manually)')
[void]$assetsSB.Append('gameabc_face.spineAssets = [')
for ($i = 0; $i -lt $names.Count; $i++) {
if ($i -gt 0) { [void]$assetsSB.Append(',') }
[void]$assetsSB.AppendLine('')
[void]$assetsSB.Append("`t`"$($names[$i])`"")
}
[void]$assetsSB.AppendLine('')
[void]$assetsSB.AppendLine('];')
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($assetsOut, $assetsSB.ToString(), $utf8NoBom)
Write-Host "[OK] $assetsOut" -ForegroundColor Green
# ==================== spine_data.js ====================
$dataSB = [System.Text.StringBuilder]::new()
[void]$dataSB.AppendLine('// Spine text data (auto-generated, do not edit manually)')
[void]$dataSB.AppendLine('// Embeds .json/.atlas content into JS to bypass file:// XHR CORS')
[void]$dataSB.AppendLine('gameabc_face.spineTextData = {};')
$totalOriginal = 0
foreach ($name in $names) {
foreach ($ext in '.json', '.atlas') {
$filePath = Join-Path $spineDir "$name$ext"
if (-not (Test-Path $filePath)) {
Write-Host "[WARN] Missing file: $name$ext" -ForegroundColor Yellow
continue
}
$raw = [System.IO.File]::ReadAllText($filePath, [System.Text.Encoding]::UTF8)
$totalOriginal += (Get-Item $filePath).Length
$escaped = $raw.Replace('\', '\\').Replace("'", "\'")
$escaped = $escaped.Replace("`r`n", '\n').Replace("`n", '\n').Replace("`r", '')
[void]$dataSB.AppendLine("gameabc_face.spineTextData['$name$ext'] = '$escaped';")
}
}
[System.IO.File]::WriteAllText($dataOut, $dataSB.ToString(), $utf8NoBom)
$dataSize = (Get-Item $dataOut).Length
$overhead = $dataSize - $totalOriginal
Write-Host "[OK] $dataOut" -ForegroundColor Green
Write-Host ''
Write-Host '=== Summary ===' -ForegroundColor Cyan
Write-Host " Resources: $($names.Count)"
Write-Host " Original size: $([math]::Round($totalOriginal/1024, 1)) KB"
Write-Host " Output size: $([math]::Round($dataSize/1024, 1)) KB (overhead: $overhead bytes)"