forked from Zackptg5/Audio-Modification-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-fs-data.sh
More file actions
309 lines (306 loc) · 17.2 KB
/
post-fs-data.sh
File metadata and controls
309 lines (306 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/system/bin/sh
# Please don't hardcode /magisk/modname/... ; instead, please use $MODDIR/...
# This will make your scripts compatible even if Magisk change its mount point in the future
# This script will be executed in post-fs-data mode
# More info in the main Magisk thread
#Variables
MODPATH=${0%/*}
MODDIR=$(dirname $MODPATH)
NVBASE=/data/adb
MAGISKTMP=/sbin/.magisk
REMPATCH=false
NEWPATCH=false
OREONEW=false
MODS=""
#Functions
cp_mv() {
if [ -z $4 ]; then
mkdir -p "$(dirname $3)"
cp -f "$2" "$3"
else
mkdir -p "$(dirname $3)"
cp -f "$2" "$3"
chmod $4 "$3"
fi
[ "$1" == "-m" ] && rm -f $2
return 0
}
osp_detect() {
case $1 in
*.conf) SPACES=$(sed -n "/^output_session_processing {/,/^}/ {/^ *music {/p}" $1 | sed -r "s/( *).*/\1/")
EFFECTS=$(sed -n "/^output_session_processing {/,/^}/ {/^$SPACES\music {/,/^$SPACES}/p}" $1 | grep -E "^$SPACES +[A-Za-z]+" | sed -r "s/( *.*) .*/\1/g")
for EFFECT in ${EFFECTS}; do
SPACES=$(sed -n "/^effects {/,/^}/ {/^ *$EFFECT {/p}" $1 | sed -r "s/( *).*/\1/")
[ "$EFFECT" != "atmos" -a "$EFFECT" != "dtsaudio" ] && sed -i "/^effects {/,/^}/ {/^$SPACES$EFFECT {/,/^$SPACES}/d}" $1
done;;
*.xml) EFFECTS=$(sed -n "/^ *<postprocess>$/,/^ *<\/postprocess>$/ {/^ *<stream type=\"music\">$/,/^ *<\/stream>$/ {/<stream type=\"music\">/d; /<\/stream>/d; s/<apply effect=\"//g; s/\"\/>//g; s/ *//g; p}}" $1)
for EFFECT in ${EFFECTS}; do
[ "$EFFECT" != "atmos" -a "$EFFECT" != "dtsaudio" ] && sed -i "/^\( *\)<apply effect=\"$EFFECT\"\/>/d" $1
done;;
esac
}
patch_cfgs() {
local first=true file lib=false effect=false outsp=false proxy=false replace=false libname libpath effname uid libname_sw uid_sw libname_hw uid_hw libpathsw libpathhw conf xml
local opt=`getopt :leoqpr "$@"`
eval set -- "$opt"
while true; do
case "$1" in
-l) lib=true; first=false; shift;;
-e) effect=true; first=false; shift;;
-o) outsp=true; conf=output_session_processing; xml=postprocess; first=false; shift;;
-q) outsp=true; conf=pre_processing; xml=preprocess; first=false; shift;;
-p) proxy=true; effect=false; outsp=false; first=false; shift;;
-r) replace=true; shift;;
--) shift; break;;
*) return 1;;
esac
done
case $1 in
*.conf|*.xml) case $1 in
*audio_effects*) file=$1; shift;;
*) return;;
esac;;
*) file=$MODPATH/$NAME;;
esac
$first && { lib=true; effect=true; }
if $proxy; then
effname=$1; uid=${2:?}; shift 2
libname_sw=$1; uid_sw=${2:?}; shift 2
$lib && { libpathsw=$1; shift; }
libname_hw=$1; uid_hw=${2:?}; shift 2
$lib && { libpathhw=${1:?}; shift; }
else
$outsp && { type=${1:?}; shift; }
{ $effect || $outsp; } && { effname=${1:?}; shift; }
$effect && { uid=${1:?}; shift; }
{ $lib || $effect; } && { libname=${1:?}; shift; }
$lib && { libpath=${1:?}; shift; }
fi
case "$file" in
*.conf)
if $proxy; then
if $replace && [ "$(sed -n "/^effects {/,/^}/ {/^ $effname {/,/^ }/p}" $file)" ]; then
SPACES=$(sed -n "/^effects {/,/^}/ {/^ *$effname {/p}" $file | sed -r "s/( *).*/\1/")
sed -i "/^effects {/,/^}/ {/^$SPACES$effname {/,/^$SPACES}/d}" $file
fi
[ ! "$(sed -n "/^effects {/,/^}/ {/^ $effname {/,/^ }/p}" $file)" ] && sed -i "s/^effects {/effects {\n $effname {\n library proxy\n uuid $uid\n\n libsw {\n library $libname_sw\n uuid $uid_sw\n }\n\n libhw {\n library $libname_hw\n uuid $uid_hw\n }\n }/g" $file
if $lib; then
patch_cfgs -l "$file" "proxy" "$LIBDIR/libeffectproxy.so"
if $replace; then
patch_cfgs -rl "$file" "$libname_sw" "$libpathsw"
patch_cfgs -rl "$file" "$libname_hw" "$libpathhw"
else
patch_cfgs -l "$file" "$libname_sw" "$libpathsw"
patch_cfgs -l "$file" "$libname_hw" "$libpathhw"
fi
fi
return
fi
if $lib; then
if $replace && [ "$(sed -n "/^libraries {/,/^}/ {/^ *$libname {/,/}/p}" $file)" ]; then
SPACES=$(sed -n "/^libraries {/,/^}/ {/^ *$libname {/p}" $file | sed -r "s/( *).*/\1/")
sed -i "/^libraries {/,/^}/ {/^$SPACES$libname {/,/^$SPACES}/d}" $file
fi
[ ! "$(sed -n "/^libraries {/,/^}/ {/^ *$libname {/,/}/p}" $file)" ] && sed -i "s|^libraries {|libraries {\n $libname {\n path $libpath\n }|" $file
fi
if $effect; then
if $replace && [ "$(sed -n "/^effects {/,/^}/ {/^ *$effname {/,/}/p}" $file)" ]; then
SPACES=$(sed -n "/^effects {/,/^}/ {/^ *$effname {/p}" $file | sed -r "s/( *).*/\1/")
sed -i "/^effects {/,/^}/ {/^$SPACES$effname {/,/^$SPACES}/d}" $file
fi
[ ! "$(sed -n "/^effects {/,/^}/ {/^ *$effname {/,/}/p}" $file)" ] && sed -i "s|^effects {|effects {\n $effname {\n library $libname\n uuid $uid\n }|" $file
fi
if $outsp && [ "$API" -ge 26 ]; then
local OIFS=$IFS; local IFS=','
for i in $type; do
if [ ! "$(sed -n "/^$conf {/,/^}/p" $file)" ]; then
echo -e "\n$conf {\n $i {\n $effname {\n }\n }\n}" >> $file
elif [ ! "$(sed -n "/^$conf {/,/^}/ {/$i {/,/^ }/p}" $file)" ]; then
sed -i "/^$conf {/,/^}/ s/$conf {/$conf {\n $i {\n $effname {\n }\n }/" $file
elif [ ! "$(sed -n "/^$conf {/,/^}/ {/$i {/,/^ }/ {/$effname {/,/}/p}}" $file)" ]; then
sed -i "/^$conf {/,/^}/ {/$i {/,/^ }/ s/$i {/$i {\n $effname {\n }/}" $file
fi
done
local IFS=$OIFS
fi;;
*.xml)
if $proxy; then
if $replace && [ "$(sed -n "/<effects>/,/<\/effects>/ {/^ *<effectProxy name=\"$effname\".*>/,/^ *<\/effectProxy>/p}" $file)" -o "$(sed -n "/<effects>/,/<\/effects>/ {/^ *<effect name=\"$effname\".*\/>/p}" $file)" ]; then
sed -i "/<effects>/,/<\/effects>/ {/^ *<effectProxy name=\"$effname\".*>/,/^ *<\/effectProxy>/d}" $file
sed -i "/<effects>/,/<\/effects>/ {/^ *<effect name=\"$effname\".*\/>/d}" $file
fi
[ ! "$(sed -n "/<effects>/,/<\/effects>/ {/^ *<effectProxy name=\"$effname\".*>/,/^ *<\/effectProxy>/p}" $file)" -a ! "$(sed -n "/<effects>/,/<\/effects>/ {/^ *<effect name=\"$effname\".*>/,/^ *\/>/p}" $file)" ] && sed -i -e "/<effects>/ a\ <effectProxy name=\"$effname\" library=\"proxy\" uuid=\"$uid\">\n <libsw library=\"$libname_sw\" uuid=\"$uid_sw\"\/>\n <libhw library=\"$libname_hw\" uuid=\"$uid_hw\"\/>\n <\/effectProxy>" $file
if $lib; then
patch_cfgs -l "$file" "proxy" "$LIBDIR/libeffectproxy.so"
if $replace; then
patch_cfgs -rl "$file" "$libname_sw" "$libpathsw"
patch_cfgs -rl "$file" "$libname_hw" "$libpathhw"
else
patch_cfgs -l "$file" "$libname_sw" "$libpathsw"
patch_cfgs -l "$file" "$libname_hw" "$libpathhw"
fi
fi
return
fi
if $lib; then
if $replace && [ "$(sed -n "/<libraries>/,/<\/libraries>/ {/^ *<library name=\"$libname\" path=\"$(basename $libpath)\"\/>/p}" $file)" ]; then
sed -i "/<libraries>/,/<\/libraries>/ {/^ *<library name=\"$libname\" path=\"$(basename $libpath)\"\/>/d}" $file
fi
[ ! "$(sed -n "/<libraries>/,/<\/libraries>/ {/^ *<library name=\"$libname\" path=\"$(basename $libpath)\"\/>/p}" $file)" ] && sed -i "/<libraries>/ a\ <library name=\"$libname\" path=\"$(basename $libpath)\"\/>" $file
fi
if $effect; then
if $replace && [ "$(sed -n "/<effects>/,/<\/effects>/ {/^ *<effect name=\"$effname\".*\/>/p}" $file)" -o "$(sed -n "/<effects>/,/<\/effects>/ {/^ *<effectProxy name=\"$effname\".*>/,/^ *<\/effectProxy>/p}" $file)" ]; then
sed -i "/<effects>/,/<\/effects>/ {/^ *<effect name=\"$effname\".*\/>/d}" $file
sed -i "/<effects>/,/<\/effects>/ {/^ *<effectProxy name=\"$effname\".*>/,/^ *<\/effectProxy>/d}" $file
fi
[ ! "$(sed -n "/<effects>/,/<\/effects>/ {/^ *<effect name=\"$effname\".*\/>/p}" $file)" ] && sed -i "/<effects>/ a\ <effect name=\"$effname\" library=\"$(basename $libname)\" uuid=\"$uid\"\/>" $file
fi
if $outsp && [ "$API" -ge 26 ]; then
local OIFS=$IFS; local IFS=','
for i in $type; do
if [ ! "$(sed -n "/^ *<$xml>/,/^ *<\/$xml>/p" $file)" ]; then
sed -i "/<\/audio_effects_conf>/i\ <$xml>\n <stream type=\"$type\">\n <apply effect=\"$effname\"\/>\n <\/stream>\n <\/$xml>" $file
elif [ ! "$(sed -n "/^ *<$xml>/,/^ *<\/$xml>/ {/<stream type=\"$type\">/,/<\/stream>/p}" $file)" ]; then
sed -i "/^ *<$xml>/,/^ *<\/$xml>/ s/ <$xml>/ <$xml>\n <stream type=\"$type\">\n <apply effect=\"$effname\"\/>\n <\/stream>/" $file
elif [ ! "$(sed -n "/^ *<$xml>/,/^ *<\/$xml>/ {/<stream type=\"$type\">/,/<\/stream>/ {/^ *<apply effect=\"$effname\"\/>/p}}" $file)" ]; then
sed -i "/^ *<$xml>/,/^ *<\/$xml>/ {/<stream type=\"$type\">/,/<\/stream>/ s/<stream type=\"$type\">/<stream type=\"$type\">\n <apply effect=\"$effname\"\/>/}" $file
fi
done
local IFS=$OIFS
fi;;
esac
}
grep_prop() {
REGEX="s/^$1=//p"
shift
FILES=$@
[ -z "$FILES" ] && FILES='/system/build.prop'
sed -n "$REGEX" $FILES 2>/dev/null | head -n 1
}
main() {
DIR=$1
LAST=false; NUM=1
#Some loop shenanigans so it'll run once or twice depending on supplied DIR
until $LAST; do
[ "$1" == "$MODDIR/*/system" -o $NUM -ne 1 ] && LAST=true
[ $NUM -ne 1 ] && DIR=$MODDIR/*/system
for MOD in $(find $DIR -maxdepth 0 -type d); do
RUNONCE=false
$LAST && [ "$MOD" == "$MODPATH/system" -o -f "$(dirname $MOD)/disable" ] && continue
FILES=$(find $MOD -type f -name "*audio_effects*.conf" -o -name "*audio_effects*.xml" -o -name "*audio_*policy*.conf" -o -name "*audio_*policy*.xml" -o -name "*mixer_paths*.xml" -o -name "*mixer_gains*.xml" -o -name "*audio_device*.xml" -o -name "*sapa_feature*.xml" -o -name "*audio_platform_info*.xml")
[ -z "$FILES" ] && continue
MODNAME=$(basename $(dirname $MOD))
$LAST && [ ! "$(grep "$MODNAME" $NVBASE/aml/mods/modlist)" ] && echo "$MODNAME" >> $NVBASE/aml/mods/modlist
COUNT=1
[ "$MODNAME" == "ainur_sauron" ] && LIBDIR="$(dirname $(find $MOD -type f -name "libbundlewrapper.so" | head -n 1) | sed -e "s|$MOD|/system|" -e "s|/system/vendor|/vendor|" -e "s|/lib64|/lib|")"
if [ -f "$(dirname $MOD)/.aml.sh" ]; then
case $(sed -n 1p $(dirname $MOD)/.aml.sh) in
\#*~*.sh) cp_mv -c $(dirname $MOD)/.aml.sh $MODPATH/.scripts/$(sed -n 1p $(dirname $MOD)/.aml.sh | sed -r "s|#(.*)|\1|")
[ "$(sed -n "/RUNONCE=true/p" $MODPATH/mods/$(sed -n 1p $(dirname $MOD)/.aml.sh | sed -r "s|#(.*)|\1|"))" ] && . $MODPATH/.scripts/$(sed -n 1p $(dirname $MOD)/.aml.sh | sed -r "s|#(.*)|\1|");;
*) cp_mv -c $(dirname $MOD)/.aml.sh $MODPATH/.scripts/$MODNAME.sh
[ "$(sed -n "/RUNONCE=true/p" $MODPATH/mods/$MODNAME.sh)" ] && . $MODPATH/.scripts/$(sed -n 1p $(dirname $MOD)/.aml.sh | sed -r "s|#(.*)|\1|");;
esac
fi
for FILE in ${FILES}; do
NAME=$(echo "$FILE" | sed "s|$MOD|system|")
$RUNONCE || case $FILE in
*audio_effects*.conf) for AUDMOD in $(ls $MODPATH/.scripts); do
if [ "$AUDMOD" == "$MODNAME.sh" ]; then
. $MODPATH/.scripts/$AUDMOD
COUNT=$(($COUNT + 1))
break
else
LIB=$(echo "$AUDMOD" | sed -r "s|(.*)~.*.sh|\1|")
UUID=$(echo "$AUDMOD" | sed -r "s|.*~(.*).sh|\1|")
if [ "$(sed -n "/^libraries {/,/^}/ {/$LIB.so/p}" $FILE)" ] && [ "$(sed -n "/^effects {/,/^}/ {/uuid $UUID/p}" $FILE)" ] && [ "$(find $MODDIR/$MODNAME/system -type f -name "$LIB.so")" ]; then
LIBDIR="$(dirname $(find $MODDIR/$MODNAME/system -type f -name "$LIB.so" | head -n 1) | sed -e "s|$MODDIR/$MODNAME||" -e "s|/system/vendor|/vendor|" -e "s|/lib64|/lib|")"
. $MODPATH/.scripts/$AUDMOD
COUNT=$(($COUNT + 1))
break
fi
fi
done;;
*audio_effects*.xml) for AUDMOD in $(ls $MODPATH/.scripts); do
if [ "$AUDMOD" == "$MODNAME.sh" ]; then
. $MODPATH/.scripts/$AUDMOD
COUNT=$(($COUNT + 1))
break
else
LIB=$(echo "$AUDMOD" | sed -r "s|(.*)~.*.sh|\1|")
UUID=$(echo "$AUDMOD" | sed -r "s|.*~(.*).sh|\1|")
if [ "$(sed -n "/<libraries>/,/<\/libraries>/ {/path=\"$LIB.so\"/p}" $FILE)" ] && [ "$(sed -n "/<effects>/,/<\/effects>/ {/uuid=\"$UUID\"/p}" $FILE)" ] && [ "$(find $MOD -type f -name "$LIB.so")" ]; then
LIBDIR="$(dirname $(find $MOD -type f -name "$LIB.so" | head -n 1) | sed -e "s|$MOD|/system|" -e "s|/system/vendor|/vendor|" -e "s|/lib64|/lib|")"
. $MODPATH/.scripts/$AUDMOD
COUNT=$(($COUNT + 1))
break
fi
fi
done;;
esac
$LAST && cp_mv -m $FILE $NVBASE/aml/mods/$MODNAME/$NAME
done
if $LAST && [ -f $(dirname $MOD)/system.prop ]; then
sed -i "/^$/d" $(dirname $MOD)/system.prop
[ "$(tail -1 $(dirname $MOD)/system.prop)" ] && echo "" >> $(dirname $MOD)/system.prop
while read PROP; do
[ ! "$PROP" ] && break
TPROP=$(echo "$PROP" | sed -r "s/(.*)=.*/\1/")
if [ ! "$(grep "$TPROP" $MODPATH/system.prop)" ]; then
echo "$PROP" >> $MODPATH/system.prop
elif [ "$(grep "^$TPROP" $MODPATH/system.prop)" ] && [ ! "$(grep "^$PROP" $MODPATH/system.prop)" ]; then
sed -i "s|^$TPROP|^#$TPROP|" $MODPATH/system.prop
echo "#$PROP" >> $MODPATH/system.prop
fi
done < $(dirname $MOD)/system.prop
cp_mv -m $(dirname $MOD)/system.prop $NVBASE/aml/mods/$MODNAME/system.prop
fi
done
if $LAST; then
[ -s $MODPATH/system.prop ] || rm -f $MODPATH/system.prop
for FILE in $MODPATH/*.sh $MODPATH/*.prop; do
[ "$(tail -1 $FILE)" ] && echo "" >> $FILE
done
fi
NUM=$((NUM+1))
done
}
#Script logic
#Determine if an audio mod was removed
while read LINE; do
if [ ! -d $MODDIR/$LINE ]; then
export MODS="${MODS} $LINE"; REMPATCH=true
elif [ -f "$MODDIR/$LINE/disable" ]; then
for FILE in $(find $NVBASE/aml/mods/$LINE -type f); do
NAME=$(echo "$FILE" | sed "s|$NVBASE/aml/mods/||")
cp_mv -m $FILE $MODDIR/$NAME
done
export MODS="${MODS} $LINE"; REMPATCH=true
fi
done < $NVBASE/aml/mods/modlist
#Determine if an audio mod has been added/changed
DIR=$(find $MODDIR/* -type d -maxdepth 0 | sed -e "s|$MODDIR/lost\+found ||g" -e "s|$MODDIR/aml ||g")
[ "$(find $DIR -type f -name "*audio_effects*.conf" -o -name "*audio_effects*.xml" -o -name "*audio_*policy*.conf" -o -name "*audio_*policy*.xml" -o -name "*mixer_paths*.xml" -o -name "*mixer_gains*.xml" -o -name "*audio_device*.xml" -o -name "*sapa_feature*.xml" -o -name "*audio_platform_info*.xml" | head -n 1)" ] && NEWPATCH=true
#Main method
if $REMPATCH; then
if [ -f $MODPATH/system.prop ]; then > $MODPATH/system.prop; else touch $MODPATH/system.prop; fi
for MODNAME in ${MODS}; do
rm -rf $NVBASE/aml/mods/$MODNAME
sed -i "/$MODNAME/d" $NVBASE/aml/mods/modlist
done
if [ -d $MAGISKTMP/mirror/system_root ]; then
FILES="$(find $MAGISKTMP/mirror/system_root/system -type f -name "*audio_effects*.conf" -o -name "*audio_effects*.xml" -o -name "*audio_*policy*.conf" -o -name "*audio_*policy*.xml" -o -name "*mixer_paths*.xml" -o -name "*mixer_gains*.xml" -o -name "*audio_device*.xml" -o -name "*sapa_feature*.xml" -o -name "*audio_platform_info*.xml")"
[ -L /system/vendor ] && FILES="$FILES $(find $MAGISKTMP/mirror/vendor -type f -name "*audio_effects*.conf" -o -name "*audio_effects*.xml" -o -name "*audio_*policy*.conf" -o -name "*audio_*policy*.xml" -o -name "*mixer_paths*.xml" -o -name "*mixer_gains*.xml" -o -name "*audio_device*.xml" -o -name "*sapa_feature*.xml" -o -name "*audio_platform_info*.xml")"
else
FILES="$(find $MAGISKTMP/mirror/system $MAGISKTMP/mirror/vendor -type f -name "*audio_effects*.conf" -o -name "*audio_effects*.xml" -o -name "*audio_*policy*.conf" -o -name "*audio_*policy*.xml" -o -name "*mixer_paths*.xml" -o -name "*mixer_gains*.xml" -o -name "*audio_device*.xml" -o -name "*sapa_feature*.xml" -o -name "*audio_platform_info*.xml")"
fi
for FILE in ${FILES}; do
NAME=$(echo "$FILE" | sed -e "s|$MAGISKTMP/mirror||" -e "s|/system/||")
cp_mv -c $FILE $MODPATH/system/$NAME
done
for FILE in $(find $MODPATH/system -type f -name "*audio_effects*.conf" -o -name "*audio_effects*.xml"); do
osp_detect $FILE
done
main "$NVBASE/aml/mods/*/system"
elif $NEWPATCH; then
main "$MODDIR/*/system"
fi