-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinstall-windows.ps1
More file actions
executable file
·729 lines (623 loc) · 30.3 KB
/
install-windows.ps1
File metadata and controls
executable file
·729 lines (623 loc) · 30.3 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# Deadlock API Ingest - Windows Installation Script
# This script downloads and installs the application to run automatically on user login via Task Scheduler.
# Suppress PSScriptAnalyzer warnings for Write-Host in this interactive installation script
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification='Interactive installation script requires colored console output for user experience')]
param()
# --- Configuration ---
$AppName = "deadlock-api-ingest"
$GithubRepo = "deadlock-api/deadlock-api-ingest"
$AssetKeyword = "windows-latest.exe"
# Installation Paths
$InstallDir = "$env:LOCALAPPDATA\$AppName"
$FinalExecutableName = "$AppName.exe"
$LogFile = "$env:TEMP\${AppName}-install.log"
# --- Script Setup ---
$ErrorActionPreference = 'Stop'
# Global error tracking
$script:HasErrors = $false
$script:ErrorDetails = @()
$script:SkipPressAnyKey = $false
if ($env:CI -eq 'true' -or $env:GITHUB_ACTIONS -eq 'true' -or $env:TF_BUILD -eq 'True') {
$script:SkipPressAnyKey = $true
}
# Function to handle errors and keep window open
function Invoke-FatalError {
param(
[Parameter(Mandatory = $true)]
[string]$ErrorMessage,
[string]$DetailedError = "",
[int]$ExitCode = 1
)
$script:HasErrors = $true
$script:ErrorDetails += $ErrorMessage
Write-Host ""
Write-Host "========================================" -ForegroundColor Red
Write-Host " INSTALLATION FAILED " -ForegroundColor Red
Write-Host "========================================" -ForegroundColor Red
Write-Host ""
Write-InstallLog -Level 'ERROR' $ErrorMessage
if ($DetailedError) {
Write-Host "Error Details:" -ForegroundColor Yellow
Write-Host $DetailedError -ForegroundColor White
Write-Host ""
Add-Content -Path $LogFile -Value "Detailed Error: $DetailedError"
}
# Show log file location
Write-Host "Complete installation log available at:" -ForegroundColor Cyan
Write-Host $LogFile -ForegroundColor White
Write-Host ""
# Wait for user acknowledgment (skip in CI environments)
if (-not $script:SkipPressAnyKey) {
Write-Host "Press any key to close this window..." -ForegroundColor Green
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
exit $ExitCode
}
# Function to show final status and wait for user input
function Show-FinalStatus {
if ($script:HasErrors) {
Write-Host ""
Write-Host "Installation completed with errors. Please review the messages above." -ForegroundColor Yellow
Write-Host "Log file: $LogFile" -ForegroundColor Cyan
Write-Host ""
} else {
Write-Host ""
Write-Host "Installation completed successfully!" -ForegroundColor Green
Write-Host ""
}
# Wait for user acknowledgment (skip in CI environments)
if (-not $script:SkipPressAnyKey) {
Write-Host "Press any key to close this window..." -ForegroundColor Green
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
# --- Helper Functions ---
# Function to write to log and console with color
function Write-InstallLog {
param(
[Parameter(Mandatory = $true)]
[ValidateSet('INFO', 'WARN', 'ERROR', 'SUCCESS')]
[string]$Level,
[Parameter(Mandatory = $true)]
[string]$Message
)
$ColorMap = @{ 'INFO' = 'Cyan'; 'WARN' = 'Yellow'; 'ERROR' = 'Red'; 'SUCCESS' = 'Green' }
$Color = $ColorMap[$Level]
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$LogMessage = "[$Timestamp] [$Level] $Message"
Write-Host $LogMessage -ForegroundColor $Color
Add-Content -Path $LogFile -Value $LogMessage
}
# Function to execute commands quietly while logging details
function Invoke-Quietly {
param(
[Parameter(Mandatory = $true)]
[string]$Description,
[Parameter(Mandatory = $true)]
[scriptblock]$ScriptBlock,
[switch]$ContinueOnError
)
Write-InstallLog -Level 'INFO' $Description
try {
# Capture output and redirect to log file
$output = & $ScriptBlock 2>&1
Add-Content -Path $LogFile -Value $output
return $true
}
catch {
$errorMsg = "Command failed: $($_.Exception.Message)"
Write-InstallLog -Level 'ERROR' $errorMsg
Add-Content -Path $LogFile -Value "Error: $($_.Exception.Message)"
Add-Content -Path $LogFile -Value "Stack Trace: $($_.ScriptStackTrace)"
if (-not $ContinueOnError) {
Invoke-FatalError -ErrorMessage $errorMsg -DetailedError $_.Exception.Message
}
return $false
}
}
# Function to check for Administrator privileges
function Test-IsAdmin {
try {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$isAdmin = (New-Object Security.Principal.WindowsPrincipal $currentUser).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Invoke-FatalError -ErrorMessage "Administrator privileges are required to create scheduled tasks." -DetailedError "Please run this script as an Administrator."
}
}
catch {
Invoke-FatalError -ErrorMessage "Failed to check Administrator privileges." -DetailedError $_.Exception.Message
}
}
# Function to get the latest release from GitHub
function Get-LatestRelease {
Write-InstallLog -Level 'INFO' "Fetching latest release from repository: $GithubRepo"
$ApiUrl = "https://api.github.com/repos/$GithubRepo/releases/latest"
try {
$releaseInfo = Invoke-RestMethod -Uri $ApiUrl -UseBasicParsing
}
catch {
$errorMsg = "Failed to fetch release information from GitHub API."
$detailedError = "URL: $ApiUrl`nError: $($_.Exception.Message)`n`nPossible causes:`n- No internet connection`n- GitHub API rate limit exceeded`n- Repository not found or private`n- Firewall blocking the request"
Invoke-FatalError -ErrorMessage $errorMsg -DetailedError $detailedError
}
try {
$asset = $releaseInfo.assets | Where-Object { $_.name -like "*$AssetKeyword*" } | Select-Object -First 1
if (-not $asset) {
$availableAssets = if ($releaseInfo.assets) { $releaseInfo.assets.name -join ', ' } else { "None" }
$errorMsg = "Could not find a release asset containing the keyword: '$AssetKeyword'"
$detailedError = "Available assets are: $availableAssets`n`nThe release may not have been built for Windows, or the asset naming convention has changed."
Invoke-FatalError -ErrorMessage $errorMsg -DetailedError $detailedError
}
return [PSCustomObject]@{
Version = $releaseInfo.tag_name
DownloadUrl = $asset.browser_download_url
Size = $asset.size
}
}
catch {
Invoke-FatalError -ErrorMessage "Failed to process release information." -DetailedError $_.Exception.Message
}
}
# Function to create desktop shortcut
function New-DesktopShortcut {
param(
[Parameter(Mandatory = $true)]
[string]$ExecutablePath,
[Parameter(Mandatory = $false)]
[string]$Arguments = "",
[Parameter(Mandatory = $false)]
[string]$ShortcutName = $AppName,
[Parameter(Mandatory = $false)]
[string]$Description = "Deadlock API Ingest - Monitors Steam cache for Deadlock match replays"
)
Write-InstallLog -Level 'INFO' "Creating desktop shortcut: $ShortcutName..."
try {
$DesktopPath = [Environment]::GetFolderPath('Desktop')
if (-not (Test-Path $DesktopPath)) {
throw "Desktop folder not found at: $DesktopPath"
}
$WshShell = New-Object -ComObject WScript.Shell
$ShortcutPath = Join-Path -Path $DesktopPath -ChildPath "$ShortcutName.lnk"
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = $ExecutablePath
$Shortcut.Arguments = $Arguments
$Shortcut.WorkingDirectory = $InstallDir
$Shortcut.Description = $Description
$Shortcut.IconLocation = $ExecutablePath
$Shortcut.Save()
Write-InstallLog -Level 'SUCCESS' "Desktop shortcut created at: $ShortcutPath"
return $true
}
catch {
Write-InstallLog -Level 'ERROR' "Failed to create desktop shortcut: $ShortcutName"
Write-InstallLog -Level 'WARN' "Continuing installation without desktop shortcut."
Add-Content -Path $LogFile -Value "Error details: $($_.Exception.Message)"
$script:HasErrors = $true
$script:ErrorDetails += "Desktop shortcut creation failed (non-critical)"
return $false
}
}
# Function to manage the Scheduled Task for autostart
function Set-StartupTask {
param(
[Parameter(Mandatory = $true)]
[ValidateSet('Remove', 'Create')]
[string]$Action,
[string]$ExecutablePath
)
switch ($Action) {
'Remove' {
Invoke-Quietly "Removing existing scheduled task..." {
Unregister-ScheduledTask -TaskName $AppName -Confirm:$false -ErrorAction SilentlyContinue
} | Out-Null
}
'Create' {
Write-InstallLog -Level 'INFO' "Creating startup task..."
try {
# Create a VBS wrapper script to run the executable hidden
$vbsWrapperPath = Join-Path -Path $InstallDir -ChildPath "run-hidden.vbs"
$vbsContent = @"
Set WshShell = CreateObject("WScript.Shell")
args = ""
For Each arg In WScript.Arguments
args = args & " " & arg
Next
WshShell.Run """$ExecutablePath""" & args, 0, False
"@
Set-Content -Path $vbsWrapperPath -Value $vbsContent -Force
# Define the action (run the VBS wrapper with wscript.exe, forwarding extra args to the exe)
$vbsArgs = "`"$vbsWrapperPath`""
if ($extraArgs -ne "") {
$vbsArgs = "`"$vbsWrapperPath`" $extraArgs"
}
$taskAction = New-ScheduledTaskAction -Execute "wscript.exe" -Argument $vbsArgs -WorkingDirectory $InstallDir
# Define the trigger (when to run it - at user logon)
$taskTrigger = New-ScheduledTaskTrigger -AtLogOn
# Define the user and permissions (run as current user with standard privileges)
$taskPrincipal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType S4U -RunLevel Limited
# Define settings (allow it to run indefinitely, prevent multiple instances)
$taskSettings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-ExecutionTimeLimit 0 `
-StartWhenAvailable `
-MultipleInstances IgnoreNew `
-Hidden
# Register the task with the system
Register-ScheduledTask -TaskName $AppName -Action $taskAction -Trigger $taskTrigger -Principal $taskPrincipal -Settings $taskSettings -Description "Monitors Steam cache for Deadlock match replays and submits metadata to the Deadlock API." | Out-Null
Write-InstallLog -Level 'SUCCESS' "Startup task created successfully."
}
catch {
Invoke-FatalError -ErrorMessage "Failed to create startup task." -DetailedError "Error: $($_.Exception.Message)`n`nThis could be due to:`n- Task Scheduler service not running`n- Conflicting task name`n- System policy restrictions"
}
}
}
}
# --- Main Installation Logic ---
# Wrap the entire installation in a try-catch block
try {
Clear-Content -Path $LogFile -ErrorAction SilentlyContinue
Write-InstallLog -Level 'INFO' "Starting Deadlock API Ingest installation..."
Write-InstallLog -Level 'INFO' "Log file is available at: $LogFile"
$release = Get-LatestRelease
# Try to run uninstall script if it exists (clean uninstall before fresh install)
Write-Host "Removing scheduled tasks..." -ForegroundColor Cyan
$tasksToRemove = @(
"$AppName",
"$AppName-Watchdog",
"$AppName-updater"
)
foreach ($taskName in $tasksToRemove) {
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($task) {
Write-Host " - Removing task: $taskName" -ForegroundColor Gray
Stop-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
}
}
# Stop any running process
Write-Host "Stopping running processes..." -ForegroundColor Cyan
$processes = Get-Process -Name $AppName -ErrorAction SilentlyContinue
if ($processes) {
Write-Host " - Stopping $($processes.Count) process(es)" -ForegroundColor Gray
Stop-Process -Name $AppName -Force -ErrorAction SilentlyContinue
}
Write-InstallLog -Level 'INFO' "Preparing installation environment..."
try {
Stop-Process -Name $AppName -Force -ErrorAction SilentlyContinue
}
catch {
# This is expected if the process isn't running
Write-InstallLog -Level 'INFO' "No existing process to stop."
}
try {
New-Item -Path $InstallDir -ItemType Directory -Force | Out-Null
}
catch {
Invoke-FatalError -ErrorMessage "Failed to create installation directory." -DetailedError "Error: $($_.Exception.Message)`n`nInstall Directory: $InstallDir"
}
$downloadPath = Join-Path -Path $InstallDir -ChildPath $FinalExecutableName
Write-InstallLog -Level 'INFO' "Downloading application binary..."
# Log detailed URL to file only
Add-Content -Path $LogFile -Value "Downloading from: $($release.DownloadUrl)"
try {
Invoke-WebRequest -Uri $release.DownloadUrl -OutFile $downloadPath -UseBasicParsing
}
catch {
Invoke-FatalError -ErrorMessage "Failed to download application binary." -DetailedError "URL: $($release.DownloadUrl)`nDestination: $downloadPath`nError: $($_.Exception.Message)`n`nPossible causes:`n- No internet connection`n- Insufficient disk space`n- Permission denied to write to installation directory"
}
# Verify file size
try {
$actualSize = (Get-Item -Path $downloadPath).Length
if ($actualSize -ne $release.Size) {
Invoke-FatalError -ErrorMessage "File size mismatch! Download may be corrupted." -DetailedError "Expected: $($release.Size) bytes`nActual: $actualSize bytes`n`nPlease try running the installation again."
}
Write-InstallLog -Level 'SUCCESS' "Download complete and verified."
}
catch {
Invoke-FatalError -ErrorMessage "Failed to verify downloaded file." -DetailedError $_.Exception.Message
}
try {
Unblock-File -Path $downloadPath
}
catch {
Write-InstallLog -Level 'WARN' "Failed to unblock downloaded file, but continuing installation."
$script:HasErrors = $true
$script:ErrorDetails += "File unblock failed (non-critical)"
}
# Download uninstall script
Write-InstallLog -Level 'INFO' "Downloading uninstall script..."
$uninstallScriptPath = Join-Path -Path $InstallDir -ChildPath "uninstall-windows.ps1"
$uninstallScriptUrl = "https://raw.githubusercontent.com/deadlock-api/deadlock-api-ingest/master/uninstall-windows.ps1"
try {
Invoke-WebRequest -Uri $uninstallScriptUrl -OutFile $uninstallScriptPath -UseBasicParsing
Write-InstallLog -Level 'SUCCESS' "Uninstall script downloaded to: $uninstallScriptPath"
}
catch {
Write-InstallLog -Level 'WARN' "Failed to download uninstall script, but continuing installation."
Write-InstallLog -Level 'INFO' "You can manually download it from: $uninstallScriptUrl"
$script:HasErrors = $true
$script:ErrorDetails += "Uninstall script download failed (non-critical)"
}
Write-InstallLog -Level 'INFO' "Installing application..."
# Check if running in interactive mode (used by all prompts below)
$isInteractive = [Environment]::UserInteractive -and -not [Console]::IsInputRedirected
# Ask user if they want Statlocker integration
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " STATLOCKER INTEGRATION (OPTIONAL) " -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Statlocker integration sends match IDs to statlocker.gg after each ingestion." -ForegroundColor White
Write-Host "This helps track community match statistics. No personal data is sent." -ForegroundColor White
Write-Host ""
Write-Host "Enable Statlocker integration? (Y/N): " -ForegroundColor Yellow -NoNewline
$enableStatlocker = $false
$statlockerAttempts = 0
$maxStatlockerAttempts = 2
if (-not $isInteractive) {
Write-Host "Y (default in non-interactive mode)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "Non-interactive mode detected. Enabling Statlocker integration by default."
$enableStatlocker = $true
} else {
$timeoutSeconds = 10
$startTime = Get-Date
$keyPressed = $false
while ($statlockerAttempts -lt $maxStatlockerAttempts -and -not $keyPressed) {
if ([Console]::KeyAvailable) {
$response = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$key = $response.Character.ToString().ToUpper()
if ($key -eq "Y" -or $key -eq "N") {
Write-Host $key -ForegroundColor Cyan
if ($key -eq "Y") {
$enableStatlocker = $true
}
$keyPressed = $true
break
} else {
$statlockerAttempts++
if ($statlockerAttempts -lt $maxStatlockerAttempts) {
Write-Host ""
Write-Host "Invalid response. Please enter 'Y' for yes or 'N' for no." -ForegroundColor Yellow
Write-Host "Enable Statlocker integration? (Y/N): " -ForegroundColor Yellow -NoNewline
}
}
}
# Check timeout
if (((Get-Date) - $startTime).TotalSeconds -ge $timeoutSeconds -and -not $keyPressed) {
Write-Host "Y (timeout - defaulting to yes)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "No response received within $timeoutSeconds seconds. Enabling Statlocker integration by default."
$enableStatlocker = $true
$keyPressed = $true
break
}
Start-Sleep -Milliseconds 100
}
if (-not $keyPressed) {
Write-Host "Y (max attempts reached - defaulting to yes)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "Maximum attempts reached. Enabling Statlocker integration by default."
$enableStatlocker = $true
}
}
Write-Host ""
$extraArgs = ""
if ($enableStatlocker) {
Write-InstallLog -Level 'SUCCESS' "Statlocker integration enabled."
} else {
$extraArgs = "--no-statlocker"
Write-InstallLog -Level 'INFO' "Statlocker integration disabled."
}
# Ask user if they want auto-start
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " AUTO-START SETUP (OPTIONAL) " -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Auto-start will automatically start the application when the system boots." -ForegroundColor White
Write-Host "This ensures the application is always running in the background." -ForegroundColor White
Write-Host "CAREFUL: This requires Administrator privileges to create a scheduled task." -ForegroundColor Yellow
Write-Host ""
Write-Host "Enable auto-start on system boot? (Y/N): " -ForegroundColor Yellow -NoNewline
$enableAutoStart = $false
$autoStartAttempts = 0
$maxAutoStartAttempts = 2
if (-not $isInteractive) {
Write-Host "Y (default in non-interactive mode)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "Non-interactive mode detected. Enabling auto-start by default."
$enableAutoStart = $true
} else {
# Try to read with timeout
$timeoutSeconds = 10
$startTime = Get-Date
$keyPressed = $false
while ($autoStartAttempts -lt $maxAutoStartAttempts -and -not $keyPressed) {
if ([Console]::KeyAvailable) {
$response = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$key = $response.Character.ToString().ToUpper()
if ($key -eq "Y" -or $key -eq "N") {
Write-Host $key -ForegroundColor Cyan
if ($key -eq "Y") {
$enableAutoStart = $true
}
$keyPressed = $true
break
} else {
$autoStartAttempts++
if ($autoStartAttempts -lt $maxAutoStartAttempts) {
Write-Host ""
Write-Host "Invalid response. Please enter 'Y' for yes or 'N' for no." -ForegroundColor Yellow
Write-Host "Enable auto-start on system boot? (Y/N): " -ForegroundColor Yellow -NoNewline
}
}
}
# Check timeout
if (((Get-Date) - $startTime).TotalSeconds -ge $timeoutSeconds -and -not $keyPressed) {
Write-Host "Y (timeout - defaulting to yes)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "No response received within $timeoutSeconds seconds. Enabling auto-start by default."
$enableAutoStart = $true
$keyPressed = $true
break
}
Start-Sleep -Milliseconds 100
}
if (-not $keyPressed) {
Write-Host "Y (max attempts reached - defaulting to yes)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "Maximum attempts reached. Enabling auto-start by default."
$enableAutoStart = $true
}
}
Write-Host ""
if ($enableAutoStart) {
Write-InstallLog -Level 'INFO' "User chose to enable auto-start."
# Create the main scheduled task
Test-IsAdmin
Set-StartupTask -Action 'Create' -ExecutablePath $downloadPath
# Start the main task
try {
Start-ScheduledTask -TaskName $AppName
Write-InstallLog -Level 'SUCCESS' "Application started successfully with auto-start enabled."
}
catch {
Write-InstallLog -Level 'ERROR' "Failed to start the application task."
Write-InstallLog -Level 'WARN' "Continuing installation - you can start the task manually later."
Add-Content -Path $LogFile -Value "Error: $($_.Exception.Message)"
Add-Content -Path $LogFile -Value "The application was installed but failed to start automatically. You can start it manually using: Start-ScheduledTask -TaskName $AppName"
$script:HasErrors = $true
$script:ErrorDetails += "Task start failed (non-critical)"
}
} else {
Write-InstallLog -Level 'INFO' "User chose to skip auto-start."
Write-Host "Auto-start will not be enabled." -ForegroundColor Yellow
Write-Host ""
# Offer to create a desktop shortcut instead
Write-Host "Would you like to create a desktop shortcut instead? (Y/N): " -ForegroundColor Yellow -NoNewline
$createShortcut = $false
$shortcutAttempts = 0
$maxShortcutAttempts = 2
if (-not $isInteractive) {
Write-Host "Y (default in non-interactive mode)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "Non-interactive mode detected. Creating desktop shortcut by default."
$createShortcut = $true
} else {
# Try to read with timeout
$startTime = Get-Date
$keyPressed = $false
while ($shortcutAttempts -lt $maxShortcutAttempts -and -not $keyPressed) {
if ([Console]::KeyAvailable) {
$response = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$key = $response.Character.ToString().ToUpper()
if ($key -eq "Y" -or $key -eq "N") {
Write-Host $key -ForegroundColor Cyan
if ($key -eq "Y") {
$createShortcut = $true
}
$keyPressed = $true
break
} else {
$shortcutAttempts++
if ($shortcutAttempts -lt $maxShortcutAttempts) {
Write-Host ""
Write-Host "Invalid response. Please enter 'Y' for yes or 'N' for no." -ForegroundColor Yellow
Write-Host "Would you like to create a desktop shortcut instead? (Y/N): " -ForegroundColor Yellow -NoNewline
}
}
}
# Check timeout
if (((Get-Date) - $startTime).TotalSeconds -ge $timeoutSeconds -and -not $keyPressed) {
Write-Host "Y (timeout - defaulting to yes)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "No response received within $timeoutSeconds seconds. Creating desktop shortcut by default."
$createShortcut = $true
$keyPressed = $true
break
}
Start-Sleep -Milliseconds 100
}
if (-not $keyPressed) {
Write-Host "Y (max attempts reached - defaulting to yes)" -ForegroundColor Cyan
Write-InstallLog -Level 'INFO' "Maximum attempts reached. Creating desktop shortcut by default."
$createShortcut = $true
}
}
Write-Host ""
if ($createShortcut) {
# Create main shortcut
New-DesktopShortcut -ExecutablePath $downloadPath -Arguments $extraArgs
# Create "once" shortcut for initial cache ingest only
$onceArgs = "--once"
if ($extraArgs -ne "") {
$onceArgs = "--once $extraArgs"
}
New-DesktopShortcut -ExecutablePath $downloadPath `
-Arguments $onceArgs `
-ShortcutName "$AppName (Once)" `
-Description "Deadlock API Ingest - Scan existing Steam cache once and exit"
Write-Host "Desktop shortcuts created:" -ForegroundColor White
} else {
Write-Host "You can manually start the application by running: $downloadPath" -ForegroundColor White
Write-Host "To run once (ingest existing cache only): $downloadPath --once" -ForegroundColor White
}
Write-Host "To enable auto-start later, re-run this installer." -ForegroundColor White
Write-Host ""
}
}
catch {
Invoke-FatalError -ErrorMessage "Unexpected error during installation." -DetailedError $_.Exception.Message
}
# Display final status
if (-not $script:HasErrors) {
Write-Host " "
Write-InstallLog -Level 'SUCCESS' "Deadlock API Ingest has been installed successfully!"
# Check if auto-start is enabled
$autoStartEnabled = $false
try {
$task = Get-ScheduledTask -TaskName $AppName -ErrorAction SilentlyContinue
if ($task) {
$autoStartEnabled = $true
Write-Host "[+] Auto-start is enabled" -ForegroundColor Green
Write-Host " The application will start automatically every time you log in." -ForegroundColor White
} else {
Write-Host "[-] Auto-start is disabled" -ForegroundColor Yellow
Write-Host " The application will not start automatically on user login." -ForegroundColor White
Write-Host " To enable auto-start later, re-run this installer." -ForegroundColor White
}
} catch {
Write-Host "[?] Auto-start status unknown" -ForegroundColor Yellow
}
Write-Host " "
# Show log file location
$LogDir = "$env:APPDATA\$AppName\logs"
Write-Host "Log files are written to: $LogDir" -ForegroundColor White
Write-Host " Logs rotate daily and the last 7 days are kept." -ForegroundColor White
Write-Host " "
if ($autoStartEnabled) {
Write-Host "You can manage the main task via the Task Scheduler (taskschd.msc) or PowerShell:" -ForegroundColor White
Write-Host " - Check status: Get-ScheduledTask -TaskName $AppName | Get-ScheduledTaskInfo" -ForegroundColor Yellow
Write-Host " - Run manually: Start-ScheduledTask -TaskName $AppName" -ForegroundColor Yellow
Write-Host " - Stop it: Stop-ScheduledTask -TaskName $AppName" -ForegroundColor Yellow
Write-Host " - Disable auto-start: Unregister-ScheduledTask -TaskName $AppName" -ForegroundColor Yellow
Write-Host " "
}
Write-Host "To uninstall, run: $InstallDir\uninstall-windows.ps1" -ForegroundColor White
Write-Host " "
# --- User-friendly usage explanation ---
Write-Host "How to use Deadlock API Ingest:" -ForegroundColor Green
Write-Host "1. Restart your game after installation." -ForegroundColor Cyan
Write-Host "2. Once in the game, go to your match history and click on 'matches'." -ForegroundColor Cyan
Write-Host "3. The tool will now work in the background to enhance your experience!" -ForegroundColor Cyan
Write-Host "If you have any questions, check the README or reach out for support.`n" -ForegroundColor Yellow
} else {
Write-Host " "
Write-Host "========================================" -ForegroundColor Yellow
Write-Host " INSTALLATION COMPLETED WITH ISSUES " -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow
Write-Host " "
Write-InstallLog -Level 'WARN' "Installation completed but some non-critical components failed:"
foreach ($errorDetail in $script:ErrorDetails) {
Write-Host " - $errorDetail" -ForegroundColor Yellow
}
Write-Host " "
Write-InstallLog -Level 'INFO' "The main application should still work. Check the log file for details: $LogFile"
Write-Host " "
}
# Always show final status and wait for user input
Show-FinalStatus