Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion tests/test_world_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,67 @@ def test_bad_table_pointer_is_rejected(self):
struct.pack_into("<Q", b, 0x28, 0x60000000)
self.assertIsNone(WI.parse_resource(bytes(b)))

def test_schema_member_type_mismatch_is_rejected(self):
# Position declared as scalar float (0x21) instead of vector3 (0x32) (#5601).
b = bytearray(make_resource())
b[0x318] = 0x21
self.assertIsNone(WI.parse_resource(bytes(b)))

# Archetype name declared as vector3 (0x32).
b = bytearray(make_resource())
b[0x308] = 0x32
self.assertIsNone(WI.parse_resource(bytes(b)))

# LOD level declared as vector3 (0x32).
b = bytearray(make_resource())
b[0x378] = 0x32
self.assertIsNone(WI.parse_resource(bytes(b)))

# Map extent declared as scalar float (0x21).
b = bytearray(make_resource())
# Map members start at 0x3A0; streamingExtentsMin is index 3 (offset 0x3A0 + 16*3 = 0x3D0)
b[0x3D8] = 0x21
self.assertIsNone(WI.parse_resource(bytes(b)))

# Member offset + width overflowing struct size.
b = bytearray(make_resource())
# Position offset at 0x314 set to struct size (0x90), width 12 overflows struct.
struct.pack_into("<I", b, 0x314, 0x90)
self.assertIsNone(WI.parse_resource(bytes(b)))

def test_non_finite_placement_floats_produce_valid_json(self):
# Placement position containing NaN and +inf is written as null in valid JSON (#5602).
b = bytearray(make_resource())
struct.pack_into("<3f", b, 0x438, float("nan"), 2.5, float("inf"))
row = WI.parse_resource(bytes(b))
self.assertIsNotNone(row)
self.assertEqual(row["position_min"], [None, 2.5, None])
self.assertEqual(row["position_max"], [None, 2.5, None])

src = os.path.join(self.dir, "non_finite.bin")
out = os.path.join(self.dir, "out.json")
with open(src, "wb") as f:
f.write(bytes(b))
self.assertEqual(WI.main([src, "--out", out]), 0)

with open(out, "r", encoding="utf-8") as f:
raw = f.read()
self.assertNotIn("NaN", raw)
self.assertNotIn("Infinity", raw)
# Parse with strict handler that rejects NaN/Infinity tokens.
parsed = json.loads(raw, parse_constant=lambda c: (_ for _ in ()).throw(ValueError(f"non-JSON {c}")))
self.assertEqual(parsed["resources"][0]["position_min"], [None, 2.5, None])
self.assertEqual(parsed["resources"][0]["position_max"], [None, 2.5, None])

def test_non_finite_lod_dist_is_ignored(self):
b = bytearray(make_resource())
# lodDist at 0x460 set to +inf
struct.pack_into("<f", b, 0x460, float("inf"))
row = WI.parse_resource(bytes(b))
self.assertIsNotNone(row)
self.assertIsNone(row["lod_dist_min"])
self.assertIsNone(row["lod_dist_max"])

def test_split_resource_needs_continuation(self):
self.assertFalse(WI._needs_continuation(make_resource()))
self.assertTrue(WI._needs_continuation(make_resource()[:0x150]))
Expand Down Expand Up @@ -676,7 +737,7 @@ def carve(self, parts, gap=0):
f.write(part)
files.append({"off": off, "stored": stored, "size": len(part)})
off = ((off + stored + 15) & ~15) + gap
with open(os.path.join(folder, "index.json"), "w", encoding="utf-8") as f:
with open(os.path.join(folder, "index.json"), "w", encoding="utf-8", newline="\n") as f:
json.dump({"summary": {}, "files": files}, f)
return folder

