Files
larp/linux/common.sh
Oğuz Sivrikaya 908a1ca601 fix(linux): upgrade outdated D3D shader compiler
- Upgrade the known 2013 compiler from Microsoft SDK after verifying its hash.
- Warn about the outdated DLL and document shader model 5.1 failures.
2026-12-06 01:57:39 +03:00

280 lines
7.4 KiB
Bash

# Shared helpers for larp Linux build, setup, and launch scripts.
# Sourced by linux/build.sh, linux/setup.sh, and linux/run.sh.
larp_repo_root() {
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
(cd "$script_dir/.." && pwd -P)
}
larp_abspath() {
local p="$1"
if command -v realpath >/dev/null 2>&1; then
realpath -m "$p"
elif command -v readlink >/dev/null 2>&1; then
readlink -f "$p" 2>/dev/null || printf '%s\n' "$p"
else
printf '%s\n' "$p"
fi
}
larp_is_inside_repo() {
local candidate repo
candidate="$(larp_abspath "$1")"
repo="$(larp_abspath "$2")"
[[ "$candidate" == "$repo" || "$candidate" == "$repo/"* ]]
}
larp_toolchain_channel() {
local repo_root="$1"
local file="$repo_root/rust-toolchain.toml"
local channel=""
if [[ -f "$file" ]]; then
channel="$(sed -n 's/^[[:space:]]*channel[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' "$file" | head -n 1)"
fi
printf '%s\n' "${channel:-1.98.1}"
}
larp_to_wine_path() {
local abs
abs="$(larp_abspath "$1")"
printf 'Z:%s\n' "${abs//\//\\}"
}
larp_load_config() {
local repo_root="$1"
local cfg="${LARP_LINUX_CONFIG:-${XDG_CONFIG_HOME:-$HOME/.config}/larp/linux.env}"
local -a saved_names=()
local -A saved_vals=()
local var
# Preserve caller-provided environment overrides when sourcing linux.env defaults.
while IFS= read -r var; do
[[ -n "$var" ]] || continue
if [[ -n "${!var+x}" ]]; then
saved_names+=("$var")
saved_vals["$var"]="${!var}"
fi
done < <(compgen -e LARP_ || true; printf '%s\n' WINEPREFIX PROTONPATH GAMEID)
if [[ -f "$cfg" && "$cfg" != "/dev/null" ]]; then
local allexport_was=0
[[ $- == *a* ]] && allexport_was=1
set -a
# shellcheck source=/dev/null
source "$cfg"
if [[ "$allexport_was" -eq 0 ]]; then
set +a
fi
fi
for var in "${saved_names[@]}"; do
export "$var=${saved_vals[$var]}"
done
if [[ -n "${LARP_HOME:-}" ]]; then
LARP_LINUX_DATA="${LARP_LINUX_DATA:-$LARP_HOME}"
LARP_LINUX_CACHE="${LARP_LINUX_CACHE:-$LARP_HOME/cache}"
else
LARP_LINUX_DATA="${LARP_LINUX_DATA:-${XDG_DATA_HOME:-$HOME/.local/share}/larp}"
LARP_LINUX_CACHE="${LARP_LINUX_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/larp}"
fi
if larp_is_inside_repo "$LARP_LINUX_DATA" "$repo_root"; then
printf 'error: LARP_LINUX_DATA (%s) must be outside the larp repo (%s) because of .larp-root.\n' "$LARP_LINUX_DATA" "$repo_root" >&2
return 1
fi
if larp_is_inside_repo "$LARP_LINUX_CACHE" "$repo_root"; then
printf 'error: LARP_LINUX_CACHE (%s) must be outside the larp repo (%s) because of .larp-root.\n' "$LARP_LINUX_CACHE" "$repo_root" >&2
return 1
fi
export WINEPREFIX="${WINEPREFIX:-$LARP_LINUX_DATA/pfx}"
if larp_is_inside_repo "$WINEPREFIX" "$repo_root"; then
printf 'error: WINEPREFIX (%s) must be outside the larp repo (%s) because of .larp-root.\n' "$WINEPREFIX" "$repo_root" >&2
return 1
fi
export LARP_LINUX_DATA
export LARP_LINUX_CACHE
export LARP_LINUX_DLSS_DIR="${LARP_LINUX_DLSS_DIR:-$LARP_LINUX_DATA/dlss}"
export LARP_LINUX_LOG="${LARP_LINUX_LOG:-$LARP_LINUX_DATA/play.log}"
}
larp_find_proton() {
if [[ -n "${PROTONPATH:-}" ]]; then
if [[ -f "$PROTONPATH/proton" ]]; then
printf '%s\n' "$PROTONPATH"
return 0
fi
return 1
fi
local steam_roots=(
"$HOME/.local/share/Steam"
"$HOME/.steam/root"
"$HOME/.steam/steam"
"$HOME/.var/app/com.valvesoftware.Steam/.local/share/Steam"
)
local root cand
for root in "${steam_roots[@]}"; do
[[ -d "$root" ]] || continue
cand="$root/steamapps/common/Proton - Experimental"
if [[ -f "$cand/proton" ]]; then
printf '%s\n' "$cand"
return 0
fi
done
for root in "${steam_roots[@]}"; do
[[ -d "$root" ]] || continue
while IFS= read -r cand; do
if [[ -f "$cand/proton" ]]; then
printf '%s\n' "$cand"
return 0
fi
done < <(compgen -G "$root/steamapps/common/Proton *" 2>/dev/null | sort -r)
while IFS= read -r cand; do
if [[ -f "$cand/proton" ]]; then
printf '%s\n' "$cand"
return 0
fi
done < <(compgen -G "$root/compatibilitytools.d/GE-Proton*" 2>/dev/null | sort -r)
done
return 1
}
larp_detect_gpu() {
local override="${LARP_LINUX_GPU:-auto}"
override="${override,,}"
case "$override" in
nvidia|amd)
printf '%s\n' "$override"
return 0
;;
esac
local vendor_file vendor has_nvidia=0 has_amd=0
for vendor_file in /sys/class/drm/card*/device/vendor; do
[[ -r "$vendor_file" ]] || continue
vendor="$(<"$vendor_file")"
vendor="${vendor,,}"
case "$vendor" in
0x10de) has_nvidia=1 ;;
0x1002) has_amd=1 ;;
esac
done
if [[ "$has_nvidia" -eq 1 ]]; then
printf 'nvidia\n'
elif [[ "$has_amd" -eq 1 ]]; then
printf 'amd\n'
else
printf 'unknown\n'
fi
}
larp_wine_system32_dir() {
printf '%s/pfx/drive_c/windows/system32\n' "$WINEPREFIX"
}
larp_has_native_d3dcompiler() {
local dll size
dll="$(larp_wine_system32_dir)/d3dcompiler_47.dll"
[[ -f "$dll" ]] || return 1
if command -v stat >/dev/null 2>&1; then
size="$(stat -c '%s' "$dll" 2>/dev/null || echo 0)"
else
size="$(wc -c < "$dll" 2>/dev/null || echo 0)"
fi
[[ "$size" -ge 1000000 ]]
}
larp_has_old_d3dcompiler() {
local dll
dll="$(larp_wine_system32_dir)/d3dcompiler_47.dll"
[[ -f "$dll" ]] && [[ "$(sha256sum "$dll" | cut -d ' ' -f 1)" == "e994847e01a6f1e4cbdc5a864616ac262f67ee4f14db194984661a8d927ab7f4" ]]
}
larp_find_nvidia_wine_dir() {
local dirs=(
"/run/opengl-driver/lib/nvidia/wine"
"/usr/lib/nvidia/wine"
"/usr/lib64/nvidia/wine"
"/usr/lib/x86_64-linux-gnu/nvidia/wine"
"/usr/lib/x86_64-linux-gnu/GL/default/lib/nvidia/wine"
)
local d
for d in "${dirs[@]}"; do
if [[ -d "$d" ]]; then
printf '%s\n' "$d"
return 0
fi
done
return 1
}
larp_collect_pv_paths() {
local -a raw_paths=()
local item existing target parent
if [[ -n "${PRESSURE_VESSEL_FILESYSTEMS_RW:-}" ]]; then
IFS=':' read -r -a existing <<< "$PRESSURE_VESSEL_FILESYSTEMS_RW"
raw_paths+=("${existing[@]}")
fi
for item in "$@"; do
[[ -n "$item" ]] || continue
if [[ -e "$item" ]]; then
if [[ -L "$item" ]]; then
target="$(realpath -e "$item" 2>/dev/null || true)"
if [[ -n "$target" ]]; then
parent="$target"
[[ -f "$parent" ]] && parent="$(dirname "$parent")"
raw_paths+=("$parent")
fi
fi
parent="$(larp_abspath "$item")"
[[ -f "$parent" ]] && parent="$(dirname "$parent")"
raw_paths+=("$parent")
else
raw_paths+=("$(larp_abspath "$item")")
fi
done
local -A seen=()
local -a filtered=()
local p
for p in "${raw_paths[@]}"; do
[[ -n "$p" && "$p" != *:* ]] || continue
case "$p" in
/|/home|/home/*|/tmp|/tmp/*|/usr|/usr/*|/var|/var/*|/etc|/etc/*|/run|/run/*|/dev|/dev/*|/proc|/proc/*|/sys|/sys/*|/nix|/nix/*)
continue
;;
esac
if [[ -z "${seen[$p]:-}" ]]; then
seen["$p"]=1
filtered+=("$p")
fi
done
local IFS=':'
printf '%s\n' "${filtered[*]:-}"
}
larp_reexec_in_nix() {
local repo_root="$1"
local script_path="$2"
shift 2
if [[ "${LARP_NO_NIX_AUTO:-0}" == "1" || "${LARP_IN_NIX_SHELL:-0}" == "1" ]]; then
return 1
fi
if ! command -v nix >/dev/null 2>&1; then
return 1
fi
export LARP_IN_NIX_SHELL=1
exec nix develop "path:$repo_root" -c "$script_path" "$@"
}