@vim3d/core 0.2.2 → 0.2.4
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.
- package/README.md +7 -0
- package/compile.d.ts +6 -1
- package/compile.js +61 -52
- package/contexts.d.ts +5 -1
- package/contexts.js +45 -0
- package/esm/compile.js +63 -55
- package/esm/contexts.js +42 -0
- package/esm/evaluate.js +8 -2
- package/esm/index.js +1 -0
- package/esm/model.js +2 -1
- package/esm/storage.js +575 -0
- package/evaluate.js +8 -2
- package/index.d.ts +2 -0
- package/index.js +6 -1
- package/model.d.ts +20 -4
- package/model.js +4 -2
- package/package.json +3 -2
- package/storage.d.ts +36 -0
- package/storage.js +581 -0
package/storage.js
ADDED
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toStorageGraph = toStorageGraph;
|
|
4
|
+
exports.fromStorageGraph = fromStorageGraph;
|
|
5
|
+
exports.serializeStorageGraph = serializeStorageGraph;
|
|
6
|
+
exports.hashStorageGraph = hashStorageGraph;
|
|
7
|
+
const ch_operators_js_1 = require("./ch-operators.js");
|
|
8
|
+
const channels_js_1 = require("./channels.js");
|
|
9
|
+
const compile_js_1 = require("./compile.js");
|
|
10
|
+
const contexts_js_1 = require("./contexts.js");
|
|
11
|
+
const expression_js_1 = require("./expression.js");
|
|
12
|
+
const graph_js_1 = require("./graph.js");
|
|
13
|
+
const model_js_1 = require("./model.js");
|
|
14
|
+
const registry_js_1 = require("./registry.js");
|
|
15
|
+
const types_js_1 = require("./types.js");
|
|
16
|
+
const dataProp = '/data';
|
|
17
|
+
const linksProp = '/links';
|
|
18
|
+
const inputMarker = '$';
|
|
19
|
+
const scopeOf = (path) => path.split('/')[1];
|
|
20
|
+
const ownerOf = (path) => path.slice(0, path.lastIndexOf('/'));
|
|
21
|
+
const memberOf = (path) => path.slice(path.lastIndexOf('/') + 1);
|
|
22
|
+
const storeSpan = (span) => ({ ...span, end: span.end === Infinity ? null : span.end });
|
|
23
|
+
const loadSpan = (span) => ({ ...span, end: span.end === null ? Infinity : span.end });
|
|
24
|
+
function toStorageGraph(scene) {
|
|
25
|
+
const plan = model_js_1.plans.get(scene);
|
|
26
|
+
if (!plan)
|
|
27
|
+
(0, types_js_1.fail)('invalid-scene', 'Scene was not created by compileScene or fromStorageGraph');
|
|
28
|
+
const pending = new Map();
|
|
29
|
+
function node(path, category) {
|
|
30
|
+
const props = new Map();
|
|
31
|
+
const entry = {
|
|
32
|
+
path, category, props, inputs: new Map(), outputs: new Map(), links: [],
|
|
33
|
+
route: address => scopeOf(address) === scopeOf(path) ? address : inputMarker + address.slice(1),
|
|
34
|
+
};
|
|
35
|
+
pending.set(path, entry);
|
|
36
|
+
return entry;
|
|
37
|
+
}
|
|
38
|
+
function property(entry, name, value) {
|
|
39
|
+
entry.props.set(name, { name, value });
|
|
40
|
+
}
|
|
41
|
+
function output(entry, name, type, value = {}) {
|
|
42
|
+
property(entry, name, value);
|
|
43
|
+
entry.outputs.set(name, { name, type });
|
|
44
|
+
}
|
|
45
|
+
function expression(entry, ast) {
|
|
46
|
+
return (0, expression_js_1.bindExpression)(ast, entry.route);
|
|
47
|
+
}
|
|
48
|
+
const sceneData = {
|
|
49
|
+
fps: scene.fps, frameCount: scene.frameCount, ranges: scene.ranges,
|
|
50
|
+
contexts: plan.extensions.registry.contexts
|
|
51
|
+
.filter(({ stage }) => stage === 'evaluate' || stage === 'output').map(({ name }) => name).sort(graph_js_1.comparePaths),
|
|
52
|
+
events: plan.events.map(({ object, frame, command, args, source }) => ({
|
|
53
|
+
object: (0, model_js_1.objectPath)(object), frame, command, args, source,
|
|
54
|
+
})),
|
|
55
|
+
};
|
|
56
|
+
const settings = node('/@scene', 'scene');
|
|
57
|
+
property(settings, dataProp, sceneData);
|
|
58
|
+
for (const object of plan.objects) {
|
|
59
|
+
const path = (0, model_js_1.objectPath)(object);
|
|
60
|
+
const entry = node(path, object.kind);
|
|
61
|
+
const { source, kind, parent, geometry, geometrySource, pathBindings, aimBindings } = object;
|
|
62
|
+
const { parameters, ...authored } = object.authored;
|
|
63
|
+
const data = {
|
|
64
|
+
kind, source, authored, parent: parent && (0, model_js_1.objectPath)(parent), geometry,
|
|
65
|
+
geometrySource: geometrySource && entry.route(geometrySource),
|
|
66
|
+
paths: pathBindings.map(({ curve, position, freq, ...span }) => storeSpan({
|
|
67
|
+
...span, curve: (0, model_js_1.objectPath)(curve), ...(position ? { position: expression(entry, position) } : { freq }),
|
|
68
|
+
})),
|
|
69
|
+
aims: aimBindings.map(({ target, ...span }) => storeSpan({ ...span, target: (0, model_js_1.objectPath)(target) })),
|
|
70
|
+
};
|
|
71
|
+
property(entry, dataProp, data);
|
|
72
|
+
for (const name of Object.keys(parameters).sort(graph_js_1.comparePaths)) {
|
|
73
|
+
const drivers = (plan.drivers.get(path + '/' + name) ?? []).map(driver => driver.kind === 'expr' ? storeSpan({ ...driver, expression: expression(entry, driver.expression) })
|
|
74
|
+
: storeSpan({ ...driver, input: entry.route(driver.input) }));
|
|
75
|
+
output(entry, name, 'number', { value: parameters[name], drivers });
|
|
76
|
+
}
|
|
77
|
+
output(entry, 'worldMatrix', 'matrix4');
|
|
78
|
+
output(entry, 'localMatrix', 'matrix4');
|
|
79
|
+
output(entry, 'snapshot', 'object');
|
|
80
|
+
}
|
|
81
|
+
for (const channel of plan.channels) {
|
|
82
|
+
const path = (0, model_js_1.channelPath)(channel);
|
|
83
|
+
const entry = node(path, 'channel');
|
|
84
|
+
const { table, extend, ramp, live, source, operator } = channel;
|
|
85
|
+
property(entry, dataProp, {
|
|
86
|
+
table, extend, ramp, live, source,
|
|
87
|
+
operator: operator && { type: operator.definition.type, props: Object.entries(operator.props) },
|
|
88
|
+
});
|
|
89
|
+
for (const [name, port] of [...channel.ports].sort(([a], [b]) => (0, graph_js_1.comparePaths)(a, b))) {
|
|
90
|
+
output(entry, name, 'number', { ...port, expression: port.expression && expression(entry, port.expression) });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const edges = new Map();
|
|
94
|
+
const rootEdges = [];
|
|
95
|
+
const boundaries = new Map();
|
|
96
|
+
const addEdge = (scope, edge) => {
|
|
97
|
+
if (!edges.has(scope))
|
|
98
|
+
edges.set(scope, []);
|
|
99
|
+
edges.get(scope).push(edge);
|
|
100
|
+
};
|
|
101
|
+
function boundary(scope, kind, portName, dataType) {
|
|
102
|
+
if (!boundaries.has(scope))
|
|
103
|
+
boundaries.set(scope, new Map());
|
|
104
|
+
const ports = boundaries.get(scope);
|
|
105
|
+
const key = kind + '/' + portName;
|
|
106
|
+
const existing = ports.get(key);
|
|
107
|
+
if (existing)
|
|
108
|
+
return { name: existing.name, created: false };
|
|
109
|
+
let index = ports.size;
|
|
110
|
+
let name;
|
|
111
|
+
do
|
|
112
|
+
name = '@boundary-' + index++;
|
|
113
|
+
while (pending.has('/' + scope + '/' + name) || plan.extensions.nodes.has('/' + scope + '/' + name)
|
|
114
|
+
|| [...ports.values()].some(port => port.name === name));
|
|
115
|
+
ports.set(key, { name, type: kind, props: [{ name: 'portName', value: portName }, { name: 'dataType', value: dataType }] });
|
|
116
|
+
return { name, created: true };
|
|
117
|
+
}
|
|
118
|
+
function link(to, role, from, type) {
|
|
119
|
+
const entry = pending.get(to);
|
|
120
|
+
const scope = scopeOf(to);
|
|
121
|
+
const port = JSON.stringify(role);
|
|
122
|
+
if (ownerOf(from) === to) {
|
|
123
|
+
entry.links.push({ port, from });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
let src = { node: memberOf(ownerOf(from)), port: memberOf(from) };
|
|
127
|
+
const sourceScope = scopeOf(from);
|
|
128
|
+
if (sourceScope !== scope) {
|
|
129
|
+
const outputPort = from.slice(sourceScope.length + 2);
|
|
130
|
+
const inputPort = from.slice(1);
|
|
131
|
+
const output = boundary(sourceScope, 'graphOutput', outputPort, type);
|
|
132
|
+
const input = boundary(scope, 'graphInput', inputPort, type);
|
|
133
|
+
if (output.created)
|
|
134
|
+
addEdge(sourceScope, { src, dst: { node: output.name, port: 'value' } });
|
|
135
|
+
if (input.created)
|
|
136
|
+
rootEdges.push({ src: { node: sourceScope, port: outputPort }, dst: { node: scope, port: inputPort } });
|
|
137
|
+
src = { node: input.name, port: 'value' };
|
|
138
|
+
}
|
|
139
|
+
const previous = entry.inputs.get(port);
|
|
140
|
+
entry.inputs.set(port, { name: port, type, ...(previous && { multi: true }) });
|
|
141
|
+
addEdge(scope, { src, dst: { node: memberOf(to), port } });
|
|
142
|
+
}
|
|
143
|
+
for (const [path, extension] of [...plan.extensions.nodes].sort(([a], [b]) => (0, graph_js_1.comparePaths)(a, b))) {
|
|
144
|
+
const entry = node(path, 'extension');
|
|
145
|
+
const { definition, source, props, drivers } = extension;
|
|
146
|
+
const expressions = [];
|
|
147
|
+
for (const [input, driver] of [...drivers].sort(([a], [b]) => (0, graph_js_1.comparePaths)(a, b))) {
|
|
148
|
+
if (driver.kind === 'wire')
|
|
149
|
+
link(path, ['connect', input], driver.from.address, driver.from.type);
|
|
150
|
+
else {
|
|
151
|
+
expressions.push([input, expression(entry, driver.expression), driver.source]);
|
|
152
|
+
for (const from of driver.references)
|
|
153
|
+
link(path, ['expression', input], from.address, from.type);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
property(entry, dataProp, { type: definition.type, source, props: [...props], expressions });
|
|
157
|
+
for (const [name, { type }] of definition.outputs)
|
|
158
|
+
output(entry, name, type);
|
|
159
|
+
}
|
|
160
|
+
for (const [to, inputs] of [...plan.inputs].sort(([a], [b]) => (0, graph_js_1.comparePaths)(a, b))) {
|
|
161
|
+
for (const from of [...inputs].sort(graph_js_1.comparePaths))
|
|
162
|
+
link(ownerOf(to), ['depend', memberOf(to)], from, 'number');
|
|
163
|
+
}
|
|
164
|
+
for (const { to, input, from } of [...plan.contextWires].sort((a, b) => (0, graph_js_1.comparePaths)(a.to, b.to) || (0, graph_js_1.comparePaths)(a.input, b.input) || (0, graph_js_1.comparePaths)(a.from.address, b.from.address))) {
|
|
165
|
+
link(ownerOf(to), ['wire', memberOf(to), input], from.address, from.type);
|
|
166
|
+
}
|
|
167
|
+
const types = new Map();
|
|
168
|
+
const definitions = [];
|
|
169
|
+
const scopes = new Map();
|
|
170
|
+
for (const name of ['obj', 'ch', ...sceneData.contexts])
|
|
171
|
+
scopes.set(name, []);
|
|
172
|
+
let metadata;
|
|
173
|
+
function type(entry) {
|
|
174
|
+
const { category } = entry;
|
|
175
|
+
const inputs = [...entry.inputs.values()].sort((a, b) => (0, graph_js_1.comparePaths)(a.name, b.name));
|
|
176
|
+
const outputs = [...entry.outputs.values()].sort((a, b) => (0, graph_js_1.comparePaths)(a.name, b.name));
|
|
177
|
+
const props = [...entry.props.keys()].sort(graph_js_1.comparePaths).map(name => ({ name, type: 'object' }));
|
|
178
|
+
const signature = JSON.stringify([category, inputs, outputs, props]);
|
|
179
|
+
let found = types.get(signature);
|
|
180
|
+
if (!found) {
|
|
181
|
+
const name = category + '-' + definitions.length;
|
|
182
|
+
found = 'scene:' + name;
|
|
183
|
+
types.set(signature, found);
|
|
184
|
+
definitions.push({ context: 'vim3d', category: 'scene', name, inputs, outputs, props });
|
|
185
|
+
}
|
|
186
|
+
return found;
|
|
187
|
+
}
|
|
188
|
+
for (const [path, entry] of [...pending].sort(([a], [b]) => (0, graph_js_1.comparePaths)(a, b))) {
|
|
189
|
+
if (entry.links.length)
|
|
190
|
+
property(entry, linksProp, entry.links);
|
|
191
|
+
const stored = { name: memberOf(path), type: type(entry), props: [...entry.props.values()] };
|
|
192
|
+
if (path === settings.path)
|
|
193
|
+
metadata = stored;
|
|
194
|
+
else {
|
|
195
|
+
const scope = scopeOf(path);
|
|
196
|
+
if (!scopes.has(scope))
|
|
197
|
+
scopes.set(scope, []);
|
|
198
|
+
scopes.get(scope).push(stored);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const nodes = [...scopes].sort(([a], [b]) => (0, graph_js_1.comparePaths)(a, b)).map(([name, nodes]) => ({ name, type: 'subnet', nodes: [...nodes, ...(boundaries.get(name)?.values() ?? [])], edges: edges.get(name) ?? [] }));
|
|
202
|
+
return JSON.parse(serializeStorageGraph({ context: 'vim3d', nodes: [metadata, ...nodes], edges: rootEdges, definitions }));
|
|
203
|
+
}
|
|
204
|
+
function invalid(message) {
|
|
205
|
+
(0, types_js_1.fail)('invalid-storage-graph', message);
|
|
206
|
+
}
|
|
207
|
+
function propsOf(node) {
|
|
208
|
+
const props = new Map();
|
|
209
|
+
for (const prop of node.props ?? []) {
|
|
210
|
+
if (!prop.name || props.has(prop.name))
|
|
211
|
+
invalid('Invalid or duplicate property on ' + node.name);
|
|
212
|
+
props.set(prop.name, prop);
|
|
213
|
+
}
|
|
214
|
+
return props;
|
|
215
|
+
}
|
|
216
|
+
function fromStorageGraph(graph, options = {}) {
|
|
217
|
+
try {
|
|
218
|
+
return { value: load(JSON.parse(serializeStorageGraph(graph)), options) };
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
if (error instanceof types_js_1.EvaluationError)
|
|
222
|
+
return { diagnostics: [error.diagnostic] };
|
|
223
|
+
return { diagnostics: [{ code: 'invalid-storage-graph', message: 'Invalid Vim3D scene data' }] };
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function load(graph, options) {
|
|
227
|
+
if (graph.context !== 'vim3d')
|
|
228
|
+
invalid('Expected a Vim3D graph');
|
|
229
|
+
const settings = graph.nodes.find(node => node.name === '@scene');
|
|
230
|
+
if (!settings)
|
|
231
|
+
invalid('Missing @scene settings');
|
|
232
|
+
const metadata = propsOf(settings).get(dataProp)?.value;
|
|
233
|
+
const { contexts, events, fps, frameCount, ranges } = metadata;
|
|
234
|
+
if (!(fps > 0) || !Number.isFinite(fps) || (frameCount !== undefined && (!Number.isSafeInteger(frameCount) || frameCount < 0))) {
|
|
235
|
+
invalid('Invalid scene timing');
|
|
236
|
+
}
|
|
237
|
+
const registry = (0, registry_js_1.createContextRegistry)(options.contexts?.filter(({ name }) => contexts.includes(name)));
|
|
238
|
+
for (const name of contexts) {
|
|
239
|
+
if (!registry.get(name))
|
|
240
|
+
invalid('Missing host context /' + name);
|
|
241
|
+
}
|
|
242
|
+
const extensions = new contexts_js_1.Extensions(registry);
|
|
243
|
+
const objects = new Map();
|
|
244
|
+
const channels = new Map();
|
|
245
|
+
const drivers = new Map();
|
|
246
|
+
const inputs = new Map();
|
|
247
|
+
const contextWires = [];
|
|
248
|
+
const declarations = new Map(graph.definitions.map(def => [def.category + ':' + def.name, def]));
|
|
249
|
+
const nodes = new Map();
|
|
250
|
+
const links = [];
|
|
251
|
+
const boundaryNodes = new Map();
|
|
252
|
+
const inputPorts = new Map();
|
|
253
|
+
const outputPorts = new Map();
|
|
254
|
+
for (const scope of graph.nodes) {
|
|
255
|
+
if (scope === settings)
|
|
256
|
+
continue;
|
|
257
|
+
if (!(0, channels_js_1.validSegment)(scope.name) || scope.type !== 'subnet')
|
|
258
|
+
invalid('Expected a context subnet');
|
|
259
|
+
const prefix = '/' + scope.name + '/';
|
|
260
|
+
for (const node of scope.nodes ?? []) {
|
|
261
|
+
const path = prefix + node.name;
|
|
262
|
+
if (!(0, channels_js_1.validSegment)(node.name) || nodes.has(path) || boundaryNodes.has(path)) {
|
|
263
|
+
invalid('Invalid node in /' + scope.name + ': ' + node.name);
|
|
264
|
+
}
|
|
265
|
+
const props = propsOf(node);
|
|
266
|
+
if (node.type === 'graphInput' || node.type === 'graphOutput') {
|
|
267
|
+
const name = props.get('portName')?.value;
|
|
268
|
+
const type = props.get('dataType')?.value;
|
|
269
|
+
const ports = node.type === 'graphInput' ? inputPorts : outputPorts;
|
|
270
|
+
if (typeof name !== 'string' || !name || typeof type !== 'string' || ports.has(prefix + name)) {
|
|
271
|
+
invalid('Invalid boundary ' + path);
|
|
272
|
+
}
|
|
273
|
+
const boundary = { kind: node.type, name, type };
|
|
274
|
+
boundaryNodes.set(path, boundary);
|
|
275
|
+
ports.set(prefix + name, boundary);
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
const def = declarations.get(node.type);
|
|
279
|
+
if (!def || def.context !== 'vim3d')
|
|
280
|
+
invalid('Missing node definition for ' + path);
|
|
281
|
+
nodes.set(path, { props, def });
|
|
282
|
+
}
|
|
283
|
+
for (const { src, dst } of scope.edges ?? []) {
|
|
284
|
+
if (!(0, channels_js_1.validSegment)(src.node) || !(0, channels_js_1.validSegment)(dst.node))
|
|
285
|
+
invalid('Expected scope-local edge endpoints');
|
|
286
|
+
const to = prefix + dst.node;
|
|
287
|
+
const from = prefix + src.node;
|
|
288
|
+
const output = boundaryNodes.get(to);
|
|
289
|
+
const input = boundaryNodes.get(from);
|
|
290
|
+
const sourceType = input ? input.type : nodes.get(from)?.def.outputs?.find(port => port.name === src.port)?.type;
|
|
291
|
+
const destType = output ? output.type : nodes.get(to)?.def.inputs?.find(port => port.name === dst.port)?.type;
|
|
292
|
+
if (!sourceType || sourceType !== destType)
|
|
293
|
+
invalid('Mismatched edge ports in /' + scope.name);
|
|
294
|
+
if (output) {
|
|
295
|
+
if (output.kind !== 'graphOutput' || input || dst.port !== 'value' || output.source)
|
|
296
|
+
invalid('Invalid graphOutput ' + to);
|
|
297
|
+
output.source = from + '/' + src.port;
|
|
298
|
+
}
|
|
299
|
+
else if (input) {
|
|
300
|
+
if (input.kind !== 'graphInput' || src.port !== 'value')
|
|
301
|
+
invalid('Invalid graphInput ' + from);
|
|
302
|
+
links.push({ to, port: dst.port, from: inputMarker + input.name });
|
|
303
|
+
}
|
|
304
|
+
else
|
|
305
|
+
links.push({ to, port: dst.port, from: from + '/' + src.port });
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
for (const { src, dst } of graph.edges) {
|
|
309
|
+
if (!(0, channels_js_1.validSegment)(src.node) || !(0, channels_js_1.validSegment)(dst.node) || src.node === dst.node)
|
|
310
|
+
invalid('Expected an edge between context subnets');
|
|
311
|
+
const output = outputPorts.get('/' + src.node + '/' + src.port);
|
|
312
|
+
const input = inputPorts.get('/' + dst.node + '/' + dst.port);
|
|
313
|
+
if (!input || input.source || !output?.source || output.type !== input.type)
|
|
314
|
+
invalid('Invalid subnet connection');
|
|
315
|
+
input.source = output.source;
|
|
316
|
+
}
|
|
317
|
+
function prop(path, name) {
|
|
318
|
+
return nodes.get(path)?.props.get(name)?.value;
|
|
319
|
+
}
|
|
320
|
+
function resolve(owner, path) {
|
|
321
|
+
if (path.startsWith(inputMarker)) {
|
|
322
|
+
const port = path.slice(inputMarker.length);
|
|
323
|
+
const boundary = inputPorts.get('/' + scopeOf(owner) + '/' + port);
|
|
324
|
+
if (!boundary?.source)
|
|
325
|
+
invalid('Unconnected subnet input ' + port);
|
|
326
|
+
path = boundary.source;
|
|
327
|
+
}
|
|
328
|
+
else if (scopeOf(path) !== scopeOf(owner))
|
|
329
|
+
invalid('Cross-context read requires a subnet input');
|
|
330
|
+
const target = nodes.get(ownerOf(path));
|
|
331
|
+
if (!target?.props.has(memberOf(path)) || !target.def.outputs?.some(output => output.name === memberOf(path))) {
|
|
332
|
+
invalid('Missing output property ' + path);
|
|
333
|
+
}
|
|
334
|
+
return path;
|
|
335
|
+
}
|
|
336
|
+
const reads = new Map();
|
|
337
|
+
const scalarReads = new Set();
|
|
338
|
+
const offTimeRefs = [];
|
|
339
|
+
const rejectSource = (_code, message) => invalid(message);
|
|
340
|
+
function recordRead(target, from) {
|
|
341
|
+
if (!reads.has(target))
|
|
342
|
+
reads.set(target, new Set());
|
|
343
|
+
reads.get(target).add(from);
|
|
344
|
+
}
|
|
345
|
+
function readAddress(owner, member, from, name, source) {
|
|
346
|
+
const address = resolve(owner, from);
|
|
347
|
+
const scope = scopeOf(owner);
|
|
348
|
+
const target = scope === 'ch' || scope === 'obj' ? scope : 'extension';
|
|
349
|
+
const output = sourceAt(address);
|
|
350
|
+
const ramp = output.stage === 'ch' && !!prop(ownerOf(address), dataProp)?.ramp;
|
|
351
|
+
(0, contexts_js_1.validateExpressionSource)(target, output, name, ramp, rejectSource);
|
|
352
|
+
if (name === 'chf' || name === 'cht')
|
|
353
|
+
offTimeRefs.push({ path: address, source });
|
|
354
|
+
const kind = target === 'extension' ? 'expression' : output.stage === 'audio' ? 'wire' : 'depend';
|
|
355
|
+
recordRead(JSON.stringify([owner, kind, member]), from);
|
|
356
|
+
if (name === 'ch')
|
|
357
|
+
scalarReads.add(JSON.stringify([owner, member, from]));
|
|
358
|
+
return address;
|
|
359
|
+
}
|
|
360
|
+
const ast = (owner, member, expression, source) => (0, expression_js_1.bindExpression)(expression, (address, name) => readAddress(owner, member, address, name, source));
|
|
361
|
+
function timedInput(owner, member, driver) {
|
|
362
|
+
const address = resolve(owner, driver.input);
|
|
363
|
+
const from = sourceAt(address);
|
|
364
|
+
if (from.stage !== (driver.kind === 'register' ? 'ch' : 'obj') || from.type !== 'number') {
|
|
365
|
+
invalid('Invalid ' + driver.kind + ' source: ' + address);
|
|
366
|
+
}
|
|
367
|
+
if (driver.kind === 'register' && (driver.freq !== 1 || driver.shift !== 0 || /^(rx|ry|rz)$/.test(member))) {
|
|
368
|
+
offTimeRefs.push({ path: address, source: driver.source });
|
|
369
|
+
}
|
|
370
|
+
recordRead(JSON.stringify([owner, 'depend', member]), driver.input);
|
|
371
|
+
return address;
|
|
372
|
+
}
|
|
373
|
+
function objectAt(path) {
|
|
374
|
+
const object = objects.get(path);
|
|
375
|
+
if (!object)
|
|
376
|
+
invalid('Missing object ' + path);
|
|
377
|
+
return object;
|
|
378
|
+
}
|
|
379
|
+
function sourceAt(path) {
|
|
380
|
+
const owner = ownerOf(path), member = memberOf(path), stage = scopeOf(path);
|
|
381
|
+
if (stage === 'ch')
|
|
382
|
+
return { address: path, path, port: 'value', type: 'number', stage };
|
|
383
|
+
if (stage === 'obj') {
|
|
384
|
+
if (member === 'worldMatrix' || member === 'localMatrix' || member === 'snapshot') {
|
|
385
|
+
return { address: path, path: owner + '/@world', port: member === 'worldMatrix' ? 'value' : member,
|
|
386
|
+
type: member === 'snapshot' ? 'object' : 'matrix4', stage };
|
|
387
|
+
}
|
|
388
|
+
return { address: path, path, port: 'value', type: 'number', stage };
|
|
389
|
+
}
|
|
390
|
+
return extensions.output(path, prop(owner, dataProp).source);
|
|
391
|
+
}
|
|
392
|
+
for (const path of nodes.keys()) {
|
|
393
|
+
if (scopeOf(path) === 'obj' || scopeOf(path) === 'ch')
|
|
394
|
+
continue;
|
|
395
|
+
const data = prop(path, dataProp);
|
|
396
|
+
const node = extensions.add(path, data.type, data.source);
|
|
397
|
+
node.props = new Map(data.props);
|
|
398
|
+
}
|
|
399
|
+
for (const [path, { def }] of nodes) {
|
|
400
|
+
const context = scopeOf(path);
|
|
401
|
+
const name = memberOf(path);
|
|
402
|
+
if (context === 'obj') {
|
|
403
|
+
const data = prop(path, dataProp);
|
|
404
|
+
const parameters = Object.create(null);
|
|
405
|
+
for (const { name: param, type } of def.outputs ?? []) {
|
|
406
|
+
if (type !== 'number')
|
|
407
|
+
continue;
|
|
408
|
+
const info = prop(path, param);
|
|
409
|
+
parameters[param] = info.value;
|
|
410
|
+
if (info.drivers.length)
|
|
411
|
+
drivers.set(path + '/' + param, info.drivers.map(driver => driver.kind === 'expr'
|
|
412
|
+
? loadSpan({ ...driver, expression: ast(path, param, driver.expression, driver.source) })
|
|
413
|
+
: loadSpan({ ...driver, input: timedInput(path, param, driver) })));
|
|
414
|
+
}
|
|
415
|
+
objects.set(path, {
|
|
416
|
+
id: objects.size, name, kind: data.kind, source: data.source, authored: { ...data.authored, parameters },
|
|
417
|
+
geometry: data.geometry, geometrySource: data.geometrySource && resolve(path, data.geometrySource),
|
|
418
|
+
pathBindings: [], aimBindings: [],
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
else if (context === 'ch') {
|
|
422
|
+
const data = prop(path, dataProp);
|
|
423
|
+
const { operator, ...config } = data;
|
|
424
|
+
const definition = operator && ch_operators_js_1.channelOperators.find(definition => definition.type === operator.type);
|
|
425
|
+
if (operator && !definition)
|
|
426
|
+
invalid('Unknown channel operator ' + operator.type);
|
|
427
|
+
const ports = new Map();
|
|
428
|
+
for (const { name: port } of def.outputs ?? []) {
|
|
429
|
+
const descriptor = prop(path, port);
|
|
430
|
+
ports.set(port, { ...descriptor, ...(descriptor.expression && {
|
|
431
|
+
expression: ast(path, port, descriptor.expression, descriptor.source),
|
|
432
|
+
}) });
|
|
433
|
+
}
|
|
434
|
+
channels.set(path, { ...config, name, id: channels.size, ports,
|
|
435
|
+
...(operator && { operator: { definition, props: Object.fromEntries(operator.props) } }),
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
for (const { port, from } of prop(path, linksProp) ?? [])
|
|
439
|
+
links.push({ to: path, port, from });
|
|
440
|
+
}
|
|
441
|
+
for (const [path, object] of objects) {
|
|
442
|
+
const { parent, paths, aims, geometrySource } = prop(path, dataProp);
|
|
443
|
+
if (parent)
|
|
444
|
+
object.parent = objectAt(parent);
|
|
445
|
+
object.pathBindings = paths.map(({ curve, position, freq, ...span }) => loadSpan({
|
|
446
|
+
...span, curve: objectAt(curve),
|
|
447
|
+
...(position ? { position: ast(path, memberOf((0, model_js_1.positionStep)(object)), position, span.source) } : { freq }),
|
|
448
|
+
}));
|
|
449
|
+
object.aimBindings = aims.map(span => loadSpan({ ...span, target: objectAt(span.target) }));
|
|
450
|
+
if (geometrySource) {
|
|
451
|
+
if (object.kind !== 'geo' || object.geometry)
|
|
452
|
+
invalid('Invalid geometry destination: ' + path);
|
|
453
|
+
(0, contexts_js_1.validateGeometrySource)(path + '/geometry', sourceAt(object.geometrySource), rejectSource);
|
|
454
|
+
recordRead(JSON.stringify([path, 'wire', memberOf((0, model_js_1.worldStep)(object))]), geometrySource);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
for (const [path] of extensions.nodes) {
|
|
458
|
+
for (const [input, expression, source] of prop(path, dataProp).expressions) {
|
|
459
|
+
const references = new Map();
|
|
460
|
+
const decoded = (0, expression_js_1.bindExpression)(expression, (value, name) => {
|
|
461
|
+
const address = readAddress(path, input, value, name, source);
|
|
462
|
+
references.set(address, sourceAt(address));
|
|
463
|
+
return address;
|
|
464
|
+
});
|
|
465
|
+
extensions.input(path + '/' + input, 'number', { kind: 'expr', expression: decoded, references: [...references.values()], source }, source);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
const linkedReads = new Map();
|
|
469
|
+
function matchRead(key, target, from) {
|
|
470
|
+
if (!reads.get(key)?.has(from))
|
|
471
|
+
invalid('Stored links disagree with reads for ' + target);
|
|
472
|
+
if (!linkedReads.has(key))
|
|
473
|
+
linkedReads.set(key, new Set());
|
|
474
|
+
linkedReads.get(key).add(from);
|
|
475
|
+
}
|
|
476
|
+
for (const { to, port, from } of links) {
|
|
477
|
+
if (!nodes.has(to))
|
|
478
|
+
invalid('Missing edge destination ' + to);
|
|
479
|
+
const [kind, member, input] = JSON.parse(port);
|
|
480
|
+
const target = to + '/' + member;
|
|
481
|
+
const address = resolve(to, from);
|
|
482
|
+
const readKey = JSON.stringify([to, kind, member]);
|
|
483
|
+
if (kind === 'depend' || kind === 'expression')
|
|
484
|
+
matchRead(readKey, target, from);
|
|
485
|
+
if (kind === 'depend') {
|
|
486
|
+
if (!inputs.has(target))
|
|
487
|
+
inputs.set(target, new Set());
|
|
488
|
+
inputs.get(target).add(address);
|
|
489
|
+
}
|
|
490
|
+
else if (kind === 'wire') {
|
|
491
|
+
const output = sourceAt(address);
|
|
492
|
+
const object = objects.get(to);
|
|
493
|
+
const channel = channels.get(to);
|
|
494
|
+
if (object && target === (0, model_js_1.worldStep)(object)) {
|
|
495
|
+
if (input !== 'geometry')
|
|
496
|
+
invalid('Invalid geometry wire for ' + target);
|
|
497
|
+
(0, contexts_js_1.validateGeometrySource)(to + '/geometry', output, rejectSource);
|
|
498
|
+
}
|
|
499
|
+
else if (object && target === (0, model_js_1.positionStep)(object)) {
|
|
500
|
+
if (output.stage !== 'audio')
|
|
501
|
+
invalid('Invalid path expression wire for ' + target);
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
if (!(object && Object.hasOwn(object.authored.parameters, member)) && !channel?.ports.has(member)) {
|
|
505
|
+
invalid('Invalid scalar wire for ' + target);
|
|
506
|
+
}
|
|
507
|
+
(0, contexts_js_1.validateScalarConnectionSource)(target, !!channel?.operator, output, rejectSource);
|
|
508
|
+
if (!scalarReads.has(JSON.stringify([to, member, from])))
|
|
509
|
+
invalid('Stored links disagree with reads for ' + target);
|
|
510
|
+
}
|
|
511
|
+
if (output.stage !== 'ch')
|
|
512
|
+
matchRead(readKey, target, from);
|
|
513
|
+
contextWires.push({ to: target, input, from: output });
|
|
514
|
+
}
|
|
515
|
+
else if (kind === 'connect') {
|
|
516
|
+
const source = prop(to, dataProp).source;
|
|
517
|
+
const from = sourceAt(address);
|
|
518
|
+
extensions.input(target, from.type, { kind: 'wire', from }, source);
|
|
519
|
+
}
|
|
520
|
+
else if (kind !== 'expression')
|
|
521
|
+
invalid('Unknown link on ' + to);
|
|
522
|
+
}
|
|
523
|
+
for (const [key, addresses] of reads) {
|
|
524
|
+
if (linkedReads.get(key)?.size !== addresses.size) {
|
|
525
|
+
const [owner, , member] = JSON.parse(key);
|
|
526
|
+
invalid('Stored links disagree with reads for ' + owner + '/' + member);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const loadedEvents = events.map(({ object: path, ...event }) => {
|
|
530
|
+
const object = objectAt(path);
|
|
531
|
+
const edit = (0, model_js_1.propertyEdit)(event.command, event.args, object, event.source);
|
|
532
|
+
if (!edit)
|
|
533
|
+
invalid('Invalid event command ' + event.command);
|
|
534
|
+
return { ...event, object, edit };
|
|
535
|
+
});
|
|
536
|
+
return (0, compile_js_1.finishScene)({ fps, frameCount, ranges }, {
|
|
537
|
+
objects: [...objects.values()], channels: [...channels.values()], drivers, events: loadedEvents, extensions, inputs, contextWires,
|
|
538
|
+
}, offTimeRefs);
|
|
539
|
+
}
|
|
540
|
+
function json(value) {
|
|
541
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
542
|
+
return JSON.stringify(value);
|
|
543
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
544
|
+
return Object.is(value, -0) ? '-0' : JSON.stringify(value);
|
|
545
|
+
if (Array.isArray(value))
|
|
546
|
+
return '[' + value.map(json).join(',') + ']';
|
|
547
|
+
if (value && typeof value === 'object') {
|
|
548
|
+
return '{' + Object.entries(value).filter(([, value]) => value !== undefined)
|
|
549
|
+
.sort(([a], [b]) => (0, graph_js_1.comparePaths)(a, b)).map(([key, value]) => JSON.stringify(key) + ':' + json(value)).join(',') + '}';
|
|
550
|
+
}
|
|
551
|
+
return invalid('Expected finite JSON scene data');
|
|
552
|
+
}
|
|
553
|
+
function sorted(values, key) {
|
|
554
|
+
return values && [...values].sort((a, b) => (0, graph_js_1.comparePaths)(key(a), key(b)));
|
|
555
|
+
}
|
|
556
|
+
function sortedNodes(nodes) {
|
|
557
|
+
return sorted(nodes, node => node.name)?.map(node => ({
|
|
558
|
+
...node,
|
|
559
|
+
props: sorted(node.props, prop => prop.name),
|
|
560
|
+
nodes: sortedNodes(node.nodes),
|
|
561
|
+
edges: sorted(node.edges, json),
|
|
562
|
+
}));
|
|
563
|
+
}
|
|
564
|
+
function serializeStorageGraph(graph) {
|
|
565
|
+
return json({
|
|
566
|
+
...graph,
|
|
567
|
+
nodes: sortedNodes(graph.nodes),
|
|
568
|
+
edges: sorted(graph.edges, json),
|
|
569
|
+
definitions: sorted(graph.definitions, def => json([def.context, def.category, def.name]))?.map(def => ({
|
|
570
|
+
...def,
|
|
571
|
+
props: sorted(def.props, prop => prop.name),
|
|
572
|
+
inputs: sorted(def.inputs, port => port.name),
|
|
573
|
+
outputs: sorted(def.outputs, port => port.name),
|
|
574
|
+
})),
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
async function hashStorageGraph(graph) {
|
|
578
|
+
const bytes = new TextEncoder().encode(serializeStorageGraph(graph));
|
|
579
|
+
const digest = await globalThis.crypto.subtle.digest('SHA-256', bytes);
|
|
580
|
+
return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('');
|
|
581
|
+
}
|