Skip to content

Instantly share code, notes, and snippets.

@badvision
Created September 4, 2017 06:36
Show Gist options
  • Save badvision/039c2d089e46295e8d08da572c73c793 to your computer and use it in GitHub Desktop.
Save badvision/039c2d089e46295e8d08da572c73c793 to your computer and use it in GitHub Desktop.
Update RetroArch playlist files with missing roms
import java.util.zip.*
// Update these two lines to match the location of your playlists and what extensions to skip
playlistDir = "C:\\Users\\brobert\\AppData\\Roaming\\RetroArch\\playlists" as File
ignoreExtensions="cht|clt|sav|dtc|txt|html"
def appendPlaylist(playlistFile, path) {
println("Appending ${path}")
// Mac | Linux users change this to forward slash, I could have used the path separator property but I was lazy.
name = path.substring(path.lastIndexOf('\\')+1)
playlistFile.append("""${path}
${name}
DETECT
DETECT
0|crc
${playlistFile.name}
""")
}
playlistDir.eachFile{playlistFile ->
println "Playlist file ${playlistFile.name}"
entries = [:]
counter = 0
entry = {}
playlistFile.eachLine{line ->
switch (counter) {
case 0:
entry = {}
path = line.contains("#") ? line.substring(0, line.indexOf('#')) : line
entry.path = path
break
case 1:
entry.name = line
break
case 4:
entry.crc = line
entries[entry.path]=entry
break
}
counter = (counter + 1) % 6
}
println "${entries.size()} entries found in file"
firstEntry = entries.values().toArray()[0]
romFile = firstEntry.path as File
romDirectory = romFile.parentFile
println "Detected folder is ${romDirectory.path}"
missing = 0
present = 0
romDirectory.eachFile{rom ->
if (rom.isDirectory()) return
ext = rom.path.substring(rom.path.lastIndexOf('.')+1).toLowerCase()
if (ignoreExtensions.contains(ext)) return
if (entries[rom.path] == null) {
missing++
if (ext.equals("zip")) {
// ZIP contents
ZipFile file = new ZipFile(rom)
file.entries().each { entry ->
name = entry.name.toLowerCase()
if (!name.endsWith("html") && !name.endsWith("txt") && !name.endsWith("me")) {
appendPlaylist(playlistFile, "${rom.path}#${entry.name}")
}
}
} else {
// Basic rom path
appendPlaylist(playlistFile, "${rom.path}")
}
} else {
present++
}
}
println "Directory scan found ${missing+present} files, ${present} are in playlist, ${missing} are missing from playlist"
}
@badvision
Copy link
Author

Highly recommended: Backup your playlist files first and hand-edit them when this is done to ensure everything looks right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment