Skip to content

Commit

Permalink
misc: Flatten getGameWindowPID
Browse files Browse the repository at this point in the history
Remove a bunch of nesting for readibility.

Needs tested to make sure it still works."
  • Loading branch information
sonic2kk committed Jun 12, 2024
1 parent 28c9858 commit caace67
Showing 1 changed file with 67 additions and 63 deletions.
130 changes: 67 additions & 63 deletions steamtinkerlaunch
Original file line number Diff line number Diff line change
Expand Up @@ -7674,75 +7674,79 @@ function getGameWinNameFromXid {
function getGameWindowPID {
if [ "$MO2MODE" == "gui" ]; then
writelog "SKIP" "${FUNCNAME[0]} - Skipping check for window pid, because MO2MODE is '$MO2MODE'"
else
if [ -n "$GAMEWINPID" ]; then
writelog "SKIP" "${FUNCNAME[0]} - Already have the GAMEWINPID '$GAMEWINPID'"
echo "$GAMEWINPID"
else
MAXWAIT=20
COUNTER=0
TESTPID="$NON"
WASCLOSED=0
fi

touch "$PIDLOCK"
if [ -n "$GAMEWINPID" ]; then
writelog "SKIP" "${FUNCNAME[0]} - Already have the GAMEWINPID '$GAMEWINPID'"
echo "$GAMEWINPID"

while [ "$COUNTER" -lt "$MAXWAIT" ]; do
if [ -f "$CLOSETMP" ]; then
writelog "WAIT" "${FUNCNAME[0]} - ${PROGNAME,,} is just closing - leaving loop"
WASCLOSED=1
break
fi
TESTPID="$("$XDO" getactivewindow getwindowpid)"
SYMCWD="$(readlink "/proc/$TESTPID/cwd")"
SYMEXE="$(readlink "/proc/$TESTPID/exe")"

if [ -n "$TESTPID" ]; then
if [ -n "$HAVPA" ]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for game 'HAVPA' '$HAVPA' - leaving loop"
FOUNDWIN="$YES"
break;
elif [ -n "$EXECUTABLE" ] && [ "$EXECUTABLE" == "$(cat "/proc/$TESTPID/comm")" ]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for game 'EXECUTABLE' '$EXECUTABLE' - leaving loop"
FOUNDWIN="$YES"
break;
elif [ -n "$GE" ] && [ "$GE" == "$(cut -d '.' -f1 < "/proc/$TESTPID/comm")" ]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for game executable 'GE' '$GE' - leaving loop"
FOUNDWIN="$YES"
break;
else
# might not even be required anymore - maybe remove later:
if [[ "$SYMCWD" == "$EFD" ]] && [[ "$SYMEXE" != *"$YAD"* ]]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for CWD '$SYMCWD' with EXE '$SYMEXE' - leaving loop"
FOUNDWIN="$YES"
break;
fi
if [ -n "$STEAM_COMPAT_CLIENT_INSTALL_PATH" ] && [[ "$SYMCWD" == "$STEAM_COMPAT_CLIENT_INSTALL_PATH" ]] && [[ "$SYMEXE" != *"$YAD"* ]]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for STEAM_COMPAT_CLIENT_INSTALL_PATH '$SYMCWD' with EXE '$SYMEXE' - leaving loop"
FOUNDWIN="$YES"
break;
fi
fi
fi
writelog "WAIT" "${FUNCNAME[0]} - Sec $COUNTER/$MAXWAIT Game Window with pwd '$EFD' not yet in front"
COUNTER=$((COUNTER+1))
sleep 1
done
return
fi

MAXWAIT=20
COUNTER=0
TESTPID="$NON"
WASCLOSED=0

rm "$PIDLOCK" 2>/dev/null
touch "$PIDLOCK"

if [ "$FOUNDWIN" == "$YES" ]; then
if [ "$TESTPID" == "$NON" ]; then
writelog "SKIP" "${FUNCNAME[0]} - FAIL - Found PID returned but it is empty: '$TESTPID'"
fi
writelog "INFO" "${FUNCNAME[0]} - Found PID '$TESTPID' for running exe '$(readlink "/proc/$TESTPID/exe")'"
echo "$TESTPID"
else
if [ "$WASCLOSED" -eq 0 ]; then
writelog "SKIP" "${FUNCNAME[0]} - ERROR - timeout waiting for '$EFD' window"
echo "$NON"
fi
while [ "$COUNTER" -lt "$MAXWAIT" ]; do
if [ -f "$CLOSETMP" ]; then
writelog "WAIT" "${FUNCNAME[0]} - ${PROGNAME,,} is just closing - leaving loop"
WASCLOSED=1
break
fi
TESTPID="$("$XDO" getactivewindow getwindowpid)"
SYMCWD="$(readlink "/proc/$TESTPID/cwd")"
SYMEXE="$(readlink "/proc/$TESTPID/exe")"

if [ -z "$TESTPID" ]; then
continue
fi

if [ -n "$HAVPA" ]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for game 'HAVPA' '$HAVPA' - leaving loop"
FOUNDWIN="$YES"
break;
elif [ -n "$EXECUTABLE" ] && [ "$EXECUTABLE" == "$(cat "/proc/$TESTPID/comm")" ]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for game 'EXECUTABLE' '$EXECUTABLE' - leaving loop"
FOUNDWIN="$YES"
break;
elif [ -n "$GE" ] && [ "$GE" == "$(cut -d '.' -f1 < "/proc/$TESTPID/comm")" ]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for game executable 'GE' '$GE' - leaving loop"
FOUNDWIN="$YES"
break;
else
# might not even be required anymore - maybe remove later:
if [[ "$SYMCWD" == "$EFD" ]] && [[ "$SYMEXE" != *"$YAD"* ]]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for CWD '$SYMCWD' with EXE '$SYMEXE' - leaving loop"
FOUNDWIN="$YES"
break;
fi
if [ -n "$STEAM_COMPAT_CLIENT_INSTALL_PATH" ] && [[ "$SYMCWD" == "$STEAM_COMPAT_CLIENT_INSTALL_PATH" ]] && [[ "$SYMEXE" != *"$YAD"* ]]; then
writelog "WAIT" "${FUNCNAME[0]} - Found PID '$TESTPID' for STEAM_COMPAT_CLIENT_INSTALL_PATH '$SYMCWD' with EXE '$SYMEXE' - leaving loop"
FOUNDWIN="$YES"
break;
fi
fi

writelog "WAIT" "${FUNCNAME[0]} - Sec $COUNTER/$MAXWAIT Game Window with pwd '$EFD' not yet in front"
COUNTER=$((COUNTER+1))
sleep 1
done

rm "$PIDLOCK" 2>/dev/null

if [ "$FOUNDWIN" == "$YES" ]; then
if [ "$TESTPID" == "$NON" ]; then
writelog "SKIP" "${FUNCNAME[0]} - FAIL - Found PID returned but it is empty: '$TESTPID'"
fi

writelog "INFO" "${FUNCNAME[0]} - Found PID '$TESTPID' for running exe '$(readlink "/proc/$TESTPID/exe")'"
echo "$TESTPID"
elif [ "$WASCLOSED" -eq 0 ]; then
writelog "SKIP" "${FUNCNAME[0]} - ERROR - timeout waiting for '$EFD' window"
echo "$NON"
fi
}

Expand Down

0 comments on commit caace67

Please sign in to comment.