build-portable.ps1 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # 创建 SteamSH 便携式自解压包
  2. # 将 exe 打包成双击即可运行的便携版
  3. param(
  4. [string]$SourceExe = "steamSH_standalone.exe",
  5. [string]$OutputFile = "SteamSH_Portable.exe"
  6. )
  7. $ErrorActionPreference = "Stop"
  8. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  9. Write-Host "========================================" -ForegroundColor Cyan
  10. Write-Host "SteamSH 便携版打包工具" -ForegroundColor Cyan
  11. Write-Host "========================================" -ForegroundColor Cyan
  12. # 检查输入文件
  13. Write-Host "`n[1/4] 检查输入文件..." -ForegroundColor Yellow
  14. if (-not (Test-Path $SourceExe)) {
  15. Write-Host "错误: 找不到源文件 $SourceExe" -ForegroundColor Red
  16. Write-Host "请先运行构建脚本生成 exe 文件" -ForegroundColor Yellow
  17. exit 1
  18. }
  19. Write-Host " ✓ 找到源文件: $SourceExe" -ForegroundColor Green
  20. # 创建临时目录
  21. Write-Host "`n[2/4] 准备便携包内容..." -ForegroundColor Yellow
  22. $tempDir = Join-Path $env:TEMP "SteamSH_Portable_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
  23. New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
  24. # 复制 exe
  25. Copy-Item $SourceExe (Join-Path $tempDir "steamSH.exe") -Force
  26. Write-Host " ✓ 已复制主程序" -ForegroundColor Green
  27. # 创建启动脚本
  28. $startBat = @'
  29. @echo off
  30. chcp 65001 >nul
  31. echo 正在启动 SteamSH...
  32. cd /d "%~dp0"
  33. start "" "steamSH.exe"
  34. '@
  35. $startBat | Out-File (Join-Path $tempDir "启动SteamSH.bat") -Encoding UTF8
  36. Write-Host " ✓ 已创建启动脚本" -ForegroundColor Green
  37. # 创建说明文件
  38. $readme = @'
  39. SteamSH 便携版
  40. =============
  41. 使用说明:
  42. 1. 双击 "启动SteamSH.bat" 即可启动程序
  43. 2. 或直接运行 steamSH.exe
  44. 注意:
  45. - 这是独立可执行文件,无需安装任何依赖
  46. - 所有数据会存储在用户目录下
  47. - 可以直接复制到U盘或移动设备使用
  48. '@
  49. $readme | Out-File (Join-Path $tempDir "说明.txt") -Encoding UTF8
  50. Write-Host " ✓ 已创建说明文件" -ForegroundColor Green
  51. # 压缩为 zip
  52. Write-Host "`n[3/4] 创建压缩包..." -ForegroundColor Yellow
  53. $zipFile = "SteamSH_Portable.zip"
  54. if (Test-Path $zipFile) { Remove-Item $zipFile -Force }
  55. Compress-Archive -Path (Join-Path $tempDir "*") -DestinationPath $zipFile -Force
  56. Write-Host " ✓ 已创建 $zipFile" -ForegroundColor Green
  57. # 查找 7z SFX 模块
  58. Write-Host "`n[4/4] 创建自解压 exe..." -ForegroundColor Yellow
  59. $sfxModule = $null
  60. $possiblePaths = @(
  61. "C:\Program Files\7-Zip\7zSD.sfx",
  62. "C:\Program Files\2345Soft\HaoZip\sfx\HaoZip7zSetup.sfx",
  63. "C:\Program Files\2345Soft\HaoZip\sfx\HaoZip7zCon.sfx"
  64. )
  65. foreach ($path in $possiblePaths) {
  66. if (Test-Path $path) {
  67. $sfxModule = $path
  68. Write-Host " ✓ 找到 SFX 模块: $sfxModule" -ForegroundColor Green
  69. break
  70. }
  71. }
  72. if (-not $sfxModule) {
  73. Write-Host "`n警告: 未找到 7-Zip SFX 模块" -ForegroundColor Yellow
  74. Write-Host "将只生成 ZIP 压缩包" -ForegroundColor Yellow
  75. Write-Host "`n提示: 安装 7-Zip 后可创建自解压 exe" -ForegroundColor Cyan
  76. Write-Host "下载地址: https://www.7-zip.org/" -ForegroundColor Cyan
  77. exit 0
  78. }
  79. # 拼接 SFX
  80. try {
  81. $sfxBytes = [System.IO.File]::ReadAllBytes($sfxModule)
  82. $zipBytes = [System.IO.File]::ReadAllBytes($zipFile)
  83. $outputBytes = $sfxBytes + $zipBytes
  84. [System.IO.File]::WriteAllBytes($OutputFile, $outputBytes)
  85. $sizeMB = [math]::Round((Get-Item $OutputFile).Length / 1MB, 2)
  86. Write-Host "`n========================================" -ForegroundColor Green
  87. Write-Host "便携包构建完成!" -ForegroundColor Green
  88. Write-Host "========================================" -ForegroundColor Green
  89. Write-Host "`n输出文件:" -ForegroundColor White
  90. Write-Host " ZIP 包: $zipFile ($([math]::Round((Get-Item $zipFile).Length / 1MB, 2)) MB)" -ForegroundColor Cyan
  91. Write-Host " 自解压 exe: $OutputFile ($sizeMB MB)" -ForegroundColor Green
  92. Write-Host "`n分发方式:" -ForegroundColor White
  93. Write-Host " 1. 直接分发 $OutputFile 给用户" -ForegroundColor White
  94. Write-Host " 2. 用户双击即可自动解压并启动" -ForegroundColor White
  95. } catch {
  96. Write-Host "错误: 创建 SFX 失败 - $_" -ForegroundColor Red
  97. exit 1
  98. }
  99. # 清理临时文件
  100. Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue