lug-helper/lug-helper.sh

448 lines
16 KiB
Bash
Raw Normal View History

#!/usr/bin/env sh
2020-08-02 18:12:02 +00:00
############################################################################
# Star Citizen's Linux Users Group Helper Script
############################################################################
#
# Greetings, fellow penguin!
#
#
# This script is designed to help you with some housekeeping tasks.
#
# It will check your system for optimal settings,
# allowing you to change them as needed to prevent game crashes.
#
#
# It also includes an easy way for you to wipe your USER folder
# whenever there is a major version update. It will back up your
# exported keybinds, delete your USER folder, then restore your keybinds.
#
# To export your keybinds from within the game, go to
# Options->Keybindings->Control Profiles->Save Control Settings
#
############################################################################
2020-08-10 20:03:11 +00:00
wine_conf="winedir.conf"
game_conf="gamedir.conf"
backup_conf="backupdir.conf"
# Use the XDG config directory if defined
if [ -z "$XDG_CONFIG_HOME" ]; then
conf_dir="$HOME/.config"
else
conf_dir="$XDG_CONFIG_HOME"
fi
2020-08-02 18:12:02 +00:00
2020-08-10 20:03:11 +00:00
conf_subdir="starcitizen-lug"
2020-08-02 18:12:02 +00:00
############################################################################
############################################################################
2020-08-10 01:30:10 +00:00
# Display a message to the user. Expects a numerical argument followed by the string to display.
message() {
2020-08-03 22:15:38 +00:00
if [ "$has_zen" -eq 1 ]; then
if [ "$1" -eq 1 ]; then
# info
2020-08-04 01:32:00 +00:00
margs=("--info" "--no-wrap" "--text=")
2020-08-03 22:15:38 +00:00
elif [ "$1" -eq 2 ]; then
# warning
2020-08-12 03:10:16 +00:00
margs=("--warning" "--text=")
2020-08-03 22:15:38 +00:00
elif [ "$1" -eq 3 ]; then
# question
2020-08-04 01:32:00 +00:00
margs=("--question" "--text=")
2020-08-03 22:15:38 +00:00
elif [ "$1" -eq 4 ]; then
# radio list
2020-08-04 01:32:00 +00:00
margs=("--list" "--radiolist" "--height=200" "--column=" "--column=What would you like to do?")
2020-08-03 22:15:38 +00:00
elif [ "$1" -eq 5 ]; then
# main menu radio list
2020-08-04 01:32:00 +00:00
margs=("--list" "--radiolist" "--height=175" "--text=Welcome, fellow penguin, to the Star Citizen LUG Helper Script!" "--column=" "--column=What would you like to do?")
2020-08-03 22:15:38 +00:00
else
echo -e "Invalid message format.\n\nThe message function expects a numerical argument followed by the string to display.\n"
read -n 1 -s -p "Press any key..."
fi
2020-08-02 18:12:02 +00:00
2020-08-03 22:15:38 +00:00
# Display the message
2020-08-04 01:32:00 +00:00
if [ "$1" -eq 4 ] || [ "$1" -eq 5 ]; then
# requires a space between the assembled arguments
shift 1 # drop the first numerical argument and shift the remaining up one
zenity "${margs[@]}" "$@" --width="400" --title="Star Citizen LUG Helper Script"
2020-08-04 01:32:00 +00:00
else
# no space between the assmebled arguments
shift 1 # drop the first numerical argument and shift the remaining up one
zenity "${margs[@]}""$@" --width="400" --title="Star Citizen LUG Helper Script"
2020-08-04 01:32:00 +00:00
fi
else
# Text based menu. Does not work with message types 4 and 5 (zenity radio lists)
# those need to be handled specially in the code
if [ "$1" -eq 1 ]; then
# info
echo -e "\n$2\n"
read -n 1 -s -p "Press any key..."
elif [ "$1" -eq 2 ]; then
# warning
echo -e "\n$2\n"
read -n 1 -s -p "Press any key..."
exit 0
elif [ "$1" -eq 3 ]; then
# question
2020-08-03 22:15:38 +00:00
echo -e "$2"
2020-08-12 02:02:44 +00:00
while read -p "[y/n]: " yn; do
case "$yn" in
[Yy]*)
2020-08-03 22:15:38 +00:00
return 0
;;
[Nn]*)
2020-08-03 22:15:38 +00:00
return 1
;;
*)
2020-08-03 22:15:38 +00:00
echo "Please type 'y' or 'n'"
;;
esac
done
else
echo -e "Invalid message type.\n\nText menus are not compatible with message types 4 and 5 (zenity radio lists)\nand require special handling.\n"
read -n 1 -s -p "Press any key..."
fi
fi
2020-08-02 18:12:02 +00:00
}
2020-08-10 20:03:11 +00:00
# Get paths to the user's wine prefix, game directory, and a backup directory
2020-08-10 01:30:10 +00:00
getdirs() {
2020-08-10 20:03:11 +00:00
# Sanity checks
if [ ! -d "$conf_dir" ]; then
2020-08-12 03:10:16 +00:00
message 2 "Config directory not found. The script will now exit.\n\n$conf_dir"
2020-08-10 20:03:11 +00:00
exit 0
fi
if [ ! -d "$conf_dir/$conf_subdir" ]; then
mkdir "$conf_dir/$conf_subdir"
fi
# Check if the config files already exist
if [ -f "$conf_dir/$conf_subdir/$wine_conf" ]; then
2020-08-12 00:42:44 +00:00
wine_prefix="$(cat "$conf_dir/$conf_subdir/$wine_conf")"
2020-08-10 20:03:11 +00:00
fi
if [ -f "$conf_dir/$conf_subdir/$game_conf" ]; then
2020-08-12 00:42:44 +00:00
game_path="$(cat "$conf_dir/$conf_subdir/$game_conf")"
2020-08-10 20:03:11 +00:00
fi
if [ -f "$conf_dir/$conf_subdir/$backup_conf" ]; then
2020-08-12 00:42:44 +00:00
backup_path="$(cat "$conf_dir/$conf_subdir/$backup_conf")"
2020-08-10 20:03:11 +00:00
fi
2020-08-12 00:42:44 +00:00
2020-08-12 02:02:44 +00:00
if [ -z "$wine_prefix" ] || [ -z "$game_path" ] || [ -z "$backup_path" ]; then
message 1 "You will now be asked to provide some directories needed by this script.\n\nThey will be saved for later use in:\n$conf_dir/$conf_subdir/"
2020-08-12 00:42:44 +00:00
if [ "$has_zen" -eq 1 ]; then
# Get the wine prefix directory
2020-08-12 02:02:44 +00:00
if [ -z "$wine_prefix" ]; then
wine_prefix="$(zenity --file-selection --directory --title="Select your WINE prefix directory" --filename="$HOME/.wine")"
2020-08-12 00:42:44 +00:00
if [ "$?" -eq -1 ]; then
message 2 "An unexpected error has occurred."
2020-08-12 02:02:44 +00:00
exit 0
elif [ -z "$wine_prefix" ]; then
# User clicked cancel
message 2 "Operation cancelled. The script will now exit."
exit 0
2020-08-12 00:42:44 +00:00
fi
2020-08-10 20:03:11 +00:00
fi
2020-08-12 00:42:44 +00:00
# Get the game path
2020-08-12 02:02:44 +00:00
if [ -z "$game_path" ]; then
while game_path="$(zenity --file-selection --directory --title="Select your Star Citizen LIVE directory" --filename="$wine_prefix/")"; do
2020-08-12 00:42:44 +00:00
if [ "$?" -eq -1 ]; then
message 2 "An unexpected error has occurred."
2020-08-12 02:02:44 +00:00
exit 0
elif [ "$(basename "$game_path")" != "LIVE" ]; then
2020-08-12 00:42:44 +00:00
message 2 "You must select your LIVE directory."
2020-08-12 02:02:44 +00:00
else
# All good or cancel
2020-08-12 00:42:44 +00:00
break
fi
done
2020-08-12 02:02:44 +00:00
if [ -z "$game_path" ]; then
# User clicked cancel
message 2 "Operation cancelled. The script will now exit."
exit 0
fi
2020-08-12 00:42:44 +00:00
fi
2020-08-10 20:03:11 +00:00
2020-08-12 00:42:44 +00:00
# Get the backup directory
2020-08-12 02:02:44 +00:00
if [ -z "$backup_path" ]; then
2020-08-12 00:42:44 +00:00
backup_path="$(zenity --file-selection --directory --title="Select a backup directory for your keybinds" --filename="$HOME/")"
if [ "$?" -eq -1 ]; then
message 2 "An unexpected error has occurred."
2020-08-12 02:02:44 +00:00
exit 0
elif [ -z "$backup_path" ]; then
# User clicked cancel
message 2 "Operation cancelled. The script will now exit."
exit 0
2020-08-12 00:42:44 +00:00
fi
2020-08-10 20:03:11 +00:00
fi
2020-08-12 00:42:44 +00:00
else
clear
2020-08-10 20:03:11 +00:00
2020-08-12 00:42:44 +00:00
# Get the wine prefix directory
2020-08-12 02:02:44 +00:00
if [ -z "$wine_prefix" ]; then
echo -e "Enter the full path to your WINE prefix directory (case sensitive)"
2020-08-12 00:42:44 +00:00
echo -e "ie. /home/USER/.wine/"
while read -rp ": " wine_prefix; do
if [ ! -d "$wine_prefix" ]; then
echo -e "That directory is invalid or does not exist. Please try again.\n"
else
break
fi
done
2020-08-10 01:30:10 +00:00
2020-08-12 00:42:44 +00:00
# Get the game path
2020-08-12 02:02:44 +00:00
if [ -z "$game_path" ]; then
echo -e "\nEnter the full path to your Star Citizen installation LIVE directory\n(case sensitive)"
2020-08-12 00:42:44 +00:00
echo -e "ie. /home/USER/.wine/drive_c/Program Files/Roberts Space Industries/Star Citizen/LIVE/"
while read -rp ": " game_path; do
if [ ! -d "$game_path" ]; then
echo -e "That directory is invalid or does not exist. Please try again.\n"
elif [ "$(basename "$game_path")" != "LIVE" ]; then
echo -e "You must select your LIVE directory."
else
break
fi
done
fi
2020-08-10 01:30:10 +00:00
2020-08-12 00:42:44 +00:00
# Get the backup directory
2020-08-12 02:02:44 +00:00
if [ -z "$backup_path" ]; then
echo -e "\nEnter the full path to a backup directory for your keybinds (case sensitive)"
2020-08-12 00:42:44 +00:00
echo -e "ie. /home/USER/backups/"
while read -rp ": " backup_path; do
if [ ! -d "$backup_path" ]; then
echo -e "That directory is invalid or does not exist. Please try again.\n"
else
break
fi
done
fi
fi
fi
2020-08-10 20:03:11 +00:00
# Save the paths for later use
echo "$wine_prefix" > "$conf_dir/$conf_subdir/$wine_conf"
echo "$game_path" > "$conf_dir/$conf_subdir/$game_conf"
echo "$backup_path" > "$conf_dir/$conf_subdir/$backup_conf"
fi
# Set some remaining directory paths
user_dir="$game_path/USER"
mappings_dir="$user_dir/Controls/Mappings"
2020-08-10 01:30:10 +00:00
}
2020-08-02 18:12:02 +00:00
# Save exported keybinds, wipe the USER directory, and restore keybinds
sanitize() {
clear
2020-08-12 00:42:44 +00:00
# Prompt user to back up the current keybinds in the game
message 1 "Before proceeding, please be sure you have\nexported your Star Citizen keybinds from within the game!\n\nTo do this, launch the game and go to:\nOptions->Keybindings->Control Profiles->Save Control Settings"
2020-08-10 20:03:11 +00:00
2020-08-12 00:42:44 +00:00
# Get/Set directory paths
getdirs
2020-08-02 18:12:02 +00:00
2020-08-12 02:46:05 +00:00
# Sanity check
if [ ! -d "$user_dir" ]; then
2020-08-12 03:10:16 +00:00
message 2 "Directory not found. The script will now exit.\n\n$user_dir"
2020-08-12 02:46:05 +00:00
exit 0
fi
2020-08-12 00:42:44 +00:00
# Check for exported keybind files
if [ ! -d "$mappings_dir" ] || [ -z "$(ls -A "$mappings_dir")" ]; then
2020-08-12 02:46:05 +00:00
if message 3 "Warning: No exported keybindings found. Keybinds will not be backed up!\n\nDo you want to continue anyway?"; then
exported=0
else
message 2 "The script will now exit."
exit 0
fi
2020-08-12 00:42:44 +00:00
else
exported=1
fi
2020-08-02 18:12:02 +00:00
2020-08-12 00:42:44 +00:00
# Back up keybinds
if [ "$exported" -eq 1 ]; then
echo "Backing up all saved keybinds..."
2020-08-12 03:01:08 +00:00
cp -r "$mappings_dir/." "$backup_path/keybinds/"
2020-08-12 00:42:44 +00:00
echo -e "Done.\n"
fi
# Wipe the user directory
echo "Wiping USER directory..."
2020-08-12 03:01:08 +00:00
mv "$user_dir" "$backup_path/userbackup_$(date +"%Y%m%d-%H%M%S")"
2020-08-12 00:42:44 +00:00
echo -e "Done.\n"
2020-08-02 18:12:02 +00:00
2020-08-12 00:42:44 +00:00
# Restore custom keybinds
if [ "$exported" -eq 1 ]; then
echo "Restoring keybinds..."
mkdir -p "$mappings_dir" && cp -r "$backup_path/keybinds/." "$mappings_dir/"
2020-08-12 00:42:44 +00:00
echo -e "Done.\n"
message 1 "\nTo re-import your keybinds, select it in-game from the list:\nOptions->Keybindings->Control Profiles\n"
fi
2020-08-02 18:12:02 +00:00
2020-08-10 20:03:11 +00:00
message 1 "Your USER directory has been cleaned up!"
2020-08-02 18:12:02 +00:00
}
check_map_count() {
2020-08-02 18:12:02 +00:00
if [ "$(cat /proc/sys/vm/max_map_count)" -lt 16777216 ]; then
message 2 "As far as this script can detect, your system is not configured\nto allow Star Citizen to use more than ~8GB or memory.\n\nYou will most likely experience crashes."
2020-08-02 18:12:02 +00:00
fi
}
# Check vm.max_map_count for the correct setting and let the user fix it if needed
set_map_count() {
clear
2020-08-02 18:12:02 +00:00
# If vm.max_map_count is already set, no need to do anything
if [ "$(cat /proc/sys/vm/max_map_count)" -ge 16777216 ]; then
message 1 "vm.max_map_count is already set to the optimal value. You're all set!"
2020-08-02 18:12:02 +00:00
exit 0
fi
trap check_map_count EXIT
2020-08-02 18:12:02 +00:00
if grep -E -x -q "vm.max_map_count" /etc/sysctl.conf /etc/sysctl.d/* 2>/dev/null; then
2020-08-12 03:13:12 +00:00
if message 3 "It looks like you already configured your system to work with Star Citizen\nand saved the setting to persist across reboots.\nHowever, for some reason the persistence part did not work.\n\nFor now, would you like to enable the setting again until the next reboot?"; then
2020-08-02 18:12:02 +00:00
pkexec sh -c 'sysctl -w vm.max_map_count=16777216'
fi
exit 0
fi
once="Change setting until next reboot"
persist="Change setting and persist after reboot"
manual="Show me the commands; I'll handle it myself"
2020-08-04 02:08:19 +00:00
if message 3 "Running Star Citizen requires changing a system setting.\n\nvm.max_map_count must be increased to at least 16777216\nto avoid crashes in areas with lots of geometry.\n\nAs far as this script can detect, the setting\nhas not been changed on your system.\n\nWould you like to change the setting now?"; then
2020-08-03 22:15:38 +00:00
if [ "$has_zen" -eq 1 ]; then
# zenity menu
2020-08-04 01:32:00 +00:00
list=("TRUE" "$once" "FALSE" "$persist" "FALSE" "$manual")
RESULT="$(message 4 "${list[@]}")"
case "$RESULT" in
"$once")
2020-08-03 22:15:38 +00:00
pkexec sh -c 'sysctl -w vm.max_map_count=16777216'
;;
"$persist")
2020-08-03 22:15:38 +00:00
if [ -d "/etc/sysctl.d" ]; then
pkexec sh -c 'echo "vm.max_map_count = 16777216" >> /etc/sysctl.d/20-max_map_count.conf && sysctl -p'
else
pkexec sh -c 'echo "vm.max_map_count = 16777216" >> /etc/sysctl.conf && sysctl -p'
fi
;;
"$manual")
2020-08-03 22:15:38 +00:00
if [ -d "/etc/sysctl.d" ]; then
message 1 "To change the setting (a kernel parameter) until next boot, run:\n\nsudo sh -c 'sysctl -w vm.max_map_count=16777216'\n\nTo persist the setting between reboots, run:\n\nsudo sh -c 'echo \"vm.max_map_count = 16777216\" >> /etc/sysctl.d/20-max_map_count.conf && sysctl -p'"
else
message 1 "To change the setting (a kernel parameter) until next boot, run:\n\nsudo sh -c 'sysctl -w vm.max_map_count=16777216'\n\nTo persist the setting between reboots, run:\n\nsudo sh -c 'echo \"vm.max_map_count = 16777216\" >> /etc/sysctl.conf && sysctl -p'"
fi
# Anyone who wants to do it manually doesn't need another warning
trap - EXIT
;;
*)
2020-08-03 22:15:38 +00:00
echo "Dialog canceled or unknown option selected: $RESULT"
;;
esac
else
# text menu
2020-08-03 22:15:38 +00:00
echo -e "\n"
options=("$once" "$persist" "$manual")
PS3="Enter selection number or 'q' to quit: "
select choice in "${options[@]}"
do
case "$REPLY" in
"1")
2020-08-03 22:15:38 +00:00
pkexec sh -c 'sysctl -w vm.max_map_count=16777216'
break
;;
"2")
2020-08-03 22:15:38 +00:00
if [ -d "/etc/sysctl.d" ]; then
pkexec sh -c 'echo "vm.max_map_count = 16777216" >> /etc/sysctl.d/20-max_map_count.conf && sysctl -p'
else
pkexec sh -c 'echo "vm.max_map_count = 16777216" >> /etc/sysctl.conf && sysctl -p'
fi
break
;;
"3")
2020-08-03 22:15:38 +00:00
clear
if [ -d "/etc/sysctl.d" ]; then
message 1 "To change the setting (a kernel parameter) until next boot, run:\n\nsudo sh -c 'sysctl -w vm.max_map_count=16777216'\n\nTo persist the setting between reboots, run:\n\nsudo sh -c 'echo \"vm.max_map_count = 16777216\" >> /etc/sysctl.d/20-max_map_count.conf && sysctl -p'"
else
message 1 "To change the setting (a kernel parameter) until next boot, run:\n\nsudo sh -c 'sysctl -w vm.max_map_count=16777216'\n\nTo persist the setting between reboots, run:\n\nsudo sh -c 'echo \"vm.max_map_count = 16777216\" >> /etc/sysctl.conf && sysctl -p'"
fi
# Anyone who wants to do it manually doesn't need another warning
trap - EXIT
break
;;
"q")
2020-08-03 22:15:38 +00:00
break
;;
*)
2020-08-03 22:15:38 +00:00
echo -e "\nInvalid selection"
continue
;;
esac
done
fi
2020-08-02 18:12:02 +00:00
fi
}
############################################################################
# MAIN
############################################################################
2020-08-04 02:04:27 +00:00
clear
2020-08-02 18:12:02 +00:00
2020-08-03 22:15:38 +00:00
# Check if Zenity is available
has_zen=0
if [ -x "$(command -v zenity)" ]; then
2020-08-12 02:02:44 +00:00
has_zen=1
2020-08-03 22:15:38 +00:00
fi
2020-08-02 18:12:02 +00:00
# Use Zenity if it is available
2020-08-03 22:15:38 +00:00
if [ "$has_zen" -eq 1 ]; then
2020-08-04 02:04:27 +00:00
check="Check vm.max_map_count for optimal performance"
2020-08-02 18:12:02 +00:00
clean="Delete my USER folder and preserve my keybinds"
2020-08-12 02:02:44 +00:00
list=("TRUE" "$check" "FALSE" "$clean")
2020-08-04 01:32:00 +00:00
options="$(message 5 "${list[@]}")"
case "$options" in
"$check")
set_map_count
2020-08-02 18:12:02 +00:00
;;
2020-08-04 01:32:00 +00:00
"$clean")
2020-08-02 18:12:02 +00:00
sanitize
;;
*)
;;
esac
else
2020-08-03 22:15:38 +00:00
# Use a text menu if Zenity is not available
2020-08-02 18:12:02 +00:00
echo -e "\nWelcome, fellow penguin, to the Star Citizen Linux Users Group Helper Script!\nWhat would you like to do?\n"
2020-08-04 02:04:27 +00:00
options=("Check vm.max_map_count for optimal performance" "Delete my USER folder and preserve my keybinds")
2020-08-02 18:12:02 +00:00
PS3="Enter selection number or 'q' to quit: "
select choice in "${options[@]}"
do
case "$REPLY" in
2020-08-02 18:12:02 +00:00
"1")
echo -e "\n"
set_map_count
2020-08-02 18:12:02 +00:00
break
;;
"2")
echo -e "\n"
sanitize
break
;;
"q")
break
;;
*)
echo -e "\nInvalid selection"
continue
;;
esac
done
fi