-
Notifications
You must be signed in to change notification settings - Fork 1
/
mc
executable file
·150 lines (132 loc) · 3.96 KB
/
mc
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
#!/usr/bin/bash
set -e
# Hardcoded path
SCRIPT_DIR="${HOME}/.local/share/minecraft"
BINDMOUNT_TARGET="${HOME}/.minecraft"
# Derived path
SHARED_DOT_MINECRAFT="${SCRIPT_DIR}/dotminecraft"
FIREJAIL_CONFIG="${SCRIPT_DIR}/firejail-java-minecraft.conf"
GAMEDIR=""
LAUNCHER_PY="${SCRIPT_DIR}/launcher.py"
cd "$SCRIPT_DIR"
# Parse arguments
OFFLINE_USERNAME=""
PRINT_HELP="NO"
LIST_VERSIONS="NO"
JAVA_EXECUTABLE="/usr/bin/java"
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-d|--open-folder)
xdg-open "${SCRIPT_DIR}"
exit
;;
-f|--firejail)
FIREJAIL_CONFIG="$2"
shift # past argument
shift # past value
;;
-g|--gamedir)
GAMEDIR="$SCRIPT_DIR/$2"
shift # past argument
shift # past value
;;
-h|--help)
PRINT_HELP=YES
shift # past argument
;;
-j|--java)
JAVA_EXECUTABLE="$2"
shift # past argument
shift # past value
;;
-l|--list-version)
LIST_VERSIONS=YES
shift # past argument
;;
-p|--offline)
OFFLINE_USERNAME="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ "$LIST_VERSIONS" == "YES" ]]; then
ls -la "${SHARED_DOT_MINECRAFT}/versions"
exit
fi
VERSION="$1"
if [[ "$VERSION" == "" || "$PRINT_HELP" == "YES" ]]; then
echo "Usage: $0 [opts] <version>"
echo " -d : Open in file browser: ${SCRIPT_DIR}"
echo " -f <filejail_cfg> : Start game in firejail"
echo " Default to ${SCRIPT_DIR}/firejail-java-minecraft.conf"
echo " Set to empty to disable firejail"
echo " -g <gamedir> : Gamedir name in script dir"
echo " -h : Print this help message"
echo " -j <path> : Java executable. Default to /usr/bin/java"
echo " -l : List versions"
echo " -p <offline_name> : Offline mode player name"
echo
echo "The following files may be put inside the gamedir"
echo " custom_jvm_args.txt : Extra JVM arguments to be prepended."
echo " extra_natives/ : Extra native binaries to be loaded."
exit
fi
if [[ "$GAMEDIR" == "" ]]; then
GAMEDIR="${SCRIPT_DIR}/gamedir_${VERSION}"
fi
mkdir -p "$SCRIPT_DIR"
mkdir -p "$BINDMOUNT_TARGET"
mkdir -p "$SHARED_DOT_MINECRAFT"
mkdir -p "$GAMEDIR"
echo "Selected version: ${VERSION}"
echo "Selected gamedir: ${GAMEDIR}"
# Bind mount gamedir to target
echo "Password for bind mount:"
sudo systemd-mount --unmount "${BINDMOUNT_TARGET}" || true
sudo systemd-mount -o bind "${GAMEDIR}" "${BINDMOUNT_TARGET}"
not_support() {
echo "Not supported"
exit
}
ensure_file() {
FNAME="$1"
DEFAULT_CONTENT="$2"
if [[ ! -f "$FNAME" ]]; then
echo -n "$DEFAULT_CONTENT" > "$FNAME"
fi
}
launch_with_firejail() {
if [[ "${OFFLINE_USERNAME}" != "" ]]; then
EXTRA_LAUNCHER_PY_ARGS="--offline ${OFFLINE_USERNAME}"
fi
EXTRA_NATIVES=$(shopt -s nullglob; echo ${GAMEDIR}/extra_natives/* | tr ' ' ,)
export PYTHONDONTWRITEBYTECODE=1
${LAUNCHER_PY} $EXTRA_LAUNCHER_PY_ARGS\
--extra_natives "${EXTRA_NATIVES}"\
--version ${VERSION} --dotmc_folder "${SHARED_DOT_MINECRAFT}"\
--gamedir "${BINDMOUNT_TARGET}" --argfile="${GAMEDIR}/launch_argfile.txt"
cd "${BINDMOUNT_TARGET}"
ensure_file "custom_jvm_args.txt" $'-XX:+UseZGC\n-Xmx8G\n'
# Prepend custom JVM argument
cat "custom_jvm_args.txt" "launch_argfile.txt" > "launch_argfile.txt.new"
mv "launch_argfile.txt.new" "launch_argfile.txt"
firejail --profile="${FIREJAIL_CONFIG}"\
--whitelist="${SHARED_DOT_MINECRAFT}"\
--whitelist="${BINDMOUNT_TARGET}"\
--read-only="${SHARED_DOT_MINECRAFT}"\
--read-write="${SHARED_DOT_MINECRAFT}/assets/skins"\
xargs -a launch_argfile.txt -d '\n' "${JAVA_EXECUTABLE}"
}
if [[ "${FIREJAIL_CONFIG}" == "" ]]; then
not_support
else
launch_with_firejail
fi