6 class WadException(Exception):
11 wadinfo_size = struct.calcsize (wadinfo_t)
13 filelump_size = struct.calcsize (filelump_t)
21 for (handle, pos, size, name) in self.lumps:
25 def AddFile(self, filename):
26 "Add a single file to the wad database"
27 (base, ext) = os.path.splitext (filename)
29 fileHandle = open (filename, 'rb')
31 raise WadException ("Could not open '%s'" % filename)
34 if ext.lower() == '.wad':
36 (id, numLumps, infoTable) = struct.unpack (WadDesc.wadinfo_t, fileHandle.read(WadDesc.wadinfo_size))
38 if id != 'IWAD' and id != 'PWAD':
39 raise WadException("Wad file %s doesn't have IWAD or PWAD id" % filename)
41 fileHandle.seek (infoTable)
43 for i in range (0, numLumps):
44 lumpinfo.append (struct.unpack (WadDesc.filelump_t, fileHandle.read(WadDesc.filelump_size)))
46 # single file, boring.
47 size = os.stat (filename).st_size
48 lumpinfo.append ((0, size, base.upper()[:8]))
50 # Dump the info into our table of accumulated crap.
51 for (pos, length, name) in lumpinfo:
52 self.lumps.append ((fileHandle, pos, length, name.rstrip('\x00')))
53 self.lumpcache.append ((False, ''))
55 def InitMultipleFiles (self, filenames):
56 "Add multiple files to the wad database"
60 def InitFile (self, filename):
61 "Add a single file to the wad database"
65 "Return the number of lumps in the wad database"
66 return len (self.lumps)
68 def CheckNumForName (self, name):
69 "Check if a specific lump name exists in the wad database, returning its index or -1 if it was not found"
70 searchName = name.upper()[:8]
71 for (x, y) in enumerate (reversed(self.lumps)):
72 (handle, pos, length, name) = y
73 if searchName == name:
74 return len(self.lumps) - x - 1
77 def GetNumForName (self, name):
78 "Get the lump index of a specific lump name, or throw an exception if it was not found"
79 index = self.CheckNumForName (name)
81 raise WadException ("%s not found!" % name)
84 def LumpLength (self, lump):
85 "Return the size of a specific lump index"
86 if lump < 0 or lump > len(self.lumps):
87 raise WadException ('Invalid lump number %d requested' % lump)
88 (handle, pos, length, name) = self.lumps[lump]
91 def ReadLump (self, lump):
92 "Return the data associated with a specific lump index"
93 if lump < 0 or lump > len(self.lumps):
94 raise WadException ('Invalid lump number %d requested' % lump)
95 (handle, pos, length, name) = self.lumps[lump]
97 return handle.read (length)
99 def CacheLumpNum (self, lump, tag=None):
100 "Cache and return the data associated with a specific lump index"
101 if lump < 0 or lump > len(self.lumps):
102 raise WadException ('Invalid lump number %d requested' % lump)
103 (cached, data) = self.lumpcache[lump]
105 data = self.ReadLump (lump)
106 self.lumpcache[lump] = (True, data)
109 def CacheLumpName (self, name, tag=None):
110 "Cache and return the data associated with a specific lump name"
111 return self.CacheLumpNum (self.GetNumForName (name), tag)