127 lines
5.1 KiB
Python
127 lines
5.1 KiB
Python
# DirectX .x fixtures (tools/fixtures.py): assimp's X test models (text, binary 32-bit, MSZIP-compressed binary;
|
|
# models and models-nonbsd, read from the nixpkgs assimp source, never committed), plus variants made here from the
|
|
# text ones: binary with 64-bit floats ("bin 0064") and MSZIP-compressed text ("tzip", per-block raw deflate with
|
|
# the previous block as dictionary, as D3DX writes). Reference: assimp (`assimp info -r`: meshes, faces, corners,
|
|
# materials, nodes, animations, bones; world bounds from its PreTransformVertices export), on the variants too.
|
|
# tests/gen/x_cases.luau { name, data, assimp = { ... } }
|
|
import importlib.util, os, re, struct, zlib
|
|
|
|
INPUTS = ["src/Rosetta/model/x", "tools/fixtures/_model_ref.py", "tools/fixtures/fbx.py"]
|
|
|
|
|
|
def module(ctx, name):
|
|
spec = importlib.util.spec_from_file_location(name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
return m
|
|
|
|
|
|
TOKEN = re.compile(rb'\s+|//[^\n]*|#[^\n]*|"[^"]*"|<[^>]*>|[{}]|[;,]|[^\s;,{}"]+')
|
|
|
|
|
|
def to_binary64(text):
|
|
"""a text .x file -> binary tokens with 64-bit floats (templates dropped; numbers with . or e are floats)"""
|
|
body = text[16:]
|
|
out = bytearray(b"xof 0303bin 0064")
|
|
toks = [t for t in TOKEN.findall(body) if t.strip() and not t.startswith((b"//", b"#"))]
|
|
i, nums = 0, []
|
|
|
|
def flush():
|
|
while nums:
|
|
isf = nums[0][0]
|
|
run = []
|
|
while nums and nums[0][0] == isf:
|
|
run.append(nums.pop(0)[1])
|
|
if isf:
|
|
out.extend(struct.pack("<HI", 7, len(run)) + b"".join(struct.pack("<d", v) for v in run))
|
|
else:
|
|
out.extend(struct.pack("<HI", 6, len(run)) + b"".join(struct.pack("<I", int(v) & 0xFFFFFFFF) for v in run))
|
|
|
|
while i < len(toks):
|
|
t = toks[i]
|
|
if t == b"template":
|
|
flush()
|
|
depth = 0
|
|
while i < len(toks):
|
|
if toks[i] == b"{":
|
|
depth += 1
|
|
elif toks[i] == b"}":
|
|
depth -= 1
|
|
if depth == 0:
|
|
break
|
|
i += 1
|
|
elif t in (b";", b","):
|
|
pass
|
|
elif t[:1] in b"0123456789-+.":
|
|
try:
|
|
v = float(t)
|
|
except ValueError:
|
|
v = 0.0
|
|
nums.append((b"." in t or b"e" in t.lower() or b"#" in t, v))
|
|
else:
|
|
flush()
|
|
if t == b"{":
|
|
out.extend(struct.pack("<H", 0x0A))
|
|
elif t == b"}":
|
|
out.extend(struct.pack("<H", 0x0B))
|
|
elif t.startswith(b'"'):
|
|
s = t[1:-1]
|
|
out.extend(struct.pack("<HI", 2, len(s)) + s + struct.pack("<H", 0x14))
|
|
elif t.startswith(b"<"):
|
|
pass
|
|
else:
|
|
out.extend(struct.pack("<HI", 1, len(t)) + t)
|
|
i += 1
|
|
flush()
|
|
return bytes(out)
|
|
|
|
|
|
def to_tzip(text):
|
|
"""a text .x file -> "tzip": MSZIP blocks of 32 KiB, each raw deflate with the previous block as dictionary"""
|
|
body = text[16:]
|
|
out = bytearray(b"xof 0303tzip0032") + struct.pack("<I", 0)
|
|
data = body
|
|
prev = b""
|
|
for p in range(0, len(data), 32768):
|
|
blk = data[p:p + 32768]
|
|
c = zlib.compressobj(9, zlib.DEFLATED, -15, zdict=prev) if prev else zlib.compressobj(9, zlib.DEFLATED, -15)
|
|
z = c.compress(blk) + c.flush(zlib.Z_FINISH)
|
|
out.extend(struct.pack("<HH", len(blk), len(z) + 2) + b"CK" + z)
|
|
prev = blk
|
|
struct.pack_into("<I", out, 16, len(data) + 16) # the size counts the 16-byte header (D3DX)
|
|
return bytes(out)
|
|
|
|
|
|
def generate(ctx):
|
|
M = module(ctx, "_model_ref")
|
|
fbx = module(ctx, "fbx")
|
|
tmp = M.tempdir()
|
|
src = fbx.assimp_src(ctx)
|
|
cases = []
|
|
for sub in ("models/X", "models-nonbsd/X"):
|
|
d0 = os.path.join(src, "test", sub)
|
|
for f in sorted(os.listdir(d0)):
|
|
p = os.path.join(d0, f)
|
|
if os.path.isfile(p) and open(p, "rb").read(4) == b"xof ":
|
|
cases.append({"name": sub.split("/")[0] + "/" + f, "path": p})
|
|
# variants of the small text files: binary 64-bit floats and tzip
|
|
for name in ("test_cube_text.x", "kwxport_test_cubewithvcolors.x", "test.x", "anim_test.x"):
|
|
p = os.path.join(src, "test", "models", "X", name)
|
|
text = open(p, "rb").read()
|
|
for kind, data in (("bin64", to_binary64(text)), ("tzip", to_tzip(text))):
|
|
q = os.path.join(tmp, "%s.%s.x" % (name[:-2], kind))
|
|
open(q, "wb").write(data)
|
|
cases.append({"name": "variant/%s.%s.x" % (name[:-2], kind), "path": q, "of": "models/" + name})
|
|
infos = M.assimp_info(ctx, [c["path"] for c in cases], raw=True)
|
|
for c, a in zip(cases, infos):
|
|
a.pop("min", None)
|
|
a.pop("max", None)
|
|
a.pop("perMesh", None)
|
|
if not a.get("failed"):
|
|
a.update(fbx.assimp_extra(ctx, c["path"]))
|
|
a.update(fbx.ptv_bounds(ctx, c["path"], tmp))
|
|
c["assimp"] = a
|
|
c["data"] = open(c["path"], "rb").read()
|
|
del c["path"]
|
|
ctx.emit("x_cases", cases)
|