summaryrefslogtreecommitdiffhomepage
path: root/dist/update.bat
blob: 5deb2699f105be643d6d618198e903227f4942d2 (plain)
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
<# : This script file is dedicated to the public domain. SST itself is not.
:: Note: the control flow in this file is pretty wacky, and for that, I apologise.
@echo off
pushd "%~dp0"
powershell -WindowStyle hidden -NoProfile -NoLogo -ExecutionPolicy bypass "iex (${%~f0} | out-string)"
if exist .sst-update.zip (
	del .sst-update.zip
	if exist sst-postupdate.bat (
		call sst-postupdate.bat
		del sst-postupdate.bat
	)
	:: if updated from game launch script, plugin can find out and display a message
	set SST_UPDATED=1
)
popd
goto :eof
#>
$ProgressPreference = "SilentlyContinue"
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') >$null
$url = [IO.File]::ReadAllText("update-url").Trim()
$realurl = $url
$origurl = $url
$h = @{}
try {
	$etag = [IO.File]::ReadAllText("update-version")
	$h["If-None-Match"] = $etag
}
catch {}
function errorbox {
	[System.Windows.Forms.MessageBox]::Show('Failed to check for SST updates. You may or may not be up to date!', 'Update error') >$null
}
for ($i = 0; $i -lt 10; $i++) {
	$r = Invoke-WebRequest -UseBasicParsing -UserAgent "sstupdate v2" -Uri $url -OutFile ".sst-update.zip" -PassThru -Headers $h -MaximumRedirection 0 -ErrorAction SilentlyContinue
	if ($r.StatusCode -eq 301) {
		$url = $r.Headers.Location
		$realurl = $url
		continue
	}
	if ($r.StatusCode -eq 302) { # don't immediately save a Found.
		$url = $r.Headers.Location
		continue
	}
	if ($r.StatusCode -eq 200 -or $r.StatusCode -eq 304) {
		if ($r.Headers.ETag) { [IO.File]::WriteAllText("update-version", $r.Headers.ETag) }
		if ($realurl -ne $origurl) { [IO.File]::WriteAllText("update-url", $realurl) }
		if ($r.StatusCode -eq 200) { Expand-Archive -Force -DestinationPath . .sst-update.zip }
		else { del .sst-update.zip } # stupid but oh well
		exit
	}
	errorbox
	exit
}
errorbox # too many redirects