-
Notifications
You must be signed in to change notification settings - Fork 1
/
updater.ps1
119 lines (88 loc) · 3.05 KB
/
updater.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
119
#
# Copyright Heroes Share
# https://heroesshare.net
#
# Stop on all errors
$ErrorActionPreference = "Stop"
$AppDir = [Environment]::GetFolderPath('ApplicationData') + "\Heroes Share"
$LogFile = "$AppDir\updater.log"
# Add timestamps to transcript log outputs
filter LogLine {"[$(Get-Date -Format u)] $_"}
# Make sure application directory exists
if ( -not (Test-Path "$AppDir" -PathType Container) ) {
Start-Transcript -Path "$LogFile" -Append
Write-Output "Application directory missing: '$AppDir'. Quitting." | LogLine
Stop-Transcript
exit 1
}
# Check for version.txt
if ( -not ( Test-Path "$AppDir\version.txt" -PathType Leaf ) ) {
$Installed = "0.0.0000"
} else {
# Get local version
$Installed = Get-Content "$AppDir\version.txt" -Raw
}
# Get latest version from the website
$Latest = Invoke-RestMethod -Uri "https://heroesshare.net/clients/check/win"
if ( -not ("$Latest") -Or "$Latest" -eq "error" ) {
Start-Transcript -Path "$LogFile" -Append
Write-Output "Error loading version from website." | LogLine
Stop-Transcript
exit 2
}
# Compare versions
if ( "$Latest" -eq "$Installed" ) {
exit 0
}
# Start logging
Start-Transcript -Path "$LogFile" -Append
Write-Output "Latest version $Latest differs from current $Installed" | LogLine
# Download latest installer
$TmpFile = New-TemporaryFile
$client = New-Object System.Net.WebClient
$client.DownloadFile("https://heroesshare.net/clients/update/win", $TmpFile)
Write-Output "Download complete: $TmpFile" | LogLine
# Get correct hash from website
$Hash = Invoke-RestMethod -Uri "https://heroesshare.net/clients/hash/win"
# test it against download
$Test = (Get-FileHash $TmpFile.FullName -Algorithm MD5).Hash.ToLower()
if ( "$Test" -ne "$Hash" ) {
Write-Output "Hash on downloaded file is incorrect:" | LogLine
Write-Output "$Test versus $Hash" | LogLine
Remove-Item -Force "$TmpFile"
Stop-Transcript
exit 3
}
# Expand the archive
Rename-Item -Path "$TmpFile" -NewName "$($TmpFile).zip"
$TmpDir = Split-Path -Resolve -Path "$($TmpFile).zip"
Expand-Archive "$($TmpFile).zip" -DestinationPath "$TmpDir\HeroesShareLive" -Force
Remove-Item -Force "$($TmpFile).zip"
# Verify extraction
if ( -not ( Test-Path "$TmpDir\HeroesShareLive\Setup.bat" -PathType Leaf ) ) {
Write-Output "Archive missing Setup.bat. Try a manual update:" | LogLine
Write-Output "https://heroesshare.net/clients/install/win" | LogLine
Stop-Transcript
exit 4
}
# Run Setup.bat
& "$TmpDir\HeroesShareLive\Setup.bat"
$Result = $LastExitCode
Remove-Item "$TmpDir\HeroesShareLive" -Force -Recurse
# Verify installer succeeded
if ( $Result -ne 0 ) {
Write-Output "Installation failed. Try a manual update:" | LogLine
Write-Output "https://heroesshare.net/clients/install/win" | LogLine
exit 5
}
# Confirm new version
$Installed = Get-Content "$AppDir\version.txt" -Raw
if ( "$Latest" -ne "$Installed" ) {
Write-Output "Installation complete but version mismatch. Try a manual update:"
Write-Output "https://heroesshare.net/clients/install/win"
exit 6
}
Write-Output "Installation complete! Updated to $Latest"
# Stop logging
Stop-Transcript
exit 0