-
Notifications
You must be signed in to change notification settings - Fork 551
Expand file tree
/
Copy pathark-se-minmanagemods.sh
More file actions
646 lines (566 loc) · 24.7 KB
/
ark-se-minmanagemods.sh
File metadata and controls
646 lines (566 loc) · 24.7 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
#!/bin/bash
# Inspired by https://github.com/Bletch1971/ServerManagers/blob/source/src/ARKServerManager/Utils/ModUtils.cs and
# https://github.com/arkmanager/ark-server-tools/blob/master/tools/arkmanager
set -euo pipefail
# --- Global variables ---
readonly arkRootDir="./arkse"
readonly arkBaseDir="${arkRootDir}/376030"
readonly workshopContentDir="${arkBaseDir}/Engine/Binaries/ThirdParty/SteamCMD/Linux/steamapps/workshop/content/346110"
readonly modsInstallDir="${arkBaseDir}/ShooterGame/Content/Mods"
# --- Embedded Perl scripts ---
createModFilePerlScriptContent=$(cat <<'PERL_CREATE_MOD_EOF'
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use File::Basename;
use Encode qw(encode decode FB_CROAK);
use Getopt::Long qw(GetOptions);
use IO::Handle;
my $MODTYPE_MOD = "1";
sub read_ue4_string {
my ($fh) = @_;
my $buffer;
my $bytes_read;
$bytes_read = read($fh, $buffer, 4);
unless (defined $bytes_read && $bytes_read == 4) { return undef; }
my $count = unpack('l<', $buffer);
my $was_negative_originally = 0;
if ($count < 0) { $was_negative_originally = 1; $count = -$count; }
if ($was_negative_originally || $count <= 0) {
if (!$was_negative_originally && $count == 1) { read($fh, $buffer, 1); }
return "";
}
$bytes_read = read($fh, $buffer, $count);
unless (defined $bytes_read && $bytes_read == $count) { return undef; }
my $str_data = substr($buffer, 0, $count - 1);
return decode('UTF-8', $str_data, FB_CROAK);
}
sub write_ue4_string {
my ($fh, $string_to_write) = @_;
my $utf8_bytes = encode('UTF-8', $string_to_write, FB_CROAK);
my $num_bytes_for_string_itself = length($utf8_bytes);
my $total_length_field = $num_bytes_for_string_itself + 1;
print $fh pack('l<', $total_length_field);
print $fh $utf8_bytes;
print $fh pack('C', 0);
}
sub parse_mod_info {
my ($mod_info_filepath) = @_;
my @map_names;
open my $fh, '<:raw', $mod_info_filepath or die "Perl: Cannot open mod.info '$mod_info_filepath': $!";
my $mod_name_from_info_file = read_ue4_string($fh);
if (!defined $mod_name_from_info_file) { close $fh; return (); }
my $buffer;
my $bytes_read = read($fh, $buffer, 4);
unless (defined $bytes_read && $bytes_read == 4) { close $fh; return (); }
my $num_map_names = unpack('l<', $buffer);
for (my $i = 0; $i < $num_map_names; $i++) {
my $map_name = read_ue4_string($fh);
if (defined $map_name) { push @map_names, $map_name; }
}
close $fh;
return @map_names;
}
sub parse_modmeta_info {
my ($modmeta_info_filepath) = @_;
my %meta_info;
unless (-e $modmeta_info_filepath && -f _ && -r _) { return %meta_info; }
if (-z $modmeta_info_filepath) { return %meta_info; }
open my $fh, '<:raw', $modmeta_info_filepath or die "Perl: Cannot open modmeta.info '$modmeta_info_filepath': $!";
my $buffer;
my $bytes_read = read($fh, $buffer, 4);
unless (defined $bytes_read && $bytes_read == 4) { close $fh; return %meta_info; }
my $num_pairs = unpack('l<', $buffer);
if ($num_pairs < 0) { $num_pairs = 0; }
for (my $i = 0; $i < $num_pairs; $i++) {
my $key = read_ue4_string($fh);
my $value = read_ue4_string($fh);
if (defined $key && defined $value) { $meta_info{$key} = $value; }
}
close $fh;
return %meta_info;
}
sub create_mod_file {
my ($output_filepath, $mod_id_str, $map_names_ref, $meta_info_ref) = @_;
open my $fh, '>:raw', $output_filepath or die "Perl: Cannot create .mod file '$output_filepath': $!";
my $mod_id_val;
if ($mod_id_str =~ /^\d+$/) { $mod_id_val = $mod_id_str; }
else { die "Perl: Invalid modId: '$mod_id_str'."; }
print $fh pack('Q<', $mod_id_val);
print $fh pack('l<', 0);
my $path_string = "../../../ShooterGame/Content/Mods/" . $mod_id_str;
write_ue4_string($fh, $path_string);
my $num_map_names = scalar(@$map_names_ref);
print $fh pack('l<', $num_map_names);
foreach my $map_name (@$map_names_ref) { write_ue4_string($fh, $map_name); }
print $fh pack('L<', 4280483635);
print $fh pack('l<', 2);
my $has_mod_type_key = exists($meta_info_ref->{'ModType'}) ? 1 : 0;
print $fh pack('C', $has_mod_type_key);
my $num_meta_pairs = scalar(keys %$meta_info_ref);
print $fh pack('l<', $num_meta_pairs);
foreach my $key (sort keys %$meta_info_ref) {
my $value = $meta_info_ref->{$key};
write_ue4_string($fh, $key);
write_ue4_string($fh, $value);
}
close $fh;
}
my $modIdArg;
my $modInfoFileArg;
my $modmetaInfoFileArg = '';
my $outputModFileArg;
my $defaultModtypeIfMetaEmptyArg = 0;
GetOptions(
'modid=s' => \$modIdArg,
'modinfo=s' => \$modInfoFileArg,
'modmeta:s' => \$modmetaInfoFileArg,
'output=s' => \$outputModFileArg,
'default-modtype-if-meta-empty!' => \$defaultModtypeIfMetaEmptyArg,
) or die "Perl Usage: $0 --modid <id> --modinfo <path> [--modmeta <path>] --output <path> [--default-modtype-if-meta-empty]\n";
unless (-f $modInfoFileArg && -r _) {
die "Perl: mod.info file not found or not readable at '$modInfoFileArg' for create_mod_file.pl.\n";
}
my @map_names = parse_mod_info($modInfoFileArg);
my %meta_information;
my $modmetaFileWasProvided = (defined $modmetaInfoFileArg && length $modmetaInfoFileArg > 0);
my $metaInfoActuallyParsed = 0;
if ($modmetaFileWasProvided) {
if (-f $modmetaInfoFileArg && -r _) {
%meta_information = parse_modmeta_info($modmetaInfoFileArg);
if (scalar(keys %meta_information) > 0) {
$metaInfoActuallyParsed = 1;
} elsif (-e $modmetaInfoFileArg) {
$metaInfoActuallyParsed = 1;
}
}
}
if ($defaultModtypeIfMetaEmptyArg) {
if (!$modmetaFileWasProvided ||
($modmetaFileWasProvided && !scalar(keys %meta_information))
) {
$meta_information{'ModType'} = $MODTYPE_MOD;
}
}
create_mod_file($outputModFileArg, $modIdArg, \@map_names, \%meta_information);
exit 0;
PERL_CREATE_MOD_EOF
)
ue4BatchDecompressPerlScriptContent=$(cat <<'PERL_BATCH_DECOMPRESS_EOF'
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use File::Basename;
use Compress::Zlib;
use Getopt::Long qw(GetOptions);
use JSON::PP;
use IO::Handle;
STDERR->autoflush(1);
use constant {
PACKAGE_FILE_TAG => 2653586369,
LOADING_COMPRESSION_CHUNK_SIZE => 131072
};
sub read_int64_le {
my ($fh) = @_;
my $buffer;
my $bytes_read = read($fh, $buffer, 8);
unless (defined $bytes_read && $bytes_read == 8) {
return undef;
}
return unpack('q<', $buffer);
}
sub decompress_single_z_file_core {
my ($source_filepath, $destination_filepath) = @_;
open my $in_fh, '<:raw', $source_filepath or die "Perl_Batch: Cannot open source '$source_filepath': $!";
open my $out_fh, '>:raw', $destination_filepath or die "Perl_Batch: Cannot open dest '$destination_filepath': $!";
my $header1_compressed_size = read_int64_le($in_fh);
die "Failed h1_comp_size from '$source_filepath'" unless defined $header1_compressed_size;
my $header1_uncompressed_size = read_int64_le($in_fh);
die "Failed h1_uncomp_size from '$source_filepath'" unless defined $header1_uncompressed_size;
my $header2_compressed_size = read_int64_le($in_fh);
die "Failed h2_comp_size from '$source_filepath'" unless defined $header2_compressed_size;
my $total_uncompressed_size = read_int64_le($in_fh);
die "Failed total_uncomp_size from '$source_filepath'" unless defined $total_uncompressed_size;
my $ue4_uncompressed_chunk_size = $header1_uncompressed_size;
if ($ue4_uncompressed_chunk_size == PACKAGE_FILE_TAG) {
$ue4_uncompressed_chunk_size = LOADING_COMPRESSION_CHUNK_SIZE;
}
if ($ue4_uncompressed_chunk_size <= 0) {
die "UE4 Chunk Size must be positive, got $ue4_uncompressed_chunk_size from '$source_filepath'\n";
}
my $num_chunks = 0;
if ($total_uncompressed_size > 0) {
$num_chunks = int(($total_uncompressed_size + $ue4_uncompressed_chunk_size - 1) / $ue4_uncompressed_chunk_size);
} elsif ($total_uncompressed_size == 0) {
$num_chunks = 0;
} else {
die "Total uncomp size cannot be negative ($total_uncompressed_size) from '$source_filepath'.\n";
}
if ($num_chunks < 0) {
die "Number of chunks cannot be negative ($num_chunks) from '$source_filepath'.\n";
}
my @chunk_table;
for (my $i = 0; $i < $num_chunks; $i++) {
my $chunk_compressed_size = read_int64_le($in_fh);
die "Failed to read compressed size for chunk $i from '$source_filepath'" unless defined $chunk_compressed_size;
my $chunk_uncompressed_size = read_int64_le($in_fh);
die "Failed to read uncompressed size for chunk $i from '$source_filepath'" unless defined $chunk_uncompressed_size;
if ($chunk_compressed_size < 0 || $chunk_uncompressed_size < 0) {
die "Chunk $i from '$source_filepath' has negative size(s).";
}
push @chunk_table, {
compressed_size => $chunk_compressed_size,
uncompressed_size => $chunk_uncompressed_size
};
}
my $current_uncompressed_total = 0;
for (my $i = 0; $i < $num_chunks; $i++) {
my $chunk_info = $chunk_table[$i];
my $bytes_to_read_for_chunk = $chunk_info->{compressed_size};
my $uncompressed_data;
if ($bytes_to_read_for_chunk == 0) {
$uncompressed_data = "";
} else {
my $compressed_data_buffer;
my $bytes_read = read($in_fh, $compressed_data_buffer, $bytes_to_read_for_chunk);
unless (defined $bytes_read && $bytes_read == $bytes_to_read_for_chunk) {
die "Failed to read $bytes_to_read_for_chunk bytes for chunk $i from '$source_filepath'. Expected $bytes_to_read_for_chunk, got " .
($bytes_read // 0) . ". Error: " . ($! // "Unknown");
}
$uncompressed_data = Compress::Zlib::uncompress($compressed_data_buffer);
unless (defined $uncompressed_data) {
my $z_error_number;
{
no warnings 'once';
$z_error_number = $Compress::Zlib::unzerrno;
}
my $zlib_error_string = Compress::Zlib::unzerror($z_error_number) || "Unknown Zlib err $z_error_number";
die "Zlib uncomp fail chunk $i from '$source_filepath': $zlib_error_string";
}
}
print {$out_fh} $uncompressed_data;
$current_uncompressed_total += length($uncompressed_data);
}
close $in_fh;
close $out_fh;
if ($num_chunks > 0 && $current_uncompressed_total != $total_uncompressed_size) {
warn "Perl_Batch_Warning: Decompressed size mismatch for '$source_filepath'. Expected $total_uncompressed_size, got $current_uncompressed_total.\n";
}
return 1;
}
my $json_job_file_path_arg;
GetOptions('jsonjobfile=s' => \$json_job_file_path_arg)
or die "Perl_Batch Usage: $0 --jsonjobfile <path_to_jobfile.json>\n";
die "Perl_Batch_Error: --jsonjobfile not specified.\n" unless defined $json_job_file_path_arg;
open my $json_job_fh, '<:raw', $json_job_file_path_arg
or die "Perl_Batch_Error: Cannot open job file '$json_job_file_path_arg': $!";
my $json_text = do { local $/; <$json_job_fh> };
close $json_job_fh;
my $jobs_array_ref;
eval {
$jobs_array_ref = JSON::PP->new->utf8->decode($json_text);
1;
}
or do {
my $json_err = $@ || "Unknown JSON error";
chomp $json_err;
die "Perl_Batch_Error: Could not decode JSON from job file. Error: $json_err\n";
};
unless (ref $jobs_array_ref eq 'ARRAY') {
die "Perl_Batch_Error: Job file content is not valid JSON array.\n";
}
my $error_count = 0;
my $processed_count = 0;
my $job_number = 0;
foreach my $job (@$jobs_array_ref) {
$job_number++;
unless (ref $job eq 'HASH' && defined $job->{SourcePath} && defined $job->{DestPath}) {
print STDERR "Perl_Batch_Error: Skipping malformed JSON job object $job_number.\n";
$error_count++;
next;
}
my $src_path = $job->{SourcePath};
my $dest_path = $job->{DestPath};
eval {
decompress_single_z_file_core($src_path, $dest_path);
1;
};
if ($@) {
my $eval_error = $@;
chomp $eval_error;
print STDERR "Perl_Batch_Error: FAILED on job $job_number '$src_path' -> '$dest_path': $eval_error\n";
$error_count++;
} else {
$processed_count++;
}
}
exit $error_count;
PERL_BATCH_DECOMPRESS_EOF
)
# --- Temporary file setup ---
tmpDir=$(mktemp -d -t ark_mod_proc_XXXXXX)
if [[ "$tmpDir" != /* ]]; then
tmpDir="$PWD/$tmpDir"
fi
trap 'rm -rf "${tmpDir}"' EXIT HUP INT QUIT TERM
createModFilePerlExecutable="${tmpDir}/create_mod_file.pl"
ue4BatchDecompressPerlExecutable="${tmpDir}/ue4_batch_decompress.pl"
echo "${createModFilePerlScriptContent}" > "${createModFilePerlExecutable}"
echo "${ue4BatchDecompressPerlScriptContent}" > "${ue4BatchDecompressPerlExecutable}"
chmod +x "${createModFilePerlExecutable}" "${ue4BatchDecompressPerlExecutable}"
# --- Main functions ---
CheckJq() {
if ! command -v jq >/dev/null 2>&1; then
echo "Error: jq command not found. Please install it" >&2
return 1
fi
return 0
}
CheckPerl() {
if ! command -v perl >/dev/null 2>&1; then
echo "Error: Perl executable not found. Please install it" >&2
return 1
fi
if ! perl -MCompress::Zlib -e 1 >/dev/null 2>&1; then
echo "Error: Perl module 'Compress::Zlib' not found (core module). Please install it" >&2
return 1
fi
if ! perl -MJSON::PP -e 1 >/dev/null 2>&1; then
echo "Error: Perl module 'JSON::PP' not found. Please install it" >&2
return 1
fi
return 0
}
DownloadMod() {
local modId="$1"
local steamScript="${arkRootDir}/steamcmd.sh"
local steamInstallDir
steamInstallDir=$(realpath "${arkBaseDir}/Engine/Binaries/ThirdParty/SteamCMD/Linux")
local maxRetries=5
local attempt=0
local outputLog
local successStatus=0
echo "Downloading item ${modId} ..."
while (( attempt < maxRetries )); do
((attempt++))
outputLog=$("${steamScript}" +force_install_dir "${steamInstallDir}" +login anonymous +workshop_download_item 346110 "${modId}" validate +quit 2>&1)
if echo "${outputLog}" | grep -q -F "Success. Downloaded item ${modId}"; then
echo "Success. Downloaded item ${modId}"
successStatus=1
break
fi
if (( attempt < maxRetries )); then
echo "Warning: Item ${modId} download attempt ${attempt}/${maxRetries} failed. Retrying in 10s ..." >&2
sleep 10
fi
done
if [ "${successStatus}" -eq 1 ]; then return 0; else
echo "Error: Item ${modId} download failed after ${maxRetries} attempts" >&2
return 1
fi
}
InstallMod() {
local currentModId="$1"
echo "Installing/updating item ${currentModId} ..."
local sourceRootDir="${workshopContentDir}/${currentModId}"
local modContentDestDir="${modsInstallDir}/${currentModId}"
local modDefinitionFile="${modsInstallDir}/${currentModId}.mod"
if [ ! -d "${sourceRootDir}" ]; then
echo "Error: Source for item ${currentModId} ('${sourceRootDir}') not found" >&2
return 1
fi
mkdir -p "${modContentDestDir}" \
|| { echo "Error: Failed to create destination '${modContentDestDir}' for item ${currentModId}" >&2; return 1; }
local originalModInfoFile="${sourceRootDir}/mod.info"
local originalModMetaFile="${sourceRootDir}/modmeta.info"
if [ ! -f "${originalModInfoFile}" ]; then
echo "Error: mod.info for item ${currentModId} ('${originalModInfoFile}') not found" >&2
return 1
fi
local effectiveContentSourceDir="${sourceRootDir}"
local modMetaExistsAndReadable=0
if [ -f "${originalModMetaFile}" ] && [ -r "${originalModMetaFile}" ]; then
modMetaExistsAndReadable=1
effectiveContentSourceDir="${sourceRootDir}/WindowsNoEditor"
fi
local foundPrimalGameDataFile=0
local jobListFilePath="${tmpDir}/perl_z_job_list_${currentModId}_${RANDOM}.json"
if [ ! -d "${effectiveContentSourceDir}" ]; then
echo "Warning: Effective content source ('${effectiveContentSourceDir}') for item ${currentModId} does not exist. Cleaning destination" >&2
if [ -d "${modContentDestDir}" ]; then
find "${modContentDestDir}" -mindepth 1 -delete
fi
else
local -a zJobSourcePathsForPerl=()
local -a zJobDestPathsForPerl=()
local -a zJobsForBashTouch=()
local allSourceFilesList="${tmpDir}/all_source_files_${currentModId}_${RANDOM}.txt"
find "${effectiveContentSourceDir}" -type f -print0 > "${allSourceFilesList}"
if [ -s "${allSourceFilesList}" ]; then
while IFS= read -r -d $'\0' sourceFileFullPath; do
local cleanRelativeFile="${sourceFileFullPath#${effectiveContentSourceDir}/}"
cleanRelativeFile="${cleanRelativeFile#/}"
if [ -z "${cleanRelativeFile}" ]; then
continue
fi
local destFileParentDir
if [[ "${cleanRelativeFile}" == *".z.uncompressed_size" ]]; then
continue
elif [[ "${cleanRelativeFile}" == *".z" ]]; then
local destUncompressedFileFullPath="${modContentDestDir}/${cleanRelativeFile%.z}"
if [ ! -f "${destUncompressedFileFullPath}" ] || [ "${sourceFileFullPath}" -nt "${destUncompressedFileFullPath}" ]; then
destFileParentDir=$(dirname "${destUncompressedFileFullPath}")
mkdir -p "${destFileParentDir}" \
|| { echo "Error: Failed to create directory '${destFileParentDir}' for item ${currentModId}" >&2; return 1; }
local sourceMTime
sourceMTime=$(stat -c %Y "${sourceFileFullPath}")
zJobSourcePathsForPerl+=("${sourceFileFullPath}")
zJobDestPathsForPerl+=("${destUncompressedFileFullPath}")
zJobsForBashTouch+=("${sourceMTime}"$'\t'"${sourceFileFullPath}"$'\t'"${destUncompressedFileFullPath}")
fi
else
local destFileFullPath="${modContentDestDir}/${cleanRelativeFile}"
destFileParentDir=$(dirname "${destFileFullPath}")
if [ ! -e "${destFileFullPath}" ] || [ "${sourceFileFullPath}" -nt "${destFileFullPath}" ]; then
mkdir -p "${destFileParentDir}" \
|| { echo "Error: Failed to create directory '${destFileParentDir}' for item ${currentModId}" >&2; return 1; }
if ! { cp -p --reflink=auto "${sourceFileFullPath}" "${destFileFullPath}" 2>/dev/null || cp -p "${sourceFileFullPath}" "${destFileFullPath}"; }; then
echo "Error: Failed to copy '${cleanRelativeFile}' to '${destFileFullPath}' for item ${currentModId}." >&2
return 1
fi
fi
fi
done < "${allSourceFilesList}"
fi
rm -f "${allSourceFilesList}"
if [ ${#zJobSourcePathsForPerl[@]} -gt 0 ]; then
local jobListFilePath="${tmpDir}/perl_z_job_list_${currentModId}_${RANDOM}.json"
{
echo '['
for (( i=0; i < ${#zJobSourcePathsForPerl[@]}; i++ )); do
local srcPath="$(realpath "${zJobSourcePathsForPerl[i]}")"
local destPath="$(realpath -m "${zJobDestPathsForPerl[i]}")"
printf '{"SourcePath":"%s","DestPath":"%s"}' "$srcPath" "$destPath"
(( i < ${#zJobSourcePathsForPerl[@]} - 1 )) && echo ','
done
echo ']'
} > "$jobListFilePath"
local perlCmdOutput
if ! perlCmdOutput=$(perl "${ue4BatchDecompressPerlExecutable}" --jsonjobfile "${jobListFilePath}" 2>&1); then
local perlExitCode=$?
echo "Error: Perl batch decompression for item ${currentModId} failed. Exit code: ${perlExitCode}" >&2
echo "${perlCmdOutput}" >&2
rm -f "${jobListFilePath}"
return 1
fi
local perlExitCode=$?
if [ $perlExitCode -ne 0 ]; then
echo "Error: Perl batch decompression for item ${currentModId} reported ${perlExitCode} file error(s)." >&2
echo "${perlCmdOutput}" >&2
rm -f "${jobListFilePath}"
return 1
fi
for jobEntryWithTime in "${zJobsForBashTouch[@]}"; do
IFS=$'\t' read -r mtime src dest <<< "$jobEntryWithTime"
if [ -f "${dest}" ]; then
touch -c -m -d @"${mtime}" "${dest}" \
|| echo "Warning: Failed to set timestamp on '${dest}' for item ${currentModId}" >&2
else
echo "Warning: Decompressed file '${dest}' for item ${currentModId} not found after batch (Perl reported success)" >&2
fi
done
rm -f "${jobListFilePath}"
fi
local tempDestAllFiles="${tmpDir}/dest_all_files_check_${currentModId}_${RANDOM}.txt"
find "${modContentDestDir}" -type f -print0 > "${tempDestAllFiles}"
if [ -s "${tempDestAllFiles}" ] ; then
while IFS= read -r -d $'\0' destFileToCheck; do
local relativeDestPath="${destFileToCheck#${modContentDestDir}/}"
local correspondingSourceDirectFile="${effectiveContentSourceDir}/${relativeDestPath}"
local correspondingSourceZFile="${effectiveContentSourceDir}/${relativeDestPath}.z"
if [[ "${destFileToCheck}" == *.z ]]; then
rm -f "${destFileToCheck}"
elif [ ! -f "${correspondingSourceDirectFile}" ] && [ ! -f "${correspondingSourceZFile}" ]; then
rm -f "${destFileToCheck}"
fi
done < "${tempDestAllFiles}"
fi
rm -f "${tempDestAllFiles}"
find "${modContentDestDir}" -depth -type d -empty -delete 2>/dev/null || true
fi
if find "${modContentDestDir}" -type f -iname '*PrimalGameData*' -print -quit | grep -q '.'; then
foundPrimalGameDataFile=1
fi
local defaultModtypePerlArg=""
if [ "${modMetaExistsAndReadable}" -eq 0 ] && [ "${foundPrimalGameDataFile}" -eq 1 ]; then
defaultModtypePerlArg="--default-modtype-if-meta-empty"
fi
local createModCmdArray=( "${createModFilePerlExecutable}" )
createModCmdArray+=( "--modid" "${currentModId}" )
createModCmdArray+=( "--modinfo" "${originalModInfoFile}" )
createModCmdArray+=( "--output" "${modDefinitionFile}" )
if [ "${modMetaExistsAndReadable}" -eq 1 ]; then
createModCmdArray+=( "--modmeta" "${originalModMetaFile}" )
fi
if [ -n "${defaultModtypePerlArg}" ]; then
createModCmdArray+=( "${defaultModtypePerlArg}" )
fi
rm -f "${modDefinitionFile}"
if ! "${createModCmdArray[@]}"; then
echo "Error: Creation of .mod file for item ${currentModId} failed" >&2
return 1
fi
echo "Success: Installed/updated item ${currentModId}"
return 0
}
# --- Script entry point ---
if [ -z "$1" ]; then
echo "No workshop item IDs specified"
exit 1
fi
if ! CheckPerl; then
exit 1
fi
if ! CheckJq; then
exit 1
fi
commaSeparatedModIds=$(echo "$1" | sed 's/^"\(.*\)"$/\1/')
IFS=',' read -ra modIdArray <<< "$commaSeparatedModIds"
mkdir -p "${modsInstallDir}" \
|| { echo "Error: Failed to create base mods install directory '${modsInstallDir}'" >&2; exit 1; }
processedCount=0
failedCount=0
totalModsAttempted=0
echo "Installing/updating workshop items ..."
for modIdArg in "${modIdArray[@]}"; do
modIdArg=$(echo "${modIdArg}" | tr -d '[:space:]')
if [ -z "${modIdArg}" ]; then continue; fi
totalModsAttempted=$((totalModsAttempted + 1))
if ! [[ "${modIdArg}" =~ ^[0-9]+$ ]]; then
echo "Error: Invalid workshop item ID format '${modIdArg}'. Must be numeric. Skipping" >&2
failedCount=$((failedCount + 1))
continue
fi
if DownloadMod "${modIdArg}"; then
if InstallMod "${modIdArg}"; then
processedCount=$((processedCount + 1))
else
failedCount=$((failedCount + 1))
fi
else
failedCount=$((failedCount + 1))
fi
done
echo "--------------------------------------------------"
echo "Workshop item installation/update process finished"
echo "Summary:"
echo " Total items attempted: ${totalModsAttempted}"
echo " Successfully processed: ${processedCount}"
echo " Failed to process: ${failedCount}"
echo "--------------------------------------------------"
if [ "${failedCount}" -gt 0 ]; then
exit 1
fi
exit 0