-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecover_official_store_then_repatch.ps1
More file actions
271 lines (230 loc) · 9.11 KB
/
Copy pathrecover_official_store_then_repatch.ps1
File metadata and controls
271 lines (230 loc) · 9.11 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
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
param(
[string]$WorkspaceRoot,
[string]$StoreProductId = '9PLM9XGG6VKS',
[string]$BackupRoot,
[string]$LogPath,
[string]$RestoreManifestPath
)
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
if (-not $WorkspaceRoot) {
$WorkspaceRoot = Join-Path $repoRoot 'work\official_app_update'
}
if (-not $BackupRoot) {
$BackupRoot = Join-Path $repoRoot ("work\official_store_recovery\backup_" + (Get-Date -Format 'yyyy-MM-dd_HHmmss'))
}
if (-not $LogPath) {
$LogPath = Join-Path $repoRoot 'work\official_store_recovery\recover_official_store_then_repatch.log'
}
New-Item -ItemType Directory -Force (Split-Path -Parent $LogPath) | Out-Null
New-Item -ItemType Directory -Force $BackupRoot | Out-Null
function Write-Log {
param([string]$Message)
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff'
Add-Content -Path $LogPath -Value "$timestamp $Message"
}
function Invoke-RobocopyMirror {
param(
[string]$Source,
[string]$Destination
)
if (-not (Test-Path $Source)) {
Write-Log "backup-skip missing=$Source"
return
}
New-Item -ItemType Directory -Force $Destination | Out-Null
robocopy $Source $Destination /MIR /R:1 /W:1 /NFL /NDL /NJH /NJS /NP | Out-Null
$exitCode = $LASTEXITCODE
if ($exitCode -gt 7) {
throw "robocopy failed source=$Source destination=$Destination exit=$exitCode"
}
Write-Log "backup-ok source=$Source destination=$Destination exit=$exitCode"
}
function Stop-CodexProcesses {
$targets = Get-CodexProcesses
foreach ($proc in $targets) {
try {
Stop-Process -Id $proc.ProcessId -Force -ErrorAction Stop
Write-Log "stopped pid=$($proc.ProcessId) path=$($proc.ExecutablePath)"
}
catch {
Write-Log "stop-failed pid=$($proc.ProcessId) path=$($proc.ExecutablePath) error=$($_.Exception.Message)"
}
}
}
function Get-CodexProcesses {
Get-CimInstance Win32_Process -ErrorAction SilentlyContinue |
Where-Object {
$_.ExecutablePath -like '*\OpenAI.Codex_*\app\Codex.exe' -or
$_.ExecutablePath -like '*\OpenAI.Codex_*\app\resources\codex.exe' -or
$_.ExecutablePath -like '*\work\official_app*\*\Codex.exe' -or
$_.ExecutablePath -like '*\work\official_app*\*\resources\codex.exe'
}
}
function Wait-ForNoCodexProcesses {
param([int]$TimeoutSeconds = 30)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
do {
$running = @(Get-CodexProcesses)
if ($running.Count -eq 0) {
return $true
}
Start-Sleep -Milliseconds 500
} while ((Get-Date) -lt $deadline)
return $false
}
function Wait-ForPackageRemoval {
param([int]$TimeoutSeconds = 60)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
do {
$pkg = Get-AppxPackage -Name 'OpenAI.Codex' -ErrorAction SilentlyContinue
if ($null -eq $pkg) {
return $true
}
Start-Sleep -Seconds 1
} while ((Get-Date) -lt $deadline)
return $false
}
function Wait-ForOfficialPackageInstall {
param([int]$TimeoutSeconds = 180)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
do {
$pkg = Get-AppxPackage -Name 'OpenAI.Codex' -ErrorAction SilentlyContinue | Sort-Object Version -Descending | Select-Object -First 1
if ($null -ne $pkg) {
$installLocation = [string]$pkg.InstallLocation
if ($pkg.SignatureKind -ne 'None' -or $installLocation -like '*\WindowsApps\*' -or -not $pkg.IsDevelopmentMode) {
return $pkg
}
}
Start-Sleep -Seconds 2
} while ((Get-Date) -lt $deadline)
return $null
}
function Get-NextDeploySubdir {
param([string]$Root)
$deployRoot = Join-Path $Root 'deploy'
if (-not (Test-Path $deployRoot)) {
return 'package_root_telegram_registered_v1'
}
$maxVersion = 0
foreach ($dir in Get-ChildItem $deployRoot -Directory -ErrorAction SilentlyContinue) {
if ($dir.Name -match '^package_root_telegram_registered_v(\d+)$') {
$version = [int]$Matches[1]
if ($version -gt $maxVersion) {
$maxVersion = $version
}
}
}
return ('package_root_telegram_registered_v{0}' -f ($maxVersion + 1))
}
function Get-LatestRegisteredManifestPath {
param([string]$Root)
$deployRoot = Join-Path $Root 'deploy'
if (-not (Test-Path $deployRoot)) {
return $null
}
$candidate = Get-ChildItem $deployRoot -Directory -ErrorAction SilentlyContinue |
Where-Object { $_.Name -match '^package_root_telegram_registered_v(\d+)$' } |
Sort-Object { [int]([regex]::Match($_.Name, '^package_root_telegram_registered_v(\d+)$').Groups[1].Value) } -Descending |
Select-Object -First 1
if ($null -eq $candidate) {
return $null
}
$manifestPath = Join-Path $candidate.FullName 'AppxManifest.xml'
if (Test-Path $manifestPath) {
return $manifestPath
}
return $null
}
function Restore-PreviousPatchedPackage {
if (-not (Test-Path $RestoreManifestPath)) {
throw "Restore manifest is unavailable: $RestoreManifestPath"
}
try {
$current = Get-AppxPackage -Name 'OpenAI.Codex' -ErrorAction SilentlyContinue | Sort-Object Version -Descending | Select-Object -First 1
if ($null -ne $current) {
Remove-AppxPackage -Package $current.PackageFullName -ErrorAction Stop
Write-Log "restore-removed-current package=$($current.PackageFullName)"
}
}
catch {
Write-Log "restore-remove-current-failed error=$($_.Exception.Message)"
}
try {
Add-AppxPackage -Register $RestoreManifestPath -ForceApplicationShutdown -ForceUpdateFromAnyVersion -ErrorAction Stop
Write-Log "restore-success manifest=$RestoreManifestPath"
}
catch {
Write-Log "restore-failed manifest=$RestoreManifestPath error=$($_.Exception.Message)"
}
}
if (-not $RestoreManifestPath) {
$RestoreManifestPath = Get-LatestRegisteredManifestPath -Root $WorkspaceRoot
}
try {
Write-Log "recover-start workspace=$WorkspaceRoot storeProduct=$StoreProductId backup=$BackupRoot"
if (-not $RestoreManifestPath -or -not (Test-Path $RestoreManifestPath)) {
throw "Rollback manifest unavailable before recovery start: $RestoreManifestPath"
}
Write-Log "restore-manifest manifest=$RestoreManifestPath"
Start-Sleep -Seconds 5
Stop-CodexProcesses
if (-not (Wait-ForNoCodexProcesses)) {
throw 'Timed out waiting for Codex desktop processes to stop before package removal.'
}
Write-Log 'pre-removal-process-stop-confirmed'
Invoke-RobocopyMirror -Source "$env:APPDATA\Codex" -Destination (Join-Path $BackupRoot 'Roaming_Codex')
Invoke-RobocopyMirror -Source "$env:LOCALAPPDATA\Packages\OpenAI.Codex_2p2nqsd0c76g0" -Destination (Join-Path $BackupRoot 'LocalPackage_OpenAI.Codex_2p2nqsd0c76g0')
$current = Get-AppxPackage -Name 'OpenAI.Codex' -ErrorAction SilentlyContinue | Sort-Object Version -Descending | Select-Object -First 1
if ($null -eq $current) {
throw 'OpenAI.Codex package was not found before removal.'
}
Write-Log "current-package fullName=$($current.PackageFullName) signature=$($current.SignatureKind) devMode=$($current.IsDevelopmentMode) install=$($current.InstallLocation)"
Remove-AppxPackage -Package $current.PackageFullName -ErrorAction Stop
Write-Log "removed-package fullName=$($current.PackageFullName)"
if (-not (Wait-ForPackageRemoval)) {
throw 'Timed out waiting for OpenAI.Codex removal.'
}
Write-Log 'removal-confirmed'
& winget install --id $StoreProductId --source msstore --accept-source-agreements --accept-package-agreements *>> $LogPath
if ($LASTEXITCODE -ne 0) {
throw "winget install failed exit=$LASTEXITCODE"
}
Write-Log "winget-install-finished product=$StoreProductId"
$official = Wait-ForOfficialPackageInstall
if ($null -eq $official) {
throw 'Timed out waiting for the official Store package installation.'
}
Write-Log "official-installed fullName=$($official.PackageFullName) signature=$($official.SignatureKind) devMode=$($official.IsDevelopmentMode) install=$($official.InstallLocation)"
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'run_official_telegram_update.ps1') `
-WorkspaceRoot $WorkspaceRoot *>> $LogPath
if ($LASTEXITCODE -ne 0) {
throw "run_official_telegram_update.ps1 failed exit=$LASTEXITCODE"
}
Write-Log 'telegram-stage-build-apply-complete'
Stop-CodexProcesses
if (-not (Wait-ForNoCodexProcesses)) {
throw 'Timed out waiting for Codex desktop processes to stop before package registration.'
}
Write-Log 'pre-registration-process-stop-confirmed'
$deploySubdir = Get-NextDeploySubdir -Root $WorkspaceRoot
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'register_official_telegram_package.ps1') `
-WorkspaceRoot $WorkspaceRoot `
-DeploySubdir $deploySubdir `
-ReplaceInstalled `
-StopRunningCodex `
-Launch *>> $LogPath
if ($LASTEXITCODE -ne 0) {
throw "register_official_telegram_package.ps1 failed exit=$LASTEXITCODE"
}
$final = Get-AppxPackage -Name 'OpenAI.Codex' -ErrorAction SilentlyContinue | Sort-Object Version -Descending | Select-Object -First 1
if ($null -eq $final) {
throw 'Final OpenAI.Codex package lookup failed.'
}
Write-Log "recover-success final=$($final.PackageFullName) install=$($final.InstallLocation)"
}
catch {
Write-Log "recover-error error=$($_.Exception.Message)"
Restore-PreviousPatchedPackage
throw
}