-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgenerate_packages.ps1
More file actions
232 lines (203 loc) · 9.34 KB
/
Copy pathgenerate_packages.ps1
File metadata and controls
232 lines (203 loc) · 9.34 KB
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
# ================================================================
# GeneralUpdate Sample Server — Upgrade Package Generator
#
# Generates full (VersionChain) and differential (CrossVersion)
# upgrade packages from the sample content directories.
#
# Output: wwwroot/packages/*.zip + wwwroot/packages/versions.json
#
# For differential packages, this script invokes the PatchGenerator
# C# project when available; otherwise generates only full packages.
# ================================================================
param(
[switch]$FullOnly # Skip differential packages (fast mode)
)
$ErrorActionPreference = "Stop"
$srcDir = Split-Path -Parent $PSScriptRoot
$contentClientDir = Join-Path $srcDir "content_client"
$contentUpgradeDir = Join-Path $srcDir "content_upgrade"
$packagesDir = Join-Path $PSScriptRoot "wwwroot\packages"
$timestamp = Get-Date -Format "yyyyMMddHHmmssfff"
$baseUrl = "http://localhost:5000/"
$productId = "2d974e2a-31e6-4887-9bb1-b4689e98c77a"
# Ensure output directory exists
New-Item -ItemType Directory -Force -Path $packagesDir | Out-Null
# Clean old generated packages
Get-ChildItem -Path $packagesDir -Filter "packet_*.zip" | Remove-Item -Force
Write-Host "Cleaned old packages." -ForegroundColor Cyan
$allVersions = @()
# ===================================================================
# Helper functions
# ===================================================================
function Get-SHA256Hash($filePath) {
$stream = [System.IO.File]::OpenRead($filePath)
$sha = [System.Security.Cryptography.SHA256]::Create()
$hash = [System.BitConverter]::ToString($sha.ComputeHash($stream)).Replace("-", "").ToLowerInvariant()
$stream.Close()
return $hash
}
function New-VersionEntry {
param(
[string]$PacketName, [string]$Version, [int]$AppType,
[int]$Platform = 1, [string]$ProductId,
[bool]$IsForcibly = $false, [string]$Format = ".zip",
[int]$PackageType = 2, # 1=Chain(差分), 2=Full(完整包)
[bool]$IsFreeze = $false
)
$zipPath = Join-Path $packagesDir "$PacketName.zip"
$hash = Get-SHA256Hash $zipPath
$size = (Get-Item $zipPath).Length
return @{
PacketName = $PacketName
Hash = $hash
Version = $Version
Url = "$baseUrl`File/Download/$hash"
PubTime = (Get-Date).ToString("o")
AppType = $AppType
Platform = $Platform
ProductId = $ProductId
IsForcibly = $IsForcibly
Format = $Format
Size = $size
IsFreeze = $IsFreeze
PackageType = $PackageType
}
}
function Add-ZipPackage {
param(
[string]$SourceDir, [string]$PacketName,
[string]$Version, [int]$AppType,
[int]$PackageType = 2 # 1=Chain(差分), 2=Full(完整包)
)
$zipPath = Join-Path $packagesDir "$PacketName.zip"
Write-Host " Creating: $PacketName.zip" -ForegroundColor Yellow
if (Test-Path $zipPath) { Remove-Item $zipPath -Force }
# Use .NET for compression (available in PowerShell 5+)
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory(
$SourceDir, $zipPath,
[System.IO.Compression.CompressionLevel]::Optimal,
$false # includeBaseDirectory = false
)
$size = (Get-Item $zipPath).Length
Write-Host " Size: $size bytes" -ForegroundColor Gray
$entry = New-VersionEntry -PacketName $PacketName -Version $Version `
-AppType $AppType -ProductId $productId `
-PackageType $PackageType
$script:allVersions += $entry
}
# ===================================================================
# CLIENT packages (AppType=1)
# Current version: 1.0.0 → Target version: 2.0.0.0
# ===================================================================
Write-Host ""
Write-Host "===== CLIENT Packages (AppType=1) =====" -ForegroundColor Green
$clientV1 = Join-Path $contentClientDir "v1.0.0.0"
$clientV2 = Join-Path $contentClientDir "v2.0.0.0"
if ((Test-Path $clientV2) -and (Test-Path $clientV1)) {
# Full (VersionChain) package
$pkgName = "packet_${timestamp}_full_client_2.0.0.0"
Add-ZipPackage -SourceDir $clientV2 -PacketName $pkgName `
-Version "2.0.0.0" -AppType 1 -PackageType 2
if (-not $FullOnly) {
# Differential (CrossVersion) package via PatchGenerator
Write-Host " [CrossVersion] Generating differential patches via PatchGenerator..." -ForegroundColor Yellow
$patchGeneratorProj = Join-Path $srcDir "PatchGenerator\PatchGenerator.csproj"
if (Test-Path $patchGeneratorProj) {
Push-Location (Join-Path $srcDir "PatchGenerator")
try {
dotnet run --project $patchGeneratorProj
Write-Host " PatchGenerator completed." -ForegroundColor Gray
}
catch {
Write-Host " [Warn] PatchGenerator failed: $_" -ForegroundColor Magenta
Write-Host " Run 'dotnet run --project $patchGeneratorProj' manually for differential packages." -ForegroundColor Magenta
}
finally { Pop-Location }
# PatchGenerator writes directly to Server/wwwroot/packages/
# Also write its output to versions.json
$patchVersionsJson = Join-Path $packagesDir "versions.json"
if (Test-Path $patchVersionsJson) {
$patchEntries = Get-Content $patchVersionsJson -Raw | ConvertFrom-Json
foreach ($pe in $patchEntries) {
$script:allVersions += @{
PacketName = $pe.PacketName
Hash = $pe.Hash
Version = $pe.Version
Url = $pe.Url
PubTime = $pe.PubTime
AppType = $pe.AppType
Platform = $pe.Platform
ProductId = $pe.ProductId
IsForcibly = $pe.IsForcibly
Format = $pe.Format
Size = $pe.Size
IsFreeze = $pe.IsFreeze
PackageType = $(if ($pe.PackageType) { $pe.PackageType } elseif ($pe.IsCrossVersion) { 1 } else { 2 })
}
}
}
}
else {
Write-Host " [Skip] PatchGenerator project not found at $patchGeneratorProj" -ForegroundColor Yellow
}
}
}
else {
Write-Host " [Skip] content_client directories not found: $clientV1 or $clientV2" -ForegroundColor Yellow
}
# ===================================================================
# UPGRADE packages (AppType=2)
# Current version: 1.0.0.1 → Target version: 2.0.0.0
# ===================================================================
Write-Host ""
Write-Host "===== UPGRADE Packages (AppType=2) =====" -ForegroundColor Green
$upgradeV1 = Join-Path $contentUpgradeDir "v1.0.0.0"
$upgradeV2 = Join-Path $contentUpgradeDir "v2.0.0.0"
if ((Test-Path $upgradeV2) -and (Test-Path $upgradeV1)) {
# Full (VersionChain) package
$pkgName = "packet_${timestamp}_full_upgrade_2.0.0.0"
Add-ZipPackage -SourceDir $upgradeV2 -PacketName $pkgName `
-Version "2.0.0.0" -AppType 2 -PackageType 2
}
else {
Write-Host " [Skip] content_upgrade directories not found: $upgradeV1 or $upgradeV2" -ForegroundColor Yellow
}
# ===================================================================
# Also generate smaller mock packages from content/ dirs
# (for quick testing without large binary packages)
# ===================================================================
Write-Host ""
Write-Host "===== MOCK Content Packages =====" -ForegroundColor Green
$contentDir = Join-Path $srcDir "content"
foreach ($ver in @("1.0.0.1", "1.0.0.2")) {
$srcPath = Join-Path $contentDir "v$ver"
if (Test-Path $srcPath) {
# Client full package
$pkgName = "packet_${timestamp}_full_client_$ver"
Add-ZipPackage -SourceDir $srcPath -PacketName $pkgName `
-Version $ver -AppType 1 -PackageType 2
# Upgrade full package
$pkgName = "packet_${timestamp}_full_upgrade_$ver"
Add-ZipPackage -SourceDir $srcPath -PacketName $pkgName `
-Version $ver -AppType 2 -PackageType 2
}
}
# ===================================================================
# Write versions.json
# ===================================================================
# Deduplicate entries (PatchGenerator may have added some already)
$uniqueVersions = $allVersions | Sort-Object PacketName -Unique
$versionsJsonPath = Join-Path $packagesDir "versions.json"
$json = $uniqueVersions | ConvertTo-Json -Depth 10
[System.IO.File]::WriteAllText($versionsJsonPath, $json, [System.Text.Encoding]::UTF8)
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Generated $($uniqueVersions.Count) package(s)" -ForegroundColor Cyan
Write-Host " Output: $packagesDir" -ForegroundColor Cyan
Write-Host " Metadata: $versionsJsonPath" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
foreach ($v in $uniqueVersions) {
$cross = if ($v.PackageType -eq 1) { "Chain" } else { "Full" }
Write-Host " AppType=$($v.AppType) v$($v.Version) [$cross] $($v.PacketName)" -ForegroundColor Gray
}