$ErrorActionPreference = 'Stop' $sourceDir = 'apps/game_server/cache_recovered' $prefixDir = 'apps/game_server/cache' $outDir = 'apps/game_server/cache_recovered_grouped' if (-not (Test-Path $sourceDir)) { throw "Missing source dir: $sourceDir" } if (Test-Path $outDir) { $resolved = (Resolve-Path $outDir).Path $root = (Resolve-Path '.').Path if (-not $resolved.StartsWith($root, [StringComparison]::OrdinalIgnoreCase)) { throw "Refusing to clean outside workspace: $resolved" } Remove-Item -LiteralPath $resolved -Recurse -Force } New-Item -ItemType Directory -Force -Path $outDir | Out-Null $knownPrefixes = @() if (Test-Path $prefixDir) { $knownPrefixes = Get-ChildItem $prefixDir -Filter '*.table' | ForEach-Object { $_.BaseName } | Sort-Object Length -Descending } function Get-GroupName([string]$TableName, $KnownPrefixes) { foreach ($prefix in $KnownPrefixes) { if ($TableName -eq $prefix -or $TableName.StartsWith("$prefix`_", [StringComparison]::Ordinal)) { return $prefix } } $idx = $TableName.IndexOf('_') if ($idx -gt 0) { return $TableName.Substring(0, $idx) } return $TableName } $groups = @{} Get-ChildItem $sourceDir -Filter '*.table' | Sort-Object Name | ForEach-Object { $tableName = $_.BaseName $groupName = Get-GroupName $tableName $knownPrefixes if (-not $groups.ContainsKey($groupName)) { $groups[$groupName] = [System.Collections.Generic.List[object]]::new() } [void]$groups[$groupName].Add($_) } foreach ($groupName in ($groups.Keys | Sort-Object)) { $lines = [System.Collections.Generic.List[string]]::new() [void]$lines.Add("%% recovered grouped table file: $groupName.table") [void]$lines.Add("%% source: apps/game_server/cache_recovered/*.table") [void]$lines.Add('') foreach ($file in ($groups[$groupName] | Sort-Object Name)) { $content = [IO.File]::ReadAllText($file.FullName).Trim() $content = [regex]::Replace($content, '(?m)^%% recovered from .*\r?\n', '') [void]$lines.Add("%% -----------------------------------------------------------------------------") [void]$lines.Add("%% $($file.BaseName)") [void]$lines.Add($content) [void]$lines.Add('') } $outFile = Join-Path $outDir "$groupName.table" [IO.File]::WriteAllText($outFile, ($lines -join "`r`n")) } "source_files=$((Get-ChildItem $sourceDir -Filter '*.table').Count)" "grouped_files=$((Get-ChildItem $outDir -Filter '*.table').Count)"