Skip to content

Instantly share code, notes, and snippets.

@Sanaki
Last active August 1, 2020 05:19
Show Gist options
  • Save Sanaki/0c3c114e9fe3e4499deb16978e74c9ec to your computer and use it in GitHub Desktop.
Save Sanaki/0c3c114e9fe3e4499deb16978e74c9ec to your computer and use it in GitHub Desktop.
CD-type chd extractor (V5 only, supports clones)
#!/usr/bin/env bash
declare -A parents
function getparents {
for i in *.chd; do
if [[ "$(xxd -ps -s 104 -l 20 "$i")" = "0000000000000000000000000000000000000000" ]]; then
parents["$(xxd -ps -s 84 -l 20 "$i")"]="$i"
fi
done
}
for chdFile in *.chd; do
[[ $chdFile = '*.chd' ]] && echo "No chd files exist in the current directory" && break
# If first track is CHGD, extraction type should be gdi
parenthash=$(xxd -ps -s 104 -l 20 "$chdFile")
[[ $(xxd -ps -s 124 -l 4 "$chdFile") = "43484744" ]] && ext=".gdi" || ext=".cue"
game="${chdFile%.chd}"
echo "Extracting $game..."
if [[ $parenthash = "0000000000000000000000000000000000000000" ]]; then
# No parent, thus can be extracted directly
chdman extractcd -i "$chdFile" -o "$game$ext"
else
# Has parent, run getparents if the parents array is empty
[[ ${#x[@]} -eq 0 ]] && getparents
chdver=$(xxd -ps -s 12 -l 4 "$chdFile")
[[ chdver -ne 5 ]] && echo "Currently only CHD v5 is supported" && continue
# Bail if parent wasn't located
if [[ ${parents[$parenthash]} ]]; then
chdman extractcd -i "$chdFile" -ip "${parents[$parenthash]}" -o "$game$ext"
else
echo "**Cannot extract, parent was not found**"
fi
fi
echo
done
echo "All done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment