326 lines
10 KiB
Bash
326 lines
10 KiB
Bash
|
#!/usr/bin/env bash
|
||
|
|
||
|
license() {
|
||
|
cat << EOF
|
||
|
■The contents of this file are subject to the Common Public Attribution
|
||
|
License Version 1.0 (the ■License■); you may not use this file except in
|
||
|
compliance with the License. You may obtain a copy of the License at
|
||
|
https://opensource.org/licenses/CPAL-1.0. The License is based on the Mozilla Public License Version
|
||
|
1.1 but Sections 14 and 15 have been added to cover use of software over a
|
||
|
computer network and provide for limited attribution for the Original
|
||
|
Developer. In addition, Exhibit A has been modified to be consistent with
|
||
|
Exhibit B.
|
||
|
|
||
|
Software distributed under the License is distributed on an ■AS IS■ basis,
|
||
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||
|
for the specific language governing rights and limitations under the
|
||
|
License.
|
||
|
|
||
|
The Original Code is linux-game-manager.
|
||
|
|
||
|
The Original Developer is not the Initial Developer and is . If
|
||
|
left blank, the Original Developer is the Initial Developer.
|
||
|
|
||
|
The Initial Developer of the Original Code is Billy "Storm Dragon" Wolfe. All portions of
|
||
|
the code written by Billy Wolfe are Copyright (c) 2020. All Rights
|
||
|
Reserved.
|
||
|
|
||
|
Contributor Michael Taboada.
|
||
|
|
||
|
Attribution Copyright Notice: linux-game-manager copyright 2022 Storm Dragon. All rights reserved.
|
||
|
|
||
|
Attribution Phrase (not exceeding 10 words): A Stormux project
|
||
|
|
||
|
Attribution URL: https://stormgames.wolfe.casa
|
||
|
|
||
|
Graphic Image as provided in the Covered Code, if any.
|
||
|
|
||
|
Display of Attribution Information is required in Larger
|
||
|
Works which are defined in the CPAL as a work which combines Covered Code
|
||
|
or portions thereof with code not governed by the terms of the CPAL.
|
||
|
EOF
|
||
|
}
|
||
|
|
||
|
# Dialog accessibility
|
||
|
export DIALOGOPTS='--no-lines --visit-items'
|
||
|
|
||
|
|
||
|
# Function to open urls
|
||
|
open_url() {
|
||
|
xdg-open "${*}" 2> /dev/null
|
||
|
}
|
||
|
|
||
|
# Create desktop launcher file
|
||
|
desktop_launcher() {
|
||
|
local desktopFile="${HOME}/linux-game-manager.desktop"
|
||
|
if [[ -e "${desktopFile}" ]]; then
|
||
|
echo "the file ${desktopFile} exists. Cannot create the launcher."
|
||
|
exit 1
|
||
|
fi
|
||
|
local dotDesktop
|
||
|
local terminal
|
||
|
# Try to find an accessible terminal
|
||
|
for i in mate-terminal lxterminal terminator gnome-terminal ; do
|
||
|
if command -v $i &> /dev/null ; then
|
||
|
terminal="$i"
|
||
|
break
|
||
|
fi
|
||
|
done
|
||
|
dotDesktop=('[Desktop Entry]'
|
||
|
'Name=Linux game manager'
|
||
|
'GenericName=Linux game Manager'
|
||
|
'Comment=Install and launch games that are accessible to the blind'
|
||
|
"Exec=${terminal} -t \"Linux Game Manager\" -e \"/usr/bin/bash -c 'nohup $(readlink -e "$0") 2> /dev/null'\""
|
||
|
'Terminal=false'
|
||
|
'Type=Application'
|
||
|
'StartupNotify=false'
|
||
|
'Keywords=game;'
|
||
|
'Categories=Game;'
|
||
|
'Version=1.0')
|
||
|
for i in "${dotDesktop[@]}" ; do
|
||
|
echo "$i" >> "${desktopFile}"
|
||
|
done
|
||
|
desktop-file-install --dir "${HOME}/.local/share/applications" -m 755 "${desktopFile}"
|
||
|
xdg-desktop-icon install ~/.local/share/applications/linux-game-manager.desktop
|
||
|
rm "${desktopFile}"
|
||
|
exit 0
|
||
|
}
|
||
|
|
||
|
clear_cache() {
|
||
|
local answer
|
||
|
if [[ ! -d "${cache}" ]]; then
|
||
|
echo "No cache found at ${cache}."
|
||
|
return
|
||
|
fi
|
||
|
while ! [[ "${answer,,}" =~ ^yes$|^no$ ]]; do
|
||
|
echo "This will delete all contents of ${cache}. Are you sure you want to continue?"
|
||
|
echo "Please type yes or no."
|
||
|
echo
|
||
|
read -r answer
|
||
|
done
|
||
|
if [[ "$answer" == "no" ]]; then
|
||
|
return
|
||
|
fi
|
||
|
# All safety checks done. Delete the cache.
|
||
|
rm -rfv "${cache}"
|
||
|
echo "Cache deleted."
|
||
|
}
|
||
|
|
||
|
download() {
|
||
|
local source=($@)
|
||
|
for i in "${source[@]}" ; do
|
||
|
local dest="${i##*/}"
|
||
|
dest="${dest//%20/ }"
|
||
|
# Remove the destination file if it is empty.
|
||
|
test -s "${cache}/${dest}" || rm -f "${cache}/${dest}" 2> /dev/null
|
||
|
if [[ "${redownload}" == "true" ]] && [[ -e "${cache}/${dest}" ]]; then
|
||
|
rm -v "${cache}/${dest}"
|
||
|
fi
|
||
|
# Skip if the item is in cache.
|
||
|
test -e "${cache}/${dest}" && continue
|
||
|
if ! wget -4 -O "${cache}/${dest}" "${i}" ; then
|
||
|
echo "Could not download \"$i\"..."
|
||
|
exit 1
|
||
|
fi
|
||
|
done
|
||
|
}
|
||
|
|
||
|
get_installer() {
|
||
|
trap "exit 0" SIGINT
|
||
|
# If the file is in cache nothing else needs to be done.
|
||
|
if [[ -f "${cache}/$1" ]]; then
|
||
|
return
|
||
|
fi
|
||
|
# Create message for dialog.
|
||
|
local message="Make sure $1 is available in either your Downloads or Desktop directory and press enter to continue."
|
||
|
if [[ -n "$2" ]]; then
|
||
|
message+="\n\nThe last good known URL for $game is:"
|
||
|
message+="\n$2"
|
||
|
fi
|
||
|
if echo "$2" | xclip -selection clipboard 2> /dev/null ; then
|
||
|
message+="\n\nThe URL has been copied to the clipboard."
|
||
|
fi
|
||
|
dialog --ok-label "Continue" \
|
||
|
--backtitle "Linux Game Manager" \
|
||
|
--msgbox "$message" -1 -1
|
||
|
# Search the Desktop and Downloads directories for the installation file
|
||
|
for i in ~/Downloads ~/Desktop ; do
|
||
|
find $i -type f -name "$1" -exec cp -v {} "${cache}/" \;
|
||
|
done
|
||
|
# If the file is still not available abort.
|
||
|
if [[ ! -f "${cache}/$1" ]]; then
|
||
|
echo "couldn't find $1. Please download the file and try again."
|
||
|
exit 1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
help() {
|
||
|
echo "${0##*/}"
|
||
|
echo "Released under the terms of the Common Public Attribution License Version 1.0"
|
||
|
echo -e "This is a Stormux project: https://stormux.org\n"
|
||
|
echo -e "Usage:\n"
|
||
|
echo "With no arguments, open the game launcher."
|
||
|
for i in "${!command[@]}" ; do
|
||
|
echo "-${i/:/ <parameter>}: ${command[${i}]}"
|
||
|
done | sort
|
||
|
exit 0
|
||
|
}
|
||
|
|
||
|
|
||
|
# main script
|
||
|
|
||
|
add_launcher() {
|
||
|
local launchSettings="${game}|${*}"
|
||
|
if ! grep -F -q -x "${launchSettings}" "${configFile}" 2> /dev/null ; then
|
||
|
echo "${launchSettings}" >> "${configFile}"
|
||
|
sort -o "${configFile}" "${configFile}"
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# Install games
|
||
|
game_installer() {
|
||
|
mapfile -t installedGames < <(sed '/^$/d' "${configFile}" 2> /dev/null | cut -d '|' -f3)
|
||
|
# Create the menu of installed games
|
||
|
declare -a menuList
|
||
|
for i in "${gameList[@]}" ; do
|
||
|
local menuItem="$i"
|
||
|
for j in "${installedGames[@]}" ; do
|
||
|
if [[ "$j" == "$menuItem" ]]; then
|
||
|
unset menuItem
|
||
|
fi
|
||
|
done
|
||
|
if [[ -n "$menuItem" ]]; then
|
||
|
menuList+=("$menuItem" "$menuItem")
|
||
|
fi
|
||
|
done
|
||
|
if [[ ${#menuList[@]} -eq 0 ]]; then
|
||
|
echo "All games are already installed."
|
||
|
exit 0
|
||
|
fi
|
||
|
menuList+=("Donate" "Donate")
|
||
|
game="$(dialog --backtitle "Game Installer" \
|
||
|
--clear \
|
||
|
--no-tags \
|
||
|
--menu "Please select a game to install" 0 0 0 "${menuList[@]}" --stdout)"
|
||
|
}
|
||
|
|
||
|
# launch games that are installed
|
||
|
game_launcher() {
|
||
|
mapfile -t lines < <(sed '/^$/d' "${configFile}" 2> /dev/null)
|
||
|
if [[ ${#lines} -eq 0 ]]; then
|
||
|
echo "Install some games first."
|
||
|
exit 0
|
||
|
fi
|
||
|
# Create the menu of installed games
|
||
|
declare -a menuList
|
||
|
for i in "${lines[@]}" ; do
|
||
|
menuList+=("${i#*|}" "${i%|*}")
|
||
|
done
|
||
|
menuList+=("Donate" "Donate")
|
||
|
game="$(dialog --backtitle "Linux Game Launcher" \
|
||
|
--clear \
|
||
|
--no-tags \
|
||
|
--menu "Please select a game to play" 0 0 0 "${menuList[@]}" --stdout)"
|
||
|
local menuCode=$?
|
||
|
if [[ $menuCode -eq 1 ]]; then
|
||
|
exit 0
|
||
|
fi
|
||
|
if [[ "$game" == "Donate" ]]; then
|
||
|
open_url "https://ko-fi.com/stormux"
|
||
|
exit 0
|
||
|
fi
|
||
|
exec ${game%|}
|
||
|
}
|
||
|
|
||
|
|
||
|
# If display isn't set assume we are launching from console and an X environment is running using display :0
|
||
|
if [[ -z "$DISPLAY" ]]; then
|
||
|
export DISPLAY=":0"
|
||
|
fi
|
||
|
# Settings file
|
||
|
cache="${XDG_CACHE_HOME:-$HOME/.cache}/linux-game-manager"
|
||
|
configFile="${XDG_CONFIG_HOME:-$HOME/.config}/storm-games/linux-game-manager/games.conf"
|
||
|
mkdir -p "${cache}"
|
||
|
mkdir -p "${configFile%/*}"
|
||
|
# Load any arguments from settings.conf file
|
||
|
if [[ -r "${configFile%/*}/settings.conf" ]]; then
|
||
|
source "${configFile%/*}/settings.conf"
|
||
|
fi
|
||
|
unset noCache
|
||
|
|
||
|
# Set up logging
|
||
|
echo -e "\n\n-----> Logging started at $(date '+%A, %B %d, %Y at %I:%M%p')\n" >> "${cache}/linux-game-manager.log"
|
||
|
exec &> >(/usr/bin/tee -a "${cache}/linux-game-manager.log")
|
||
|
|
||
|
# The list of games available for installation.
|
||
|
# Use menu friendly names.
|
||
|
gameList=(
|
||
|
"Fantasy Story II"
|
||
|
)
|
||
|
|
||
|
# Check for required packages
|
||
|
requiredPackages=(
|
||
|
"curl"
|
||
|
"unzip"
|
||
|
"wget"
|
||
|
)
|
||
|
for i in "${requiredPackages}" ; do
|
||
|
if ! command -v $i &> /dev/null ; then
|
||
|
echo "Please install $i before continuing."
|
||
|
exit 1
|
||
|
fi
|
||
|
done
|
||
|
# With no arguments, open the game launcher.
|
||
|
if [[ $# -eq 0 ]]; then
|
||
|
game_launcher
|
||
|
fi
|
||
|
|
||
|
# Array of command line arguments
|
||
|
declare -A command=(
|
||
|
[C]="Clear the cache. All game installers will be deleted."
|
||
|
[D]="Create desktop shortcut. You can launch Linux Game Manager from the desktop or applications menu."
|
||
|
[h]="This help screen."
|
||
|
[i]="Install games."
|
||
|
[L]="Display license information."
|
||
|
[N]="No cache, delete the installer after it has been extracted."
|
||
|
[R]="Redownload. Removes old versions of packages from cache before installing."
|
||
|
)
|
||
|
|
||
|
# Convert the keys of the associative array to a format usable by getopts
|
||
|
args="${!command[*]}"
|
||
|
args="${args//[[:space:]]/}"
|
||
|
while getopts "${args}" i ; do
|
||
|
case "$i" in
|
||
|
C) clear_cache;;
|
||
|
D) desktop_launcher;;
|
||
|
h) help;;
|
||
|
i) game_installer;;
|
||
|
L) license;;
|
||
|
N) noCache="true";;
|
||
|
R) redownload="true";;
|
||
|
esac
|
||
|
done
|
||
|
|
||
|
# Install game based on the selection above.
|
||
|
installPath="${HOME}/.local/games"
|
||
|
mkdir -p "${installPath}"
|
||
|
case "${game}" in
|
||
|
"Fantasy Story II")
|
||
|
get_installer "FS2_2.6_Linux.zip" "https://drive.google.com/file/d/187OaD1HhPoty85yJafZyvfqD5_G3P04c/view?usp=sharing"
|
||
|
pushd "${installPath}"
|
||
|
unzip "${cache}/FS2_2.6_Linux.zip"
|
||
|
popd
|
||
|
chmod +x "${installPath}/FS2_2.6_Linux/FS2.x86_64"
|
||
|
add_launcher "${installPath}/FS2_2.6_Linux/FS2.x86_64" "-ESpeakApplication=espeak-ng"
|
||
|
;;
|
||
|
"Donate")
|
||
|
open_url "https://ko-fi.com/stormux"
|
||
|
;;
|
||
|
*)
|
||
|
[[ -n "${game}" ]] && echo "Game \"${game}\" not found."
|
||
|
exit 1
|
||
|
;;
|
||
|
esac
|
||
|
|
||
|
exit 0
|