-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpl.py
136 lines (94 loc) · 4.25 KB
/
impl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import utils
import io
def GetTextureNamesFromWad(path, mapwadName):
textureNames = []
mapWad = utils.Wad(path + mapwadName)
if mapWad is None:
return
print("Opened wad " + mapwadName)
mapWad.header.Print()
sideDefLump = mapWad.GetLump(utils.SideDefs())
if sideDefLump is not None:
print("Found sidedefs lump!")
sideDefLumpByteReader = io.BytesIO(sideDefLump.data)
size = utils.GetFileSize(sideDefLumpByteReader)
sideDefLumpByteReader.seek(0)
while sideDefLumpByteReader.tell() < size:
sideDefLumpByteReader.read(2) # x offset
sideDefLumpByteReader.read(2) # y offset
upper = sideDefLumpByteReader.read(8)
if upper.decode("ascii")[0] != "-":
if upper not in textureNames:
textureNames.append(upper)
lower = sideDefLumpByteReader.read(8)
if lower.decode("ascii")[0] != "-":
if lower not in textureNames:
textureNames.append(lower)
middle = sideDefLumpByteReader.read(8)
if middle.decode("ascii")[0] != "-":
if middle not in textureNames:
textureNames.append(middle)
sideDefLumpByteReader.read(2) # Sector
return textureNames
def GetTextureLumpsFromTexWad(path, texWadName, textureNames):
texLumps = []
texWad = utils.Wad(path + texWadName)
if texWad is None:
return
print("Opened wad " + texWadName)
texWad.header.Print()
for texName in textureNames:
lump = texWad.GetLump(texName)
if lump is not None:
texLumps.append(lump)
print("getting data for: " + texName.decode("UTF-8"))
# texStart = texWad.GetLump(utils.TextureMarkerStart())
# print("start marker location: " + str(texStart.location))
# print("start marker size: " + str(texStart.length))
# texEnd = texWad.GetLump(utils.TextureMarkerEnd())
# print("end marker location: " + str(texEnd.location))
# print("end marker size: " + str(texEnd.length))
return texLumps
def AddTexturesToWad(path, source, destination, textureLumps):
sourceWad = utils.Wad(path + source)
if sourceWad is None:
return
print("Opened wad " + source)
sourceWad.header.Print()
destFile = open(path + destination, 'w+b')
if destFile is None:
return
destFile.write(sourceWad.header.type) #wad type
destFile.write((0).to_bytes(4, byteorder = "little", signed = False))
destFile.write((12).to_bytes(4, byteorder = "little", signed = False))
startTextureMarkerIndex = sourceWad.GetLumpIndex(utils.TextureMarkerStart())
if startTextureMarkerIndex == -1:
startTxDirectoryEntry = utils.DirectoryEntry(0, 0, utils.TextureMarkerStart())
texStartLump = utils.Lump(startTxDirectoryEntry, b'')
sourceWad.lumps.append(texStartLump)
endTextureMarkerIndex = sourceWad.GetLumpIndex(utils.TextureMarkerEnd())
if endTextureMarkerIndex == -1:
endTxDirectoryEntry = utils.DirectoryEntry(0, 0, utils.TextureMarkerEnd())
texEndLump = utils.Lump(endTxDirectoryEntry, b'')
sourceWad.lumps.append(texEndLump)
for texLump in textureLumps:
if sourceWad.GetLump(texLump.directoryEnt.name) is None:
endTextureMarkerIndex = sourceWad.GetLumpIndex(utils.TextureMarkerEnd())
sourceWad.lumps.insert(endTextureMarkerIndex, texLump)
print("Adding lump: " + str(texLump.directoryEnt.name) + " at index: " + str(endTextureMarkerIndex))
# write data + update location in directory entries
for lump in sourceWad.lumps:
lump.directoryEnt.location = destFile.tell()
destFile.write(lump.data)
directoryStart = destFile.tell().to_bytes(4, byteorder = "little", signed = False)
# write directory entries
for lump in sourceWad.lumps:
dirEnt = lump.directoryEnt
destFile.write(dirEnt.location.to_bytes(4, byteorder = "little", signed = False))
destFile.write(dirEnt.length.to_bytes(4, byteorder = "little", signed = False))
destFile.write(dirEnt.name)
#update wad header
destFile.seek(4)
destFile.write(len(sourceWad.lumps).to_bytes(4, byteorder = "little", signed = False))
destFile.write(directoryStart)
destFile.close()