-
Notifications
You must be signed in to change notification settings - Fork 5
/
readrs_gzrs2.py
310 lines (241 loc) · 12.4 KB
/
readrs_gzrs2.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#####
# Most of the code is based on logic found in...
#
### GunZ 1
# - RTypes.h
# - RToken.h
# - RealSpace2.h/.cpp
# - RBspObject.h/.cpp
# - RMaterialList.h/.cpp
# - RMesh_Load.cpp
# - RMeshUtil.h
# - MZFile.cpp
# - R_Mtrl.cpp
# - EluLoader.h/cpp
# - LightmapGenerator.h/.cpp
# - MCPlug2_Mesh.cpp
#
### GunZ 2
# - RVersions.h
# - RTypes.h
# - RD3DVertexUtil.h
# - RStaticMeshResource.h
# - RStaticMeshResourceFileLoadImpl.cpp
# - MTypes.h
# - MVector3.h
# - MSVector.h
# - RMesh.cpp
# - RMeshNodeData.h
# - RMeshNodeLoadImpl.h/.cpp
# - RSkeleton.h/.cpp
#
# Please report maps and models with unsupported features to me on Discord: Krunk#6051
#####
import os, io
# import struct
# from struct import unpack
from .constants_gzrs2 import *
from .classes_gzrs2 import *
from .io_gzrs2 import *
def readRs(self, path, state):
file = io.open(path, 'rb')
file.seek(0, os.SEEK_END)
fileSize = file.tell()
file.seek(0, os.SEEK_SET)
if state.logRsPortals or state.logRsCells or state.logRsGeometry or state.logRsTrees or state.logRsLeaves or state.logRsVerts:
print("=================== Read RS ===================")
print()
id = readUInt(file)
version = readUInt(file)
if state.logRsPortals or state.logRsCells or state.logRsGeometry or state.logRsTrees or state.logRsLeaves or state.logRsVerts:
print(f"Path: { path }")
print(f"ID: { hex(id) }")
print(f"Version: { hex(version) }")
print()
if not (id == RS2_ID or id == RS3_ID) or version < RS3_VERSION1:
self.report({ 'ERROR' }, f"GZRS2: RS header invalid! { hex(id) }, { hex(version) }")
file.close()
return { 'CANCELLED' }
if id == RS2_ID and version == RS2_VERSION:
matCount = readInt(file)
if matCount != len(state.xmlRsMats):
self.report({ 'ERROR' }, f"GZRS2: RS material count did not match the XML parse! { matCount }, { len(state.xmlRsMats) }")
file.close()
return { 'CANCELLED' }
for _ in range(matCount): # skip packed material strings
for __ in range(256):
if file.read(1) == b'\x00':
break
auxPolyCount = readInt(file)
skipBytes(file, 4) # skip auxiliary vertex count
# I think these are pre-cut, either during the bsp process or to separate convex n-gons, or both
# The uvs mays be inferred through the leaf aux ids
for _ in range(auxPolyCount):
skipBytes(file, 4 + 4 + 4 * 4 + 4) # skip material id, draw flags, plane and area data
skipBytes(file, 2 * readUInt(file) * 4 * 3) # skip vertex data and normal data
skipBytes(file, 4 * 4) # skip unused, unknown counts
skipBytes(file, 4) # skip node count
state.rsPolygonCount = readUInt(file)
state.rsVertexCount = readInt(file)
skipBytes(file, 4) # skip indices count
vertexOffset = 0
l = 0
def openRS2BspNode():
nonlocal state, vertexOffset, l
state.bspBounds.append(readBounds(file, state.convertUnits, state.convertUnits))
skipBytes(file, 4 * 4) # skip plane
if readBool(file): openRS2BspNode() # positive
if readBool(file): openRS2BspNode() # negative
for _ in range(readUInt(file)):
leafMatID = readInt(file)
skipBytes(file, 4) # skip auxiliary polygon id
drawFlags = readUInt(file)
leafVertexCount = readUInt(file)
for v in range(leafVertexCount):
pos = readCoordinate(file, state.convertUnits, True)
nor = readDirection(file, True)
uv1 = readUV2(file)
uv2 = readUV2(file)
# Why does the second UV layer end up garbled? I don't understand...
# file.seek(-8, os.SEEK_CUR)
# uv1x = file.read(4)
# uv1y = file.read(4)
# print(v, format(int.from_bytes(uv1x, 'little'), '0>32b'), format(int.from_bytes(uv1y, 'little'), '0>32b'), struct.unpack('<f', uv1x)[0], struct.unpack('<f', uv1y)[0])
state.rsVerts.append(RsVertex(pos, nor, (0, 0, 0), 1, uv1, uv2))
if state.logRsVerts:
print(f"===== Vertex { v + 1 } ===========================")
print("Position: ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*pos))
print("Normal: ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*nor))
print("UV1: ({:>6.03f}, {:>6.03f})".format(*uv1))
print("UV2: ({:>6.03f}, {:>6.03f})".format(*uv2))
print()
skipBytes(file, 4 * 3) # skip plane normal
if not (0 <= leafMatID < len(state.xmlRsMats)):
self.report({ 'INFO' }, f"GZRS2: Material ID out of bounds, setting to 0 and continuing. { leafMatID }, { len(state.xmlRsMats) }")
leafMatID = 0
state.rsLeaves.append(RsLeaf(leafMatID, drawFlags, leafVertexCount, vertexOffset))
vertexOffset += leafVertexCount
if state.logRsLeaves:
print(f"===== Leaf { l + 1 } =============================")
print(f"Material ID: { leafMatID }")
print(f"Draw Flags: { drawFlags }")
print(f"Vertex Count: { leafVertexCount }")
print(f"Vertex Offset: { vertexOffset }")
print()
l += 1
openRS2BspNode()
if state.rsVertexCount != len(state.rsVerts):
self.report({ 'ERROR' }, f"GZRS2: Bsp vertex count did not match vertices written! { state.rsVertexCount }, { len(state.rsVerts) }")
elif id == RS3_ID and version >= RS3_VERSION1:
if version not in RS_SUPPORTED_VERSIONS:
self.report({ 'ERROR' }, f"GZRS2: RS3 version is not supported yet! Model will not load properly! Please submit to Krunk#6051 for testing! { path }, { hex(version) }")
file.close()
return { 'CANCELLED' }
for p in range(readUInt(file)):
name = readString(file, readInt(file))
vertices = tuple(readVec3(file) for _ in range(readUInt(file)))
cellID1 = readInt(file)
cellID2 = readInt(file)
state.smrPortals.append(RsPortal(name, vertices, cellID1, cellID2))
if state.logRsPortals:
print(f"===== Portal { p + 1 } ===========================")
print(f"Name: { name }")
print(f"Vertices: { len(vertices) }")
print(f"Cell ID 1: { cellID1 }")
print(f"Cell ID 2: { cellID2 }")
print()
for c in range(readUInt(file)):
name = readString(file, readInt(file))
planes = tuple(readVec4(file) for _ in range(readUInt(file)))
faces = ()
geometryCount = 1
if version >= RS3_VERSION4:
faces = tuple(tuple(readVec3(file) for _ in range(readUInt(file))) for _ in range(readUInt(file)))
if version >= RS3_VERSION2:
geometryCount = readInt(file)
geometry = []
for g in range(geometryCount):
if version >= RS3_VERSION2: skipBytes(file, 4) # skip FVF flags
skipBytes(file, 4 + 4) # skip node count and polygon info count
geoVertexCount = readInt(file)
indexCount = readInt(file)
for v in range(geoVertexCount):
pos = readCoordinate(file, state.convertUnits, False)
nor = readDirection(file, False)
col = (1, 1, 1)
alpha = 1
if version >= RS3_VERSION2:
col = readVec3(file)
alpha = min(col[0] + col[1] + col[2] / 3, 1)
uv1 = readUV2(file)
uv2 = readUV2(file)
state.rsVerts.append(RsVertex(pos, nor, col, alpha, uv1, uv2))
if state.logRsVerts:
print(f"===== Vertex { v + 1 } ===========================")
print("Position: ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*pos))
print("Normal: ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*nor))
print("Color: ({:>6.03f}, {:>6.03f}, {:>6.03f}, {:>6.03f})".format(*col, alpha))
print("UV1: ({:>6.03f}, {:>6.03f})".format(*uv1))
print("UV2: ({:>6.03f}, {:>6.03f})".format(*uv2))
print()
vertexOffset = 0
trees = []
for t in range(readUInt(file)):
matCount = readInt(file)
lightmapID = readInt(file)
treeVertexCount = readInt(file)
l = 0
def openRS3BspNode():
nonlocal state, vertexOffset, l
state.bspBounds.append(readBounds(file, state.convertUnits, state.convertUnits))
if not readBool(file):
openRS3BspNode() # positive
openRS3BspNode() # negative
for _ in range(readUInt(file)):
leafMatID = readInt(file)
drawFlags = readUInt(file)
if version >= RS3_VERSION2:
drawFlags = RM_FLAG_COLLISION_MESH
leafVertexCount = readInt(file)
skipBytes(file, 4) # skip vertex offset, we determine our own
state.rsLeaves.append(RsLeaf(leafMatID, drawFlags, leafVertexCount, vertexOffset))
vertexOffset += leafVertexCount
if state.logRsLeaves:
print(f"===== Leaf { l + 1 } =============================")
print(f"Material ID: { leafMatID }")
print(f"Draw Flags: { drawFlags }")
print(f"Vertex Count: { leafVertexCount }")
print(f"Vertex Offset: { vertexOffset }")
print()
l += 1
openRS3BspNode()
trees.append(RsTree(matCount, lightmapID, treeVertexCount))
if state.logRsTrees:
print(f"===== Tree { t + 1 } =============================")
print(f"Material Count: { matCount }")
print(f"Lightmap ID: { lightmapID }")
print(f"Vertex Count: { treeVertexCount }")
print(f"Vertex Offset: { vertexOffset }")
print()
geometry.append(RsGeometry(geoVertexCount, indexCount, tuple(trees)))
if state.logRsGeometry:
print(f"===== Geometry { g + 1 } =============================")
print(f"Vertex Count: { geoVertexCount }")
print(f"Index Count: { indexCount }")
print(f"Trees: { len(trees) }")
print()
state.smrCells.append(RsCell(name, planes, faces, tuple(geometry)))
if state.logRsCells:
print(f"===== Cell { c + 1 } =============================")
print(f"Name: { name }")
print(f"Planes: { len(planes) }")
print(f"Faces: { len(faces) }")
print(f"Geometry: { len(geometry) }")
print()
if state.logRsPortals or state.logRsCells or state.logRsGeometry or state.logRsTrees or state.logRsLeaves or state.logRsVerts:
bytesRemaining = fileSize - file.tell()
if bytesRemaining > 0:
self.report({ 'INFO' }, f"GZRS2: RS import finished with bytes remaining! { path }, { hex(id) }, { hex(version) }")
print(f"Bytes Remaining: { bytesRemaining }")
print()
file.close()