forked from tactikauan/thcrap-steam-proton-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thcrap_proton
executable file
·154 lines (130 loc) · 5.47 KB
/
thcrap_proton
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
#!/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"
}
run_background() {
# 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
if [[ "$USE_THPRAC" == 1 && -z $(echo "$COMMAND" | grep -E 'custom\.exe$') ]]; then
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
# 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"
}
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 "---------------------------------------------------------"
set -m
run_background &
BACKGROUND_PID=$!
sh -c "$COMMAND"
kill -- -$BACKGROUND_PID