forked from xenia-canary/game-patches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_patch.sh
283 lines (270 loc) · 11.8 KB
/
create_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
#!/usr/bin/env bash
prompt_error() {
read -rn1 -p $'\e[31mERROR:\e[0m'" $1"$'\n\nPress any key to continue . . . '
exit 1
}
if [ ! -d patches ]; then
prompt_error "patches directory doesn't exist!"
fi
xenia_log_default='xenia.log'
if [ -f "$xenia_log_default" ]; then
xenia_log="$xenia_log_default"
elif [ -f "$1" ]; then
xenia_log="$1"
else
prompt_error "Log doesn't exist!"
fi
echo "Found $xenia_log"
echo 'Checking log...'
xenia_log_cleaned=$(grep -oP ' {7}(Media|Title) ID: [0-9A-F]{8}|i> [0-9A-F]{8} Title name:.+|i> [0-9A-F]{8} Module hash: [0-9A-F]{16} for.+' "$xenia_log" | sort -u)
new_patch_media_ids=($(grep -oP '(?<= {7}Media ID: )[0-9A-F]{8}' <<<"$xenia_log_cleaned"))
new_patch_title_ids=($(grep -oP '(?<= {7}Title ID: )[0-9A-F]{8}' <<<"$xenia_log_cleaned"))
readarray -t new_patch_title_names < <(grep -oP '(?<=i> [0-9A-F]{8} Title name: ).+' <<<"$xenia_log_cleaned")
new_patch_hashes=($(grep -oP '(?<=i> [0-9A-F]{8} Module hash: )[0-9A-F]{16}(?= for .+)' <<<"$xenia_log_cleaned"))
new_patch_hashes_modules=($(grep -oP '(?<=i> [0-9A-F]{8} Module hash: [0-9A-F]{16} for ).+' <<<"$xenia_log_cleaned"))
set -e
prompt() {
if [[ -n "$valid_input_length" && $valid_input_length != nonempty && -z $valid_input_length_maximum ]]; then
read -rn$valid_input_length -p$'\n'"$1"$'\n' "$2"
elif [ -n "$valid_input_length_maximum" ]; then
read -rn$valid_input_length_maximum -p$'\n'"$1"$'\n' "$2"
else
read -rp$'\n'"$1"$'\n' "$2"
fi
local user_input="${!2,,}"
if [[ "$user_input" = q || "$user_input" = quit ]]; then
exit
fi
if [ -n "$valid_input_length" ]; then
if [[ ( -z "$user_input" && $valid_input_length = nonempty ) || ( ${#user_input} != $valid_input_length && $valid_input_length != nonempty && -z $valid_input_length_minimum ) || ( -n "$valid_input_length_minimum" && ${#user_input} -lt $valid_input_length_minimum ) || ( -n "$valid_input_length_maximum" && ${#user_input} -gt $valid_input_length_maximum ) ]]; then
${FUNCNAME[0]} "$@"
fi
fi
if [ -n "$valid_input_params" ]; then
for valid_input_param in "${valid_input_params[@]}"; do
if [ "$user_input" = "$valid_input_param" ]; then
local valid_input=true
break
fi
done
if [ -z $valid_input ]; then
${FUNCNAME[0]} "$@"
elif [ -z $3 ]; then
unset valid_input_params
fi
fi
if [ -n "$valid_input_regex" ]; then
if [[ "$user_input" =~ $valid_input_regex ]]; then
unset valid_input_regex
else
${FUNCNAME[0]} "$@"
fi
fi
unset valid_input_length
unset valid_input_length_minimum
#unset valid_input_length_maximum
}
check_multiple_choice() {
local array_2="$2"'[@]'
local expanded_2=("${!array_2}")
if [ ${#expanded_2[@]} -gt 1 ]; then
if [ ${#expanded_2[@]} -le 9 ]; then
valid_input_length=1
else
valid_input_length=2
valid_input_length_minimum=1
fi
new_patch_thing_prompt="What is the $1 of your patch? [(Q)uit]"$'\n'
for (( i=0; i <= ( ${#expanded_2[@]} - 1 ); i++ )); do
valid_input_key_number=$(($i + 1))
valid_input_params+=($valid_input_key_number)
if [ -n "$4" ]; then
local expanded_4="$4"'[$i]'
local new_patch_thing_prompt_addendum=" - ${!expanded_4}"
fi
if [[ $valid_input_key_number -le 9 && ${#expanded_2[@]} -ge 10 ]]; then
if [ -z "$space" ]; then
local space=' '
fi
else
unset space
fi
local new_patch_thing_prompt+=" ${space}${valid_input_key_number}. ${expanded_2[$i]}$new_patch_thing_prompt_addendum"$'\n'
done
prompt "${new_patch_thing_prompt/%$'\n'}" new_patch_thing_choice
declare -g "$3"="${expanded_2[($new_patch_thing_choice - 1)]}"
if [[ -n "$4" && -n "$5" ]]; then
local unexpanded_4_user_choice="$4"\[$(($new_patch_thing_choice - 1))\]
declare -g "$5"="${!unexpanded_4_user_choice}"
fi
elif [ ${#expanded_2[@]} -gt 0 ]; then
declare -g "$3"="$expanded_2"
if [[ -n "$4" && -n "$5" ]]; then
declare -g "$5"="${!4}"
fi
fi
}
# Media/Title ID (and maybe name?) can potentially mismatch, but having more than 1 is already rare so meh
check_multiple_choice 'media ID' new_patch_media_ids new_patch_media_id
check_multiple_choice 'title ID' new_patch_title_ids new_patch_title_id
check_multiple_choice 'title name' new_patch_title_names new_patch_title_name
new_patch_title_name=$(tr -d '"\\' <<<$new_patch_title_name) # " and \ are unsafe even for TOML
check_multiple_choice hash new_patch_hashes new_patch_hash new_patch_hashes_modules new_patch_hash_module
if [[ -n "$new_patch_media_id" && -n "$new_patch_title_id" && -n "$new_patch_title_name" && -n "$new_patch_hash" && -n "$new_patch_hash_module" ]]; then
new_patch_serial="$(xxd -p -r <<<${new_patch_title_id::4})-$((16#${new_patch_title_id:4}))"
new_patch_filename="$new_patch_title_id - $(tr -d '/:*?<>|™©' <<<"${new_patch_title_name}.patch.toml")"
echo -e "\n\nPatch filename: ${new_patch_filename}\nPatch serial: ${new_patch_serial}\nPatch hash: ${new_patch_hash}\n"
else
prompt_error 'Media ID, title ID, title name, and/or hash are missing from the log.'$'\nMake sure log_level is set to 2 in the Xenia config.'
fi
echo_warning() {
echo -e "\e[33mWARNING:\e[0m ${1}\n"
}
for existing_patch in patches/*; do
if [[ "$existing_patch" =~ $new_patch_title_id ]]; then
if [[ "$existing_patch" =~ "$new_patch_filename" ]]; then
if [ -z $patch_conflict_level ]; then
echo_warning 'Patch already exists;'
patch_conflict_level=2
fi
echo " Matched with: $existing_patch"
else
if [ -z $patch_conflict_level ]; then
echo_warning 'Patch might already exist;'
patch_conflict_level=1
fi
echo " Partially matched with: $existing_patch"
fi
fi
done
if [ -n "$patch_conflict_level" ]; then
valid_input_length=1
valid_input_params=y
case "$patch_conflict_level" in
1)
prompt 'Potentially overwrite patch? [(Y)es, (Q)uit]' answer;;
2)
prompt 'Overwrite patch? [(Y)es, (Q)uit]' answer;;
esac
fi
valid_input_length=nonempty
prompt $'\nWhat is the name of your patch? [(Q)uit]' new_patch_name
prompt 'What is the description of your patch? [(Q)uit]' new_patch_desc
valid_input_length=nonempty
prompt 'Who are the authors of your patch? [(Q)uit]' new_patch_author
# New patch is_enabled
valid_input_length=1
valid_input_params=(t f)
prompt 'Do you want this patch to be enabled? [(t)rue, (f)alse, (q)uit]' new_patch_is_enabled keep_params
case $new_patch_is_enabled in
${valid_input_params[0]})
new_patch_is_enabled=true;;
${valid_input_params[1]})
new_patch_is_enabled=false;;
esac
unset valid_input_params
# New patch address
valid_input_length=8
valid_input_regex_hex='[0-9A-Fa-f]{$valid_input_length}'
eval valid_input_regex=$valid_input_regex_hex # eval might be bad
prompt $'\nWhat is the address of your patch? [(Q)uit]' new_patch_address
# New patch type
valid_patch_types=(be{8,16,32,64} f{32,64})
valid_patch_types_length=(2 4 8 16)
valid_input_length=1
valid_input_params=({1..6} a)
prompt $'\nWhat is the type of your patch? [(Q)uit]'$'\n'" 1. ${valid_patch_types[0]}"$'\n'" 2. ${valid_patch_types[1]}"$'\n'" 3. ${valid_patch_types[2]}"$'\n'" 4. ${valid_patch_types[3]}"$'\n'" 5. ${valid_patch_types[4]}"$'\n'" 6. ${valid_patch_types[5]}"$'\n'" a. Automatic" new_patch_type_input keep_params
case "$new_patch_type_input" in
${valid_input_params[0]})
new_patch_type=${valid_patch_types[0]}
valid_input_length=${valid_patch_types_length[0]};;
${valid_input_params[1]})
new_patch_type=${valid_patch_types[1]}
valid_input_length=${valid_patch_types_length[1]};;
${valid_input_params[2]})
new_patch_type=${valid_patch_types[2]}
valid_input_length=${valid_patch_types_length[2]};;
${valid_input_params[3]})
new_patch_type=${valid_patch_types[3]}
valid_input_length=${valid_patch_types_length[3]};;
${valid_input_params[4]})
new_patch_type=${valid_patch_types[4]}
valid_input_length=nonempty;; # This probably isn't right
${valid_input_params[5]})
new_patch_type=${valid_patch_types[5]}
valid_input_length=nonempty;; # This probably isn't right
${valid_input_params[6]})
valid_input_length=nonempty
valid_input_length_maximum=32;;
esac
unset valid_input_params
if [ -n "$new_patch_type" ]; then
if [[ $new_patch_type != ${valid_patch_types[4]} && $new_patch_type != ${valid_patch_types[5]} ]]; then
eval valid_input_regex=$valid_input_regex_hex # eval might be bad
fi
else
valid_input_regex="^[0-9a-f]{${valid_patch_types_length[0]}}$|^[0-9a-f]{${valid_patch_types_length[1]}}$|^[0-9a-f]{${valid_patch_types_length[2]}}$|^[0-9a-f]{${valid_patch_types_length[3]}}$|^[0-9]+(\.[0-9]{1,${valid_input_length_maximum}}){0,1}$"
fi
prompt $'\nWhat is the value of your patch? [(Q)uit]' new_patch_value
if [ -z $new_patch_type ]; then
case ${#new_patch_value} in
${valid_patch_types_length[0]})
new_patch_type=${valid_patch_types[0]};;
${valid_patch_types_length[1]})
new_patch_type=${valid_patch_types[1]};;
${valid_patch_types_length[2]})
new_patch_type=${valid_patch_types[2]};;
${valid_patch_types_length[3]})
new_patch_type=${valid_patch_types[3]};;
*)
if [[ $new_patch_value =~ ^[0-9]+(\.[0-9]{1,6}){0,1}$ ]]; then
new_patch_type=${valid_patch_types[4]}
elif [[ $new_patch_value =~ ^[0-9]+(\.[0-9]{7,$valid_input_length_maximum}){0,1}$ ]]; then
new_patch_type=${valid_patch_types[5]}
fi;;
esac
fi
unset valid_input_length_maximum
if [[ $new_patch_type != ${valid_patch_types[4]} && $new_patch_type != ${valid_patch_types[5]} ]]; then
new_patch_value="0x${new_patch_value^^}"
fi
new_patch_path="patches/$new_patch_filename"
new_patch_contents="title_name = \"${new_patch_title_name}\"
title_id = \"${new_patch_title_id}\" # $new_patch_serial
hash = \"${new_patch_hash}\" # $new_patch_hash_module
#media_id = \"${new_patch_media_id}\"
[[patch]]
name = \"${new_patch_name^}\""
if [ -n "$new_patch_desc" ]; then
new_patch_contents+="
desc = \"${new_patch_desc^}\""
fi
new_patch_contents+="
author = \"${new_patch_author}\"
is_enabled = $new_patch_is_enabled
[[patch.${new_patch_type}]]
address = 0x${new_patch_address^^}
value = ${new_patch_value}"
echo "$new_patch_contents" > "$new_patch_path"
echo -e "\n\n\e[32mPatch '$new_patch_path' created successfully! \e[0m"
valid_input_length=1
valid_input_params=(o p)
prompt 'What would you like to do? [(O)pen patch, (P)review patch, (Q)uit]' answer keep_params
case $answer in
${valid_input_params[0]})
if [[ ! "$OSTYPE" = cygwin && ! "$OSTYPE" = msys ]]; then
editors=($EDITOR {xdg-,}open code{,-insiders} codium gedit atom emacs notepadqq subl nano pico {g,n}vim)
for editor in "${editors[@]}"; do
if hash $editor 2>/dev/null; then
break
fi
done
$editor "$new_patch_path" &
else
powershell -NoLogo -command ./\""$new_patch_path\"" &
fi;;
${valid_input_params[1]})
echo -e "\n\n\n$(cat "$new_patch_path")"
read -rn1 -p $'\n\nPress any key to continue . . . ';;
esac
unset valid_input_params