-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate_LRZ.ps1
118 lines (94 loc) · 3.13 KB
/
Update_LRZ.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
param (
[Parameter(HelpMessage = "Do not prompt for any user input")]
[switch]$Silent = $True,
[Parameter(HelpMessage = "Clean unneeded files listed in _delete.txt after update")]
[switch]$Clean = $False,
[Parameter(HelpMessage = "Only update releases in the verified stream")]
[switch]$Verified = $False,
[Parameter(HelpMessage = "Directory to install to")]
[ValidateScript({
if (-Not($_ | Test-Path))
{
throw "File or folder does not exist"
} return $true
})]
[System.IO.FileInfo]$Directory
)
Write-Output "======================================="
Write-Output " LRZ++ Updater v1 "
Write-Output " by TheFantasticLoki "
Write-Output "======================================="
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
$repoName = "TheFantasticLoki/LokisRagnarok"
$assetPattern = "LokisRagnarok*.zip"
if ($Verified)
{
$releasesUri = "https://api.github.com/repos/$repoName/releases/latest"
}
else
{
$releasesUri = "https://api.github.com/repos/$repoName/releases"
}
Write-Output "Retrieving latest version info..."
$releaseInfo = (Invoke-WebRequest $releasesUri | ConvertFrom-Json) | Select -First 1
$asset = $releaseInfo.assets | Where-Object name -like $assetPattern | Select -First 1
$downloadUri = $asset.browser_download_url
$filename = Split-Path $downloadUri -leaf
Write-Output "The latest version is $( $releaseInfo.tag_name ) released $( $releaseInfo.published_at )"
if (!$Silent)
{
$stopwatch.Stop()
Write-Warning "All LRZ++ files will be updated. Are you sure you want to continue?" -WarningAction Inquire
$stopwatch.Start()
}
Write-Output "Downloading update. This might take a moment..."
$fileDownload = Invoke-WebRequest -Uri $downloadUri
if ($fileDownload.StatusDescription -ne "OK")
{
throw "Could not update LRZ++. ($fileDownload.StatusDescription)"
}
$remoteHash = $fileDownload.Headers['Content-MD5']
$decodedHash = [System.BitConverter]::ToString([System.Convert]::FromBase64String($remoteHash)).replace('-', '')
$directoryPath = Get-Location
$fullPath = "$directoryPath\$filename"
$outputFile = [System.IO.File]::Open($fullPath, 2)
$stream = [System.IO.BinaryWriter]::new($outputFile)
if ($Directory)
{
$outputDir = $Directory
}
else
{
$outputDir = Get-Location
}
try
{
$stream.Write($fileDownload.Content)
}
finally
{
$stream.Dispose()
$outputFile.Dispose()
}
$localHash = (Get-FileHash -Path $fullPath -Algorithm MD5).Hash
if ($localHash -ne $decodedHash)
{
throw "Failed to update. File hashes don't match!"
}
Write-Output "Extracting $filename to $outputDir"
Expand-Archive -Path $fullPath -DestinationPath $outputDir -Force
if ($Clean)
{
Write-Output "Running post-update clean..."
$DeleteList = Get-Content -Path ./_delete.txt
ForEach ($file in $DeleteList)
{
Write-Output "Deleting $file"
Remove-Item -Path $file
}
}
Write-Output "Removing temporary files..."
Remove-Item -Force $fullPath
$stopwatch.Stop()
$executionTime = [math]::Round($stopwatch.Elapsed.TotalSeconds, 0)
Write-Output "Update completed successfully in $executionTime seconds!"