-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.ps1
188 lines (185 loc) · 8.11 KB
/
update.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#
# Script Name: update.ps1
#
# Author: Lhoerion
#
# Description: The following script compares SHA-1 hashes of remote and local alt:V files. If local file is missing or outdated, script automatically downloads it to script directory.
# Old files are preserved as *.old. Script also keep track of current branch and build. Server start script gets created if missing.
#
# Run Information: This script is run manually.
# Dependencies: >=PowerShell 5.0
#
param(
[Alias("no-logfile")]
[Switch] $noLogFile,
[Alias("no-backup")]
[Switch] $noBackup,
[Alias("dry-run")]
[Switch] $dryRun,
[Switch] $silent
)
[System.Collections.ArrayList]$files=@()
function printAndLog($str, $type) {
$date=(Get-Date -UFormat "%T")
if($type -eq "ERR") {
"[$date][Error] $str" | %{if($noLogFile){$_}else{$_|Add-Content -Path "update.log" -NoNewline -PassThru}} | %{if(-not $silent) { $_ | Write-Host -NoNewline } else { $_ > $null }}
} elseif($type -eq "WARN") {
"[$date][Warning] $str" | %{if($noLogFile){$_}else{$_|Add-Content -Path "update.log" -NoNewline -PassThru}} | %{if(-not $silent) { $_ | Write-Host -NoNewline } else { $_ > $null }}
} elseif($type -eq "APP") {
"$str" | %{if($noLogFile){$_}else{$_|Add-Content -Path "update.log" -NoNewline -PassThru}} | %{if(-not $silent) { $_ | Write-Host -NoNewline } else { $_ > $null }}
} else {
"[$date] $str" | %{if($noLogFile){$_}else{$_|Add-Content -Path "update.log" -NoNewline -PassThru}} | %{if(-not $silent) { $_ | Write-Host -NoNewline } else { $_ > $null }}
}
}
function semVerCmp($verA, $verB) {
Set-Variable "semVerRegex" -Option "Constant" -Value $([System.Text.RegularExpressions.Regex]"^(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$")
$matchA = $semVerRegex.Match($verA.TrimStart('v'));
$matchB = $semVerRegex.Match($verB.TrimStart('v'));
if($verA -eq $verB) { return 0 }
if(-not $matchA.Groups[4].Success -and $matchB.Groups[4].Success) {
return 1
} elseif($matchA.Groups[4].Success -and -not $matchB.Groups[4].Success) {
return -1
}
for($i=1; $i -lt $matchA.Groups.Count; $i++) {
$a=$matchA.Groups[$i]
$b=$matchB.Groups[$i]
if(($i -eq 2) -and (-not $a.Success -or -not $b.Success)) { continue }
if($a.Value -gt $b.Value) {
return 1
} elseif($a.Value -lt $b.Value) {
return -1
}
}
return 0
}
function getFileHash($file) {
return (Get-FileHash -Path "$file" -Algorithm "SHA1").Hash
}
function fetchUpdateData() {
$hashTable=@{}
try {
$script:updateData=(Invoke-RestMethod -Uri "https://cdn.alt-mp.com/server/$localBranch/x64_win32/update.json" -UserAgent "AltPublicAgent")
$script:updateData.hashList.psobject.properties | %{ $hashTable[$_.Name]=@($_.Value, "server") }
$updateDataTmp=(Invoke-RestMethod -Uri "https://cdn.alt-mp.com/data/$localBranch/update.json" -UserAgent "AltPublicAgent")
$updateDataTmp.hashList.psobject.properties | %{ $hashTable[$_.Name]=@($_.Value, "data") }
} catch {
printAndLog "Failed to check for update, try again later`n" "ERR"
exit 1
}
foreach($moduleName in $modules) {
try {
if($moduleName -eq "csharp-module") { $moduleName="coreclr-module" }
$updateData2=(Invoke-RestMethod -Uri "https://cdn.alt-mp.com/$moduleName/$localBranch/x64_win32/update.json" -UserAgent "AltPublicAgent")
$updateData2.hashList.psobject.properties | %{ $hashTable[$_.Name]=@($_.Value,"$moduleName") }
} catch {
printAndLog "Failed to check for $moduleName update`n" "WARN"
}
}
$script:updateData.hashList=[pscustomobject]$hashTable
}
function validateFiles() {
$script:files.Clear()
$hashList=$script:updateData.hashList
foreach($file in ($hashList | Get-Member -MemberType NoteProperty).Name) {
if(-not (Test-Path -Path "$file") -or ('0'.PadRight(39, '0') -ne $hashList."$file"[0] -and (getFileHash "$file") -ne $hashList."$file"[0])) {
$script:files+=$file
}
}
if(-not (Test-Path -Path "server.cfg")) {
printAndLog "Server file server.cfg not found, creating one . . . "
if(-not $dryRun) {
$result=(New-Item -Path "server.cfg" -Value "name: 'alt:V Server'`nhost: 0.0.0.0`nport: 7788`nplayers: 128`n#password: ultra-password`nannounce: false`n#token: YOUR_TOKEN`ngamemode: Freeroam`nwebsite: example.com`nlanguage: en`ndescription: 'alt:V Sample Server'`nmodules: [`n `n]`nresources: [`n `n]`n" -Force)
if($result) {
printAndLog "done`n" "APP"
} else {
printAndLog "failed`n" "APP"
}
} else {
printAndLog "done`n" "APP"
}
}
if(($script:updateData.latestBuildNumber -is [string]) -or ($script:updateData.latestBuildNumber -ge 1232)) {
$nodeExist = Test-Path -Path "libnode.dll"
$moduleExist = Test-Path -Path "modules/node-module.dll"
if($nodeExist -or $moduleExist) {
printAndLog "Found old node-module files, removing . . . "
if(-not $dryRun) {
if($nodeExist) {
$result1=Remove-Item -Path "libnode.dll" -Force >$null
}
if($moduleExist) {
$result2=Remove-Item -Path "modules/node-module.dll" -Force >$null
}
if($result1 -and $result2) {
printAndLog "done`n" "APP"
} else {
printAndLog "failed`n" "APP"
}
} else {
printAndLog "done`n" "APP"
}
}
}
if($localBuild -ne $remoteBuild) {
printAndLog "Server files update is available`n"
} elseif($files.Count -ne 0) {
printAndLog "Server files are invalidated/corrupted, $($script:files.Count) in total`n"
} else {
printAndLog "Server files are up-to-date, no action required`n"
}
if(-not $dryRun) {
$localBuild=$remoteBuild
[ordered]@{branch=$localBranch;build=$localBuild;modules=$modules} | ConvertTo-Json | Out-File -FilePath "update.cfg" -Encoding "default"
}
}
function downloadFiles() {
if($script:files.Count -eq 0) { return }
foreach($file in $script:files) {
$dlType=($updateData.hashList."$file"[1])
$platform=if($dlType -ne "data") { "/x64_win32" } else { "" }
$outDir=(Split-Path -Path "$file" -Parent).Replace('/', '\')
if($outDir -eq "") { $outDir='.' }
printAndLog "Downloading file $file . . . "
if(-not $dryRun) {
if(-not $noBackup -and (Test-Path -Path "$file")) {
Move-Item -Path "$file" -Destination "$file.old" -Force >$null
}
if(-not (Test-Path -Path "$outDir")) {
New-Item -Path "$outDir" -ItemType "Directory" -Force >$null
}
$ProgressPreference="SilentlyContinue"
$result=(Invoke-WebRequest -Uri "https://cdn.alt-mp.com/$dlType/$localBranch$platform/${file}?build=$localBuild" -UserAgent "AltPublicAgent" -UseBasicParsing -OutFile "$file" -PassThru)
$ProgressPreference="Continue"
if($result.StatusCode -eq 200) {
printAndLog "done`n" "APP"
} else {
printAndLog "failed`n" "APP"
}
} else {
printAndLog "done`n" "APP"
}
}
validateFiles
}
if(-not $noLogFile) {
Clear-Content -Path "update.log" 2>&1 >$null
}
if(-not $dryRun -and -not (Test-Path "update.cfg")) {
New-Item "update.cfg" -Force >$null
[ordered]@{branch="release";modules=@("js-module")} | ConvertTo-Json | Out-File -FilePath "update.cfg" -Encoding "default"
}
$updateCfg=if(Test-Path "update.cfg") { $(Get-Content "update.cfg" | ConvertFrom-Json) } else { "{}" | ConvertFrom-Json }
$localBranch=$updateCfg.branch
if((-not $localBranch -or ($localBranch -ne "release")) -and ($localBranch -ne "rc") -and ($localBranch -ne "dev")) { $localBranch="release" }
$modules=$updateCfg.modules
if(-not $modules) { $modules=@("js-module") }
fetchUpdateData
$remoteBuild=$updateData.latestBuildNumber
if($updateData.latestBuildNumber -eq -1) { $remoteBuild=$updateData.version }
$localBuild=$updateCfg.build
if(-not $localBuild -or ($localBuild -eq -1)) { $localBuild=$remoteBuild }
printAndLog "Current version: $localBuild`n"
printAndLog "Latest version: $remoteBuild`n"
validateFiles
downloadFiles