@margoengine/save 0.0.0-stage → 1.0.0
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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +655 -2
- package/dist/codec.d.ts +25 -0
- package/dist/delta.d.ts +51 -0
- package/dist/delta.js +114 -0
- package/dist/document.d.ts +90 -0
- package/dist/engine.d.ts +113 -0
- package/dist/engine.js +467 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +46 -0
- package/dist/load-BaYFdOQQ.js +172 -0
- package/dist/load.d.ts +74 -0
- package/dist/materials.d.ts +12 -0
- package/dist/materials.js +16 -0
- package/dist/migrations.d.ts +31 -0
- package/dist/node.d.ts +18 -0
- package/dist/node.js +108 -0
- package/dist/opfs.d.ts +17 -0
- package/dist/opfs.js +105 -0
- package/dist/prefab-DeWmT5ey.js +277 -0
- package/dist/prefab.d.ts +23 -0
- package/dist/queue-C2AsmZge.js +12 -0
- package/dist/queue.d.ts +2 -0
- package/dist/remote.d.ts +59 -0
- package/dist/remote.js +235 -0
- package/dist/safe-DVo3kH0O.js +128 -0
- package/dist/safe.d.ts +46 -0
- package/dist/safe.js +1 -0
- package/dist/scene-state.d.ts +29 -0
- package/dist/scene-state.js +86 -0
- package/dist/signed.d.ts +17 -0
- package/dist/signed.js +66 -0
- package/dist/slots.d.ts +114 -0
- package/dist/slots.js +248 -0
- package/dist/storage-DZeyzOh1.js +153 -0
- package/dist/storage.d.ts +59 -0
- package/package.json +119 -4
package/dist/engine.js
ADDED
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { s as e, t } from "./prefab-DeWmT5ey.js";
|
|
5
|
+
import { r as n } from "./load-BaYFdOQQ.js";
|
|
6
|
+
import { i as r, t as i } from "./storage-DZeyzOh1.js";
|
|
7
|
+
import { MargoError as a } from "@margoengine/math/errors";
|
|
8
|
+
import { Geometry as o } from "@margoengine/core";
|
|
9
|
+
//#region ../shared/scene-scope.js
|
|
10
|
+
var s = 0, c = (e, t) => {
|
|
11
|
+
let n = t + s++;
|
|
12
|
+
for (; e.plugins?.has(n);) n += ":";
|
|
13
|
+
return n;
|
|
14
|
+
};
|
|
15
|
+
function l(e, t) {
|
|
16
|
+
return {
|
|
17
|
+
scene: t,
|
|
18
|
+
run(n) {
|
|
19
|
+
if (!t) return n();
|
|
20
|
+
let r, i = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
|
|
21
|
+
name: c(e, "scene-scope:"),
|
|
22
|
+
setup() {
|
|
23
|
+
r = n();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
return t.use(i), t.removePlugin(i.name), r;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/engine.ts
|
|
32
|
+
var u = [
|
|
33
|
+
"mesh",
|
|
34
|
+
"model",
|
|
35
|
+
"geometry",
|
|
36
|
+
"material",
|
|
37
|
+
"sharedMaterial",
|
|
38
|
+
"node",
|
|
39
|
+
"visible",
|
|
40
|
+
"receiveShadow"
|
|
41
|
+
], d = 24e-8, f = /* @__PURE__ */ new WeakMap();
|
|
42
|
+
function p(e) {
|
|
43
|
+
return f.get(e);
|
|
44
|
+
}
|
|
45
|
+
var m = (e) => e, h = (e) => {
|
|
46
|
+
let { engine: t, id: n } = m(e), r = t.store.parents[n];
|
|
47
|
+
return r < 0 ? void 0 : t.meshes.get(r);
|
|
48
|
+
}, g = (e) => {
|
|
49
|
+
let t = e;
|
|
50
|
+
return Array.isArray(t.nodes) && typeof t.root == "object" && typeof t.asset == "object";
|
|
51
|
+
}, _ = (e) => [...e.plugins.values()].filter(g), v = (e) => typeof e == "object" && !!e && !Array.isArray(e), y = (e, t) => Array.isArray(e) && (t === void 0 || e.length === t) && e.every(Number.isFinite), b = (e) => new Uint8Array(e.buffer, e.byteOffset, e.byteLength);
|
|
52
|
+
function x(e) {
|
|
53
|
+
let t = m(e), { qx: n, qy: r, qz: i, qw: a } = t.engine.store.transforms, o = t.id;
|
|
54
|
+
return t.syncRotation(), [
|
|
55
|
+
n[o],
|
|
56
|
+
r[o],
|
|
57
|
+
i[o],
|
|
58
|
+
a[o]
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
function S(e) {
|
|
62
|
+
let t = {}, { position: n, scale: r } = e, i = x(e);
|
|
63
|
+
return (n.x || n.y || n.z) && (t.position = [
|
|
64
|
+
n.x,
|
|
65
|
+
n.y,
|
|
66
|
+
n.z
|
|
67
|
+
]), (i[0] || i[1] || i[2] || i[3] !== 1) && (t.rotation = i), (r.x !== 1 || r.y !== 1 || r.z !== 1) && (t.scale = [
|
|
68
|
+
r.x,
|
|
69
|
+
r.y,
|
|
70
|
+
r.z
|
|
71
|
+
]), Object.keys(t).length ? t : void 0;
|
|
72
|
+
}
|
|
73
|
+
function C(e, t) {
|
|
74
|
+
t?.position && e.setPosition(t.position), t?.rotation && e.setQuaternion(t.rotation), t?.scale && e.setScale(t.scale);
|
|
75
|
+
}
|
|
76
|
+
function w(e, t = 0) {
|
|
77
|
+
if (e === null || typeof e == "string" || typeof e == "boolean") return e;
|
|
78
|
+
if (typeof e == "number") return Number.isFinite(e) ? e : void 0;
|
|
79
|
+
if (t > 16 || typeof e != "object") return;
|
|
80
|
+
if (ArrayBuffer.isView(e)) return e instanceof DataView || e instanceof BigInt64Array || e instanceof BigUint64Array ? void 0 : Array.from(e);
|
|
81
|
+
if (Array.isArray(e)) {
|
|
82
|
+
let n = e.map((e) => w(e, t + 1));
|
|
83
|
+
return n.includes(void 0) ? void 0 : n;
|
|
84
|
+
}
|
|
85
|
+
if (Object.getPrototypeOf(e) !== Object.prototype) return;
|
|
86
|
+
let n = {};
|
|
87
|
+
for (let [r, i] of Object.entries(e)) {
|
|
88
|
+
let e = w(i, t + 1);
|
|
89
|
+
e !== void 0 && (n[r] = e);
|
|
90
|
+
}
|
|
91
|
+
return n;
|
|
92
|
+
}
|
|
93
|
+
function T(e) {
|
|
94
|
+
let t = /* @__PURE__ */ new Set();
|
|
95
|
+
for (let n = Object.getPrototypeOf(e); n && n !== Object.prototype; n = Object.getPrototypeOf(n)) for (let [e, r] of Object.entries(Object.getOwnPropertyDescriptors(n))) r.get && r.set && t.add(e);
|
|
96
|
+
return [...t].sort();
|
|
97
|
+
}
|
|
98
|
+
function E(e, t) {
|
|
99
|
+
let n = e;
|
|
100
|
+
if (t?.save) return t.save(e);
|
|
101
|
+
if (!t?.properties && typeof n.saveState == "function") return n.saveState();
|
|
102
|
+
let r = {};
|
|
103
|
+
for (let i of t?.properties ?? T(e)) {
|
|
104
|
+
let e = w(n[i]);
|
|
105
|
+
e !== void 0 && (r[i] = e);
|
|
106
|
+
}
|
|
107
|
+
let i = w(n.options), a = {};
|
|
108
|
+
return Object.keys(r).length && (a.properties = r), v(i) && Object.keys(i).length && (a.options = i), Object.keys(a).length ? a : void 0;
|
|
109
|
+
}
|
|
110
|
+
function D(e, t, n) {
|
|
111
|
+
let r = e;
|
|
112
|
+
if (t?.load) return t.load(e, n);
|
|
113
|
+
if (!t?.properties && !t?.save && typeof r.loadState == "function") return r.loadState(n);
|
|
114
|
+
if (v(n) && v(n.properties)) for (let [e, t] of Object.entries(n.properties)) {
|
|
115
|
+
let n = r[e];
|
|
116
|
+
ArrayBuffer.isView(n) ? n.set(t) : r[e] = t;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
var O = (e, t) => e && Object.hasOwn(e, t) ? e[t] : void 0;
|
|
120
|
+
function k(e) {
|
|
121
|
+
let t = {}, n = e.asset.doc?.nodes ?? [], r = (e, t, n) => {
|
|
122
|
+
let r = t ?? n;
|
|
123
|
+
return e.every((e, t) => Math.abs(e - r[t]) <= d * Math.max(1, Math.abs(r[t])));
|
|
124
|
+
};
|
|
125
|
+
return e.nodes.forEach((e, i) => {
|
|
126
|
+
let a = n[i] ?? {}, { position: o, scale: s } = e, c = a.extensions?.KHR_node_visibility, l = x(e), u = a.matrix === void 0 && r([
|
|
127
|
+
o.x,
|
|
128
|
+
o.y,
|
|
129
|
+
o.z
|
|
130
|
+
], a.translation, [
|
|
131
|
+
0,
|
|
132
|
+
0,
|
|
133
|
+
0
|
|
134
|
+
]) && r(l, a.rotation, [
|
|
135
|
+
0,
|
|
136
|
+
0,
|
|
137
|
+
0,
|
|
138
|
+
1
|
|
139
|
+
]) && r([
|
|
140
|
+
s.x,
|
|
141
|
+
s.y,
|
|
142
|
+
s.z
|
|
143
|
+
], a.scale, [
|
|
144
|
+
1,
|
|
145
|
+
1,
|
|
146
|
+
1
|
|
147
|
+
]);
|
|
148
|
+
if (u && e.visible === (c?.visible ?? !0)) return;
|
|
149
|
+
let d = { visible: e.visible };
|
|
150
|
+
u || (d.transform = {
|
|
151
|
+
position: [
|
|
152
|
+
o.x,
|
|
153
|
+
o.y,
|
|
154
|
+
o.z
|
|
155
|
+
],
|
|
156
|
+
rotation: l,
|
|
157
|
+
scale: [
|
|
158
|
+
s.x,
|
|
159
|
+
s.y,
|
|
160
|
+
s.z
|
|
161
|
+
]
|
|
162
|
+
}), t[i] = d;
|
|
163
|
+
}), t;
|
|
164
|
+
}
|
|
165
|
+
var A = (e) => e.split(/[?#]/)[0].split("/").pop()?.replace(/\.[^.]*$/, "") || "model";
|
|
166
|
+
function j(e, t = {}) {
|
|
167
|
+
let { scene: n = e.scene, version: i = 1, meta: a, camera: o = !0, plugins: s, filter: c, components: l, modelURL: d, onSkip: p } = t, v = /* @__PURE__ */ new Map();
|
|
168
|
+
for (let e of _(n)) {
|
|
169
|
+
for (let t of e.ownedMeshes ?? e.nodes) v.set(t, e);
|
|
170
|
+
v.set(e.root, e);
|
|
171
|
+
}
|
|
172
|
+
let y = /* @__PURE__ */ new Map(), x = (e) => {
|
|
173
|
+
let t = y.get(e);
|
|
174
|
+
if (t === void 0) {
|
|
175
|
+
let n = h(e);
|
|
176
|
+
t = (c?.(e) ?? !0) && (!n || x(n)), y.set(e, t);
|
|
177
|
+
}
|
|
178
|
+
return t;
|
|
179
|
+
}, C = [...n.meshes.values()].filter((e) => x(e) && (!v.has(e) || v.get(e).root === e)).sort((e, t) => m(e).id - m(t).id), w = /* @__PURE__ */ new Map();
|
|
180
|
+
for (let e of C) {
|
|
181
|
+
let t = v.get(e);
|
|
182
|
+
if (!t) continue;
|
|
183
|
+
let n = d?.(t) ?? t.asset.baseURL;
|
|
184
|
+
if (!n) throw TypeError(`Model '${t.name}' was not loaded from a URL; pass modelURL to say where it lives`);
|
|
185
|
+
w.set(e, n);
|
|
186
|
+
}
|
|
187
|
+
let T = new Set(C.map((e) => f.get(e)));
|
|
188
|
+
for (let e of C) {
|
|
189
|
+
if (f.has(e)) continue;
|
|
190
|
+
let t = e.name || (w.has(e) ? A(w.get(e)) : "mesh"), n = t;
|
|
191
|
+
for (let e = 1; T.has(n); e++) n = `${t}-${e}`;
|
|
192
|
+
T.add(n), f.set(e, n);
|
|
193
|
+
}
|
|
194
|
+
let D = /* @__PURE__ */ new Map();
|
|
195
|
+
for (let e of C) w.has(e) || D.set(e.material, (D.get(e.material) ?? 0) + 1);
|
|
196
|
+
let j = [...D].filter(([, e]) => e > 1).map(([e]) => e), M = /* @__PURE__ */ new Map(), N = [], P = C.map((e) => {
|
|
197
|
+
let t = { id: f.get(e) }, n = {}, i = h(e), a = v.get(e);
|
|
198
|
+
if (e.name && (t.name = e.name), i) {
|
|
199
|
+
let e = v.get(i);
|
|
200
|
+
t.parent = f.get(e ? e.root : i), e && e.root !== i && (n.node = e.nodes.indexOf(i));
|
|
201
|
+
}
|
|
202
|
+
let o = S(e);
|
|
203
|
+
if (o && (t.transform = o), e.visible || (n.visible = !1), a) {
|
|
204
|
+
n.mesh = w.get(e);
|
|
205
|
+
let t = k(a), r = {};
|
|
206
|
+
a.selectedVariant !== void 0 && (r.variant = a.selectedVariant), Object.keys(t).length && (r.nodes = t), Object.keys(r).length && (n.model = r);
|
|
207
|
+
} else {
|
|
208
|
+
if (e.receiveShadow || (n.receiveShadow = !1), e.geometry) {
|
|
209
|
+
let t = M.get(e.geometry);
|
|
210
|
+
if (t === void 0) {
|
|
211
|
+
let { vertices: n, indices: i } = e.geometry;
|
|
212
|
+
t = N.push({
|
|
213
|
+
vertices: r(b(n)),
|
|
214
|
+
indices: r(b(i))
|
|
215
|
+
}) - 1, M.set(e.geometry, t);
|
|
216
|
+
}
|
|
217
|
+
n.geometry = t;
|
|
218
|
+
}
|
|
219
|
+
let { data: t } = e.material;
|
|
220
|
+
n.material = {
|
|
221
|
+
color: [
|
|
222
|
+
t[0],
|
|
223
|
+
t[1],
|
|
224
|
+
t[2]
|
|
225
|
+
],
|
|
226
|
+
roughness: t[4],
|
|
227
|
+
metalness: t[5],
|
|
228
|
+
doubleSided: !!t[6]
|
|
229
|
+
};
|
|
230
|
+
let i = j.indexOf(e.material);
|
|
231
|
+
i >= 0 && (n.sharedMaterial = i);
|
|
232
|
+
}
|
|
233
|
+
let s = l?.(e);
|
|
234
|
+
for (let e of Object.keys(s ?? {})) if (u.includes(e)) throw TypeError(`Component '${e}' is the save binding's; name yours differently`);
|
|
235
|
+
return t.components = {
|
|
236
|
+
...n,
|
|
237
|
+
...s
|
|
238
|
+
}, t;
|
|
239
|
+
}), F = { lighting: {
|
|
240
|
+
direction: Array.from(n.lighting.direction),
|
|
241
|
+
radiance: Array.from(n.lighting.radiance),
|
|
242
|
+
ambient: Array.from(n.lighting.ambient)
|
|
243
|
+
} };
|
|
244
|
+
if (n.background !== void 0 && (F.background = n.background), o) {
|
|
245
|
+
let { fov: t, near: n, far: r, position: i, target: a, up: o } = e.camera;
|
|
246
|
+
F.camera = {
|
|
247
|
+
fov: t,
|
|
248
|
+
near: n,
|
|
249
|
+
far: r,
|
|
250
|
+
position: [...i],
|
|
251
|
+
target: [...a],
|
|
252
|
+
up: [...o]
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
N.length && (F.geometries = N);
|
|
256
|
+
let I = {};
|
|
257
|
+
for (let e of n.plugins.values()) {
|
|
258
|
+
let t = O(s, e.name);
|
|
259
|
+
if (g(e) || t === null) continue;
|
|
260
|
+
let n = E(e, t ?? void 0);
|
|
261
|
+
n === void 0 ? p?.(e) : I[e.name] = n;
|
|
262
|
+
}
|
|
263
|
+
return Object.keys(I).length && (F.plugins = I), {
|
|
264
|
+
format: "margo-save",
|
|
265
|
+
version: i,
|
|
266
|
+
entities: P,
|
|
267
|
+
meta: {
|
|
268
|
+
...a,
|
|
269
|
+
engine: F
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function M(e, t) {
|
|
274
|
+
if (!v(e) || typeof e.vertices != "string" || typeof e.indices != "string") throw TypeError(`meta.engine.geometries[${t}]: must be { vertices, indices } in base64`);
|
|
275
|
+
let n = (e) => {
|
|
276
|
+
let n = i(e);
|
|
277
|
+
if (n.length % 4) throw TypeError(`meta.engine.geometries[${t}]: the bytes do not fit; the save is damaged, load an earlier one`);
|
|
278
|
+
return n.buffer;
|
|
279
|
+
}, r = new Float32Array(n(e.vertices)), a = Math.floor(r.length / 6), s = new Float32Array(a * 3), c = new Float32Array(a * 3);
|
|
280
|
+
for (let e = 0; e < a; e++) s.set(r.subarray(e * 6, e * 6 + 3), e * 3), c.set(r.subarray(e * 6 + 3, e * 6 + 6), e * 3);
|
|
281
|
+
return new o(s, c, new Uint32Array(n(e.indices)));
|
|
282
|
+
}
|
|
283
|
+
function N(e, t) {
|
|
284
|
+
let n = e.meta?.engine ?? {}, r = (e) => /* @__PURE__ */ TypeError(`meta.engine.${e}`);
|
|
285
|
+
if (!v(n)) throw r("must be what captureScene wrote");
|
|
286
|
+
let { camera: i, lighting: a, geometries: o, plugins: s } = n;
|
|
287
|
+
if (i !== void 0) {
|
|
288
|
+
if (!(v(i) && y(i.position, 3) && y(i.target, 3) && y(i.up, 3) && [
|
|
289
|
+
i.fov,
|
|
290
|
+
i.near,
|
|
291
|
+
i.far
|
|
292
|
+
].every(Number.isFinite))) throw r("camera: { fov, near, far, position, target, up }");
|
|
293
|
+
let { fov: e, near: t, far: n, position: a, target: o, up: s } = i, c = [
|
|
294
|
+
a[0] - o[0],
|
|
295
|
+
a[1] - o[1],
|
|
296
|
+
a[2] - o[2]
|
|
297
|
+
];
|
|
298
|
+
if (!(e > 0 && e < 180 && t > 0 && n > t) || !(c[0] || c[1] || c[2]) || !(s[1] * c[2] - s[2] * c[1] || s[2] * c[0] - s[0] * c[2] || s[0] * c[1] - s[1] * c[0])) throw r("camera: 0 < fov < 180, 0 < near < far, position off target and up off the view");
|
|
299
|
+
}
|
|
300
|
+
if (a !== void 0) {
|
|
301
|
+
let { direction: e, radiance: n, ambient: i } = t.lighting;
|
|
302
|
+
if (!(v(a) && y(a.direction, e.length) && y(a.radiance, n.length) && y(a.ambient, i.length))) throw r("lighting: { direction, radiance, ambient } as the scene has them");
|
|
303
|
+
}
|
|
304
|
+
if (n.background !== void 0 && n.background !== null && typeof n.background != "string") throw r("background: a colour string or null");
|
|
305
|
+
if (o !== void 0 && !Array.isArray(o)) throw r("geometries: an array");
|
|
306
|
+
if (s !== void 0 && !v(s)) throw r("plugins: state by plugin name");
|
|
307
|
+
return n;
|
|
308
|
+
}
|
|
309
|
+
function P(e, t) {
|
|
310
|
+
if (v(t)) {
|
|
311
|
+
typeof t.variant == "number" && e.selectVariant?.(t.variant);
|
|
312
|
+
for (let [n, r] of Object.entries(t.nodes ?? {})) {
|
|
313
|
+
let t = e.nodes[+n];
|
|
314
|
+
if (!t) throw TypeError(`Model node ${n} is not in the file; has the model changed since the save?`);
|
|
315
|
+
C(t, r.transform), t.visible = r.visible !== !1;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function F(e, t, n, r) {
|
|
320
|
+
let i = n === void 0 ? void 0 : r.get(n);
|
|
321
|
+
i ? e.setMaterial(i) : (v(t) && e.material.set(t), n !== void 0 && r.set(n, e.material));
|
|
322
|
+
}
|
|
323
|
+
function I(e, t, n, r, i) {
|
|
324
|
+
let a = [], o = "save:restore";
|
|
325
|
+
for (; t.plugins.has(o) || n.some(([e]) => e === o);) o += ":";
|
|
326
|
+
let s = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
|
|
327
|
+
name: o,
|
|
328
|
+
setup: c
|
|
329
|
+
};
|
|
330
|
+
try {
|
|
331
|
+
t.use(s);
|
|
332
|
+
} finally {
|
|
333
|
+
t.plugins.get(o) === s && t.removePlugin(o);
|
|
334
|
+
}
|
|
335
|
+
function c() {
|
|
336
|
+
try {
|
|
337
|
+
for (let [o, s] of n) {
|
|
338
|
+
let n = O(i, o) ?? void 0, c = r.get(o);
|
|
339
|
+
if (c) {
|
|
340
|
+
let e = c, t = E(e, n);
|
|
341
|
+
t !== void 0 && a.push(() => D(e, n, t));
|
|
342
|
+
} else {
|
|
343
|
+
let r = new Set(t.plugins.values());
|
|
344
|
+
try {
|
|
345
|
+
c = n.type.attach(e, v(s) ? s.options : void 0);
|
|
346
|
+
} finally {
|
|
347
|
+
for (let e of t.plugins.values()) r.has(e) || a.push(() => {
|
|
348
|
+
t.plugins.get(e.name) === e && t.removePlugin(e.name);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
D(c, n, s);
|
|
353
|
+
}
|
|
354
|
+
} catch (e) {
|
|
355
|
+
let t = [];
|
|
356
|
+
for (let e of a.reverse()) try {
|
|
357
|
+
e();
|
|
358
|
+
} catch (e) {
|
|
359
|
+
t.push(e);
|
|
360
|
+
}
|
|
361
|
+
throw t.length ? Object.assign(e, { undoErrors: t }) : e;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
var L = async (e, t, n, r) => {
|
|
366
|
+
let { loadGLTF: i } = await import("@margoengine/gltf/pbr");
|
|
367
|
+
return l(e, r).run(() => i(e, t, n));
|
|
368
|
+
}, R = async (e) => {
|
|
369
|
+
let { cloneModel: t } = await import("@margoengine/animation/skeleton-utils");
|
|
370
|
+
return t(e);
|
|
371
|
+
};
|
|
372
|
+
async function z(r, i, o = {}) {
|
|
373
|
+
let { scene: s = r.scene, migrations: c, clear: l = !0, camera: u = !0, plugins: d, filter: p, onEntity: m, signal: g } = o, { load: v = (e, t) => L(r, e, t, s), clone: y = R, components: b, strictComponents: x } = o, S = {
|
|
374
|
+
components: b,
|
|
375
|
+
strictComponents: x,
|
|
376
|
+
signal: g
|
|
377
|
+
};
|
|
378
|
+
g?.throwIfAborted();
|
|
379
|
+
let w = e(i, {}), T = c ? c.migrate(w) : w, E = t(e(T, {
|
|
380
|
+
components: b,
|
|
381
|
+
strictComponents: x
|
|
382
|
+
})), D = N(T, s), k = (D.geometries ?? []).map(M);
|
|
383
|
+
for (let e of E) {
|
|
384
|
+
let { geometry: t, mesh: n } = e.components;
|
|
385
|
+
if (t !== void 0 && !(typeof t == "number" && k[t])) throw TypeError(`Entity '${e.id}': no saved geometry ${t}; the save is damaged`);
|
|
386
|
+
if (n !== void 0 && typeof n != "string") throw TypeError(`Entity '${e.id}' mesh: a model URL; shapes are for addScene of @margoengine/stage/scene`);
|
|
387
|
+
}
|
|
388
|
+
let A = new Map([...s.plugins.values()].map((e) => [e.name, e])), j = Object.entries(D.plugins ?? {}).filter(([e]) => O(d, e) !== null);
|
|
389
|
+
for (let [e] of j) if (!A.has(e) && !O(d, e)?.type) throw TypeError(`The save has plugin '${e}' and the scene does not; attach it first or pass its type in plugins`);
|
|
390
|
+
let z = l ? _(s) : [], B = new Set(z.flatMap((e) => [e.root, ...e.ownedMeshes ?? e.nodes])), V = [], H = (e) => {
|
|
391
|
+
if (!(p?.(e) ?? !0)) return !0;
|
|
392
|
+
let t = h(e);
|
|
393
|
+
return t ? H(t) : !1;
|
|
394
|
+
};
|
|
395
|
+
if (l) for (let e of s.meshes.values()) !B.has(e) && !H(e) && V.push(e);
|
|
396
|
+
let U = /* @__PURE__ */ new Map();
|
|
397
|
+
for (let e of E) {
|
|
398
|
+
let t = e.components.mesh;
|
|
399
|
+
t !== void 0 && U.set(t, [...U.get(t) ?? [], e]);
|
|
400
|
+
}
|
|
401
|
+
let W = /* @__PURE__ */ new Map(), G = /* @__PURE__ */ new Set(), K = () => {
|
|
402
|
+
for (let e of G) e.dispose();
|
|
403
|
+
G.clear();
|
|
404
|
+
}, q = (e) => (e.root.visible = !1, G.add(e), e);
|
|
405
|
+
try {
|
|
406
|
+
let e = await Promise.allSettled([...U.keys()].map((e) => v(e, { signal: g }).then(q))), t = e.find((e) => e.status === "rejected");
|
|
407
|
+
if (t) throw t.reason;
|
|
408
|
+
g?.throwIfAborted();
|
|
409
|
+
let n = 0;
|
|
410
|
+
for (let t of U.values()) {
|
|
411
|
+
let r = e[n++].value;
|
|
412
|
+
W.set(t[0].id, r);
|
|
413
|
+
for (let e of t.slice(1)) W.set(e.id, q(await y(r))), g?.throwIfAborted();
|
|
414
|
+
}
|
|
415
|
+
} catch (e) {
|
|
416
|
+
throw K(), g?.aborted ? g.reason : Object.assign(new a("failed", "A model of the save did not load; the scene is unchanged, see error.cause"), { cause: e });
|
|
417
|
+
}
|
|
418
|
+
let J = /* @__PURE__ */ new Map(), Y = new Map([...W.values()].map((e) => [e.root, e])), X = /* @__PURE__ */ new Map(), Z;
|
|
419
|
+
try {
|
|
420
|
+
Z = n(T, {
|
|
421
|
+
create(e) {
|
|
422
|
+
let t = e.components, n = W.get(e.id), r = n ? n.root : s.createMesh();
|
|
423
|
+
if (r.name = e.name ?? "", X.set(r, t.visible !== !1), n) C(r, e.transform), P(n, t.model);
|
|
424
|
+
else {
|
|
425
|
+
r.visible = !1;
|
|
426
|
+
try {
|
|
427
|
+
C(r, e.transform), t.receiveShadow === !1 && (r.receiveShadow = !1), t.geometry !== void 0 && r.setGeometry(k[t.geometry]), F(r, t.material, t.sharedMaterial, J);
|
|
428
|
+
} catch (e) {
|
|
429
|
+
throw r.dispose(), e;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return r;
|
|
433
|
+
},
|
|
434
|
+
link(e, t, n) {
|
|
435
|
+
let r = t.components.node, i = t.parent === void 0 ? null : n(t.parent);
|
|
436
|
+
if (r !== void 0) {
|
|
437
|
+
let e = i ? Y.get(i)?.nodes[r] : void 0;
|
|
438
|
+
if (!e) throw TypeError(`Entity '${t.id}' node: ${r} is not a node of '${t.parent}'; has the model changed since the save?`);
|
|
439
|
+
i = e;
|
|
440
|
+
}
|
|
441
|
+
e.setParent(i), m?.(e, t, n);
|
|
442
|
+
},
|
|
443
|
+
commit(e) {
|
|
444
|
+
I(r, s, j, A, d);
|
|
445
|
+
for (let [t, n] of e) f.set(n, t), n.visible = X.get(n);
|
|
446
|
+
G.clear();
|
|
447
|
+
let { lighting: t, background: n } = D;
|
|
448
|
+
t && (s.lighting.direction.set(t.direction), s.lighting.radiance.set(t.radiance), s.lighting.ambient.set(t.ambient)), n !== void 0 && (s.background = n), u && D.camera && (r.camera.cancelFit(), Object.assign(r.camera, D.camera));
|
|
449
|
+
for (let e of z) e.dispose();
|
|
450
|
+
for (let e of V.reverse()) e.dispose();
|
|
451
|
+
},
|
|
452
|
+
destroy(e) {
|
|
453
|
+
Y.has(e) || e.dispose();
|
|
454
|
+
}
|
|
455
|
+
}, S);
|
|
456
|
+
} catch (e) {
|
|
457
|
+
throw K(), e;
|
|
458
|
+
}
|
|
459
|
+
let { engine: Q, ...$ } = T.meta ?? {};
|
|
460
|
+
return {
|
|
461
|
+
meshes: Z,
|
|
462
|
+
models: W,
|
|
463
|
+
meta: $
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
//#endregion
|
|
467
|
+
export { j as captureScene, p as meshId, z as restoreScene };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { MAX_ENTITIES, MAX_JSON_DEPTH, MAX_PREFAB_DEPTH, findRefs, parseSave, validateSave, type ComponentCheck, type EntityRef, type Json, type PrefabDefinition, type Quat, type SaveDocument, type SavedEntity, type SavedTransform, type SaveIssue, type ValidateOptions, type Vec3, } from './document.js';
|
|
2
|
+
export { expandSave, mergePatch, type ResolvedEntity } from './prefab.js';
|
|
3
|
+
export { Migrations, type Migration } from './migrations.js';
|
|
4
|
+
export { decodeSave, encodeSave, type DecodeOptions, type EncodeOptions } from './codec.js';
|
|
5
|
+
export { indexedDBStorage, memoryStorage, webStorage, type IndexedDBStorage, type IndexedDBStorageOptions, type SaveStorage, type StorageCallOptions, type WebStorageLike, } from './storage.js';
|
|
6
|
+
export { ecsAdapter, loadGame, loadWorld, saveEcs, saveGame, type EcsComponentLike, type EcsWorldLike, type LoadOptions, type SaveGameOptions, type WorldAdapter, } from './load.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { a as e, c as t, i as n, n as r, o as i, r as a, s as o, t as s } from "./prefab-DeWmT5ey.js";
|
|
2
|
+
import { a as c, i as l, n as u, o as d, r as f, s as p, t as m } from "./load-BaYFdOQQ.js";
|
|
3
|
+
import { a as h, n as g, r as _ } from "./storage-DZeyzOh1.js";
|
|
4
|
+
import { MargoError as v } from "@margoengine/math/errors";
|
|
5
|
+
//#region src/migrations.ts
|
|
6
|
+
var y = class {
|
|
7
|
+
constructor(e = 1) {
|
|
8
|
+
if (this.current = e, this.steps = /* @__PURE__ */ new Map(), !Number.isInteger(e) || e < 1) throw RangeError("Latest schema version must be a whole number of 1 or more");
|
|
9
|
+
}
|
|
10
|
+
get latest() {
|
|
11
|
+
let e = this.current;
|
|
12
|
+
for (let t of this.steps.keys()) e = Math.max(e, t + 1);
|
|
13
|
+
return e;
|
|
14
|
+
}
|
|
15
|
+
register(e, t) {
|
|
16
|
+
if (!Number.isInteger(e) || e < 1) throw RangeError("Migration version must be a whole number of 1 or more");
|
|
17
|
+
if (typeof t != "function") throw TypeError("Migration must be a function (document) => document");
|
|
18
|
+
if (this.steps.has(e)) throw new v("state", `A migration from version ${e} is already registered; register each version once`);
|
|
19
|
+
return this.steps.set(e, t), this;
|
|
20
|
+
}
|
|
21
|
+
covers(e, t = this.latest) {
|
|
22
|
+
for (let n = e; n < t; n++) if (!this.steps.has(n)) return !1;
|
|
23
|
+
return !0;
|
|
24
|
+
}
|
|
25
|
+
migrate(e, t = this.latest) {
|
|
26
|
+
let n = e?.version;
|
|
27
|
+
if (!Number.isInteger(n) || n < 1) throw TypeError("Save version must be a whole number of 1 or more; is this a save document?");
|
|
28
|
+
if (n > t) throw RangeError(`Save is version ${n}, newer than ${t}; update the game to load it`);
|
|
29
|
+
let r = structuredClone(e);
|
|
30
|
+
for (let e = n; e < t; e++) {
|
|
31
|
+
let t = this.steps.get(e);
|
|
32
|
+
if (!t) throw new v("failed", `No migration from save version ${e} to ${e + 1}; register one with migrations.register(${e}, fn)`);
|
|
33
|
+
let n;
|
|
34
|
+
try {
|
|
35
|
+
n = t(r);
|
|
36
|
+
} catch (t) {
|
|
37
|
+
throw Object.assign(new v("failed", `Migration from save version ${e} to ${e + 1} threw; fix that step (the error is the cause)`), { cause: t });
|
|
38
|
+
}
|
|
39
|
+
if (typeof n != "object" || !n || n.version !== e + 1) throw new v("failed", `Migration from save version ${e} must return the document with version ${e + 1}; set document.version in the step`);
|
|
40
|
+
r = n;
|
|
41
|
+
}
|
|
42
|
+
return r;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
//#endregion
|
|
46
|
+
export { a as MAX_ENTITIES, n as MAX_JSON_DEPTH, e as MAX_PREFAB_DEPTH, y as Migrations, d as decodeSave, m as ecsAdapter, p as encodeSave, s as expandSave, i as findRefs, g as indexedDBStorage, u as loadGame, f as loadWorld, _ as memoryStorage, r as mergePatch, o as parseSave, l as saveEcs, c as saveGame, t as validateSave, h as webStorage };
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { s as e, t } from "./prefab-DeWmT5ey.js";
|
|
2
|
+
import { MargoError as n } from "@margoengine/math/errors";
|
|
3
|
+
//#region src/codec.ts
|
|
4
|
+
var r = async (e, t, n) => {
|
|
5
|
+
let r = new Response(t.readable).arrayBuffer(), i = t.writable.getWriter(), a = () => void i.abort(n.reason).then(void 0, () => void 0);
|
|
6
|
+
n?.addEventListener("abort", a, { once: !0 });
|
|
7
|
+
try {
|
|
8
|
+
let t = (async () => {
|
|
9
|
+
await i.write(e), await i.close();
|
|
10
|
+
})(), [, a] = await Promise.all([t, r]);
|
|
11
|
+
return n?.throwIfAborted(), new Uint8Array(a);
|
|
12
|
+
} finally {
|
|
13
|
+
n?.removeEventListener("abort", a);
|
|
14
|
+
}
|
|
15
|
+
}, i = () => {
|
|
16
|
+
if (typeof CompressionStream != "function") throw new n("unsupported", "CompressionStream is not available here; save with compress: false or use a current browser or Node 18+");
|
|
17
|
+
return CompressionStream;
|
|
18
|
+
};
|
|
19
|
+
async function a(e, t = {}) {
|
|
20
|
+
t.signal?.throwIfAborted();
|
|
21
|
+
let n = new TextEncoder().encode(JSON.stringify(e));
|
|
22
|
+
return t.compress ? r(n, new (i())("gzip"), t.signal) : n;
|
|
23
|
+
}
|
|
24
|
+
async function o(e, t, n) {
|
|
25
|
+
let r = new DecompressionStream("gzip"), i = r.writable.getWriter(), a = r.readable.getReader(), o = [], s = (e) => {
|
|
26
|
+
i.abort(e).then(void 0, () => void 0), a.cancel(e).then(void 0, () => void 0);
|
|
27
|
+
}, c = () => s(n.reason);
|
|
28
|
+
n?.addEventListener("abort", c, { once: !0 });
|
|
29
|
+
let l = i.write(e).then(() => i.close());
|
|
30
|
+
l.catch(() => void 0);
|
|
31
|
+
try {
|
|
32
|
+
let e = 0;
|
|
33
|
+
for (;;) {
|
|
34
|
+
let { done: n, value: r } = await a.read();
|
|
35
|
+
if (n) break;
|
|
36
|
+
if (e += r.byteLength, e > t) {
|
|
37
|
+
let e = /* @__PURE__ */ RangeError(`Save decompresses past ${t} bytes; it is not a save of this game, or pass a larger maxBytes to decodeSave`);
|
|
38
|
+
throw s(e), e;
|
|
39
|
+
}
|
|
40
|
+
o.push(r);
|
|
41
|
+
}
|
|
42
|
+
await l, n?.throwIfAborted();
|
|
43
|
+
} finally {
|
|
44
|
+
n?.removeEventListener("abort", c);
|
|
45
|
+
}
|
|
46
|
+
let u = new Uint8Array(o.reduce((e, t) => e + t.byteLength, 0)), d = 0;
|
|
47
|
+
for (let e of o) u.set(e, d), d += e.byteLength;
|
|
48
|
+
return u;
|
|
49
|
+
}
|
|
50
|
+
async function s(e, t = {}) {
|
|
51
|
+
if (t.signal?.throwIfAborted(), !(e instanceof Uint8Array)) throw TypeError("Save bytes must be a Uint8Array; read the file as bytes");
|
|
52
|
+
let r = t.maxBytes ?? 268435456;
|
|
53
|
+
if (!(Number.isSafeInteger(r) && r >= 0)) throw RangeError("decodeSave maxBytes must be a whole number of bytes, 0 or more");
|
|
54
|
+
let i = e;
|
|
55
|
+
if (e.length >= 2 && e[0] === 31 && e[1] === 139) {
|
|
56
|
+
if (typeof DecompressionStream != "function") throw new n("unsupported", "DecompressionStream is not available here; load gzipped saves in a current browser or Node 18+");
|
|
57
|
+
try {
|
|
58
|
+
i = await o(new Uint8Array(e), r, t.signal);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
throw t.signal?.throwIfAborted(), e instanceof RangeError ? e : Object.assign(/* @__PURE__ */ TypeError("Save is damaged: its gzip data does not decompress; restore the file from a backup"), { cause: e });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let a;
|
|
64
|
+
try {
|
|
65
|
+
a = new TextDecoder("utf-8", { fatal: !0 }).decode(i);
|
|
66
|
+
} catch (e) {
|
|
67
|
+
throw Object.assign(/* @__PURE__ */ TypeError("Save is not UTF-8 text; the file is damaged or not a save"), { cause: e });
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(a);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
throw Object.assign(/* @__PURE__ */ TypeError(`Save is not valid JSON (${e.message}); the file is damaged or not a save`), { cause: e });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/load.ts
|
|
77
|
+
function c(e, t, r = {}) {
|
|
78
|
+
if (t == null || typeof t.create != "function" || typeof t.destroy != "function") throw TypeError("loadWorld needs an adapter with create(entity) and destroy(handle)");
|
|
79
|
+
r.signal?.throwIfAborted();
|
|
80
|
+
let i = l(e, r), a = /* @__PURE__ */ new Map(), o = [], s = (e) => {
|
|
81
|
+
let t = a.get(e);
|
|
82
|
+
if (t === void 0) throw new n("state", `No entity '${e}' in this load; resolve only ids the save holds`);
|
|
83
|
+
return t;
|
|
84
|
+
}, c = "create", u = null;
|
|
85
|
+
try {
|
|
86
|
+
for (let e of i) {
|
|
87
|
+
u = e;
|
|
88
|
+
let n = t.create(e);
|
|
89
|
+
o.push(n), a.set(e.id, n);
|
|
90
|
+
}
|
|
91
|
+
if (c = "link", t.link) for (let e of i) u = e, t.link(a.get(e.id), e, s);
|
|
92
|
+
return u = null, r.signal?.throwIfAborted(), c = "commit", t.commit?.(a), a;
|
|
93
|
+
} catch (e) {
|
|
94
|
+
let i = [];
|
|
95
|
+
for (let e = o.length - 1; e >= 0; e--) try {
|
|
96
|
+
t.destroy(o[e]);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
i.push(e);
|
|
99
|
+
}
|
|
100
|
+
if (r.signal?.aborted && e === r.signal.reason) throw e;
|
|
101
|
+
let a = u ? ` at entity '${u.id}'` : "";
|
|
102
|
+
throw Object.assign(new n("failed", `Load failed in ${c}${a}; nothing was kept. Fix the cause (see error.cause) and load again`), {
|
|
103
|
+
cause: e,
|
|
104
|
+
...i.length ? { undoErrors: i } : {}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function l(n, r) {
|
|
109
|
+
let i = typeof n == "string" ? e(n, { components: {} }) : n;
|
|
110
|
+
if (r.migrations) {
|
|
111
|
+
let t = e(i, {});
|
|
112
|
+
i = r.migrations.migrate(t);
|
|
113
|
+
}
|
|
114
|
+
return t(e(i, r));
|
|
115
|
+
}
|
|
116
|
+
async function u(t, n, r, i = {}) {
|
|
117
|
+
let o = e(r, i.validate), s = i.compress ?? typeof CompressionStream == "function";
|
|
118
|
+
await t.write(n, await a(o, {
|
|
119
|
+
compress: s,
|
|
120
|
+
signal: i.signal
|
|
121
|
+
}), { signal: i.signal });
|
|
122
|
+
}
|
|
123
|
+
async function d(t, n, r = {}) {
|
|
124
|
+
let i = await t.read(n, { signal: r.signal });
|
|
125
|
+
if (i === null) return null;
|
|
126
|
+
let a = await s(i, { signal: r.signal });
|
|
127
|
+
return r.migrations && (a = r.migrations.migrate(e(a, {}))), e(a, r);
|
|
128
|
+
}
|
|
129
|
+
function f(e, t, n = 1) {
|
|
130
|
+
let r = [], i = /* @__PURE__ */ new Set();
|
|
131
|
+
for (let n of Object.values(t)) for (let t = 0; t < n.size; t++) {
|
|
132
|
+
let a = n.entities[t];
|
|
133
|
+
e.alive(a) && !i.has(a) && (i.add(a), r.push(a));
|
|
134
|
+
}
|
|
135
|
+
return r.sort((e, t) => e - t), {
|
|
136
|
+
format: "margo-save",
|
|
137
|
+
version: n,
|
|
138
|
+
entities: r.map((e) => {
|
|
139
|
+
let n = {};
|
|
140
|
+
for (let [r, i] of Object.entries(t)) {
|
|
141
|
+
let t = i.index(e);
|
|
142
|
+
t < 0 || (n[r] = Object.fromEntries(Object.entries(i.fields).map(([e, n]) => [e, n[t]])));
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
id: `e${e}`,
|
|
146
|
+
components: n
|
|
147
|
+
};
|
|
148
|
+
})
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function p(e, t) {
|
|
152
|
+
return {
|
|
153
|
+
create(n) {
|
|
154
|
+
for (let e of Object.keys(n.components)) if (!Object.hasOwn(t, e)) throw TypeError(`Unknown ECS component '${e}'; pass it in the components of ecsAdapter`);
|
|
155
|
+
let r = e.create();
|
|
156
|
+
try {
|
|
157
|
+
for (let [e, i] of Object.entries(n.components)) {
|
|
158
|
+
if (typeof i != "object" || !i || Array.isArray(i)) throw TypeError(`Component '${e}' of '${n.id}' must be an object of numeric fields`);
|
|
159
|
+
t[e].add(r, i);
|
|
160
|
+
}
|
|
161
|
+
} catch (t) {
|
|
162
|
+
throw e.destroy(r), t;
|
|
163
|
+
}
|
|
164
|
+
return r;
|
|
165
|
+
},
|
|
166
|
+
destroy(t) {
|
|
167
|
+
e.alive(t) && e.destroy(t);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
export { u as a, f as i, d as n, s as o, c as r, a as s, p as t };
|