#!/usr/bin/env bash [ -z "$XDG_DATA_HOME" ] && XDG_DATA_HOME="$HOME/.local/share" THCRAP_FOLDER="$XDG_DATA_HOME/thcrap" THCRAP_CONFIG=en.js # This is `thprac` not `thcrap` THPRAC_FOLDER="$XDG_DATA_HOME/thprac" ZENITY=$(which zenity) echo "" > '/tmp/thcrap_proton.log' exec &>>'/tmp/thcrap_proton.log' exec 2>&1 launch_config() { RUN=$( echo "$COMMAND" | sed -r "s@ waitforexitandrun.*@ waitforexitandrun '$THCRAP_FOLDER/thcrap.exe' --skip-search-games@" ) sh -c "$RUN" } while getopts "vp c:e:" flag; do case "${flag}" in v) USE_VPATCH=1;; p) USE_THPRAC=1;; c) THCRAP_CONFIG=${OPTARG};; e) COMMAND=${OPTARG};; # Deprecated esac done if [ -z "$COMMAND" ]; then shift "$(($OPTIND -1))" COMMAND='' # Quote all arguments that contain whitespace for i in "$@"; do [[ $i =~ ( ) ]] && i=${i@Q} COMMAND="$COMMAND $i" done fi # Format byte-encoded characters back to text COMMAND=$(printf "$COMMAND") echo "---------------------ORIGINAL COMMAND--------------------" echo "$COMMAND" echo "---------------------------------------------------------" if [ ! -f "$THCRAP_FOLDER/thcrap.exe" ]; then if ${ZENITY} --question --text="thcrap doesn't appear to be installed on \"$THCRAP_FOLDER\". Would you like this script to install it there?"; then mkdir -p "$THCRAP_FOLDER" if [ ! -w "$THCRAP_FOLDER" ]; then ${ZENITY} --error --text="The directory \"$THCRAP_FOLDER\" could not be created or is not writable. Check your permissions and try again." exit fi LD_LIBRARY_PATH=/usr/lib64:/usr/lib curl -L -f 'https://github.com/thpatch/thcrap/releases/latest/download/thcrap.zip' -o /tmp/thcrap.zip | ${ZENITY} --progress --pulsate --auto-close --text="Downloading thcrap.zip" if [ $? != 0 ] || [ ! -f "/tmp/thcrap.zip" ]; then ${ZENITY} --error --text="Download failed. Aborting" exit fi if bsdtar -C "$THCRAP_FOLDER" -xf /tmp/thcrap.zip || unzip -d "$THCRAP_FOLDER" /tmp/thcrap.zip; then launch_config else ${ZENITY} --error --text="Failed to extract archive. Make sure you have installed either unzip or bsdtar (libarchive). Aborting" exit fi rm /tmp/thcrap.zip fi fi if [ ! -f "$THCRAP_FOLDER/config/$THCRAP_CONFIG" ]; then CONFIGS=$(ls -Qm "$THCRAP_FOLDER/config" || printf none) if ${ZENITY} --question --text="Config file \"$THCRAP_CONFIG\" not found (found: $CONFIGS). Would you like to launch the configuration tool?"; then launch_config fi fi if [ "$USE_THPRAC" == 1 ]; then THPRAC_VERSION=$(LD_LIBRARY_PATH=/usr/lib64:/usr/lib curl --connect-timeout 5 -si https://github.com/touhouworldcup/thprac/releases/latest | grep location: | sed 's@.*/@@' | tr -d '[:space:]') if [[ -n "$THPRAC_VERSION" && ! -f "$THPRAC_FOLDER/thprac.$THPRAC_VERSION.exe" ]]; then # Install latest version mkdir -p "$THPRAC_FOLDER" if [ ! -w "$THPRAC_FOLDER" ]; then ${ZENITY} --error --text="The directory \"$THPRAC_FOLDER\" could not be created or is not writable. Check your permissions and try again." exit fi LD_LIBRARY_PATH=/usr/lib64:/usr/lib curl -L -f "https://github.com/touhouworldcup/thprac/releases/download/$THPRAC_VERSION/thprac.$THPRAC_VERSION.exe" -o "$THPRAC_FOLDER/thprac.$THPRAC_VERSION.exe" | ${ZENITY} --progress --pulsate --auto-close --text="Downloading \"thprac.$THPRAC_VERSION.exe\"" if [ $? = 0 ]; then # Create a symlink to latest version ln -srf "$THPRAC_FOLDER/thprac.$THPRAC_VERSION.exe" "$THPRAC_FOLDER/thprac.exe" # Remove old versions, only keep the latest version find "$THPRAC_FOLDER" -maxdepth 1 -name "thprac.*.exe" \! -name "thprac.$THPRAC_VERSION.exe" -exec rm {} \; fi fi # Make sure thprac is installed if [ ! -f "$THPRAC_FOLDER/thprac.exe" ]; then # no installed version ${ZENITY} --error --text="thprac installation failed. Aborting" exit fi fi TH_REGEX="(th[0-9]+|東方紅魔郷)" if [ "$USE_VPATCH" == 1 ]; then COMMAND=$( echo "$COMMAND" | sed -r "s/$TH_REGEX\.exe/vpatch.exe/" ) fi COMMAND=$( echo "$COMMAND" | sed -r "s@ waitforexitandrun@& '$THCRAP_FOLDER/thcrap_loader.exe' $THCRAP_CONFIG@" ) echo "--------------------GENERATED COMMAND--------------------" echo "$COMMAND" echo "---------------------------------------------------------" sh -c "$COMMAND" & if [[ "$USE_THPRAC" == 1 && -z $(echo "$COMMAND" | grep -E 'custom\.exe$') ]]; then # wait 5s and launch thprac so it could auto attach to the game sleep 5 PROTON_CMD=$( echo "$COMMAND" | sed -n "s/^.*-- \(.*\) waitforexitandrun.*$/\\1/p" ) RUN_THPRAC="$PROTON_CMD run '$THPRAC_FOLDER/thprac.exe'" echo "-----------------------THPRAC COMMAND--------------------" echo "$RUN_THPRAC" echo "---------------------------------------------------------" sh -c "$RUN_THPRAC" & fi # Get PID while ensuring only the actual Touhou executable is chosen while [ -z "$TH_PID" ]; do # Loop until a matching process is found TH_PID=$(grep -Er "\(($TH_REGEX|custom\.exe)" /proc/*/stat | sed "s/.*:\([0-9]\+\).*/\\1/") sleep 5 done # Check if Touhou is still running while ps -p $TH_PID > /dev/null; do sleep 5 done # Find and kill the thcrap_loader process pkill -f "thcrap_loader.exe"