forked from j-hc/revanced-magisk-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sh
executable file
·617 lines (586 loc) · 21.3 KB
/
utils.sh
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/usr/bin/env bash
MODULE_TEMPLATE_DIR="revanced-magisk"
CWD=$(pwd)
TEMP_DIR="temp"
BIN_DIR="bin"
BUILD_DIR="build"
if [ "${GITHUB_TOKEN-}" ]; then GH_HEADER="Authorization: token ${GITHUB_TOKEN}"; else GH_HEADER=; fi
NEXT_VER_CODE=${NEXT_VER_CODE:-$(date +'%Y%m%d')}
OS=$(uname -o)
toml_prep() {
if [ ! -f "$1" ]; then return 1; fi
if [ "${1##*.}" == toml ]; then
__TOML__=$($TOML --output json --file "$1" .)
elif [ "${1##*.}" == json ]; then
__TOML__=$(cat "$1")
else abort "config extension not supported"; fi
}
toml_get_table_names() { jq -r -e 'to_entries[] | select(.value | type == "object") | .key' <<<"$__TOML__"; }
toml_get_table_main() { jq -r -e 'to_entries | map(select(.value | type != "object")) | from_entries' <<<"$__TOML__"; }
toml_get_table() { jq -r -e ".\"${1}\"" <<<"$__TOML__"; }
toml_get() {
local op
op=$(jq -r ".\"${2}\" | values" <<<"$1")
if [ "$op" ]; then
op="${op#"${op%%[![:space:]]*}"}"
op="${op%"${op##*[![:space:]]}"}"
op=${op//"'"/'"'}
echo "$op"
else return 1; fi
}
pr() { echo -e "\033[0;32m[+] ${1}\033[0m"; }
epr() {
echo >&2 -e "\033[0;31m[-] ${1}\033[0m"
if [ "${GITHUB_REPOSITORY-}" ]; then echo -e "::error::utils.sh [-] ${1}\n"; fi
}
abort() {
epr "ABORT: ${1-}"
exit 1
}
get_rv_prebuilts() {
local cli_src=$1 cli_ver=$2 patches_src=$3 patches_ver=$4
pr "Getting prebuilts (${patches_src%/*})" >&2
local cl_dir=${patches_src%/*}
cl_dir=${TEMP_DIR}/${cl_dir,,}-rv
[ -d "$cl_dir" ] || mkdir "$cl_dir"
for src_ver in "$cli_src CLI $cli_ver revanced-cli" "$patches_src Patches $patches_ver patches"; do
set -- $src_ver
local src=$1 tag=$2 ver=${3-} fprefix=$4
local ext
if [ "$tag" = "CLI" ]; then
ext="jar"
elif [ "$tag" = "Patches" ]; then
ext="rvp"
else abort unreachable; fi
local dir=${src%/*}
dir=${TEMP_DIR}/${dir,,}-rv
[ -d "$dir" ] || mkdir "$dir"
local rv_rel="https://api.github.com/repos/${src}/releases" name_ver
if [ "$ver" = "dev" ]; then
name_ver="*-dev*"
elif [ "$ver" = "latest" ]; then
rv_rel+="/latest"
name_ver="*"
else
rv_rel+="/tags/${ver}"
name_ver="$ver"
fi
local url file tag_name name
file=$(find "$dir" -name "${fprefix}-${name_ver#v}.${ext}" -type f 2>/dev/null)
if [ -z "$file" ]; then
local resp asset name
resp=$(gh_req "$rv_rel" -) || return 1
if [ "$ver" = "dev" ]; then resp=$(jq -r '.[0]' <<<"$resp"); fi
tag_name=$(jq -r '.tag_name' <<<"$resp")
asset=$(jq -e -r ".assets[] | select(.name | endswith(\"$ext\"))" <<<"$resp") || return 1
url=$(jq -r .url <<<"$asset")
name=$(jq -r .name <<<"$asset")
file="${dir}/${name}"
gh_dl "$file" "$url" >&2 || return 1
echo "$tag: $(cut -d/ -f1 <<<"$src")/${name} " >>"${cl_dir}/changelog.md"
else
local for_err=$file
if [ "$ver" = "latest" ]; then
file=$(grep -v '/[^/]*dev[^/]*$' <<<"$file" | head -1)
else file=$(grep "/[^/]*${ver#v}[^/]*\$" <<<"$file" | head -1); fi
if [ -z "$file" ]; then abort "filter fail: '$for_err' with '$ver'"; fi
name=$(basename "$file")
tag_name=$(cut -d'-' -f3- <<<"$name")
tag_name=v${tag_name%.*}
fi
if [ "$tag" = "Patches" ]; then
if [ ! -f "$file" ]; then echo -e "[Changelog](https://github.com/${src}/releases/tag/${tag_name})\n" >>"${cl_dir}/changelog.md"; fi
if [ "$REMOVE_RV_INTEGRATIONS_CHECKS" = true ]; then
if ! (
mkdir -p "${file}-zip" || return 1
unzip -qo "${file}" -d "${file}-zip" || return 1
java -cp "${BIN_DIR}/paccer.jar:${BIN_DIR}/dexlib2.jar" com.jhc.Main "${file}-zip/extensions/shared.rve" "${file}-zip/extensions/shared-patched.rve" || return 1
mv -f "${file}-zip/extensions/shared-patched.rve" "${file}-zip/extensions/shared.rve" || return 1
rm "${file}" || return 1
cd "${file}-zip" || abort
zip -0rq "${CWD}/${file}" . || return 1
) >&2; then
echo >&2 "Patching revanced-integrations failed"
fi
rm -r "${file}-zip" || :
fi
fi
echo -n "$file "
done
echo
}
set_prebuilts() {
APKSIGNER="${BIN_DIR}/apksigner.jar"
if [ "$OS" = Android ]; then
local arch
if [ "$(uname -m)" = aarch64 ]; then arch=arm64; else arch=arm; fi
HTMLQ="${BIN_DIR}/htmlq/htmlq-${arch}"
AAPT2="${BIN_DIR}/aapt2/aapt2-${arch}"
TOML="${BIN_DIR}/toml/tq-${arch}"
else
HTMLQ="${BIN_DIR}/htmlq/htmlq-x86_64"
TOML="${BIN_DIR}/toml/tq-x86_64"
fi
}
config_update() {
if [ ! -f build.md ]; then abort "build.md not available"; fi
declare -A sources
: >"$TEMP_DIR"/skipped
local upped=()
local prcfg=false
for table_name in $(toml_get_table_names); do
if [ -z "$table_name" ]; then continue; fi
t=$(toml_get_table "$table_name")
enabled=$(toml_get "$t" enabled) || enabled=true
if [ "$enabled" = false ]; then continue; fi
PATCHES_SRC=$(toml_get "$t" patches-source) || PATCHES_SRC=$DEF_PATCHES_SRC
PATCHES_VER=$(toml_get "$t" patches-version) || PATCHES_VER=$DEF_PATCHES_VER
if [[ -v sources["$PATCHES_SRC/$PATCHES_VER"] ]]; then
if [ "${sources["$PATCHES_SRC/$PATCHES_VER"]}" = 1 ]; then upped+=("$table_name"); fi
else
sources["$PATCHES_SRC/$PATCHES_VER"]=0
local rv_rel="https://api.github.com/repos/${PATCHES_SRC}/releases"
if [ "$PATCHES_VER" = "dev" ]; then
last_patches=$(gh_req "$rv_rel" - | jq -e -r '.[0]')
elif [ "$PATCHES_VER" = "latest" ]; then
last_patches=$(gh_req "$rv_rel/latest" -)
else
last_patches=$(gh_req "$rv_rel/tags/${ver}" -)
fi
if ! last_patches=$(jq -e -r '.assets[] | select(.name | endswith("rvp")) | .name' <<<"$last_patches"); then
abort oops
fi
if [ "$last_patches" ]; then
if ! OP=$(grep "^Patches: ${PATCHES_SRC%%/*}/" build.md | grep "$last_patches"); then
sources["$PATCHES_SRC/$PATCHES_VER"]=1
prcfg=true
upped+=("$table_name")
else
echo "$OP" >>"$TEMP_DIR"/skipped
fi
fi
fi
done
if [ "$prcfg" = true ]; then
local query=""
for table in "${upped[@]}"; do
if [ -n "$query" ]; then query+=" or "; fi
query+=".key == \"$table\""
done
jq "to_entries | map(select(${query} or (.value | type != \"object\"))) | from_entries" <<<"$__TOML__"
fi
}
_req() {
local ip="$1" op="$2"
shift 2
if [ "$op" = - ]; then
wget -nv -O "$op" "$@" "$ip"
else
if [ -f "$op" ]; then return; fi
local dlp
dlp="$(dirname "$op")/tmp.$(basename "$op")"
if [ -f "$dlp" ]; then
while [ -f "$dlp" ]; do sleep 1; done
return
fi
wget -nv -O "$dlp" "$@" "$ip" || return 1
mv -f "$dlp" "$op"
fi
}
req() { _req "$1" "$2" --header="User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0"; }
gh_req() { _req "$1" "$2" --header="$GH_HEADER"; }
gh_dl() {
if [ ! -f "$1" ]; then
pr "Getting '$1' from '$2'"
_req "$2" "$1" --header="$GH_HEADER" --header="Accept: application/octet-stream"
fi
}
log() { echo -e "$1 " >>"build.md"; }
get_highest_ver() {
local vers m
vers=$(tee)
m=$(head -1 <<<"$vers")
if ! semver_validate "$m"; then echo "$m"; else sort -rV <<<"$vers" | head -1; fi
}
semver_validate() {
local a="${1%-*}"
local ac="${a//[.0-9]/}"
[ ${#ac} = 0 ]
}
get_patch_last_supported_ver() {
local pkg_name=$1 inc_sel=$2 _exc_sel=$3 _exclusive=$4 # TODO: resolve using all of these
local op
if [ "$inc_sel" ]; then
if ! op=$(java -jar "$rv_cli_jar" list-patches "$rv_patches_jar" -f "$pkg_name" -v -p 2>&1 | awk '{$1=$1}1'); then
epr "list-patches: '$op'"
return 1
fi
local ver vers="" NL=$'\n'
while IFS= read -r line; do
line="${line:1:${#line}-2}"
ver=$(sed -n "/^Name: $line\$/,/^\$/p" <<<"$op" | sed -n "/^Compatible versions:\$/,/^\$/p" | tail -n +2)
vers=${ver}${NL}
done <<<"$(list_args "$inc_sel")"
vers=$(awk '{$1=$1}1' <<<"$vers")
if [ "$vers" ]; then
get_highest_ver <<<"$vers"
return
fi
fi
if ! op=$(java -jar "$rv_cli_jar" list-versions "$rv_patches_jar" -f "$pkg_name" 2>&1 | tail -n +3 | awk '{$1=$1}1'); then
epr "list-versions: '$op'"
return 1
fi
if [ "$op" = "Any" ]; then return; fi
pcount=$(head -1 <<<"$op") pcount=${pcount#*(} pcount=${pcount% *}
if [ -z "$pcount" ]; then abort "unreachable: '$pcount'"; fi
grep -F "($pcount patch" <<<"$op" | sed 's/ (.* patch.*//' | get_highest_ver || return 1
}
isoneof() {
local i=$1 v
shift
for v; do [ "$v" = "$i" ] && return 0; done
return 1
}
merge_splits() {
local bundle=$1 output=$2
pr "Merging splits"
gh_dl "$TEMP_DIR/apkeditor.jar" "https://github.com/REAndroid/APKEditor/releases/download/V1.3.9/APKEditor-1.3.9.jar" >/dev/null || return 1
if ! OP=$(java -jar "$TEMP_DIR/apkeditor.jar" merge -i "${bundle}" -o "${bundle}.mzip" -clean-meta -f 2>&1); then
epr "$OP"
return 1
fi
# this is required because of apksig
mkdir "${bundle}-zip"
unzip -qo "${bundle}.mzip" -d "${bundle}-zip"
pushd "${bundle}-zip" || abort
zip -0rq "${CWD}/${bundle}.zip" .
popd || abort
# if building module, sign the merged apk properly
if isoneof "module" "${build_mode_arr[@]}"; then
patch_apk "${bundle}.zip" "${output}" "--exclusive" "${args[cli]}" "${args[ptjar]}"
local ret=$?
else
cp "${bundle}.zip" "${output}"
local ret=$?
fi
rm -r "${bundle}-zip" "${bundle}.zip" "${bundle}.mzip" || :
return $ret
}
# -------------------- apkmirror --------------------
apk_mirror_search() {
local resp="$1" dpi="$2" arch="$3" apk_bundle="$4"
local apparch dlurl node app_table
if [ "$arch" = all ]; then
apparch=(universal noarch 'arm64-v8a + armeabi-v7a')
else apparch=("$arch" universal noarch 'arm64-v8a + armeabi-v7a'); fi
for ((n = 1; n < 40; n++)); do
node=$($HTMLQ "div.table-row.headerFont:nth-last-child($n)" -r "span:nth-child(n+3)" <<<"$resp")
if [ -z "$node" ]; then break; fi
app_table=$($HTMLQ --text --ignore-whitespace <<<"$node")
if [ "$(sed -n 3p <<<"$app_table")" = "$apk_bundle" ] && [ "$(sed -n 6p <<<"$app_table")" = "$dpi" ] &&
isoneof "$(sed -n 4p <<<"$app_table")" "${apparch[@]}"; then
dlurl=$($HTMLQ --base https://www.apkmirror.com --attribute href "div:nth-child(1) > a:nth-child(1)" <<<"$node")
echo "$dlurl"
return 0
fi
done
return 1
}
dl_apkmirror() {
local url=$1 version=${2// /-} output=$3 arch=$4 dpi=$5 is_bundle=false
if [ -f "${output}.apkm" ]; then
is_bundle=true
else
if [ "$arch" = "arm-v7a" ]; then arch="armeabi-v7a"; fi
local resp node app_table dlurl=""
url="${url}/${url##*/}-${version//./-}-release/"
resp=$(req "$url" -) || return 1
node=$($HTMLQ "div.table-row.headerFont:nth-last-child(1)" -r "span:nth-child(n+3)" <<<"$resp")
if [ "$node" ]; then
if ! dlurl=$(apk_mirror_search "$resp" "$dpi" "${arch}" "APK"); then
if ! dlurl=$(apk_mirror_search "$resp" "$dpi" "${arch}" "BUNDLE"); then
return 1
else is_bundle=true; fi
fi
[ -z "$dlurl" ] && return 1
resp=$(req "$dlurl" -)
fi
url=$(echo "$resp" | $HTMLQ --base https://www.apkmirror.com --attribute href "a.btn") || return 1
url=$(req "$url" - | $HTMLQ --base https://www.apkmirror.com --attribute href "span > a[rel = nofollow]") || return 1
fi
if [ "$is_bundle" = true ]; then
req "$url" "${output}.apkm"
merge_splits "${output}.apkm" "${output}"
else
req "$url" "${output}"
fi
}
get_apkmirror_vers() {
local vers apkm_resp
apkm_resp=$(req "https://www.apkmirror.com/uploads/?appcategory=${__APKMIRROR_CAT__}" -)
vers=$(sed -n 's;.*Version:</span><span class="infoSlide-value">\(.*\) </span>.*;\1;p' <<<"$apkm_resp" | awk '{$1=$1}1')
if [ "$__AAV__" = false ]; then
local IFS=$'\n'
vers=$(grep -iv "\(beta\|alpha\)" <<<"$vers")
local v r_vers=()
for v in $vers; do
grep -iq "${v} \(beta\|alpha\)" <<<"$apkm_resp" || r_vers+=("$v")
done
echo "${r_vers[*]}"
else
echo "$vers"
fi
}
get_apkmirror_pkg_name() { sed -n 's;.*id=\(.*\)" class="accent_color.*;\1;p' <<<"$__APKMIRROR_RESP__"; }
get_apkmirror_resp() {
__APKMIRROR_RESP__=$(req "${1}" -)
__APKMIRROR_CAT__="${1##*/}"
}
# -------------------- uptodown --------------------
get_uptodown_resp() {
__UPTODOWN_RESP__=$(req "${1}/versions" -)
__UPTODOWN_RESP_PKG__=$(req "${1}/download" -)
}
get_uptodown_vers() { $HTMLQ --text ".version" <<<"$__UPTODOWN_RESP__"; }
dl_uptodown() {
local uptodown_dlurl=$1 version=$2 output=$3 arch=$4 _dpi=$5
if [ "$arch" = "arm-v7a" ]; then arch="armeabi-v7a"; fi
local op resp data_code
data_code=$($HTMLQ "#detail-app-name" --attribute data-code <<<"$__UPTODOWN_RESP__")
local versionURL=""
for i in {1..5}; do
resp=$(req "${uptodown_dlurl}/apps/${data_code}/versions/${i}" -)
if ! op=$(jq -e -r ".data | map(select(.version == \"${version}\" and .kindFile == \"apk\")) | .[0]" <<<"$resp"); then
continue
fi
if versionURL=$(jq -e -r '.versionURL' <<<"$op"); then break; else return 1; fi
done
if [ -z "$versionURL" ]; then return 1; fi
resp=$(req "$versionURL" -) || return 1
if [ "$arch" != all ]; then
local data_version files node_arch data_file_id
data_version=$($HTMLQ '.button.variants' --attribute data-version <<<"$resp") || return 1
files=$(req "${uptodown_dlurl%/*}/app/${data_code}/version/${data_version}/files" - | jq -e -r .content) || return 1
for ((n = 1; n < 12; n += 2)); do
node_arch=$($HTMLQ ".content > p:nth-child($n)" --text <<<"$files" | xargs) || return 1
if [ -z "$node_arch" ]; then return 1; fi
if [ "$node_arch" != "$arch" ]; then continue; fi
data_file_id=$($HTMLQ "div.variant:nth-child($((n + 1))) > .v-report" --attribute data-file-id <<<"$files") || return 1
resp=$(req "${uptodown_dlurl}/download/${data_file_id}" -)
break
done
fi
local data_url
data_url=$($HTMLQ "#detail-download-button" --attribute data-url <<<"$resp") || return 1
req "https://dw.uptodown.com/dwn/${data_url}" "$output"
}
get_uptodown_pkg_name() { $HTMLQ --text "tr.full:nth-child(1) > td:nth-child(3)" <<<"$__UPTODOWN_RESP_PKG__"; }
# -------------------- archive --------------------
dl_archive() {
local url=$1 version=$2 output=$3 arch=$4
local path version=${version// /}
path=$(grep "${version_f#v}-${arch// /}" <<<"$__ARCHIVE_RESP__") || return 1
req "${url}/${path}" "$output"
}
get_archive_resp() {
local r
r=$(req "$1" -)
if [ -z "$r" ]; then return 1; else __ARCHIVE_RESP__=$(sed -n 's;^<a href="\(.*\)"[^"]*;\1;p' <<<"$r"); fi
__ARCHIVE_PKG_NAME__=$(awk -F/ '{print $NF}' <<<"$1")
}
get_archive_vers() { sed 's/^[^-]*-//;s/-\(all\|arm64-v8a\|arm-v7a\)\.apk//g' <<<"$__ARCHIVE_RESP__"; }
get_archive_pkg_name() { echo "$__ARCHIVE_PKG_NAME__"; }
# --------------------------------------------------
patch_apk() {
local stock_input=$1 patched_apk=$2 patcher_args=$3 rv_cli_jar=$4 rv_patches_jar=$5
local cmd="java -jar $rv_cli_jar patch $stock_input --purge -o $patched_apk -p $rv_patches_jar --keystore=ks.keystore \
--keystore-entry-password=123456789 --keystore-password=123456789 --signer=jhc --keystore-entry-alias=jhc $patcher_args"
if [ "$OS" = Android ]; then cmd+=" --custom-aapt2-binary=${AAPT2}"; fi
pr "$cmd"
if eval "$cmd"; then [ -f "$patched_apk" ]; else
rm "$patched_apk" 2>/dev/null || :
return 1
fi
}
check_sig() {
local file=$1 pkg_name=$2
local sig
if grep -q "$pkg_name" sig.txt; then
sig=$(java -jar "$APKSIGNER" verify --print-certs "$file" | grep ^Signer | grep SHA-256 | tail -1 | awk '{print $NF}')
grep -qFx "$sig $pkg_name" sig.txt
fi
}
build_rv() {
eval "declare -A args=${1#*=}"
local version="" pkg_name=""
local mode_arg=${args[build_mode]} version_mode=${args[version]}
local app_name=${args[app_name]}
local app_name_l=${app_name,,}
app_name_l=${app_name_l// /-}
local table=${args[table]}
local dl_from=${args[dl_from]}
local arch=${args[arch]}
local arch_f="${arch// /}"
local p_patcher_args=()
if [ "${args[excluded_patches]}" ]; then p_patcher_args+=("$(join_args "${args[excluded_patches]}" -d)"); fi
if [ "${args[included_patches]}" ]; then p_patcher_args+=("$(join_args "${args[included_patches]}" -e)"); fi
[ "${args[exclusive_patches]}" = true ] && p_patcher_args+=("--exclusive")
local tried_dl=()
for dl_p in archive apkmirror uptodown; do
if [ -z "${args[${dl_p}_dlurl]}" ]; then continue; fi
if ! get_${dl_p}_resp "${args[${dl_p}_dlurl]}" || ! pkg_name=$(get_"${dl_p}"_pkg_name); then
args[${dl_p}_dlurl]=""
epr "ERROR: Could not find ${table} in ${dl_p}"
continue
fi
tried_dl+=("$dl_p")
dl_from=$dl_p
break
done
if [ -z "$pkg_name" ]; then
epr "empty pkg name, not building ${table}."
return 0
fi
local get_latest_ver=false
if [ "$version_mode" = auto ]; then
if ! version=$(get_patch_last_supported_ver "$pkg_name" \
"${args[included_patches]}" "${args[excluded_patches]}" "${args[exclusive_patches]}"); then
exit 1
elif [ -z "$version" ]; then get_latest_ver=true; fi
elif isoneof "$version_mode" latest beta; then
get_latest_ver=true
p_patcher_args+=("-f")
else
version=$version_mode
p_patcher_args+=("-f")
fi
if [ $get_latest_ver = true ]; then
if [ "$version_mode" = beta ]; then __AAV__="true"; else __AAV__="false"; fi
pkgvers=$(get_"${dl_from}"_vers)
version=$(get_highest_ver <<<"$pkgvers") || version=$(head -1 <<<"$pkgvers")
fi
if [ -z "$version" ]; then
epr "empty version, not building ${table}."
return 0
fi
if [ "$mode_arg" = module ]; then
build_mode_arr=(module)
elif [ "$mode_arg" = apk ]; then
build_mode_arr=(apk)
elif [ "$mode_arg" = both ]; then
build_mode_arr=(apk module)
fi
pr "Choosing version '${version}' for ${table}"
local version_f=${version// /}
version_f=${version_f#v}
local stock_apk="${TEMP_DIR}/${pkg_name}-${version_f}-${arch_f}.apk"
if [ ! -f "$stock_apk" ]; then
for dl_p in archive apkmirror uptodown; do
if [ -z "${args[${dl_p}_dlurl]}" ]; then continue; fi
pr "Downloading '${table}' from ${dl_p}"
if ! isoneof $dl_p "${tried_dl[@]}"; then get_${dl_p}_resp "${args[${dl_p}_dlurl]}"; fi
if ! dl_${dl_p} "${args[${dl_p}_dlurl]}" "$version" "$stock_apk" "$arch" "${args[dpi]}" "$get_latest_ver"; then
epr "ERROR: Could not download '${table}' from ${dl_p} with version '${version}', arch '${arch}', dpi '${args[dpi]}'"
continue
fi
break
done
if [ ! -f "$stock_apk" ]; then return 0; fi
fi
if ! check_sig "$stock_apk" "$pkg_name"; then
abort "apk signature mismatch '$stock_apk'"
fi
log "${table}: ${version}"
local microg_patch
microg_patch=$(java -jar "$rv_cli_jar" list-patches "$rv_patches_jar" -f "$pkg_name" -v -p 2>&1 |
grep "^Name: " | grep -i "gmscore\|microg" || :) microg_patch=${microg_patch#*: }
if [ -n "$microg_patch" ] && [[ ${p_patcher_args[*]} =~ $microg_patch ]]; then
epr "You cant include/exclude microg patches as that's done by rvmm builder automatically."
p_patcher_args=("${p_patcher_args[@]//-[ei] ${microg_patch}/}")
fi
local patcher_args patched_apk build_mode
local rv_brand_f=${args[rv_brand],,}
rv_brand_f=${rv_brand_f// /-}
if [ "${args[patcher_args]}" ]; then p_patcher_args+=("${args[patcher_args]}"); fi
for build_mode in "${build_mode_arr[@]}"; do
patcher_args=("${p_patcher_args[@]}")
pr "Building '${table}' in '$build_mode' mode"
if [ -n "$microg_patch" ]; then
patched_apk="${TEMP_DIR}/${app_name_l}-${rv_brand_f}-${version_f}-${arch_f}-${build_mode}.apk"
if [ "$build_mode" = apk ]; then
patcher_args+=("-e \"${microg_patch}\"")
elif [ "$build_mode" = module ]; then
patcher_args+=("-d \"${microg_patch}\"")
fi
else
patched_apk="${TEMP_DIR}/${app_name_l}-${rv_brand_f}-${version_f}-${arch_f}.apk"
fi
if [ "${args[riplib]}" = true ]; then
patcher_args+=("--rip-lib x86_64 --rip-lib x86")
if [ "$build_mode" = module ]; then
patcher_args+=("--rip-lib arm64-v8a --rip-lib armeabi-v7a --unsigned")
else
if [ "$arch" = "arm64-v8a" ]; then
patcher_args+=("--rip-lib armeabi-v7a")
elif [ "$arch" = "arm-v7a" ]; then
patcher_args+=("--rip-lib arm64-v8a")
fi
fi
fi
if [ "${NORB:-}" != true ] || [ ! -f "$patched_apk" ]; then
if ! patch_apk "$stock_apk" "$patched_apk" "${patcher_args[*]}" "${args[cli]}" "${args[ptjar]}"; then
epr "Building '${table}' failed!"
return 0
fi
fi
if [ "$build_mode" = apk ]; then
local apk_output="${BUILD_DIR}/${app_name_l}-${rv_brand_f}-v${version_f}-${arch_f}.apk"
mv -f "$patched_apk" "$apk_output"
pr "Built ${table} (non-root): '${apk_output}'"
continue
fi
local base_template
base_template=$(mktemp -d -p "$TEMP_DIR")
cp -a $MODULE_TEMPLATE_DIR/. "$base_template"
local upj="${table,,}-update.json"
module_config "$base_template" "$pkg_name" "$version" "$arch"
module_prop \
"${args[module_prop_name]}" \
"${app_name} ${args[rv_brand]}" \
"$version" \
"${app_name} ${args[rv_brand]} Magisk module" \
"https://raw.githubusercontent.com/${GITHUB_REPOSITORY-}/update/${upj}" \
"$base_template"
local module_output="${app_name_l}-${rv_brand_f}-magisk-v${version_f}-${arch_f}.zip"
pr "Packing module ${table}"
cp -f "$patched_apk" "${base_template}/base.apk"
if [ "${args[include_stock]}" = true ]; then cp -f "$stock_apk" "${base_template}/${pkg_name}.apk"; fi
pushd >/dev/null "$base_template" || abort "Module template dir not found"
zip -"$COMPRESSION_LEVEL" -FSqr "${CWD}/${BUILD_DIR}/${module_output}" .
popd >/dev/null || :
pr "Built ${table} (root): '${BUILD_DIR}/${module_output}'"
done
}
list_args() { tr -d '\t\r' <<<"$1" | tr -s ' ' | sed 's/" "/"\n"/g' | sed 's/\([^"]\)"\([^"]\)/\1'\''\2/g' | grep -v '^$' || :; }
join_args() { list_args "$1" | sed "s/^/${2} /" | paste -sd " " - || :; }
module_config() {
local ma=""
if [ "$4" = "arm64-v8a" ]; then
ma="arm64"
elif [ "$4" = "arm-v7a" ]; then
ma="arm"
fi
echo "PKG_NAME=$2
PKG_VER=$3
MODULE_ARCH=$ma" >"$1/config"
}
module_prop() {
echo "id=${1}
name=${2}
version=v${3} (${NEXT_VER_CODE})
versionCode=${NEXT_VER_CODE}
author=j-hc
description=${4}" >"${6}/module.prop"
if [ "$ENABLE_MAGISK_UPDATE" = true ]; then echo "updateJson=${5}" >>"${6}/module.prop"; fi
}