]> icculus.org git repositories - theoddone33/hheretic.git/blob - pyhexen/wad.py
8a733bad21c004d592541ebd580b98ab7aa41ff1
[theoddone33/hheretic.git] / pyhexen / wad.py
1
2 import os
3 import os.path
4 from struct import *
5
6 class WadException(Exception):
7         pass
8
9 class wad:
10         wadinfo_t = '<4sii'
11         wadinfo_size = calcsize (wadinfo_t)
12         filelump_t = '<ii8s'
13         filelump_size = calcsize (filelump_t)
14
15         def __init__(self):
16                 self.lumps = []
17
18         def __del__(self):
19                 for (handle, pos, size, name) in self.lumps:
20                         if not handle.closed:
21                                 handle.close()
22
23         def AddFile(self, filename):
24                 (base, ext) = os.path.splitext (filename)
25                 fileHandle = open (filename, 'rb')
26
27                 lumpinfo = []
28                 if ext.lower() == '.wad':
29                         # yay, a wad file.
30                         (id, numLumps, infoTable) = unpack (wad.wadinfo_t, fileHandle.read(wad.wadinfo_size))
31
32                         if id != 'IWAD' and id != 'PWAD':
33                                 raise WadException("Wad file %s doesn't have IWAD or PWAD id" % filename)
34
35                         fileHandle.seek (infoTable)
36
37                         for i in range (0, numLumps):
38                                 lumpinfo.append (unpack (wad.filelump_t, fileHandle.read(wad.filelump_size)))
39                 else:
40                         # single file, boring.
41                         size = os.stat (filename).st_size
42                         lumpinfo.append ((0, size, base.upper()[:8]))
43
44                 # Dump the info into our table of accumulated crap.
45                 for (start, len, name) in lumpinfo:
46                         self.lumps.append ((fileHandle, start, len, name))
47
48         def InitMultipleFiles (self, filenames):
49                 for f in filenames:
50                         self.AddFile (f)
51
52         def InitFile (self, filename):
53                 self.AddFile (f)
54
55         def NumLumps (self):
56                 return len (self.lumps)
57
58         def CheckNumForName (self, name):
59                 # meh sucky in-place crappy way to do
60                 # stuff :(
61                 searchName = name.upper()[:8]
62                 searchLumps = [x for (x, y, z, w) in self.lumps]
63                 lumpIndex = -1
64                 while True:
65                         try:
66                                 lumpIndex = searchLumps.index(searchName)
67                         except ValueError:
68                                 break
69                 return lumpIndex
70
71         def GetNumForName (self, name):
72                 return self.CheckNumForName (name)
73
74         def LumpLength (self, lump):
75                 (handle, start, length, name) = self.lumps[lump]
76                 return length
77
78         def ReadLump (self, lump, dest):
79                 (handle, start, len, name) = self.lumps[lump]
80                 handle.seek (start)
81                 dest.write (handle.read (len))
82
83         def CacheLumpNum (self, lump, tag):
84                 pass
85
86         def CacheLumpName (self, name, tag):
87                 return self.CacheLumpNum (self.GetNumForName (name), tag)
88
89