-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmw5-sync.ps1
More file actions
372 lines (306 loc) · 12.3 KB
/
Copy pathmw5-sync.ps1
File metadata and controls
372 lines (306 loc) · 12.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
param(
[Parameter(Mandatory=$false, HelpMessage="Path to the MW5Mercs folder")]
[string]$MW5MercsFolder
)
. (Join-Path -Path $PSScriptRoot -ChildPath 'archive-type.ps1')
. (Join-Path -Path $PSScriptRoot -ChildPath 'mod-path.ps1')
$DEFAULT_MW5_DIR="C:\Program Files\Epic Games\MW5Mercs"
#$DEFAULT_MW5_DIR="/tmp/mercs"
# Resolve MW5 directory from parameter or default
$MW5_DIR = [string]::IsNullOrWhiteSpace($MW5MercsFolder) ? $DEFAULT_MW5_DIR : $MW5MercsFolder
# Validate that MW5_DIR exists and is a directory before proceeding
if (-not (Test-Path -Path $MW5_DIR -PathType Container)) {
throw "MW5 directory '${MW5_DIR}' does not exist or is not a directory. Please verify the path or pass -MW5MercsFolder parameter."
}
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true
if (-not ${env:TEMP}) {
${env:TEMP} = "/tmp"
}
$env:PATH = '{0}{1}{2}' -f $env:PATH,[IO.Path]::PathSeparator,'.'
$UNPACK_DIR = Join-Path -Path (Join-Path -Path $MW5_DIR -ChildPath "MW5Mercs") -ChildPath "mods"
$DOWNLOAD_PATH = Join-Path -Path (Join-Path -Path $MW5_DIR -ChildPath "MW5Mercs") -ChildPath "mw5modsync-cache"
if (-not (Test-Path -Path $UNPACK_DIR -PathType Container)) {
throw "Mod directory does not exist at ${UNPACK_DIR}. Ensure MW5 directory is set to a valid MechWarrior 5 installation."
}
if (-not (Test-Path -Path $DOWNLOAD_PATH)) {
New-Item -Path $DOWNLOAD_PATH -ItemType Directory | Out-Null
}
# Validates that internalPath is a safe single-directory name (non-empty, no path separators,
# no leading dot) before deleting it under $UNPACK_DIR.
# NOTE: Windows reserved device names (CON, NUL, LPT1, etc.) and names with trailing spaces or
# dots are not rejected here — they are illegal in mod.json but are not guarded against. This is
# a known limitation; the regex guards against traversal, not every illegal Windows filename.
function Remove-Mod-Directory {
param([string]$internalPath)
if ($internalPath -match '^[^./\\]+$') {
Remove-Item (Join-Path -Path $UNPACK_DIR -ChildPath $internalPath) -Recurse -Force
} else {
throw "refusing to delete: internalPath '$internalPath' is empty, contains path separators, or starts with a dot"
}
}
function Get-Cygpath {
param($_path)
if ([IO.Path]::DirectorySeparatorChar -eq '/') {
return $_path
}
$_cygwin_path = cygpath --unix "${_path}" | Out-String
if ($LASTEXITCODE -gt 0) {
throw "cygpath failed for '${_path}' (exit code $LASTEXITCODE)"
}
$_cygwin_path = $_cygwin_path.replace("`r`n", "").replace("`n", "")
return $_cygwin_path
}
function Get-Local-Filelist {
param( $_folder )
$localdir = Join-Path $DOWNLOAD_PATH -ChildPath $_folder
if (-not (Test-Path -Path $localdir)) {
return @()
}
return [array](Get-ChildItem -Path $localdir -File -Name)
}
function Get-Json-From-File {
param( $_filename )
$contents = Get-Content -Path $_filename -Raw -Encoding UTF8
try {
$json = ConvertFrom-Json -InputObject $contents
} catch {
throw "failed to parse JSON from ${_filename}: $_"
}
return $json
}
function Get-Mod-Info-From-File {
param( $_file )
$json = Get-Json-From-File $_file.FullName
$_internalPath = Get-Mod-Internal-Path $UNPACK_DIR $_file.FullName.ToString()
return @{
id = $_internalPath.ToLower()
file = $_file.FullName.ToString()
internalPath = $_internalPath
displayName = $json.displayName
version = $json.version
buildNumber = $json.buildNumber
}
}
function Get-Mod-Info-Files-From-List {
param(
[Parameter(ValueFromPipeline=$true)]
$_archive_output
)
return $_archive_output -split '\r?\n' | Where-Object { $_ -match "^[^\\/]+[\\/]mod\.json" } | ForEach-Object { $_.replace("Path = ", "") };
}
function Get-Archive-Contents {
param(
[string]$archive_file,
[scriptblock]$list_cmd,
[scriptblock]$extract_cmd
)
$modfiles = & $list_cmd | Out-String | Get-Mod-Info-Files-From-List
if ($LASTEXITCODE -gt 0) {
throw "failed to determine mod.json path inside archive ${archive_file}"
}
if (-not $modfiles) {
throw "no mod.json found in archive ${archive_file}"
}
$json = @{}
$modfiles | ForEach-Object {
$modfile = $_
$contents = & $extract_cmd $modfile | Out-String
if ($LASTEXITCODE -gt 0) {
throw "failed to get contents of mod.json inside archive ${archive_file}"
}
try {
$json[$modfile] = ConvertFrom-Json -InputObject $contents
} catch {
throw "failed to parse mod.json at '${modfile}' inside archive ${archive_file}: $_"
}
}
return $json
}
function Get-Mod-Info-From-Archive {
param( $_archive_file )
if (is_rar($_archive_file)) {
$json = Get-Archive-Contents "${_archive_file}" `
{ unrar lb "${_archive_file}" } `
{ param($modfile); unrar p "${_archive_file}" $modfile }
} elseif (is_7z_compatible($_archive_file)) {
$cyg_archive_file = Get-Cygpath "${_archive_file}"
$json = Get-Archive-Contents "${_archive_file}" `
{ 7z l -slt "${cyg_archive_file}" } `
{ param($modfile); 7z e -so "${cyg_archive_file}" "${modfile}" }
} else {
Write-Host -ForegroundColor Yellow "Unknown file type: ${_archive_file}"
return @()
}
$ret = @()
foreach ($modfile in $json.Keys) {
$_archiveInternalPath = ($modfile.ToString() -split "[/\\]")[0]
$ret += @{
id = $_archiveInternalPath.ToLower()
file = $_archive_file.ToString()
internalPath = $_archiveInternalPath
displayName = $json[$modfile].displayName
version = $json[$modfile].version
buildNumber = $json[$modfile].buildNumber
}
}
return $ret;
}
function Expand-Mod {
param($_mod)
if (is_zip($_mod.file)) {
$_cyg_unpack_dir = Get-Cygpath $UNPACK_DIR
$_cyg_zip_file = Get-Cygpath $_mod.file
unzip -q -o -d "${_cyg_unpack_dir}" "${_cyg_zip_file}"
} elseif (is_rar($_mod.file)) {
unrar -y x -idq $_mod.file -op $UNPACK_DIR
} elseif (is_7zip($_mod.file)) {
$cyg_archive_file = Get-Cygpath $_mod.file
$output_dir = Get-Cygpath $UNPACK_DIR
7z -y x "${cyg_archive_file}" "-o${output_dir}" | Select-String "Error" -Context 10
}
if ($LASTEXITCODE -gt 0) {
throw "failed to unpack file"
}
}
function Write-Mod-Name {
param($_mod)
Write-Host -NoNewline -ForegroundColor Green $_mod.displayName
}
function Write-Mod-Version {
param($_mod)
Write-Host -NoNewline "version "
Write-Host -NoNewline -ForegroundColor Yellow $_mod.version
Write-Host -NoNewline ", build "
Write-Host -NoNewline -ForegroundColor Yellow $_mod.buildNumber
}
$active_mods = @{}
Write-Host "### DOWNLOADING NEW FILES ###" -ForegroundColor Cyan
$_local_download_path = Get-Cygpath "${DOWNLOAD_PATH}"
rsync -avr --partial --progress --no-perms --delete --exclude='*.filepart' --include='Required/***' --include='Optional/***' --exclude='*' "ln1.raccoonfink.com::mw5/" "${_local_download_path}/"
if ($LASTEXITCODE -gt 0) {
throw "rsync failed with exit code $LASTEXITCODE"
}
foreach ($mod_dir in (Get-ChildItem -Directory $DOWNLOAD_PATH | Select-Object -ExpandProperty Name)) {
$local_filelist = Get-Local-Filelist $mod_dir
foreach ($localfile in $local_filelist) {
$relative_path = Join-Path -Path $mod_dir -ChildPath $localfile
$full_path = Join-Path -Path $DOWNLOAD_PATH -ChildPath $relative_path
Get-Mod-Info-From-Archive($full_path) | ForEach-Object {
$archive_modinfo = $_
if ($archive_modinfo.ContainsKey('id')) {
$active_mods[$archive_modinfo.id] = $archive_modinfo
}
}
}
}
Write-Host ""
Write-Host "### SCANNING INSTALLED MODS ###" -ForegroundColor Cyan
$existing_modfiles = Get-ChildItem -Path $UNPACK_DIR -Filter 'mod.json' -Recurse | Sort-Object
$existing_mods = @{}
$unpacked_mods = @{}
foreach ($json_file in $existing_modfiles) {
# Write-Host "existing: $($jsonfile.FullName)"
$json_modinfo = Get-Mod-Info-From-File $json_file
Write-Host -NoNewline "* Found installed mod "
Write-Mod-Name $json_modinfo
Write-Host -NoNewline " ("
Write-Mod-Version $json_modinfo
Write-Host -NoNewline ") at "
Write-Host $json_modinfo.internalPath -ForegroundColor Magenta
$existing_mods[$json_modinfo.id] = $json_modinfo;
}
Write-Host ""
Write-Host "### SYNCING DOWNLOADS TO MOD DIRECTORY ###" -ForegroundColor Cyan
$active_mods.GetEnumerator() | Sort-Object { $_.Value.displayName } | ForEach-Object {
$id = $_.Key.ToString().ToLower()
$active = $_.Value
$existing = $existing_mods[$id]
# Write-Host "id: $($id)"
# Write-Host "existing: " + ($existing | Out-String)
# Write-Host "active: " + ($active | Out-String)
if ($existing.id -and ($existing.version -eq $active.version) -and ($existing.buildNumber -eq $active.buildNumber)) {
Write-Host -ForegroundColor DarkGray "* $($existing.displayName) already installed: version $($existing.version), build $($existing.buildNumber)"
} else {
Write-Host -NoNewline "+ "
Write-Mod-Name $active
Write-Host -NoNewline " new or changed: "
if ($existing.id) {
Write-Mod-Version $existing
Write-Host -NoNewline " => "
}
Write-Mod-Version $active
Write-Host ""
if ($existing.id -and $existing.version) {
Write-Host -NoNewline " ! Deleting existing "
Write-Host -NoNewline -ForegroundColor Magenta $existing.internalPath
Write-Host -NoNewline " mod directory... "
Remove-Mod-Directory $existing.internalPath
Write-Host "done"
}
$archive_file_name = Split-Path $active.file -Leaf
Write-Host -NoNewline " * Unpacking archive: "
Write-Host -NoNewline -ForegroundColor Blue $archive_file_name
Write-Host -NoNewline "... "
if ($unpacked_mods.ContainsKey($active.file)) {
Write-Host "already unpacked"
} else {
Expand-Mod $active
Write-Host "done"
$unpacked_mods[$active.file] = $true
}
}
}
Write-Host ""
Write-Host "### REMOVING OBSOLETE MODS ###" -ForegroundColor Cyan
$existing_mods.GetEnumerator() | Sort-Object { $_.Value.displayName } | ForEach-Object {
$id = $_.Key.ToString()
$existing = $_.Value
$active = $active_mods[$id]
if (-not $active) {
Write-Host -NoNewline "! Deleting removed "
Write-Mod-Name $existing
Write-Host -NoNewline " ("
Write-Mod-Version $existing
Write-Host -NoNewline ") mod from the "
Write-Host -NoNewline -ForegroundColor Magenta $existing.internalPath
Write-Host " directory..."
Remove-Mod-Directory $existing.internalPath
Write-Host "done"
}
}
Write-Host ""
Write-Host "### Updating modlist.json ###" -ForegroundColor Cyan
$modlist_filename = Join-Path -Path $UNPACK_DIR -ChildPath "modlist.json"
$modlist = @{ modStatus = @{} };
if (Test-Path -Path $modlist_filename) {
$modlist = Get-Json-From-File $modlist_filename
} else {
Write-Host -ForegroundColor Yellow "! ${modlist_filename} does not already exist... creating"
}
if (-not $modlist.modStatus) {
$modlist.modStatus = @{}
}
$active_mods.GetEnumerator() | Sort-Object { $_.Value.displayName } | ForEach-Object {
$mod_info = $_.Value
if ($mod_info.file -imatch "[\\/]Optional[\\/]") {
Write-Host -ForegroundColor DarkGray "* Skipping optional mod $($mod_info.displayName)"
} else {
if ($mod_info.internalPath -notmatch '^[^./\\]+$') {
throw "invalid internalPath in mod: '$($mod_info.internalPath)'"
}
$modlist.modStatus | Add-Member -Force -NotePropertyName $mod_info.internalPath -NotePropertyValue @{ bEnabled = $true }
Write-Host -NoNewline "* Enabling required mod "
Write-Mod-Name $mod_info
Write-Host ""
}
}
if (Test-Path -Path "${modlist_filename}.bak") {
Remove-Item "${modlist_filename}.bak"
}
if (Test-Path -Path $modlist_filename) {
Rename-Item -Path $modlist_filename -NewName "${modlist_filename}.bak"
}
$modlist | ConvertTo-Json -Depth 10 | Out-File -FilePath $modlist_filename -Encoding utf8
Write-Host ""
pause