Last active
December 4, 2015 01:26
-
-
Save JPRuskin/60ac338e32d8fd63695a to your computer and use it in GitHub Desktop.
Gets all of the files from the public or test StarCitizen manifests and downloads them to $directory (prompts if not imported).
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-StarCitizenManifest { | |
[CmdletBinding(SupportsShouldProcess)] | |
param() | |
$return = Invoke-WebRequest -Uri 'http://manifest.robertsspaceindustries.com/Launcher/_LauncherInfo' -OutFile $env:temp/launcherInfo | |
Get-Content -Path $env:temp/launcherInfo | ConvertFrom-StringData | |
} | |
function Get-StarCitizenFiles { | |
[CmdletBinding(SupportsShouldProcess)] | |
param( | |
[Parameter(Mandatory)][System.String] | |
$destination, | |
[ValidateSet('public','test')][System.String] | |
$version = 'public', | |
[switch]$force | |
) | |
begin { | |
$Manifest = Get-StarCitizenManifest | |
switch ($version) { | |
public {$fileList = $Manifest.Public_fileIndex} | |
test {$fileList = $Manifest.Test_fileIndex} | |
} | |
if (!(Test-Path $destination -PathType Container)) { | |
Write-Warning 'Directory does not exist. Creating it now!' | |
try { | |
New-Item -Path $destination -ItemType Directory | |
} | |
catch { | |
Write-Error "Creating $destination failed." | |
break | |
} | |
} | |
Write-Verbose "Grabbing File Listing for v$((Split-Path $fileList -Leaf).SubString(0,6))" | |
$fileData = Invoke-WebRequest $fileList | ConvertFrom-Json | |
$server = ($fileData.webseed_urls | Get-Random) + '/' + ($fileData.key_prefix) | |
if ((Get-PSDrive ($destination.Substring(0,1)) -OutVariable $null).Free -lt $fileData.byte_count_total) { | |
if (!$force) { | |
Write-Warning 'Not enough room to download this on the targeted drive. Choose another location, or run with -force!' | |
break | |
} | |
} | |
} | |
process { | |
foreach ($file in $fileData.file_list) { | |
$file = $file | Select @{name='local';expression={Join-Path $destination $_}},@{name='url';expression={"$server/$_"}} | |
$i++ | |
Write-Progress -Activity 'Downloading Star Citizen' -CurrentOperation "Downloading file $i/$($fileData.file_count_total) [$($file.local)]" -Id 0 -ParentId -1 -PercentComplete (100*($i)/$fileData.file_count_total-1) | |
if($PSCmdlet.ShouldProcess($file.url, 'Download')) { | |
try { | |
if (!(Test-Path (Split-Path $file.local -Parent))) {New-Item (Split-Path $file.local -Parent) -ItemType Directory | Out-Null} | |
if (!(Test-Path $file.local)) {Invoke-WebRequest -Uri $file.url -OutFile $file.local} | |
} | |
catch { | |
Write-Error "$($file.url) failed to download." | |
} | |
} | |
} | |
} | |
} | |
if ($MyInvocation.InvocationName -NE '.') {Get-StarCitizenFiles} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment