-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetroArchCoreUpdater.ps1
66 lines (55 loc) · 2.42 KB
/
RetroArchCoreUpdater.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Param(
[Parameter(Mandatory=$true)]
[Uri]
$CoreUpdaterUrl,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[String]
$RetroArchPath
)
$WorkingDir = ".\working"
If (-NOT (Test-Path $WorkingDir -PathType Container)) {
Write-Output "Creating Working Directory $WorkingDir"
New-Item $WorkingDir -ItemType Directory
} Else {
Write-Output "Cleaning Working Directory"
Remove-Item $WorkingDir\*.*
}
#Check cores directory exists
$CoresDir = "$RetroArchPath\cores"
If (-NOT (Test-Path $CoresDir -PathType Container)) {
Write-Output "Unable to find cores directory ($CoresDir). Exiting!"
exit
}
$Cores = Get-ChildItem -Path $CoresDir -File -Filter *.dll
#Check cores exist
If ($Cores.Length -eq 0) {
Write-Output "No cores found in $CoresDir. Exiting!"
exit
}
Write-Output "Found $($Cores.Length) cores to update"
#Pull data from core updater URL
$CoresRequestUrl = "$($CoreUpdaterUrl.Scheme)://$($CoreUpdaterUrl.Host)/"
$CoresRequestBody = @{ action = "get"; items = @{ href = $CoreUpdaterUrl.AbsolutePath; what = 1;}}
$CoresResult = Invoke-RestMethod -ContentType application/json -Method POST -Uri $CoreUpdaterUrl -Body $(ConvertTo-Json $CoresRequestBody)
$RemoteCores = $CoresResult.items.Where({$_.href.StartsWith($CoreUpdaterUrl.AbsolutePath) -and $_.href.EndsWith(".zip")})
Write-Output "Successfully fetched cores from $CoreUpdaterUrl"
foreach($Core in $Cores) {
Write-Output "Updating $($Core.Name)"
$RemoteCore = $RemoteCores.Where({$_.href.Contains($Core.Name)})
$CoreZipFileName = "$($Core.Name).zip"
if(-NOT $RemoteCore.href -match $CoreZipFileName) {
Write-Output "Warning - Didn't find $CoreZipFileName in $($RemoteCore.href). Did libretro change file naming scheme? Skipping Core"
continue
}
$CoreZipUrl = "$($CoreUpdaterUrl.Scheme)://$($CoreUpdaterUrl.Host)$($RemoteCore.href)"
Write-Output "Downloading Core from $CoreZipUrl"
Invoke-WebRequest $CoreZipUrl -Method Get -OutFile "$WorkingDir\$CoreZipFileName"
Write-Output "Unzipping to $WorkingDir"
Expand-Archive "$WorkingDir\$CoreZipFileName" -DestinationPath $WorkingDir
Write-Output "Moving $WorkingDir\$($Core.Name) to $CoresDir"
Move-Item -Path "$WorkingDir\$($Core.Name)" -Destination $CoresDir -Force
Write-Output "Finished updating $($Core.Name)"
}
Write-Output "Deleting Working Directory $WorkingDir"
Remove-Item $WorkingDir -Recurse