-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprophunt.ps1
581 lines (463 loc) · 18.1 KB
/
prophunt.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# Set directory to script directory
$scriptRoot = $PSScriptRoot
Set-Location $scriptRoot | out-null
# Options
$opt_reroll = $true
$opt_menu = $true
# Directories
$dir_downloads = join-path $scriptRoot "downloads"
$dir_tools = join-path $scriptRoot "tools"
$dir_logs = join-path $scriptRoot "logs"
$dir_tf2 = join-path $scriptRoot "tf2"
$dir_temp = join-path $scriptRoot "temp"
$dir_7zip = join-path $dir_tools "7zip"
$dir_steamcmd = join-path $dir_tools "steam"
$dir_temp_mms = join-path $dir_temp "mms"
$dir_mms_dest = join-path $dir_tf2 "tf"
$dir_temp_sm = join-path $dir_temp "sm"
$dir_sm_dest = join-path $dir_tf2 "tf"
$dir_temp_tf2items = join-path $dir_temp "tf2items"
$dir_tf2items_dest = join-path $dir_tf2 "tf"
$dir_temp_st = join-path $dir_temp "steamtools"
$dir_st_dest = join-path $dir_tf2 "tf"
$dir_temp_dh = join-path $dir_temp "dhooks"
$dir_dh_dest = join-path $dir_tf2 "tf"
$dir_gamedata_dest = join-path $dir_tf2 "tf/addons/sourcemod/gamedata/"
$dir_downloads_maps_ftproot = join-path $dir_downloads "maps"
$dir_downloads_maps = join-path $dir_downloads_maps_ftproot "maps"
$dir_maps_dest = join-path $dir_tf2 "tf/custom/prophunt/maps/"
$dir_sound_dest = join-path $dir_tf2 "tf/custom/prophunt/sound/"
$dir_temp_maps = join-path $dir_temp "maps"
$dir_temp_sound = join-path $dir_temp "soundpack"
$dir_temp_ph = join-path $dir_temp "prophunt"
$dir_ph_dest = join-path $dir_tf2 "tf"
$dir_sm_plugins = join-path $dir_tf2 "tf/addons/sourcemod/plugins"
$dir_sm_plugins_disabled = join-path $dir_tf2 "tf/addons/sourcemod/plugins/disabled"
$dirs_create = @( $dir_downloads, $dir_tools, $dir_logs, $dir_tf2, $dir_temp, $dir_downloads_maps_ftproot, $dir_downloads_maps )
Function VerifyDir([string]$dir)
{
if ((Test-Path $dir) -ne $true) {
New-Item $dir -type directory | out-null
}
}
$dirs_create | Foreach { VerifyDir $_ }
# Files
$file_7z = join-path $dir_7zip "7za.exe"
$file_steamcmd = join-path $dir_steamcmd "steamcmd.exe"
$file_updatelog = join-path $dir_logs "steamcmd.log"
$file_servercfgsource = join-path $scriptRoot "server.cfg"
$file_servercfg = join-path $dir_tf2 "tf/cfg/server.cfg"
$file_ftpcfg = join-path $scriptRoot "ftp.cfg"
$file_log = join-path $dir_logs "update.log"
$file_mapcycle = join-path $dir_tf2 "tf/cfg/mapcycle.txt"
$file_arenacfg = join-path $dir_tf2 "tf/cfg/config_arena.cfg"
$file_phcfg = join-path $dir_tf2 "tf/cfg/sourcemod/prophunt_redux.cfg"
# Comment out plugins to not enable
# NOTE: removing a plugin from here won't uninstall it if already installed. Must do that manually for now.
$files_sm_plugins = @( "mapchooser.smx", "randomcycle.smx", "nominations.smx", "rockthevote.smx" )
$files_verify = @( $file_servercfgsource )
foreach ($file in $files_verify) { if ((Test-Path $file) -ne $true) { Write-Host "Error: server.cfg is missing"; exit } }
# Download locations
$url_7zip = "http://www.7-zip.org/a/7za920.zip"
$url_steamcmd = "http://media.steampowered.com/installer/steamcmd.zip"
$url_mms_versions = "https://wiki.alliedmods.net/Required_Versions_%28SourceMod%29"
$url_mms_home = "http://www.metamodsource.net/"
$url_mms_snapshots = "http://www.metamodsource.net/snapshots"
$url_sm_home = "http://www.sourcemod.net/"
$url_sm_downloads = "http://www.sourcemod.net/downloads.php"
$url_sm_snapshots = "http://www.sourcemod.net/snapshots.php"
$url_tf2items_home = "https://builds.limetech.org/"
$url_tf2items_downloads = "https://builds.limetech.org/?p=tf2items"
$url_steamtools_home = "https://builds.limetech.org/"
$url_steamtools_downloads = "https://builds.limetech.org/?p=steamtools"
$url_dhooks_home = "http://users.alliedmods.net/~drifter/builds/dhooks/2.0/"
$url_releases = "https://api.github.com/repos/powerlord/sourcemod-prophunt/releases"
# Ftp
$cred_ftp = $null
$ftp_subpath_maps = "/maps"
$ftp_subpath_sounds = "/sound"
# Flags
$exists_7zip = $false
# Regex
$regex_servercfgvar = "([a-zA-Z_]+) `"(.*)`""
$regex_cvar_reroll = "ph_propreroll .*"
$regex_cvar_menu = "ph_propmenu .*"
# File search patterns
$search_soundpack = "PHSoundPack.zip"
$search_maps = "*.bsp.bz2"
$search_ph = "prophunt-redux-*.zip"
$search_gamedata = "tf2-roundend.games.txt"
# Initialize log
New-Item $file_log -Type file -Force
Add-Content $file_log (Get-Date)
# Helper functions
Function Log([string]$message)
{
Add-Content $file_log $message
Write-Host $message
}
Add-Type -AssemblyName "system.io.compression.filesystem"
Function Unzip([string]$zipfile, [string]$destination)
{
if ($exists_7zip)
{
Seven-Zip x "$zipfile" -o"$destination" -y | out-null
}
elseif ($zipfile.EndsWith(".zip"))
{
if (Test-Path $destination)
{
# delete it
Remove-Item $destination -Force -Recurse
}
[io.compression.zipfile]::ExtractToDirectory($zipfile, $destination)
}
}
# Downloads a url, and returns whether or not it was downloaded
# Size can be used to verify that the server size ($size) matches the disk size of any cached file
Function Download([string]$url, [string]$logname, [parameter(Mandatory=$false)][long]$size = -1, [parameter(Mandatory=$false)][string]$path = $dir_downloads)
{
$outfilename = $url.split('/')[-1]
$outpath = join-path $path $outfilename
if (Test-Path $outpath)
{
$rval = [PSCustomObject]@{
Filename = $outpath
Downloaded = $false
}
if ($size -ne -1)
{
$filesize = (Get-Item $outpath).length
# Write-Host ("Disk({0}) Server({1})" -f $filesize,$size)
if ($size -eq $filesize)
{
Log ("File {0} matches server file" -f $logname)
return $rval
}
}
else
{
# Size wasn't specified, don't download again if filename matches
# NOTE: may want to do a [System.Net.WebRequest]::Create($url).GetResponse().ContentLength to check if size matches
# in a try{}finally{} block so response can call Dispose() within finally
return $rval
}
}
# Download
Log ("Downloading {0}..." -f $logname)
Invoke-WebRequest $url -OutFile $outpath
return [PSCustomObject]@{
Filename = $outpath
Downloaded = $true
}
}
Function DownloadCompressed([string]$url, [string]$destination, [string]$logname, [parameter(Mandatory=$false)][long]$size = -1)
{
$result = Download $url $logname -Size $size
if ($result.Downloaded -eq $true)
{
Unzip $result.Filename $destination
}
}
Function UnzipCopy([string]$file, [string]$tempdir, [string]$dest)
{
Unzip $file $tempdir
Get-ChildItem $tempdir | Foreach { Copy-Item $_.FullName $dest -Recurse -Force }
}
Function HttpGetFileSize([string]$url)
{
try
{
$result = (Invoke-WebRequest $url -Method HEAD).Headers
if ($result.ContainsKey("Content-Length"))
{
return $result["Content-Length"]
}
}
catch [System.Net.WebException]
{
return -1
}
}
Function FtpCreateDir([string]$dirpath)
{
try
{
# Path likely doesn't exist, create it
$mkdir = [System.Net.FtpWebRequest]([System.Net.FtpWebRequest]::Create($dirpath))
$mkdir.Credentials = $cred_ftp.Credentials
$mkdir.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory
$mkdir.KeepAlive = $false
$response = $mkdir.GetResponse()
$response.Close()
}
catch [Net.WebException]
{
}
}
Function FtpUploadFile([string]$localpath, [string]$serverpath)
{
# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $cred_ftp.Credentials
$webclient.UploadFile($serverpath, $localpath)
}
Function FtpUpload([string]$dir, [string]$ftppath = $null)
{
$ftpdir = $cred_ftp.Host
if ($ftppath -ne $null)
{
$ftpdir = $cred_ftp.Host + $ftppath
}
# Get directory listing
$rootpath = (Get-Item $dir).FullName
$dirs = Get-ChildItem $dir -Directory -Recurse | Foreach { $_.FullName.Substring($rootpath.Length + 1) }
# Make sure dirs exist in ftp
$dirs | Foreach {
Write-Host ("Ftp: Verifying ftp directory {0}..." -f $_)
FtpCreateDir($ftpdir + $_)
}
# Upload files
$files = Get-ChildItem $dir -File -Recurse
$files | Foreach {
$filepath = $_.FullName.Substring($rootpath.Length + 1)
$httpname = $url_quickdownload + $filepath
# NOTE: server must support HEAD request
$serversize = HttpGetFileSize($httpname)
$localsize = $_.Length
# TODO: offer "always upload" option
if ($serversize -ne $localsize)
{
# Upload the file
Write-Host ("Ftp: Uploading {0}..." -f $filepath)
$ftpname = $ftpdir + $filepath
FtpUploadFile $_.FullName $ftpname
}
else
{
Log ("Ftp: File {0} matches server file" -f $filepath)
}
}
}
Function ParseCfg([string]$file)
{
$cfg = @{}
Get-Content $file | Where-Object {
$_ -match $regex_servercfgvar
} | ForEach-Object {
$_ -match $regex_servercfgvar | out-null
$cfg.Add($Matches[1], $Matches[2])
}
return $cfg
}
# Install script
# Read and verify server config
Log "Verifying server config..."
if ((Test-Path $file_servercfgsource) -eq $false)
{
Throw "Server.cfg is missing."
}
$servercfg = ParseCfg $file_servercfgsource
if ($servercfg.ContainsKey("sv_downloadurl") -eq $true)
{
$url_quickdownload = $servercfg.Get_Item("sv_downloadurl")
if ((Test-Path $file_ftpcfg) -eq $false)
{
Throw ("Server.cfg specifies fast download url {0} but ftp.cfg is missing." -f $url_quickdownload)
}
$ftpcfg = ParseCfg $file_ftpcfg
$cred_ftp = [PSCustomObject]@{
Host = $ftpcfg.Get_Item("host")
Credentials = New-Object System.Net.NetworkCredential($ftpcfg.Get_Item("username"), $ftpcfg.Get_Item("password"))
}
}
# Prepare 7-zip
Log "Preparing Tools..."
$result = DownloadCompressed $url_7zip $dir_7zip "7zip"
set-alias Seven-Zip $file_7z
$exists_7zip = $true
# Prepare steamcmd
$result = DownloadCompressed $url_steamcmd $dir_steamcmd "steamcmd"
set-alias Steam-Cmd $file_steamcmd
# Update TF2 Install
Log "Updating TF2 Install..."
Steam-Cmd +login anonymous +force_install_dir "$dir_tf2" +app_update 232250 validate +quit | Tee-Object $file_updatelog
Log "Copying Server Config..."
Copy-Item $file_servercfgsource $file_servercfg -Force
# Find Metamod and Sourcemod Versions
Log "Finding Metamod:Source and SourceMod required versions..."
$result = Invoke-WebRequest $url_mms_versions
$versionInfo = ($result.ParsedHtml.getElementsByTagName("TR") | Where { $_.innerText.Contains("Team Fortress 2") }).Cells | Foreach { $_.innerText }
$version_mms = $versionInfo[1].Trim()
$version_sm = $versionInfo[2].Trim()
Log ("Versions: MM:S = {0}, SM = {1}" -f $version_mms, $version_sm)
if ($versionInfo.Count -gt 3)
{
# There is a note, log it
Log "Version note included:"
Log $versionInfo[3]
}
# Get Metamod
Log "Updating Metamod:Source..."
$url_mms = $null
if ($version_mms -eq "Current Stable")
{
# Must find current stable version
$result = Invoke-WebRequest $url_mms_home
$url_mms_mirrors = $url_mms_home + ($result.Links | Where { $_.innerText -match "MM:S .*, Windows" }).href
$result = Invoke-WebRequest $url_mms_mirrors
$url_mms = ($result.Links | Where { $_.innerText -ne $null -and $_.innerText.Contains("[wget]") } | Select -First 1).href
}
else
{
# Must find the link to the required version
$result = Invoke-WebRequest $url_mms_snapshots
$url_mms = ($result.Links | Where { $_.href.Contains($version_mms) -and $_.href.Contains("windows") } | Select -First 1).href
}
$result = Download $url_mms "Metamod:Source"
# Always uzip and copy, just in case an earlier update wiped it
UnzipCopy $result.Filename $dir_temp_mms $dir_mms_dest
# Get Sourcemod
Log "Updating SourceMod..."
$url_sm = $null
if ($version_sm -eq "Current Stable")
{
# Must find current stable version
$result = Invoke-WebRequest $url_sm_downloads
$url_sm_mirrors = $url_mms_home + ($result.Links | Where { $_.innerText -match "sourcemod-.*-windows.zip" }).href
$result = Invoke-WebRequest $url_sm_mirrors
$url_sm = ($result.Links | Where { $_.innerText -ne $null -and $_.innerText.Contains("[wget]") } | Select -First 1).href
}
else
{
# Must find the link to the required version
$result = Invoke-WebRequest $url_sm_snapshots
$url_sm = ($result.Links | Where { $_.href.Contains($version_sm) -and $_.href.Contains("windows") } | Select -First 1).href
}
$result = Download $url_sm "SourceMod"
# Always uzip and copy, just in case an earlier update wiped it
UnzipCopy $result.Filename $dir_temp_sm $dir_sm_dest
# Get TF2Items
Log "Updating TF2Items..."
$result = Invoke-WebRequest $url_tf2items_downloads
$url_tf2items = $url_tf2items_home + ($result.Links | Where { $_.href.Contains("windows") } | Select -First 1).href
$result = Download $url_tf2items "TF2Items"
# Always uzip and copy, just in case an earlier update wiped it
UnzipCopy $result.Filename $dir_temp_tf2items $dir_tf2items_dest
# Get SteamTools
Log "Updating SteamTools..."
$result = Invoke-WebRequest $url_steamtools_downloads
$url_st = $url_steamtools_home + ($result.Links | Where { $_.href.Contains("windows") } | Select -First 1).href
$result = Download $url_st "SteamTools"
# Always uzip and copy, just in case an earlier update wiped it
UnzipCopy $result.Filename $dir_temp_st $dir_st_dest
# Get DHooks
Log "Updating DHooks..."
$result = Invoke-WebRequest $url_dhooks_home
$url_dh = $url_dhooks_home + ($result.Links | Where { $_.href.Contains("windows") } | Select -First 1).href
$result = Download $url_dh "DHooks"
# Always uzip and copy, just in case an earlier update wiped it
UnzipCopy $result.Filename $dir_temp_dh $dir_dh_dest
# Get listing of released files
Log "Getting PropHunt file listing from Github..."
$releases = Invoke-RestMethod $url_releases
$releases_files = $releases | Foreach {
[PSCustomObject]@{ tag=$_.tag_name; assets=($_.assets | select name).name }
} | Foreach {
[PSCustomObject]@{ assets=$($tag=$_.tag; return ($_.assets | Foreach { [PSCustomObject]@{ tag=$tag; file=$_ } } ) ) }
} | Group file | Foreach {
[PSCustomObject]@{ file=$_.Name; tag=($_.Group | Select -First 1).tag }
}
Function Find-ReleaseFiles([string]$searchpattern)
{
return $releases_files | Where { $_.file -like $searchpattern } | Foreach {
$file = $_
$release = ($releases | Where { $_.tag_name -eq $file.tag } | Select -First 1)
$releasefile = ($release.assets | Where { $_.name -eq $file.file } | Select -First 1)
[PSCustomObject]@{
file=$file.file
tag=$file.tag
date=[DateTime]::Parse($release.published_at)
size=$releasefile.size
url=$releasefile.browser_download_url
}
} | Sort-Object -property date -descending
}
# Get Gamedata
Log "Updating Gamedata..."
$file_gamedata = Find-ReleaseFiles $search_gamedata
$result = Download $file_gamedata.url "Gamedata" -Size $file_gamedata.size
Copy-Item $result.Filename $dir_gamedata_dest -Force
# Install Prophunt Sound Pack
Log "Updating Sound Pack..."
$file_soundpack = (Find-ReleaseFiles $search_soundpack | Select -First 1)
$result = Download $file_soundpack.url "Sound Pack" -Size $file_soundpack.size
# Always uzip and copy, just in case an earlier update wiped it
# TODO: don't copy if files match
UnzipCopy $result.Filename $dir_temp_sound $dir_sound_dest
# Upload sounds to ftp
Log "Uploading Sounds..."
FtpUpload $dir_temp_sound
# Install Prophunt Maps
Log "Updating Maps..."
$files_map = Find-ReleaseFiles $search_maps
$files_map | Foreach {
$result = Download $_.url ("Map {0}" -f $_.file) $_.size $dir_downloads_maps
Unzip $result.Filename $dir_temp_maps
}
VerifyDir $dir_maps_dest
Get-ChildItem $dir_temp_maps | Foreach { Copy-Item $_.FullName $dir_maps_dest -Recurse -Force }
# Upload maps to ftp
Log "Uploading Maps..."
FtpUpload $dir_downloads_maps_ftproot
# Install Prophunt Redux
Log "Updating Prophunt Mod..."
$file_ph = (Find-ReleaseFiles $search_ph | Select -First 1)
$result = Download $file_ph.url "Prophunt Mod" -Size $file_ph.size
# Always uzip and copy, just in case an earlier update wiped it
UnzipCopy $result.Filename $dir_temp_ph $dir_ph_dest
# Update mapcycle (cfg/mapcycle.txt)
# Randomize map order
Log "Updating Mapcycle..."
$mapcycle = Get-ChildItem $dir_maps_dest | Foreach { $_.BaseName } | Sort-Object { Get-Random }
$mapcycle | Out-File $file_mapcycle
# Remove everything from cfg/config_arena.cfg to prevent server naming bug
Log "Fixing server name bug..."
"" | Out-File $file_arenacfg
# Set up prop-reroll menu
Log "Setting Prophunt cvar options..."
if (Test-Path $file_phcfg)
{
$cfglines = (Get-Content $file_phcfg)
$outlines = $cfglines | Foreach {
if ($opt_menu -and ($_ -match $regex_cvar_menu))
{
return "ph_propmenu `"1`""
}
elseif ($opt_reroll -and ($_ -match $regex_cvar_reroll))
{
return "ph_propreroll `"1`""
}
else
{
return $_
}
}
$outlines | Set-Content $file_phcfg
}
else
{
Log "Prophunt config file not found. Please run the server at least once to generate the file."
}
# Set up rock the vote and other plugins
$files_sm_plugins | Foreach {
$sm_plugin_file = join-path $dir_sm_plugins_disabled $_
if (Test-Path $sm_plugin_file)
{
Move-Item (Get-ChildItem $sm_plugin_file) $dir_sm_plugins -Force
}
}
# TODO: Test TF2 Server Ports to make sure firewall is open
# Clean up (optional)
#Log "Cleaning up..."
#Remove-Item $dir_temp -Force -Recurse$