-
Notifications
You must be signed in to change notification settings - Fork 0
/
simfile-tool.ps1
530 lines (471 loc) · 19 KB
/
simfile-tool.ps1
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
<# Sukibaby's Simfile Tool
https://github.com/sukibaby/simfile-tool
You can run the script directly like so:
PS C:\Users\Stepper\Downloads> & '.\simfile-tool.ps1' "C:\Games\StepMania 5\Songs\In The Groove" #>
#region BEGINNING OF PROGRAM
param(
[Parameter(Position = 0)]
[string]$directoryToUse
)
if ($null -ne $directoryToUse) {
$directoryToUse = $directoryToUse.Replace("`"","'")
}
$ARTARRAY = @(
@(
" ",
" ( ( ) ( ",
" ( ) )\ ) ( )\ ( ( /( )\ ",
" ( )\ ( (()/( )\((_)))\ )\())( ( ((_) ",
" )((_) )\ '/(_)|(_)_ /((_) (_))/ )\ )\ _ ",
"((_|_)_((_))(_) _|(_) (_)) | |_ ((_) ((_) | ",
"(_-< | ' \()| _|| | / -_) | _/ _ \/ _ \ | ",
"/__/_|_|_|_| |_| |_|_\___| \__\___/\___/_| "
),
@(
" _ _",
" /) // _/_ //",
" _ o ______ // o // _ / __ __ //",
"/_)_<_/ / / <_//_<_</_</_ <__(_)(_)</_",
" />",
" </"
),
@(
"____ _ _ _ ____ _ _ ____ ___ ____ ____ _ ",
"[__ | |\/| |___ | | |___ | | | | | | ",
"___] | | | | | |___ |___ | |__| |__| |___"
)
)
$randomArt = Get-Random -InputObject $ARTARRAY
foreach ($line in $randomArt) {
Write-Host $line
}
Write-Host " ~ Version 8/20/2024 ~"
Write-Host "Check for new versions at:"
Write-Host "https://github.com/sukibaby/simfile-tool"
Write-Host ""
Write-Host "Be sure to make a backup of your files first."
Write-Host ""
#endregion
#region FUNCTION Get-Directory
function Get-Directory {
param($dir)
if (!$dir -or $dir -eq "" -or !(Test-Path $dir -PathType Container)) {
Write-Host "No directory provided."
if (!$dir -or $dir -eq "" -or !(Test-Path $dir -PathType Container)) {
Write-Host "Press any key to exit..."
$null = Read-Host
exit
}
}
return $dir
}
# METHOD TO GET DIRECTORY
$directoryToUse = Get-Directory -dir $directoryToUse
if ($null -eq $directoryToUse) {
return
}
#endregion
#region FUNCTION Draw-Separator
function Draw-Separator {
Write-Host ""
Write-Host "--------------------------------------------------"
Write-Host ""
}
#endregion
#region FUNCTION Get-Files
function Get-Files {
param($dir,$rec)
Get-ChildItem $dir -Include *.sm,*.ssc -Recurse:$rec
}
#endregion
#region FUNCTION Update-Field-Capitalization, Update-Capitalization, Update-Capitalization-StepArtist
# The result of the prompt is a global variable so it can be reused if Update-Capitalization-StepArtist is called.
function Update-Field-Capitalization {
param($dir,$rec,$field)
Write-Host ""
$changeField = Read-Host -Prompt "Would you like to change the $field field capitalization? (yes/no, default is no)"
if ($changeField -eq 'yes') {
$global:capitalizationPromptAnswer = Read-Host -Prompt "Please enter one of the following switches: u (uppercase), t (title case), l (lower case)"
if ($global:capitalizationPromptAnswer -notin @("u","t","l")) {
Write-Error "Invalid switch: $global:capitalizationPromptAnswer. Please provide one of the following switches: u (uppercase), t (title case), l (lower case)"
return
}
if ($field -eq "STEPARTIST") {
Update-Capitalization-StepArtist -StepArtist_dir $dir -StepArtist_rec $rec
} else {
Update-Capitalization -dir $dir -rec $rec -field $field -switch $global:capitalizationPromptAnswer
}
}
}
function Update-Capitalization {
param($dir,$rec,$field,$switch)
$files = Get-Files -dir $dir -rec $rec
foreach ($file in $files) {
$content = Get-Content -LiteralPath $file.FullName
$found = $false
$content = $content | ForEach-Object {
if (!$found -and ($_ -match "#$field")) {
$parts = $_ -split ':'
switch ($switch) {
"u" { $parts[1] = $parts[1].ToUpper() }
"t" { $parts[1] = (Get-Culture).TextInfo.ToTitleCase($parts[1].ToLower()) }
"l" { $parts[1] = $parts[1].ToLower() }
}
$found = $true
return ($parts -join ':')
}
return $_
}
Set-Content -Path $file.FullName -Value $content
}
}
function Update-Capitalization-StepArtist {
param($StepArtist_dir,$StepArtist_rec)
$StepArtist_files = Get-Files -dir $StepArtist_dir -rec $StepArtist_rec
foreach ($StepArtist_file in $StepArtist_files) {
$StepArtist_content = Get-Content -LiteralPath $StepArtist_file.FullName
for ($i = 0; $i -lt $StepArtist_content.Length; $i++) {
if ($StepArtist_content[$i] -match "//---------------(dance-.*) - (.*?)----------------") {
$matchedGroup = $Matches[2]
switch ($global:capitalizationPromptAnswer) {
"u" { $StepArtist_content[$i] = $StepArtist_content[$i].Replace($matchedGroup,$matchedGroup.ToUpper()) }
"t" { $StepArtist_content[$i] = $StepArtist_content[$i].Replace($matchedGroup,(Get-Culture).TextInfo.ToTitleCase($matchedGroup.ToLower())) }
"l" { $StepArtist_content[$i] = $StepArtist_content[$i].Replace($matchedGroup,$matchedGroup.ToLower()) }
}
}
}
Set-Content -Path $StepArtist_file.FullName -Value $StepArtist_content
}
}
#endregion
#region FUNCTION Update-Content, Update-Offset, Update-File
function Update-Content {
param($dir,$rec,$pattern,$replacement)
$files = Get-Files -dir $dir -rec $rec
foreach ($file in $files) {
$content = Get-Content -LiteralPath $file.FullName
for ($i = 0; $i -lt $content.Length; $i++) {
if ($content[$i] -match $pattern) {
$content[$i] = $content[$i] -replace $pattern,$replacement
break
}
}
Set-Content -LiteralPath $file.FullName -Value $content
}
}
function Update-Offset {
param($dir,$rec)
$files = Get-Files -dir $dir -rec $rec
$operation = Read-Host "Do you want to add or subtract the 9ms ITG offset? (add/subtract/no)"
$adjustment = switch ($operation) {
"add" { 0.009 }
"subtract" { -0.009 }
default {
Write-Host "No adjustment will be made."
return
}
}
foreach ($file in $files) {
$content = Get-Content -LiteralPath $file.FullName
$found = $false
$content = $content | ForEach-Object {
if (!$found -and ($_ -match "#OFFSET")) {
$parts = $_ -split ':'
$semicolon = $parts[1].EndsWith(';')
$parts[1] = $parts[1].TrimEnd(';')
$oldOffset = $parts[1]
$parts[1] = [double]$parts[1] + $adjustment
if ($semicolon) {
$parts[1] = $parts[1].ToString() + ';'
}
$found = $true
Write-Host "Changed offset in $($file.FullName) from $oldOffset to $($parts[1])"
return ($parts -join ':')
}
return $_
}
Set-Content -Path $file.FullName -Value $content
}
}
function Update-File {
param($file, $operations)
$content = Get-Content -LiteralPath $file.FullName
$contentModified = $false
foreach ($operation in $operations) {
$pattern = $operation.Pattern
$replacement = $operation.Replacement
$appendIfNotFound = $operation.AppendIfNotFound -or $false
$found = $false
for ($i = 0; $i -lt $content.Length; $i++) {
if ($content[$i] -match $pattern) {
Write-Host "Replacing '$($content[$i])' with '$replacement'"
$content[$i] = $content[$i] -replace $pattern, $replacement
$found = $true
$contentModified = $true
}
}
if ($appendIfNotFound -and -not $found) {
Write-Host "Appending '$replacement' to the file."
$content += $replacement
$contentModified = $true
}
}
if ($contentModified) {
Set-Content -LiteralPath $file.FullName -Value $content
}
}
#endregion
#region FUNCTION Check-FilePaths, Remove-OldFiles
function Check-FilePaths {
param($dir)
$files = Get-ChildItem $dir -Recurse -File
$containsSpecialChars = $false
foreach ($file in $files) {
if ($file.FullName -match '\[|\]') {
$containsSpecialChars = $true
break
}
}
if ($containsSpecialChars) {
Write-Warning @"
One or more file paths contain `[` or `]`.
Unfortunately, PowerShell doesn't handle square brackets well.
Some operations will not succeed with these
characters present in the file path.
Please rename those files, and then run the tool again.
"@
}
}
function Remove-OldFiles {
param($targetDir)
if (!(Test-Path -Path $targetDir)) {
Write-Host "The directory `"$targetDir`" does not exist."
return
}
$oldFileList = Get-ChildItem -Path $targetDir -Recurse -Filter "*.old"
if ($oldFileList) {
Write-Host "The following .old files were found:"
foreach ($oldFile in $oldFileList) {
Write-Host "`"$($oldFile.FullName)`""
}
$firstCheckMessage = "Do you want to remove all of the above files? (yes/no, default is no)"
$userResponse = Read-Host -Prompt $firstCheckMessage
if ($userResponse -eq 'yes') {
$doubleCheckMessage = "Are you sure you want to delete these files? (yes/no, default is no)"
$doubleCheckResponse = Read-Host -Prompt $doubleCheckMessage
if ($doubleCheckResponse -eq 'yes') {
foreach ($oldFile in $oldFileList) {
Remove-Item -Path $oldFile.FullName
}
Write-Host "All .old files have been removed."
} else {
Write-Host "No files were removed."
}
} else {
Write-Host "No files were removed."
}
} else {
Write-Host "No .old files found in `"$targetDir`"."
}
}
#endregion
#region FUNCTION Prepare-Filenames-For-Filesharing
function Prepare-Filenames-For-Filesharing {
param($dir,$rec)
# If I missed any file types that we should look for, they can be added here.
$files = Get-ChildItem $dir -Include *.sm,*.ssc,*.mp3,*.ogg,*.png,*.gif,*.jpg,*.jpeg -Recurse:$rec
# First, rename all the files in the directory
$renamedFiles = @{}
foreach ($file in $files) {
$newFileName = $file.Name -replace '[^a-zA-Z0-9._]','_'
try {
Rename-Item -LiteralPath $file.FullName -NewName $newFileName -ErrorAction Stop
$renamedFiles[$file.FullName] = Join-Path $file.Directory $newFileName
Write-Host "Renamed file '$($file.FullName)' to '$newFileName'"
} catch {
Write-Warning "Failed to rename file '$($file.FullName)' to '$newFileName'. Error: $_"
Write-Warning "Values in the simfile will not be changed."
return
}
}
# Refresh the $files variable
$files = Get-ChildItem $dir -Include *.sm,*.ssc,*.mp3,*.ogg,*.png,*.gif,*.jpg,*.jpeg -Recurse:$rec
# Then, update the references in each file
foreach ($file in $files) {
$content = Get-Content -LiteralPath $file.FullName
$content = $content | ForEach-Object {
if ($_ -match "#MUSIC" -or $_ -match "#BANNER" -or $_ -match "#BACKGROUND" -or $_ -match "#CDTITLE") {
$parts = $_ -split ':',2
$parts[1] = $parts[1].TrimStart().Replace(' ','_')
$_ = $parts -join ':'
}
$_
}
Set-Content -LiteralPath $file.FullName -Value $content
}
}
#endregion
#region USER INPUT Get Subdirectory Query
$recursePrompt = Read-Host -Prompt "Do you want to search in subdirectories as well? (yes/no, default is yes)"
$recurseOption = $recursePrompt -ne "no"
Write-Host ""
$simFiles = Get-Files -dir $directoryToUse -Recurse $recurseOption
if ($simFiles.Count -eq 0) {
Write-Host "No simfiles were found. Exiting..."
exit
}
$displayFilesPrompt = Read-Host -Prompt 'Would you like to see the complete list of files that will be modified? (yes/no, default is yes)'
if ($displayFilesPrompt -ne 'no') {
Write-Host ""
Write-Host "The following files will be modified."
Write-Host "Please note you'll get a chance to confirm changes before they are applied."
$simFiles | ForEach-Object { Write-Host $_.FullName }
}
Write-Host ""
Check-FilePaths -dir $directoryToUse
Draw-Separator # End every region in this section with a Draw-Separator so everything looks nice.
#endregion
#region USER INPUT ISO-8859-1 Verification
Write-Host "To ensure compatibility with all versions of StepMania/ITG, you can check for characters which may not be rendered correctly."
Write-Host ""
$encoding = [System.Text.Encoding]::GetEncoding('iso-8859-1')
$unicodeCheckParams = @{
Prompt = 'Would you like to check for Unicode characters? (yes/no, default is no)'
AsSecureString = $false
}
$unicodeCheckInput = Read-Host @unicodeCheckParams
if ($unicodeCheckInput -eq 'yes') {
$unicodeFiles = Get-Files -dir $directoryToUse -Recurse $recurse
$nonUnicodeCompliantFiles = @()
foreach ($file in $unicodeFiles) {
$fileContent = Get-Content -Path $file.FullName | Out-String
$convertedContent = $encoding.GetString($encoding.GetBytes($fileContent))
if ($convertedContent -ne $fileContent) {
$nonUnicodeCompliantFiles += $file.FullName
}
}
if ($nonUnicodeCompliantFiles.Count -eq 0) {
Write-Host "Check completed successfully. No problematic characters were found."
} else {
$nonUnicodeCompliantFiles
}
} else {}
Draw-Separator
#endregion
#region USER INPUT Capitalization
$wannaCapitalize = Read-Host -Prompt 'Would you like to standardize capitalization? (yes/no, default is no)'
Write-Host "Note: This function may break Unicode-only characters."
if ($wannaCapitalize -eq 'yes') {
Update-Field-Capitalization -dir $directoryToUse -rec $recurse -field "TITLE"
Update-Field-Capitalization -dir $directoryToUse -rec $recurse -field "SUBTITLE"
Update-Field-Capitalization -dir $directoryToUse -rec $recurse -field "ARTIST"
Update-Field-Capitalization -dir $directoryToUse -rec $recurse -field "STEPARTIST"
}
Draw-Separator
#endregion
#region USER INPUT Offset Adjustment
Update-Offset -dir $directoryToUse -rec $recurse
Draw-Separator
#endregion
#region USER INPUT Adjust Values In Simfile
$operations = @()
$wannaMessage = @"
The following section changes the text values inside the simfile. It won't
move any files. For example, if you plan to have a banner called 'banner.png'
in all your song directories, you would enter banner.png when prompted. You
can change the banner, CD title, background, step artist, and credit fields
here.
"@
Write-Host $wannaMessage
$wannaModify = Read-Host -Prompt 'Would you like to modify any of these values? (yes/no, default is no)'
if ($wannaModify -eq 'yes') {
Write-Host ""
$addBanner = Read-Host -Prompt 'Would you like to add a banner to all files? (yes/no, default is no)'
if ($addBanner -eq 'yes') {
$bannerPrompt = Read-Host -Prompt 'Enter the banner file name, including extension'
$operations += @{ Pattern = '^#BANNER:.*?;'; Replacement = "#BANNER:$bannerPrompt;" }
}
Write-Host ""
$addCDTitle = Read-Host -Prompt 'Would you like to add a CD title to all files? (yes/no, default is no)'
if ($addCDTitle -eq 'yes') {
$CDTitlePrompt = Read-Host -Prompt 'Enter the CD title file name, including extension'
$operations += @{ Pattern = '^#CDTITLE:.*?;'; Replacement = "#CDTITLE:$CDTitlePrompt;" }
}
Write-Host ""
$addBG = Read-Host -Prompt 'Would you like to add a background to all files? (yes/no, default is no)'
if ($addBG -eq 'yes') {
$BGPrompt = Read-Host -Prompt 'Enter the background file name, including extension'
$operations += @{ Pattern = '^#BACKGROUND:.*?;'; Replacement = "#BACKGROUND:$BGPrompt;" }
}
Write-Host ""
$setStepArtist = Read-Host -Prompt 'Would you like to set something for the step artist field? This is the per-chart credit. (yes/no, default is no)'
if ($setStepArtist -eq 'yes') {
$stepArtist = Read-Host -Prompt 'Enter the credit value'
<# To-do: add more chart types below (pump, smx, etc) #>
$danceTypes = @("dance-single","dance-double","dance-couple","dance-solo")
foreach ($danceType in $danceTypes) {
$operations += @{ Pattern = "//--------------- $danceType - (.*?) ----------------"; Replacement = "//--------------- $danceType - $stepArtist ----------------" }
}
}
Write-Host ""
$setCredit = Read-Host -Prompt 'Would you like to set something for the credit field? (This is the #CREDIT field for the simfile, not the per-chart "Step artist" field.) (yes/no, default is no)'
if ($setCredit -eq 'yes') {
$creditValue = Read-Host -Prompt 'Enter the credit value'
$operations += @{ Pattern = '^#CREDIT:.*?;'; Replacement = "#CREDIT:$creditValue;" }
}
Write-Host ""
$addGenre = Read-Host -Prompt 'Would you like to apply a genre to these files? (yes/no, default is no)'
if ($addGenre -eq 'yes') {
$GenrePrompt = Read-Host -Prompt 'Enter the genre'
$operations += @{ Pattern = '^#GENRE:.*?;'; Replacement = "#GENRE:$GenrePrompt;"; AppendIfNotFound = $true }
}
$files = Get-Files -dir $directoryToUse -Recurse $recurse
Write-Host ""
$confirmation = Read-Host "Are you sure you want to apply changes? (yes/no, default is no)"
Write-Host ""
if ($confirmation -eq "yes") {
foreach ($file in $files) {
Write-Host "Applying changes to file: $($file.FullName)"
Update-File -File $file -operations $operations
}
} else {
Write-Host "No changes were made."
}
}
Draw-Separator
#endregion
#region USER INPUT Remove .old Files
$oldFilesConfirm = Read-Host -Prompt 'Would you like to check for .old files and remove them if found? (yes/no, default is no)'
if ($oldFilesConfirm -eq 'yes') {
Remove-OldFiles -targetDir $directoryToUse -Recurse $recurse
} else {
Write-Host ""
}
Draw-Separator
#endregion
#region USER INPUT Portable Filenames
$wannaMessage = @"
The following section changes the text values inside the simfile. It won't
move any files. For example, if you plan to have a banner called 'banner.png'
in all your song directories, you would enter banner.png when prompted. You
can change the banner, CD title, background, step artist, and credit fields
here.
"@
Write-Host $renameFilesForSharingMessage
$renameFilesForSharingConfirm = Read-Host -Prompt 'Would you like to check for spaces and special characters and rename the files? (yes/no, default is no)'
if ($renameFilesForSharingConfirm -eq 'yes') {
Prepare-Filenames-For-Filesharing -dir $directoryToUse -rec $recurse
} else {
Write-Host ""
}
Draw-Separator
#endregion
#region END OF PROGRAM
# Tell the user everything succeeded.
Write-Host "All done :)"
Write-Host ""
Write-Host "~hint~ If you're finalizing a pack, here is an easy way to normalize a batch of audio files:"
Write-Host "https://www.youtube.com/watch?v=ML7YkFLlL1U"
Write-Host "You can use the 'Batch File/Item Converter' to normalize all your oggs volumes at once ;)"
Write-Host ""
#endregion