Last active
January 12, 2021 17:49
-
-
Save dvessel/cb9d5e80824e01dcbeda070ea343e0c6 to your computer and use it in GitHub Desktop.
Manage MAME files for OpenEmu Arcade core. Make it executable and unquarantine: `chmod +x oeadvscan && xattr -d com.apple.quarantine oeadvscan`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# | |
# For use on MAME files managed by OpenEmu. | |
# advscan roms/disks/samples verifies and repairs. | |
# | |
# All options apply to roms, disks (chd) and samples. | |
# Options: | |
# | |
# scan : Generates a report specific to your collection. (default) | |
# scan-verbose : Massive report dump includes everything supported by MAME. | |
# repair : Repairs roms/disks/samples. | |
# repair-preview : Safe preview of changes. | |
# | |
# Or pass in arguments for advscan minus the xml info file. | |
# Preset text commands and pass-through arguments cannot be combined. | |
# See 'advscan --help' or 'man advscan'." | |
# | |
if type advscan &>/dev/null; then | |
command=${@:-scan} | |
base_dir=$HOME/Library/Application\ Support/OpenEmu | |
if [[ -d $base_dir/Cores/MAME.oecoreplugin ]]; then | |
version=${${${:-`defaults read \ | |
$base_dir/Cores/MAME.oecoreplugin/Contents/Info.plist CFBundleVersion` | |
}/./}%%.*} | |
else | |
echo "Could not determin MAME core version. Install it through OpenEmu's core preferences." | |
exit 1 | |
fi | |
# Go back to current directory when done. | |
# advscan can't resolve paths when run outside advscan.rc. | |
trap 'cd "`pwd`"' INT TERM EXIT | |
if [[ ! -e $base_dir/AdvanceScan ]]; then | |
echo "Setting up AdvanceScan...\n" | |
mkdir -p $base_dir/AdvanceScan && cd "$_" && open . | |
mkdir -p new _import _unknown/roms _unknown/disks _unknown/samples ../MAME/samples | |
ln -s ../Game\ Library/roms/Arcade roms && ln -s ../MAME/samples samples | |
cat <<-EOF > advscan.rc | |
rom_import _import | |
rom_new new | |
rom roms:new | |
# Disk paths will be overwritten. | |
# Subfolders will be added automatically so advscan can see chd files. | |
disk roms | |
sample samples | |
rom_unknown _unknown/roms | |
disk_unknown _unknown/disks | |
sample_unknown _unknown/samples | |
EOF | |
else | |
cd $base_dir/AdvanceScan | |
fi | |
if [[ ! -f mame${version}.xml ]]; then | |
if [[ ! -f $TMPDIR/mame${version}lx.zip ]]; then | |
curl -Lo $TMPDIR/mame${version}lx.zip --fail \ | |
https://github.com/mamedev/mame/releases/download/mame${version}/mame${version}lx.zip | |
curl -Lo mame${version}-whatsnew.txt --fail \ | |
https://github.com/mamedev/mame/releases/download/mame${version}/whatsnew_${version}.txt | |
fi | |
unzip $TMPDIR/mame${version}lx.zip -d ./ | |
fi | |
# Add each sub-directory to the .rc file so it can see them. | |
sed -i '' -E "s|^(disk ).*|\1roms:`find roms/* -type d | tr '\n' ':'`|" advscan.rc | |
# Color key: | |
# duplicate : purple | |
# preliminary : cyan | |
# bad or incomplete : red | |
# missing : yellow | |
# various others : gray | |
colors='/^(?:\+ )?' | |
colors+='(?:[a-z]+_){0,2}(good|bad|miss|dup|text|binary|garbage) |' | |
colors+='Rom.*(preliminary|bad|incomplete|missing) |' | |
colors+='(total|Missing|-)[_ ]' | |
colors+='.*/; print ( | |
$1 eq dup ? "\033[0;35m" : | |
$2 eq preliminary ? "\033[0;36m" : | |
($1 eq bad or | |
$2 eq bad or | |
$2 eq incomplete) ? "\033[0;31m" : | |
($1 eq miss or | |
$2 eq missing or | |
$3 eq Missing) ? "\033[0;33m" : | |
($1 eq text or | |
$1 eq binary or | |
$1 eq garbage or | |
$3 eq total or | |
$3 eq "-") ? "\033[0;37m" : | |
"\033[0m" | |
)' | |
if [[ $command == scan ]]; then | |
echo "Scanning... ver.$version\n" | |
# Ignore 'sampleof' errors and clean excessive output. | |
# Ignore missing files since most users won't have a complete set in their OpenEmu library. | |
# List the good, bad, misreported missing files and supplementary info. | |
advscan -rkspP < mame${version}.xml 2> >(grep -v sampleof >&2) | | |
perl -ne 'print if ! (/^game_(rom|sample|disk)_(good|bad|miss|dup) ([a-z0-9_]*) .*\n/ && | |
( ! -e "$1s/$3.zip" && ! -e "new/$3.zip" && ! -d "roms/$3" ))' | perl -pe $colors | |
elif [[ $command == scan-verbose ]]; then | |
echo "Scanning (verbose)... ver.$version\n" | |
advscan -rksvpP < mame${version}.xml 2> >(grep -v sampleof >&2) | | |
perl -pe 'print /^game_(rom|sample|disk)_(good|bad|miss|dup) ([a-z0-9_]*) .*/ && | |
( -e "$1s/$3.zip" || -e "new/$3.zip" || -d "roms/$3" ) ? "+ " : /^game_.*/ ? "- " : ""' | perl -pe $colors | |
elif [[ $command == repair ]]; then | |
echo "Repairing... ver.$version\n" | |
advscan -rksbdutgP < mame${version}.xml 2> >(grep -v sampleof >&2) | perl -pe $colors | |
elif [[ $command == repair-preview ]]; then | |
echo "Repairing (preview)... ver.$version\n" | |
advscan -rksbdutgnP < mame${version}.xml 2> >(grep -v sampleof >&2) | perl -pe $colors | |
elif [[ $command == -* ]]; then | |
echo "advscan $command ... ver.$version\n" | |
advscan $command < mame${version}.xml 2> >(grep -v sampleof >&2) | perl -pe $colors | |
else | |
echo "Use one of the following: | |
scan : Generates a report specific to your collection. (default) | |
scan-verbose : Massive report dump includes everything supported by MAME. | |
repair : Repairs roms/disks/samples. | |
repair-preview : Safe preview of changes. | |
Or pass in arguments for advscan minus the xml info file. | |
Preset text commands and pass-through arguments cannot be combined. | |
See 'advscan --help' or 'man advscan'.\n" | |
exit 1 | |
fi | |
else | |
echo "advscan is not available. Install 'advancescan' through homebrew." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
more info