Exemple #1
0
class DofusSniff(object):
	def __init__(self):
		self.b = Buffer()
		self.monsters = Monsters("/home/heero/src/jeux/dofus/Dofus/share/data/common/Monsters.d2o")
		self.i18n_fr = I18nFileAccessor("/home/heero/src/jeux/dofus/Dofus/share/data/i18n/i18n_fr.d2i")

	def decode(self, string):
		self.b.addInfos(string)

		while(self.b.getSize() > self.b.getCursor()):
			try:
				self.deserialize()
			except MessageToShort:

				# Don't wait too long
				if(self.b.discardReadAndSaveUnread() >= 3):
					self.b.reset()
				break
			except UnknowMessage as exp:
				printRed("Unknow packet type: {}\n".format(exp))
				printRed("info restantes: {}\n".format(
					self.b.getSize() - self.b.getCursor()
				))

				self.b.reset()
				break
			except StopException:

				self.b.reset()
				break
			except:
				print "Exception in user code:"
				print '-'*60
				traceback.print_exc(file=sys.stdout)
				print '-'*60

				self.b.reset()
				break

	def deserialize(self):
		var1 = self.b.readUnsignedShort()
		msgId = var1>>2
		sizeLen = var1 & 0x03
	
		msgSize = 0
		if(sizeLen == 0):
			msgSize = 0
		elif(sizeLen == 1):
			msgSize = self.b.readUnsignedByte()
		elif(sizeLen == 2):
			msgSize = self.b.readUnsignedShort()
		else:
			printRed("wrong size {}\n".format(sizeLen))
			raise StopException()

		if(msgSize > self.b.getSize()):
			#printGreen("size: {}, cursor: {}\n".format(b.getSize(), b.getCursor()))
			if(sizeLen == 1):
				self.b.moveCursor(-3)
			elif(sizeLen == 2):
				self.b.moveCursor(-4)

			#printGreen("size: {}, cursor: {}\n".format(b.getSize(), b.getCursor()))
			printRed("message too short (id: {}) {} > {}\n".format(
				msgId, msgSize, self.b.getSize()
			))
			raise MessageToShort()

		if(msgId == MapComplementaryInformationsDataMessage.getProtocolId()):
			printBlue("[new map]\n")

			self.infos = MapComplementaryInformationsDataMessage()
			self.infos.deserialize(self.b)
		elif(msgId == BasicNoOperationMessage.getProtocolId()):
			self.basicBNOM = BasicNoOperationMessage()
			self.basicBNOM.deserialize(self.b)
		elif(msgId == BasicLatencyStatsRequestMessage.getProtocolId()):
			self.basicLSRM = BasicLatencyStatsRequestMessage()
			self.basicLSRM.deserialize(self.b)
		elif(msgId == NotificationListMessage.getProtocolId()):
			self.notiLM = NotificationListMessage()
			self.notiLM.deserialize(self.b)
		elif(msgId == CharacterSelectedSuccessMessage.getProtocolId()):
			self.characterSSM = CharacterSelectedSuccessMessage()
			self.characterSSM.deserialize(self.b)
		elif(msgId == InventoryContentAndPresetMessage.getProtocolId()):
			self.inventoryCAPM = InventoryContentAndPresetMessage()
			self.inventoryCAPM.deserialize(self.b)

			objs = self.inventoryCAPM.objects
			for obj in objs:
				if(obj.objectGID == 10418):
					for effect in obj.effects:
						monster = self.monsters.getObj(effect.diceConst)
						if(monster._race == 78):
							printBlue("{}\n".format(self.i18n_fr.getText(monster._nameId)))
		else:
			raise UnknowMessage(msgId)