-
Notifications
You must be signed in to change notification settings - Fork 61
/
patch.sh
executable file
·360 lines (313 loc) · 10.2 KB
/
patch.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
#!/bin/sh -e
# shellcheck disable=2154
#---------------------------#
# Made by XDream8 #
#---------------------------#
# deps #
#---------------------------#
# aria2, axel, curl or wget #
# awk #
# java(17) #
# grep or ripgrep #
#---------------------------#
RED='\033[0;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
out() {
# print a message
printf '%b\n' "$@"
}
log() {
printf '%b\n' "${YELLOW}$*${NC}"
}
err() {
printf '%b\n' "${RED}$*${NC}"
exit 1
}
notset() {
case $1 in '') return 0 ;; *) return 1 ;; esac
}
equals() {
case $1 in "$2") return 0 ;; *) return 1 ;; esac
}
cleanup() {
for tmp_file in "cache/tmp.revanced_"*; do
rm -rf "$tmp_file"
done
}
check_dep() {
if ! command -v "$1" >/dev/null; then
[ "$2" ] && out "${RED}$2${NC}"
return 1
else
return 0
fi
}
checkadb() {
if [ ! "$(pidof adb)" ]; then
if check_dep "rdo"; then
sudo=rdo
elif check_dep "doas"; then
sudo=doas
elif check_dep "sudo"; then
sudo=sudo
else
sudo=
fi
log "starting adb server"
$sudo adb start-server || {
err "starting adb server failed, exiting!"
}
fi
device_id=$(adb devices | awk 'FNR == 2 {print $1}')
if [ "$device_id" = "" ]; then
err "your phone is not connected to pc, exiting!"
else
log "adb device_id=$device_id"
fi
}
checkyt() {
# shellcheck disable=2143
if [ ! "$(adb shell cmd package list packages | $grep -o 'com.google.android.youtube')" ]; then
err "root variant: install $what_to_patch v${apk_version} on your device to mount, exiting!"
fi
}
get_version_info() {
## revanced cli
resp=$(curl -sL -H 'User-Agent: revanced-creator' "https://github.com/revanced/revanced-$1/releases/latest")
ver=$(printf '%s' "$resp" | $grep -oe "v[0-9].*[0-9]" | awk -F'/' 'NR==1 {print $1}')
ver=${ver#v}
printf '%s\n' "$ver" >"cache/tmp.revanced_$1"
}
get_and_print_versions() {
## getting versions information
out "${BLUE}getting latest versions info${NC}"
for i in cli patches; do
get_version_info "$i" &
done
## wait for getting versions to get finished
wait
for i in cli patches; do
export "revanced_${i}_version=$(cat cache/tmp.revanced_$i)"
done
## print info
log "revanced_cli_version : $revanced_cli_version"
log "revanced_patches_version : $revanced_patches_version"
}
get_stock_apk_version() {
notset "$apk_version" && {
apk_version=$(curl -sL -H 'User-Agent: revanced-creator' "https://api.github.com/repos/XDream8/revanced-creator/releases" | sed 's/"name":"/\n/g; s/.apk",/\n/g' | $grep -ioe "^$what_to_patch-[0-9].*[0-9]" | $grep -oe "[0-9].*[0-9]" | sort -n | awk 'END{print}')
}
notset "$apk_version" && {
err "getting $what_to_patch apk version failed, exiting!"
}
log "$what_to_patch version to be patched : $apk_version"
}
remove_old() {
if check_dep "find"; then
find . -maxdepth 1 -type f -name "revanced-*.jar" ! \( -name "*.keystore" -or -name "$cli_filename" -or -name "$patches_filename" -or -name "$apk_filename" \) -delete && out "${BLUE}removed old files${NC}"
fi
}
download_needed() {
# number
n=0
out "${BLUE}Downloading needed files${NC}"
for i in \
$cli_link \
$patches_link \
$aapt2_link \
$apk_link; do
n=$((n + 1))
out "${CYAN}$n) ${YELLOW}downloading $i${NC}"
$downloader "$i" &
done
wait
}
patch() {
out "${BLUE}patching process started(${RED}$root_text${BLUE})${NC}"
out "${BLUE}it may take a while please be patient${NC}"
base_cmd="java -jar $cli_filename patch $apk_filename -o $output_apk -p $patches_filename"
# shellcheck disable=2086
$base_cmd $additional_args
}
addarg() {
if notset "$additional_args"; then
additional_args="$*"
else
additional_args="$additional_args $*"
fi
}
main() {
## defaults
: "${what_to_patch=youtube}"
: "${root=0}"
: "${grep=$(command -v rg || command -v grep)}"
[ ! -d "cache" ] && mkdir -p cache
## downloader
if notset "$downloader"; then
if check_dep "aria2c"; then
downloader="aria2c -x 16 -s 1"
elif check_dep "axel"; then
downloader="axel -n 16"
elif check_dep "curl"; then
downloader="curl -qLJO"
elif check_dep "wget"; then
downloader="wget"
fi
fi
## dependecy checks
check_dep "curl" "curl is required, exiting!"
equals "$downloader" "curl*" || check_dep "${downloader%% -*}" "${downloader%% -*} is missing, exiting!"
equals "$root" "1" && check_dep "adb" "adb is required for root variant, exiting!"
check_dep "java" "java 17 is required, exiting!"
check_dep "awk" "awk is required, exiting!"
check_dep "${grep%% -*}" "${grep%% -*} is required, exiting!"
## java version check
JAVA_VERSION="$(java -version 2>&1 | $grep -o "version \".*\"" | awk 'match($0, /([0-9]+)/) {print substr($0, RSTART, RLENGTH)}')"
if [ "$JAVA_VERSION" -lt "11" ]; then
err "java 11(minimum) is required but you have version $JAVA_VERSION, exiting!"
else
log "java version $JAVA_VERSION found"
fi
## check $root
if [ "$root" -eq 0 ]; then
root_text="non-root"
else
root_text="root"
out "${RED}please be sure that your phone is connected to your pc, waiting 5 seconds${NC}"
sleep 5s
checkadb
addarg "-d $device_id --disable=microg-support --mount"
fi
# termux support
if command -v termux-setup-storage >/dev/null; then
## check arch
case "$(uname -m)" in
aarch64)
aapt2_filename="termux-arm64-v8a-aapt2"
;;
*)
err "that architecture is not supported by revanced-creator at the moment, please create a issue"
;;
esac
addarg "--custom-aapt2-binary=./$aapt2_filename"
[ ! -f "$aapt2_filename" ] && aapt2_link="https://github.com/XDream8/revanced-creator/releases/download/other/$aapt2_filename"
fi
## what should we patch
case "$what_to_patch" in
youtube)
get_stock_apk_version
apk_filename=YouTube-$apk_version.apk
addarg "--disable=enable-debugging"
;;
youtube-music)
get_stock_apk_version
apk_filename=YouTube-Music-$apk_version.apk
;;
twitch)
get_stock_apk_version
apk_filename=Twitch-$apk_version.apk
addarg "--disable=debug-mode"
;;
twitter)
get_stock_apk_version
apk_filename=Twitter-$apk_version.apk
;;
reddit)
get_stock_apk_version
apk_filename=Reddit-$apk_version.apk
;;
tiktok)
get_stock_apk_version
apk_filename=TikTok-$apk_version.apk
;;
spotify)
get_stock_apk_version
apk_filename=Spotify-$apk_version.apk
;;
*)
if notset "$what_to_patch" && check_dep "find"; then
what_to_patch=$(find . -maxdepth 1 -type f -iname "*.apk" -not -iname "app-release-unsigned.apk" -or -not -iname "revanced-*.apk" -or -not -iname "*.keystore" | sort -r | awk -F'/' 'NR==1 {print $2}')
elif [ -z "$what_to_patch" ] && ! check_dep "find"; then
err "please specify an apk file using '--patch/-p' flag"
else
[ -z "$what_to_patch" ] && {
err "please specify an apk file using '--path/-p' flag"
}
fi
if [ ! -f "$what_to_patch" ]; then
err "apk file does not exist, please specify an existing apk file using 'apk_filename' arg"
fi
apk_filename="$what_to_patch"
[ -z "$output_apk" ] && output_apk=revanced-$apk_filename
log "custom apk : $apk_filename"
;;
esac
## get and print versions
get_and_print_versions
## set filenames
cli_filename=revanced-cli-$revanced_cli_version-all.jar
patches_filename=patches-$revanced_patches_version.rvp
## set output apk name
notset "$output_apk" && {
output_apk=revanced-${what_to_patch#*-}-$apk_version-$root_text.apk
}
## link to download $what_to_patch
[ ! -f "$apk_filename" ] && apk_link=https://github.com/XDream8/revanced-creator/releases/download/$what_to_patch/$apk_filename
## remove old files with find
remove_old
[ ! -f "$cli_filename" ] && cli_link=https://github.com/revanced/revanced-cli/releases/download/v$revanced_cli_version/$cli_filename
[ ! -f "$patches_filename" ] && patches_link=https://github.com/revanced/revanced-patches/releases/download/v$revanced_patches_version/$patches_filename
download_needed
if [ "$root" -eq 1 ]; then
out "${BLUE}root variant: installing stock youtube-$apk_version first${NC}"
adb install -r "$apk_filename" || {
err "install failed, exiting!"
}
checkyt
fi
if [ -f "$aapt2_filename" ]; then
chmod +x "$aapt2_filename"
fi
patch
}
for opt in "$@"; do
case $opt in
--nocolors | -nc)
# unset colors
for color in RED GREEN YELLOW BLUE CYAN NC; do
unset $color
done
;;
--root)
root=1
;;
--downloader=*)
downloader="${opt#*=}"
;;
--patch=* | -p=*)
what_to_patch="${opt#*=}"
;;
--additional-args=* | -args=*)
addarg "${opt#*=}"
;;
--help | -h | *)
out "${RED}------${NC} ${CYAN}revanced-creator${NC} ${RED}------${NC}
${GREEN}Create revanced apps easily${NC}
${YELLOW}Usage:${NC} $0 ${BLUE}<flags>${NC}
${RED}--nocolors${NC},${RED} -nc\t\t\t\t\t${GREEN}Do not print colored text${NC}
${RED}--downloader=${BLUE}<string>\t\t\t\t${GREEN}Set a custom downloader
${RED}--root\t\t\t\t\t\t${GREEN}Build for rooted phones
${RED}--patch=${BLUE}<string>${NC},${RED} -p=${BLUE}<string>\t\t\t${GREEN}What should we patch
${RED}--additional-args=${BLUE}<string>${NC},${RED} -args=${BLUE}<string>\t${GREEN}Args that will be passed to java cmd
"
exit 0
;;
esac
done
trap cleanup EXIT
main
# vim: set sw=4 sts=4 ft=sh: