Last active
July 27, 2018 20:17
-
-
Save possatti/b4cf5ee36d5cc1970d147c48167e415f to your computer and use it in GitHub Desktop.
Use a DAT file to scan a ROM folder and create a RetroArch playlist. #MAME
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function, division | |
import xml.etree.ElementTree as ET | |
import argparse | |
import sys | |
import os | |
def parse_args(): | |
# Arguments and options. | |
parser = argparse.ArgumentParser(description=''' | |
Use a DAT file to scan a ROM folder and create a RetroArch playlist. | |
''') | |
parser.add_argument('datfile') | |
parser.add_argument('romdir') # FIXME: Possibly allow for scanning multiple directories at once? | |
# parser.add_argument('-j', '--jobs', type=int, , default=1, help='Use `n` parallel jobs (default 1).') # TODO | |
parser.add_argument('-s', '--save-path', help='Path to where the playlist should be saved to. Otherwise it will just print to stdout.') | |
parser.add_argument('-n', '--playlist-name', help='Playlist name (Default: `MAME`, or infer from `--save-path`).', default='MAME') | |
parser.add_argument('--core-path', help='Path to the desired core. Leave blank for `DETECT` (recommended).', default='DETECT') | |
parser.add_argument('--core-name', help='Name of the desired core. Leave blank for `DETECT` (recommended).', default='DETECT') | |
parser.add_argument('-B', '--include-bios', dest='include_bios', action='store_true') | |
parser.add_argument('-b', '--ignore-bios', dest='include_bios', action='store_false') | |
parser.set_defaults(include_bios=False) | |
args = parser.parse_args() | |
# Verify paths exist. | |
if not os.path.isfile(args.datfile): | |
raise IOError('Could not find `.dat` on `{}`, file does not exist.'.format(args.datfile)) | |
if not os.path.isdir(args.romdir): | |
raise IOError('Could not find ROM\'s directory on `{}`, directory does not exist.'.format(args.romdir)) | |
if args.core_path is not 'DETECT': | |
if not os.path.isfile(args.core_path): | |
raise IOError('Could not find core on `{}`, file does not exist.'.format(args.core_path)) | |
if args.save_path is not None: | |
args.playlist_name = os.path.splitext(os.path.basename(args.save_path))[0] | |
save_dir_path = os.path.dirname(args.save_path) | |
if save_dir_path != '' and not os.path.isdir(save_dir_path): | |
raise IOError('The directory where you are trying to save does not exist: `{}`.'.format(save_dir_path)) | |
# TODO: Possibly prompt the user to create the directories: | |
# if not os.path.isdir(save_dir_path): | |
# os.makedirs(save_dir_path) | |
return args | |
def main(): | |
args = parse_args() | |
dat_tree = ET.parse(args.datfile) | |
dat_root = dat_tree.getroot() | |
playlist_lines = [] | |
# Scan the ROM's directory. | |
for root, dirs, files in os.walk(args.romdir): | |
for filename in files: | |
rom_name, extension = os.path.splitext(filename) | |
# FIXME: Maybe there are ROM sets that don't use the zip extension? I am not aware of this. | |
if extension == '.zip': | |
game_node = dat_root.find(".//game[@name='{}']".format(rom_name)) | |
isbios = game_node.get('isbios') == 'yes' | |
if args.include_bios or not isbios: | |
# Get game info. | |
filepath = os.path.join(root, filename) | |
game_name = game_node.find('description').text | |
game_crc = '00000000' # TODO: Calculate crc. | |
# Include on playlist. | |
playlist_lines.append(filepath+'\n') | |
playlist_lines.append(game_name+'\n') | |
playlist_lines.append(args.core_path+'\n') | |
playlist_lines.append(args.core_name+'\n') | |
playlist_lines.append(game_crc + '|crc'+'\n') | |
playlist_lines.append(args.playlist_name + '.lpl'+'\n') | |
if args.save_path is None: | |
sys.stdout.writelines(playlist_lines) | |
else: | |
with open(args.save_path, 'w') as f: | |
f.writelines(playlist_lines) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I created this python script to generate MAME playlists on Retroarch from DAT files. But then I noticed Retroarch scans the most common ROM sets nowadays (MAME 0.37b5, 0.78, 0.139, 0.159).
This script, different from MamePlaylistBuilder, it ignores bios files (by default), so they won't be listed as games. The script will probably run slower than MamePlaylistBuilder, but if you don't have too many roms, that won't be a big deal.