-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVortexModExport.ps1
More file actions
1510 lines (1271 loc) · 51.8 KB
/
Copy pathVortexModExport.ps1
File metadata and controls
1510 lines (1271 loc) · 51.8 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
[CmdletBinding()]
param()
# ==========================================================
# Global cache
# ==========================================================
$script:UVME_SnapshotCache = $null
$script:UVME_DuplicatesFixed = $false
# Small helper to safely get a trimmed string
function Get-UVMEString {
param($Value)
if ($null -eq $Value) { return $null }
return ([string]$Value).Trim()
}
function Fix-UVMEJsonDuplicates {
param(
[Parameter(Mandatory = $true)]
[string]$Json
)
# Fast path: if JSON parses fine, don't touch it
try {
$null = $Json | ConvertFrom-Json -ErrorAction Stop
return $Json
} catch {
if ($_.Exception.Message -notmatch 'Duplicate.*property name') {
# It's failing for some other reason; let the caller see that
throw
}
}
Write-Host "UVME: Detected duplicate JSON property names. Attempting generic de-duplication..." -ForegroundColor Yellow
$text = $Json
$len = $text.Length
$sb = New-Object System.Text.StringBuilder
$stack = New-Object System.Collections.Stack
# Each object on the stack holds a case-insensitive set of keys seen at that object level
$stack.Push(@{})
$inString = $false
$escape = $false
for ($i = 0; $i -lt $len; $i++) {
$ch = $text[$i]
if ($inString) {
# Inside string: just copy and track escapes
$sb.Append($ch) | Out-Null
if ($escape) {
$escape = $false
} elseif ($ch -eq '\') {
$escape = $true
} elseif ($ch -eq '"') {
$inString = $false
}
continue
}
switch ($ch) {
'{' {
$sb.Append($ch) | Out-Null
# New object scope, new key set
$stack.Push(@{})
continue
}
'}' {
$sb.Append($ch) | Out-Null
if ($stack.Count -gt 1) {
$null = $stack.Pop()
}
continue
}
'"' {
# Potential start of a key or a string value
# Look backwards for the previous non-whitespace char
$j = $i - 1
while ($j -ge 0 -and [char]::IsWhiteSpace($text[$j])) { $j-- }
$prev = if ($j -ge 0) { $text[$j] } else { [char]0 }
# Find the closing quote for this string, respecting escapes
$k = $i + 1
$esc2 = $false
while ($k -lt $len) {
$c2 = $text[$k]
if ($esc2) {
$esc2 = $false
} elseif ($c2 -eq '\') {
$esc2 = $true
} elseif ($c2 -eq '"') {
break
}
$k++
}
if ($k -ge $len) {
# Malformed JSON – just fall back to normal string handling
$inString = $true
$sb.Append($ch) | Out-Null
continue
}
# Look forward to see if this string is followed by a colon => it's a property name
$l = $k + 1
while ($l -lt $len -and [char]::IsWhiteSpace($text[$l])) { $l++ }
$next = if ($l -lt $len) { $text[$l] } else { [char]0 }
$isKey = ($prev -eq '{' -or $prev -eq ',') -and $next -eq ':'
if ($isKey -and $stack.Count -gt 0) {
# This is a property name at the current object depth
$keyText = $text.Substring($i + 1, $k - $i - 1)
$ctx = $stack.Peek()
$keyNorm = $keyText.ToLowerInvariant()
if ($ctx.ContainsKey($keyNorm)) {
# Duplicate key in this object: rename this occurrence
$baseName = $keyText
$suffixIndex = 1
$newName = $null
while ($true) {
$candidate = if ($suffixIndex -eq 1) {
"$baseName (Duplicate)"
} else {
"$baseName (Duplicate $suffixIndex)"
}
$candNorm = $candidate.ToLowerInvariant()
if (-not $ctx.ContainsKey($candNorm)) {
$newName = $candidate
$ctx[$candNorm] = $true
break
}
$suffixIndex++
}
Write-Host "UVME: Renaming duplicate JSON key '$keyText' => '$newName'." -ForegroundColor Yellow
# Write the renamed key (with quotes)
$sb.Append('"').Append($newName).Append('"') | Out-Null
} else {
# First time we've seen this key at this depth
$ctx[$keyNorm] = $true
# Copy original key string including quotes
$sb.Append($text, $i, $k - $i + 1) | Out-Null
}
# Skip the characters we just handled inside the quotes
$i = $k
continue
} else {
# Normal string value, not a key
$inString = $true
$sb.Append($ch) | Out-Null
continue
}
}
default {
$sb.Append($ch) | Out-Null
continue
}
}
}
$fixed = $sb.ToString()
# Try again after fixing duplicates
try {
$null = $fixed | ConvertFrom-Json -ErrorAction Stop
Write-Host "UVME: JSON de-duplication complete." -ForegroundColor Yellow
return $fixed
} catch {
Write-Host "UVME: JSON still failed to parse after de-duplication: $($_.Exception.Message)" -ForegroundColor Red
throw
}
}
# ==========================================================
# Helpers
# ==========================================================
function Format-GameLabel {
param(
[Parameter(Mandatory = $true)]
[string]$Name
)
$t = $Name -replace '_', ' '
if ($t -match '^(.*?)(\d+)$') {
$t = "$($matches[1]) $($matches[2])"
}
$culture = [System.Globalization.CultureInfo]::CurrentCulture
$label = $culture.TextInfo.ToTitleCase($t.ToLower())
return $label.Trim()
}
function Get-VortexSnapshot {
if ($script:UVME_SnapshotCache) {
return $script:UVME_SnapshotCache
}
$pattern = Join-Path $env:APPDATA "Vortex\temp\state_backups_full\*.json"
$latest = Get-ChildItem -Path $pattern -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
if (-not $latest) {
throw "No Vortex backup JSON found at $pattern. Start/restart Vortex so it writes a full state backup, then run UVME again."
}
$rawJson = Get-Content -Path $latest.FullName -Raw
# Generic duplicate-key handling (for any game / section)
if (-not $script:UVME_DuplicatesFixed) {
$rawJson = Fix-UVMEJsonDuplicates -Json $rawJson
$script:UVME_DuplicatesFixed = $true
}
$snapshot = $rawJson | ConvertFrom-Json
$script:UVME_SnapshotCache = $snapshot
return $snapshot
}
function Get-UVMEGameOverview {
param(
[Parameter(Mandatory = $true)]
$Snapshot
)
$modsRoot = $Snapshot.persistent.mods
$profilesRoot = $Snapshot.persistent.profiles
$lastActiveMap = $Snapshot.settings.profiles.lastActiveProfile
if (-not $modsRoot) {
throw "Snapshot has no persistent.mods section."
}
$rows = @()
foreach ($gameProp in $modsRoot.PSObject.Properties) {
$gameName = $gameProp.Name
$modBlock = $gameProp.Value
if (-not $modBlock) { continue }
$profileId = $null
if ($lastActiveMap -and $lastActiveMap.PSObject.Properties.Name -contains $gameName) {
$profileId = $lastActiveMap.$gameName
}
$profileState = $null
if ($profileId -and $profilesRoot -and $profilesRoot.PSObject.Properties.Name -contains $profileId) {
$profileState = $profilesRoot.$profileId.modState
}
$deployIndex = 0
foreach ($modProp in $modBlock.PSObject.Properties) {
$modKey = $modProp.Name
$m = $modProp.Value
if (-not $m) {
$deployIndex++
continue
}
$attrs = $m.attributes
# Enabled flag from profile
$enabled = $false
if ($profileState -and $profileState.PSObject.Properties.Name -contains $modKey) {
$enabled = [bool]$profileState.$modKey.enabled
}
# Raw attribute values from Vortex
$rawLogical = $null
$rawModName = $null
$rawName = $null
if ($attrs) {
if ($attrs.logicalFileName) {
$tmp = Get-UVMEString $attrs.logicalFileName
if ($tmp.Length -gt 0) { $rawLogical = $tmp }
}
if ($attrs.modName) {
$tmp = Get-UVMEString $attrs.modName
if ($tmp.Length -gt 0) { $rawModName = $tmp }
}
if ($attrs.name) {
$tmp = Get-UVMEString $attrs.name
if ($tmp.Length -gt 0) { $rawName = $tmp }
}
}
# --- Vortex-like display name selection ---
if ($rawLogical) {
# 1) Prefer the same field Vortex shows in the Mods list
$modName = Get-UVMEString $rawLogical
}
elseif ($rawModName) {
# 2) Fall back to mod/page name
$modName = Get-UVMEString $rawModName
}
elseif ($rawName) {
# 3) Last resort: archive-ish name, cleaned up
$clean = Get-UVMEString $rawName
# Strip patterns like "-5124-3-09-1739477203" at the end
$clean = $clean -replace '-\d+-\d+(?:-\d+)*-\d{9,}$',''
# Strip common archive extensions
$clean = $clean -replace '\.(zip|rar|7z|7zip)$',''
$modName = Get-UVMEString $clean
}
else {
# Fallback when literally nothing is usable
if ($m.type) {
$typeStr = Get-UVMEString $m.type
if ($typeStr.Length -gt 0) {
$modName = "[Tool entry - $typeStr]"
} else {
$modName = "[Unnamed entry - Vortex has no mod name]"
}
} else {
$modName = "[Unnamed entry - Vortex has no mod name]"
}
}
# BaseModName used for grouping: prefer mod/page name, then final display name
$baseName = Get-UVMEString $rawModName
if (-not $baseName -or $baseName.Length -eq 0) {
$baseName = Get-UVMEString $modName
}
# Version: prefer per-file version, then global modVersion
$fileVersion = $null
$globalVersion = $null
$modVersion = $null
if ($attrs) {
if ($attrs.version) {
$tmp = Get-UVMEString $attrs.version
if ($tmp.Length -gt 0) { $fileVersion = $tmp }
}
if ($attrs.modVersion) {
$tmp = Get-UVMEString $attrs.modVersion
if ($tmp.Length -gt 0) { $globalVersion = $tmp }
}
}
if ($fileVersion) {
$modVersion = $fileVersion
} elseif ($globalVersion) {
$modVersion = $globalVersion
} else {
$modVersion = "[no version in Vortex]"
}
# Source + homepage + Nexus ID
$source = $null
$homepage = $null
$modNumericId = $null
$downloadGame = $null
if ($attrs) {
if ($attrs.PSObject.Properties.Name -contains 'source') {
$tmp = Get-UVMEString $attrs.source
if ($tmp.Length -gt 0) { $source = $tmp }
}
if ($attrs.PSObject.Properties.Name -contains 'homepage') {
$tmp = Get-UVMEString $attrs.homepage
if ($tmp.Length -gt 0) { $homepage = $tmp }
}
if ($attrs.PSObject.Properties.Name -contains 'modId') {
$modNumericId = [string]$attrs.modId
}
if ($attrs.PSObject.Properties.Name -contains 'downloadGame') {
$downloadGame = Get-UVMEString $attrs.downloadGame
}
}
$rows += [PSCustomObject]@{
GameName = $gameName
ModId = $m.id
ModKey = $modKey
ModName = $modName
BaseModName = $baseName
ModVersion = $modVersion
Enabled = $enabled
LoadOrder = [string]$deployIndex
Source = $source
Homepage = $homepage
FileVersion = $fileVersion
GlobalVersion = $globalVersion
RawLogicalName = $rawLogical
RawModName = $rawModName
RawName = $rawName
ModNumericId = $modNumericId
DownloadGame = $downloadGame
}
$deployIndex++
}
}
return $rows
}
function Normalize-UVMEPartMods {
param(
[Parameter(Mandatory = $true)]
[System.Collections.IEnumerable]$Mods
)
$modsArray = @()
foreach ($m in $Mods) { $modsArray += $m }
$result = @()
# Group by Game + Homepage (Nexus page) for per-mod grouping
$groups = $modsArray | Group-Object GameName, Homepage
foreach ($g in $groups) {
$items = $g.Group
if ($items.Count -eq 0) { continue }
# Detect "Part N" entries in this group
$partInfos = @()
foreach ($row in $items) {
$partNum = $null
$candidates = @(
$row.RawLogicalName,
$row.RawModName,
$row.RawName,
$row.ModName,
$row.ModKey
) | Where-Object { $_ -and (Get-UVMEString $_).Length -gt 0 }
foreach ($txt in $candidates) {
$txtStr = Get-UVMEString $txt
if ($txtStr -match '(?i)\(Part\s+0*([0-9]+)\)') {
$partNum = [int]$matches[1]
break
} elseif ($txtStr -match '(?i)\bpart\s+0*([0-9]+)\b') {
$partNum = [int]$matches[1]
break
}
}
if ($partNum -ne $null) {
$partInfos += [PSCustomObject]@{
Row = $row
PartNum = $partNum
}
}
}
if ($partInfos.Count -lt 2) {
# Not clearly multi-part -> keep all rows as-is
$result += $items
continue
}
# We have a multi-part mod: derive a base name
$baseCandidates = @()
foreach ($row in $items) {
$nameFields = @(
$row.RawModName,
$row.RawLogicalName,
$row.RawName,
$row.BaseModName,
$row.ModName
) | Where-Object { $_ -and (Get-UVMEString $_).Length -gt 0 }
foreach ($txt in $nameFields) {
$t = Get-UVMEString $txt
if ($t -match '(?i)^(.*?)(?:\s*[-–]\s*)?part\s+0*[0-9]+.*$') {
$base = Get-UVMEString $matches[1]
if ($base.Length -gt 2) {
$baseCandidates += $base
}
}
}
}
if ($baseCandidates.Count -eq 0) {
$baseCandidates = $items |
ForEach-Object { $_.BaseModName, $_.RawModName, $_.RawName, $_.ModName } |
Where-Object { $_ -and (Get-UVMEString $_).Length -gt 0 } |
Sort-Object { (Get-UVMEString $_).Length } -Descending -Unique
}
if ($baseCandidates.Count -eq 0) {
# Can't confidently determine base name; keep rows as-is
$result += $items
continue
}
$baseName = Get-UVMEString $baseCandidates[0]
if ($baseName -match '(?i)^(.*?)(?:\s*[-–]\s*)?part\s+0*[0-9]+.*$') {
$baseName = Get-UVMEString $matches[1]
}
if (-not $baseName -or $baseName.Length -le 2) {
$result += $items
continue
}
# Determine unified version for the mod
$globalVers = $partInfos |
ForEach-Object { Get-UVMEString $_.Row.GlobalVersion } |
Where-Object { $_ -and $_.Length -gt 0 } |
Select-Object -Unique
$commonVersion = $null
if ($globalVers.Count -ge 1) {
$commonVersion = $globalVers[0]
} else {
$freq = $partInfos |
Group-Object { Get-UVMEString $_.Row.ModVersion } |
Sort-Object Count -Descending
if ($freq.Count -ge 1) {
$commonVersion = Get-UVMEString $freq[0].Name
}
}
if (-not $commonVersion -or -not (Get-UVMEString $commonVersion)) {
$commonVersion = $items[0].ModVersion
}
# Aggregate Enabled + LoadOrder
$anyEnabled = ($items | Where-Object { $_.Enabled }) -ne $null
# Only consider rows with a numeric load order, then pick the smallest
$itemsWithNumericLoad = $items | Where-Object {
$_.LoadOrder -ne $null -and
$_.LoadOrder.ToString().Trim() -match '^\d+$'
}
if ($itemsWithNumericLoad.Count -gt 0) {
$representative = $itemsWithNumericLoad |
Sort-Object { [int]$_.LoadOrder } |
Select-Object -First 1
$minLoad = [int]$representative.LoadOrder
} else {
# Fallback if all load orders are missing/invalid
$representative = $items | Select-Object -First 1
$minLoad = 0
}
# Use any homepage present within this multi-part group
$groupHomepages = $items |
ForEach-Object { Get-UVMEString $_.Homepage } |
Where-Object { $_ -and $_.Length -gt 0 } |
Select-Object -Unique
$aggHomepage = if ($groupHomepages.Count -gt 0) { $groupHomepages[0] } else { $representative.Homepage }
$agg = [PSCustomObject]@{
GameName = $representative.GameName
ModId = $representative.ModId
ModKey = $representative.ModKey
ModName = $baseName
BaseModName = $baseName
ModVersion = $commonVersion
Enabled = [bool]$anyEnabled
LoadOrder = [string]$minLoad
Source = $representative.Source
Homepage = $aggHomepage
FileVersion = $representative.FileVersion
GlobalVersion = $representative.GlobalVersion
RawLogicalName = $representative.RawLogicalName
RawModName = $representative.RawModName
RawName = $representative.RawName
ModNumericId = $representative.ModNumericId
DownloadGame = $representative.DownloadGame
}
# Only one row for this multi-part mod
$result += $agg
}
return $result
}
function Normalize-UVMEGenericLabels {
param(
[Parameter(Mandatory = $true)]
[System.Collections.IEnumerable]$Mods
)
$modsArray = @()
foreach ($m in $Mods) { $modsArray += $m }
$groups = $modsArray | Group-Object GameName, RawLogicalName
foreach ($g in $groups) {
$items = $g.Group
if ($items.Count -lt 2) { continue }
$logical = Get-UVMEString $items[0].RawLogicalName
if (-not $logical -or $logical.Length -eq 0) { continue }
# Distinct mod names or homepages?
$distinctModNames = $items |
ForEach-Object { Get-UVMEString $_.RawModName } |
Where-Object { $_ -and $_.Length -gt 0 } |
Select-Object -Unique
$distinctHomes = $items |
ForEach-Object { Get-UVMEString $_.Homepage } |
Where-Object { $_ -and $_.Length -gt 0 } |
Select-Object -Unique
if ($distinctModNames.Count -lt 2 -and $distinctHomes.Count -lt 2) {
continue
}
# Promote mod/page name (or archive name) so rows are distinguishable
foreach ($row in $items) {
$candidate = Get-UVMEString $row.RawModName
if (-not $candidate -or $candidate.Length -eq 0) {
$candidate = Get-UVMEString $row.RawName
}
if ($candidate -and $candidate.Length -gt 0) {
$row.ModName = $candidate
$row.BaseModName = $candidate
}
}
}
return $modsArray
}
function Normalize-UVMEHomepages {
param(
[Parameter(Mandatory = $true)]
[System.Collections.IEnumerable]$Mods
)
# Materialize into an array so we can do multi-pass logic
$modsArray = @()
foreach ($m in $Mods) { $modsArray += $m }
#
# STEP 1: Propagate homepage within (GameName, ModNumericId) groups
# e.g. all SBS files share modId = 84015
#
$groups = $modsArray |
Where-Object {
$_.ModNumericId -and (Get-UVMEString $_.ModNumericId).Length -gt 0
} |
Group-Object GameName, ModNumericId
foreach ($g in $groups) {
$items = $g.Group
if ($items.Count -lt 2) { continue }
# Any homepage in this modId group?
$homepages = $items |
ForEach-Object { Get-UVMEString $_.Homepage } |
Where-Object { $_ -and $_.Length -gt 0 } |
Select-Object -Unique
if ($homepages.Count -lt 1) { continue }
$chosen = $homepages[0]
foreach ($row in $items) {
$cur = Get-UVMEString $row.Homepage
if (-not $cur -or $cur.Length -eq 0) {
$row.Homepage = $chosen
}
}
}
#
# STEP 2: Learn Nexus "game slug" per GameName from existing homepages
# Example:
# https://www.nexusmods.com/fallout4/mods/84015/
# https://www.nexusmods.com/skyrimspecialedition/mods/32444/
# -> slugs: fallout4, skyrimspecialedition
#
$slugByGame = @{}
foreach ($row in $modsArray) {
$src = Get-UVMEString $row.Source
$hp = Get-UVMEString $row.Homepage
if (-not $src -or $src.ToLower() -ne 'nexus') { continue }
if (-not (Test-UVMEIsUrl $hp)) { continue }
$m = [regex]::Match(
$hp,
'^https?://(?:www\.)?nexusmods\.com/([^/]+)/mods/\d+/?'
)
if ($m.Success) {
$gameName = $row.GameName
if ($gameName -and -not $slugByGame.ContainsKey($gameName)) {
$slugByGame[$gameName] = $m.Groups[1].Value
}
}
}
#
# STEP 3: For any remaining Nexus mods with a numeric modId but no homepage,
# synthesize the URL from (slug, modId).
#
foreach ($row in $modsArray) {
$src = Get-UVMEString $row.Source
if (-not $src -or $src.ToLower() -ne 'nexus') { continue }
$hp = Get-UVMEString $row.Homepage
if (Test-UVMEIsUrl $hp) { continue } # already has a real URL
$modIdStr = Get-UVMEString $row.ModNumericId
if (-not $modIdStr -or -not ($modIdStr -match '^\d+$')) { continue }
$gameName = $row.GameName
if (-not $gameName) { continue }
if (-not $slugByGame.ContainsKey($gameName)) { continue }
$slug = $slugByGame[$gameName]
$row.Homepage = "https://www.nexusmods.com/$slug/mods/$modIdStr/"
}
return $modsArray
}
function Get-UVMEArchiveOverview {
param(
[Parameter(Mandatory = $true)]
$Snapshot,
[string]$GameFilter
)
$downloadsNode = $Snapshot.persistent.downloads
if (-not $downloadsNode -or -not $downloadsNode.files) {
return @()
}
$downloadsRoot = Join-Path $env:APPDATA "Vortex\downloads"
$rows = @()
foreach ($fileProp in $downloadsNode.files.PSObject.Properties) {
$entry = $fileProp.Value
if (-not $entry) { continue }
$gamesForFile = $entry.game
if (-not $gamesForFile) { continue }
foreach ($g in $gamesForFile) {
if ($GameFilter -and $g -ne $GameFilter) { continue }
$gameName = $g
$fileName = $entry.localPath
$modName = $null
if ($entry.modInfo -and $entry.modInfo.name) {
$modName = Get-UVMEString $entry.modInfo.name
}
$sizeBytes = [double]($entry.size | ForEach-Object { $_ })
$sizeMB = if ($sizeBytes -gt 0) { [math]::Round($sizeBytes / 1MB, 2) } else { 0 }
$modified = $null
if ($entry.fileTime) {
try {
$modified = [DateTimeOffset]::FromUnixTimeMilliseconds([int64]$entry.fileTime).LocalDateTime
} catch {
$modified = $null
}
}
$fullPath = $null
$exists = $false
if ($fileName) {
$gameFolder = $gameName
$fullPath = Join-Path (Join-Path $downloadsRoot $gameFolder) $fileName
$exists = Test-Path -LiteralPath $fullPath
}
$rows += [PSCustomObject]@{
GameName = $gameName
FileName = $fileName
ModName = $modName
SizeMB = $sizeMB
Modified = $modified
ExistsOnDisk = $exists
FullPath = $fullPath
}
}
}
return $rows
}
function New-ExcelLayout {
param(
[Parameter(Mandatory = $true)]
$Sheet
)
try {
$ps = $Sheet.PageSetup
$ps.Orientation = 2
$ps.Zoom = 100
} catch { }
try {
$Sheet.Columns.Item(1).ColumnWidth = 12
$Sheet.Columns.Item(2).ColumnWidth = 45
$Sheet.Columns.Item(3).ColumnWidth = 18
$Sheet.Columns.Item(4).ColumnWidth = 10
$Sheet.Columns.Item(5).ColumnWidth = 12
$Sheet.Columns.Item(6).ColumnWidth = 12
$Sheet.Columns.Item(7).ColumnWidth = 70
$Sheet.Columns.Item(2).WrapText = $true
} catch { }
}
function Test-UVMEIsUrl {
param(
[AllowNull()]
$Value
)
if (-not $Value) { return $false }
$s = Get-UVMEString $Value
if (-not $s) { return $false }
return ($s.StartsWith("http://") -or $s.StartsWith("https://") -or $s.StartsWith("ftp://"))
}
# ---------- New helpers for robust user input ----------
function Read-UVMEChoice {
param(
[Parameter(Mandatory = $true)]
[string]$Prompt,
[Parameter(Mandatory = $true)]
[string[]]$ValidValues
)
while ($true) {
$choice = Read-Host $Prompt
if ($ValidValues -contains $choice) {
return $choice
}
Write-Host "Invalid selection. Please choose one of: $($ValidValues -join ', ')." -ForegroundColor Red
}
}
function Read-UVMEGameSelection {
param(
[Parameter(Mandatory = $true)]
[System.Collections.IEnumerable]$Games
)
while ($true) {
$raw = Read-Host "Enter a game number to scope to that game, or press Enter for ALL games"
if ([string]::IsNullOrWhiteSpace($raw)) {
return $null # All games
}
if ($raw -as [int]) {
$idx = [int]$raw
$selected = $Games | Where-Object { $_.Index -eq $idx }
if ($selected) {
return $selected
}
}
Write-Host "That isn't a valid game selection. Please enter a number from 1 to $($Games.Count), or press Enter for ALL games." -ForegroundColor Red
}
}
# ==========================================================
# Main
# ==========================================================
Write-Host ""
Write-Host "Universal Vortex Mod Exporter (UVME)" -ForegroundColor Cyan
Write-Host "-----------------------------------" -ForegroundColor Cyan
Write-Host ""
Write-Host "TIP:" -ForegroundColor Yellow
Write-Host " If you've recently changed mods/games in Vortex," -ForegroundColor Yellow
Write-Host " restart Vortex so it writes a fresh backup before running UVME." -ForegroundColor Yellow
Write-Host ""
try {
$snapshot = Get-VortexSnapshot
} catch {
Write-Error $_
exit 1
}
try {
$allMods = Get-UVMEGameOverview -Snapshot $snapshot
$allMods = Normalize-UVMEPartMods -Mods $allMods
$allMods = Normalize-UVMEGenericLabels -Mods $allMods
$allMods = Normalize-UVMEHomepages -Mods $allMods
} catch {
Write-Error $_
exit 1
}
if (-not $allMods -or $allMods.Count -eq 0) {
Write-Error "No mods found in Vortex snapshots."
exit 1
}
$gamesGrouped = $allMods | Select-Object GameName -Unique | Sort-Object GameName
$games = @()
$idx = 1
foreach ($g in $gamesGrouped) {
$games += [PSCustomObject]@{
Index = $idx
GameName = $g.GameName
DisplayName = Format-GameLabel $g.GameName
}
$idx++
}
Write-Host ""
Write-Host "Games found in Vortex backup:" -ForegroundColor Yellow
foreach ($g in $games) {
Write-Host ("{0}) {1}" -f $g.Index, $g.DisplayName)
}
Write-Host ""
$selection = Read-UVMEGameSelection -Games $games
$selectedGame = $null
if ($null -eq $selection) {
# ALL games
$modsForScope = $allMods
$gameLabel = "AllGames"
} else {
$selectedGame = $selection.GameName
$modsForScope = $allMods | Where-Object { $_.GameName -eq $selectedGame }
$gameLabel = $selection.DisplayName
if (-not $modsForScope -or $modsForScope.Count -eq 0) {
Write-Host "No mods found for game '$($selection.DisplayName)'." -ForegroundColor Red
exit 1
}
}
Write-Host ""
Write-Host "What would you like to export?" -ForegroundColor Cyan
Write-Host "1) Installed / managed mods from Vortex (current state)"
Write-Host "2) Download archives from Vortex 'downloads' folder"
$exportMode = Read-UVMEChoice -Prompt "Choose 1 or 2" -ValidValues @("1","2")
if ($exportMode -eq "2") {
# ------------------------------------------------------
# Download archive export
# ------------------------------------------------------
$archives = Get-UVMEArchiveOverview -Snapshot $snapshot -GameFilter $selectedGame
if (-not $archives -or $archives.Count -eq 0) {
Write-Host ""
Write-Host "No archive files found in the Vortex download folder(s) for the chosen scope." -ForegroundColor Yellow
exit 0
}
Write-Host ""
Write-Host "1) Export as JSON"
Write-Host "2) Export as Excel (XLSX)"
Write-Host "3) Export as HTML (sortable, with full paths)"
$formatChoice = Read-UVMEChoice -Prompt "Choose 1, 2 or 3" -ValidValues @("1","2","3")
$basePath = $PSScriptRoot
if (-not $basePath) { $basePath = (Get-Location).Path }
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$fileBase = if ($selectedGame) { "$(Format-GameLabel $selectedGame)_Downloads_$timestamp" } else { "AllGames_Downloads_$timestamp" }
$sortedArchives = $archives | Sort-Object GameName, FileName