File size: 4,132 Bytes
92f480e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
"""Read scene metadata ONLY from Friction v34, little-endian, 64-bit-qreal files.

Does not parse layers, validate the whole project, render, or execute expressions.
Unsupported/malformed input is an inspection error, not an agent score.
"""
import argparse
import hashlib
import json
import math
from pathlib import Path
import struct


class InspectionError(ValueError):
    pass


def inspect(data: bytes) -> dict:
    if len(data) < 61:
        raise InspectionError('File too short for the supported header and footer')
    footer = len(data) - 49
    version = struct.unpack_from('<i', data, footer)[0]
    if data[footer + 4:footer + 19] != b'enve ev' + b'\0' * 8:
        raise InspectionError('Unrecognized native project footer')
    if version != 34:
        raise InspectionError(f'Unsupported format version {version}; only 34 is implemented')
    offset = 0

    def read(fmt):
        nonlocal offset
        size = struct.calcsize('<' + fmt)
        if offset + size > footer:
            raise InspectionError('Truncated scene metadata')
        if fmt == '?' and data[offset] not in (0, 1):
            raise InspectionError('Unsupported boolean representation')
        value = struct.unpack_from('<' + fmt, data, offset)[0]
        offset += size
        return value

    if read('q') != 0:
        raise InspectionError('Unexpected initial checkpoint / byte order')
    count = read('i')
    if not 0 <= count <= 10000:
        raise InspectionError('Invalid or unsupported scene count')
    scenes = []
    for index in range(count):
        current = read('i')
        clip = read('?')
        width, height = read('i'), read('i')
        fps = read('d')
        start, end = read('i'), read('i')
        in_enabled, in_frame = read('?'), read('i')
        out_enabled, out_frame = read('?'), read('i')
        markers_size = read('i')
        if markers_size == -1:  # Qt null QByteArray
            markers_size = 0
        if markers_size < 0 or offset + markers_size > footer:
            raise InspectionError('Truncated or invalid marker array')
        markers = data[offset:offset + markers_size]
        offset += markers_size
        if width <= 0 or height <= 0 or not math.isfinite(fps) or fps <= 0 or end < start:
            raise InspectionError('Invalid scene settings / unsupported binary layout')
        try:
            markers_text = markers.decode('utf-8')
        except UnicodeDecodeError as exc:
            raise InspectionError('Invalid UTF-8 scene markers') from exc
        scenes.append({
            'scene_index': index, 'current_frame': current, 'clip_to_canvas': clip,
            'width': width, 'height': height, 'fps': fps,
            'frame_range_inclusive': [start, end], 'frame_count': end - start + 1,
            'duration_seconds': (end - start + 1) / fps,
            'in_marker': {'enabled': in_enabled, 'frame': in_frame},
            'out_marker': {'enabled': out_enabled, 'frame': out_frame},
            'markers_raw': markers_text,
        })
    return {
        'inspector': 'friction-scene-header-v1', 'scope': 'scene_metadata_only',
        'full_project_validity': 'not_checked',
        'format_version': version, 'binary_layout': 'little_endian_qreal64',
        'artifact_sha256': hashlib.sha256(data).hexdigest(),
        'scene_count': count, 'scenes': scenes,
        'capabilities': {'scene_settings': True, 'layers': False, 'keyframes': False,
                         'expressions': False, 'effects': False, 'rendering': False,
                         'action_history': False},
    }


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('project', type=Path)
    parser.add_argument('--out', type=Path)
    args = parser.parse_args()
    try:
        result = inspect(args.project.read_bytes())
    except (OSError, InspectionError) as exc:
        parser.exit(2, f'Inspection error: {exc}\n')
    output = json.dumps(result, indent=2) + '\n'
    if args.out:
        args.out.write_text(output)
    else:
        print(output, end='')


if __name__ == '__main__':
    main()