Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Updates the lwjgl that minecraft uses.
#!/usr/bin/env bash
echo "Determining OS..."
if [[ "$(uname -s)" == "Linux" ]]; then
mcdir="$HOME/.minecraft/"
downloader="wget --no-check-certificate -q -O"
os="linux"
natives="libjinput-linux libjinput-linux64 liblwjgl liblwjgl64 libopenal libopenal64"
elif [[ "$(uname -s)" == "Darwin" ]]; then
mcdir="$HOME/Library/Application\ Support/minecraft/"
downloader="curl -o"
os="macosx"
natives="libjinput-osx.jnilib liblwjgl.jnilib openal.dylib"
else
echo "OS not supported. Exploding..."
exit 1
fi
echo "Determining installed LWJGL version..."
installed="$(unzip -p $mcdir/bin/lwjgl.jar | strings | grep '^[0-9]*\.[0-9]*\.[0-9]*')"
echo "LWJGL $installed installed"
echo "Determining latest online version..."
latest=$(${downloader%%-o} - http://lwjgl.org/download.php |\
grep -oE "https?:\/\/sourceforge.net\/projects\/java-game-lib\/files\/Official%20Releases\/LWJGL%20[0-9|\.]*")
echo "Found version ${latest##*%20}"
if [[ "${latest##*%20}" == "$installed" ]]; then
echo "LWJGL already at current version."
if [[ "$1" == "-force" ]]; then
echo "Updating anyways..."
else
exit 0
fi
fi
echo "Determining download URL..."
dlurl=$(${downloader%%-o} - "$latest" |\
grep -oE -m1 "https?://sourceforge.net/projects/java-game-lib/files/Official%20Releases/LWJGL%20[0-9|\.]*/lwjgl-[0-9|\.]*.zip")
echo "Checking if ~./cache/ exists..."
if [[ ! -d "$HOME/.cache/" ]]; then
echo "~/.cache/ did not exist. Creating..."
mkdir "$HOME/.cache/"
fi
echo "Downloading latest LWJGL..."
$downloader "$HOME/.cache/lwjgl.zip" "$dlurl"
echo "Extracting zip file..."
unzip -qqo "$HOME/.cache/lwjgl.zip" -d "$HOME/.cache/"
lwjgldir=$(find -L "$HOME/.cache" -maxdepth 1 -type d -name "*lwjgl*" -print)
echo "Copying files..."
for i in "jinput" "lwjgl" "lwjgl_util"; do
echo "Copying $i..."
cp "$lwjgldir/jar/$i.jar" "$mcdir/bin/"
done
for i in $natives; do
echo "Copying $i..."
cp "$lwjgldir/native/$os/$i.so" "$mcdir/bin/natives/"
done
echo "Deleting cache..."
rm -rf "$lwjgldir"
@fndragon

This comment has been minimized.

Copy link

commented Mar 15, 2013

Change line 62 to lwjgldir=$(find -L "$HOME/.cache" -maxdepth 1 -type d -name "lwjgl" -print)

This way if your home directory has a symbolic link for .cache (I have a symbolic link there for speed), find correctly points to the lwjgl directory extracted by unzip.

@aperson

This comment has been minimized.

Copy link
Owner Author

commented Mar 16, 2013

I've updated the script accordingly. thanks!

@MestreLion

This comment has been minimized.

Copy link

commented Mar 21, 2013

It's great to see more people and scripts using XDG dirs like ~/.config, ~/.local, ~/.cache, etc to unclutter user's $HOME, so kudos for that! And, if you want to further improve that, use cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"} instead of hardcoding "$HOME/.cache". Remember all XDG dirs are configurable via env vars, and ~/.cache is just a default. Also, setting this value in a cachedir var somewhere near the top will allow you to avoid repeating it over and over through the code..

@pbhj

This comment has been minimized.

Copy link

commented Feb 5, 2014

