Skip to content

Instantly share code, notes, and snippets.

@rizaumami
Created August 23, 2024 09:07
Show Gist options
  • Save rizaumami/c1121d66ebcee10cf70da11a414a91cd to your computer and use it in GitHub Desktop.
Save rizaumami/c1121d66ebcee10cf70da11a414a91cd to your computer and use it in GitHub Desktop.
Bash script to build UIX Lite's Icons.xbx.
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Checking dependencies:
declare -a DEPS=(wget xxd)
for ((NUM=${#DEPS[@]},i=0; i<NUM;i++)); do
if command -v "${DEPS[i]}" &>/dev/null ; then
unset -v 'DEPS[i]'
fi
done
# Exit if dependency not found.
if [[ "${#DEPS[@]}" -gt 0 ]]; then
printf '\e[31m!! %s\n\e[m' 'Package(s) not found'
printf '\e[33m:: %s\n\e[m' 'Install the proper distribution package for your system:'
printf ' - %s\n' "${DEPS[@]}"
exit 1
fi
# VARIABLES --------------------------------------------------------------------
# Get the current working directory.
HERE=$(dirname "$(readlink -f "${0}")")
# Use script's directory when output directory not specified
OUT="$HERE"
# FTP password
PASS='xbox'
# Temporary directory for downloaded xbes
TMPDIR=$(mktemp -d)
# FTP username
USER='xbox'
# default.xbes directory
XBEDIR="$TMPDIR"
# FUNCTIONS --------------------------------------------------------------------
print_usage() {
printf '%s\n' "
${0##*/} is a Bash script to build UIX Lite's Icons.xbx.
Usage: ${0##*/} OPTION
OPTION:
-a Xbox's IP address.
If only ogxbox's IP given, then the script will download default.xbes
from all of the partitions.
Specific directory is recommended for large disk, e.g.
192.168.0.1/F/Games instead of just 192.168.0.1.
-i Local directory contains downloaded default.xbes.
Use this to not redownloading the xbes from ogxbox.
When -a and -i are provided, -i will be prioritized.
-o Output Icons.xbx.
Default to script's working directory.
-p FTP password.
Default to xbox.
-u FTP username.
Default to xbox.
"
exit
}
# MAIN -------------------------------------------------------------------------
# Cleanup after an unexpected event.
trap 'rm -r "$TMPDIR"; exit' SIGINT ERR
# If this script run without argument.
[[ ${#@} -eq 0 ]] && print_usage
while getopts ":a:hi:o:p:u:" opt; do
case $opt in
a)
ADDR="${OPTARG:-}"
;;
h)
print_usage
;;
i)
LDIR="${OPTARG:-}"
;;
o)
OUT="$OPTARG"
;;
p)
PASS="$OPTARG"
;;
u)
USER="$OPTARG"
;;
\?)
print_w "Wrong option: -$OPTARG" >&2
exit 1
;;
:)
print_w "Option -$OPTARG need an argument." >&2
exit 1
;;
esac
done
# Cannot use two input. Prioritize local input.
if [[ -n ${LDIR:-} ]]; then
if [[ -d $LDIR ]]; then
XBEDIR="$LDIR"
else
echo "$LDIR: No such file or directory"
exit
fi
else
echo "Downloading default.xbes from ${ADDR}, please wait...."
wget -q -P "$TMPDIR" -r -A 'default.xbe' ftp://"${USER}":"${PASS}"@"${ADDR}"
fi
mapfile -t XBES < <(find "$XBEDIR" -type f -iname "default.xbe" 2>/dev/null)
declare -A XBX=()
for ((NUM=${#XBES[@]},i=0; i<NUM;i++)); do
# Get game's directory name
GAMEDIR="${XBES[$i]%/*}"
GAMEDIR="${GAMEDIR##*/}"
# Get default.xbe base address.
BASE="$(xxd -e -l 4 -s 0x104 "${XBES[$i]}")"
BASE="0x${BASE:10:8}"
# Get certificate address.
CERT="$(xxd -e -l 4 -s 0x118 "${XBES[$i]}")"
CERT="0x${CERT:10:8}"
# Reverse Title ID.
RTID="$(xxd -e -u -l 8 -s "$(((CERT-BASE)+8))" "${XBES[$i]}")"
RTID="${RTID:10:8}"
# Save to the array.
XBX[$i]="${GAMEDIR}=${RTID}"
done
mkdir -p "$OUT"
printf '%s\n' "${XBX[@]}" | sort > "${OUT}/Icons.xbx"
echo 'Done:'
echo "${OUT}/Icons.xbx"
rm -r "$TMPDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment