install.ps1 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. param(
  2. [Parameter(Mandatory=$true)]
  3. [string]$SteamUser,
  4. [Parameter(Mandatory=$true)]
  5. [string]$SteamPass
  6. )
  7. Set-ExecutionPolicy Bypass -Scope Process -Force
  8. $scriptDir = if ($PSScriptRoot) { $PSScriptRoot }
  9. else { Split-Path -Parent $MyInvocation.MyCommand.Definition }
  10. if (-not $scriptDir) { $scriptDir = Get-Location }
  11. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  12. $appId = "223350"
  13. $installDir = Join-Path $scriptDir "game_server"
  14. $vcRedistUrl = "https://aka.ms/vs/17/release/vc_redist.x64.exe"
  15. $vcRedistPath = Join-Path -Path $env:TEMP -ChildPath "vc_redist_x64.exe"
  16. try {
  17. Write-Host "正在下载 VC++ 运行库..."
  18. (New-Object System.Net.WebClient).DownloadFile($vcRedistUrl, $vcRedistPath)
  19. if (Test-Path $vcRedistPath) {
  20. Write-Host "正在静默安装 VC++ 运行库..."
  21. $installProcess = Start-Process -FilePath $vcRedistPath `
  22. -ArgumentList "/install /quiet /norestart" `
  23. -Wait -NoNewWindow -PassThru
  24. Remove-Item $vcRedistPath -Force
  25. }
  26. }
  27. catch {
  28. Write-Error "VC++安装失败: $_"
  29. exit 1
  30. }
  31. $steamcmdDir = Join-Path $scriptDir "steamcmd"
  32. $steamcmdUrl = "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip"
  33. $steamcmdZip = Join-Path $steamcmdDir "steamcmd.zip"
  34. try {
  35. if (-not (Test-Path $steamcmdDir)) {
  36. New-Item -Path $steamcmdDir -ItemType Directory -Force | Out-Null
  37. }
  38. Write-Host "正在下载 SteamCMD..."
  39. (New-Object System.Net.WebClient).DownloadFile($steamcmdUrl, $steamcmdZip)
  40. if (Test-Path $steamcmdZip) {
  41. Write-Host "正在解压 SteamCMD..."
  42. Expand-Archive -Path $steamcmdZip -DestinationPath $steamcmdDir -Force
  43. Remove-Item $steamcmdZip -Force
  44. }
  45. }
  46. catch {
  47. Write-Error "SteamCMD操作失败: $_"
  48. exit 1
  49. }
  50. $steamcmdExe = Join-Path $steamcmdDir "steamcmd.exe"
  51. try {
  52. if (Test-Path $steamcmdExe) {
  53. Write-Host "正在更新游戏服务器 (AppID: $appId)..."
  54. $arguments = @(
  55. "+force_install_dir `"$installDir`"",
  56. "+login `"$SteamUser`" `"$SteamPass`"",
  57. "+app_update $appId validate",
  58. "+quit"
  59. )
  60. $process = Start-Process -FilePath $steamcmdExe `
  61. -ArgumentList $arguments `
  62. -Wait -NoNewWindow -PassThru `
  63. }
  64. else {
  65. throw "SteamCMD可执行文件未找到: $steamcmdExe"
  66. }
  67. }
  68. catch {
  69. Write-Error "游戏更新失败: $_"
  70. exit 1
  71. }
  72. $updateBatPath = Join-Path $scriptDir "更新服务器.bat"
  73. $batContent = @"
  74. @echo off
  75. set STEAMCMD_PATH="$steamcmdDir"
  76. set STEAM_USER=$SteamUser
  77. set STEAM_PASS=$SteamPass
  78. set INSTALL_DIR="$installDir"
  79. set APP_ID=$appId
  80. cd /d %STEAMCMD_PATH%
  81. steamcmd.exe +login %STEAM_USER% %STEAM_PASS% +force_install_dir %INSTALL_DIR% +app_update %APP_ID% validate +quit
  82. echo 服务器已更新完成!
  83. pause
  84. "@
  85. $batContent | Out-File -FilePath $updateBatPath -Encoding Default
  86. Write-Host "已创建更新脚本: $updateBatPath" -ForegroundColor Cyan
  87. Write-Host "双击此文件可一键更新服务器" -ForegroundColor Green
  88. Write-Host "`n所有操作已完成!" -ForegroundColor Green