From 046054a848716df16cc69cb8d0c46c36eef423f2 Mon Sep 17 00:00:00 2001 From: Natanael Date: Mon, 20 Jan 2020 23:33:37 +0000 Subject: [PATCH] Rearranges the code for better viewing No change in functionality --- data/WineLauncher | 186 +++++++++++++++++++++++++++++----------------- 1 file changed, 119 insertions(+), 67 deletions(-) diff --git a/data/WineLauncher b/data/WineLauncher index 48370b6..32b536a 100644 --- a/data/WineLauncher +++ b/data/WineLauncher @@ -1,75 +1,127 @@ -#!/bin/bash - -function cleanup() { - if [ -d "${WINEPREFIX}/drive_c" ]; then - find "${WINEPREFIX}/drive_c" -maxdepth 1 -type l | sed 's|^|unlink "|' | sed 's|$|"|' | sh - fi - if [ -f "${WINEPREFIX}/.update-timestamp" ]; then - echo "disabled" > "${WINEPREFIX}/.update-timestamp" - fi -} - -[ "${XDG_CONFIG_HOME}" = "" ] && export XDG_CONFIG_HOME="${HOME}/.config" - -export HERE="$(dirname "$(readlink -f "${0}")")" -export APP_ID=$(basename -s .desktop $(ls "${HERE}/"*".desktop")) -export APP_PATH=$(cat "${HERE}/${APP_ID}.desktop" | grep ^"X-WineExec=" | cut -c 13- | sed 's/.$//') -export WINEPREFIX="${XDG_CONFIG_HOME}/${APP_ID}" -export APP_FILES=$(find "${HERE}/app" -maxdepth 1 | sed '1d') -export LD_LIBRARY_PATH="${HERE}/usr/lib":${LD_LIBRARY_PATH} -export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu":${LD_LIBRARY_PATH} -export LD_LIBRARY_PATH="${HERE}/lib":${LD_LIBRARY_PATH} -export LD_LIBRARY_PATH="${HERE}/lib/i386-linux-gnu":${LD_LIBRARY_PATH} -export LIBGL_DRIVERS_PATH="${HERE}/usr/lib/i386-linux-gnu/dri" -export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu/pulseaudio":${LD_LIBRARY_PATH} -export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu/alsa-lib":${LD_LIBRARY_PATH} -export FONTCONFIG_PATH="${HERE}/etc/fonts" -export WINELDLIBRARY="${HERE}/lib/ld-linux.so.2" -export LD_PRELOAD="${HERE}/bin/libhookexecv.so" - -mkdir -p "${WINEPREFIX}/drive_c" +#!/usr/bin/env bash +# +# Launcher of Application +# +# Where to find libraries and stuff + function setup_environment_variables(){ + # AppDir + export HERE="$(dirname "$(readlink -f "${0}")")" + # Apps + export APP_ID=$(basename -s .desktop $(ls "${HERE}/"*".desktop")) + export APP_PATH=$(cat "${HERE}/${APP_ID}.desktop" | grep ^"X-WineExec=" \ + | cut -c 16- \ + | sed 's/.$//') + export APP_FILES=$(find "${HERE}/app" -maxdepth 1 | sed '1d') + # Link dynamic + export LD_DEBUG_OUTPUT=/dev/null + export LD_LIBRARY_PATH="${HERE}/usr/lib":"${LD_LIBRARY_PATH}" + export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu":"${LD_LIBRARY_PATH}" + export LD_LIBRARY_PATH="${HERE}/lib":"${LD_LIBRARY_PATH}" + export LD_LIBRARY_PATH="${HERE}/lib/i386-linux-gnu":"${LD_LIBRARY_PATH}" + export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu/pulseaudio":"${LD_LIBRARY_PATH}" + export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu/alsa-lib":"${LD_LIBRARY_PATH}" + # Mesa 3D + export LIBGL_DRIVERS_PATH="/usr/lib/i386-linux-gnu/:${HERE}/usr/lib/i386-linux-gnu/dri" + # Font anti aliasing + export FONTCONFIG_PATH="${HERE}/etc/fonts" + # Wine + export WINEDLLOVERRIDES="mscoree,mshtml=" # Do not ask to install Mono or Gecko + export WINEDEBUG=-all # Do not print Wine debug messages + export WINELDLIBRARY="${HERE}/lib/ld-linux.so.2" + export WINEPREFIX="${XDG_CONFIG_HOME}/${APP_ID}" + } -trap cleanup EXIT - cleanup +# Helper functions + function clear_links() { + [ -d "${WINEPREFIX}/drive_c" ] && { + find "${WINEPREFIX}/drive_c" -maxdepth 1 -type l | \ + sed 's|^|unlink "|' \ + | sed 's|$|"|' | sh -if [ "$(cat ${HERE}/copy_app_files)" == "yes" ]; then - if [ ! -f "${WINEPREFIX}/already_copied" ]; then - echo "${APP_FILES}" | grep -v "windows"$ | sed "s/^/cp -vr \"/g" | sed "s|$|\" ${WINEPREFIX}/drive_c|" | sh - touch "${WINEPREFIX}/already_copied" - fi - export APP_FILES=$(echo "${APP_FILES}" | grep "windows"$) -fi + [ -f "${WINEPREFIX}/.update-timestamp" ] && { + echo "disabled" > "${WINEPREFIX}/.update-timestamp" + } + } + } + + function create_links(){ + echo "${APP_FILES}" | sed "s|^|ln -fs \"|" | \ + sed "s|$|\" ${WINEPREFIX}/drive_c|" | sh + } + + function wine_run_regedit(){ + LD_PRELOAD="${HERE}/bin/libhookexecv.so" "${WINELDLIBRARY}" "${HERE}/bin/wineserver" --persistent=1 > grep -v ":fixme:" + LD_PRELOAD="${HERE}/bin/libhookexecv.so" "${WINELDLIBRARY}" "${HERE}/bin/wine" regedit "${WINEPREFIX}/default.reg" + } + + function monitorate_app_process(){ + INVALID_APP_STATUS=( "" "Z" ) + while true; do + [ "${APP_PID}" = "" ] && { + APP_PID=$(ps -e -o pid,cmd | $(which grep) "${HERE}/app/${APP_PATH}" \ + | $(which grep) -v "$(which grep)" \ + | sed 's/^ //' | cut -d' ' -f1) + } + APP_STATUS=$(ps -q $APP_PID -o state --no-headers) + if [[ " ${INVALID_APP_STATUS[@]} " =~ " ${APP_STATUS} " ]]; then + LD_PRELOAD="${HERE}/bin/libhookexecv.so" "${WINELDLIBRARY}" "${HERE}/bin/wineserver" -k + exit + fi + sleep 0.3 + done + } -echo "${APP_FILES}" | sed "s|^|ln -s \"|" | sed "s|$|\" ${WINEPREFIX}/drive_c|" | sh +# Main functon + function main(){ + # Ensures that prefix base directory exists + mkdir -p "${WINEPREFIX}/drive_c" + + # Checks if the application needs to run in your directory + [ "$(cat ${HERE}/change-directory)" == "yes" ] && { + cd $(dirname "${HERE}/app/${APP_PATH}") + } + + # Checks if the application needs to be in a writable directory + [ "$(cat ${HERE}/copy_app_files)" == "yes" ] && { + [ ! -f "${WINEPREFIX}/already_copied" ] && { + echo "${APP_FILES}" | grep -v "windows"$ | sed "s/^/cp -vr \"/g" | sed "s|$|\" ${WINEPREFIX}/drive_c|" | sh + touch "${WINEPREFIX}/already_copied" + } + export APP_FILES=$(echo "${APP_FILES}" | grep "windows"$) + } + + # Shortcuts + clear_links + create_links + + # Checks if registry must be created + [ ! -f "${WINEPREFIX}/system.reg" ] && { + local TODAY="$(date '+%Y%m%d')" + cp "${HERE}/improve.reg" "${WINEPREFIX}/default.reg" + sed -i "s/{Install date here}/\"InstallDate\"=\"${TODAY}\"/g" "${WINEPREFIX}/default.reg" + wine_run_regedit + } + + # Starts the application + LD_PRELOAD="${HERE}/bin/libhookexecv.so" "${WINELDLIBRARY}" \ + "${HERE}/bin/wine" \ + "${HERE}/app/${APP_PATH}" ${*} & -if [ ! -f "${WINEPREFIX}/system.reg" ]; then - export TODAY="$(date '+%Y%m%d')" - cp "${HERE}/improve.reg" "${WINEPREFIX}/default.reg" - sed -i "s/{Install date here}/\"InstallDate\"=\"${TODAY}\"/g" "${WINEPREFIX}/default.reg" - "${WINELDLIBRARY}" "${HERE}/bin/wineserver" --persistent=1 - "${HERE}/wine" create-register "${WINEPREFIX}/default.reg" -fi + # Kill the wine server after the application's end + monitorate_app_process + } -if [ "$(cat ${HERE}/change-directory)" == "yes" ]; then - cd "$(dirname "${APP_PATH}")" -fi +# Ensures that XDG_CONFIG_HOME is defined + [ -z "${XDG_CONFIG_HOME}" ] && { + export XDG_CONFIG_HOME="${HOME}/.config" + } -APP_PATH=$(echo "${HERE}/app/$(echo "${APP_PATH}" | cut -c 4-)") -"${WINELDLIBRARY}" "${HERE}/bin/wine" "${APP_PATH}" ${*} 2>&1 | grep -v ":fixme:" & +# Clears links at exit + trap clear_links EXIT -INVALID_APP_STATUS=( "" "Z" ) +# Initializes environment + setup_environment_variables + args=("$@") -# Kill wineserver after app closes -while true; do - [ "${APP_PID}" = "" ] && { - APP_PID=$(ps -e -o pid,cmd | $(which grep) "${APP_PATH}" \ - | $(which grep) -v "$(which grep)" \ - | sed 's/^ //' | cut -d' ' -f1) - } - APP_STATUS=$(ps -q $APP_PID -o state --no-headers) - if [[ " ${INVALID_APP_STATUS[@]} " =~ " ${APP_STATUS} " ]]; then - "${WINELDLIBRARY}" "${HERE}/bin/wineserver" -k - exit - fi - sleep 0.3 -done +# Call initialization processes + main