| 1 | #! /bin/bash -e |
| 2 | |
| 3 | ##################################################### |
| 4 | # # |
| 5 | # Encode an unitypackage archive # |
| 6 | # # |
| 7 | # (c) Grame, 2017 # |
| 8 | # # |
| 9 | ##################################################### |
| 10 | |
| 11 | |
| 12 | # Structure of a unitypackage archive |
| 13 | #---------------------------------------------------------------------------------- |
| 14 | # FaustPlugin_<dspname>.unitypackage/ |
| 15 | # <md5 key of file1>/ |
| 16 | # asset (file1 renamed) |
| 17 | # pathname (file containing the address of file1 in the Unity hierarchy |
| 18 | # <md5 key of file2>/ |
| 19 | # asset (file2 renamed) |
| 20 | # pathname (file containing the address of file2 in the Unity hierarchy |
| 21 | # <md5 key of file3>/ |
| 22 | # ... |
| 23 | |
| 24 | # The Unity hierarchy will be the same as the original folder hierarchy. |
| 25 | # However, the first folder needs to be called "Assets", if not the script creates it before packing. |
| 26 | # When the package is imported, Unity retrieves the hierarchy from the pathname files |
| 27 | |
| 28 | #------------------------------------------------------------------- |
| 29 | #Dispatch command arguments |
| 30 | |
| 31 | for p in $@; do |
| 32 | if [ $p = "-help" ] || [ $p = "-h" ]; then |
| 33 | echo "encoderunitypackage <folder>" |
| 34 | echo "Builds a unity package according to the specified sub folder hierarchy." |
| 35 | echo "Handles if the folder isn't called Assets/ (adds a parent folder called Assets/)." |
| 36 | echo "If the package is called FaustPlugin_<pluginname>, change <pluginname> to the correct name." |
| 37 | echo "See architecture/unity/README.md for more info." |
| 38 | exit |
| 39 | fi |
| 40 | |
| 41 | if [ ${p:0:1} = "-" ]; then |
| 42 | OPTIONS="$OPTIONS $p" |
| 43 | elif [[ -d "$p" ]]; then |
| 44 | FOLDER="$FOLDER $p" |
| 45 | else |
| 46 | OPTIONS="$OPTIONS $p" |
| 47 | fi |
| 48 | done |
| 49 | |
| 50 | for p in $FOLDER; do |
| 51 | |
| 52 | # If the parent folder is not called "Assets", create it (to respect the Unity hierarchy). |
| 53 | if [ ! $p = "Assets" ]; then |
| 54 | mkdir Assets |
| 55 | mv $p Assets |
| 56 | p=Assets |
| 57 | fi |
| 58 | |
| 59 | # Retrieve the name of the dsp file stored in ".VAR" |
| 60 | if [ -f .VAR ]; then |
| 61 | FNAME=FaustPlugin_$(cat .VAR) |
| 62 | else |
| 63 | FNAME=FaustPlugin_\<pluginname\> |
| 64 | fi |
| 65 | |
| 66 | rm -rf .VAR |
| 67 | TMP=$(mktemp -d unity.XXXX) |
| 68 | |
| 69 | # Finds all files in $p to encode them |
| 70 | FILES=$(find $p -type f) |
| 71 | |
| 72 | for f in $FILES; do |
| 73 | FILE=$(basename $f) |
| 74 | |
| 75 | # Computes the md5 key of each file, for Darwin system the command is md5, if not the command is md5sum |
| 76 | if [[ $(uname) == Darwin ]]; then |
| 77 | DIR=$(md5 -q $f) |
| 78 | else |
| 79 | DIR=$(md5sum $f | cut -f1 -d" ") |
| 80 | fi |
| 81 | |
| 82 | # Creates the folder named with the md5 key of the file |
| 83 | if [ -d "$DIR" ]; then |
| 84 | rm -rf "$DIR" |
| 85 | fi |
| 86 | mkdir -p "$DIR" |
| 87 | mv $DIR $TMP/$DIR |
| 88 | |
| 89 | # Retrieves the file address and creates the pathname file |
| 90 | echo "$f" >> $TMP/$DIR/pathname |
| 91 | |
| 92 | # Moves and renames the file in the $DIR (md5 key folder) |
| 93 | mv "$f" "$TMP/$DIR/asset" |
| 94 | |
| 95 | done |
| 96 | |
| 97 | # Make sure all .* files have been deleted |
| 98 | FILES=$(find $p -name "\.*") |
| 99 | for f in $FILES; do |
| 100 | rm -rf $f |
| 101 | done |
| 102 | |
| 103 | # Compresses the folder and renames it FaustPlugin_<dspname>.unityackage |
| 104 | # The archive needs to be a tar gz archive to be correctly read by Unity |
| 105 | cd unity.* |
| 106 | tar zcvf $FNAME.unitypackage * &> /dev/null |
| 107 | mv "$FNAME.unitypackage" ../ |
| 108 | cd .. |
| 109 | rm -rf unity.* $p |
| 110 | |
| 111 | done |
| 112 | |
| 113 |