5 # This script replaces broken hardcoded strings in binary files (e.g. maps)
6 # with another string given by the user.
8 # The input and ouput strings are given by the file passed as first argument,
9 # which should be of the form:
11 # old string<TAB>new string
12 # other old string<TAB>other new string
15 # The strings are fixed by binary replacements of their hex code. The hex
16 # representation of a string starts with its length on four bytes, but in
17 # this script we currently only fix the fourth byte (the others are typically
18 # null), which means we support strings up to 255 chars.
20 # The binary files to fix are passed as second and optionally more arguments.
25 # ./Misc/fix-hardcoded-hex-paths.sh [file describing fixes] [maps to fix]
29 # ./Misc/fix-hardcoded-hex-paths.sh Misc/list-hardcoded-paths-fixes.txt Data/Maps/jendraz*
35 local len=$(echo -n $1 | wc -c)
36 if [ $len -gt 255 ]; then echo "This poor script can't handle such long strings. Blame Akien."; exit 1; fi
37 len='\x'$(printf '%02x\n' $len)
38 local hexstring=$(echo -n $1 | xxd -ps | sed 's/[[:xdigit:]]\{2\}/\\x&/g')
39 # Seems xxd will put some separators on long strings
40 return=$len$(echo $hexstring | sed 's/ //g')
45 local string=$(echo -n $1 | cut -c 4-)
46 return=$(echo $string | sed 's/\x//g' | xxd -r -ps)
55 if [ ! -e $in_file ]; then echo "You must pass a valid filename as argument, got $in_file."; exit 1; fi
57 if [ -z "${target_files}" ]; then echo "You muss pass a list of files as second argument."; exit 1; fi
59 echo -e "Replacing hardcoded strings as listed in $in_file in the following files:\n\n$target_files\n"
61 while IFS='' read -r line || [[ -n "$line" ]]; do
62 input=$(echo "$line" | cut -f1)
63 output=$(echo "$line" | cut -f2)
64 echo "Replacing '${input}' by '${output}'."
69 if [ "$return" != "$input" ]; then echo "Something went wrong in the conversion of $input back and forth to hex."; fi
74 if [ "$return" != "$output" ]; then echo "Something went wrong in the conversion of $output back and forth to hex."; fi
76 for target_file in $target_files; do
77 sed -i 's/'${in_hex}'/'${out_hex}'/g' "${target_file}"