Files
mandy/scripts/play-linux.sh

134 lines
6.3 KiB
Bash
Executable File

#!/bin/sh
# Starts Leonida's D3D12 runtime (d3d12/, a Windows program) on Linux under Wine, with the VKD3D-Proton that comes in
# the package (scripts/fetch_vkd3d_proton.py puts it in vkd3d-proton/ beside this script), so nothing else needs to be
# downloaded for Direct3D 12. Wine itself and a Vulkan driver (Mesa or NVIDIA's) are the system's.
#
# ./play-linux.sh /path/to/your/game_scarlett_master.exe [--series-x]
#
# Leonida keeps its own Wine prefix (LEONIDA_WINEPREFIX, default ~/.local/share/leonida/prefix): the bundled
# d3d12.dll and d3d12core.dll are installed there and preferred over Wine's (WINEDLLOVERRIDES), so no other prefix
# is changed. LEONIDA_* variables already set win over the defaults below.
set -eu
usage() {
echo "usage: $0 /path/to/game_scarlett_master.exe [--series-x]"
echo
echo "LEONIDA_GAME_DIR, LEONIDA_CACHE_DIR and LEONIDA_DATA_DIR place the title's files, the caches and the saves;"
echo "LEONIDA_RUNTIME, LEONIDA_VKD3D and LEONIDA_WINEPREFIX choose the runtime, the bundled VKD3D-Proton and"
echo "the Wine prefix."
}
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
usage
exit 0
fi
if [ $# -lt 1 ]; then
usage >&2
exit 2
fi
game_exe=$1
shift
console=series-s
if [ "${1:-}" = "--series-x" ]; then console=series-x; fi
[ -f "$game_exe" ] || { echo "No $game_exe" >&2; exit 1; }
here=$(cd "$(dirname "$0")" && pwd)
runtime=${LEONIDA_RUNTIME:-$here/leonida.exe}
vkd3d=${LEONIDA_VKD3D:-$here/vkd3d-proton}
[ -f "$runtime" ] || { echo "No $runtime (the Windows build of d3d12/)" >&2; exit 1; }
for dll in d3d12.dll d3d12core.dll; do
[ -f "$vkd3d/$dll" ] || { echo "No $vkd3d/$dll: run scripts/fetch_vkd3d_proton.py when packaging" >&2; exit 1; }
done
command -v wine >/dev/null 2>&1 || { echo "Wine is needed (your distribution's wine package, or Proton's)" >&2; exit 1; }
# The title's graphics memory is write-watched (MEM_WRITE_WATCH). Wine 10.7 and newer and Proton's Wine track those
# writes in the kernel (userfaultfd and PAGEMAP_SCAN, Linux 6.7 or newer); older Wine, or an older kernel, takes a
# signal at the first write to every watched page after each reset: about 3 to 5 times the cost of the kernel's
# path measured natively (docs/linux.md, "Which Wine"). Said once, never fatal; LEONIDA_WINE_CHECK=0 leaves it out.
# Prints "kernel", "signals" or "unknown" for the Wine at $1 whose `wine --version` is $2, on kernel release $3. An
# ntdll.so found beside that Wine decides: the kernel path's code names PAGEMAP_SCAN in its messages (upstream and
# Proton alike). Without one the version does: 10.7 is the first release with that code.
write_watch_kind() {
wine_bin=$(readlink -f "$1" 2>/dev/null || echo "$1")
wine_root=$(dirname "$(dirname "$wine_bin")")
ntdll=
for candidate in "$wine_root"/lib/wine/x86_64-unix/ntdll.so "$wine_root"/lib64/wine/x86_64-unix/ntdll.so \
"$wine_root"/lib/x86_64-linux-gnu/wine/x86_64-unix/ntdll.so; do
if [ -f "$candidate" ]; then ntdll=$candidate; break; fi
done
wine_major=$(echo "$2" | sed -n 's/^wine-\([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\1/p')
wine_minor=$(echo "$2" | sed -n 's/^wine-\([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\2/p')
kernel_major=$(echo "$3" | sed -n 's/^\([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\1/p')
kernel_minor=$(echo "$3" | sed -n 's/^\([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\2/p')
if [ -n "$ntdll" ]; then
if grep -q -a PAGEMAP_SCAN "$ntdll"; then wine_has=yes; else wine_has=no; fi
elif [ -z "$wine_major" ]; then
wine_has=unknown
elif [ "$wine_major" -gt 10 ] || { [ "$wine_major" -eq 10 ] && [ "$wine_minor" -ge 7 ]; }; then
wine_has=yes
else
wine_has=no
fi
if [ "$wine_has" = no ]; then
echo signals
elif [ "$wine_has" = unknown ] || [ -z "$kernel_major" ]; then
echo unknown
elif [ "$kernel_major" -gt 6 ] || { [ "$kernel_major" -eq 6 ] && [ "$kernel_minor" -ge 7 ]; }; then
echo kernel
else
echo signals
fi
}
if [ "${LEONIDA_WINE_CHECK:-1}" != 0 ]; then
wine_version=$(wine --version 2>/dev/null || true)
if [ "$(write_watch_kind "$(command -v wine)" "$wine_version" "$(uname -r)")" = signals ]; then
echo "Leonida: this Wine (${wine_version:-unknown}) on Linux $(uname -r) watches the title's memory with a" \
"signal per page; Wine 10.7+ or Proton on Linux 6.7+ does it in the kernel, faster (docs/linux.md," \
"\"Which Wine\")" >&2
fi
fi
data=${XDG_DATA_HOME:-$HOME/.local/share}/leonida
export WINEPREFIX=${LEONIDA_WINEPREFIX:-$data/prefix}
export WINEARCH=win64
if [ ! -d "$WINEPREFIX/drive_c/windows/system32" ]; then
echo "Leonida: making its Wine prefix in $WINEPREFIX"
mkdir -p "$WINEPREFIX"
WINEDEBUG=-all wineboot -u >/dev/null 2>&1
fi
# The bundled VKD3D-Proton, copied in when it is missing or differs (a new package).
system32=$WINEPREFIX/drive_c/windows/system32
for dll in d3d12.dll d3d12core.dll; do
if ! cmp -s "$vkd3d/$dll" "$system32/$dll"; then cp "$vkd3d/$dll" "$system32/$dll"; fi
done
export WINEDLLOVERRIDES="d3d12,d3d12core=n${WINEDLLOVERRIDES:+;$WINEDLLOVERRIDES}"
# Where the title's files are, and where the caches and saves go.
export LEONIDA_GAME_DIR=${LEONIDA_GAME_DIR:-$(cd "$(dirname "$game_exe")" && pwd)}
export LEONIDA_CACHE_DIR=${LEONIDA_CACHE_DIR:-$data/cache}
export LEONIDA_DATA_DIR=${LEONIDA_DATA_DIR:-$data/data}
mkdir -p "$LEONIDA_CACHE_DIR" "$LEONIDA_DATA_DIR"
# The settings scripts/play-nvidia.ps1 measured (d3d12/README.md), as defaults.
set_default() { eval "[ -n \"\${$1:-}\" ] || export $1=\"$2\""; }
set_default LEONIDA_TEXTURE_NO_WAIT 1
set_default LEONIDA_RELAY 2
set_default LEONIDA_CARD_STEPS 2
set_default LEONIDA_DYNAMIC_SLOTS 524288
set_default LEONIDA_PIPELINE_WAIT_MS 10000
set_default LEONIDA_RENDER_SCALE 0.5
set_default LEONIDA_OWN_UPSCALER 1
set_default LEONIDA_BVH_MISS 1
set_default LEONIDA_CARD_BUFFERS_MIN_MB 1
set_default LEONIDA_CARD_BUFFERS_MB 1536
set_default LEONIDA_COMPOSE 1
set_default LEONIDA_NAN_SCRUB 1
set_default LEONIDA_AMBIENT_AIR 1.5
set_default LEONIDA_AMBIENT_MOST 2
# Match the Windows launcher defaults; explicit 0 keeps either path off for comparisons.
set_default LEONIDA_COPY_SCOPED 1
set_default LEONIDA_PORT_DESCRIPTORS 1
echo "Leonida: $console under Wine with the bundled VKD3D-Proton ($(head -n 1 "$vkd3d/SOURCE.txt" 2>/dev/null || echo bundled))"
exec wine "$runtime" run "$game_exe" --execute --console "$console"