| 1 | #! /bin/bashscip-shell filesystem . . `/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 [scip-shell system . . `[`. $p = "-help" ] || [scip-shell system . . `[`. $p = "-h" ]; then |
| 33 | echoscip-shell system . . echo. "encoderunitypackage <folder>" |
| 34 | echoscip-shell system . . echo. "Builds a unity package according to the specified sub folder hierarchy." |
| 35 | echoscip-shell system . . echo. "Handles if the folder isn't called Assets/ (adds a parent folder called Assets/)." |
| 36 | echoscip-shell system . . echo. "If the package is called FaustPlugin_<pluginname>, change <pluginname> to the correct name." |
| 37 | echoscip-shell system . . echo. "See architecture/unity/README.md for more info." |
| 38 | exit |
| 39 | fi |
| 40 | |
| 41 | if [scip-shell system . . `[`. ${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 [scip-shell system . . `[`. ! $p = "Assets" ]; then |
| 54 | mkdirscip-shell system . . mkdir. Assets |
| 55 | mvscip-shell system . . mv. $p Assets |
| 56 | p=Assets |
| 57 | fi |
| 58 | |
| 59 | # Retrieve the name of the dsp file stored in ".VAR" |
| 60 | if [scip-shell system . . `[`. -f .VAR ]; then |
| 61 | FNAME=FaustPlugin_$(catscip-shell system . . cat. .VAR) |
| 62 | else |
| 63 | FNAME=FaustPlugin_\<pluginname\> |
| 64 | fi |
| 65 | |
| 66 | rmscip-shell system . . rm. -rf .VAR |
| 67 | TMP=$(mktempscip-shell system . . mktemp. -d unity.XXXX) |
| 68 | |
| 69 | # Finds all files in $p to encode them |
| 70 | FILES=$(findscip-shell system . . find. $p -type f) |
| 71 | |
| 72 | for f in $FILES; do |
| 73 | FILE=$(basenamescip-shell system . . 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=$(md5sumscip-shell system . . md5sum. $f | cutscip-shell system . . cut. -f1 -d" ") |
| 80 | fi |
| 81 | |
| 82 | # Creates the folder named with the md5 key of the file |
| 83 | if [scip-shell system . . `[`. -d "$DIR" ]; then |
| 84 | rmscip-shell system . . rm. -rf "$DIR" |
| 85 | fi |
| 86 | mkdirscip-shell system . . mkdir. -p "$DIR" |
| 87 | mvscip-shell system . . mv. $DIR $TMP/$DIR |
| 88 | |
| 89 | # Retrieves the file address and creates the pathname file |
| 90 | echoscip-shell system . . echo. "$f" >> $TMP/$DIR/pathname |
| 91 | |
| 92 | # Moves and renames the file in the $DIR (md5 key folder) |
| 93 | mvscip-shell system . . mv. "$f" "$TMP/$DIR/asset" |
| 94 | |
| 95 | done |
| 96 | |
| 97 | # Make sure all .* files have been deleted |
| 98 | FILES=$(findscip-shell system . . find. $p -name "\.*") |
| 99 | for f in $FILES; do |
| 100 | rmscip-shell system . . 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 | tarscip-shell system . . tar. zcvf $FNAME.unitypackage * &> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 107 | mvscip-shell system . . mv. "$FNAME.unitypackage" ../ |
| 108 | cd .. |
| 109 | rmscip-shell system . . rm. -rf unity.* $p |
| 110 | |
| 111 | done |
| 112 | |
| 113 |