Expand Down
114 changes: 94 additions & 20 deletions tools/world_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import argparse
import csv
import json
import math
import mmap
import os
import re
Expand Down Expand Up @@ -80,6 +81,46 @@
PARENT_MAPS = 0x75D16ADF
PARENT_MAP_SLOT = 0x84799847
LOD_NAMES = {0: "HD", 1: "LOD", 2: "SLOD1", 3: "SLOD2", 4: "SLOD3", 5: "ORPHAN_HD", 6: "SLOD4"}
# Byte widths of supported member types in CEntityDef and CMapData schemas.
MEMBER_WIDTH = {
0x10: 1, 0x11: 1, 0x60: 1,
0x12: 2, 0x13: 2,
0x14: 4, 0x15: 4, 0x21: 4, 0x4A: 4,
0x08: 8, 0x4F: 8,
0x32: 12, 0x33: 12,
0x34: 16,
}
# Expected member types for known fields in CEntityDef and CMapData schemas.
# A resource whose schema declares a known field with a mismatched type byte is rejected.
ENTITY_MEMBER_TYPES = {
FIELD["archetypeName"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x4A),
ARCHETYPE_KEY: (0x08, 0x4F),
FIELD["position"]: (0x32, 0x33),
FIELD["rotation"]: (0x34,),
FIELD["scaleXY"]: (0x21,),
FIELD["scaleZ"]: (0x21,),
FIELD["flags"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x4A),
FIELD["guid"]: (0x08, 0x4F),
FIELD["lodLevel"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x60),
FIELD["parentIndex"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15),
FIELD["parentInstance"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15),
FIELD["numChildren"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15),
FIELD["lodDist"]: (0x21,),
FIELD["childLodDist"]: (0x21,),
FIELD["priorityLevel"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x60),
PARENT_MAP_SLOT: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15),
}
MAPDATA_MEMBER_TYPES = {
FIELD["name"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x4A),
FIELD["name64"]: (0x08, 0x4F),
FIELD["parent"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x4A),
FIELD["streamingExtentsMin"]: (0x32, 0x33),
FIELD["streamingExtentsMax"]: (0x32, 0x33),
FIELD["entitiesExtentsMin"]: (0x32, 0x33),
FIELD["entitiesExtentsMax"]: (0x32, 0x33),
FIELD["flags_map"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x4A),
FIELD["contentFlags"]: (0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x4A),
}


def _u16(b, o):
Expand Down Expand Up @@ -149,7 +190,10 @@ def _value(b, base, member):


def _r4(values):
return [round(v, 4) for v in values] if values else None
if not values:
return None
res = [round(v, 4) if (isinstance(v, (int, float)) and math.isfinite(v)) else None for v in values]
return res if any(v is not None for v in res) else None


def _typed_member(schema, key, ty, width):
Expand Down Expand Up @@ -224,6 +268,23 @@ def parse_resource(b, collect=None):
return None
structs.setdefault(name, (size, members))

if ENTITY in structs:
esize, em = structs[ENTITY]
for k, allowed in ENTITY_MEMBER_TYPES.items():
if k in em:
m = em[k]
w = MEMBER_WIDTH.get(m[1], 0)
if m[1] not in allowed or (w and m[0] + w > esize):
return None
if MAPDATA in structs:
msize, mm = structs[MAPDATA]
for k, allowed in MAPDATA_MEMBER_TYPES.items():
if k in mm:
m = mm[k]
w = MEMBER_WIDTH.get(m[1], 0)
if m[1] not in allowed or (w and m[0] + w > msize):
return None

blocks = []
for i in range(nbl):
o = it + 16 * i
Expand Down Expand Up @@ -270,34 +331,38 @@ def parse_resource(b, collect=None):
for k in range(size // entity_size):
o = off + k * entity_size
a = _value(b, o, em.get(FIELD["archetypeName"]))
if a is not None:
if a is not None and isinstance(a, int):
archetypes.add(a)
pos = _value(b, o, em.get(FIELD["position"]))
if pos:
if isinstance(pos, list) and len(pos) == 3:
positions.append(pos)
level = _value(b, o, em.get(FIELD["lodLevel"]))
if level in LOD_NAMES:
if isinstance(level, int) and level in LOD_NAMES:
lod[LOD_NAMES[level]] = lod.get(LOD_NAMES[level], 0) + 1
dist = _value(b, o, em.get(FIELD["lodDist"]))
if isinstance(dist, float) and dist >= 0:
if isinstance(dist, (int, float)) and math.isfinite(dist) and dist >= 0:
lod_dists.append(dist)
if collect is not None:
rot = _value(b, o, em.get(FIELD["rotation"]))
sxy = _value(b, o, em.get(FIELD["scaleXY"]))
sz = _value(b, o, em.get(FIELD["scaleZ"]))
cld = _value(b, o, em.get(FIELD["childLodDist"]))
entity = {
"complete": o + entity_size <= len(b),
"archetype": a,
"archetype_key": _value(b, o, em.get(ARCHETYPE_KEY)),
"position": pos,
"rotation": _value(b, o, em.get(FIELD["rotation"])),
"scale_xy": _value(b, o, em.get(FIELD["scaleXY"])),
"scale_z": _value(b, o, em.get(FIELD["scaleZ"])),
"position": [v if (isinstance(v, (int, float)) and math.isfinite(v)) else None for v in pos] if pos else None,
"rotation": [v if (isinstance(v, (int, float)) and math.isfinite(v)) else None for v in rot] if rot else None,
"scale_xy": sxy if (isinstance(sxy, (int, float)) and math.isfinite(sxy)) else None,
"scale_z": sz if (isinstance(sz, (int, float)) and math.isfinite(sz)) else None,
"flags": _value(b, o, em.get(FIELD["flags"])),
"guid": _value(b, o, em.get(FIELD["guid"])),
"lod_level": level,
"parent_index": _value(b, o, em.get(FIELD["parentIndex"])),
"parent_instance": _value(b, o, em.get(FIELD["parentInstance"])),
"num_children": _value(b, o, em.get(FIELD["numChildren"])),
"lod_dist": dist,
"child_lod_dist": _value(b, o, em.get(FIELD["childLodDist"])),
"lod_dist": dist if (isinstance(dist, (int, float)) and math.isfinite(dist)) else None,
"child_lod_dist": cld if (isinstance(cld, (int, float)) and math.isfinite(cld)) else None,
}
# Match a typed record start exactly, without renumbering or reordering the legacy rows.
ordinal = ordinals.get(((k * entity_size) << 20) | block_number) if ordinals else None
Expand All @@ -319,8 +384,15 @@ def parse_resource(b, collect=None):
"lod_dist_max": max(lod_dists) if lod_dists else None,
})
if positions:
row["position_min"] = _r4([min(p[i] for p in positions) for i in range(3)])
row["position_max"] = _r4([max(p[i] for p in positions) for i in range(3)])
pos_min = []
pos_max = []
for i in range(3):
axis_vals = [p[i] for p in positions if isinstance(p[i], (int, float)) and math.isfinite(p[i])]
pos_min.append(round(min(axis_vals), 4) if axis_vals else None)
pos_max.append(round(max(axis_vals), 4) if axis_vals else None)
if any(v is not None for v in pos_min):
row["position_min"] = pos_min
row["position_max"] = pos_max

