#!/usr/bin/env python3 """Rebuild or check the client's res/brickdb.zip in the layout the LEGO Universe client (1.10.64) accepts. The client mounts brickdb.zip with LEGO Digital Designer's own zip reader (see docs/BrickDb.md). It is lenient about compression and entry order, but a few things other zip tools commonly do make it drop the whole brick database: a wrapping folder or "./" prefix, backslash separators, ZIP64 end records, and data descriptors whose local header still carries the sizes. This script always writes the same layout the shipped file uses. repack.py build SRC OUT.zip [--overlay DIR]... SRC is an extracted brickdb folder or a brickdb.zip; each --overlay folder (same layout) adds or replaces entries repack.py check FILE.zip [--res RES_DIR] report everything the client would reject; with --res also look for each primitive's geometry in brickprimitives/ Repacking the shipped brickdb.zip with no overlay gives back the identical file. """ import argparse import os import struct import sys import time import zipfile import zlib import xml.etree.ElementTree as ElementTree SIG_LOCAL = 0x04034B50 SIG_CENTRAL = 0x02014B50 SIG_END = 0x06054B50 SIG_END64 = 0x06064B50 SIG_END64_LOCATOR = 0x07064B50 # Where each file lives in the database and what the client does with it ROOT_FILES = ("info.xml", "Materials.xml") NUMBERED_DIRS = {"Primitives": ".xml", "Assemblies": ".lxfml"} def entry_order(name): """The shipped order: info.xml, Materials.xml, then primitives and assemblies together by design ID, then the rest.""" if name in ROOT_FILES: return (0, ROOT_FILES.index(name), 0, name) folder, _, file = name.partition("/") stem, ext = os.path.splitext(file) if folder in NUMBERED_DIRS and ext.lower() == NUMBERED_DIRS[folder] and stem.isdigit(): return (1, int(stem), 0 if folder == "Primitives" else 1, name) return (2, 0, 0, name.lower()) def read_source(src): """{name: (bytes, date_time)} from an extracted folder or a zip.""" entries = {} if os.path.isdir(src): for root, dirs, files in os.walk(src): dirs.sort() for file in files: path = os.path.join(root, file) name = os.path.relpath(path, src).replace(os.sep, "/") with open(path, "rb") as f: entries[name] = (f.read(), time.localtime(os.path.getmtime(path))[:6]) else: with zipfile.ZipFile(src) as z: for info in z.infolist(): if info.is_dir(): continue entries[info.filename.replace("\\", "/")] = (z.read(info), info.date_time) # drop a "./" prefix or a wrapping folder if the source has one entries = {(n[2:] if n.startswith("./") else n): v for n, v in entries.items()} tops = {n.split("/", 1)[0] for n in entries} if "info.xml" not in entries and len(tops) == 1 and tops.pop() + "/info.xml" in entries: top = next(iter(entries)).split("/", 1)[0] + "/" entries = {n[len(top):]: v for n, v in entries.items()} return entries def build(src, out, overlays): entries = read_source(src) for overlay in overlays: for name, value in read_source(overlay).items(): if name.startswith("brickprimitives/"): continue entries[name] = value missing = [n for n in ("info.xml",) if n not in entries] if missing or not any(n.startswith("Primitives/") for n in entries): sys.exit(f"{src}: no info.xml or Primitives/ at the top of the database") tmp = out + ".tmp" # Plain deflate at zlib's default level, no directory entries, no extra fields, no data descriptors, no ZIP64 with zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED, allowZip64=False) as z: for name in sorted(entries, key=entry_order): data, date_time = entries[name] info = zipfile.ZipInfo(name, date_time=max(tuple(date_time), (1980, 1, 1, 0, 0, 0))) info.compress_type = zipfile.ZIP_DEFLATED info.create_system = 3 info.external_attr = 0o600 << 16 z.writestr(info, data) os.replace(tmp, out) report(check(out, None), "warning: ") print(f"wrote {out}: {len(entries)} entries") def check(path, res): """Everything the client's zip reader and brick database would reject, as a list of messages.""" problems = [] data = open(path, "rb").read() end = data.rfind(struct.pack(" end or cd_offset > len(data): return problems + ["central directory offset is outside the file (data before the zip?)"] names = set() owners = {} # design ID or alias -> primitives that claim it pos = cd_offset for _ in range(count): if struct.unpack_from(" 1: problems.append(f"design ID/alias {i} is claimed by {', '.join(claimed)}") lower = {n.lower() for n in names} if "info.xml" not in lower: problems.append("no info.xml at the top level") primitives = sorted(n for n in names if n.lower().startswith("primitives/") and n.lower().endswith(".xml")) if not primitives: problems.append("no Primitives/*.xml at the top level (wrapping folder?); the client drops the whole database") if res: for n in primitives: stem = os.path.splitext(n.split("/")[-1])[0] if stem.isdigit() and not os.path.exists(os.path.join(res, "brickprimitives", "lod0", stem + ".g")): problems.append(f"{n}: no brickprimitives/lod0/{stem}.g") return problems def report(problems, prefix=""): """Prints the problems, at most three entries for each kind.""" kinds = {} for p in problems: kind = p.split(": ", 1)[1] if p.startswith("'") and ": " in p else p kinds.setdefault(kind, []).append(p) for kind, items in kinds.items(): for p in items[:3]: print(prefix + p) if len(items) > 3: print(f"{prefix}... and {len(items) - 3} more entries: {kind}") print(f"{len(problems)} problem(s)") def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) sub = parser.add_subparsers(dest="command", required=True) b = sub.add_parser("build", help="write a brickdb.zip the client accepts") b.add_argument("src") b.add_argument("out") b.add_argument("--overlay", action="append", default=[]) c = sub.add_parser("check", help="list what the client would reject") c.add_argument("zip") c.add_argument("--res", help="the client's res folder, to look for geometry") args = parser.parse_args() if args.command == "build": build(args.src, args.out, args.overlay) else: problems = check(args.zip, args.res) report(problems) sys.exit(1 if problems else 0) if __name__ == "__main__": main()