f3d2solidcraft 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- f3d2solidcraft/__init__.py +3 -0
- f3d2solidcraft/__main__.py +3 -0
- f3d2solidcraft/_vendor/__init__.py +0 -0
- f3d2solidcraft/_vendor/ezf3d/LICENSE +21 -0
- f3d2solidcraft/_vendor/ezf3d/__init__.py +1 -0
- f3d2solidcraft/_vendor/ezf3d/container/__init__.py +1 -0
- f3d2solidcraft/_vendor/ezf3d/container/archive.py +204 -0
- f3d2solidcraft/_vendor/ezf3d/model/__init__.py +1 -0
- f3d2solidcraft/_vendor/ezf3d/model/parameters.py +365 -0
- f3d2solidcraft/_vendor/ezf3d/model/sketch.py +645 -0
- f3d2solidcraft/_vendor/ezf3d/model/timeline.py +493 -0
- f3d2solidcraft/_vendor/ezf3d/streams/__init__.py +1 -0
- f3d2solidcraft/_vendor/ezf3d/streams/manifest.py +262 -0
- f3d2solidcraft/_vendor/ezf3d/streams/primitives.py +191 -0
- f3d2solidcraft/_vendor/ezf3d/streams/segment.py +521 -0
- f3d2solidcraft/cli.py +574 -0
- f3d2solidcraft/engine.py +458 -0
- f3d2solidcraft/fallback.py +250 -0
- f3d2solidcraft/geometry.py +397 -0
- f3d2solidcraft/mappers/__init__.py +141 -0
- f3d2solidcraft/mappers/bodies.py +99 -0
- f3d2solidcraft/mappers/combine.py +73 -0
- f3d2solidcraft/mappers/components.py +542 -0
- f3d2solidcraft/mappers/edges.py +449 -0
- f3d2solidcraft/mappers/extrude.py +1501 -0
- f3d2solidcraft/mappers/faces.py +50 -0
- f3d2solidcraft/mappers/loft.py +149 -0
- f3d2solidcraft/mappers/outputs.py +73 -0
- f3d2solidcraft/mappers/params.py +134 -0
- f3d2solidcraft/mappers/pattern.py +68 -0
- f3d2solidcraft/mappers/planes.py +222 -0
- f3d2solidcraft/mappers/primitives.py +144 -0
- f3d2solidcraft/mappers/records.py +51 -0
- f3d2solidcraft/mappers/revolve.py +134 -0
- f3d2solidcraft/mappers/sheet.py +95 -0
- f3d2solidcraft/mappers/shell.py +180 -0
- f3d2solidcraft/mappers/sketch.py +455 -0
- f3d2solidcraft/mcp.py +218 -0
- f3d2solidcraft/model.py +503 -0
- f3d2solidcraft/oracle.py +905 -0
- f3d2solidcraft/pairing.py +105 -0
- f3d2solidcraft/reader/__init__.py +77 -0
- f3d2solidcraft/reader/cadir.py +832 -0
- f3d2solidcraft/reader/fusion.py +218 -0
- f3d2solidcraft/reader/locale.py +115 -0
- f3d2solidcraft/replay.py +322 -0
- f3d2solidcraft/report.py +107 -0
- f3d2solidcraft/tools.py +219 -0
- f3d2solidcraft/verify.py +186 -0
- f3d2solidcraft/visibility.py +95 -0
- f3d2solidcraft-0.3.0.dist-info/METADATA +189 -0
- f3d2solidcraft-0.3.0.dist-info/RECORD +74 -0
- f3d2solidcraft-0.3.0.dist-info/WHEEL +4 -0
- f3d2solidcraft-0.3.0.dist-info/entry_points.txt +5 -0
- f3d2solidcraft-0.3.0.dist-info/licenses/LICENSE +21 -0
- f3d2solidcraft-0.3.0.dist-info/licenses/src/f3d2solidcraft/_vendor/ezf3d/LICENSE +21 -0
- f3d2solidcraft_app/__init__.py +5 -0
- f3d2solidcraft_app/__main__.py +40 -0
- f3d2solidcraft_app/engines.py +158 -0
- f3d2solidcraft_app/events.py +35 -0
- f3d2solidcraft_app/opener.py +22 -0
- f3d2solidcraft_app/outcome.py +54 -0
- f3d2solidcraft_app/resources/icon-128.png +0 -0
- f3d2solidcraft_app/resources/icon-16.png +0 -0
- f3d2solidcraft_app/resources/icon-256.png +0 -0
- f3d2solidcraft_app/resources/icon-32.png +0 -0
- f3d2solidcraft_app/resources/icon-512.png +0 -0
- f3d2solidcraft_app/resources/icon-64.png +0 -0
- f3d2solidcraft_app/resources/icon.icns +0 -0
- f3d2solidcraft_app/resources/icon.ico +0 -0
- f3d2solidcraft_app/resources/icon.svg +26 -0
- f3d2solidcraft_app/runner.py +91 -0
- f3d2solidcraft_app/texts.py +98 -0
- f3d2solidcraft_app/window.py +677 -0
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
"""Readers for a segment's ``MetaStream.dat`` / ``BulkStream.dat`` pair.
|
|
2
|
+
|
|
3
|
+
A *segment* is one subsystem's slice of the document — the design timeline, the
|
|
4
|
+
browser tree, the assembly-context tree. Each is stored as two files:
|
|
5
|
+
|
|
6
|
+
``MetaStream.dat``
|
|
7
|
+
The index, and it earns the name: after the header it holds one record per
|
|
8
|
+
saved module state, then a table mapping **object id to byte offset in the
|
|
9
|
+
bulk stream**. Because the offsets ascend with the ids, consecutive
|
|
10
|
+
entries delimit each record — so the bulk stream is randomly addressable
|
|
11
|
+
and its objects have known extents.
|
|
12
|
+
|
|
13
|
+
``BulkStream.dat``
|
|
14
|
+
The payload: a typed object graph, **uncompressed**, whose records carry
|
|
15
|
+
readable meta-type names (``DcExtrudeFeatureMetaType``, ``SketchesRoot``,
|
|
16
|
+
...).
|
|
17
|
+
|
|
18
|
+
**Those names are a dictionary, not a timeline.** Fusion writes each kind once,
|
|
19
|
+
sorted by name, and the objects index into that — so a design that declares
|
|
20
|
+
``DcExtrudeFeatureMetaType`` has *some* number of extrudes, and the stream does
|
|
21
|
+
not say how many at that point. Counting declarations and calling the result a
|
|
22
|
+
feature census is the mistake this module used to make; see
|
|
23
|
+
:meth:`BulkStream.feature_registries`.
|
|
24
|
+
|
|
25
|
+
Headers, the meta record chain and the object index are decoded here. The
|
|
26
|
+
*contents* of a bulk object still need a schema-versioned decoder; until then
|
|
27
|
+
:meth:`Segment.object_bytes` hands back exactly the bytes of one.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import re
|
|
33
|
+
import struct
|
|
34
|
+
from collections import Counter
|
|
35
|
+
from dataclasses import dataclass, field
|
|
36
|
+
|
|
37
|
+
from f3d2solidcraft._vendor.ezf3d.streams.primitives import Reader, StreamError, scan_strings
|
|
38
|
+
|
|
39
|
+
#: The two-entry table every meta stream ends with, naming the schema revision
|
|
40
|
+
#: of the subsystems that wrote it.
|
|
41
|
+
FOOTER_NAMES = ("Application", "Server")
|
|
42
|
+
|
|
43
|
+
#: Longest ``str8`` treated as a name while walking meta records. Guids run to
|
|
44
|
+
#: 37 characters -- Fusion writes at least one with five dashes rather than
|
|
45
|
+
#: four -- and module names are shorter still.
|
|
46
|
+
_MAX_META_NAME = 256
|
|
47
|
+
|
|
48
|
+
#: Same sentinel as the document manifest; absent in older revisions, so the
|
|
49
|
+
#: version block is located by validating what follows it.
|
|
50
|
+
META_COOKIE = 1234
|
|
51
|
+
|
|
52
|
+
#: Candidate widths, in words, of the version block before the type/owner pair.
|
|
53
|
+
_VERSION_BLOCK_WORDS = (4, 2, 6)
|
|
54
|
+
|
|
55
|
+
#: Type names Fusion uses in the bulk stream. ``Dc*MetaType`` are timeline
|
|
56
|
+
#: features and sketches, ``*Root`` are the containers that own them, and
|
|
57
|
+
#: ``IntrinsicMetaType*`` declares a scalar type such as ``uint64``.
|
|
58
|
+
#:
|
|
59
|
+
#: Matched against **decoded strings**, not raw bytes, and matched whole. Run
|
|
60
|
+
#: over raw bytes it truncates: every one of SUCKER's 1,118 hits for
|
|
61
|
+
#: ``IntrinsicMetaType`` is really ``IntrinsicMetaTypeuint64``, and reporting
|
|
62
|
+
#: the prefix turned a scalar-type declaration into a phantom timeline feature.
|
|
63
|
+
#: The trailing ``[A-Za-z0-9_]*`` is what keeps those names whole.
|
|
64
|
+
TYPE_NAME_RE = re.compile(
|
|
65
|
+
r"[A-Za-z][A-Za-z0-9_]{3,60}(?:MetaType|Root|Manager|Attributes)[A-Za-z0-9_]*"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
#: Largest per-kind counter treated as real. Guards against reading eight
|
|
69
|
+
#: bytes of something else as a tally; the largest seen is 171.
|
|
70
|
+
_MAX_FEATURE_COUNTER = 1_000_000
|
|
71
|
+
|
|
72
|
+
#: Prefix Fusion gives a timeline feature's meta-type. ``PassiveRefMetaType``,
|
|
73
|
+
#: ``StrongRefMetaType`` and the intrinsics also end in ``MetaType`` and are not
|
|
74
|
+
#: features, so the prefix is what selects rather than the suffix.
|
|
75
|
+
FEATURE_PREFIX = "Dc"
|
|
76
|
+
FEATURE_SUFFIX = "MetaType"
|
|
77
|
+
|
|
78
|
+
#: How the design graph names a B-Rep body: by its blob filename. Current
|
|
79
|
+
#: documents write it as UTF-16LE, older ones as ASCII, so both are searched.
|
|
80
|
+
BREP_REF_RE = re.compile(rb"BREP\.[0-9a-fA-F-]{36}\.smbh?")
|
|
81
|
+
BREP_REF_WIDE_RE = re.compile(
|
|
82
|
+
rb"B\x00R\x00E\x00P\x00\.\x00(?:[0-9a-fA-F-]\x00){36}\.\x00s\x00m\x00b\x00(?:h\x00)?"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@dataclass(frozen=True, slots=True)
|
|
87
|
+
class MetaRecord:
|
|
88
|
+
"""One entry of a segment's meta record list.
|
|
89
|
+
|
|
90
|
+
Two GUIDs, and what separates them is how they repeat. :attr:`identity`
|
|
91
|
+
is unique across the list in all fourteen segments of the samples -- 299
|
|
92
|
+
distinct across Robotic_Bhujha's 299 records. :attr:`group` repeats
|
|
93
|
+
heavily, 45 distinct values over the wheel's 167 records with one of them
|
|
94
|
+
used 22 times, so it names something several records share.
|
|
95
|
+
|
|
96
|
+
The first few records of a segment look like a chain, each ``group``
|
|
97
|
+
turning up as the next record's ``identity``. That does not survive the
|
|
98
|
+
whole list -- 44 of the wheel's 167 -- so it is a coincidence of the
|
|
99
|
+
opening records rather than the structure, and no chain is claimed here.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
#: This record's own GUID; unique within the segment.
|
|
103
|
+
identity: str
|
|
104
|
+
#: A GUID several records share. What it names is not established.
|
|
105
|
+
group: str
|
|
106
|
+
#: Small enum, 0-4. Meaning not established.
|
|
107
|
+
kind: int
|
|
108
|
+
#: Subsystem that wrote it -- ``Fusion``, ``Geometry``, ``EntityTracking``,
|
|
109
|
+
#: ``Component``, ``Scene``, ``Body``, ``MSketch``, ``CommonData``.
|
|
110
|
+
owner: str
|
|
111
|
+
#: Object ids this record refers to.
|
|
112
|
+
ids: tuple[int, ...] = ()
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@dataclass(slots=True)
|
|
116
|
+
class MetaStream:
|
|
117
|
+
"""Parsed ``MetaStream.dat``: the header, the record chain and the index."""
|
|
118
|
+
|
|
119
|
+
#: Folder prefix this segment was written under -- ``Design``, ``ACT``, or
|
|
120
|
+
#: the full type name. Matches the asset manifest's declaration prefix.
|
|
121
|
+
prefix: str
|
|
122
|
+
#: Slot id from the asset manifest's segment table.
|
|
123
|
+
slot: int
|
|
124
|
+
#: Identity GUID; all-zero for a segment that has never been branched.
|
|
125
|
+
guid: str
|
|
126
|
+
reserved: tuple[int, ...] = ()
|
|
127
|
+
#: Fully-qualified segment type, e.g. ``FusionDesignSegmentType``.
|
|
128
|
+
declared_type: str = ""
|
|
129
|
+
#: Owning subsystem, e.g. ``Fusion``.
|
|
130
|
+
owner: str = ""
|
|
131
|
+
#: Offset at which the header ends and per-module records begin.
|
|
132
|
+
body_offset: int = 0
|
|
133
|
+
body: bytes = b""
|
|
134
|
+
#: Record count the header declares, which the list always matches.
|
|
135
|
+
declared_records: int = 0
|
|
136
|
+
#: The records, in file order.
|
|
137
|
+
records: list[MetaRecord] = field(default_factory=list)
|
|
138
|
+
#: Object ids the segment names as its roots.
|
|
139
|
+
roots: tuple[int, ...] = ()
|
|
140
|
+
#: **Object id to byte offset in the bulk stream.** Ascending in both, so
|
|
141
|
+
#: consecutive entries give each object's extent.
|
|
142
|
+
index: dict[int, int] = field(default_factory=dict)
|
|
143
|
+
#: One past the highest object id ever issued -- 453 in a segment whose
|
|
144
|
+
#: largest indexed id is 452.
|
|
145
|
+
next_id: int = 0
|
|
146
|
+
#: Schema revision per subsystem, from the footer.
|
|
147
|
+
schema: dict[str, int] = field(default_factory=dict)
|
|
148
|
+
#: Bytes between the index and the footer that this reader did not
|
|
149
|
+
#: account for. Zero in the plain documents; the ``.f3z`` members carry a
|
|
150
|
+
#: further section holding a wide GUID, which is counted rather than
|
|
151
|
+
#: guessed at.
|
|
152
|
+
unread: int = 0
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@dataclass(slots=True)
|
|
156
|
+
class BulkStream:
|
|
157
|
+
"""Parsed header of a ``BulkStream.dat``."""
|
|
158
|
+
|
|
159
|
+
#: Schema revision as Fusion writes it — a numeric string such as ``"397"``.
|
|
160
|
+
version: str
|
|
161
|
+
#: Zero in most streams, 2 in browser streams; role not yet established.
|
|
162
|
+
flags: int = 0
|
|
163
|
+
body_offset: int = 0
|
|
164
|
+
body: bytes = b""
|
|
165
|
+
|
|
166
|
+
def named_types(self) -> list[tuple[int, str]]:
|
|
167
|
+
"""``(offset, name)`` for every type name in the payload, in file order.
|
|
168
|
+
|
|
169
|
+
Anchored on :func:`scan_strings`, so every entry is a real
|
|
170
|
+
length-prefixed string at the offset given rather than a byte sequence
|
|
171
|
+
that happens to spell one.
|
|
172
|
+
"""
|
|
173
|
+
return [
|
|
174
|
+
(found.offset, found.value)
|
|
175
|
+
for found in scan_strings(self.body, min_len=4)
|
|
176
|
+
if TYPE_NAME_RE.fullmatch(found.value)
|
|
177
|
+
]
|
|
178
|
+
|
|
179
|
+
def type_names(self) -> Counter[str]:
|
|
180
|
+
"""Census of the type names the payload declares."""
|
|
181
|
+
return Counter(name for _, name in self.named_types())
|
|
182
|
+
|
|
183
|
+
def feature_registries(self) -> list[list[str]]:
|
|
184
|
+
"""The feature dictionaries, in file order, each one sorted by name.
|
|
185
|
+
|
|
186
|
+
Fusion writes the timeline's meta-types as a registry — one entry per
|
|
187
|
+
kind, alphabetical, that the objects index into. A design can hold
|
|
188
|
+
several: Robotic_Bhujha has eleven, of 2 to 14 entries each.
|
|
189
|
+
|
|
190
|
+
Two measurements say a *count* of these names means nothing. No
|
|
191
|
+
registry repeats a name, and every name's total across the stream
|
|
192
|
+
equals the number of registries that declare it — so Robotic_Bhujha's
|
|
193
|
+
nine ``DcExtrudeFeatureMetaType`` are nine registries that each allow
|
|
194
|
+
an extrude, not nine extrudes. That number is the one this project's
|
|
195
|
+
own README used to advertise as a feature census.
|
|
196
|
+
"""
|
|
197
|
+
blocks: list[list[str]] = []
|
|
198
|
+
current: list[str] = []
|
|
199
|
+
for _, name in self.named_types():
|
|
200
|
+
if is_feature_type(name):
|
|
201
|
+
current.append(name)
|
|
202
|
+
continue
|
|
203
|
+
if current:
|
|
204
|
+
blocks.append(current)
|
|
205
|
+
current = []
|
|
206
|
+
if current:
|
|
207
|
+
blocks.append(current)
|
|
208
|
+
return blocks
|
|
209
|
+
|
|
210
|
+
def declared_feature_types(self) -> set[str]:
|
|
211
|
+
"""The timeline feature kinds this design declares, prefix and suffix stripped.
|
|
212
|
+
|
|
213
|
+
A **set**, because the stream declares each kind once per registry.
|
|
214
|
+
How many of a kind a design has is :meth:`feature_counters`, which
|
|
215
|
+
reads the number Fusion writes beside each name.
|
|
216
|
+
"""
|
|
217
|
+
return {
|
|
218
|
+
name[len(FEATURE_PREFIX) : -len(FEATURE_SUFFIX)]
|
|
219
|
+
for _, name in self.named_types()
|
|
220
|
+
if is_feature_type(name)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
def feature_counters(self) -> Counter[str]:
|
|
224
|
+
"""How many of each kind the registries say were issued.
|
|
225
|
+
|
|
226
|
+
Each registry entry is ``str8 name`` followed by a ``u64``, and that
|
|
227
|
+
number is Fusion's per-kind counter -- what its timeline labels count
|
|
228
|
+
up with. Summed across a design's registries it is an *ever-created*
|
|
229
|
+
tally, so it is an upper bound on the live timeline rather than a
|
|
230
|
+
census of it: SUCKER declares 83 and its timeline holds 58.
|
|
231
|
+
|
|
232
|
+
This corrects what this module used to say. Counting the *names* is
|
|
233
|
+
still meaningless -- each registry writes each kind once -- but there
|
|
234
|
+
was a count beside them all along, and it was being stepped over.
|
|
235
|
+
"""
|
|
236
|
+
counters: Counter[str] = Counter()
|
|
237
|
+
for offset, name in self.named_types():
|
|
238
|
+
if not is_feature_type(name):
|
|
239
|
+
continue
|
|
240
|
+
at = offset + 4 + len(name)
|
|
241
|
+
if at + 8 > len(self.body):
|
|
242
|
+
continue
|
|
243
|
+
value = int.from_bytes(self.body[at : at + 8], "little")
|
|
244
|
+
if value <= _MAX_FEATURE_COUNTER:
|
|
245
|
+
counters[name[len(FEATURE_PREFIX) : -len(FEATURE_SUFFIX)]] += value
|
|
246
|
+
return counters
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def is_feature_type(name: str) -> bool:
|
|
250
|
+
"""True for a timeline feature's meta-type, e.g. ``DcExtrudeFeatureMetaType``."""
|
|
251
|
+
return name.startswith(FEATURE_PREFIX) and name.endswith(FEATURE_SUFFIX)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
@dataclass(frozen=True, slots=True)
|
|
255
|
+
class BulkObject:
|
|
256
|
+
"""One object of the bulk stream, located by the meta stream's index."""
|
|
257
|
+
|
|
258
|
+
#: Object id, as the meta stream's records refer to it.
|
|
259
|
+
oid: int
|
|
260
|
+
offset: int
|
|
261
|
+
size: int
|
|
262
|
+
|
|
263
|
+
@property
|
|
264
|
+
def end(self) -> int:
|
|
265
|
+
return self.offset + self.size
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
@dataclass(slots=True)
|
|
269
|
+
class Segment:
|
|
270
|
+
"""A segment's two streams, parsed as far as Phase 1 goes."""
|
|
271
|
+
|
|
272
|
+
name: str
|
|
273
|
+
segment_type: str
|
|
274
|
+
meta: MetaStream
|
|
275
|
+
bulk: BulkStream
|
|
276
|
+
#: Bytes on disk, before decompression, for size reporting.
|
|
277
|
+
meta_size: int = 0
|
|
278
|
+
bulk_size: int = 0
|
|
279
|
+
|
|
280
|
+
@property
|
|
281
|
+
def is_design(self) -> bool:
|
|
282
|
+
"""True for a design segment.
|
|
283
|
+
|
|
284
|
+
The type is ``FusionDesignSegmentType`` in current documents,
|
|
285
|
+
``AnimationDesignSegmentType`` for an animation asset, and plain
|
|
286
|
+
``Design`` in documents written before the type names were qualified.
|
|
287
|
+
"""
|
|
288
|
+
return "Design" in (self.segment_type or self.meta.declared_type)
|
|
289
|
+
|
|
290
|
+
def body_refs(self) -> list[str]:
|
|
291
|
+
"""B-Rep blob filenames this segment references, in first-seen order.
|
|
292
|
+
|
|
293
|
+
The design graph names its bodies by blob file, which is how a
|
|
294
|
+
``BREP.<uuid>.smb`` in ``Breps.BlobParts`` is tied back to the
|
|
295
|
+
component that owns it.
|
|
296
|
+
"""
|
|
297
|
+
seen: dict[str, None] = {}
|
|
298
|
+
for match in BREP_REF_WIDE_RE.findall(self.bulk.body):
|
|
299
|
+
seen.setdefault(match.decode("utf-16-le"), None)
|
|
300
|
+
for match in BREP_REF_RE.findall(self.bulk.body):
|
|
301
|
+
seen.setdefault(match.decode("ascii"), None)
|
|
302
|
+
return list(seen)
|
|
303
|
+
|
|
304
|
+
def objects(self) -> list[BulkObject]:
|
|
305
|
+
"""Every indexed object, in bulk-stream order, with its extent.
|
|
306
|
+
|
|
307
|
+
The index gives each object's start; the next object's start gives its
|
|
308
|
+
end, and the last runs to the end of the payload. Offsets ascend with
|
|
309
|
+
ids and never repeat, in every sample.
|
|
310
|
+
"""
|
|
311
|
+
ordered = sorted(self.meta.index.items(), key=lambda item: item[1])
|
|
312
|
+
limit = len(self.bulk.body)
|
|
313
|
+
return [
|
|
314
|
+
BulkObject(
|
|
315
|
+
oid=oid,
|
|
316
|
+
offset=offset,
|
|
317
|
+
size=(ordered[i + 1][1] if i + 1 < len(ordered) else limit) - offset,
|
|
318
|
+
)
|
|
319
|
+
for i, (oid, offset) in enumerate(ordered)
|
|
320
|
+
]
|
|
321
|
+
|
|
322
|
+
def object_bytes(self, oid: int) -> bytes:
|
|
323
|
+
"""Exactly the bytes of one object, or ``b""`` if it is not indexed."""
|
|
324
|
+
for item in self.objects():
|
|
325
|
+
if item.oid == oid:
|
|
326
|
+
return self.bulk.body[item.offset : item.end]
|
|
327
|
+
return b""
|
|
328
|
+
|
|
329
|
+
def strings(self, min_len: int = 4) -> list[str]:
|
|
330
|
+
"""Distinct printable strings in the bulk payload, in first-seen order."""
|
|
331
|
+
seen: dict[str, None] = {}
|
|
332
|
+
for found in scan_strings(self.bulk.body, min_len=min_len):
|
|
333
|
+
seen.setdefault(found.value, None)
|
|
334
|
+
return list(seen)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def _readable(text: str) -> bool:
|
|
338
|
+
return bool(text) and text.isascii() and text.isidentifier()
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def _read_version_block(r: Reader) -> tuple[tuple[int, ...], str, str]:
|
|
342
|
+
"""Read the version words plus the segment type and owner that follow.
|
|
343
|
+
|
|
344
|
+
Width varies by document revision, so each candidate is accepted only when
|
|
345
|
+
two readable identifiers decode immediately after it.
|
|
346
|
+
"""
|
|
347
|
+
anchor = r.pos
|
|
348
|
+
for words in _VERSION_BLOCK_WORDS:
|
|
349
|
+
r.seek(anchor)
|
|
350
|
+
try:
|
|
351
|
+
reserved = tuple(r.u32() for _ in range(words))
|
|
352
|
+
declared_type = r.str8()
|
|
353
|
+
owner = r.str8()
|
|
354
|
+
except StreamError:
|
|
355
|
+
continue
|
|
356
|
+
# Older revisions leave the owner blank, so only the type must read.
|
|
357
|
+
if _readable(declared_type) and (not owner or _readable(owner)):
|
|
358
|
+
return reserved, declared_type, owner
|
|
359
|
+
raise StreamError(f"no readable segment type after the meta stream header at {anchor}")
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def _name_at(body: bytes, pos: int) -> tuple[str, int] | None:
|
|
363
|
+
"""A ``str8`` at *pos*, or ``None`` if one does not start there.
|
|
364
|
+
|
|
365
|
+
Empty is a legitimate value -- some records name no group and some no
|
|
366
|
+
owner -- so zero length reads as the empty string rather than a failure.
|
|
367
|
+
"""
|
|
368
|
+
if pos < 0 or pos + 4 > len(body):
|
|
369
|
+
return None
|
|
370
|
+
size = int.from_bytes(body[pos : pos + 4], "little")
|
|
371
|
+
if size > _MAX_META_NAME or pos + 4 + size > len(body):
|
|
372
|
+
return None
|
|
373
|
+
raw = body[pos + 4 : pos + 4 + size]
|
|
374
|
+
if any(char < 0x20 or char >= 0x7F for char in raw):
|
|
375
|
+
return None
|
|
376
|
+
return raw.decode("ascii"), pos + 4 + size
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def _read_footer(body: bytes) -> tuple[dict[str, int], int]:
|
|
380
|
+
"""``({subsystem: revision}, offset it starts at)`` from the end of *body*.
|
|
381
|
+
|
|
382
|
+
Located by searching backwards for its first name rather than by counting
|
|
383
|
+
forwards, so an unread section before it cannot hide it.
|
|
384
|
+
"""
|
|
385
|
+
for pos in range(len(body) - 8, 0, -1):
|
|
386
|
+
found = _name_at(body, pos)
|
|
387
|
+
if found is None or found[0] != FOOTER_NAMES[0]:
|
|
388
|
+
continue
|
|
389
|
+
start = pos - 4
|
|
390
|
+
schema: dict[str, int] = {}
|
|
391
|
+
cursor = pos
|
|
392
|
+
for _ in FOOTER_NAMES:
|
|
393
|
+
entry = _name_at(body, cursor)
|
|
394
|
+
if entry is None or cursor + 4 > len(body):
|
|
395
|
+
return {}, len(body)
|
|
396
|
+
name, cursor = entry
|
|
397
|
+
if cursor + 4 > len(body):
|
|
398
|
+
return {}, len(body)
|
|
399
|
+
schema[name] = int.from_bytes(body[cursor : cursor + 4], "little")
|
|
400
|
+
cursor += 4
|
|
401
|
+
return schema, start
|
|
402
|
+
return {}, len(body)
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def _read_meta_body(stream: MetaStream) -> None:
|
|
406
|
+
"""Fill in the record chain, the roots, the object index and the footer.
|
|
407
|
+
|
|
408
|
+
Laid out as::
|
|
409
|
+
|
|
410
|
+
u32 ?, u32 ?, u32 record_count
|
|
411
|
+
record_count x { str8 identity, str8 group, u32 kind,
|
|
412
|
+
str8 owner, u32 n, n x u64 ids }
|
|
413
|
+
u32 n_roots, n_roots x u64
|
|
414
|
+
{ u32 n, n x (u64 object_id, u64 bulk_offset) } ...
|
|
415
|
+
u64 next_id, u32 0
|
|
416
|
+
u32 2, { str8 subsystem, u32 revision } x 2
|
|
417
|
+
"""
|
|
418
|
+
body = stream.body
|
|
419
|
+
if len(body) < 12:
|
|
420
|
+
return
|
|
421
|
+
stream.declared_records = int.from_bytes(body[8:12], "little")
|
|
422
|
+
stream.schema, footer = _read_footer(body)
|
|
423
|
+
|
|
424
|
+
pos = 12
|
|
425
|
+
for _ in range(stream.declared_records):
|
|
426
|
+
identity = _name_at(body, pos)
|
|
427
|
+
if identity is None:
|
|
428
|
+
break
|
|
429
|
+
group = _name_at(body, identity[1])
|
|
430
|
+
if group is None or group[1] + 4 > len(body):
|
|
431
|
+
break
|
|
432
|
+
kind = int.from_bytes(body[group[1] : group[1] + 4], "little")
|
|
433
|
+
owner = _name_at(body, group[1] + 4)
|
|
434
|
+
if owner is None or owner[1] + 4 > len(body):
|
|
435
|
+
break
|
|
436
|
+
count = int.from_bytes(body[owner[1] : owner[1] + 4], "little")
|
|
437
|
+
start = owner[1] + 4
|
|
438
|
+
if count > len(body) // 8 or start + 8 * count > len(body):
|
|
439
|
+
break
|
|
440
|
+
ids = tuple(
|
|
441
|
+
int.from_bytes(body[start + 8 * i : start + 8 * i + 8], "little") for i in range(count)
|
|
442
|
+
)
|
|
443
|
+
stream.records.append(
|
|
444
|
+
MetaRecord(identity=identity[0], group=group[0], kind=kind, owner=owner[0], ids=ids)
|
|
445
|
+
)
|
|
446
|
+
pos = start + 8 * count
|
|
447
|
+
|
|
448
|
+
if pos + 4 <= footer:
|
|
449
|
+
roots = int.from_bytes(body[pos : pos + 4], "little")
|
|
450
|
+
end = pos + 4 + 8 * roots
|
|
451
|
+
if end <= footer:
|
|
452
|
+
stream.roots = struct.unpack_from(f"<{roots}Q", body, pos + 4) if roots else ()
|
|
453
|
+
pos = end
|
|
454
|
+
|
|
455
|
+
# The index arrives as one or more tables; later ones revise earlier ones,
|
|
456
|
+
# so they are merged in the order written.
|
|
457
|
+
while pos + 4 <= footer:
|
|
458
|
+
count = int.from_bytes(body[pos : pos + 4], "little")
|
|
459
|
+
end = pos + 4 + 16 * count
|
|
460
|
+
if end > footer:
|
|
461
|
+
break
|
|
462
|
+
flat = struct.unpack_from(f"<{2 * count}Q", body, pos + 4) if count else ()
|
|
463
|
+
stream.index.update(zip(flat[0::2], flat[1::2], strict=True))
|
|
464
|
+
pos = end
|
|
465
|
+
if count == 0:
|
|
466
|
+
break
|
|
467
|
+
|
|
468
|
+
if pos + 12 <= footer:
|
|
469
|
+
stream.next_id = int.from_bytes(body[pos : pos + 8], "little")
|
|
470
|
+
pos += 12
|
|
471
|
+
stream.unread = max(0, footer - pos)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def read_meta_stream(data: bytes) -> MetaStream:
|
|
475
|
+
"""Parse a ``MetaStream.dat``."""
|
|
476
|
+
r = Reader(data)
|
|
477
|
+
prefix = r.str8()
|
|
478
|
+
slot = r.u32()
|
|
479
|
+
guid = r.wstr()
|
|
480
|
+
reserved, declared_type, owner = _read_version_block(r)
|
|
481
|
+
stream = MetaStream(
|
|
482
|
+
prefix=prefix,
|
|
483
|
+
slot=slot,
|
|
484
|
+
guid=guid,
|
|
485
|
+
reserved=reserved,
|
|
486
|
+
declared_type=declared_type,
|
|
487
|
+
owner=owner,
|
|
488
|
+
body_offset=r.pos,
|
|
489
|
+
body=data[r.pos :],
|
|
490
|
+
)
|
|
491
|
+
_read_meta_body(stream)
|
|
492
|
+
return stream
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def read_bulk_stream(data: bytes) -> BulkStream:
|
|
496
|
+
"""Parse a ``BulkStream.dat`` header."""
|
|
497
|
+
r = Reader(data)
|
|
498
|
+
version = r.str8()
|
|
499
|
+
flags = r.u64()
|
|
500
|
+
return BulkStream(version=version, flags=flags, body_offset=r.pos, body=data[r.pos :])
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
def read_segment(
|
|
504
|
+
name: str,
|
|
505
|
+
meta_bytes: bytes,
|
|
506
|
+
bulk_bytes: bytes,
|
|
507
|
+
*,
|
|
508
|
+
segment_type: str = "",
|
|
509
|
+
) -> Segment:
|
|
510
|
+
"""Read both streams of one segment.
|
|
511
|
+
|
|
512
|
+
*segment_type* comes from the asset manifest's declaration; when it is not
|
|
513
|
+
supplied the meta stream's own ``declared_type`` is used instead.
|
|
514
|
+
"""
|
|
515
|
+
meta = read_meta_stream(meta_bytes)
|
|
516
|
+
return Segment(
|
|
517
|
+
name=name,
|
|
518
|
+
segment_type=segment_type or meta.declared_type,
|
|
519
|
+
meta=meta,
|
|
520
|
+
bulk=read_bulk_stream(bulk_bytes),
|
|
521
|
+
)
|