6 class WadException(Exception):
11 wadinfo_size = calcsize (wadinfo_t)
13 filelump_size = calcsize (filelump_t)
19 for (handle, pos, size, name) in self.lumps:
23 def AddFile(self, filename):
24 (base, ext) = os.path.splitext (filename)
25 fileHandle = open (filename, 'rb')
28 if ext.lower() == '.wad':
30 (id, numLumps, infoTable) = unpack (wad.wadinfo_t, fileHandle.read(wad.wadinfo_size))
32 if id != 'IWAD' and id != 'PWAD':
33 raise WadException("Wad file %s doesn't have IWAD or PWAD id" % filename)
35 fileHandle.seek (infoTable)
37 for i in range (0, numLumps):
38 lumpinfo.append (unpack (wad.filelump_t, fileHandle.read(wad.filelump_size)))
40 # single file, boring.
41 size = os.stat (filename).st_size
42 lumpinfo.append ((0, size, base.upper()[:8]))
44 # Dump the info into our table of accumulated crap.
45 for (pos, length, name) in lumpinfo:
46 self.lumps.append ((fileHandle, pos, length, name.rstrip('\x00')))
48 def InitMultipleFiles (self, filenames):
52 def InitFile (self, filename):
56 return len (self.lumps)
58 def CheckNumForName (self, name):
59 # meh sucky in-place crappy way to do
61 searchName = name.upper()[:8]
62 for (x, y) in enumerate (reversed(self.lumps)):
63 (handle, pos, length, name) = y
64 if searchName == name:
65 return len(self.lumps) - x - 1
68 def GetNumForName (self, name):
69 index = self.CheckNumForName (name)
71 raise WadException ("%s not found!" % name)
74 def LumpLength (self, lump):
75 (handle, pos, length, name) = self.lumps[lump]
78 def ReadLump (self, lump):
79 (handle, pos, length, name) = self.lumps[lump]
81 return handle.read (length)
83 def CacheLumpNum (self, lump, tag):
86 def CacheLumpName (self, name, tag):
87 return self.CacheLumpNum (self.GetNumForName (name), tag)