actions.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. class Action(object):
  2. def __init__(self, code, length):
  3. self._code = code
  4. self._length = length
  5. @property
  6. def code(self):
  7. return self._code
  8. @property
  9. def length(self):
  10. return self._length;
  11. @property
  12. def version(self):
  13. return 3
  14. def parse(self, data):
  15. # Do nothing. Many Actions don't have a payload.
  16. # For the ones that have one we override this method.
  17. pass
  18. def __repr__(self):
  19. return "[Action] Code: 0x%x, Length: %d" % (self._code, self._length)
  20. class ActionUnknown(Action):
  21. ''' Dummy class to read unknown actions '''
  22. def __init__(self, code, length):
  23. super(ActionUnknown, self).__init__(code, length)
  24. def parse(self, data):
  25. if self._length > 0:
  26. #print "skipping %d bytes..." % self._length
  27. data.skip_bytes(self._length)
  28. def __repr__(self):
  29. return "[ActionUnknown] Code: 0x%x, Length: %d" % (self._code, self._length)
  30. class Action4(Action):
  31. ''' Base class for SWF 4 actions '''
  32. def __init__(self, code, length):
  33. super(Action4, self).__init__(code, length)
  34. @property
  35. def version(self):
  36. return 4
  37. class Action5(Action):
  38. ''' Base class for SWF 5 actions '''
  39. def __init__(self, code, length):
  40. super(Action5, self).__init__(code, length)
  41. @property
  42. def version(self):
  43. return 5
  44. class Action6(Action):
  45. ''' Base class for SWF 6 actions '''
  46. def __init__(self, code, length):
  47. super(Action6, self).__init__(code, length)
  48. @property
  49. def version(self):
  50. return 6
  51. class Action7(Action):
  52. ''' Base class for SWF 7 actions '''
  53. def __init__(self, code, length):
  54. super(Action7, self).__init__(code, length)
  55. @property
  56. def version(self):
  57. return 7
  58. # =========================================================
  59. # SWF 3 actions
  60. # =========================================================
  61. class ActionGetURL(Action):
  62. CODE = 0x83
  63. def __init__(self, code, length):
  64. self.urlString = None
  65. self.targetString = None
  66. super(ActionGetURL, self).__init__(code, length)
  67. def parse(self, data):
  68. self.urlString = data.readString()
  69. self.targetString = data.readString()
  70. class ActionGotoFrame(Action):
  71. CODE = 0x81
  72. def __init__(self, code, length):
  73. self.frame = 0
  74. super(ActionGotoFrame, self).__init__(code, length)
  75. def parse(self, data):
  76. self.frame = data.readUI16()
  77. class ActionGotoLabel(Action):
  78. CODE = 0x8c
  79. def __init__(self, code, length):
  80. self.label = None
  81. super(ActionGotoLabel, self).__init__(code, length)
  82. def parse(self, data):
  83. self.label = data.readString()
  84. class ActionNextFrame(Action):
  85. CODE = 0x04
  86. def __init__(self, code, length):
  87. super(ActionNextFrame, self).__init__(code, length)
  88. class ActionPlay(Action):
  89. CODE = 0x06
  90. def __init__(self, code, length):
  91. super(ActionPlay, self).__init__(code, length)
  92. def __repr__(self):
  93. return "[ActionPlay] Code: 0x%x, Length: %d" % (self._code, self._length)
  94. class ActionPreviousFrame(Action):
  95. CODE = 0x05
  96. def __init__(self, code, length):
  97. super(ActionPreviousFrame, self).__init__(code, length)
  98. class ActionSetTarget(Action):
  99. CODE = 0x8b
  100. def __init__(self, code, length):
  101. self.targetName = None
  102. super(ActionSetTarget, self).__init__(code, length)
  103. def parse(self, data):
  104. self.targetName = data.readString()
  105. class ActionStop(Action):
  106. CODE = 0x07
  107. def __init__(self, code, length):
  108. super(ActionStop, self).__init__(code, length)
  109. def __repr__(self):
  110. return "[ActionStop] Code: 0x%x, Length: %d" % (self._code, self._length)
  111. class ActionStopSounds(Action):
  112. CODE = 0x09
  113. def __init__(self, code, length):
  114. super(ActionStopSounds, self).__init__(code, length)
  115. class ActionToggleQuality(Action):
  116. CODE = 0x08
  117. def __init__(self, code, length):
  118. super(ActionToggleQuality, self).__init__(code, length)
  119. class ActionWaitForFrame(Action):
  120. CODE = 0x8a
  121. def __init__(self, code, length):
  122. self.frame = 0
  123. self.skipCount = 0
  124. super(ActionWaitForFrame, self).__init__(code, length)
  125. def parse(self, data):
  126. self.frame = data.readUI16()
  127. self.skipCount = data.readUI8()
  128. # =========================================================
  129. # SWF 4 actions
  130. # =========================================================
  131. class ActionAdd(Action4):
  132. CODE = 0x0a
  133. def __init__(self, code, length):
  134. super(ActionAdd, self).__init__(code, length)
  135. class ActionAnd(Action4):
  136. CODE = 0x10
  137. def __init__(self, code, length):
  138. super(ActionAnd, self).__init__(code, length)
  139. # urgh! some 100 to go...
  140. ActionTable = {}
  141. for name, value in dict(locals()).items():
  142. if type(value) == type and issubclass(value, Action) and hasattr(value, 'CODE'):
  143. ActionTable[value.CODE] = value
  144. class SWFActionFactory(object):
  145. @classmethod
  146. def create(cls, code, length):
  147. return ActionTable.get(code, ActionUnknown)(code, length)