Advertisement
Guest User

Untitled

a guest
Jul 1st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #!/usr/lib/python2.7/
  2. # -*- coding: utf-8 -*-
  3. import xml.etree.ElementTree as etree
  4. import sys,os,glob,collections
  5. from random import randrange
  6.  
  7. cfgInDir = '/recalbox/share_init/system/.emulationstation/es_systems.cfg'
  8. romsInDir = '/recalbox/share/roms'
  9. logDir= '/recalbox/share/'
  10. exclusionList = ['random','favorites','moonlight','imageviewer']
  11.  
  12. System = collections.namedtuple('System', 'name command games')
  13. Game = collections.namedtuple('Game', 'path name image emulator core')
  14.  
  15. def get(i,e):
  16. ll=i.find(e)
  17. return ll.text if ll != None else None
  18.  
  19. # gets systems list
  20. def systemList(p):
  21. systems=[]
  22. for i in etree.parse(p).findall(".//system"):
  23. system,command=get(i,"name"),get(i,"command")
  24. gamelistFile = os.path.join(romsInDir,system,"gamelist.xml")
  25. if (os.path.exists(gamelistFile) and system not in exclusionList ):
  26. s = System(system,command,[])
  27. systems.append(s)
  28.  
  29. return systems
  30.  
  31. # selects a game and launches command
  32. def selectGame(syst,ctrlStr):
  33. game = syst.games[randrange(len(syst.games))]
  34. core = game.core if game.core != None else 'default'
  35. emulator = game.emulator if game.emulator != None else 'default'
  36. log("Selected %s from system %s with core %s and emulator %s" % (game.name, syst.name, core, emulator))
  37. command = syst.command.replace('%SYSTEM%',syst.name)
  38. command = command.replace('%ROM%','"' + game.path +'"')
  39. command = command.replace('%EMULATOR%',emulator)
  40. command = command.replace('%CORE%',core)
  41. command = command.replace('%CONTROLLERSCONFIG%',ctrlStr)
  42. command = command.replace('%NETPLAY%','')
  43. log("out: "+command)
  44. os.popen(command)
  45.  
  46. # randomly selects a valid system (with games)
  47. def selectSystem(systems,syst):
  48. gamelistFile = os.path.join(romsInDir,syst.name,"gamelist.xml")
  49. try :
  50. parser = etree.XMLParser(encoding="utf-8")
  51. games = etree.parse(gamelistFile, parser=parser).findall(".//game")
  52. if (len(games) > 0) : # remove systems with no games
  53. gs = []
  54. for g in games:
  55. hidden = get(g,'hidden')
  56. gpath = os.path.join(romsInDir, syst.name , get(g,'path').encode('utf-8')) #full path to rom
  57. if (hidden != 'true' and os.path.exists(gpath)): #do not use hidden games and nonexistent files
  58. gs.append(Game(gpath,get(g,'name'),get(g,'image'),get(g,'emulator'),get(g,'core')))
  59.  
  60. if (len(gs) > 0):
  61. syst = System(syst.name,syst.command,gs)
  62. log("%i games selected in %s directory" % (len(syst.games),syst.name))
  63. return syst
  64. else :
  65. print(" ")
  66. except :
  67. print(sys.exc_info())
  68. if len(syst.games) > 0 :
  69. return syst
  70. else :
  71. log("no games in %s directory" % syst.name)
  72. return selectSystem(systems,systems[randrange(len(systems))])#unvalid system, reselect
  73.  
  74. # manages spaces in controllers names
  75. def parseControllerCfg(arg):
  76. for i in range(1,6):
  77. pname = "-p" + str(i) + "name"
  78. try:
  79. pindex = arg.index(pname)
  80. arg[pindex+1] = '"' + arg[pindex+1] + '"'
  81. except ValueError:
  82. print ("%s not in args list" %pname)
  83.  
  84. return " ".join(arg)
  85.  
  86. def log(stri):
  87. print(stri)
  88. f = open(logDir + "randomlog.csv","a+")
  89. f.write(stri +"\n")
  90. f.close()
  91.  
  92. # get a system by name
  93. def getSystem(systems, param):
  94. name=os.path.splitext(os.path.split(param)[1])[0]
  95. for s in systems:
  96. if s.name==name: return s
  97.  
  98. if __name__ == "__main__":
  99. log("---------")
  100. log("in: "+ " ".join(sys.argv))
  101. ctrlStr = parseControllerCfg(sys.argv[1:len(sys.argv)-2])
  102. systems = systemList(cfgInDir)
  103. paramSystem = getSystem(systems,sys.argv[-1])
  104. launchSyst = paramSystem if paramSystem != None else systems[randrange(len(systems))]
  105. ssystem = selectSystem(systems,launchSyst)
  106. selectGame(ssystem,ctrlStr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement