-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUE5ShellConfiguration.ps1
More file actions
370 lines (334 loc) · 13 KB
/
Copy pathUE5ShellConfiguration.ps1
File metadata and controls
370 lines (334 loc) · 13 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
<#
.SYNOPSIS
Installs, uninstalls, and reports status for this repo's UE5 shell extension features.
.PARAMETER Feature
A feature Id from $Features to target. Omit to target all features.
.PARAMETER ScriptPath
Overrides auto-detection of the feature's script for -Install. Requires -Feature.
#>
param(
[switch]$Install,
[switch]$Uninstall,
[switch]$Status,
[string]$Feature,
[string]$ScriptPath
)
$MenuLabelSuffix = ' (SE)'
$Features = @(
[PSCustomObject]@{
Id = 'ClearCacheAndRebuild'
MenuLabel = 'Clear Cache and Rebuild'
RelativePath = 'ClearCacheAndRebuild\UE5ClearCache.ps1'
},
[PSCustomObject]@{
Id = 'Build'
MenuLabel = 'Build'
RelativePath = 'Build\UE5Build.ps1'
},
[PSCustomObject]@{
Id = 'RunHeadlessServer'
MenuLabel = 'Run Headless Server'
RelativePath = 'RunHeadlessServer\UE5RunHeadlessServer.ps1'
},
[PSCustomObject]@{
Id = 'CookContent'
MenuLabel = 'Cook Content'
RelativePath = 'CookContent\UE5CookContent.ps1'
},
[PSCustomObject]@{
Id = 'PackageProject'
MenuLabel = 'Package Project'
RelativePath = 'PackageProject\UE5PackageProject.ps1'
},
[PSCustomObject]@{
Id = 'RunAutomationTests'
MenuLabel = 'Run Automation Tests'
RelativePath = 'RunAutomationTests\UE5RunAutomationTests.ps1'
},
[PSCustomObject]@{
Id = 'ClearDDC'
MenuLabel = 'Clear Derived Data Cache'
RelativePath = 'ClearDDC\UE5ClearDDC.ps1'
},
[PSCustomObject]@{
Id = 'OpenSavedLogs'
MenuLabel = 'Open Saved Logs'
RelativePath = 'OpenSavedLogs\UE5OpenSavedLogs.ps1'
},
[PSCustomObject]@{
Id = 'OpenProjectTerminal'
MenuLabel = 'Open Terminal Here'
RelativePath = 'OpenProjectTerminal\UE5OpenProjectTerminal.ps1'
}
)
$RegRoot = 'HKCU:\Software\Classes\SystemFileAssociations\.uproject\shell'
function Get-FeatureRegKeyPath {
param($FeatureItem)
"$RegRoot\$($FeatureItem.Id)"
}
function Get-FeatureRegCommandPath {
param($FeatureItem)
"$(Get-FeatureRegKeyPath $FeatureItem)\command"
}
function Get-FeatureDefaultScriptPath {
param($FeatureItem)
Join-Path $PSScriptRoot $FeatureItem.RelativePath
}
function Test-FeatureInstalled {
param($FeatureItem)
Test-Path (Get-FeatureRegKeyPath $FeatureItem)
}
function Get-FeatureInstalledScriptPath {
param($FeatureItem)
$cmdPath = Get-FeatureRegCommandPath $FeatureItem
if (-not (Test-Path $cmdPath)) { return $null }
$command = (Get-ItemProperty -Path $cmdPath -Name '(default)' -ErrorAction SilentlyContinue).'(default)'
if ($command -match '-File\s+"([^"]+)"') { return $Matches[1] }
return $null
}
function Get-FeatureAutoScriptPath {
param($FeatureItem)
$installedPath = Get-FeatureVerifiedInstalledScriptPath $FeatureItem
if ($installedPath) { return $installedPath }
return Get-FeatureVerifiedDefaultScriptPath $FeatureItem
}
function Get-FeatureVerifiedInstalledScriptPath {
param($FeatureItem)
if (-not (Test-FeatureInstalled $FeatureItem)) { return $null }
$installedPath = Get-FeatureInstalledScriptPath $FeatureItem
if ($installedPath -and (Test-Path $installedPath -PathType Leaf)) { return $installedPath }
return $null
}
function Get-FeatureVerifiedDefaultScriptPath {
param($FeatureItem)
$defaultPath = Get-FeatureDefaultScriptPath $FeatureItem
if (Test-Path $defaultPath -PathType Leaf) { return $defaultPath }
return $null
}
function Find-UnrealLogoIcon {
$icon = Get-UProjectFileAssociationIcon
if ($icon) { return $icon }
return Find-InstalledEngineExecutableIcon
}
function Get-UProjectFileAssociationIcon {
# A source-built engine's own UnrealEditor.exe icon renders black, not the
# blue Unreal logo, so copy the icon from the .uproject file association
# itself - what "Switch Unreal Engine version...", "Generate Visual Studio
# project files", etc. all already point at - to match those exactly.
$progId = (Get-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\.uproject' -Name '(default)' -ErrorAction SilentlyContinue).'(default)'
if (-not $progId) { return $null }
return (Get-ItemProperty -Path "Registry::HKEY_CLASSES_ROOT\$progId\DefaultIcon" -Name '(default)' -ErrorAction SilentlyContinue).'(default)'
}
function Find-InstalledEngineExecutableIcon {
$engineDirs = Get-InstalledEngineDirectories
foreach ($exeName in @('UnrealVersionSelector.exe', 'UnrealEditor.exe')) {
foreach ($dir in $engineDirs) {
$exe = Join-Path $dir "Engine\Binaries\Win64\$exeName"
if (Test-Path $exe -PathType Leaf) { return "`"$exe`",0" }
}
}
return $null
}
function Get-InstalledEngineDirectories {
$engineDirs = @()
$hklmRoot = 'HKLM:\SOFTWARE\EpicGames\Unreal Engine'
if (Test-Path $hklmRoot) {
Get-ChildItem -Path $hklmRoot -ErrorAction SilentlyContinue | ForEach-Object {
$dir = (Get-ItemProperty -Path $_.PSPath -Name 'InstalledDirectory' -ErrorAction SilentlyContinue).InstalledDirectory
if ($dir) { $engineDirs += $dir }
}
}
$buildsKey = 'HKCU:\Software\Epic Games\Unreal Engine\Builds'
if (Test-Path $buildsKey) {
$buildsItem = Get-Item -Path $buildsKey -ErrorAction SilentlyContinue
foreach ($name in $buildsItem.Property) {
$dir = (Get-ItemProperty -Path $buildsKey -Name $name -ErrorAction SilentlyContinue).$name
if ($dir) { $engineDirs += $dir }
}
}
return $engineDirs
}
function Confirm-RegistryPath {
param([Parameter(Mandatory)][string]$Path)
$segments = $Path.Substring(5) -split '\\'
$current = 'HKCU:'
foreach ($segment in $segments) {
if ([string]::IsNullOrWhiteSpace($segment)) { continue }
$current = "$current\$segment"
if (-not (Test-Path $current)) {
New-Item -Path $current -Force | Out-Null
}
}
}
function Read-ScriptPath {
param($FeatureItem, [string]$Default)
while ($true) {
$label = "Path to $($FeatureItem.Id) script"
$prompt = if ($Default) { "$label [$Default]" } else { $label }
$response = Read-Host $prompt
if ([string]::IsNullOrWhiteSpace($response)) {
if (-not $Default) { continue }
$response = $Default
}
$candidate = $response.Trim('"')
if (-not (Test-Path $candidate -PathType Leaf)) {
Write-Host "File not found: $candidate" -ForegroundColor Red
continue
}
if ((Get-Item $candidate).Extension -ne '.ps1') {
Write-Host 'Path must point to a .ps1 file.' -ForegroundColor Red
continue
}
return (Resolve-Path $candidate).Path
}
}
function Install-Feature {
param($FeatureItem, [Parameter(Mandatory)][string]$TargetScriptPath)
$keyPath = Get-FeatureRegKeyPath $FeatureItem
$cmdPath = Get-FeatureRegCommandPath $FeatureItem
Confirm-RegistryPath -Path $cmdPath
Set-Item -Path $keyPath -Value "$($FeatureItem.MenuLabel)$MenuLabelSuffix"
$iconValue = Find-UnrealLogoIcon
if ($iconValue) {
Set-ItemProperty -Path $keyPath -Name 'Icon' -Value $iconValue
} else {
Write-Host 'No installed Unreal Engine found - menu entry will use the default icon.' -ForegroundColor Yellow
}
$command = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$TargetScriptPath`" `"%1`""
Set-Item -Path $cmdPath -Value $command
Write-Host "Installed $($FeatureItem.Id). Context menu will run: $TargetScriptPath" -ForegroundColor Green
}
function Uninstall-Feature {
param($FeatureItem)
if (-not (Test-FeatureInstalled $FeatureItem)) {
Write-Host "$($FeatureItem.Id) is not installed." -ForegroundColor Yellow
return
}
Remove-Item -Path (Get-FeatureRegKeyPath $FeatureItem) -Recurse -Force
Write-Host "Uninstalled $($FeatureItem.Id)." -ForegroundColor Green
}
function Show-FeatureStatus {
param([array]$FeaturesToShow = $Features)
foreach ($featureItem in $FeaturesToShow) {
if (Test-FeatureInstalled $featureItem) {
$path = Get-FeatureInstalledScriptPath $featureItem
Write-Host "[installed] $($featureItem.Id)" -ForegroundColor Green
Write-Host " $(if ($path) { $path } else { '(unable to read path)' })"
} else {
Write-Host "[not installed] $($featureItem.Id)" -ForegroundColor Yellow
}
}
}
function Select-Features {
param(
[Parameter(Mandatory)][array]$Candidates,
[string]$Prompt = 'Select feature(s)',
[switch]$Single
)
if ($Candidates.Count -eq 0) { return @() }
if ($Single -and $Candidates.Count -eq 1) { return @($Candidates[0]) }
for ($i = 0; $i -lt $Candidates.Count; $i++) {
Write-Host " $($i + 1)) $($Candidates[$i].Id)"
}
while ($true) {
$hint = if ($Single) { 'number' } else { 'numbers separated by commas, or "all"' }
$response = Read-Host "$Prompt ($hint)"
if ([string]::IsNullOrWhiteSpace($response)) { continue }
if (-not $Single -and $response.Trim().ToLower() -eq 'all') { return $Candidates }
$valid = $true
$selected = @()
foreach ($token in ($response -split ',')) {
$token = $token.Trim()
if ($token -match '^\d+$' -and [int]$token -ge 1 -and [int]$token -le $Candidates.Count) {
$selected += $Candidates[[int]$token - 1]
} else {
Write-Host "Invalid selection: $token" -ForegroundColor Red
$valid = $false
break
}
}
if (-not $valid) { continue }
if ($Single -and $selected.Count -ne 1) {
Write-Host 'Choose exactly one.' -ForegroundColor Red
continue
}
return $selected
}
}
function Get-TargetFeatures {
param([string]$FeatureId)
if (-not $FeatureId) { return $Features }
$match = $Features | Where-Object { $_.Id -eq $FeatureId }
if (-not $match) {
Write-Host "Unknown feature: $FeatureId" -ForegroundColor Red
Write-Host "Known features: $(($Features | ForEach-Object { $_.Id }) -join ', ')"
exit 1
}
return @($match)
}
function Invoke-NonInteractiveInstall {
param([array]$Targets)
if ($ScriptPath -and $Targets.Count -gt 1) {
Write-Host '-ScriptPath requires -Feature to target a single feature.' -ForegroundColor Red
exit 1
}
foreach ($featureItem in $Targets) {
$path = if ($ScriptPath) { $ScriptPath } else { Get-FeatureAutoScriptPath $featureItem }
if (-not $path -or -not (Test-Path $path -PathType Leaf)) {
Write-Host "Could not find a script for $($featureItem.Id) automatically. Pass -Feature $($featureItem.Id) -ScriptPath <path>." -ForegroundColor Red
continue
}
Install-Feature -FeatureItem $featureItem -TargetScriptPath (Resolve-Path $path).Path
}
}
function Invoke-NonInteractiveMode {
$targets = Get-TargetFeatures -FeatureId $Feature
if ($Status) { Show-FeatureStatus -FeaturesToShow $targets; return }
if ($Uninstall) {
foreach ($featureItem in $targets) { Uninstall-Feature -FeatureItem $featureItem }
return
}
if ($Install) { Invoke-NonInteractiveInstall -Targets $targets }
}
function Install-SelectedFeatures {
$selected = Select-Features -Candidates $Features -Prompt 'Install which feature(s)?'
foreach ($featureItem in $selected) {
$path = Get-FeatureAutoScriptPath $featureItem
if (-not $path) { $path = Read-ScriptPath -FeatureItem $featureItem -Default $null }
Install-Feature -FeatureItem $featureItem -TargetScriptPath $path
}
}
function Uninstall-SelectedFeatures {
$installed = @($Features | Where-Object { Test-FeatureInstalled $_ })
if ($installed.Count -eq 0) {
Write-Host 'No features installed.' -ForegroundColor Yellow
return
}
$selected = Select-Features -Candidates $installed -Prompt 'Uninstall which feature(s)?'
foreach ($featureItem in $selected) { Uninstall-Feature -FeatureItem $featureItem }
}
function Invoke-InteractiveMenu {
do {
Write-Host ''
Write-Host '=== UE5 Shell Configuration ===' -ForegroundColor Cyan
Show-FeatureStatus
Write-Host ''
Write-Host '1) Install / Reinstall feature(s)'
Write-Host '2) Uninstall feature(s)'
Write-Host '3) Exit'
$choice = Read-Host 'Choose an option'
switch ($choice) {
'1' { Install-SelectedFeatures }
'2' { Uninstall-SelectedFeatures }
'3' { }
default { Write-Host 'Invalid choice.' -ForegroundColor Red }
}
} while ($choice -ne '3')
Write-Host ''
Read-Host 'Press Enter to exit'
}
if ($Install -or $Uninstall -or $Status) {
Invoke-NonInteractiveMode
} else {
Invoke-InteractiveMenu
}