Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity App Size Analyzer
#!/usr/bin/env bash
function dumpBundle {
FILE=$1
OUTDIR=$2
LZMADIR=$3
OUTPATH="$OUTDIR/$FILE"
LZMAPATH="$LZMADIR/$FILE"
mkdir -p $(dirname "$OUTPATH")
cp "$TMP_FOLDER/$FILE" "$OUTPATH.unity3d"
$DISUNITY extract-raw "$OUTPATH.unity3d" > /dev/null 2>&1
CABDIR=`ls -d $OUTPATH/CAB-* 2>/dev/null`
if [ -n "$CABDIR" ]; then
mv $CABDIR/* "$OUTPATH/"
rmdir "$CABDIR"
fi
while read line; do
NAME=`echo $line | cut -d'|' -f6 | tr -d '[[:space:]]'`
if [ -n "$NAME" ]; then
ID=`echo $line | cut -d'|' -f1`
printf -v PADDED "%06d" $ID
CATEGORY=`echo $line | cut -d'|' -f3 | tr -d '[[:space:]]'`
mv "$OUTPATH/$CATEGORY/$PADDED.bin" "$OUTPATH/$CATEGORY/$NAME.bin"
fi
done < <($DISUNITY list "$OUTPATH.unity3d" 2>/dev/null | grep "^[0-9]\+")
rm "$OUTPATH.unity3d"
mkdir -p $(dirname "$LZMAPATH")
cp -r "$OUTPATH" "$LZMAPATH"
find "$LZMAPATH" -type f -name "*.bin" -print0 | xargs -0 -n32 -P8 lzma -z9
}
function explode {
FILE=$1
COMPRESSED=$2
DIR=$(dirname "$FILE")
mkdir -p "dump/compressed/$DIR"
dd if=/dev/zero of="dump/compressed/$FILE" bs=$COMPRESSED count=1 > /dev/null 2>&1
if [[ $FILE =~ /((level[0-9]+)|(mainData)|(.*\.assets)|(unity default resources)|(.*\.unity3d))$ ]]; then
dumpBundle "$FILE" dump/unbundled dump/lzma
else
mkdir -p "dump/unbundled/$DIR"
mkdir -p "dump/lzma/$DIR"
cp "$TMP_FOLDER/$FILE" "dump/unbundled/$FILE"
cp "dump/compressed/$FILE" "dump/lzma/$FILE"
fi
}
BASEDIR=$(dirname "$0")
DISUNITY="java -jar $BASEDIR/disunity/disunity.jar"
TMP_FOLDER=`mktemp -d -t 'unitySize'`
echo "Decompressing $1" 1>&2
unzip -q $1 -d $TMP_FOLDER
PLISTFILE=`find $TMP_FOLDER -type f -maxdepth 3 -name Info.plist`
EXENAME=`defaults read $PLISTFILE CFBundleExecutable`
EXEPATH=$(dirname "${PLISTFILE#$TMP_FOLDER/}")/$EXENAME
DISTSIZE=0
while read line; do
DIR=`echo $line | cut -c 1`
SIZE=`echo $line | cut -d' ' -f4`
COMPRESSED=`echo $line | cut -d' ' -f6`
FILE=`echo $line | cut -d' ' -f10-`
# Exécutable encrypté
if [ "$FILE" == "$EXEPATH" ]; then
COMPRESSED=$SIZE
fi
if [ "$DIR" != "d" ]; then
explode "$FILE" "$COMPRESSED"
fi
((DISTSIZE += COMPRESSED))
done < <(zipinfo --h-tl "$1")
echo "Expected distribution size: $DISTSIZE" 1>&2
rm -rf $TMP_FOLDER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment