-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit.ps1
287 lines (245 loc) · 8.29 KB
/
split.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
$myMcFolder = ".\mymc"
$psvConverterCmd = ".\psv-converter\psv-converter-win.exe"
$importFolder = ".\import"
$exportFolder = ".\export"
$tempFolder = ".\temp"
$cmd = "$($myMcFolder)\mymc.exe"
$psuNameMaxLength = 32
Function Confirm-MyMcPresent {
$fileExists = Test-Path $cmd
if (!$fileExists) {
Write-Output "ERROR: mymc.exe not dedected - please check the readme"
exit
}
}
Function Confirm-PsvConverterPresent {
return Test-Path $psvConverterCmd
}
Function Confirm-MyMcVersion {
$version = (& $cmd "--version" | Select-String -Pattern '2.6.g2').Matches.Value
if($version -ne '2.6.g2') {
Write-Output "ERROR: Incorrect version of MyMc detected - please check the readme"
exit
}
}
Function Confirm-FilesToImport {
$saveFileCount = (Get-ChildItem -Path "$($importFolder)\*" -Include ('*.psu','*.xps','*.max','*.cbs','*.sps','*.psv','*.mc2','*.ps2','*.bin') | Measure-Object).Count
if ($saveFileCount -eq 0) {
Write-Output "No save files detected in import folder"
exit
}
}
Function Move-PsuFromRootDir {
$psuFiles = Get-ChildItem -Path ".\*" -Include *.psu
foreach($psuFile in $psuFiles) {
if (Test-Path -Path ".\$($tempFolder)\$($psuFile.Name)") {
$filesWithMatchingName = Get-ChildItem -Path ".\$($tempFolder)\$($psuFile.BaseName)*" -Include *.psu
$newName = "$($psuFile.BaseName)-$($filesWithMatchingName.Count + 1).psu"
Move-Item -Path ".\$($psuFile.Name)" -Destination ".\$($tempFolder)\$($newName)"
} else {
Move-Item -Path ".\$($psuFile.Name)" -Destination ".\$($tempFolder)\$($psuFile.Name)"
}
}
}
Function Move-Mc2sToTemp {
$mcFiles = Get-ChildItem -Path "$($importFolder)\*" -Include *.mc2
foreach($mcFile in $mcFiles) {
Copy-Item -Force -Path "$($importFolder)\$($mcFile.Name)" -Destination "$($tempFolder)\$($mcFile.BaseName).bin"
}
}
Function Move-Ps2sToTemp {
$mcFiles = Get-ChildItem -Path "$($importFolder)\*" -Include *.ps2
foreach($mcFile in $mcFiles) {
Copy-Item -Force -Path (Join-Path $importFolder $mcFile.Name) -Destination (Join-Path $tempFolder $mcFile.Name)
}
}
Function Move-BinsToTemp {
$binFiles = Get-ChildItem -Path "$($importFolder)\*" -Include *.bin
foreach($binFile in $binFiles) {
if (Test-Path -Path "$($tempFolder)\$($binFile.BaseName).bin") {
$filesWithMatchingName = Get-ChildItem -Path "$($tempFolder)\$($binFile.BaseName)*" -Include *.bin
$newName = "$($binFile.BaseName)-$($filesWithMatchingName.Count).bin"
Copy-Item -Force -Path "$($importFolder)\$($binFile.Name)" -Destination "$($tempFolder)\$($newName).bin"
} else {
Copy-Item -Force -Path "$($importFolder)\$($binFile.Name)" -Destination "$($tempFolder)\$($binFile.BaseName).bin"
}
}
}
Function Move-SaveFilesToTemp {
$saveFiles = Get-ChildItem -Path "$($importFolder)\*" -Include ('*.psu','*.xps','*.max','*.cbs','*.sps')
foreach($saveFile in $saveFiles) {
Copy-Item -Force -Path (Join-Path $importFolder $saveFile.Name) -Destination (Join-Path $tempFolder $saveFile.Name)
}
}
Function Move-PsvsToTemp {
$psvFiles = Get-ChildItem -Path (Join-Path $importFolder "\*") -Include *.psv
foreach($psvFile in $psvFiles) {
Copy-Item -Force -Path (Join-Path $importFolder $psvFile.Name) -Destination (Join-Path $tempFolder $psvFile.Name)
}
}
Function Convert-PsvsToPsus {
$psvFiles = Get-ChildItem -Path (Join-Path $tempFolder "\*") -Include *.psv
foreach($psvFile in $psvFiles) {
& $psvConverterCmd (Join-Path $psvFile.Directory $psvFile.Name)
}
}
Function Export-Psus($mcFile) {
$prm = "$($tempFolder)\$($mcFile.Name)", "dir"
$saveList = & $cmd $prm
if($saveList.getType().Name -eq "String") {
Write-Output "No saves found in $($mcFile.BaseName)"
continue
}
$saves = New-Object Collections.Generic.List[String]
for($i = 0; $i -lt $saveList.Length; $i = $i + 3) {
if ($saveList[$i] -match 'S[A-Z][A-Z][A-Z]-\d\d\d\d\d') {
$psuName = $saveList[$i].Substring(0, $psuNameMaxLength).Trim()
if($psuName.Length) {
$saves.Add($psuName)
}
}
}
if($saves.Length) {
foreach($save in $saves) {
Write-Output "Found $($save) in $($mcFile.BaseName)..."
$prm = "$($tempFolder)\$($mcFile.Name)", "export", $save
& $cmd $prm
}
}
Move-PsuFromRootDir
}
Function Get-PsusFromBins {
$binFiles = Get-ChildItem -Path "$($tempFolder)\*" -Include *.bin
foreach($binFile in $binFiles) {
Write-Output ''
Export-Psus $binFile
}
}
Function Get-PsusFromPs2s {
$ps2Files = Get-ChildItem -Path "$($tempFolder)\*" -Include *.ps2
foreach($ps2File in $ps2Files) {
Export-Psus $ps2File
}
}
Function Get-PsuWithGameId($saveFile) {
Copy-Item -Path ".\blank.bin" -Destination (Join-Path $tempFolder "tempCard.bin")
$prm = $prm = (Join-Path $tempFolder "tempCard.bin"), "import", (Join-Path $tempFolder $($saveFile.Name))
& $cmd $prm
$newSaveFile = Get-ChildItem -Path (Join-Path $tempFolder "tempCard.bin")
Export-Psus($newSaveFile)
Remove-Item -Path (Join-Path $tempFolder "tempCard.bin")
}
Function Repair-SavesWithNoGameId {
$saveFiles = Get-ChildItem -Path "$($tempFolder)\*" -Include ('*.psu','*.xps','*.max','*.cbs','*.sps')
foreach($saveFile in $saveFiles) {
if(!($saveFile.BaseName -match 'S[A-Z][A-Z][A-Z]-\d\d\d\d\d')) {
Get-PsuWithGameId($saveFile)
Remove-Item -Path (Join-Path $saveFile.Directory $saveFile.Name)
}
}
}
Function New-CardIfNotExist {
Param
(
[Parameter(Mandatory=$true, Position=0)]
[string] $gameId,
[Parameter(Mandatory=$true, Position=1)]
[string] $channelNum
)
if (!(Test-Path -Path (Join-Path $exportFolder $gameId))) {
New-Item -ItemType Directory -Force -Path (Join-Path $exportFolder $gameId)
}
if (!(Test-Path -Path (Join-Path $exportFolder $gameId | Join-Path -ChildPath "$($gameId)-$($channelNum).bin"))) {
Copy-Item -Path ".\blank.bin" -Destination (Join-Path $exportFolder $gameId | Join-Path -ChildPath "$($gameId)-$($channelNum).bin")
}
}
Function Import-FileToCard {
Param
(
[Parameter(Mandatory=$true, Position=0)]
[string] $gameId,
[Parameter(Mandatory=$true, Position=1)]
[string] $channelNum,
[Parameter(Mandatory=$true, Position=2)]
$saveFile
)
$prm = ".\$($exportFolder)\$($gameId)\$($gameId)-$($channelNum).bin", "import", ".\$($tempFolder)\$($saveFile.Name)"
$result = & $cmd $prm 2>&1
$result = [string] $result
if($result.indexOf("directory exists") -gt -1 -or $result.indexOf("out of space") -gt -1) {
return $false
} else {
return $result
}
}
Function New-Vmcs {
$saveFiles = Get-ChildItem -Path "$($tempFolder)\*" -Include ('*.psu','*.xps','*.max','*.cbs','*.sps')
foreach($saveFile in $saveFiles) {
$channelNum = 1
if($saveFile.BaseName -match 'S[A-Z][A-Z][A-Z]-\d\d\d\d\d') {
$gameId = $Matches.0
} else {
Write-Output "Could not find Game ID in $($saveFile.Name)"
continue
}
New-CardIfNotExist $gameId $channelNum
$result = Import-FileToCard $gameId $channelNum $saveFile
if($result) {
Write-Output $result
continue
}
for ($nextChannel = 2; $nextChannel -lt 10; $nextChannel++) {
if ($nextChannel -eq 9) {
Write-Output "Too many saves for $($saveFile.BaseName), ignoring"
continue
}
New-CardIfNotExist $gameId $nextChannel
$result = Import-FileToCard $gameId $nextChannel $saveFile
if($result) {
Write-Output $result
break
}
}
}
$exportFiles = $saveFiles = Get-ChildItem -Recurse -Path "$($exportFolder)\*" -Include *.bin
foreach($exportFile in $exportFiles) {
Move-Item -Path "$($exportFile.Directory)\$($exportFile.Name)" -Destination "$($exportFile.Directory)\$($exportFile.BaseName).mc2" -Force
}
}
Function New-TempDir {
if (!(Test-Path -Path "$($tempFolder)")) {
New-Item -ItemType Directory -Force -Path "$($tempFolder)"
}
}
Function Clear-TempDir {
Remove-Item -Force -Recurse -Path "$($tempFolder)"
}
Function Move-FilesToTempDir {
Move-SaveFilesToTemp
Move-Mc2sToTemp
Move-Ps2sToTemp
Move-BinsToTemp
}
Function Convert-PsvFiles {
$psvFiles = Get-ChildItem -Path (Join-Path $importFolder "\*") -Include *.psv
if($psvFiles.Length -gt 0 -and !(Confirm-PsvConverterPresent)) {
Write-Output ".psv files ignored as psv-converter-win.exe not found - please check the readme"
return
}
Move-PsvsToTemp
Convert-PsvsToPsus
}
Function Get-Psus {
Get-PsusFromBins
Get-PsusFromPs2s
}
Confirm-MyMcPresent
Confirm-MyMcVersion
Confirm-FilesToImport
New-TempDir
Move-FilesToTempDir
Convert-PsvFiles
Get-Psus
Repair-SavesWithNoGameId
New-Vmcs
Clear-TempDir