def from_wad(self, wf, name): texture1 = wf.get_lump(name) pnames = wf.get_lump('PNAMES') if texture1 is None: raise TextureError('Lump %s not found' % (name.upper())) if pnames is None: raise TextureError('Lump PNAMES not found') texture1_stream = texture1.get_stream() pnames_stream = pnames.get_stream() texlist = self() try: pnames_cnt = struct.unpack('<I', pnames_stream.read(4))[0] pnames_entries = [] for i in range(pnames_cnt): pnames_entries.append(wad.stomp_c_string(pnames_stream.read(8)).upper()) numtextures = struct.unpack('<I', texture1_stream.read(4))[0] for i in range(numtextures): texture1_stream.seek(4 + i * 4) offset = struct.unpack('<I', texture1_stream.read(4))[0] texture1_stream.seek(offset) name = wad.stomp_c_string(texture1_stream.read(8)).upper() masked = struct.unpack('<I', texture1_stream.read(4))[0] width, height = struct.unpack('<HH', texture1_stream.read(4)) columndirectory = struct.unpack('<I', texture1_stream.read(4))[0] patchcount = struct.unpack('<H', texture1_stream.read(2))[0] tex = Texture1Texture() tex.name = name tex.width = width tex.height = height for j in range(patchcount): originx, originy = struct.unpack('<hh', texture1_stream.read(4)) patch_idx, stepdir, colormap = struct.unpack('<HHH', texture1_stream.read(6)) patch = Texture1Patch() patch.name = pnames_entries[patch_idx].upper() patch.originx = originx patch.originy = originy tex.patches.append(patch) texlist.append(tex) finally: pnames_stream.close() texture1_stream.close() return texlist
def add_sectors(self, lump): self.sectors = [] stream = lump.get_stream() try: cnt = int(len(lump.data) / 26) for i in range(cnt): sector = DoomLevelSector() sector.height_floor, sector.height_ceiling = struct.unpack('<hh', stream.read(4)) sector.tex_floor = wad.stomp_c_string(stream.read(8)).upper() sector.tex_ceiling = wad.stomp_c_string(stream.read(8)).upper() sector.light, sector.special, sector.tag = struct.unpack('<HHH', stream.read(6)) self.sectors.append(sector) finally: stream.close()
def add_sidedefs(self, lump): self.sidedefs = [] stream = lump.get_stream() try: cnt = int(len(lump.data) / 30) for i in range(cnt): sidedef = DoomLevelSidedef() sidedef.xoffs, sidedef.yoffs = struct.unpack('<hh', stream.read(4)) sidedef.tex_upper = wad.stomp_c_string(stream.read(8)).upper() sidedef.tex_lower = wad.stomp_c_string(stream.read(8)).upper() sidedef.tex_middle = wad.stomp_c_string(stream.read(8)).upper() sidedef.sector = struct.unpack('<H', stream.read(2))[0] self.sidedefs.append(sidedef) finally: stream.close()
def from_wad(self, wf): animated = wf.get_lump('ANIMATED') switches = wf.get_lump('SWITCHES') swan = self() if animated is not None: animated_stream = animated.get_stream() while True: type = struct.unpack('<B', animated_stream.read(1))[0] if type == 255: break last = wad.stomp_c_string(animated_stream.read(9)).upper() first = wad.stomp_c_string(animated_stream.read(9)).upper() speed = struct.unpack('<I', animated_stream.read(4))[0] anim = SwanAnimation() anim.first = first anim.last = last anim.speed = speed anim.decals = (type == 3) if type == 0: swan.flats.append(anim) else: swan.textures.append(anim) animated_stream.close() if switches is not None: switches_stream = switches.get_stream() while True: off = wad.stomp_c_string(switches_stream.read(9)).upper() on = wad.stomp_c_string(switches_stream.read(9)).upper() version = struct.unpack('<h', switches_stream.read(2))[0] if version == 0: break swch = SwanSwitch() swch.off = off swch.on = on swch.version = version swan.switches.append(swch) return swan
def to_wad(self, wf, name, combine_pnames=False): pnames_idx = 0 pnames_repl = dict() pnames_list = [] pnames = wf.get_lump_or_new('PNAMES') if combine_pnames: pnames_stream = pnames.get_stream() pnames_cnt = struct.unpack('<I', pnames_stream.read(4))[0] for i in range(pnames_cnt): pnames_list.append(wad.stomp_c_string(pnames_stream.read(8)).upper()) pnames_repl[pnames_list[-1]] = i pnames_stream.close() pnames_idx = len(pnames_list) for tex in self: for patch in tex.patches: if patch.name not in pnames_repl: pnames_repl[patch.name] = pnames_idx pnames_list.append(patch.name) pnames_idx += 1 pnames_stream = pnames.get_stream_write() pnames_stream.write(struct.pack('<I', len(pnames_list))) for pnames_ent in pnames_list: pnames_stream.write(struct.pack('<8s', pnames_ent)) pnames_stream.save() texture1 = wf.get_lump_or_new(name) texture1_stream = texture1.get_stream_write() texture1_stream.write(struct.pack('<I', len(self))) offset = 4 + 4 * len(self) for tex in self: texture1_stream.write(struct.pack('<I', offset)) offset += 22 + 10 * len(tex.patches) for tex in self: texture1_stream.write(struct.pack('<8sIHHIH', tex.name, 0, tex.width, tex.height, 0, len(tex.patches))) for patch in tex.patches: texture1_stream.write(struct.pack('<hhHHH', patch.originx, patch.originy, pnames_repl[patch.name], 0, 0)) texture1_stream.save()