summarylogtreecommitdiffstats
path: root/pokemmo-launcher
blob: b0a47af229d5bc7cb1bbfed79e91ae5b8194e8fd (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/bash
#
#  (c) Copyright 2012-2020 **PokeMMO Game Client** PokeMMO.eu <linux@pokemmo.eu>
#  (c) Copyright 2017-2020 **PokeMMO Installer** Carlos Donizete Froes [a.k.a coringao]
#
#  This file is part of PokeMMO Installer, is an program downloads and
#  installs the PokeMMO client to a user's home directory and provides
#  a launcher script for a convientient starting of the emulator.
#
#  The script main is published under GPLv3 with OpenSSL exception,
#  the full license can be found in the LICENSE file.
#
# Script name:    'pokemmo-installer.sh'
# Edited version: '1.4.8'

addScreenshotsDir() (

[[ ! -d "$XDG_CONFIG_HOME" ]] && XDG_CONFIG_HOME="$HOME/.config"
[[ -f "$XDG_CONFIG_HOME/user-dirs.dirs" ]] && source "$XDG_CONFIG_HOME/user-dirs.dirs"

if [[ -d "$XDG_PICTURES_DIR" ]]; then
	ln -s "$XDG_PICTURES_DIR" "$POKEMMO/screenshots"
else
	[[ -L "$POKEMMO/screenshots" ]] && unlink "$POKEMMO/screenshots"
	mkdir -p "$POKEMMO/screenshots"
fi

)

getLauncherConfig() {

while read i; do
    case $i in
        installed=1) PKMO_IS_INSTALLED=1 ;;
        debugs=1) PKMO_CREATE_DEBUGS=1 ;;
        swr=1) export LIBGL_ALWAYS_SOFTWARE=1 ;;
        *) continue ;;
    esac
done <"$PKMOCONFIGDIR/pokemmo"

}

showMessage() {
  if [ "$(command -v zenity)" ]; then
    case "$1" in
      --info) zenity --notification --window-icon=/usr/share/pixmaps/pokemmo-installer.png --text="$2" ; echo "INFO: $2" ;;
      --error) zenity --error --width="250" --text="$2" ; echo "ERROR: $2" ; exit 1 ;;
      --warn) zenity --warning --width="250" --text="$2" ; echo "WARNING: $2" ;;
    esac
  elif [ "$(command -v kdialog)" ]; then
    case "$1" in
      --info) kdialog --icon /usr/share/pixmaps/pokemmo-installer.png --passivepopup "$2" ; echo "INFO: $2" ;;
      --error) kdialog --error "$2" ; echo "ERROR: $2" ; exit 1 ;;
      --warn) kdialog --sorry "$2" ; echo "WARNING: $2" ;;
    esac
  else
    case "$1" in
      --info) echo "INFO: $2" ;;
      --error) echo "ERROR: $2" ; exit 1 ;;
      --warn) echo "WARNING: $2" ;;
    esac
  fi
}

failDownload() {
  rm -f "$CLIENT_ZIP"

  if [[ $PKMO_FIRST_INSTALL ]]; then
    rm -rf "$POKEMMO"

    if [[ $PKMO_CREATED_CONFIG_DIR ]]; then
      rm -rf "$PKMOCONFIGDIR"
    fi

    showMessage --error "Failed to download PokeMMO.\n\nThe incomplete first-time installation was removed. Please launch PokeMMO again to retry.\n\n$1"
  fi

  showMessage --error "Failed to download PokeMMO:\n\n$1"
}

downloadPokemmo() {

  unset CLIENT_URL CLIENT_ZIP CLIENT_SIZE DOWNLOAD_STATUS
  CLIENT_URL="https://dl.pokemmo.com/PokeMMO-Client.zip"
  CLIENT_ZIP="$POKEMMO/PokeMMO-Client.zip"

  [[ $(command -v wget) ]] || failDownload "The wget binary was not found. Please check your PATH is set correctly and restart the program."
  [[ $(command -v unzip) ]] || failDownload "The unzip binary was not found. Please check your PATH is set correctly and restart the program."

  if [[ $(command -v zenity) ]]; then
    DOWNLOAD_LOG=$(mktemp)
    DOWNLOAD_RC=$(mktemp)
    CLIENT_SIZE=$(wget --spider --server-response "$CLIENT_URL" 2>&1 | while read -r line; do
      if [[ $line =~ Content-Length:[[:space:]]*([0-9]+) ]]; then
        echo "${BASH_REMATCH[1]}"
        break
      fi
    done)

    (
      wget --https-only --retry-connrefused --tries=3 --timeout=15 --waitretry=1 -q -O "$CLIENT_ZIP" "$CLIENT_URL" > "$DOWNLOAD_LOG" 2>&1 &
      DOWNLOAD_PID=$!

      while kill -0 "$DOWNLOAD_PID" 2>/dev/null; do
        if [[ $CLIENT_SIZE =~ ^[0-9]+$ && $CLIENT_SIZE -gt 0 ]]; then
          DOWNLOADED_SIZE=$(stat -c%s "$CLIENT_ZIP" 2>/dev/null || printf 0)
          DOWNLOAD_PERCENT=$(( DOWNLOADED_SIZE * 100 / CLIENT_SIZE ))
          [[ $DOWNLOAD_PERCENT -gt 100 ]] && DOWNLOAD_PERCENT=100
          echo "# Downloading PokeMMO client... ${DOWNLOAD_PERCENT}%"
          echo "$DOWNLOAD_PERCENT"
        else
          echo "# Downloading PokeMMO client..."
        fi
        sleep 0.5
      done

      wait "$DOWNLOAD_PID"
      DOWNLOAD_EXIT=$?
      echo "$DOWNLOAD_EXIT" > "$DOWNLOAD_RC"
      if [[ $DOWNLOAD_EXIT == 0 ]]; then
        echo "# Download complete."
        echo 100
      fi
    ) | zenity --progress --title="PokeMMO" --text="Downloading PokeMMO client..." --percentage=0 --auto-close --icon=/usr/share/pixmaps/pokemmo-launcher.png --window-icon=/usr/share/pixmaps/pokemmo-launcher.png

    if [[ ! -s "$DOWNLOAD_RC" || $(<"$DOWNLOAD_RC") != 0 ]]; then
      DOWNLOAD_STATUS=$(<"$DOWNLOAD_LOG")
      rm -f "$DOWNLOAD_LOG" "$DOWNLOAD_RC"
      failDownload "$DOWNLOAD_STATUS"
    fi

    rm -f "$DOWNLOAD_LOG" "$DOWNLOAD_RC"
  else
    DOWNLOAD_STATUS=$(wget --https-only --retry-connrefused --tries=3 --timeout=15 --waitretry=1 -O "$CLIENT_ZIP" "$CLIENT_URL" 2>&1)
    [[ $? == 0 ]] || failDownload "$DOWNLOAD_STATUS"
  fi

  rm -f "$PKMOCONFIGDIR/pokemmo"
  find "$POKEMMO" -type f -name "*.TEMPORARY" -exec rm -f {} +

  (cd "$POKEMMO" && unzip -oq "$CLIENT_ZIP" -x "roms/*" "config/*" "log/*") || failDownload "Failed to extract PokeMMO."
  rm -f "$CLIENT_ZIP"
  chmod +x "$POKEMMO/PokeMMO.sh" "$POKEMMO/bin/linux/x64/PokeMMO" "$POKEMMO/bin/linux/arm64/PokeMMO" 2>/dev/null || true
  echo "installed=1" > "$PKMOCONFIGDIR/pokemmo"
  
  # Post-update tasks
  [[ ! -d "$POKEMMO/screenshots" ]] && addScreenshotsDir
}

verifyInstallation() {
if [ ! -d "$POKEMMO" ]; then
  if [[ -e "$POKEMMO" || -L "$POKEMMO" ]]; then
    # Could also be a broken symlink
    showMessage --error "(Error 3) Could not install to $POKEMMO\n\n$POKEMMO already exists,\nbut is not a directory.\n\nMove or delete this file and try again."
  else
    mkdir -p "$POKEMMO"
    PKMO_FIRST_INSTALL=1
    showMessage --info "PokeMMO is being installed to $POKEMMO"
    downloadPokemmo
    return
  fi
fi

if [[ ! -r "$POKEMMO" || ! -w "$POKEMMO" || ! -x "$POKEMMO" || ! "$PKMO_IS_INSTALLED" || ! -f "$POKEMMO/PokeMMO.sh" || ! -d "$POKEMMO/data" ]]; then
    showMessage --warn "(Error 1) The installation is in a corrupt state.\n\nReverifying the game files."
    # Try to fix permissions before erroring out
    (chmod u+rwx "$POKEMMO" && find "$POKEMMO" -type d -exec chmod u+rwx {} + && find "$POKEMMO" -type f -exec chmod u+rw {} +) || showMessage --error "(Error 4) Could not fix permissions of $POKEMMO.\n\nContact PokeMMO support."
    downloadPokemmo
    return
fi

[[ $PKMO_REINSTALL && $PKMO_IS_INSTALLED ]] && downloadPokemmo
[[ ! -d "$POKEMMO/screenshots" ]] && addScreenshotsDir
}

######################
# Environment checks #
######################

if [[ -d "$XDG_CONFIG_HOME" ]]; then
    PKMOCONFIGDIR="$XDG_CONFIG_HOME/pokemmo"
else
    if [[ ! -e "$XDG_CONFIG_HOME" && -L "$XDG_CONFIG_HOME" ]]; then
        showMessage --error "(Error 10) The configuration directory ($XDG_CONFIG_HOME/pokemmo) is disconnected.\n\nPlease update your symlink and restart the program."
    else
        PKMOCONFIGDIR="$HOME/.config/pokemmo"
    fi
fi

if [ ! -d "$PKMOCONFIGDIR" ]; then
    if [[ -e "$PKMOCONFIGDIR" || -L "$PKMOCONFIGDIR" ]]; then
        showMessage --error "(Error 9) The configuration directory ($PKMOCONFIGDIR) already exists,\nbut is not a directory.\n\nMove or delete this file and try again."
    else
        mkdir -p "$PKMOCONFIGDIR"
        PKMO_CREATED_CONFIG_DIR=1
    fi
fi

while getopts "vhH:-:" opt; do
    case $opt in
        -) case "$OPTARG" in
               reverify) PKMO_REINSTALL=1 ;;
               swr) export LIBGL_ALWAYS_SOFTWARE=1 ;;
           esac
        ;;
        v) set -x
           PS4='Line ${LINENO}: '
        ;;
        h) printf "\
 PokeMMO Linux Launcher v1.4\n\
 https://pokemmo.eu/\n\n\
 Usage: pokemmo [option...]\n\n\
 -h                     Display this dialogue\n\
 -H <dir>               Set the PokeMMO directory (Default: $HOME/.local/share/pokemmo).
                        This option is persistent and may be modified in $PKMOCONFIGDIR/pokemmodir \n\
 -v                     Print verbose status to stdout\n
 --swr                  Try to fallback to an available software renderer\n\
 --reverify             Reverify the game files\n"
           exit
        ;;
        H) mkdir -p "$OPTARG" || continue
           echo "$OPTARG" > "$PKMOCONFIGDIR/pokemmodir" ;;
        *) continue ;;
    esac
done

#################
# Start PokeMMO #
#################

[[ -f "$PKMOCONFIGDIR/pokemmo" ]] && getLauncherConfig

if [[ -f "$PKMOCONFIGDIR/pokemmodir" ]]; then
    POKEMMO=$(head -n1 "$PKMOCONFIGDIR/pokemmodir")
    [[ ! -d "$POKEMMO" ]] && showMessage --error "(Error 8) The configured directory ($POKEMMO) has become unavailable. Bailing!"
else
    if [[ -d "$XDG_DATA_HOME" ]]; then
        POKEMMO="$XDG_DATA_HOME/pokemmo"
    else
        if [[ ! -e "$XDG_DATA_HOME" && -L "$XDG_DATA_HOME" ]]; then
            showMessage --error "(Error 11) The XDG_DATA_HOME directory ($XDG_DATA_HOME/pokemmo) is disconnected.\n\nPlease update your symlink and restart the program."
        else
            POKEMMO="$HOME/.local/share/pokemmo"
        fi
    fi
fi

verifyInstallation

cd "$POKEMMO" && exec ./PokeMMO.sh "$@"