"""Build a public TCH2 preview without later lessons' motion or speech. The complete outline and item order stay stable for progress and purchases. Locked entries carry source=2 and no timeline; the host refuses to play them. Only Foundations and the configured numbered lessons retain actual keyframes. """ import io from pathlib import Path import re import shutil import struct import zipfile ROOT = Path(__file__).resolve().parents[1] LIMIT = 64 * 1024 * 1024 LOCKED_SOURCE = 2 def free_lesson_count(): source = (ROOT / 'apps/practice/course_access.zi').read_text() match = re.search(r'^TAI_CHI_FREE_FORM_LESSONS :: ([1-8])$', source, re.M) if match is None: raise ValueError('missing Tai Chi free lesson boundary') return int(match[1]) def item_free(identifier): if identifier.startswith('foundations/'): return len(identifier) > len('foundations/') match = re.fullmatch(r'lesson-([1-8])/.+', identifier) return match is not None and int(match[1]) <= free_lesson_count() class Reader: def __init__(self, data): if len(data) > LIMIT: raise ValueError('Tai Chi course exceeds size bound') self.data = io.BytesIO(data) def take(self, size): if size < 0 or size > LIMIT: raise ValueError('invalid Tai Chi section size') value = self.data.read(size) if len(value) != size: raise ValueError('truncated Tai Chi course') return value def count(self, bound): value = struct.unpack(' bound: raise ValueError('Tai Chi section exceeds count bound') return value def read_course(data): reader = Reader(data) if reader.take(4) != b'TCH2': raise ValueError('unsupported Tai Chi course') strings = [reader.take(struct.unpack('= len(strings): raise ValueError('invalid Tai Chi string reference') references.add(index) for keys in sets: if len(keys) < 2: raise ValueError('free Tai Chi timeline is empty') for key in keys: reference(integer(key, 4)) for header, images in characters: reference(integer(header)) reference(integer(header, 4)) for image in images: reference(integer(image)) reference(integer(image, 4)) outline = [] for title, lessons in modules: reference(integer(title)) copied_lessons = [] for heading, items in lessons: for offset in (0, 4, 8): reference(integer(heading, offset)) copied_items = [] for header, marks in items: header = bytearray(header) for offset in (0, 4, 8): reference(integer(header, offset)) if item_free(strings[integer(header)]): if header[15] == 1: struct.pack_into('= 0) else: spoken.update(strings[integer(mark)] for mark in marks if integer(mark) >= 0) else: header[15] = LOCKED_SOURCE header[16] = 0 struct.pack_into('