214 lines
7.3 KiB
Python
214 lines
7.3 KiB
Python
import os
|
|
import zipfile
|
|
import subprocess
|
|
import pickle
|
|
from lxml import etree
|
|
|
|
from config import debug
|
|
from utils import getMameResource
|
|
|
|
|
|
class mameROM(object):
|
|
def __init__(self):
|
|
self.name = ""
|
|
self.cloneof = None
|
|
self.description = ""
|
|
self.year = ""
|
|
self.manufacturer = ""
|
|
self.status = ""
|
|
self.category = ""
|
|
|
|
def __str__(self):
|
|
return self.description
|
|
|
|
def __repr__(self):
|
|
return '%s("%s", "%s", "%s", "%s", "%s", "%s", "%s")' % (
|
|
self.__class__.__name__,
|
|
self.name,
|
|
self.cloneof,
|
|
self.description,
|
|
self.year,
|
|
self.manufacturer,
|
|
self.status,
|
|
self.category,
|
|
)
|
|
|
|
|
|
class mameROMs(object):
|
|
def __init__(self, config, datadir):
|
|
self.all_roms = []
|
|
self.roms = []
|
|
self.all_categories = []
|
|
self.categories = []
|
|
self.catdict = {}
|
|
self.len = 0
|
|
self.cfg = config
|
|
self.data_dir = datadir
|
|
self.hide_mature = self.cfg["HideMature"]
|
|
self.emulator_dir = os.path.split(self.cfg["EXE"])[0]
|
|
|
|
self.parse()
|
|
self.filter()
|
|
|
|
self.snapdir = getMameResource(self.emulator_dir, "snap", is_dir=True)
|
|
self.marqueesdir = getMameResource(self.emulator_dir, "marquees", is_dir=True)
|
|
self.titlesdir = getMameResource(self.emulator_dir, "titles", is_dir=True)
|
|
self.videosnapsdir = getMameResource(self.emulator_dir, "videosnaps", is_dir=True)
|
|
self.info = getMameResource(self.emulator_dir, "mameinfo.dat", is_dir=False)
|
|
self.hostory = getMameResource(self.emulator_dir, "history.dat", is_dir=False)
|
|
|
|
def get_mame_version(self):
|
|
mamerun = subprocess.run([self.cfg["EXE"], "-?"], stdout=subprocess.PIPE)
|
|
output = mamerun.stdout.decode("utf-8")
|
|
output = output[output.find("v") :]
|
|
output = output[: output.find(" ")]
|
|
|
|
return output
|
|
|
|
def parse(self):
|
|
if not debug:
|
|
mame_version = self.get_mame_version()
|
|
print(mame_version)
|
|
|
|
xmlfile = os.path.join(self.emulator_dir, "mame.xml")
|
|
datfile = os.path.join(self.data_dir, "mame.dat")
|
|
|
|
if not debug:
|
|
if self.cfg["Version"] != mame_version:
|
|
self.cfg["Version"] = mame_version
|
|
|
|
if os.path.isfile(xmlfile):
|
|
os.unlink(xmlfile)
|
|
|
|
if os.path.isfile(datfile):
|
|
with open(datfile, "rb") as i:
|
|
temp_mame_version = pickle.load(i)
|
|
|
|
if debug or temp_mame_version == mame_version:
|
|
self.all_categories = pickle.load(i)
|
|
self.all_roms = pickle.load(i)
|
|
return True
|
|
|
|
os.unlink(datfile, "mame.dat")
|
|
|
|
tempcat = {}
|
|
catfile = open(os.path.join(self.emulator_dir, "catver.ini"))
|
|
|
|
found = False
|
|
for line in catfile:
|
|
line = line.strip()
|
|
if line == "[VerAdded]":
|
|
break
|
|
if line == "[Category]":
|
|
found = True
|
|
if found and line != "" and line[0] != ";" and line[0] != "[":
|
|
# zwackery=Platform / Run Jump
|
|
game, category = line.split("=")
|
|
tempcat[game] = category
|
|
if category not in self.all_categories:
|
|
self.all_categories.append(category)
|
|
self.catdict[category] = 0
|
|
|
|
self.all_categories.sort()
|
|
self.all_categories.insert(0, "All Games")
|
|
self.all_categories.append("Unknown")
|
|
self.catdict["All Games"] = 1
|
|
self.catdict["Unknown"] = 0
|
|
|
|
if not os.path.isfile(xmlfile):
|
|
try:
|
|
with open(xmlfile, "w") as out:
|
|
retcode = subprocess.call([self.cfg["EXE"], "-listxml"], stdout=out)
|
|
if retcode != 0:
|
|
try:
|
|
os.unlink(xmlfile)
|
|
except:
|
|
pass
|
|
return False
|
|
except OSError:
|
|
try:
|
|
os.unlink(xmlfile)
|
|
except:
|
|
pass
|
|
return False
|
|
|
|
with open(os.path.join(self.data_dir, "mame_exclude.txt"), "rt") as f:
|
|
exclude = f.readlines()
|
|
exclude = [x.strip() for x in exclude]
|
|
|
|
tree = etree.parse(xmlfile)
|
|
|
|
rom = None
|
|
for child in tree.getiterator():
|
|
if child.tag == "machine":
|
|
if rom:
|
|
if rom.category not in exclude:
|
|
self.all_roms.append(rom)
|
|
self.catdict[rom.category] += 1
|
|
rom = None
|
|
if "runnable" in child.attrib and child.attrib["runnable"] == "yes":
|
|
rom = mameROM()
|
|
rom.name = child.attrib["name"]
|
|
else:
|
|
rom = None
|
|
if rom and "cloneof" in child.attrib:
|
|
rom.cloneof = child.attrib["cloneof"]
|
|
if rom and rom.name in tempcat:
|
|
rom.category = tempcat[rom.name]
|
|
elif rom and rom.cloneof in tempcat:
|
|
rom.category = tempcat[rom.cloneof]
|
|
elif rom:
|
|
rom.category = "Unknown"
|
|
elif rom and child.tag == "description":
|
|
rom.description = child.text
|
|
elif rom and child.tag == "year":
|
|
rom.year = child.text
|
|
elif rom and child.tag == "manufacturer":
|
|
rom.manufacturer = child.text
|
|
elif rom and child.tag == "driver":
|
|
rom.status = child.attrib["status"]
|
|
|
|
if rom:
|
|
if rom.category not in exclude:
|
|
self.all_roms.append(rom)
|
|
|
|
for cat in self.catdict:
|
|
if self.catdict[cat] == 0:
|
|
del self.all_categories[self.all_categories.index(cat)]
|
|
|
|
with open(datfile, "wb") as output:
|
|
pickle.dump(mame_version, output)
|
|
pickle.dump(self.all_categories, output)
|
|
pickle.dump(self.all_roms, output)
|
|
|
|
return True
|
|
|
|
def filter(self):
|
|
self.roms = []
|
|
|
|
for rom in self.all_roms:
|
|
if rom.status in self.cfg["StatusFilter"]:
|
|
if self.cfg["Category"] == "All Games" or self.cfg["Category"] == rom.category:
|
|
if self.cfg["ShowClones"] or rom.cloneof is None:
|
|
if not self.hide_mature or "* Mature *" not in rom.category:
|
|
self.roms.append(rom)
|
|
|
|
if self.cfg["Sort"] == "Name Asc":
|
|
self.roms.sort(key=lambda x: x.description.lower(), reverse=False)
|
|
elif self.cfg["Sort"] == "Name Dec":
|
|
self.roms.sort(key=lambda x: x.description.lower(), reverse=True)
|
|
elif self.cfg["Sort"] == "Year Asc":
|
|
self.roms.sort(key=lambda x: x.year.lower(), reverse=False)
|
|
elif self.cfg["Sort"] == "Year Dec":
|
|
self.roms.sort(key=lambda x: x.year.lower(), reverse=True)
|
|
|
|
self.len = len(self.roms)
|
|
|
|
if self.hide_mature:
|
|
self.categories = []
|
|
for category in self.all_categories:
|
|
if "* Mature *" not in category:
|
|
self.categories.append(category)
|
|
else:
|
|
self.categories = self.all_categories
|