Make sure you run it as bash, I used "sh" and on my Kubuntu that's symlinked to dash (I didn't realise). That gives a couple of errors on the [[ tests. using "bash update.sh" worked fine, thanks.

@leagris

This comment has been minimized.

Copy link

commented Feb 16, 2019

Updated your script

  • fix no longer working URLs
  • Removed grep dependency
  • Added check for unzip availability
  • Hardened code style
    TODO: check download and unzip success and exit with error message if it fail
#!/usr/bin/env bash

# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Exit on error inside any functions or subshells.
set -o errtrace
# Do not overwrite an existing file with the >, >&, and <> redirection operators.
set -o noclobber
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Pipeline's return status is the value of the last (rightmost) command
# to exit with a non-zero status, or zero if all commands exit successfully.
set -o pipefail

if ! which unzip; then
  echo >&2 "the 'unzip' utility is required!"
  exit 1
fi

echo 'Determining OS...'

declare __unames
__unames="$(uname -s)"
declare __mcdir
declare __os
declare __downloader
declare -a __natives
if [[ "${__unames}" == "Linux" ]]; then
  __mcdir="${HOME}/.minecraft/"
  __downloader='wget --no-check-certificate -q -O'
  __os='linux'
  __natives=(
    'libjinput-linux'
    'libjinput-linux64'
    'liblwjgl'
    'liblwjgl64'
    'libopenal'
    'libopenal64'
  )
elif [[ "${__unames}" == "Darwin" ]]; then
  __mcdir="${HOME}/Library/Application Support/minecraft/"
  __downloader='curl -o'
  __os='macosx'
  __natives=(
    'libjinput-osx.jnilib'
    'liblwjgl.jnilib'
    'openal.dylib'
  )
else
  echo >&2 'OS not supported.  Exploding...'
  exit 1
fi

echo 'Determining installed LWJGL version...'

[[ "$(
  unzip -p "${__mcdir}/bin/lwjgl.jar" 'org/lwjgl/Sys.class' | strings
)" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]
declare __installed="${BASH_REMATCH[0]}"

echo "LWJGL ${__installed} installed"

echo 'Determining latest online version...'

[[ "$(
  ${__downloader%%-o} - 'http://legacy.lwjgl.org/download.php.html'
)" =~ https?://sourceforge.net/projects/java-game-lib/files/Official%20Releases/LWJGL%20[0-9|\.]* ]]
declare __latest="${BASH_REMATCH[0]}"

echo "Found version ${__latest##*%20}"

if [[ "${__latest##*%20}" == "${__installed}" ]]; then
  echo >&2 'LWJGL already at current version.'
  if [[ "${1:-}" == "-force" ]]; then
    echo 'Updating anyways...'
  else
    exit 0
  fi
fi

echo 'Determining download URL...'

[[ "$(
  ${__downloader%%-o} - "${__latest}"
)" =~ https?://sourceforge.net/projects/java-game-lib/files/Official%20Releases/LWJGL%20[0-9|\.]*/lwjgl-[0-9|\.]*.zip ]]
declare __dlurl="${BASH_REMATCH[0]}"

echo "Checking if ${HOME}/.cache/ exists..."

if [[ ! -d "${HOME}/.cache/" ]]; then
  echo "${HOME}/.cache/ did not exist.  Creating..."
  mkdir "${HOME}/.cache/"
fi

echo 'Downloading latest LWJGL...'

${__downloader} "${HOME}/.cache/lwjgl.zip" "${__dlurl}"

echo 'Extracting zip file...'

unzip -qqo "${HOME}/.cache/lwjgl.zip" -d "${HOME}/.cache/"

declare __lwjgldir
__lwjgldir="$(find -L "${HOME}/.cache" -maxdepth 1 -type d -name "*lwjgl*" -print)"
echo 'Copying files...'

for i in 'jinput' 'lwjgl' 'lwjgl_util'; do
  echo "Copying ${i}..."
  cp "${__lwjgldir}/jar/${i}.jar" "${__mcdir}/bin/"
done
for i in "${__natives[@]}"; do
  echo "Copying ${i}..."
  cp "${__lwjgldir}/native/${__os}/${i}.so" "${__mcdir}/bin/natives/"
done

echo 'Deleting cache...'

find "${__lwjgldir}" -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.