This repository has been archived by the owner on Nov 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathservice.sh
305 lines (237 loc) · 7.15 KB
/
service.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
#!/data/adb/magisk/busybox sh
# shellcheck shell=sh
##
# Script environment
##
export MODDIR="${0%/*}"
export MODPATH="$MODDIR"
export TMPDIR="$MODDIR"/tmp
export LOGPATH="$MODDIR"/service.log
log_service() {
# If the log path is bigger than 5MB, delete the oldest log
if [ -f "$LOGPATH" ] && [ "$(wc -c <"$LOGPATH")" -gt 5000000 ]; then
rm -f "$LOGPATH"
fi
# Write log to file
echo "[$(date)] $*" >>"$LOGPATH"
}
ui_print() {
log_service "$*"
}
abort() {
log_service "$*"
exit 1
}
wait_until_boot_complete() {
loop_count=0
until [ "$(getprop init.svc.bootanim)" = "stopped" ] && [ "$(getprop sys.boot_completed)" = "1" ] && [ -d /sdcard ]; do
# CHECK
if [ "$loop_count" -gt 10 ]; then
log_service "Exceeded the maximum number of retries for loading the service."
abort "Boot Animation: $(getprop init.svc.bootanim) | Boot Complete: $(getprop sys.boot_completed) | /sdcard: $(if [ -d /sdcard ]; then echo "Exists"; else echo "Does not exist"; fi)"
fi
# WAIT
sleep 3
loop_count=$((loop_count + 1))
done
}
export_IS64BIT() {
abi_prop=$(getprop ro.product.cpu.abi)
IS64BIT=false
if [ "${abi_prop%64}" != "$abi_prop" ]; then
IS64BIT=true
fi
export IS64BIT
}
##
# End of Script environment
##
##
##
##
# Begin patching device_prefs
##
export_prop_prefs() {
propPath="$1"
shift
for propName in "$@"; do
pref_prop="$(grep_prop "$propName" "$propPath")"
# Remove quotes from the filepath
PREF_FILEPATH="${pref_prop%\"}"
PREF_FILEPATH="${PREF_FILEPATH#\"}"
export "$propName"="$PREF_FILEPATH"
done
}
str_replace() {
search="$(echo "$1" | sed -e 's/[]\/$*.^|[]/\\&/g')"
replace="$(echo "$2" | sed -e 's/[]\/$*.^|[]/\\&/g')"
subject="$(echo "$3" | sed -e 's/[]\/$*.^|[]/\\&/g')"
file="$4"
# Logging
log_service "str_replace: $search -> $replace in $subject"
# Replace subject with a parsed version of itself
subject=$(grep "$subject" "$file" | xargs | sed -e 's/>.</>\n</g')
# Escape the subject
subject=$(echo "$subject" | sed -e 's/[]\/$*.^|[]/\\&/g')
# Save subject to a temp file
TMPDIR_FILE="$TMPDIR/$(xxd -l 5 -c 5 -p </dev/random)"
touch "$TMPDIR_FILE"
echo "$subject" >"$TMPDIR_FILE"
# Loop true subject new lines
while read -r line; do
# Grep the line from the subject and replace it
replaced_subject=$(grep "$line" "$file" | sed "s/$search/$replace/g" | xargs)
# Escape the subject
escaped_replaced_subject=$(echo "$replaced_subject" | sed -e 's/[]\/$*.^|[]/\\&/g')
# Logging
log_service "str_replace: $line -> $replaced_subject"
# Replace the subject with the replaced subject in the file
sed -i "s/$subject/$escaped_replaced_subject/g" "$file"
done <"$TMPDIR_FILE"
# Remove the temp file
rm -f "$TMPDIR_FILE"
}
bool_patch_false() {
str_replace true false "$1" "$xml_pref_path"
}
bool_patch() {
str_replace false true "$1" "$xml_pref_path"
}
value_patch() {
# Replace subject with a parsed version of itself
subject=$(grep "$1" "$xml_pref_path" | xargs | sed 's/> </>\n</g')
# Save subject to a temp file
TMPDIR_FILE="$TMPDIR/$(xxd -l 5 -c 5 -p </dev/random)"
touch "$TMPDIR_FILE"
echo "$subject" >"$TMPDIR_FILE"
while read -r line; do
# If the string has a closing tag use the value inside it
if [[ "$line" == *"</"* ]]; then
str_replace "$(echo "$line" | cut -d'>' -f2 | cut -d'<' -f1)" "$2" "$1" "$xml_pref_path"
else
str_replace "$(echo "$line" | sed -rn 's/.*value="(.*)".*/\1/p')" "$2" "$1" "$xml_pref_path"
fi
done <"$TMPDIR_FILE"
# Remove the temp file
rm -f "$TMPDIR_FILE"
}
set_prefs() {
log_service "Setting prefs..."
for file in "$MODDIR"/device_prefs/*/*.ini; do
# Get folder base name
folder_path=${file%/*} # Remove last /
folder_name=${folder_path##*/} # Remove path before folder name
if [ -f "$file" ]; then
file_name=${file##*/}
file_name_no_ext=${file_name%.*}
log_service "Patching \"$folder_name\" on \"$file_name_no_ext\""
# Export prefs
export_prop_prefs "$file" "db_pref_packageName" "db_pref_path" "xml_pref_path"
# Create temp file
TMPDIR_FILE="$TMPDIR/$(xxd -l 5 -c 5 -p </dev/random)"
touch "$TMPDIR_FILE"
# Go true file lines
while read -r line; do
# Remove new line from line
line=${line%$'\r'}
# Ignore comments, empty lines and script specific lines
if [[ -n "$line" ]] && [[ "$line" != "#"* ]] && [[ "$line" != *"_pref_"* ]]; then
# Save line to a temp file
echo "$line" >"$TMPDIR_FILE"
# Explode the key and value based on the equal sign
IFS='=' read -r key value <"$TMPDIR_FILE"
# Remove the quotes sign between the value
value=${value#\"}
value=${value%\"}
log_service "Patching \"$key\" -> \"$value\""
# Patch XML values
value_patch "$key" "$value"
# Switch the type of patch value based on folder name
case "$folder_name" in
"boolean")
insert_gms_features "FlagOverrides" "$db_pref_packageName" 0 null "$value" null null 1 "$key"
;;
"integer")
insert_gms_features "FlagOverrides" "$db_pref_packageName" 0 "$value" null null null 1 "$key"
;;
"string")
insert_gms_features "FlagOverrides" "$db_pref_packageName" 0 null null null "$value" 1 "$key"
;;
*)
ui_print "Unknown folder name: $folder_name"
;;
esac
fi
done <"$file"
# Remove temp file
rm -f "$TMPDIR_FILE"
fi
done
}
##
# End patching device_prefs
##
##
##
##
# Begin patching device_config
##
dc_set_service() {
export SERVICE_NAME="$1"
}
dc_put() {
{ [ "$SERVICE_NAME" ] && [ "$1" ] && [ "$2" ]; } && device_config put "$SERVICE_NAME" "$1" "$2"
}
set_flags() {
log_service "Setting flags..."
for file in "$MODDIR"/device_config/*.ini; do
if [ -f "$file" ]; then
file_name=${file##*/}
file_name_no_ext=${file_name%.*}
log_service "Patching \"$file_name_no_ext\""
# Set service name
dc_set_service "$file_name_no_ext"
# Create temp file
TMPDIR_FILE="$TMPDIR/$(xxd -l 5 -c 5 -p </dev/random)"
touch "$TMPDIR_FILE"
# Go true file lines
while read -r line; do
# Save line to a temp file
echo "$subject" >"$TMPDIR_FILE"
# Explode the key and value based on the equal sign
IFS='=' read -r key value <"$TMPDIR_FILE"
dc_put "$key" "$value"
done <"$file"
# Remove temp file
rm -f "$TMPDIR_FILE"
fi
done
}
##
# End patching device_config
##
##
##
##
# Script start
##
log_service "Service started."
log_service "MODDIR: $MODDIR"
wait_until_boot_complete
export_IS64BIT
mkdir "$TMPDIR"
log_service "System fully started. Beginning to patch..."
# Run utils
[ -f "$MODDIR/common/addon/Utils/install.sh" ] && . "$MODDIR/common/addon/Utils/install.sh"
# Run SQLite3
[ -f "$MODDIR/common/addon/SQLite3/install.sh" ] && . "$MODDIR/common/addon/SQLite3/install.sh"
# Start patching
set_flags
set_prefs
# Clean temp files
rm -rf "$TMPDIR"
# Finish
log_service "Service finished."
##
# Script end
##