def Binary(name): """ Load a world from a binary file. This is the world format used in transit from server to client, and used in the client's world cache. Positional arguments: name : File or URI containing the binary world """ from BZFlag.Protocol import Common blockDict = Common.getMessageDict(WorldObjects) f = Util.autoFile(name) w = World() w.erase() while 1: # Read the block header header = WorldObjects.BlockHeader() packedHeader = f.read(header.getSize()) if len(packedHeader) < header.getSize(): raise Errors.ProtocolError("Premature end of binary world data") header.unmarshall(packedHeader) # Look up the block type and instantiate it try: block = blockDict[header.id]() except KeyError: raise Errors.ProtocolError( "Unknown block type 0x%04X in binary world data" % header.id) # Read the block body packedBody = f.read(block.getSize() - len(packedHeader)) if len(packedBody) < (block.getSize() - len(packedHeader)): raise Errors.ProtocolError("Incomplete block in binary world data") block.unmarshall(packedHeader + packedBody) w.storeBlock(block) # We're done if this was the EndOfData block if isinstance(block, WorldObjects.EndOfData): break w.postprocess() return w
def __init__(self, **options): """Any options passed to the constructor will be sent to setOptions""" self.tcp = None self.udp = None self.options = {} from BZFlag import Event Event.attach(self, 'onAnyMessage', 'onUnhandledMessage') # Add events for all messages, with onUnhandledMessage as an # unhandled event handler. for message in Common.getMessageDict(self.incoming).values(): eventName = self.getMsgHandlerName(message) if hasattr(self, eventName): event = Event.Event(getattr(self, eventName)) else: event = Event.Event() event.unhandledCallback = self.onUnhandledMessage setattr(self, eventName, event) self.init() self.setOptions(**options)
def lookupMessage(self, msgModule, header): try: return Common.getMessageDict(msgModule)[header.id] except KeyError: raise Errors.ProtocolWarning("Received %s byte message with unknown type 0x%04X in %s" % (header.length, header.id, msgModule.__name__))