def Serialize(self, fileobj): """ Encodes and writes the header to a stream (usually a file). This will always encode the header as the latest version of the map file format. @type fileobj: C{file} @param fileobj: open file object to serialize to """ # nothing too special, just write everything in the right order try: BinaryStructs.SerializeUTF8(fileobj, self.MapName) BinaryStructs.SerializeUint32(fileobj, self.Width) BinaryStructs.SerializeUint32(fileobj, self.Height) BinaryStructs.SerializeUint32(fileobj, self.Depth) BinaryStructs.SerializeUint32(fileobj, self.PlayerDepth) BinaryStructs.SerializeUTF8(fileobj, self.NorthMap) BinaryStructs.SerializeUTF8(fileobj, self.EastMap) BinaryStructs.SerializeUTF8(fileobj, self.SouthMap) BinaryStructs.SerializeUTF8(fileobj, self.WestMap) BinaryStructs.SerializeUTF8(fileobj, self.BackgroundImage) except Exception as e: raise IOError("error while writing to map file", e) # pack the content flags try: contentFlags = 0 if self.Stripped: contentFlags |= self.Flag_ContentStripped BinaryStructs.SerializeUint32(fileobj, contentFlags) except Exception as e: raise IOError("error while writing to map file", e)
def SaveToOpenFile(self, fileobj): """ Writes the map to the file object. You should be expecting this method to raise some sort of I/O related error; it is your responsibility to handle these errors. @raise IOError: Not directly raised, but you should expect these to occur and handle them accordingly. @type fileobj: C{file} @param fileobj: An open file object (or compatible stream) to write the map to. """ # write the meta-header mheader = self.MetaheaderStruct.pack(self.MagicNumber, CurrentMapVersion) fileobj.write(mheader) # write the header self.header.Serialize(fileobj) # write the tiles for z in range(self.Depth): for x in range(self.Width): for y in range(self.Height): self.tiles[z][x][y].Serialize(fileobj) # store the end-of-section flag BinaryStructs.SerializeUint32(fileobj, Tile.EndOfTilesFlag)
def SerializeBody(self): # Write data. data = cStringIO.StringIO() BinaryStructs.SerializeUint32(data, self.RequestSerial) BinaryStructs.SerializeBinary(data, self.ChallengeSolution, maxlen=32) retval = data.getvalue() data.close() return retval
def SerializeBody(self): # Implemented from protocol documentation. data = cStringIO.StringIO() BinaryStructs.SerializeUint32(data, self.RequestSerial) BinaryStructs.SerializeUint16(data, self.ReasonCode) retval = data.getvalue() data.close() return retval
def SerializeBody(self): # Write data. data = cStringIO.StringIO() BinaryStructs.SerializeUint32(data, self.RequestSerial) BinaryStructs.SerializeUTF8(data, self.Username, maxlen=32) BinaryStructs.SerializeBinary(data, self.Salt, maxlen=16) BinaryStructs.SerializeBinary(data, self.PasswordHash, maxlen=64) BinaryStructs.SerializeUTF8(data, self.Email, maxlen=64) retval = data.getvalue() data.close() return retval
def Serialize(self, fileobj): """ Outputs a tile to a file using the latest mapfile version. @type fileobj: file @param fileobj: An open file object (or compatible stream) for writing """ # pack all of the flags flags = 0 try: BinaryStructs.SerializeUint32(fileobj, flags) except: msg = "An error occurred while writing to the map file.\n" msg += traceback.format_exc() mainlog.error(msg) raise IOError(msg) # write the coordinates try: BinaryStructs.SerializeUint32(fileobj, self.x) BinaryStructs.SerializeUint32(fileobj, self.y) BinaryStructs.SerializeUint32(fileobj, self.z) except: msg = "An error occurred while writing to the map file.\n" msg += traceback.format_exc() mainlog.error(msg) raise IOError(msg) # write the tile information try: BinaryStructs.SerializeUint32(fileobj, self.tileid) except: msg = "An error occurred while writing to the map file.\n" msg += traceback.format_exc() mainlog.error(msg) raise IOError(msg)