for name, size, off in blocks:
if name != MAPDATA or off is None or map_size == 0:
Expand All @@ -333,7 +405,9 @@ def parse_resource(b, collect=None):
("entities_min", "entitiesExtentsMin"), ("entities_max", "entitiesExtentsMax")):
v = _value(b, off, mm.get(FIELD[field]))
if v:
row[key] = _r4(v)
r = _r4(v)
if r is not None:
row[key] = r
row["map_flags"] = _value(b, off, mm.get(FIELD["flags_map"]))
row["content_flags"] = _value(b, off, mm.get(FIELD["contentFlags"]))
break
Expand Down Expand Up @@ -621,7 +695,7 @@ def append(self, entity):
for k in ("map_name", "entity_index", "parent_map_name"):
if k in entity:
row[k] = entity[k]
self.handle.write(json.dumps(row, separators=(",", ":")) + "\n")
self.handle.write(json.dumps(row, separators=(",", ":"), allow_nan=False) + "\n")
self.written += 1


Expand Down Expand Up @@ -809,7 +883,7 @@ def main(argv=None):
except (OSError, ValueError) as e:
print(f"ERROR: {e}", file=sys.stderr)
return 2
print(json.dumps(summary, indent=1))
print(json.dumps(summary, indent=1, allow_nan=False))
return 0


Expand All @@ -823,12 +897,12 @@ def _write_reports(a, rows, stats, agg):
doc = {"summary": summary, "resources": rows}
os.makedirs(os.path.dirname(os.path.abspath(a.out)), exist_ok=True)
with open(a.out, "w", encoding="utf-8", newline="\n") as f:
json.dump(doc, f, indent=1, sort_keys=True)
json.dump(doc, f, indent=1, sort_keys=True, allow_nan=False)
if a.csv:
cols = ["source", "offset", "size", "signature", "version", "kind", "structs", "enums", "blocks",
"root_objects", "entities", "entity_size", "archetypes", "map_name", "map_name64", "parent",
"map_flags",
"content_flags", "extent_min", "extent_max", "entities_min", "entities_max", "position_min",
"contentFlags", "extent_min", "extent_max", "entities_min", "entities_max", "position_min",
"position_max", "lod", "lod_dist_min", "lod_dist_max"]
os.makedirs(os.path.dirname(os.path.abspath(a.csv)), exist_ok=True)
with open(a.csv, "w", newline="", encoding="utf-8") as f:
Expand All @@ -839,12 +913,12 @@ def _write_reports(a, rows, stats, agg):
r["source"] = r.get("file") or r.get("path") or os.path.basename(a.path)
for k, v in list(r.items()):
if isinstance(v, (list, dict)):
r[k] = json.dumps(v, sort_keys=True)
r[k] = json.dumps(v, sort_keys=True, allow_nan=False)
w.writerow(r)
if agg is not None:
os.makedirs(os.path.dirname(os.path.abspath(a.archetypes_out)), exist_ok=True)
with open(a.archetypes_out, "w", encoding="utf-8", newline="\n") as f:
json.dump(agg.to_json(), f, indent=1, sort_keys=True)
json.dump(agg.to_json(), f, indent=1, sort_keys=True, allow_nan=False)
summary["archetype_file"] = agg.to_json()["summary"]
return summary

Expand Down