-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreadcol_gzrs2.py
188 lines (152 loc) · 6.52 KB
/
readcol_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
#####
# 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
from .constants_gzrs2 import *
from .classes_gzrs2 import *
from .io_gzrs2 import *
def readCol(self, file, path, state):
file.seek(0, os.SEEK_END)
fileSize = file.tell()
file.seek(0, os.SEEK_SET)
if state.logColHeaders or state.logColNodes or state.logColTris:
print("=================== Read Col ===================")
print()
id = readUInt(file)
version = readUInt(file)
if state.logColHeaders:
print(f"Path: { path }")
print(f"ID: { hex(id) }")
print(f"Version: { hex(version) }")
print()
if (id != COL1_ID and id != COL2_ID) or (version != COL1_VERSION and version != COL2_VERSION):
self.report({ 'ERROR' }, f"GZRS2: Col header invalid! { hex(id) }, { hex(version) }")
return { 'CANCELLED' }
trianglesRead = 0
if version == COL1_VERSION:
colNodeCount = readUInt(file)
colTriangleCount = readUInt(file)
n = 0
if state.logColHeaders:
print(f"Node Count: { colNodeCount }")
print(f"Total Triangles: { colTriangleCount }")
print()
def openCol1Node():
nonlocal n, trianglesRead
if state.logColNodes:
plane = readPlane(file, True)
else:
skipBytes(file, 4 * 4) # skip plane
hull = not readBool(file)
if readBool(file): openCol1Node() # positive
if readBool(file): openCol1Node() # negative
triangleCount = readUInt(file)
trianglesRead += triangleCount
if state.logColNodes:
print(f"===== Node { n } =============================")
print("Plane: ({:>6.03f}, {:>6.03f}, {:>6.03f}, {:>6.03f})".format(*plane))
print(f"Hull: { hull }")
print(f"Triangle Count: { triangleCount }")
print()
for t in range(triangleCount):
if not state.logColTris:
if hull:
state.colVerts += readCoordinateArray(file, 3, state.convertUnits, True)
else:
skipBytes(file, 4 * 3 * 3)
skipBytes(file, 4 * 3) # skip normal
else:
vertices = readCoordinateArray(file, 3, state.convertUnits, True)
if hull:
state.colVerts.extend(vertices)
nor = readDirection(file, True)
print(f"===== Triangle { t } ===========================")
print("Vertices: ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*vertices[0]))
print(" ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*vertices[1]))
print(" ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*vertices[2]))
print("Normal: ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*nor))
print()
n += 1
openCol1Node()
else:
colTriangleCount = readUInt(file)
colNodeCount = readUInt(file)
n = 0
if state.logColHeaders:
print(f"Node Count: { colNodeCount }")
print(f"Total Triangles: { colTriangleCount }")
print()
def openCol2Node():
nonlocal n, trianglesRead
if state.logColNodes:
bbmin, bbmax = readBounds(file, state.convertUnits)
else:
skipBytes(file, 4 * 3 * 2) # skip bounding box
if not readBool(file):
openCol2Node() # positive
openCol2Node() # negative
else:
triangleCount = readUInt(file)
trianglesRead += triangleCount
if state.logColNodes:
print(f"===== Node { n } =============================")
print("Bounds: ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*bbmin))
print(" ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*bbmax))
print(f"Triangle Count: { triangleCount }")
print()
for t in range(triangleCount):
if not state.logColTris:
state.colVerts += readCoordinateArray(file, 3, state.convertUnits, False)
skipBytes(file, 4 * 2) # skip attributes and material ID
else:
vertices = readCoordinateArray(file, 3, state.convertUnits, False)
attributes = readUInt(file)
matID = readUInt(file)
state.colVerts += vertices
print(f"===== Triangle { t } ===========================")
print("Vertices: ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*vertices[0]))
print(" ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*vertices[1]))
print(" ({:>6.03f}, {:>6.03f}, {:>6.03f})".format(*vertices[2]))
print(f"Attributes: { attributes }")
print(f"Material ID: { matID }")
print()
n += 1
openCol2Node()
if colTriangleCount != trianglesRead:
self.report({ 'ERROR' }, f"GZRS2: The number of Col triangles read did not match the recorded count! { colTriangleCount }, { trianglesRead }")
if state.logColHeaders or state.logColNodes or state.logColTris:
bytesRemaining = fileSize - file.tell()
if bytesRemaining > 0:
self.report({ 'ERROR' }, f"GZRS2: COL import finished with bytes remaining! { path }, { hex(id) }, { hex(version) }")
print(f"Bytes Remaining: { bytesRemaining }")
print()