-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjkl.py
358 lines (301 loc) · 12.2 KB
/
jkl.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import io
import re
SUBSECTION_RE = re.compile(br'(.+)\s+\d+\Z') # [ITEM TYPE] [COUNT]EOL
ITEM_RE = re.compile(br'(\d+):') # [IDX]: ...
CMP_RE = re.compile(br'(\d+):\s+(\S+)') # [IDX]: [FILENAME]
# [IDX]: [FILENAME] 1 1
FLOAT_FRAGMENT = r'-?\d*(?:\.\d+)?(?:[eE][-+]?\d+)?'
VECTOR_RE = re.compile(
fr'\(({FLOAT_FRAGMENT})/({FLOAT_FRAGMENT})/({FLOAT_FRAGMENT})\)'.encode())
MATERIAL_RE = re.compile(
fr'(\d+):\s+(\S+)\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})'.encode())
POSXYZ_RE = re.compile(
fr'(\d+):\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})'.encode()) # [IDX]: [F] [F] [F]
TEXUV_RE = re.compile(
fr'(\d+):\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})'.encode()) # [IDX]: [F] [F]
SURFACE_RE = re.compile(
fr'(\d+):\s+(\-?\d+)\s+(0x[0-9a-fA-F]+)\s+(0x[0-9a-fA-F]+)\s+(\-?\d+)\s+(\-?\d+)\s+(\-?\d+)\s+(\-?\d+)\s+({FLOAT_FRAGMENT})\s+(\-?\d+)'.encode())
SECTOR_RE = re.compile(br'SECTOR\s+(\d+)')
SECTOR_COLORMAP_RE = re.compile(br'COLORMAP\s+(\d+)')
SECTOR_SURFACES_RE = re.compile(br'SURFACES\s+(\d+)\s+(\d+)')
SECTOR_AMBIENT_LIGHT_RE = re.compile(
fr'AMBIENT\s+LIGHT\s+({FLOAT_FRAGMENT})'.encode())
SECTOR_EXTRA_LIGHT_RE = re.compile(
fr'EXTRA\s+LIGHT\s+({FLOAT_FRAGMENT})'.encode())
IDENTIFIER_FRAGMENT = r'[^0-9\s]\S+'
TEMPLATE_RE = re.compile(fr'({IDENTIFIER_FRAGMENT})\s+({IDENTIFIER_FRAGMENT})\s+(\D.+)\Z'.encode())
THING_RE = re.compile(
fr'(\d+):\s+({IDENTIFIER_FRAGMENT})\s+(\S+)\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})\s+({FLOAT_FRAGMENT})\s+(-?\d+)(\s+-?\d+)?\s*(.*)'.encode())
def _get_surface_rest_re(nverts, mots):
text = r'\s+'
text += r'(-?\d+),\s*(-?\d+)\s+' * nverts
if mots:
text += fr'({FLOAT_FRAGMENT})\s+' * nverts * 3
text += fr'({FLOAT_FRAGMENT})\s+' * (nverts - 1)
text += fr'({FLOAT_FRAGMENT})' # no trailing whitespace
rest_re = re.compile(text.encode())
return rest_re
def _parse_subsections(lines):
ss = {}
cur = []
for line in lines:
if ITEM_RE.match(line):
cur.append(line)
else:
match = SUBSECTION_RE.match(line)
if match:
name = match.group(1).strip().lower()
ss[name] = cur = []
return ss
def _get_light_config(config):
thingflags = config.get(b'thingflags', b'')
if not thingflags.startswith(b'0x'):
return None
if int(thingflags[2:], 16) & 0x1 == 0: # does not emit light
return None
light = float(config.get(b'light', b'0.0'))
if light <= 0:
return None
match = VECTOR_RE.match(config.get(b'lightoffset', b''))
if match:
offset = (float(match.group(1)), float(
match.group(2)), float(match.group(3)))
else:
offset = (0, 0, 0)
intensity = float(config.get(b'lightintensity', b'1.0'))
return {'light': light, 'intensity': intensity, 'offset': offset}
class JklFile:
def __init__(self, sections):
self._read_materials(sections[b'materials'])
self._read_georesource(sections[b'georesource'])
self._read_sectors(sections[b'sectors'])
self._prune_materials()
templates = self._read_templates(sections[b'templates'])
self._read_things(sections[b'things'], templates)
def _prune_materials(self):
used_materials = {}
for k, s in self.surfaces.items():
mat = s['material']
used_materials[mat] = True
for k in list(self.materials.keys()):
if not k in used_materials:
del self.materials[k]
def _read_materials(self, lines):
ss = _parse_subsections(lines)
mats = {}
for line in ss[b'world materials']:
match = MATERIAL_RE.match(line)
if match:
key = int(match.group(1))
name = match.group(2)
mats[key] = name
self.materials = mats
def _read_georesource(self, lines):
ss = _parse_subsections(lines)
xyzs = {}
for line in ss[b'world vertices']:
match = POSXYZ_RE.match(line)
if match:
key = int(match.group(1))
x = float(match.group(2))
y = float(match.group(3))
z = float(match.group(4))
xyzs[key] = (x, y, z)
uvs = {}
for line in ss[b'world texture vertices']:
match = TEXUV_RE.match(line)
if match:
key = int(match.group(1))
u = float(match.group(2))
v = float(match.group(3))
uvs[key] = (u, v)
surfaces = {}
mots = True
for line in ss[b'world surfaces']:
match = SURFACE_RE.match(line)
if match:
key = int(match.group(1))
mat = int(match.group(2))
surfflags = int(match.group(3)[2:], 16)
faceflags = int(match.group(4)[2:], 16)
geo = int(match.group(5))
light = int(match.group(6))
# tex = int(match.group(7))
# adjoin = int(match.group(8))
extra_light = float(match.group(9))
nverts = int(match.group(10))
rest = line[match.end(10):]
uv_scale = 0.5 if (surfflags & 0x10) else 1
uv_scale *= 2 if (surfflags & 0x20) else 1
uv_scale *= 8 if (surfflags & 0x40) else 1
ignore_lighting = (light == 1)
twosided = (faceflags & 0x1) != 0
translucent = (faceflags & 0x2) != 0
rest_re = _get_surface_rest_re(nverts, mots)
match = rest_re.match(rest)
if not match and mots:
mots = False
rest_re = _get_surface_rest_re(nverts, mots)
match = rest_re.match(rest)
vertices = []
for i in range(nverts):
xyz_idx = int(match.group(2 * i + 1))
uv_idx = int(match.group(2 * i + 2))
uv = (0.0, 0.0) if uv_idx == -1 \
else (uvs[uv_idx][0] * uv_scale, uvs[uv_idx][1] * uv_scale)
if ignore_lighting:
diffuse = (1.0, 1.0, 1.0)
elif mots:
# TODO? l = float(match.group(2 * nverts + 4 * i + 1))
r = float(match.group(2 * nverts + 4 * i + 2))
g = float(match.group(2 * nverts + 4 * i + 3))
b = float(match.group(2 * nverts + 4 * i + 4))
r = min(1, r + extra_light)
g = min(1, g + extra_light)
b = min(1, b + extra_light)
diffuse = (r, g, b)
else:
l = float(match.group(2 * nverts + i + 1))
l = min(1, l + extra_light)
diffuse = (l, l, l)
vertices.append([xyzs[xyz_idx], uv, diffuse])
if twosided:
vertices.extend(reversed(vertices[1:-1]))
surfaces[key] = {
'vertices': vertices,
'surfflags': surfflags,
'geo': geo,
'material': mat,
'translucent': translucent
}
else:
match = POSXYZ_RE.match(line) # normal vector
if match:
key = int(match.group(1))
x = float(match.group(2))
y = float(match.group(3))
z = float(match.group(4))
surfaces[key]['normal'] = (x, y, z)
self.surfaces = surfaces
cmps = {}
for line in ss[b'world colormaps']:
match = CMP_RE.match(line)
if match:
key = int(match.group(1))
cmps[key] = match.group(2)
self.colormaps = cmps
def _read_sectors(self, lines):
sectors = {}
cur = None
for line in lines:
match = SECTOR_RE.match(line)
if match:
key = int(match.group(1))
sectors[key] = cur = {}
continue
match = SECTOR_COLORMAP_RE.match(line)
if match:
cur['colormap'] = int(match.group(1))
continue
match = SECTOR_SURFACES_RE.match(line)
if match:
first = int(match.group(1))
cur['surfaces'] = (first, first + int(match.group(2)))
continue
match = SECTOR_AMBIENT_LIGHT_RE.match(line)
if match:
cur['ambient_light'] = float(match.group(1))
continue
match = SECTOR_EXTRA_LIGHT_RE.match(line)
if match:
cur['extra_light'] = float(match.group(1))
continue
self.sectors = sectors
def _read_config(self, text):
config = {}
for cfgpair in text.split():
try:
k, v = cfgpair.split(b'=')
config[k] = v
except ValueError:
continue # not in form key=value
return config
def _read_templates(self, lines):
tmpls = {}
for line in lines:
match = TEMPLATE_RE.match(line)
if match:
name = match.group(1).lower()
base_name = match.group(2).lower()
config = self._read_config(match.group(3))
if base_name in tmpls:
# merge: overwrite values of base with new ones
config = {**tmpls[base_name], **config}
tmpls[name] = config
return tmpls
def _read_things(self, lines, templates):
lights = []
models = []
spawn_points = []
for line in lines:
match = THING_RE.match(line)
if match:
# key = int(match.group(1))
template = match.group(2).lower()
# name = match.group(3)
x = float(match.group(4))
y = float(match.group(5))
z = float(match.group(6))
pitch = float(match.group(7))
yaw = float(match.group(8))
roll = float(match.group(9))
sector = int(match.group(10))
config = self._read_config(
match.group(12)) if match.group(12) else {}
if template in templates:
# merge: overwrite values of template with new ones
config = {**templates[template], **config}
if b'model3d' in config:
mdl = {'pos': (x, y, z), 'rot': (
pitch, yaw, roll), 'sector': sector, 'model': config[b'model3d']}
models.append(mdl)
if b'type' in config and config[b'type'] == b'player':
spawn_points.append(
{'pos': (x, y, z), 'rot': (pitch, yaw, roll)})
light = _get_light_config(config)
if light:
light['pos'] = (x, y, z)
lights.append(light)
self.lights = lights
self.models = models
self.spawn_points = spawn_points
def _strip(line):
start = line.find(b'#')
if start != -1:
line = line[:start]
return line.strip()
def _defines_section(line):
return len(line) > 8 and line[:8].lower() == b'section:'
def _ends_section(line):
return line == b'end' or _defines_section(line)
def _parse_section(lines, f):
for line in f:
line = _strip(line)
if not line:
continue
elif _ends_section(line):
return line
lines.append(line)
return b''
def read_from_file(f):
sections = {}
for line in f:
line = _strip(line)
while _defines_section(line):
section = line[8:].strip().lower()
section_lines = []
line = _parse_section(section_lines, f)
sections[section] = section_lines
return JklFile(sections)
def read_from_bytes(b):
return read_from_file(io.BytesIO(b))