at main 2.3 kB view raw
1#!/usr/bin/env sh 2 3# bwrap-preset-renpy 4# 5# Run an executable in a Bubblewrap sandbox set up for Ren'Py games. The saves 6# directory will be in '$XDG_CONFIG_DIR/renpy' or '$HOME/.config/renpy' instead 7# of the normal '$HOME/.renpy'. 8 9set -eu 10 11# Set script title for error messages. 12SCRIPT_TITLE="Run in Bubblewrap (Ren'Py)" 13 14# Notify the user of an error and exit. 15notify_and_exit () { 16 # Make sure enough arguments were given. 17 if [ "$#" -ne 2 ]; then 18 printf "Error: notify_and_exit() called with '%i' arguments\n" "$#" 19 exit 127 20 fi 21 22 # Name the arguments. 23 error_msg="$1" 24 exit_code="$2" 25 26 # Either print to stdout or send a notification. 27 if [ "$TERM" = "dumb" ]; then 28 notify-send --urgency=normal --icon=error "$SCRIPT_TITLE" "$error_msg" 29 else 30 printf "%s: %s\n" "$SCRIPT_TITLE" "$error_msg" 31 fi 32 33 # Exit with the error code. 34 exit "$exit_code" 35} 36 37# Only one command can be run. 38[ "$#" -ne 1 ] && notify_and_exit "Only one command can be run with this script" 1 39 40 41# Make sure Bubblewrap is installed. 42if ! command -v bwrap > /dev/null 2>&1; then 43 notify_and_exit "Could not find executable \`bwrap\`" 1 44fi 45 46# Set the Ren'Py saves directory. 47if [ ! -z "$\{XDG_CONFIG_HOME+x\}" ]; then 48 RENPY_SAVE_DIR="$XDG_CONFIG_HOME/renpy" 49else 50 RENPY_SAVE_DIR="$HOME/.config/renpy" 51fi 52 53# Create the Ren'Py saves directory if it doesn't exist. 54[ ! -d "$RENPY_SAVE_DIR" ] && mkdir -p "$RENPY_SAVE_DIR" 55 56# Get the full path of the file. 57executable=$(realpath "$1") 58 59# Get the directory the file is contained in so it can be mounted int the 60# sandbox. 61executable_dir=$(dirname "$executable") 62 63# Make sure the file is executable. 64[ ! -x "$executable" ] && notify_and_exit "File \`$executable\` is not executable" 1 65 66# Run the specified command in Bubblewrap. Unshare all namespaces, and bind the 67# working directory and Ren'Py saves directory. 68exec bwrap \ 69 --unshare-all \ 70 --die-with-parent \ 71 --new-session \ 72 --unsetenv RENPY_PATH_TO_SAVES \ 73 --ro-bind "/" "/" \ 74 --dev "/dev" \ 75 --dev-bind "/dev/dri" "/dev/dri" \ 76 --proc "/proc" \ 77 --tmpfs "$HOME" \ 78 --bind "$XDG_CONFIG_HOME/MangoHud" "$XDG_CONFIG_HOME/MangoHud" \ 79 --bind "$executable_dir" "$executable_dir" \ 80 --bind "$RENPY_SAVE_DIR" "$HOME/.renpy" \ 81 "$executable" 82