Last active
March 2, 2025 06:14
-
-
Save mavaddat/140e83cd80139948c2cb940ddf910ddb to your computer and use it in GitHub Desktop.
Small cmdlet to download and install the syzygy tablebases from lichess.org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-Tablebases | |
{ | |
[CmdletBinding()] | |
param ( | |
[int] | |
[ValidateScript({ $_ -ge 3 },ErrorMessage = "'{0}' is not a valid number of pieces")] | |
[Parameter(HelpMessage = "The maximum number of pieces whose tablebases you want to download.")] | |
$MaxPieces = 7, | |
[string] | |
[ValidateScript({ Test-Path -Path $_ -PathType Container }, ErrorMessage = "Cannot find install path '{0}' because it does not exist.")] | |
$InstallPath | |
) | |
begin | |
{ | |
Write-Verbose -Message "Retrieving tablebase download URLs for up to $MaxPieces pieces." | |
Invoke-RestMethod -Uri "https://syzygy-tables.info/download.txt?source=lichess&max-pieces=${MaxPieces}" | ForEach-Object { $_ -split "`n" } | ForEach-Object -Begin { | |
[uri]$downloadLink = [uri]$null | |
$index = 0 | |
} -Process { | |
if ([uri]::TryCreate($_, [System.UriKind]::Absolute, [ref]$downloadLink)) | |
{ | |
[pscustomobject]@{ | |
Uri = $downloadLink | |
Index = $index | |
} | |
$index++ | |
} | |
} | Set-Variable -Name TablebaseDownloadUris -Scope Script | |
Write-Verbose -Message "Retrieved $($TablebaseDownloadUris.Count) tablebase download URLs." | |
} | |
process | |
{ | |
Write-Verbose -Message 'Starting download of tablebases.' | |
if (Get-Command -Name ForEach-Object | Where-Object { $_.Parameters.ContainsKey('Parallel') }) | |
{ | |
$TablebaseDownloadUris | ForEach-Object -ThrottleLimit $env:NUMBER_OF_PROCESSORS -Parallel { | |
$outFile = Join-Path -Path $using:InstallPath -ChildPath ($_.Uri.Segments[-1]) | |
Write-Progress -Activity 'Downloading tablebases' -Status "$($_.Uri)" -PercentComplete(100 * $_.Index / $($using:TablebaseDownloadUris).Count) | |
Invoke-RestMethod -Uri $_.Uri -OutFile $outFile | |
} | |
} | |
else | |
{ | |
$client = New-Object -TypeName System.Net.WebClient | |
$downloadTasks = $( | |
foreach ($downloadLink in $TablebaseDownloadUris) | |
{ | |
$outFile = Join-Path -Path $InstallPath -ChildPath $($downloadLink.Uri.Segments[-1]) | |
Write-Progress -Activity 'Queuing tablebase downloads' -Status "$outFile" -Id 0 -PercentComplete $([System.Math]::Round(100 * $downloadLink.Index / $TablebaseDownloadUris.Count)) | |
$client.DownloadFileTaskAsync(($downloadLink.Uri), $outFile) | Write-Output | |
} | |
) | |
Write-Verbose -Message 'Waiting for all downloads to complete.' | |
$completedDownloads = 0 | |
while ($completedDownloads -lt $downloadTasks.Count) | |
{ | |
$completedDownloads = $downloadTasks | Where-Object { $_.IsCompleted } | Measure-Object | Select-Object -ExpandProperty Count | |
Write-Progress -Activity 'Downloading tablebases' -Status "$completedDownloads of $($downloadTasks.Count) downloads completed" -PercentComplete $([System.Math]::Round(100 * $completedDownloads / $downloadTasks.Count)) -CurrentOperation | |
Start-Sleep -Seconds 1 | |
} | |
} | |
} | |
end | |
{ | |
Write-Verbose -Message 'Tablebase download process completed.' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment