-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspt-linux-additions
executable file
·1357 lines (1106 loc) · 46.6 KB
/
spt-linux-additions
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# MIT License
# Copyright (c) 2025 MadByte
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to
# do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# ERROR CODES:
# 0 = normal exit (Success)
# 1 / 256 = General error
# 2 / 512 = Subshell error
# 3 / 768 = User aborted manually
#
# TODO LIST:
# TODO: Add ability to clean up backups
# TODO: Add ability to exclude files/directories from restore or backup
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Global variables #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
readonly TITLE="spt-linux-additions"
readonly VERSION="2025.1-5"
# ANSI codes
readonly BOLD="\e[1m"; readonly UNDERLINE="\e[2m"; readonly RESET="\e[0m"
readonly RED="\e[31m"; readonly GREEN="\e[32m"; readonly BLUE="\e[36m"
readonly YELLOW="\e[33m"; readonly GRAY="\e[90m"
# Common paths
readonly DATA_PATH="${XDG_DATA_HOME:-${HOME}/.local/share}"
readonly CONFIG_PATH="${XDG_CONFIG_HOME:-${HOME}/.config}"
readonly CACHE_PATH="${XDG_CACHE_HOME:-${HOME}/.cache}/${TITLE}"
readonly TEMP_PATH="${CACHE_PATH}/tmp"
readonly BINARIES_PATH="${CACHE_PATH}/binaries"
readonly METADATA_PATH="${CACHE_PATH}/json"
readonly LOG_PATH="${CACHE_PATH}/spt-linux-additions.log"
# Script variables
readonly SCRIPT_UPDATE_URL="https://raw.githubusercontent.com/MadByteDE/SPT-Linux-Guide/refs/heads/main/installers/spt-linux-additions"
readonly SCRIPT_ROOT_PATH="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Mod variables
readonly MOD_RELEASE_JSON_URL="https://spt-releases.modd.in/release.json"
readonly MOD_RELEASE_JSON_PATH="${METADATA_PATH}/release.json"
readonly MOD_FILE_PATH="${CACHE_PATH}/SPT.7z"
# Patcher variables
readonly PATCHER_MIRRORS_JSON_URL="https://slugma.waffle-lord.net/mirrors.json"
readonly PATCHER_MIRRORS_JSON_PATH="${METADATA_PATH}/mirrors.json"
readonly PATCHER_FILE_PATH="${CACHE_PATH}/Patcher.7z"
# SPT variables
readonly SPT_USER_DATA_DIRS=("user" "BepInEx/config" "BepInEx/plugins" "BepInEx/patchers")
readonly SPT_USER_CONFIG_DIRS=("user/sptSettings" "user/profiles" "user/launcher" "BepInEx/config")
readonly SPT_DEFAULT_INSTALL_PATH=${SPT_INSTALL_PATH:-"drive_c/SPTarkov"}
readonly SPT_DEFAULT_BACKUP_PATH=${SPT_BACKUP_PATH:-"drive_c/spt-backups"}
readonly SPT_LATEST_BACKUP_FILE="${CACHE_PATH%/}/latest_backup"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Dependencies #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# hpatchz (Download)
readonly HPATCHZ_URL="https://github.com/sisong/HDiffPatch/releases/download/v4.8.0/hdiffpatch_v4.8.0_bin_linux64.zip"
readonly HPATCHZ_BIN_PATH="linux64/hpatchz"
# xxd (Download)
readonly XXD_URL="http://ftp.de.debian.org/debian/pool/main/v/vim/xxd_9.0.1378-2_amd64.deb"
readonly XXD_BIN_PATH="usr/bin/xxd"
# 7z (Lutris runtime)
readonly ZIP_BIN_PATH="${DATA_PATH}/lutris/runtime/p7zip/7z"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Helper functions #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
m_echo() {
# Filter ANSI sequences
local filtered_str=$(echo -e "${@}" | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g')
case ${NO_ANSI} in
1) echo "${filtered_str}" ;;
*) echo -e "${@}" ;;
esac
# Echo to log file
echo "${filtered_str}" 2>/dev/null >> "${LOG_PATH}"
}
m_sleep() {
local delay=${1:-2}
[[ ${NO_WAIT} -ne 1 ]] && sleep "${delay}"
}
m_exit() {
local status=${1:-0}
# Make sure some stuff is only run when we're not in a subshell env
if [[ ! -v ${BASH_SUBSHELL} || ${BASH_SUBSHELL} -eq 0 ]]; then
if [[ "$status" -ne 0 ]]; then
# This is a workaround for Lutris closing the installer cli immediately after
# encountering an error & just showing the -not so useful- error code
if [[ "$status" -ne 3 ]]; then
[[ "$PATH" == *"lutris"* ]] && m_sleep 10
# open the error log using the default text editor
m_run xdg-open "${LOG_PATH}"
fi
fi
# Remove temp files before exiting
if [[ -d "${TEMP_PATH}" ]]; then
m_dbg "Removing temp files in '${TEMP_PATH}'"
rm -rf "${TEMP_PATH}" || m_warn "Failed to remove temp files"
fi
fi
exit "${status}"
}
m_msg() {
m_echo "${FUNCNAME[1]}: $*"
}
m_success() {
m_echo "${FUNCNAME[1]}: ${BOLD}${BLUE}$*${RESET}"
}
m_warn() {
m_echo "${BOLD}${YELLOW}warn: ${FUNCNAME[1]}: $*${RESET}"
}
m_dbg() {
[[ ${DEBUG_OUTPUT} -eq 1 ]] && m_echo "${GRAY}m_dbg: ${FUNCNAME[1]}: $*${RESET}"
}
m_err() {
local status=${2:-1}
m_echo "${BOLD}${RED}ERROR: ${FUNCNAME[1]}: $1 (⊙_⊙')${RESET}"
m_exit "${status}"
}
m_subshell_err() {
local status=${2:-2}
printf "${BOLD}${RED}$1${RESET}\n"
m_exit "${status}"
}
m_mkdir() {
local target_path="$1"
if [[ ! -d "${target_path}" ]]; then
# Check if the target path is inside a valid directory
case "${target_path}" in
*"${PREFIX_PATH}"* | *"${CACHE_PATH}"*) ;;
*) m_err "Invalid target directory '$target_path'" ;;
esac
# Create missing directories
mkdir -p "${target_path}" 1>/dev/null || m_err "Failed to create directory in '${target_path}'"
fi
}
m_cp() {
local source_path="${1}"
local target_path="${2}"
# Check exists
[[ ! -d "${source_path}" ]] && m_err "Source '${source_path}' does not exist"
# Check if the target path is inside a valid directory
case "${target_path}" in
*"${PREFIX_PATH}"* | *"${CACHE_PATH}"*) ;;
*) m_err "Invalid target directory '$target_path'" ;;
esac
# Create missing directories
m_mkdir "${target_path}"
# Copy files to target path
if [[ -n "$( ls -A "${source_path}" )" ]]; then
cp --reflink=auto -r "${source_path}"* "${target_path}" \
|| m_err "Failed to copy files to '${target_path}'"
fi
}
m_run() {
# Execute custom command
"$@"; local status=$?
if [[ $status -ne 0 ]]; then
# Prevent messages when in a subshell to not break return values
if [[ ! -v ${BASH_SUBSHELL} || ${BASH_SUBSHELL} -eq 0 ]]; then
m_warn "Failed to run command:"
m_echo " > $*"
m_echo " > ${BOLD}Error code:${RESET} $status"
fi
return $status
fi
}
m_ask() {
m_echo "$* [y/n]"
if [[ $NO_PROMPT -eq 1 ]]; then
m_msg "'NO_PROMPT' environment variable set - return 0"
return 0
fi
while true; do
read -r -p "> " yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) m_dbg "Aborted"; m_exit 3 ;;
esac
done
}
m_download() {
local url="${1}"
local output_path="${2}"
local max_redirs="${3:-0}"
local filename="${url##*/}"
local args=(-f -L --connect-timeout 60 --max-redirs "$max_redirs" --create-dirs)
[[ -z "${filename}" ]] \
&& filename="${output_path##*/}"
m_dbg "Download url: ${url}"
m_dbg "Download output path: ${output_path}"
m_msg "Downloading '${filename}'"
m_run curl "${args[@]}" "${url}" -o "${output_path}"; local status=$?
case $status in
0) m_success "Downloaded '${filename}' successfully" ;;
*) m_warn "Failed to download '${filename}'"; return $status ;;
esac
}
m_extract() {
local archive_path="${1}"
local target_path="${2}"
local filename="${archive_path##*/}"
m_msg "Extracting '${filename}'"
m_run "${ZIP_BIN_PATH}" x "${archive_path}" -o"${target_path}" 1>/dev/null
local status=$?
case $status in
0) m_success "Extracted '${filename}' successfully" ;;
*) m_err "Failed to extract '${filename}'" ;;
esac
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# EFT helper functions #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
get_eft_install_path() {
local prefix_path="${1:-"${PREFIX_PATH}"}"
prefix_path="${prefix_path%/}"
[[ ! -d "${prefix_path}" ]] \
&& m_err "Directory '${prefix_path}' does not exist"
# get windows formatted path
local registry="${prefix_path}/system.reg"
local reg_entry=$(sed -n '/Uninstall\\\\EscapeFromTarkov/,/^$/p' "${registry}")
local path=$(echo "${reg_entry}" | \
grep -o "\"InstallLocation\"=\"[^}]*" | cut -d "\"" -f4)
case "${path}" in
'') m_err "Looks like EFT is not installed" ;;
*)
# Replace backslashes with slashes
local path="${path//\\\\/\/}"
# Finally assemble the full path
local path=$(echo "${prefix_path}/${path//C:/drive_c}")
printf "${path}\n"
;;
esac
}
get_installed_eft_version() {
local eft_install_path="${1:-"${EFT_INSTALL_PATH}"}"
# Check exists
[[ ! -d "${eft_install_path}" ]] \
&& m_err "Directory '${eft_install_path}' does not exist"
# Check if main exe exists
local exe_file="${eft_install_path}/EscapeFromTarkov.exe"
[[ ! -f "${exe_file}" ]] \
&& m_err "File '${exe_file}' does not exist"
# Extract metadata
local tmp_dir="${TEMP_PATH}/exe"
m_extract "${exe_file}" "${tmp_dir}" 1>/dev/null
# Finally get the EFT version from the version.txt file
# make sure to remove null byte and -stupid- windows \r -_-* (learned it the hard way!)
local version_file="${tmp_dir}/.rsrc/0/version.txt"
local version=$(m_run cat "${version_file}" | head -1 | tr -d '\0\r' | cut -d "," -f4)
# Check returned value
case "${version}" in
''|*[!0-9]*) m_err "Failed to get version info" ;;
*) printf "${version}\n" ;;
esac
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Patcher helper functions #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
get_patcher_urls() {
local mirrors_json_path="${1:-"${PATCHER_MIRRORS_JSON_PATH}"}"
local mirror_index="${2}"
local urls=$(cat "${mirrors_json_path}" \
| grep -o "\"Link\":[^}]*" \
| cut -d "\"" -f4)
if [[ -n "${mirror_index}" ]]; then
echo "${urls}" | sed "${mirror_index}q;d" 2>/dev/null
return 0
fi
# Check returned value
case "${urls}" in
'') m_err "Failed to fetch mirror URLs from '${mirrors_json_path}'" ;;
*) printf "${urls}\n" ;;
esac
}
get_patcher_target_eft_version() {
local mirrors_json_path="${1:-"${PATCHER_MIRRORS_JSON_PATH}"}"
local url=$(get_patcher_urls "${mirrors_json_path}" 1) \
|| m_subshell_err "${url}"
# Split filename to get version infos from it
local formatted_filename=$(echo "${url##*/}" | tr "_." " ")
# get target EFT version
local version=$(echo "${formatted_filename}" | cut -d " " -f5)
# Check returned value
case "${version}" in
''|*[!0-9]*) m_err "Failed to get version info" ;;
*) printf "${version}\n" ;;
esac
}
download_patcher() {
# Get all mirror urls
local urls="${1:-${PATCHER_MIRROR_URLS}}"; [ "${urls}" == '' ] && m_err "Missing mirror urls"
m_dbg "Got mirrors: '${urls}'"
# Try to download from first to last
echo "${urls}" | while read -r url; do
m_msg "Using mirror: '${url}'"
m_download "${url}" "${PATCHER_FILE_PATH}" && break
done
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Installer helper functions #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
get_json_value() {
local json_file_path="${1}"
# Check if json file exists
[[ ! -f "${json_file_path}" ]] \
&& m_err "File '${json_file_path}' does not exist"
local value_name="${2}"
# Check if the value name is provided
[[ -z "${value_name}" ]] && m_err "Missing argument 'value_name'"
# Fetch the value
local value=$(cat "${json_file_path}" \
| grep -o "\"${value_name}\":[^}]*" \
| cut -d "\"" -f4)
# Check returned value
case "${value}" in
'') m_err "Failed to fetch value '$value_name' from '${json_file_path}'" ;;
*) printf "${value}\n" ;;
esac
}
check_hash() {
local file_path="${1}"
local expected_hash="${2}"
# Check arguments
[[ ! -f "${file_path}" ]] && m_err "File '${file_path}' does not exist"
[[ -z "${expected_hash}" ]] && m_err "Missing 'expected_hash' argument"
# use file to calculate MD5 hash and ENCODE in base64
m_msg "Calculating checksum of file '${file_path##*/}'"
local file_hash=$(md5sum "${file_path}" | cut -d ' ' -f 1 | m_run "$BINARIES_PATH/xxd" -r -p | base64) \
|| m_subshell_err "${file_hash}"
m_dbg "File hash: ${file_hash} Expected hash: ${expected_hash}"
# Check if a hash is returned
[[ -z "${file_hash}" ]] \
&& m_err "Failed to calculate hash"
# Check if they match
m_msg "Comparing hashes"
if [[ "${file_hash}" != "${expected_hash}" ]]; then
m_warn "Mismatch with expected hash (File: '${file_hash}' != Expected: '${expected_hash}')"
return 1
fi
# Success
m_success "OK"
}
check_flatpak_env() {
# Check for flatpak env
if [[ -v ${FLATPAK_SANDBOX_DIR} ]]; then
m_echo "${BOLD}${YELLOW}Flatpak environment detected!${RESET}"
m_echo
m_echo " > It looks like you're running the installer from a sandboxed environment."
m_echo " > Flatpak support is ${BOLD}EXPERIMENTAL${RESET}!"
m_echo
FLATPAK_ENV=1
m_sleep
fi
}
check_eft_running() {
case $(pidof "BsgLauncher.exe") in
'') m_success "OK" ;;
*) m_err "Please close the BsgLauncher and the EFT client before installing (and playing) SPT!" ;;
esac
}
check_dependencies() {
local cmds=("$ZIP_BIN_PATH" "$BINARIES_PATH/xxd" "$BINARIES_PATH/hpatchz")
# Check each cmd and error out if something is missing
# We need to handle packages as well - different ones for Fedora (who would have guessed :/)
for cmd in "${cmds[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
case ${cmd} in
"$BINARIES_PATH/xxd")
fetch_dependency "${XXD_URL}" "${XXD_BIN_PATH}" \
|| m_err "Failed to fetch dependency: 'xxd'"
;;
"$BINARIES_PATH/hpatchz")
fetch_dependency "${HPATCHZ_URL}" "${HPATCHZ_BIN_PATH}" \
|| m_err "Failed to fetch dependency: 'hpatchz'"
;;
"7z") m_err "Missing dependency: '${cmd}'" ;;
esac
fi
done
# Success
m_success "OK"
}
fetch_dependency() {
local url="${1}"
local bin_path="${2}"
local filename="${url##*/}"
local dep=${bin_path##*/}
# Ask for permission to install dependency
m_msg "'$dep' will be downloaded from:"
m_echo " ${url}"
m_ask "Do you want to continue?"
# TODO: Check if the URLs target file is an archive or an executable!
local temp_path="${TEMP_PATH}/$dep"
local archive_path="${temp_path}/${filename}"
# Download
m_download "${url}" "${archive_path}" 1 || m_err "Failed to download '$dep'"
# Extract
m_extract "${archive_path}" "${temp_path}"
case "${filename}" in
*deb*) m_extract "${temp_path}/data.tar" "${temp_path}" ;;
*) ;;
esac
# Install
m_msg "Installing '$dep'..."
m_mkdir "${BINARIES_PATH}"
mv -f "${temp_path}/$bin_path" "${BINARIES_PATH}/" || m_err "Failed to move '$dep' to '${BINARIES_PATH}'"
chmod +x "${BINARIES_PATH}/$dep" || m_err "Failed to make file executable: '$dep'"
m_msg "'${dep}' has been installed successfully!"
}
check_disk_space() {
local path=${1:-${PREFIX_PATH}}
[[ -z "${path}" ]] && m_err "Missing path argument"
# Get required values
local free_kb=$(df -k "${path}" | tail -1 | awk '{print $4}')
local free_gb=$(echo "$free_kb" | awk '{ free = $1 / 1024/1024 ; printf("%.2fGiB\n", free) }')
local eft_kb=$(du -cs "${EFT_INSTALL_PATH}" | head -1 | cut -d$'\t' -f1)
local overhead_kb=$((1024*1024*10))
local total_kb=$(($eft_kb + $overhead_kb))
local total_gb=$(echo $total_kb | awk '{ total = $1 / 1024/1024 ; printf("%.2fGiB\n", total) }')
m_dbg "Free disk space: ${free_kb} kb (checked in ${path})"
m_dbg "EFT install size: ${eft_kb} kb"
m_dbg "Total required free space: ${total_kb} kb"
# Check if there's enough space
[[ $free_kb -le $total_kb ]] \
&& m_err "Not enough free disk space! ($free_gb available < $total_gb needed)"
# Success
m_success "OK ($free_gb available > $total_gb needed)"
}
check_mod_install_path() {
# Reinstall mode - use some common files to validate that this is a game directory
if [[ ${REINSTALL_SPT} -eq 1 ]]; then
[[ ! -d "${SPT_INSTALL_PATH}" ]] \
&& m_err "SPT install directory at '${SPT_INSTALL_PATH}' does not exist"
# FIXME: Not the best way to verify the game directory.. hmmm...
local try_paths=( "SPT_Data" "BepInEx" "EscapeFromTarkov.exe" )
for path in "${try_paths[@]}"; do
local full_path="${SPT_INSTALL_PATH}/${path}"
[[ ! -e "${full_path}" ]] \
&& m_err "Directory is not a valid SPT install directory"
done
# Check if install dir is empty
elif [[ ${REINSTALL_SPT} -eq 0 ]] && [[ -n "$(ls -A "${SPT_INSTALL_PATH}" 2>/dev/null)" ]]; then
m_warn "SPT install directory not empty!"
m_echo
m_echo " > Install path: '${SPT_INSTALL_PATH}'"
m_echo " > Please make sure the path is correct and there are no files inside the directory!"
m_echo
m_err "SPT install directory not empty"
fi
# TODO: Check for weird paths passed to the installer
m_success "OK (Path: '${SPT_INSTALL_PATH}')"
}
verify_cached() {
local cached_path="${1}"; [[ -z "${cached_path}" ]] && m_err "Missing cached path argument"
local expected_hash="${2}"; [[ -z "${expected_hash}" ]] && m_err "Missing expected hash argument"
# Make sure the path actually is in CACHE_PATH
[[ "$cached_path" == *"${CACHE_PATH}"* ]] \
|| m_err "Given file isn't inside the scripts cache directory"
# Verify the file is there and valid
local filename=${cached_path##*/}
m_msg "Checking for cached file '${filename}'"
if [[ -f "${cached_path}" ]]; then
m_success "Found '${filename}' in cache directory"
# Check hash
if ! check_hash "${cached_path}" "${expected_hash}"; then
m_msg "Removing cached file '${filename}'"
rm -f "${cached_path}" \
|| m_err "Failed to remove file '${filename}'"
return 1
else
return 0
fi
else
m_warn "File not cached"
return 1
fi
}
remove_spt_install() {
# Remove old spt installation
m_msg "Removing old/corrupted SPT files"
# Make sure to remove only stuff inside the prefix directory
if [[ "${SPT_INSTALL_PATH}" == *"${PREFIX_PATH}"* ]]; then
rm -rf "${SPT_INSTALL_PATH}" || m_warn "Failed to remove SPT install directory"
fi
}
cp_eft_files() {
local source_dir="${1:-${EFT_INSTALL_PATH}}"
local target_dir="${2:-${SPT_INSTALL_PATH}}"
m_msg "Copying EFT game files to '${target_dir}'"
# In case of failure, remove the created SPT directory to prevent the next install
# attempt to fail because of existing files
if ! m_cp "${source_dir%/}/" "${target_dir%/}"; then
remove_spt_install
m_err "Failed to copy EFT game files"
fi
}
set_prefix_path() {
local absolute_path="${1:-"${SCRIPT_ROOT_PATH}"}"
# Check if the path is valid
if [[ ! -f "${absolute_path}/system.reg" ]]; then
m_warn "'${absolute_path}' is not a valid wine prefix directory"
fi
# Update global variable
PREFIX_PATH="${absolute_path}"
}
set_spt_path() {
local relative_path="${1:-${SPT_DEFAULT_INSTALL_PATH}}"
local absolute_path="${PREFIX_PATH%/}/${relative_path}"
# Check if the path is valid
if [[ "${absolute_path}" != *"drive_c"* ]]; then
m_err "The path must be installed inside the 'drive_c' directory"
elif [[ "${absolute_path}" != *"${PREFIX_PATH}"* ]]; then
m_err "The path must be inside the correct wine prefix directory"
fi
# Update global variable
SPT_INSTALL_PATH="${absolute_path}"
}
set_backup_path() {
local relative_path="${1:-${SPT_DEFAULT_BACKUP_PATH}}"
local absolute_path="${PREFIX_PATH%/}/${relative_path}"
# Check if the path is valid
if [[ "${absolute_path}" != *"drive_c"* ]]; then
m_err "The path must be inside the 'drive_c' directory"
elif [[ "${absolute_path}" != *"${PREFIX_PATH}"* ]]; then
m_err "The path must be inside the correct wine prefix directory"
fi
# Update global variable
SPT_BACKUP_PATH="${absolute_path}"
}
validate_metadata() {
local file_path="${1}"
# Check if file exists, if not - fail the check
if [[ ! -f "${file_path}" ]]; then
m_warn "File does not exist"
return 1
fi
# Calculate time elapsed since last modified
# default is 1 hour
local ttl="${2:-"3600"}"
local current=$(date +%s)
local modified=$(date -r "${file_path}" +%s)
((elapsed = current - modified))
m_dbg "Elapsed since modified: ${elapsed} seconds. File: '${file_path}'"
# If elapsed time >= TTL - fail the check
if [[ $elapsed -ge $ttl ]]; then
m_warn "File is outdated"
return 1
fi
# Check passed
m_success "File is valid"
}
fetch_metadata() {
local url="${1}"; [ ! "${url}" ] && m_err "Missing expected url argument"
local path="${2}"; [ ! "${path}" ] && m_err "Missing expected path argument"
m_dbg "Metadata URL: '${url}'"
m_dbg "Metadata PATH: '${path}'"
if ! validate_metadata "${path}"; then
# (Re-)Download json file
m_download "${url}" "${path}" \
|| m_warn "Failed to download latest metadata. The Installation might fail!"
fi
}
get_cached_backup_path() {
if [[ ! -f "${SPT_LATEST_BACKUP_FILE}" ]]; then
m_dbg "File '${SPT_LATEST_BACKUP_FILE}' does not exist"
return
fi
CACHED_BACKUP_PATH=$(cat "${SPT_LATEST_BACKUP_FILE}")
[[ -z "${CACHED_BACKUP_PATH}" ]] \
&& m_warn "Failed to get cached backup path"
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Option functions #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
opt_patcher() {
m_msg "Launching patcher..."
local target_path="${1:-"${PATCHER_TARGET_PATH}"}"
target_path="${target_path%/}"
local source_path="${2:-"${PATCHER_SOURCE_PATH}"}"
source_path="${source_path%/}"
local patcher_cmd="${BINARIES_PATH}/hpatchz"
# Make sure hpatchz is installed
if ! command -v "${BINARIES_PATH}/hpatchz" &>/dev/null; then
fetch_dependency "${HPATCHZ_URL}" "${HPATCHZ_BIN_PATH}" \
|| m_err "Missing dependency 'hpatchz'. Please install it & try again"
fi
[[ ! -d "${target_path}" ]] \
&& m_err "Target directory '${target_path}' does not exist"
# Search in optional SOURCE directory
if [ "$(find "$source_path" -maxdepth 1 -type d -name "SPT_Patches" 2>/dev/null)" ]; then
m_msg "Using 'SPT_Patches' found in '${source_path}'"
# Search in TARGET directory
elif [ "$(find "$target_path" -maxdepth 1 -type d -name "SPT_Patches" 2>/dev/null)" ]; then
m_msg "Using 'SPT_Patches' found in '${target_path}'"
source_path="${target_path}"
# No 'SPT_Patches' directory found
else
m_err "Unable to locate 'SPT_Patches' directory in '${source_path}'"
fi
local progress_start=1
local progress_total=$(find "$source_path/SPT_Patches" \( -type f -printf x \) | wc -c)
find "$source_path/SPT_Patches" \( -type f \) | while read source; do
# progress_current
local progress_current="[${progress_start}/${progress_total}]"
# Isolate the operation extension
local extension="${source##*.}"
# Isolate sub-directories of SPT_Patches
local formatted="${source#*SPT_Patches/}"
# Remove operation extension to get actual file name
formatted="${formatted%.*}"
local output="${target_path}/${formatted}"
local target="${output}.decoded"
local dir_path="${output%/*}"
case "$extension" in
"delta")
m_msg "${progress_current} (*) Patching: ${formatted##*/}"
m_run "${patcher_cmd}" "$output" "$source" "$target" &>/dev/null
# Remove old file and replace with .decoded file
[[ ! -f "${target}" ]] && m_err "Decoded file not found!"
rm "${output}" || m_err "Failed to remove '${output}'"
mv "${target}" "${output}" || m_err "Failed to move '${target##*/}' to '${output}'"
;;
"del")
m_msg "${progress_current} (-) Deleting: ${formatted##*/}"
rm "${output}" || m_err "Failed to remove '${output}'"
if [[ -z "$(ls -A "${dir_path}")" ]]; then
m_dbg "${progress_current} (-) Deleting directory: ${dir_path##*/}"
rmdir "${dir_path}" || m_err "Failed to remove: ${dir_path}"
fi
;;
"new")
m_msg "${progress_current} (+) Adding: ${formatted##*/}"
if [[ ! -d "${dir_path}" ]]; then
m_dbg "${progress_current} (+) Adding directory: ${dir_path##*/}"
m_mkdir "${dir_path}"
fi
cp "${source}" "${output}" || m_err "Failed to add file"
;;
*)
m_err "File '${source}': Invalid or missing operation extension"
;;
esac
progress_start=$((${progress_start}+1))
done
if [ $? -ne 0 ]; then
remove_spt_install
m_err "Failed to patch game files"
fi
# SUCCESS! ※\(^o^)/※
if [[ "${source_path}" == "${target_path}" ]]; then
m_msg "Cleaning up ..."
if [[ -d "${source_path}/SPT_Patches" ]]; then
rm -rf "${source_path}/SPT_Patches" || m_warn "Failed to remove 'SPT_Patches' directory"
fi
if [[ -f "${source_path}/patcher.exe" ]]; then
rm -rf "${source_path}/patcher.exe" || m_warn "Failed to remove 'patcher.exe'"
fi
fi
m_success "Done! ※\(^o^)/※"
}
opt_installer() {
m_msg "Launching installer..."
set_prefix_path "${1}"
set_spt_path "${2}"
# Check EFT installation
m_msg "Checking for EFT inside the prefix..."
EFT_INSTALL_PATH=$(get_eft_install_path) \
|| m_subshell_err "${EFT_INSTALL_PATH}"
# Check if the game directory from the install path actually exists
[[ ! -d "${EFT_INSTALL_PATH}" ]] && m_err "'${EFT_INSTALL_PATH}' does not exist!"
m_msg "Found EFT game directory in '${EFT_INSTALL_PATH}'"
# Pre-Checks
check_eft_running
check_dependencies
check_disk_space
check_mod_install_path
# Check & get installed EFT version
m_msg "Getting installed EFT version"
INSTALLED_EFT_VERSION=$(get_installed_eft_version) || m_subshell_err "${INSTALLED_EFT_VERSION}"
m_success "Found EFT version: '${INSTALLED_EFT_VERSION}'"
if [[ ${REINSTALL_SPT} -eq 1 ]]; then
# Backup user files
m_msg "Creating backup of user files"
opt_backup
fi
# Get required metadata for mod and patcher releases
fetch_metadata "${MOD_RELEASE_JSON_URL}" "${MOD_RELEASE_JSON_PATH}"
fetch_metadata "${PATCHER_MIRRORS_JSON_URL}" "${PATCHER_MIRRORS_JSON_PATH}"
# Set global variable for patcher mirror urls
PATCHER_MIRROR_URLS=$(get_patcher_urls) || m_subshell_err "${PATCHER_MIRROR_URLS}"
m_dbg "Patcher mirror urls: Got '${PATCHER_MIRROR_URLS}'"
# Fetch latest mod release URL
MOD_RELEASE_URL=$(get_json_value "${MOD_RELEASE_JSON_PATH}" "DownloadUrl") \
|| m_subshell_err "${MOD_RELEASE_URL}"
m_dbg "Mod release url: Got '${MOD_RELEASE_URL}'"
# Check & get latest SPT version
MOD_VERSION=$(get_json_value "${MOD_RELEASE_JSON_PATH}" "AkiVersion") \
|| m_subshell_err "${MOD_VERSION}"
m_dbg "Mod version: Got '${MOD_VERSION}'"
# Check & get required EFT version
REQUIRED_EFT_VERSION=$(get_json_value "${MOD_RELEASE_JSON_PATH}" "ClientVersion") \
|| m_subshell_err "${REQUIRED_EFT_VERSION}"
m_dbg "Required EFT version: Got '${REQUIRED_EFT_VERSION}'"
# Mod hash
MOD_EXPECTED_HASH=$(get_json_value "${MOD_RELEASE_JSON_PATH}" "Hash") \
|| m_subshell_err "${MOD_EXPECTED_HASH}"
m_dbg "Expected mod hash: Got '${MOD_EXPECTED_HASH}'"
# Patcher hash
PATCHER_EXPECTED_HASH=$(get_json_value "${PATCHER_MIRRORS_JSON_PATH}" "Hash" | head -1) \
|| m_subshell_err "${PATCHER_EXPECTED_HASH}"
m_dbg "Expected patcher hash: Got '${PATCHER_EXPECTED_HASH}'"
# Patcher required EFT version
PATCHER_TARGET_EFT_VERSION=$(get_patcher_target_eft_version) \
|| m_subshell_err "${PATCHER_TARGET_EFT_VERSION}"
m_dbg "Patcher targeted EFT version: Got '${PATCHER_TARGET_EFT_VERSION}'"
# Check for cached mod archive
if ! verify_cached "${MOD_FILE_PATH}" "${MOD_EXPECTED_HASH}"; then
m_download "${MOD_RELEASE_URL}" "${MOD_FILE_PATH}" \
|| m_err "Failed to download SPT mod archive"
# Make sure the file is good to use
check_hash "${MOD_FILE_PATH}" "${MOD_EXPECTED_HASH}" \
|| m_err "Downloaded mod archive is corrupted"
fi
m_msg "Latest SPT release is '${MOD_VERSION}'"
# Check if we need to patch the game files
m_msg "Comparing installed EFT <--> required EFT versions"
if [[ "${INSTALLED_EFT_VERSION}" -ne "${REQUIRED_EFT_VERSION}" ]]; then
m_msg "Incompatible EFT version found - Checking latest downpatcher"
# Check if target EFT version is lower, greater or equal to required EFT version
if [[ "${PATCHER_TARGET_EFT_VERSION}" -lt "${INSTALLED_EFT_VERSION}" ]]; then
# No patcher available yet!
m_warn "Looks like there's no patcher available for your EFT version"
m_echo
m_echo " > This usually happens when EFT has been updated recently."
m_echo " > Please wait for the SPT team to generate a new patcher."
m_echo " > Usually, this will take less then 24 hours."
m_echo
m_err "No patcher available"
elif [[ "${PATCHER_TARGET_EFT_VERSION}" -gt "${INSTALLED_EFT_VERSION}" ]]; then
# EFT is outdated!
m_warn "Looks like your installed EFT version is out-of-date"
m_echo
m_echo " > Please update your live EFT installation & try again"
m_echo
m_err "EFT is out-of-date"
elif [[ "${PATCHER_TARGET_EFT_VERSION}" -eq "${INSTALLED_EFT_VERSION}" ]]; then
# Downpatcher is compatible!
m_success "Patcher is compatible"
# Check for cached patcher archive
if ! verify_cached "${PATCHER_FILE_PATH}" "${PATCHER_EXPECTED_HASH}"; then
download_patcher
check_hash "${PATCHER_FILE_PATH}" "${PATCHER_EXPECTED_HASH}" \
|| m_err "Downloaded patcher archive is corrupted"
fi
# Extract downpatcher to temp directory
local patcher_temp_path="${TEMP_PATH}/patcher"
m_extract "${PATCHER_FILE_PATH}" "${patcher_temp_path}"
# Remove old SPT install if needed & copy EFT files
[[ ${REINSTALL_SPT} -eq 1 ]] && remove_spt_install
cp_eft_files
# Move patcher files to install directory
m_msg "Moving patcher files to '${SPT_INSTALL_PATH}'"
m_run find "$patcher_temp_path" -type d -name '*SPT_Patches*' \
| m_run xargs mv -t "${SPT_INSTALL_PATH}" \
|| m_err "Failed to move patcher files to SPT install directory"
# Patch game using downloaded patcher files
opt_patcher "${SPT_INSTALL_PATH}" || m_err "Something went wrong while patching game files"
fi
else
m_msg "Installed EFT version is compatible with 'SPT ${MOD_VERSION}'"
# Remove old SPT install if needed & copy EFT files
[[ ${REINSTALL_SPT} -eq 1 ]] && remove_spt_install
cp_eft_files
fi
# Extract mod archive to install directory
m_extract "${MOD_FILE_PATH}" "${SPT_INSTALL_PATH}"
# DONE! (⌐■_■)
m_msg "${BOLD}${GREEN}All done! ※\(^o^)/※${RESET}"
m_sleep
}
opt_version() {
m_echo "${BOLD}~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~${RESET}"
m_echo "${BOLD}| SPT-LINUX-ADDITIONS |${RESET}"
m_echo "${BOLD}~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~${RESET}"
m_echo "AiO setup tool for SPT"
m_echo "${BOLD}Version: ${VERSION}${RESET}"