-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathsetup-dev.ps1
More file actions
502 lines (424 loc) · 17.4 KB
/
Copy pathsetup-dev.ps1
File metadata and controls
502 lines (424 loc) · 17.4 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
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
$ErrorActionPreference = "Stop"
$script:Failures = @()
# ============================
# CONFIGURATION
# ============================
# vcpkg
$Triplet = "x64-uwp"
$VcpkgManifestMode = "on"
# SDK / Toolchain
$WindowsSDK = "10.0"
$ForceGenerator = $null # Default: "Visual Studio 17 2022"
# Certificate
$CertFileName = "moonlight-xbox-dx_TemporaryKey.pfx"
$CertSubject = "CN=MoonlightXbox"
$CertPassword = "moonlight"
function Step {
param([string]$Message)
Write-Host "`n=== $Message ===" -ForegroundColor Cyan
}
function Record-Failure {
param([string]$Message)
$script:Failures += $Message
Write-Host "❌ $Message" -ForegroundColor Red
}
function Try-Step {
param(
[string]$Name,
[scriptblock]$Action
)
Step $Name
try {
& $Action
}
catch {
Record-Failure "$Name failed: $($_.Exception.Message)"
}
}
function Run-IfNeeded {
param(
[string]$Name,
[scriptblock]$IsNeeded,
[scriptblock]$Action
)
Step $Name
try {
$needed = & $IsNeeded
}
catch {
# If the check fails for any reason, be conservative and run the step.
$needed = $true
}
if (-not $needed) {
Write-Host "Skipping — step not necessary." -ForegroundColor Yellow
return
}
try {
& $Action
}
catch {
Record-Failure "$Name failed: $($_.Exception.Message)"
}
}
# Simple yes/no prompt. Returns $true for yes, $false for no.
function Ask-YesNo {
param(
[string]$Question,
[bool]$DefaultYes = $true
)
$yesNo = if ($DefaultYes) { "Y/n" } else { "y/N" }
while ($true) {
$resp = Read-Host "$Question [$yesNo]"
if ([string]::IsNullOrWhiteSpace($resp)) { return $DefaultYes }
switch ($resp.ToLower()) {
'y' { return $true }
'yes' { return $true }
'n' { return $false }
'no' { return $false }
default { Write-Host "Please answer 'y' or 'n'." -ForegroundColor Yellow }
}
}
}
$script:RefreshCert = $false
$script:NeedsVSRefresh = $false
# --- VISUAL STUDIO DETECTION --------------------------------------------------
function Detect-VS {
Step "Detecting Visual Studio installation"
$programFilesX86 = [System.Environment]::GetEnvironmentVariable("ProgramFiles(x86)")
$vswhere = Join-Path $programFilesX86 "Microsoft Visual Studio\Installer\vswhere.exe"
if (-not (Test-Path $vswhere)) {
Record-Failure "vswhere.exe not found — Visual Studio Installer missing"
return $null
}
$vsJson = & $vswhere -latest -format json | ConvertFrom-Json
if (-not $vsJson) {
Record-Failure "No Visual Studio installation detected"
return $null
}
$installPath = $vsJson.installationPath
$version = $vsJson.catalog.productLineVersion
Write-Host "Found VS at: $installPath (version $version)" -ForegroundColor Green
# Required components
$required = @(
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64", # Desktop C++
"Microsoft.VisualStudio.ComponentGroup.UWP.VC", # UWP C++
"Microsoft.VisualStudio.Component.Windows10SDK.19041" # Windows 10 SDK
)
$missing = @()
foreach ($comp in $required) {
$found = & $vswhere -latest -requires $comp -property installationPath
if (-not $found) {
$missing += $comp
}
}
if ($missing.Count -gt 0) {
Write-Host "`n❌ Missing required Visual Studio components:" -ForegroundColor Red
foreach ($m in $missing) {
Write-Host " - $m" -ForegroundColor Red
}
Write-Host "`nTo fix this, open the Visual Studio Installer and enable:" -ForegroundColor Yellow
Write-Host " • Universal Windows Platform development" -ForegroundColor Yellow
Write-Host " • Desktop development with C++" -ForegroundColor Yellow
Write-Host " • Windows 10 SDK (10.0.19041 or newer)" -ForegroundColor Yellow
Record-Failure "Required Visual Studio components missing"
return $null
}
if ($ForceGenerator) {
Write-Host "`n⚙ Overriding generator: $ForceGenerator" -ForegroundColor Yellow
return $ForceGenerator
}
switch ($version) {
"2019" {
Write-Host "Using Visual Studio 2019 generator" -ForegroundColor Yellow
return "Visual Studio 16 2019"
}
"2022" {
Write-Host "Using Visual Studio 2022 generator" -ForegroundColor Yellow
return "Visual Studio 17 2022"
}
"2026" {
Write-Host "`n⚠ Visual Studio 2026 detected" -ForegroundColor Yellow
Write-Host " CMake does not yet provide a VS2026 generator." -ForegroundColor Yellow
Write-Host " This is normal — the MSVC toolchain is fully compatible." -ForegroundColor Yellow
Write-Host " Using the VS2022 generator to target the VS2026 toolchain." -ForegroundColor Yellow
return "Visual Studio 17 2022"
}
default {
Write-Host "`n⚠ Visual Studio version $version detected" -ForegroundColor Yellow
Write-Host " CMake does not provide a generator for this version." -ForegroundColor Yellow
Write-Host " Falling back to the VS2022 generator." -ForegroundColor Yellow
return "Visual Studio 17 2022"
}
}
}
$CMakeGenerator = Detect-VS
if ($CMakeGenerator) {
Write-Host "Using CMake generator: $CMakeGenerator" -ForegroundColor Yellow
}
# --- STEP 1: Validate environment ---------------------------------------------
Try-Step "Validating environment" {
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
throw "Git is not installed or not in PATH"
}
if (-not (Test-Path ".\vcpkg")) {
throw "vcpkg directory not found — did you clone with submodules?"
}
}
# --- STEP 2: Update submodules ------------------------------------------------
Run-IfNeeded "Updating git submodules" {
# Needed if any submodule is uninitialized or needs updating.
$out = git submodule status --recursive 2>&1
if ($LASTEXITCODE -ne 0) { return $true }
if (-not $out) { return $false }
if ($out -match '^-|^\+') { return $true }
return $false
} {
git submodule update --init --recursive
if ($LASTEXITCODE -ne 0) { throw "git submodule update failed" }
}
# --- STEP 3: Bootstrap vcpkg --------------------------------------------------
Run-IfNeeded "Bootstrapping vcpkg" {
return -not (Test-Path ".\vcpkg\vcpkg.exe")
} {
& .\vcpkg\bootstrap-vcpkg.bat
if ($LASTEXITCODE -ne 0) { throw "vcpkg bootstrap failed" }
}
# --- STEP 4: Install vcpkg packages ------------------------------------------
Run-IfNeeded "Installing vcpkg packages for $Triplet" {
$installed1 = Test-Path ".\vcpkg_installed\$Triplet"
$installed2 = Test-Path ".\vcpkg\installed\$Triplet"
return -not ($installed1 -or $installed2)
} {
& .\vcpkg\vcpkg.exe install --triplet $Triplet --clean-after-build
if ($LASTEXITCODE -ne 0) { throw "vcpkg install failed" }
}
# --- STEP 5: Generate third‑party projects ------------------------------------
Run-IfNeeded "Configuring moonlight-common-c" {
return -not (Test-Path "third_party/moonlight-common-c/build/CMakeCache.txt")
} {
$script:NeedsVSRefresh = $true
cmake -B "third_party/moonlight-common-c/build" `
-S "third_party/moonlight-common-c" `
-DCMAKE_TOOLCHAIN_FILE="$PSScriptRoot/vcpkg/scripts/buildsystems/vcpkg.cmake" `
-DVCPKG_TARGET_TRIPLET="$Triplet" `
-G "$CMakeGenerator" `
-DCMAKE_SYSTEM_NAME="WindowsStore" `
-DCMAKE_SYSTEM_VERSION="$WindowsSDK" `
-DTARGET_UWP=ON `
-DVCPKG_MANIFEST_MODE=$VcpkgManifestMode `
-DVCPKG_MANIFEST_DIR="."
if ($LASTEXITCODE -ne 0) { throw "moonlight-common-c CMake configuration failed" }
}
Run-IfNeeded "Configuring libgamestream" {
return -not (Test-Path "libgamestream/build/CMakeCache.txt")
} {
$script:NeedsVSRefresh = $true
cmake -B "libgamestream/build" `
-S "libgamestream" `
-DCMAKE_TOOLCHAIN_FILE="$PSScriptRoot/vcpkg/scripts/buildsystems/vcpkg.cmake" `
-DVCPKG_TARGET_TRIPLET="$Triplet" `
-G "$CMakeGenerator" `
-DCMAKE_SYSTEM_NAME="WindowsStore" `
-DCMAKE_SYSTEM_VERSION="$WindowsSDK" `
-DTARGET_UWP=ON `
-DBUILD_SHARED_LIBS=off `
-DVCPKG_MANIFEST_MODE=$VcpkgManifestMode `
-DVCPKG_MANIFEST_DIR="."
if ($LASTEXITCODE -ne 0) { throw "libgamestream CMake configuration failed" }
}
# --- STEP 6: Refresh Visual Studio project cache ------------------------------
Run-IfNeeded "Refreshing Visual Studio project cache" {
$vsRunning = Get-Process devenv -ErrorAction SilentlyContinue
if (-not $script:NeedsVSRefresh) {
return $false
}
return (Test-Path ".vs") -or $vsRunning
} {
$vsRunning = Get-Process devenv -ErrorAction SilentlyContinue
if ($vsRunning) {
Write-Host "⚠ Visual Studio is currently open — project reload will not take effect until you close and reopen the solution." -ForegroundColor Yellow
# Touch the solution file so VS reloads when reopened
$sln = Get-ChildItem -Filter *.sln | Select-Object -First 1
if ($sln) {
(Get-Item $sln.FullName).LastWriteTime = Get-Date
}
}
else {
# Safe to delete .vs folder
Remove-Item -Recurse -Force ".vs" -ErrorAction Ignore
}
}
# --- STEP 7: Generate UWP signing certificate ---------------------------------
Run-IfNeeded "Generating UWP signing certificate" {
$hasPfx = Test-Path "$PSScriptRoot\$CertFileName"
$hasCert = Get-ChildItem Cert:\CurrentUser\My | Where-Object { $_.Subject -eq $CertSubject }
if ($hasPfx -or $hasCert) {
# Ask user whether they want to refresh the existing cert
if (Ask-YesNo "A signing certificate already exists. Refresh it?" $false) {
$script:RefreshCert = $true
return $true
}
return $false
}
return $true
} {
$cert = New-SelfSignedCertificate `
-Type CodeSigningCert `
-Subject $CertSubject `
-CertStoreLocation "Cert:\CurrentUser\My"
$securePwd = ConvertTo-SecureString $CertPassword -AsPlainText -Force
Export-PfxCertificate `
-Cert $cert `
-FilePath "$PSScriptRoot\$CertFileName" `
-Password $securePwd
}
# --- STEP 8: Patch vcxproj ---------------------------------------------------
Run-IfNeeded "Updating .vcxproj with certificate info" {
if (-not $script:RefreshCert) { return $false }
$proj = Get-ChildItem -Recurse -Filter *.vcxproj | Select-Object -First 1
if (-not $proj) { return $true }
$content = Get-Content $proj.FullName -Raw
if ($content -match '<PackageCertificateKeyFile>$CertFileName</PackageCertificateKeyFile>' -and $content -match '<PackageCertificatePassword>moonlight</PackageCertificatePassword>') {
return $false
}
return $true
} {
$proj = Get-ChildItem -Recurse -Filter *.vcxproj | Select-Object -First 1
if (-not $proj) { throw "Could not find .vcxproj" }
$pfxPath = "$PSScriptRoot\$CertFileName"
$projXmlRaw = Get-Content $proj.FullName -Raw
$existingKeyFile = $null
if ($projXmlRaw -match '<PackageCertificateKeyFile>([^<]+)</PackageCertificateKeyFile>') { $existingKeyFile = $matches[1] }
if ($existingKeyFile) {
$existingKeyFull = Join-Path $PSScriptRoot $existingKeyFile
if (Test-Path $existingKeyFull) {
try {
$securePwd = ConvertTo-SecureString $CertPassword -AsPlainText -Force
$imported = Import-PfxCertificate -FilePath $existingKeyFull -CertStoreLocation Cert:\CurrentUser\My -Password $securePwd
if ($imported) {
Write-Host "Imported existing keyfile $existingKeyFile into certificate store" -ForegroundColor Green
$cert = $imported
}
}
catch {
Record-Failure "Failed to import existing keyfile ${existingKeyFile}: $($_.Exception.Message)"
}
}
}
$pfxCert = $null
if (Test-Path $pfxPath) {
try {
$pfxCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxPath, $CertPassword)
} catch { $pfxCert = $null }
}
$storeCert = Get-ChildItem Cert:\CurrentUser\My | Where-Object { $_.Subject -eq $CertSubject } | Select-Object -First 1
if ($script:RefreshCert) {
if (Test-Path $pfxPath) {
$securePwd = ConvertTo-SecureString $CertPassword -AsPlainText -Force
$cert = Import-PfxCertificate -FilePath $pfxPath -CertStoreLocation Cert:\CurrentUser\My -Password $securePwd
}
elseif (-not $storeCert) {
throw "No certificate or PFX available to import"
}
else {
$cert = $storeCert
}
}
else {
if ($projXmlRaw -match '<PackageCertificateThumbprint>([^<]+)</PackageCertificateThumbprint>') { $existingThumb = $matches[1] }
if ($pfxCert -and $storeCert) {
if ($pfxCert.Thumbprint -ne $storeCert.Thumbprint) {
if (Ask-YesNo "PFX file exists but does not match the certificate in the store. Import the PFX and update the project?" $false) {
$securePwd = ConvertTo-SecureString $CertPassword -AsPlainText -Force
$cert = Import-PfxCertificate -FilePath $pfxPath -CertStoreLocation Cert:\CurrentUser\My -Password $securePwd
}
else {
Record-Failure "Skipped updating .vcxproj due to PFX/store mismatch"
return
}
}
else {
$cert = $storeCert
}
}
elseif ($pfxCert -and -not $storeCert) {
$securePwd = ConvertTo-SecureString $CertPassword -AsPlainText -Force
$cert = Import-PfxCertificate -FilePath $pfxPath -CertStoreLocation Cert:\CurrentUser\My -Password $securePwd
}
elseif ($storeCert -and -not $pfxCert) {
$cert = $storeCert
}
else {
throw "No signing certificate found"
}
}
[xml]$xml = Get-Content $proj.FullName
$nsmgr = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$nsmgr.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003")
$pg = $xml.SelectSingleNode("//msb:PropertyGroup[@Label='UserMacros']", $nsmgr)
if (-not $pg) { throw "Could not find UserMacros PropertyGroup" }
$thumbNode = $pg.SelectSingleNode("msb:PackageCertificateThumbprint", $nsmgr)
if (-not $thumbNode) {
$thumbNode = $xml.CreateElement("PackageCertificateThumbprint", $nsmgr.LookupNamespace("msb"))
$pg.AppendChild($thumbNode) | Out-Null
}
$thumbNode.InnerText = $cert.Thumbprint
$keyFileNode = $pg.SelectSingleNode("msb:PackageCertificateKeyFile", $nsmgr)
if (-not $keyFileNode) {
$keyFileNode = $xml.CreateElement("PackageCertificateKeyFile", $nsmgr.LookupNamespace("msb"))
$pg.AppendChild($keyFileNode) | Out-Null
}
if (Test-Path $pfxPath) {
try { $pfxCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxPath, $CertPassword) } catch { $pfxCert = $null }
if ($pfxCert -and ($pfxCert.Thumbprint -eq $cert.Thumbprint)) {
$keyFileNode.InnerText = "$CertFileName"
}
else {
$keyFileNode.InnerText = ""
}
}
else {
$keyFileNode.InnerText = ""
}
$pwdNode = $pg.SelectSingleNode("msb:PackageCertificatePassword", $nsmgr)
if (-not $pwdNode) {
$pwdNode = $xml.CreateElement("PackageCertificatePassword", $nsmgr.LookupNamespace("msb"))
$pg.AppendChild($pwdNode) | Out-Null
}
$pwdNode.InnerText = "moonlight"
$xml.Save($proj.FullName)
}
# --- STEP 9: Patch appxmanifest ----------------------------------------------
Run-IfNeeded "Updating appxmanifest with certificate info" {
if (-not $script:RefreshCert) { return $false }
$manifestPath = "$PSScriptRoot\Package.appxmanifest"
if (-not (Test-Path $manifestPath)) { return $false }
[xml]$manifest = Get-Content $manifestPath
$pub = $manifest.Package.Identity.Publisher
$display = $manifest.Package.Properties.PublisherDisplayName
if (($pub -eq $CertSubject) -and ($display -eq 'MoonlightXbox')) { return $false }
return $true
} {
$manifestPath = "$PSScriptRoot\Package.appxmanifest"
if (Test-Path $manifestPath) {
[xml]$manifest = Get-Content $manifestPath
$manifest.Package.Identity.Publisher = "CN=MoonlightXbox"
$manifest.Package.Properties.PublisherDisplayName = "MoonlightXbox"
$manifest.Save($manifestPath)
}
}
# --- SUMMARY ------------------------------------------------------------------
Write-Host "`n====================================================="
Write-Host " SETUP SUMMARY"
Write-Host "====================================================="
if ($script:Failures.Count -gt 0) {
Write-Host "`n❌ The following errors occurred:" -ForegroundColor Red
foreach ($f in $script:Failures) {
Write-Host " - $f" -ForegroundColor Red
}
Write-Host "`nSetup completed with errors." -ForegroundColor Red
exit 1
}
else {
Write-Host "`n🎉 All steps completed successfully!" -ForegroundColor Green
}