Guest User

Untitled

a guest
May 12th, 2010
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.72 KB | None | 0 0
  1. import xbmc
  2. import os, sys, re
  3. import dbupdate, importsettings
  4. from gamedatabase import *
  5. import util
  6.  
  7. RCBHOME = os.getcwd()
  8.  
  9.  
  10. def getFilesByControl_Cached(gdb, controlName, gameId, publisherId, developerId, romCollectionId, fileTypeForControlDict, fileTypeDict, fileDict, romCollectionDict):
  11.  
  12. util.log("getFilesByControl controlName: " +controlName, util.LOG_LEVEL_DEBUG)
  13. util.log("getFilesByControl gameId: " +str(gameId), util.LOG_LEVEL_DEBUG)
  14. util.log("getFilesByControl publisherId: " +str(publisherId), util.LOG_LEVEL_DEBUG)
  15. util.log("getFilesByControl developerId: " +str(developerId), util.LOG_LEVEL_DEBUG)
  16. util.log("getFilesByControl romCollectionId: " +str(romCollectionId), util.LOG_LEVEL_DEBUG)
  17.  
  18.  
  19. #fileTypeForControlRows = FileTypeForControl(gdb).getFileTypesForControlByKey(romCollectionId, controlName)
  20. key = '%i;%s' %(romCollectionId, controlName)
  21. try:
  22. fileTypeForControlRows = fileTypeForControlDict[key]
  23. except:
  24. fileTypeForControlRows = None
  25. if(fileTypeForControlRows == None):
  26. util.log("fileTypeForControlRows == None", util.LOG_LEVEL_DEBUG)
  27. return
  28.  
  29. mediaFiles = []
  30. for fileTypeForControlRow in fileTypeForControlRows:
  31. util.log("fileTypeForControlRow: " +str(fileTypeForControlRow), util.LOG_LEVEL_DEBUG)
  32.  
  33. try:
  34. fileTypeRow = fileTypeDict[fileTypeForControlRow[4]]
  35. except:
  36. fileTypeRow = None
  37.  
  38. if(fileTypeRow == None):
  39. util.log("fileTypeRow == None in getFilesByControl", util.LOG_LEVEL_DEBUG)
  40. continue
  41.  
  42.  
  43. parentId = None
  44.  
  45. if(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_GAME):
  46. parentId = gameId
  47. elif(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_CONSOLE):
  48. #TODO cache RomCollections
  49. romCollectionRow = romCollectionDict[romCollectionId]
  50. if(romCollectionRow == None):
  51. util.log("romCollectionRow == None in getFilesByControl", util.LOG_LEVEL_DEBUG)
  52. continue
  53. consoleId = romCollectionRow[2]
  54. parentId = consoleId
  55. elif(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_PUBLISHER):
  56. parentId = publisherId
  57. elif(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_DEVELOPER):
  58. parentId = developerId
  59. elif(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_ROMCOLLECTION):
  60. parentId = romCollectionId
  61.  
  62. key = '%i;%i' %(parentId, fileTypeForControlRow[4])
  63. try:
  64. files = fileDict[key]
  65. except:
  66. files = None
  67.  
  68. if(files == None):
  69. util.log("files == None in getFilesByControl", util.LOG_LEVEL_DEBUG)
  70. continue
  71.  
  72. for file in files:
  73. mediaFiles.append(file[1])
  74.  
  75. return mediaFiles
  76.  
  77.  
  78.  
  79. """
  80. def getFilesByControl(gdb, controlName, gameId, publisherId, developerId, romCollectionId):
  81.  
  82. util.log("getFilesByControl controlName: " +controlName, util.LOG_LEVEL_DEBUG)
  83. util.log("getFilesByControl gameId: " +str(gameId), util.LOG_LEVEL_DEBUG)
  84. util.log("getFilesByControl publisherId: " +str(publisherId), util.LOG_LEVEL_DEBUG)
  85. util.log("getFilesByControl developerId: " +str(developerId), util.LOG_LEVEL_DEBUG)
  86. util.log("getFilesByControl romCollectionId: " +str(romCollectionId), util.LOG_LEVEL_DEBUG)
  87.  
  88.  
  89. fileTypeForControlRows = FileTypeForControl(gdb).getFileTypesForControlByKey(romCollectionId, controlName)
  90. if(fileTypeForControlRows == None):
  91. util.log("fileTypeForControlRows == None", util.LOG_LEVEL_WARNING)
  92. return
  93.  
  94. mediaFiles = []
  95. for fileTypeForControlRow in fileTypeForControlRows:
  96.  
  97. fileTypeRow = FileType(gdb).getObjectById(fileTypeForControlRow[4])
  98. if(fileTypeRow == None):
  99. util.log("fileTypeRow == None in getFilesByControl", util.LOG_LEVEL_WARNING)
  100. continue
  101.  
  102. parentId = None
  103.  
  104. if(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_GAME):
  105. parentId = gameId
  106. elif(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_CONSOLE):
  107. romCollectionRow = RomCollection(gdb).getObjectById(romCollectionId)
  108. if(romCollectionRow == None):
  109. util.log("romCollectionRow == None in getFilesByControl", util.LOG_LEVEL_WARNING)
  110. continue
  111. consoleId = romCollectionRow[2]
  112. parentId = consoleId
  113. elif(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_PUBLISHER):
  114. parentId = publisherId
  115. elif(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_DEVELOPER):
  116. parentId = developerId
  117. elif(fileTypeRow[util.FILETYPE_parent] == util.FILETYPEPARENT_ROMCOLLECTION):
  118. parentId = romCollectionId
  119.  
  120. files = File(gdb).getFilesByGameIdAndTypeId(parentId, fileTypeForControlRow[util.FILETYPEFORCONTROL_fileTypeId])
  121. for file in files:
  122. mediaFiles.append(file[1])
  123.  
  124. return mediaFiles
  125. """
  126.  
  127.  
  128. def launchEmu(gdb, gui, gameId):
  129. util.log("Begin helper.launchEmu", util.LOG_LEVEL_INFO)
  130.  
  131. gameRow = Game(gdb).getObjectById(gameId)
  132. if(gameRow == None):
  133. util.log("Game with id %s could not be found in database" %gameId, util.LOG_LEVEL_ERROR)
  134. return
  135.  
  136. gui.writeMsg("Launch Game " +str(gameRow[util.ROW_NAME]))
  137.  
  138. romPaths = Path(gdb).getRomPathsByRomCollectionId(gameRow[util.GAME_romCollectionId])
  139. romCollectionRow = RomCollection(gdb).getObjectById(gameRow[util.GAME_romCollectionId])
  140. if(romCollectionRow == None):
  141. util.log("Rom Collection with id %s could not be found in database" %gameRow[5], util.LOG_LEVEL_ERROR)
  142. return
  143. cmd = romCollectionRow[util.ROMCOLLECTION_emuCommandLine]
  144.  
  145. #handle multi rom scenario
  146. filenameRows = File(gdb).getRomsByGameId(gameRow[util.ROW_ID])
  147. fileindex = int(0)
  148. for fileNameRow in filenameRows:
  149. fileName = fileNameRow[util.ROW_ID]
  150. rom = ""
  151. #we could have multiple rom Paths - search for the correct one
  152. for romPath in romPaths:
  153. rom = os.path.join(romPath, fileName)
  154. if(os.path.isfile(rom)):
  155. break
  156. if(rom == ""):
  157. util.log("no rom file found for game: " +str(gameRow[1]), util.LOG_LEVEL_ERROR)
  158.  
  159. #cmd could be: uae {-%I% %ROM%}
  160. #we have to repeat the part inside the brackets and replace the %I% with the current index
  161. obIndex = cmd.find('{')
  162. cbIndex = cmd.find('}')
  163. if obIndex > -1 and cbIndex > 1:
  164. replString = cmd[obIndex+1:cbIndex]
  165. cmd = cmd.replace("{", "")
  166. cmd = cmd.replace("}", "")
  167. if fileindex == 0:
  168. if (romCollectionRow[util.ROMCOLLECTION_escapeEmuCmd] == 1):
  169. cmd = cmd.replace('%ROM%', re.escape(rom))
  170. else:
  171. cmd = cmd.replace('%ROM%', rom)
  172. cmd = cmd.replace('%I%', str(fileindex))
  173. else:
  174. newrepl = replString
  175. if (romCollectionRow[util.ROMCOLLECTION_escapeEmuCmd] == 1):
  176. newrepl = newrepl.replace('%ROM%', re.escape(rom))
  177. else:
  178. newrepl = newrepl.replace('%ROM%', rom)
  179. newrepl = newrepl.replace('%I%', str(fileindex))
  180. cmd += ' ' +newrepl
  181. fileindex += 1
  182.  
  183. if (romCollectionRow[util.ROMCOLLECTION_useEmuSolo] == 'True'):
  184. # Backup original autoexec.py
  185. autoexec = os.path.join(RCBHOME, '..', 'autoexec.py')
  186. doBackup(gdb, autoexec)
  187.  
  188. # Write new autoexec.py
  189. try:
  190. fh = open(autoexec,'w') # truncate to 0
  191. fh.write("#Rom Collection Browser autoexec\n")
  192. fh.write("import xbmc\n")
  193. fh.write("xbmc.executescript('"+ os.path.join(RCBHOME, 'default.py')+"')\n")
  194. fh.close()
  195. except Exception, (exc):
  196. util.log("Cannot write to autoexec.py: " +str(exc), util.LOG_LEVEL_ERROR)
  197. return
  198.  
  199. # Remember selection
  200. gui.saveViewState(False)
  201.  
  202. #invoke batch file that kills xbmc before launching the emulator
  203. env = ( os.environ.get( "OS", "win32" ), "win32", )[ os.environ.get( "OS", "win32" ) == "xbox" ]
  204. if(env == "win32"):
  205. #There is a problem with quotes passed as argument to windows command shell. This only works with "call"
  206. cmd = 'call \"' +os.path.join(RCBHOME, 'applaunch.bat') +'\" ' +cmd
  207. else:
  208. cmd = os.path.join(re.escape(RCBHOME), 'applaunch.sh ') +cmd
  209.  
  210. #update LaunchCount
  211. launchCount = gameRow[util.GAME_launchCount]
  212. Game(gdb).update(('launchCount',), (launchCount +1,) , gameRow[util.ROW_ID])
  213. gdb.commit()
  214.  
  215. util.log("cmd: " +cmd, util.LOG_LEVEL_INFO)
  216. xbmc.executebuiltin("XBMC.Runxbe(%s)" %cmd)
  217.  
  218. util.log("End helper.launchEmu", util.LOG_LEVEL_INFO)
  219.  
  220.  
  221. def doBackup(gdb, fName):
  222. util.log("Begin helper.doBackup", util.LOG_LEVEL_INFO)
  223.  
  224. if os.path.isfile(fName):
  225. newFileName = fName+'.bak'
  226.  
  227. if os.path.isfile(newFileName):
  228. util.log("Cannot backup autoexec.py: File exists.", util.LOG_LEVEL_ERROR)
  229. return
  230.  
  231. try:
  232. os.rename(fName, newFileName)
  233. except Exception, (exc):
  234. util.log("Cannot rename autoexec.py: " +str(exc), util.LOG_LEVEL_ERROR)
  235. return
  236.  
  237. rcbSetting = getRCBSetting()
  238. if (rcbSetting == None):
  239. util.log("rcbSetting == None in doBackup", util.LOG_LEVEL_WARNING)
  240. return
  241.  
  242. RCBSetting(gdb).update(('autoexecBackupPath',), (newFileName,), rcbSetting[util.ROW_ID])
  243. gdb.commit()
  244.  
  245. util.log("End helper.doBackup", util.LOG_LEVEL_INFO)
  246.  
  247.  
  248. def saveViewState(gdb, isOnExit, selectedView, selectedGameIndex, selectedConsoleIndex, selectedGenreIndex, selectedPublisherIndex, selectedYearIndex,
  249. selectedControlIdMainView, selectedControlIdGameInfoView):
  250.  
  251. util.log("Begin helper.saveViewState", util.LOG_LEVEL_INFO)
  252.  
  253. rcbSetting = getRCBSetting(gdb)
  254. if(rcbSetting == None):
  255. util.log("rcbSetting == None in helper.saveViewState", util.LOG_LEVEL_WARNING)
  256. return
  257.  
  258. if(isOnExit):
  259. #saveViewStateOnExit
  260. saveViewState = rcbSetting[util.RCBSETTING_saveViewStateOnExit]
  261. else:
  262. #saveViewStateOnLaunchEmu
  263. saveViewState = rcbSetting[util.RCBSETTING_saveViewStateOnLaunchEmu]
  264.  
  265.  
  266. if(saveViewState == 'True'):
  267. RCBSetting(gdb).update(('lastSelectedView', 'lastSelectedConsoleIndex', 'lastSelectedGenreIndex', 'lastSelectedPublisherIndex', 'lastSelectedYearIndex', 'lastSelectedGameIndex', 'lastFocusedControlMainView', 'lastFocusedControlGameInfoView'),
  268. (selectedView, selectedConsoleIndex, selectedGenreIndex, selectedPublisherIndex, selectedYearIndex, selectedGameIndex, selectedControlIdMainView, selectedControlIdGameInfoView), rcbSetting[0])
  269. else:
  270. RCBSetting(gdb).update(('lastSelectedView', 'lastSelectedConsoleIndex', 'lastSelectedGenreIndex', 'lastSelectedPublisherIndex', 'lastSelectedYearIndex', 'lastSelectedGameIndex', 'lastFocusedControlMainView', 'lastFocusedControlGameInfoView'),
  271. (None, None, None, None, None, None, None, None), rcbSetting[util.ROW_ID])
  272.  
  273. gdb.commit()
  274.  
  275. util.log("End helper.saveViewState", util.LOG_LEVEL_INFO)
  276.  
  277.  
  278.  
  279. def getRCBSetting(gdb):
  280. rcbSettingRows = RCBSetting(gdb).getAll()
  281. if(rcbSettingRows == None or len(rcbSettingRows) != 1):
  282. #TODO raise error
  283. return None
  284.  
  285. return rcbSettingRows[util.ROW_ID]
Add Comment
Please, Sign In to add comment