From 076d1f2b62515f299fc8df42ea43bccf247d09cf Mon Sep 17 00:00:00 2001 From: Deve Date: Tue, 27 Jun 2017 21:20:38 +0200 Subject: [PATCH] Save few more mbytes in the apk --- android/generate_assets.sh | 188 ++++++++++++++++++++++++++++++++++--- 1 file changed, 177 insertions(+), 11 deletions(-) diff --git a/android/generate_assets.sh b/android/generate_assets.sh index 441107df5..6f4fde08a 100755 --- a/android/generate_assets.sh +++ b/android/generate_assets.sh @@ -5,13 +5,15 @@ # A script that generates data files for Android apk -# Below are simple configuration variables +# Below you can find some simple configuration variables. # It's allowed to set "all" for KARTS and TRACKS if it's intended to create # package with full data. # The karts and tracks directories shouldn't exist in ASSETS_DIRS variable -# because they are handled separately +# because they are handled separately. # The TEXTURE_SIZE and SOUND_QUALITY take effect only if DECREASE_QUALITY has -# value greater than 0 +# value greater than 0. +# The CONVERT_TO_JPG variable enables converting all images that are safe to +# convert and keeps other images untouched. # The script needs imagemagick and ogg utils installed to use DECREASE_QUALITY # feature @@ -37,6 +39,7 @@ export SOUND_SAMPLE=32000 export RUN_OPTIMIZE_SCRIPT=0 export DECREASE_QUALITY=1 +export CONVERT_TO_JPG=0 export BLACKLIST_FILES="data/music/cocoa_river_fast.ogg2" @@ -180,10 +183,10 @@ convert_image() SCALED_W=$(($TEXTURE_SIZE * $W / $H)) SCALED_H=$TEXTURE_SIZE fi - + SCALE_CMD="-scale ${SCALED_W}x${SCALED_H}" fi - + if [ "$FILE_TYPE" = "jpg" ]; then QUALITY_CMD="-quality $JPEG_QUALITY" elif [ "$FILE_TYPE" = "png" ]; then @@ -191,7 +194,7 @@ convert_image() fi convert $SCALE_CMD $QUALITY_CMD "$FILE" "tmp.$FILE_TYPE" - + if [ -s "tmp.$FILE_TYPE" ]; then SIZE_OLD=`du -k "$FILE" | cut -f1` SIZE_NEW=`du -k "tmp.$FILE_TYPE" | cut -f1` @@ -217,20 +220,20 @@ convert_sound() if [ -s tmp.wav ]; then OGGENC_CMD="" - + if [ "$SOUND_MONO" -gt 0 ]; then OGGENC_CMD="$OGGENC_CMD --downmix" fi - + OGG_RATE=`ogginfo "$FILE" | grep "Rate: " | cut -f 2 -d " " \ | grep -o '[0-9]*'` - + if [ ! -z "$OGG_RATE" ] && [ "$OGG_RATE" -gt "$SOUND_SAMPLE" ]; then OGGENC_CMD="$OGGENC_CMD --resample $SOUND_SAMPLE" fi - + OGGENC_CMD="$OGGENC_CMD -b $SOUND_QUALITY" - + oggenc $OGGENC_CMD tmp.wav -o tmp.ogg fi @@ -246,6 +249,158 @@ convert_sound() rm -f tmp.wav tmp.ogg } +convert_b3d() +{ + if [ -z "$1" ]; then + echo "No file to convert" + return + fi + + FILE="$1" + echo "Convert file: $FILE" + + if [ ! -f "$FILE" ]; then + echo " File doesn't exist." + return + fi + + HEX_FILE=`hexdump -ve '1/1 "%.2x"' "$FILE"` + + TEXS_CHUNK="54455853" + TEXS_CHUNK_POS=24 + + FOUND_CHUNK=`echo $HEX_FILE | head -c $(($TEXS_CHUNK_POS + 8)) \ + | tail -c +$(($TEXS_CHUNK_POS + 1))` + + if [ -z "$FOUND_CHUNK" ] || [ "$FOUND_CHUNK" != "$TEXS_CHUNK" ]; then + echo " Unsupported format." + return + fi + + TEXS_SIZE=`echo $HEX_FILE | head -c $(($TEXS_CHUNK_POS + 16)) | tail -c 8` + + TEXS_SIZE_CONVERTED=`echo $TEXS_SIZE | cut -c7-8` + TEXS_SIZE_CONVERTED=$TEXS_SIZE_CONVERTED`echo $TEXS_SIZE | cut -c5-6` + TEXS_SIZE_CONVERTED=$TEXS_SIZE_CONVERTED`echo $TEXS_SIZE | cut -c3-4` + TEXS_SIZE_CONVERTED=$TEXS_SIZE_CONVERTED`echo $TEXS_SIZE | cut -c1-2` + TEXS_SIZE_CONVERTED=`echo $((0x$TEXS_SIZE_CONVERTED))` + + if [ $TEXS_SIZE_CONVERTED -le 0 ]; then + echo " Invalid TEXS size value." + return + fi + + TEXS_BEGIN=$(($TEXS_CHUNK_POS + 16)) + TEXS_END=$(($TEXS_BEGIN + $TEXS_SIZE_CONVERTED * 2)) + HEX_TEXS=`echo $HEX_FILE | head -c $TEXS_END | tail -c +$(($TEXS_BEGIN+1))` + CURR_POS=0 + + cp "$FILE" tmp.b3d + + while [ $CURR_POS -lt $TEXS_END ]; do + NULL_POS=`echo $HEX_TEXS | tail -c +$(($CURR_POS+1)) | grep -b -o "00" \ + | head -n 1 | cut -f1 -d":"` + + if [ -z $NULL_POS ]; then + echo " Done." + break + fi + + if [ $NULL_POS -lt 4 ]; then + echo " Something went wrong..." + break + fi + + TEXNAME_BEGIN=$((($TEXS_BEGIN + $CURR_POS) / 2)) + TEXNAME_END=$((($TEXS_BEGIN + $CURR_POS + $NULL_POS) / 2)) + CURR_POS=$(($CURR_POS + $NULL_POS + 58)) + + TEXTURE_NAME=`dd if="$FILE" bs=1 skip=$TEXNAME_BEGIN \ + count=$(($TEXNAME_END - $TEXNAME_BEGIN)) 2> /dev/null` + DIRNAME=`dirname "$FILE"` + TEXTURE_PATH="$DIRNAME/$TEXTURE_NAME" + + echo " Texture: $TEXTURE_NAME" + + ALREADY_CONVERTED=0 + + if [ -s "./converted_textures" ]; then + while read -r CONVERTED_TEXTURE; do + if [ "$TEXTURE_PATH" = "$CONVERTED_TEXTURE" ]; then + ALREADY_CONVERTED=1 + break + fi + done < "./converted_textures" + fi + + if [ $ALREADY_CONVERTED -eq 0 ]; then + if [ ! -f "$TEXTURE_PATH" ]; then + echo " Couldn't find texture file. Ignore..." + continue + fi + + FILE_EXTENSION=`echo "$TEXTURE_PATH" | tail -c 5` + + if [ `echo "$FILE_EXTENSION" | head -c 1` != "." ]; then + echo " Unsupported file extension. Ignore..." + continue + fi + + FILE_FORMAT=`identify -format %m "$TEXTURE_PATH"` + + if [ "$FILE_FORMAT" = "JPEG" ]; then + echo " File is already JPEG. Ignore..." + continue + fi + + #IS_OPAQUE=`identify -format '%[opaque]' "$TEXTURE_PATH"` + HAS_ALPHA=`identify -format '%A' "$TEXTURE_PATH"` + + if [ "$HAS_ALPHA" = "True" ] || [ "$HAS_ALPHA" = "true" ]; then + echo " File has alpha channel. Ignore..." + continue + fi + + NEW_TEXTURE_NAME="`echo $TEXTURE_NAME | head -c -5`.jpg" + NEW_TEXTURE_PATH="`echo $TEXTURE_PATH | head -c -5`.jpg" + + if [ -f "$NEW_TEXTURE_PATH" ]; then + echo " There is already a file with .jpg extension. Ignore..." + continue + fi + + convert -quality $JPEG_QUALITY "$TEXTURE_PATH" "$NEW_TEXTURE_PATH" + rm -f "$TEXTURE_PATH" + + if [ -s "$DIRNAME/materials.xml" ]; then + sed -i "s/name=\"$TEXTURE_NAME\"/name=\"$NEW_TEXTURE_NAME\"/g" \ + "$DIRNAME/materials.xml" + fi + + if [ -s "$DIRNAME/scene.xml" ]; then + sed -i "s/name=\"$TEXTURE_NAME\"/name=\"$NEW_TEXTURE_NAME\"/g" \ + "$DIRNAME/scene.xml" + fi + + echo "$TEXTURE_PATH" >> "./converted_textures" + fi + + echo -n ".jpg" | dd of=./tmp.b3d bs=1 seek=$(($TEXNAME_END - 4)) \ + conv=notrunc 2> /dev/null + done + + SIZE_OLD=`du -b "$FILE" | cut -f1` + SIZE_NEW=`du -b "tmp.b3d" | cut -f1` + + if [ $SIZE_NEW -ne $SIZE_OLD ]; then + echo " Something went wrong..." + exit + fi + + mv tmp.b3d "$FILE" +} + + if [ $DECREASE_QUALITY -gt 0 ]; then find assets/data -iname "*.png" | while read f; do convert_image "$f" "png"; done find assets/data -iname "*.jpg" | while read f; do convert_image "$f" "jpg"; done @@ -253,6 +408,17 @@ if [ $DECREASE_QUALITY -gt 0 ]; then fi +if [ $CONVERT_TO_JPG -gt 0 ]; then + find assets/data -iname "*.b3d" | while read f; do convert_b3d "$f"; done + + if [ -s "./converted_textures" ]; then + echo "Converted textures:" + cat "./converted_textures" + rm -f "./converted_textures" + fi +fi + + # Copy data directory echo "Copy data directory" cp -a ../data/* assets/data/