-
Notifications
You must be signed in to change notification settings - Fork 0
/
mw5-sync.ps1
346 lines (281 loc) · 11.3 KB
/
mw5-sync.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
# SET THIS TO YOUR MECHWARRIOR 5 DIRECTORY!
$MW5_DIR="C:\Program Files\Epic Games\MW5Mercs"
# don't uncomment this, Ben just uses this for testing on his Mac
#$MW5_DIR="/tmp/mercs"
$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)) {
throw "mod directory ${UNPACK_DIR} does not exist"
}
if (-not (Test-Path -Path $DOWNLOAD_PATH)) {
New-Item -Path $DOWNLOAD_PATH -ItemType Directory
}
function is_7zip {
param($_file)
return $_file.EndsWith(".7z", "CurrentCultureIgnoreCase");
}
function is_rar {
param($_file)
return $_file.EndsWith(".rar", "CurrentCultureIgnoreCase");
}
function is_zip {
param($_file)
return $_file.EndsWith(".zip", "CurrentCultureIgnoreCase");
}
function Get-Cygpath {
param($_path)
if ([IO.Path]::DirectorySeparatorChar -eq '/') {
return $_path
}
$_cygwin_path = cygpath --unix "${_path}" | Out-String
$_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
$json = ConvertFrom-Json -InputObject $contents
return $json
}
function Get-Mod-Info-From-File {
param( $_file )
$json = Get-Json-From-File $_file.FullName
# Write-Host "Full Name: $($_file.FullName)"
$_relativeName = $_file.FullName.ToString()
$_relativeName = $_relativeName.replace(($UNPACK_DIR + [IO.Path]::DirectorySeparatorChar), '')
$_internalPath = $_relativeName.Split([IO.Path]::DirectorySeparatorChar)[0]
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-Mod-Info-From-Archive {
param( $_archive_file )
$json = @{}
if (is_zip($_archive_file)) {
$_cyg_zip_file = Get-Cygpath $_archive_file
$modfiles = unzip -Z1 "${_cyg_zip_file}" | Out-String | Get-Mod-Info-Files-From-List
$modfiles | ForEach-Object {
$modfile = $_
$contents = unzip -p "${_cyg_zip_file}" $modfile | Out-String
$json[$modfile] = ConvertFrom-Json -InputObject $contents
}
} elseif (is_rar($_archive_file)) {
$modfiles = unrar lb "${_archive_file}" | Out-String | Get-Mod-Info-Files-From-List
if ($LASTEXITCODE -gt 0) {
throw "failed to determine mod.json path inside archive ${_archive_file}"
}
$modfiles | ForEach-Object {
$modfile = $_
$contents = unrar p "${_archive_file}" $modfile | Out-String
if ($LASTEXITCODE -gt 0) {
throw "failed to get contents of mod.json inside archive ${_archive_file}"
}
$json[$modfile] = ConvertFrom-Json -InputObject $contents
}
} elseif (is_7zip($_archive_file)) {
$cyg_archive_file = Get-Cygpath "${_archive_file}"
$modfiles = 7z l -slt "${cyg_archive_file}" | Out-String | Get-Mod-Info-Files-From-List
if ($LASTEXITCODE -gt 0) {
throw "failed to determine mod.json path inside archive ${_archive_file}"
}
$modfiles | ForEach-Object {
$modfile = $_
$contents = 7z e -so "${cyg_archive_file}" "${modfile}" | Out-String
if ($LASTEXITCODE -gt 0) {
throw "failed to get contents of mod.json inside archive ${_archive_file}"
}
$json[$modfile] = ConvertFrom-Json -InputObject $contents
}
} 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}/"
foreach ($mod_dir in (Get-ChildItem -Recurse -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-Item (Join-Path -Path $UNPACK_DIR -ChildPath $existing.internalPath) -Recurse -Force
Write-Host "done"
}
$archive_file_name = Split-Path $active.file -Leaf -Resolve
Write-Host -NoNewline " * Unpacking archive: "
Write-Host -NoNewline -ForegroundColor Blue $archive_file_name
Write-Host -NoNewline "... "
if ($unpacked_mods.ContainsKey($archive_file_name)) {
Write-Host "already unpacked"
} else {
Expand-Mod $active
Write-Host "done"
$unpacked_mods[$archive_file_name] = $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-Item (Join-Path -Path $UNPACK_DIR -ChildPath $existing.internalPath) -Recurse -Force
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"
}
$active_mods.GetEnumerator() | Sort-Object { $_.Value.displayName } | ForEach-Object {
$mod_info = $_.Value
if ($mod_info.file -imatch "\boptional\b") {
Write-Host -ForegroundColor DarkGray "* Skipping optional mod $($mod_info.displayName)"
} else {
$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 | Out-File -FilePath $modlist_filename
Write-Host ""
pause