Created
March 10, 2017 09:25
-
-
Save Biendeo/d7b2c261d1310ef117f00cce53933293 to your computer and use it in GitHub Desktop.
RetroArch Manual Playlist Creator
This file contains hidden or 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
| import sys, os, glob, argparse, binascii | |
| # File needs to be in this syntax: | |
| # C:\Retroarch\ROMS\Example Game.cue | |
| # Example Game | |
| # C:\Retroarch\example_libretro.dll | |
| # Example | |
| # 00000000|crc | |
| # Example.lpl | |
| # <NEXT ONE, NO GAP> | |
| # Run with: [program] [playlist name] [playlist folder] [rom folder] [rom extension] [core path] | |
| # CONSTANTS | |
| playlistExtension = ".lpl" | |
| class FoundItem: | |
| def __init__(self, filePath: str): | |
| self.filePath = filePath | |
| self.fileName = os.path.basename(filePath) | |
| name, ext = os.path.splitext(filePath) | |
| self.fileExtension = ext[1:].lower() | |
| self.crc = "0" | |
| def PrintToFile(self, openFile, corePath: str, coreName: str, playlistName: str): | |
| openFile.write(self.filePath + "\n") | |
| openFile.write(self.fileName + "\n") | |
| openFile.write(corePath + "\n") | |
| openFile.write(coreName + "\n") | |
| openFile.write(self.crc + "|crc\n") | |
| openFile.write(playlistName + playlistExtension + "\n") | |
| def ComputeCRC(self): | |
| self.crc = CRC(self.filePath) | |
| class Program: | |
| def __init__(self, args): | |
| self.args = args | |
| self.foundFiles = [] | |
| self.playlistFile = None | |
| def Run(self): | |
| # Make sure that all the ROM extensions are lower case just to simplify dealing with them. | |
| self.args.romExtensions = [x.lower() for x in self.args.romExtensions] | |
| # Then run the main program cycle. | |
| self.OpenPlaylistFile() | |
| self.ScanAllFiles() | |
| self.PrintAllFiles() | |
| self.ClosePlaylistFile() | |
| def OpenPlaylistFile(self): | |
| self.playlistFile = open(self.args.playlistFolder + "/" + self.args.playlistName + playlistExtension, mode="w") | |
| def ClosePlaylistFile(self): | |
| self.playlistFile.close() | |
| def ScanAllFiles(self): | |
| pathsToCheck = [] | |
| for argPath in self.args.romFolders: | |
| pathsToCheck.append(argPath) | |
| while len(pathsToCheck) > 0: | |
| currentPath = pathsToCheck.pop(0) | |
| for file in glob.glob(currentPath + "/*"): | |
| if not self.args.quiet: | |
| print("Scanning {}: {:50}".format(len(self.foundFiles), file)) | |
| try: | |
| if os.path.isdir(file) and self.args.recursive: | |
| pathsToCheck.append(file) | |
| elif os.path.isdir(file): | |
| pass | |
| else: | |
| newFoundItem = FoundItem(file) | |
| if newFoundItem.fileExtension not in self.args.romExtensions: | |
| print("Apparently didn't think " + newFoundItem.fileExtension + " was in " + str(self.args.romExtensions)) | |
| newFoundItem.ComputeCRC() | |
| self.foundFiles.append(FoundItem(file)) | |
| except: | |
| pass | |
| def PrintAllFiles(self): | |
| if not self.args.unordered: | |
| self.foundFiles.sort(key=lambda x: x.fileName) | |
| for foundFile in self.foundFiles: | |
| if not self.args.quiet: | |
| print("Writing: {:50}".format(foundFile.fileName)) | |
| foundFile.PrintToFile(self.playlistFile, self.args.corePath + "\\" + self.args.coreFileName, self.args.coreName, self.args.playlistName) | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("-f", required=True, type=str, dest="playlistFolder", help="the path to Retroarch's playlist folder") | |
| parser.add_argument("-c", required=True, type=str, dest="corePath", help="the path to Retroarch's core folder") | |
| parser.add_argument("-o", required=True, type=str, dest="coreFileName", help="the file name of Retroarch's core that will run these games") | |
| parser.add_argument("-b", required=True, type=str, dest="coreName", help="the display name of the Retroarch core") | |
| parser.add_argument("-n", required=True, type=str, dest="playlistName", help="the name of the new playlist") | |
| parser.add_argument("-r", nargs="+", required=True, type=str, dest="romFolders", help="the path to the ROM folder (can be multiple)") | |
| parser.add_argument("-x", nargs="+", required=True, type=str, dest="romExtensions", help="the extension of the ROM files (can be multiple, ROMs with same name will prefer the first one)") | |
| parser.add_argument("-R", action="store_true", required=False, dest="recursive", default=False, help="runs the program recursive through all folders given") | |
| parser.add_argument("-q", action="store_true", required=False, dest="quiet", default=False, help="suppresses output of this program") | |
| parser.add_argument("-u", action="store_true", required=False, dest="unordered", default=False, help="adds the files to the playlist as they're detected, rather than in alphabetical order") | |
| args = parser.parse_args() | |
| if not args.quiet: | |
| print(args) | |
| p = Program(args) | |
| p.Run() | |
| # http://stackoverflow.com/questions/1742866/compute-crc-of-file-in-python | |
| # If you're running 32-bit Python, this will not work for files larger than 800MB. Try running in 64-bit. | |
| def CRC(filePath: str): | |
| buf = open(filePath,'rb').read() | |
| buf = (binascii.crc32(buf) & 0xFFFFFFFF) | |
| return "%08X" % buf | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment