-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamLibraryUpdater.psm1
More file actions
1038 lines (876 loc) · 32.6 KB
/
Copy pathSteamLibraryUpdater.psm1
File metadata and controls
1038 lines (876 loc) · 32.6 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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires -Version 5.1
<#
.SYNOPSIS
Steam Update Manager - Automated Steam game update management
.DESCRIPTION
Monitors and updates Steam games automatically when Steam is running
#>
# Module configuration
$script:AppName = "Steam Update Manager"
$script:AppVersion = "2.0.1"
$script:ProductFolder = "Steam-Update-Manager"
$script:DataRoot = if ($env:ProgramData) {
Join-Path $env:ProgramData $script:ProductFolder
}
else {
$PSScriptRoot
}
$script:ModuleConfig = @{
ConfigPath = Join-Path $script:DataRoot "Config.json"
AppInfoPath = Join-Path $script:DataRoot "appinfo"
SteamCmdPath = Join-Path $PSScriptRoot "steamcmd\steamcmd.exe"
LogPath = Join-Path $script:DataRoot "Logs"
DataRoot = $script:DataRoot
InstallRoot = $PSScriptRoot
}
# Ensure directories exist
if (-not (Test-Path $script:ModuleConfig.AppInfoPath)) {
New-Item -ItemType Directory -Path $script:ModuleConfig.AppInfoPath -Force | Out-Null
}
if (-not (Test-Path $script:ModuleConfig.LogPath)) {
New-Item -ItemType Directory -Path $script:ModuleConfig.LogPath -Force | Out-Null
}
function Get-DefaultSteamUpdateManagerConfig {
[CmdletBinding()]
param()
return [PSCustomObject]@{
AppName = $script:AppName
AppVersion = $script:AppVersion
Theme = "Auto"
StartWithWindows = $true
ShowTaskbarIcon = $false
DiagnosticLogging = $true
MaxLogFileKB = 512
MaxLogFiles = 8
UpdateDuringGaming = $false
CheckIntervalMinutes = 1
EnableAutoUpdate = $true
UpdateCondition = "WhenAvailable"
SteamInstallPath = ""
MonitoredGames = @()
LastCheck = $null
Advanced = [PSCustomObject]@{
AutoMonitorInstalledGames = $true
UpdateCheckProvider = "https://api.steamcmd.net/v1/info"
ScheduledTaskPollMinutes = 1
SteamCmdInstallPath = ""
}
}
}
function Add-MissingConfigValue {
param(
[Parameter(Mandatory)]
[PSCustomObject]$Config,
[Parameter(Mandatory)]
[PSCustomObject]$Defaults
)
foreach ($property in $Defaults.PSObject.Properties) {
if (-not $Config.PSObject.Properties[$property.Name]) {
$Config | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value
continue
}
if ($property.Value -is [PSCustomObject] -and $Config.$($property.Name) -is [PSCustomObject]) {
Add-MissingConfigValue -Config $Config.$($property.Name) -Defaults $property.Value
}
}
}
function Get-SteamUpdateManagerPaths {
[CmdletBinding()]
param()
return [PSCustomObject]$script:ModuleConfig
}
function Get-SteamUpdateManagerVersion {
[CmdletBinding()]
param()
return $script:AppVersion
}
function Invoke-LogRetention {
param(
[Parameter(Mandatory)]
[string]$CurrentLogFile,
[int]$MaxLogFileKB = 512,
[int]$MaxLogFiles = 8
)
if ((Test-Path $CurrentLogFile) -and ((Get-Item $CurrentLogFile).Length -gt ($MaxLogFileKB * 1KB))) {
$archiveName = "{0}.{1}.log" -f ([System.IO.Path]::GetFileNameWithoutExtension($CurrentLogFile)), (Get-Date -Format "yyyyMMddHHmmss")
Rename-Item -Path $CurrentLogFile -NewName $archiveName -Force
}
Get-ChildItem -Path $script:ModuleConfig.LogPath -Filter "*.log" -File -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending |
Select-Object -Skip $MaxLogFiles |
Remove-Item -Force -ErrorAction SilentlyContinue
}
function Write-Log {
<#
.SYNOPSIS
Writes a log message to the log file
#>
param(
[Parameter(Mandatory)]
[string]$Message,
[ValidateSet('Info', 'Warning', 'Error')]
[string]$Level = 'Info'
)
$diagnosticLogging = $true
$maxLogFileKB = 512
$maxLogFiles = 8
if (Test-Path $script:ModuleConfig.ConfigPath) {
try {
$logConfig = Get-Content $script:ModuleConfig.ConfigPath -Raw | ConvertFrom-Json
if ($logConfig.PSObject.Properties['DiagnosticLogging']) {
$diagnosticLogging = [bool]$logConfig.DiagnosticLogging
}
if ($logConfig.PSObject.Properties['MaxLogFileKB'] -and [int]$logConfig.MaxLogFileKB -gt 0) {
$maxLogFileKB = [int]$logConfig.MaxLogFileKB
}
if ($logConfig.PSObject.Properties['MaxLogFiles'] -and [int]$logConfig.MaxLogFiles -gt 0) {
$maxLogFiles = [int]$logConfig.MaxLogFiles
}
}
catch {
$diagnosticLogging = $true
}
}
if ((-not $diagnosticLogging) -and $Level -ne 'Error') {
return
}
if (-not (Test-Path $script:ModuleConfig.LogPath)) {
New-Item -ItemType Directory -Path $script:ModuleConfig.LogPath -Force | Out-Null
}
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logFile = Join-Path $script:ModuleConfig.LogPath "SteamUpdateManager_$(Get-Date -Format 'yyyyMMdd').log"
$logMessage = "[$timestamp] [$Level] $Message"
Invoke-LogRetention -CurrentLogFile $logFile -MaxLogFileKB $maxLogFileKB -MaxLogFiles $maxLogFiles
Add-Content -Path $logFile -Value $logMessage
Write-Verbose $logMessage
}
function Get-SteamLibraryUpdaterConfig {
<#
.SYNOPSIS
Gets the current configuration
#>
[CmdletBinding()]
param()
$defaults = Get-DefaultSteamUpdateManagerConfig
if (Test-Path $script:ModuleConfig.ConfigPath) {
try {
$config = Get-Content $script:ModuleConfig.ConfigPath -Raw | ConvertFrom-Json
Add-MissingConfigValue -Config $config -Defaults $defaults
return $config
}
catch {
Write-Log "Error reading config file: $_" -Level Error
}
}
# Return default configuration
return $defaults
}
function Set-SteamLibraryUpdaterConfig {
<#
.SYNOPSIS
Sets the configuration
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[PSCustomObject]$Config
)
try {
if (-not (Test-Path $script:DataRoot)) {
New-Item -ItemType Directory -Path $script:DataRoot -Force | Out-Null
}
$Config.AppName = $script:AppName
$Config.AppVersion = $script:AppVersion
$Config | ConvertTo-Json -Depth 10 | Set-Content $script:ModuleConfig.ConfigPath -Force
Write-Log "Configuration saved successfully"
return $true
}
catch {
Write-Log "Error saving config file: $_" -Level Error
return $false
}
}
function Test-SteamRunning {
<#
.SYNOPSIS
Checks if Steam is currently running
#>
[CmdletBinding()]
param()
$steamProcess = Get-Process -Name "steam" -ErrorAction SilentlyContinue
return ($null -ne $steamProcess)
}
function Test-GameRunning {
<#
.SYNOPSIS
Checks if any Steam game is currently running
#>
[CmdletBinding()]
param()
$config = Get-SteamLibraryUpdaterConfig
# Get all process names once for efficiency
$runningProcessNames = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
foreach ($p in Get-Process -ErrorAction SilentlyContinue) {
$null = $runningProcessNames.Add($p.ProcessName)
}
# Check if any monitored game processes are running
foreach ($game in $config.MonitoredGames) {
if ($game.ProcessName) {
if ($runningProcessNames.Contains($game.ProcessName)) {
Write-Log "Game running: $($game.ProcessName)" -Level Info
return $true
}
}
}
# Also check for common Steam game indicators
# Steam games often have GameOverlayUI process
$overlayProcess = Get-Process -Name "GameOverlayUI" -ErrorAction SilentlyContinue
if ($null -ne $overlayProcess) {
# Additional check: see if it's actively being used (more than just idle)
$steamApps = Get-Process | Where-Object {
$_.MainWindowTitle -ne "" -and
$_.ProcessName -ne "steam" -and
$_.Path -like "*steamapps*"
}
if ($steamApps) {
Write-Log "Steam game detected via steamapps path" -Level Info
return $true
}
}
return $false
}
function Get-SteamAppInfo {
<#
.SYNOPSIS
Gets app info from Steam API
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string[]]$AppId
)
$validIds = @()
foreach ($id in $AppId) {
if ($id -match '^\d+$') {
$validIds += $id
} else {
Write-Log "Invalid AppId format: $id. Must be numeric." -Level Error
}
}
if ($validIds.Count -eq 0) { return $null }
$appIdString = $validIds -join ','
$config = Get-SteamLibraryUpdaterConfig
$provider = "https://api.steamcmd.net/v1/info"
if ($config.Advanced -and $config.Advanced.UpdateCheckProvider) {
$provider = $config.Advanced.UpdateCheckProvider.TrimEnd("/")
}
$uri = "$provider/$AppId"
$maxAttempts = 4
$delay = 1
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
try {
$response = Invoke-RestMethod -Uri $uri -Method Get -TimeoutSec 30
return $response
}
catch {
if ($attempt -lt $maxAttempts) {
$errorMsg = $_.ToString()
Write-Log "Attempt $attempt failed to get app info for ${AppId}: $errorMsg - Retrying in $delay second(s)..." -Level Warning
Start-Sleep -Seconds $delay
$delay = [Math]::Min($delay * 2, 8)
}
else {
Write-Log "Error getting app info for $AppId after $maxAttempts attempts: $_" -Level Error
return $null
}
}
}
}
function Test-GameNeedsUpdate {
<#
.SYNOPSIS
Checks if a game needs an update
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$AppId
)
if ($AppId -notmatch '^\d+$') {
Write-Log "Invalid AppId: $AppId. Must be numeric." -Level Error
return $false
}
$appInfoFile = Join-Path $script:ModuleConfig.AppInfoPath $AppId
$appInfoFileNew = "$appInfoFile-new"
Write-Log "Checking for updates for App ID: $AppId" -Level Info
# Get current app info from Steam
$appInfo = $null
$appInfoResponse = Get-SteamAppInfo -AppId $AppId
if ($null -ne $appInfoResponse) {
$appInfo = $appInfoResponse.$AppId
}
if ($null -eq $appInfo) {
Write-Log "Failed to retrieve app info for $AppId" -Level Error
return $false
}
# Save new app info
$appInfo | ConvertTo-Json -Depth 10 | Set-Content $appInfoFileNew -Force
# Compare with previous version using hash for better performance
if (Test-Path $appInfoFile) {
$oldHash = (Get-FileHash -Path $appInfoFile -Algorithm SHA256).Hash
$newHash = (Get-FileHash -Path $appInfoFileNew -Algorithm SHA256).Hash
if ($oldHash -eq $newHash) {
Write-Log "App $AppId is up-to-date" -Level Info
Remove-Item $appInfoFileNew -Force
return $false
}
}
Write-Log "App $AppId needs update" -Level Info
return $true
}
function Update-SteamGame {
<#
.SYNOPSIS
Updates a Steam game using SteamCMD
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$AppId,
[Parameter(Mandatory)]
[string]$InstallDir
)
# Validate AppId is numeric to prevent command injection
if ($AppId -notmatch '^\d+$') {
Write-Log "Invalid AppId format: $AppId. Must be numeric." -Level Error
return $false
}
$config = Get-SteamLibraryUpdaterConfig
$steamCmdPath = $script:ModuleConfig.SteamCmdPath
if ($config.Advanced -and $config.Advanced.SteamCmdInstallPath) {
$steamCmdPath = $config.Advanced.SteamCmdInstallPath
}
if (-not (Test-Path $steamCmdPath)) {
Write-Log "SteamCMD not found at: $steamCmdPath" -Level Error
return $false
}
if (-not (Test-Path $InstallDir)) {
Write-Log "Install directory not found: $InstallDir" -Level Error
return $false
}
try {
Write-Log "Starting update for App $AppId to $InstallDir" -Level Info
# Windows PowerShell 5.1 can still split Start-Process argument arrays
# in surprising ways. Quote the install path explicitly so SteamCMD
# never interprets "C:\Program Files\..." as "C:\program".
$escapedInstallDir = $InstallDir -replace '"', '\"'
$argumentLine = "+force_install_dir `"$escapedInstallDir`" +login anonymous +app_update $AppId validate +quit"
$process = Start-Process -FilePath $steamCmdPath -ArgumentList $argumentLine -Wait -PassThru -NoNewWindow
if ($process.ExitCode -eq 0) {
Write-Log "Successfully updated App $AppId" -Level Info
# Update the stored app info
$appInfoFile = Join-Path $script:ModuleConfig.AppInfoPath $AppId
$appInfoFileNew = "$appInfoFile-new"
if (Test-Path $appInfoFileNew) {
Move-Item $appInfoFileNew $appInfoFile -Force
}
return $true
}
else {
Write-Log "SteamCMD exited with code: $($process.ExitCode)" -Level Error
# Clean up the -new file on failure to prevent false positives
$appInfoFile = Join-Path $script:ModuleConfig.AppInfoPath $AppId
$appInfoFileNew = "$appInfoFile-new"
if (Test-Path $appInfoFileNew) {
Remove-Item $appInfoFileNew -Force -ErrorAction SilentlyContinue
}
return $false
}
}
catch {
Write-Log "Error updating game: $_" -Level Error
# Clean up the -new file on failure to prevent false positives
$appInfoFile = Join-Path $script:ModuleConfig.AppInfoPath $AppId
$appInfoFileNew = "$appInfoFile-new"
if (Test-Path $appInfoFileNew) {
Remove-Item $appInfoFileNew -Force -ErrorAction SilentlyContinue
}
return $false
}
}
function Start-SteamLibraryUpdate {
<#
.SYNOPSIS
Main function to check and update Steam library
#>
[CmdletBinding()]
param(
[switch]$Force
)
Write-Log "Starting Steam Library update check" -Level Info
# Check if Steam is running
if (-not (Test-SteamRunning)) {
Write-Log "Steam is not running, exiting" -Level Info
return
}
# Get configuration
$config = Get-SteamLibraryUpdaterConfig
if (-not $config.EnableAutoUpdate) {
Write-Log "Auto-update is disabled in configuration" -Level Info
return
}
if ((-not $Force) -and $config.PSObject.Properties['UpdateCondition'] -and $config.UpdateCondition -ne "WhenAvailable") {
Write-Log "Automatic updates are not configured to run when available" -Level Info
return
}
# Check if we should update during gaming
$gameRunning = Test-GameRunning
if ($gameRunning -and -not $config.UpdateDuringGaming) {
Write-Log "Game is running and UpdateDuringGaming is false, skipping update" -Level Info
return
}
# Pre-fetch app info for all monitored games to reduce API calls
$batchAppIds = @($config.MonitoredGames | ForEach-Object { $_.AppId })
$batchAppInfoResponse = Get-SteamAppInfo -AppId $batchAppIds
$batchAppInfo = $null
if ($null -ne $batchAppInfoResponse -and $batchAppInfoResponse.PSObject.Properties['status'] -and $batchAppInfoResponse.status -eq 'success') {
$batchAppInfo = $batchAppInfoResponse.data
}
# Update each monitored game
foreach ($game in $config.MonitoredGames) {
try {
$gameAppInfo = if ($null -ne $batchAppInfo) { $batchAppInfo.$($game.AppId) } else { $null }
if (Test-GameNeedsUpdate -AppId $game.AppId -AppInfoData $gameAppInfo) {
Write-Log "Updating game: $($game.Name) (AppId: $($game.AppId))" -Level Info
$result = Update-SteamGame -AppId $game.AppId -InstallDir $game.InstallDir
if ($result) {
Write-Log "Successfully updated: $($game.Name)" -Level Info
}
else {
Write-Log "Failed to update: $($game.Name)" -Level Error
}
}
}
catch {
Write-Log "Error processing game $($game.Name): $_" -Level Error
}
}
# Update last check time
$config.LastCheck = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Set-SteamLibraryUpdaterConfig -Config $config
Write-Log "Steam Library update check completed" -Level Info
}
function Add-MonitoredGame {
<#
.SYNOPSIS
Adds a game to the monitored games list
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$AppId,
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[string]$InstallDir,
[string]$ProcessName
)
$config = Get-SteamLibraryUpdaterConfig
# Check if game already exists
$existing = $config.MonitoredGames | Where-Object { $_.AppId -eq $AppId }
if ($existing) {
Write-Log "Game with AppId $AppId already exists in configuration" -Level Warning
return $false
}
# Add new game
$newGame = [PSCustomObject]@{
AppId = $AppId
Name = $Name
InstallDir = $InstallDir
ProcessName = $ProcessName
Added = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
# Use ArrayList for better performance
$gamesList = [System.Collections.ArrayList]@($config.MonitoredGames)
$gamesList.Add($newGame) | Out-Null
$config.MonitoredGames = $gamesList.ToArray()
$result = Set-SteamLibraryUpdaterConfig -Config $config
if ($result) {
Write-Log "Added game to monitoring: $Name (AppId: $AppId)" -Level Info
}
return $result
}
function Remove-MonitoredGame {
<#
.SYNOPSIS
Removes a game from the monitored games list
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$AppId
)
$config = Get-SteamLibraryUpdaterConfig
$config.MonitoredGames = @($config.MonitoredGames | Where-Object { $_.AppId -ne $AppId })
$result = Set-SteamLibraryUpdaterConfig -Config $config
if ($result) {
Write-Log "Removed game with AppId: $AppId" -Level Info
}
return $result
}
function Find-SteamInstallPath {
<#
.SYNOPSIS
Automatically detects the Steam installation path
#>
[CmdletBinding()]
param()
# Check registry for Steam installation path
$registryPaths = @(
"HKCU:\Software\Valve\Steam",
"HKLM:\Software\Valve\Steam",
"HKLM:\Software\Wow6432Node\Valve\Steam"
)
foreach ($regPath in $registryPaths) {
if (Test-Path $regPath) {
$steamPath = (Get-ItemProperty -Path $regPath -Name "SteamPath" -ErrorAction SilentlyContinue).SteamPath
if ($steamPath -and (Test-Path $steamPath)) {
Write-Log "Found Steam installation at: $steamPath" -Level Info
return $steamPath
}
}
}
# Check common installation locations
$commonPaths = @(
"C:\Program Files (x86)\Steam",
"C:\Program Files\Steam",
"$env:ProgramFiles\Steam",
"${env:ProgramFiles(x86)}\Steam"
)
foreach ($path in $commonPaths) {
if (Test-Path $path) {
$steamExe = Join-Path $path "steam.exe"
if (Test-Path $steamExe) {
Write-Log "Found Steam installation at: $path" -Level Info
return $path
}
}
}
Write-Log "Could not automatically detect Steam installation" -Level Warning
return $null
}
function Get-SteamLibraryFolders {
<#
.SYNOPSIS
Gets all Steam library folders from the Steam configuration
#>
[CmdletBinding()]
param(
[string]$SteamPath
)
if (-not $SteamPath) {
$SteamPath = Find-SteamInstallPath
if (-not $SteamPath) {
return @()
}
}
$libraryFolders = @()
# Add the default Steam library
$defaultLibrary = Join-Path $SteamPath "steamapps"
if (Test-Path $defaultLibrary) {
$libraryFolders += $defaultLibrary
}
# Read libraryfolders.vdf to find additional libraries
$libraryVdf = Join-Path $SteamPath "steamapps\libraryfolders.vdf"
if (Test-Path $libraryVdf) {
try {
$content = Get-Content $libraryVdf -Raw
# Parse VDF format - look for paths
$matches = [regex]::Matches($content, '"path"\s+"([^"]+)"')
foreach ($match in $matches) {
$libPath = $match.Groups[1].Value
# VDF uses escaped backslashes - convert double backslashes to single
$libPath = $libPath.Replace('\\', '\')
$steamappsPath = Join-Path $libPath "steamapps"
if ((Test-Path $steamappsPath) -and ($libraryFolders -notcontains $steamappsPath)) {
$libraryFolders += $steamappsPath
}
}
}
catch {
Write-Log "Error parsing libraryfolders.vdf: $_" -Level Warning
}
}
return $libraryFolders
}
function Get-InstalledSteamGames {
<#
.SYNOPSIS
Scans Steam libraries and returns information about installed games
#>
[CmdletBinding()]
param(
[string]$SteamPath
)
if (-not $SteamPath) {
$SteamPath = Find-SteamInstallPath
if (-not $SteamPath) {
Write-Log "Could not find Steam installation" -Level Warning
return @()
}
}
$libraries = Get-SteamLibraryFolders -SteamPath $SteamPath
$games = @()
# Pre-compile regexes outside loops
$regexAppId = [regex]'"appid"\s+"(\d+)"'
$regexName = [regex]'"name"\s+"([^"]+)"'
$regexInstallDir = [regex]'"installdir"\s+"([^"]+)"'
foreach ($library in $libraries) {
# Look for .acf manifest files
$manifestFiles = Get-ChildItem -Path $library -Filter "appmanifest_*.acf" -File -ErrorAction SilentlyContinue
foreach ($manifest in $manifestFiles) {
try {
$content = Get-Content $manifest.FullName -Raw
# Parse basic ACF format
$matchAppId = $regexAppId.Match($content)
$appId = if ($matchAppId.Success) { $matchAppId.Groups[1].Value } else { $null }
$matchName = $regexName.Match($content)
$name = if ($matchName.Success) { $matchName.Groups[1].Value } else { $null }
$matchInstallDir = $regexInstallDir.Match($content)
$installDir = if ($matchInstallDir.Success) { $matchInstallDir.Groups[1].Value } else { $null }
if ($appId -and $name -and $installDir) {
$fullInstallPath = Join-Path $library "common\$installDir"
if (Test-Path $fullInstallPath) {
# Try to find the main executable
$exeFiles = Get-ChildItem -Path $fullInstallPath -Filter "*.exe" -File -ErrorAction SilentlyContinue |
Where-Object { $_.Name -notmatch '\b(unins|crash|setup|installer|launcher)\b' } |
Sort-Object Length -Descending |
Select-Object -First 3
$processName = $null
if ($exeFiles) {
# Use the first (largest) exe as a guess for the process name
$processName = [System.IO.Path]::GetFileNameWithoutExtension($exeFiles[0].Name)
}
$games += [PSCustomObject]@{
AppId = $appId
Name = $name
InstallDir = $fullInstallPath
ProcessName = $processName
Library = $library
}
}
}
}
catch {
Write-Log "Error parsing manifest $($manifest.Name): $_" -Level Warning
}
}
}
return $games
}
function Get-AcfValue {
param(
[Parameter(Mandatory)]
[string]$Content,
[Parameter(Mandatory)]
[string]$Name
)
$escapedName = [regex]::Escape($Name)
$match = [regex]::Match($Content, '"' + $escapedName + '"\s+"([^"]*)"')
if ($match.Success) {
return $match.Groups[1].Value
}
return $null
}
function Set-AcfValue {
param(
[Parameter(Mandatory)]
[string]$Content,
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[string]$Value
)
$escapedName = [regex]::Escape($Name)
$pattern = '("' + $escapedName + '"\s+")([^"]*)(")'
if ([regex]::IsMatch($Content, $pattern)) {
return [regex]::Replace($Content, $pattern, '${1}' + $Value + '${3}', 1)
}
return $Content
}
function Start-SteamQueuedUpdates {
<#
.SYNOPSIS
Nudges Steam's own queued client updates to run when available.
.DESCRIPTION
SteamCMD is not reliable for normal owned client games without account
credentials. This function updates Steam appmanifest scheduling metadata
so queued updates are eligible immediately, then opens Steam's Downloads
view to let the Steam client process the queue.
#>
[CmdletBinding()]
param()
$config = Get-SteamLibraryUpdaterConfig
if (-not $config.EnableAutoUpdate) {
Write-Log "Run with Steam is disabled; queued update nudge skipped" -Level Info
return [PSCustomObject]@{
Changed = 0
Queued = 0
Message = "Run with Steam is disabled"
}
}
if ($config.PSObject.Properties['UpdateCondition'] -and $config.UpdateCondition -ne "WhenAvailable") {
Write-Log "Update condition is $($config.UpdateCondition); queued update nudge skipped" -Level Info
return [PSCustomObject]@{
Changed = 0
Queued = 0
Message = "Update condition is not WhenAvailable"
}
}
if ((Test-GameRunning) -and -not $config.UpdateDuringGaming) {
Write-Log "Game is running and Allow updates while gaming is false; queued update nudge skipped" -Level Info
return [PSCustomObject]@{
Changed = 0
Queued = 0
Message = "Gaming detected"
}
}
$steamPath = $config.SteamInstallPath
if (-not $steamPath) {
$steamPath = Find-SteamInstallPath
}
if (-not $steamPath) {
Write-Log "Steam installation was not found; queued update nudge skipped" -Level Warning
return [PSCustomObject]@{
Changed = 0
Queued = 0
Message = "Steam not found"
}
}
$libraries = Get-SteamLibraryFolders -SteamPath $steamPath
$manifestFiles = foreach ($library in $libraries) {
Get-ChildItem -Path $library -Filter "appmanifest_*.acf" -File -ErrorAction SilentlyContinue
}
$changed = 0
$queued = 0
foreach ($manifest in $manifestFiles) {
try {
$content = Get-Content -LiteralPath $manifest.FullName -Raw
# Use pre-compiled regexes where possible to avoid repeated regex parsing inside Get-AcfValue
$matchAppId = $regexAppId.Match($content)
$appid = if ($matchAppId.Success) { $matchAppId.Groups[1].Value } else { $null }
$matchName = $regexName.Match($content)
$name = if ($matchName.Success) { $matchName.Groups[1].Value } else { $null }
$bytesToDownload = [int64]((Get-AcfValue -Content $content -Name "BytesToDownload") -as [int64])
$scheduledAutoUpdate = [int64]((Get-AcfValue -Content $content -Name "ScheduledAutoUpdate") -as [int64])
if ($bytesToDownload -le 0 -and $scheduledAutoUpdate -le 0) {
continue
}
$queued++
$updatedContent = $content
if ($scheduledAutoUpdate -ne 0) {
$updatedContent = Set-AcfValue -Content $updatedContent -Name "ScheduledAutoUpdate" -Value "0"
}
if ($updatedContent -ne $content) {
Set-Content -LiteralPath $manifest.FullName -Value $updatedContent -NoNewline -Force
$changed++
Write-Log "Queued Steam update made eligible now: $name (AppId: $appid)" -Level Info
}
}
catch {
Write-Log "Unable to process Steam manifest $($manifest.FullName): $_" -Level Warning
}
}
$steamExe = Join-Path $steamPath "steam.exe"
if (Test-Path $steamExe) {
Start-Process -FilePath $steamExe -ArgumentList "steam://open/downloads" -WindowStyle Minimized -ErrorAction SilentlyContinue | Out-Null
}
$config.LastCheck = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Set-SteamLibraryUpdaterConfig -Config $config | Out-Null
Write-Log "Steam queued update nudge completed. Queued manifests: $queued. Changed schedules: $changed." -Level Info
return [PSCustomObject]@{
Changed = $changed
Queued = $queued
Message = "Queued update nudge completed"
}
}
function Find-AppIdByName {
<#
.SYNOPSIS
Searches for a Steam App ID by game name using the Steam Web API
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$GameName
)
# Validate input to prevent malicious content
if ($GameName.Length -gt 200) {
Write-Log "Game name too long (max 200 characters)" -Level Warning
return $null
}
try {
# Use Steam store search API with proper URL encoding
$encodedName = [uri]::EscapeDataString($GameName)
$searchUrl = "https://store.steampowered.com/api/storesearch/?term=$encodedName&cc=US&l=english"
$response = Invoke-RestMethod -Uri $searchUrl -Method Get -TimeoutSec 30 -ErrorAction Stop
# Validate response structure
if ($null -eq $response) {
Write-Log "Received null response from Steam API" -Level Warning
return $null
}
if ($response.PSObject.Properties['total'] -and $response.total -gt 0 -and
$response.PSObject.Properties['items'] -and $response.items) {
# Return the first result as it's usually the most relevant
$topResult = $response.items[0]
# Validate required properties exist
if ($topResult.PSObject.Properties['id'] -and $topResult.PSObject.Properties['name']) {
return [PSCustomObject]@{
AppId = $topResult.id
Name = $topResult.name
Type = if ($topResult.PSObject.Properties['type']) { $topResult.type } else { "unknown" }
}
}
}
Write-Log "No valid results found for game '$GameName'" -Level Info
}
catch {
Write-Log "Error searching for game '$GameName': $_" -Level Warning
}
return $null