summaryrefslogtreecommitdiff
path: root/src/SMAPI.Installer/assets/unix-launcher.sh
blob: 58f7a5aee31e5ef75fb8382f5a7c1e3275e828f2 (plain)
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
#!/usr/bin/env bash

##########
## Initial setup
##########
# move to script's directory
cd "$(dirname "$0")" || exit $?


##########
## Open terminal if needed
##########
# on macOS, make sure we're running in a Terminal
# Besides letting the player see errors/warnings/alerts in the console, this is also needed because
# Steam messes with the PATH.
if [ "$(uname)" == "Darwin" ]; then
    if [ ! -t 1 ]; then # https://stackoverflow.com/q/911168/262123
        # sanity check to make sure we don't have an infinite loop of opening windows
        SKIP_TERMINAL=false
        for argument in "$@"; do
            if [ "$argument" == "--no-reopen-terminal" ]; then
                SKIP_TERMINAL=true
                break
            fi
        done

        # reopen in Terminal if needed
        # https://stackoverflow.com/a/29511052/262123
        if [ "$SKIP_TERMINAL" == "false" ]; then
            echo "Reopening in the Terminal app..."
            echo "\"$0\" $@ --no-reopen-terminal" > /tmp/open-smapi-terminal.sh
            chmod +x /tmp/open-smapi-terminal.sh
            cat /tmp/open-smapi-terminal.sh
            open -W -a Terminal /tmp/open-smapi-terminal.sh
            rm /tmp/open-smapi-terminal.sh
            exit 0
        fi
    fi
fi


##########
## Validate assumptions
##########
# script must be run from the game folder
if [ ! -f "Stardew Valley.dll" ]; then
    echo "Oops! SMAPI must be placed in the Stardew Valley game folder.\nSee instructions: https://stardewvalleywiki.com/Modding:Player_Guide";
    read
    exit 1
fi

# .NET 5 must be installed
if ! command -v dotnet >/dev/null 2>&1; then
    echo "Oops! You must have .NET 5 installed to use SMAPI: https://dotnet.microsoft.com/download";
    read
    exit 1
fi


##########
## Launch SMAPI
##########
# macOS
if [ "$(uname)" == "Darwin" ]; then
    dotnet StardewModdingAPI.dll "$@"

# Linux
else
    # choose binary file to launch
    LAUNCH_FILE="./StardewModdingAPI"
    export LAUNCH_FILE

    # select terminal (prefer xterm for best compatibility, then known supported terminals)
    for terminal in xterm gnome-terminal kitty terminator xfce4-terminal konsole terminal termite alacritty mate-terminal x-terminal-emulator; do
        if command -v "$terminal" 2>/dev/null; then
            export TERMINAL_NAME=$terminal
            break;
        fi
    done

    # find the true shell behind x-terminal-emulator
    if [ "$TERMINAL_NAME" = "x-terminal-emulator" ]; then
        export TERMINAL_NAME="$(basename "$(readlink -f $(command -v x-terminal-emulator))")"
    fi

    # run in selected terminal and account for quirks
    export TERMINAL_PATH="$(command -v $TERMINAL_NAME)"
    if [ -x $TERMINAL_PATH ]; then
        case $TERMINAL_NAME in
            terminal|termite)
                # consumes only one argument after -e
                # options containing space characters are unsupported
                exec $TERMINAL_NAME -e "env TERM=xterm $LAUNCH_FILE $@"
                ;;

            xterm|konsole|alacritty)
                # consumes all arguments after -e
                exec $TERMINAL_NAME -e env TERM=xterm $LAUNCH_FILE "$@"
                ;;

            terminator|xfce4-terminal|mate-terminal)
                # consumes all arguments after -x
                exec $TERMINAL_NAME -x env TERM=xterm $LAUNCH_FILE "$@"
                ;;

            gnome-terminal)
                # consumes all arguments after --
                exec $TERMINAL_NAME -- env TERM=xterm $LAUNCH_FILE "$@"
                ;;

            kitty)
                # consumes all trailing arguments
                exec $TERMINAL_NAME env TERM=xterm $LAUNCH_FILE "$@"
                ;;

            *)
                # If we don't know the terminal, just try to run it in the current shell.
                # If THAT fails, launch with no output.
                env TERM=xterm $LAUNCH_FILE "$@"
                if [ $? -eq 127 ]; then
                    exec $LAUNCH_FILE --no-terminal "$@"
                fi
        esac

    ## terminal isn't executable; fallback to current shell or no terminal
    else
        echo "The '$TERMINAL_NAME' terminal isn't executable. SMAPI might be running in a sandbox or the system might be misconfigured? Falling back to current shell."
        env TERM=xterm $LAUNCH_FILE "$@"
        if [ $? -eq 127 ]; then
            exec $LAUNCH_FILE --no-terminal "$@"
        fi
    fi
fi