cheatfinder.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # $Id$
  2. from PyQt4 import QtCore, QtGui
  3. from PyQt4.QtGui import QColor
  4. from qt_utils import connect
  5. class Cheatfinder(object):
  6. def __init__(self, bridge):
  7. self.__cfDialog = None
  8. self.__ui = None
  9. self.__bridge = bridge
  10. def show(self):
  11. dialog = self.__cfDialog
  12. if dialog is None:
  13. self.__cfDialog = dialog = QtGui.QDialog(
  14. None, # TODO: find a way to get the real parent
  15. QtCore.Qt.Dialog
  16. | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowSystemMenuHint
  17. )
  18. # Setup UI made in Qt Designer.
  19. from ui_cheatfinder import Ui_cheatFinder
  20. ui = Ui_cheatFinder()
  21. ui.setupUi(dialog)
  22. self.__ui = ui
  23. # Connect signals.
  24. connect(ui.FindCheatLess, 'clicked()', self.__findCheatLess)
  25. connect(ui.FindCheatLessEqual, 'clicked()',
  26. self.__findCheatLessEqual)
  27. connect(ui.FindCheatEqual, 'clicked()', self.__findCheatEqual)
  28. connect(ui.FindCheatNotEqual, 'clicked()', self.__findCheatNotEqual)
  29. connect(ui.FindCheatMoreEqual, 'clicked()',
  30. self.__findCheatMoreEqual)
  31. connect(ui.FindCheatMore, 'clicked()', self.__findCheatMore)
  32. connect(ui.FindCheatRestart, 'clicked()', self.__findCheatRestart)
  33. connect(ui.FindCheatValue, 'clicked()', self.__findCheatValue)
  34. connect(ui.EmulationTogglePause, 'clicked()',
  35. self.__emulationTogglePause)
  36. connect(ui.EmulationReset, 'clicked()', self.__emulationReset)
  37. connect(ui.rbCompare, 'clicked()', self.__disableDirectSearch)
  38. connect(ui.rbSearch, 'clicked()', self.__disableCompareSearch)
  39. dialog.show()
  40. dialog.raise_()
  41. dialog.activateWindow()
  42. self.__ui.CheatTable.verticalHeader().hide()
  43. self.__ui.CheatTable.horizontalHeader().setResizeMode(
  44. QtGui.QHeaderView.Stretch
  45. )
  46. def __disableDirectSearch(self):
  47. self.__ui.FindCheatValue.setEnabled(False)
  48. self.__ui.cheatVal.setEnabled(False)
  49. self.__ui.FindCheatLess.setEnabled(True)
  50. self.__ui.FindCheatLessEqual.setEnabled(True)
  51. self.__ui.FindCheatNotEqual.setEnabled(True)
  52. self.__ui.FindCheatEqual.setEnabled(True)
  53. self.__ui.FindCheatMoreEqual.setEnabled(True)
  54. self.__ui.FindCheatMore.setEnabled(True)
  55. def __disableCompareSearch(self):
  56. self.__ui.FindCheatValue.setEnabled(True)
  57. self.__ui.cheatVal.setEnabled(True)
  58. self.__ui.FindCheatLess.setEnabled(False)
  59. self.__ui.FindCheatLessEqual.setEnabled(False)
  60. self.__ui.FindCheatNotEqual.setEnabled(False)
  61. self.__ui.FindCheatEqual.setEnabled(False)
  62. self.__ui.FindCheatMoreEqual.setEnabled(False)
  63. self.__ui.FindCheatMore.setEnabled(False)
  64. def __emulationTogglePause(self):
  65. self.__bridge.command('toggle', 'pause')()
  66. def __emulationReset(self):
  67. self.__ui.FindCheatRestart.setEnabled(False)
  68. self.__bridge.command('reset')(self.__cheatListReply)
  69. def __findCheatLess(self):
  70. self.__ui.FindCheatRestart.setEnabled(False)
  71. self.__ui.cheatResults.append('Less')
  72. self.__bridge.command('findcheat', 'less')(self.__cheatListReply)
  73. def __findCheatLessEqual(self):
  74. self.__ui.FindCheatRestart.setEnabled(False)
  75. self.__ui.cheatResults.append('Less Or Equal')
  76. self.__bridge.command('findcheat', 'loe')(self.__cheatListReply)
  77. def __findCheatEqual(self):
  78. self.__ui.FindCheatRestart.setEnabled(False)
  79. self.__ui.cheatResults.append('Equal')
  80. self.__bridge.command('findcheat', 'equal')(self.__cheatListReply)
  81. def __findCheatNotEqual(self):
  82. self.__ui.FindCheatRestart.setEnabled(False)
  83. self.__ui.cheatResults.append('Search Not Equal')
  84. self.__bridge.command('findcheat', 'notequal')(self.__cheatListReply)
  85. def __findCheatMoreEqual(self):
  86. self.__ui.FindCheatRestart.setEnabled(False)
  87. self.__ui.cheatResults.append('Search More Or Equal')
  88. self.__bridge.command('findcheat', 'moe')(self.__cheatListReply)
  89. def __findCheatMore(self):
  90. self.__ui.FindCheatRestart.setEnabled(False)
  91. self.__ui.cheatResults.append('Search More')
  92. self.__bridge.command('findcheat', 'more')(self.__cheatListReply)
  93. def __findCheatRestart(self):
  94. cheatValue = self.__ui.cheatVal.text()
  95. self.__ui.FindCheatRestart.setEnabled(False)
  96. if len(cheatValue)<1:
  97. msgText = 'Start New Search:'
  98. else:
  99. msgText = 'Start New Search Equal To: ' + str(cheatValue)
  100. self.__ui.cheatResults.append(msgText)
  101. self.__bridge.command('findcheat', '-start', cheatValue)(
  102. self.__cheatListReply
  103. )
  104. def __findCheatValue(self):
  105. cheatValue = self.__ui.cheatVal.text()
  106. self.__ui.cheatResults.append('Searching For: ' + str(cheatValue))
  107. self.__bridge.command('findcheat', cheatValue)(self.__cheatListReply)
  108. def __cheatListReply(self, *words):
  109. line = ' '.join(words)
  110. text = self.__ui.cheatResults
  111. color = QColor()
  112. color.setRgb(0, 0, 255)
  113. text.setTextColor(color)
  114. # Check if no results are found (clear table and display message).
  115. if line == 'No results left':
  116. color = QColor()
  117. color.setRgb(255, 0, 0)
  118. text.setTextColor(color)
  119. text.append(line)
  120. else:
  121. if line.find('results') > 1:
  122. text.append(line)
  123. # Check if results are found.
  124. if line.find('results') < 1:
  125. # Format output to be put into an array.
  126. line = line.replace('->', ' ')
  127. line = line.replace(':', ' ')
  128. line = line.replace(' ', ' ')
  129. line = line.replace(' ', ';')
  130. line = line.replace(';;', ';')
  131. # Put resultset into array.
  132. cheatArray = line.split('\n')
  133. # Create the table to be filled, disable sorting and set grid size.
  134. self.__ui.CheatTable.setRowCount( len(cheatArray) - 1 )
  135. self.__ui.CheatTable.setSortingEnabled(0)
  136. self.__ui.CheatTable.verticalHeader().setResizeMode(
  137. QtGui.QHeaderView.ResizeToContents
  138. )
  139. row = 0
  140. for cheatLine in cheatArray[ : -1]:
  141. # Create sub array.
  142. cheatVal = cheatLine.split(';')
  143. # Fill address value item.
  144. addrItem = QtGui.QTableWidgetItem(cheatVal[0])
  145. addrItem.setFlags(
  146. QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
  147. )
  148. self.__ui.CheatTable.setItem(row, 0, addrItem)
  149. # Fill old value item.
  150. oldValItem = QtGui.QTableWidgetItem(
  151. cheatVal[1] + ' / ' + hex(int(cheatVal[1]))
  152. )
  153. oldValItem.setFlags(
  154. QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
  155. )
  156. self.__ui.CheatTable.setItem(row, 1, oldValItem)
  157. # Fill new value item.
  158. newValItem = QtGui.QTableWidgetItem(
  159. cheatVal[2] + ' / ' + hex(int(cheatVal[2]))
  160. )
  161. newValItem.setFlags(
  162. QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
  163. )
  164. self.__ui.CheatTable.setItem(row, 2, newValItem)
  165. row += 1
  166. text.append(str(row) + ' results found -> Displayed in table')
  167. # Enable sorting.
  168. self.__ui.CheatTable.setSortingEnabled(1)
  169. # Enable search button again.
  170. color = QColor()
  171. color.setRgb(0, 0, 0)
  172. text.setTextColor(color)
  173. self.__ui.FindCheatRestart.setEnabled(True)