"""Check serialized root entity IDs required for reproducible prefab replication.""" import re def root_id(text): # Braces in resource strings and comments must not change object depth. tokens = re.findall(r'//[^\n]*|/\*[\s\S]*?\*/|"(?:\\.|[^"\\])*"|[{}]|\b\w+\b', text) tokens = [token for token in tokens if not token.startswith(('//', '/*'))] depth = 0 identities = [] for index, token in enumerate(tokens): if token == '{': depth += 1 elif token == '}': depth -= 1 elif token == 'ID' and depth == 1: identities.append(tokens[index + 1] if index + 1 < len(tokens) else '') if len(identities) != 1 or not re.fullmatch(r'"[A-F0-9]{16}"', identities[0]): raise ValueError('Prefab needs exactly one saved root entity ID') identity = identities[0][1:-1] if identity == '0000000000000000': raise ValueError('Prefab root entity ID must not be zero') return identity def check_prefabs(paths): seen = {} for path in sorted(paths): try: identity = root_id(path.read_text()) except ValueError as error: raise ValueError(f'{path}: {error}') from error if identity in seen: raise ValueError(f'Duplicate prefab root ID {identity}: {seen[identity]} and {path}') seen[identity] = path return len(seen)