def __bytes__(self): # Write the header self.pointer_data = {'headers': list(), 'data': list()} self._bytes = BytesIO(b'') # Write each of the sections self._write_header() self._write_map_layout_pointer() self._write_moving_platform_data_pointers() self._write_pushblocks_pointers() self._write_map_layers_pointer() self._write_gimmick_data_pointer() p = Pointer(self._bytes) p.assign_data(lambda _data: write_int32(_data, 0)) self.pointer_data['headers'].append(p) write_int32(self._bytes, 0) self._write_event_data_pointer() # write all the pointer data. Do all headers then do data # swap entries 30 and 32 for some reason?!? self.pointer_data['headers'] = swap(self.pointer_data['headers'], 30, 32) for pointer in self.pointer_data['headers']: pointer.write() for pointer in self.pointer_data['data']: pointer.write() # write the final file size self._bytes.seek(0x8) _bytes = self._bytes.getvalue() write_int32(self._bytes, len(_bytes)) del _bytes return self._bytes.getvalue()
def _write_map_layer_info(self, data, layer_data): write_int32(data, self.height * self.width) p = Pointer(self._bytes) p.assign_data( lambda _data: self._write_map_layer(_data, layer_data)) self.pointer_data['data'].append(p) write_int32(data, 0)
def _write_pushblocks_pointers(self): for pb in self.pushblocks: p = Pointer(self._bytes) p.assign_data( lambda _data, pb=pb: self._write_pushblocks(_data, pb)) self.pointer_data['headers'].append(p) write_int32(self._bytes, 0)
def _write_moving_platform_data_pointers(self): for mp in self.moving_platforms: p = Pointer(self._bytes) p.assign_data( lambda _data, mp=mp: self._write_moving_platform_data( _data, mp)) self.pointer_data['headers'].append(p) write_int32(self._bytes, 0)
def _write_event_data_info(self, data): write_int32(data, len(self.events)) for event in self.events: p = Pointer(self._bytes) p.assign_data( lambda _data, event=event: self._write_event_data( _data, event)) self.pointer_data['headers'].append(p) write_int32(data, 0)
def _write_map_layers_pointer(self): for i in range(0x7): p = Pointer(self._bytes) layer_data = getattr(self, 'layer{0}_data'.format(str(i))) p.assign_data( lambda _data, layer_data=layer_data: self._write_map_layer_info( # noqa _data, layer_data)) self.pointer_data['headers'].append(p) write_int32(self._bytes, 0)
def _write_map_layout_info(self, data): write_int32(data, self.width) write_int32(data, self.height) write_int32(data, self.height * self.width) p = Pointer(self._bytes) p.assign_data(self._write_map_layout) self.pointer_data['data'].append(p) write_int32(data, 0)
def _write_map_layout_pointer(self): p = Pointer(self._bytes) p.assign_data(self._write_map_layout_info) self.pointer_data['headers'].append(p) write_int32(self._bytes, 0)
def _write_header(self): self._bytes.write(struct.pack('4s', self.magic)) write_int32(self._bytes, self.version) write_int32(self._bytes, 0) write_int32(self._bytes, self.unknown_value0) write_int32(self._bytes, self.box_number) write_int32(self._bytes, self.box_set_num) write_int32(self._bytes, self.camera_height) write_int32(self._bytes, self.sound_tag)
def _write_event_data_pointer(self): p = Pointer(self._bytes) p.assign_data(lambda _data: self._write_event_data_info(_data)) self.pointer_data['headers'].append(p) write_int32(self._bytes, 0)
def _write_gimmick_data(self, data): write_int32(data, len(self.gimmick_data)) for gimmick in self.gimmick_data: data.write(bytes(gimmick))
def __bytes__(self): """ Serialize the Event data. """ data = BytesIO(b'') write_int32(data, self.wuid) write_int32(data, self.kind) write_int32(data, self.param0) write_int32(data, self.param1) write_int32(data, self.param2) write_int32(data, self.param3) write_int32(data, self.param4) write_int32(data, self.param5) return data.getvalue()
def __bytes__(self): """ Serialize the Gimmick data. """ data = BytesIO(b'') write_int32(data, self.wuid) write_int32(data, self.kind) write_int32(data, self.x) write_int32(data, self.y) write_int32(data, self.group) write_int32(data, self.appearance) for i, fmt in enumerate(self.param_fmts): if fmt == '<i': try: write_int32(data, getattr(self, 'param{0}'.format(str(i)))) except struct.error: print(getattr(self, 'param{0}'.format(str(i)))) raise elif fmt == '<hh': write_tuple16(data, getattr(self, 'param{0}'.format(str(i)))) return data.getvalue()