castle-web-cli 0.4.230 → 0.4.232
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/dist/castle-host/host.js +81 -0
- package/dist/devSaveStorage.d.ts +36 -0
- package/dist/devSaveStorage.js +469 -0
- package/dist/devSessionChild.js +22 -12
- package/dist/devSessionServer.js +8 -2
- package/dist/devSessionStorage.d.ts +9 -0
- package/dist/devSessionStorage.js +66 -1
- package/dist/init.js +1 -1
- package/dist/serve.js +5 -1
- package/dist/serverBundle.js +5 -3
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +148 -1
- package/kits/base/sdk/castle.d.ts +4 -0
- package/kits/base/sdk/castle.js +2 -0
- package/kits/base/sdk/commands.d.ts +15 -0
- package/kits/base/sdk/multiplayer.js +52 -4
- package/kits/base/sdk/save.d.ts +197 -0
- package/kits/base/sdk/save.js +525 -0
- package/kits/base/sdk/saveCoordinator.d.ts +39 -0
- package/kits/base/sdk/saveCoordinator.js +252 -0
- package/kits/base/sdk/server/storage.d.ts +3 -2
- package/kits/base/sdk/server/storage.js +25 -5
- package/kits/base/sdk/server/wrapper.d.ts +4 -0
- package/kits/base/sdk/server/wrapper.js +2 -0
- package/kits/base/sdk/store.d.ts +3 -2
- package/kits/base/sdk/store.js +5 -0
- package/kits/hub-game/CLAUDE.md +29 -4
- package/kits/hub-game/code/client/avatar.js +39 -0
- package/kits/hub-game/code/client/chat.js +12 -0
- package/kits/hub-game/code/client/chrome.js +237 -13
- package/kits/hub-game/code/client/grab.js +166 -7
- package/kits/hub-game/code/client/hats.js +26 -0
- package/kits/hub-game/code/client/hookLayer.js +36 -3
- package/kits/hub-game/code/client/hudClear.js +126 -0
- package/kits/hub-game/code/client/state.js +35 -1
- package/kits/hub-game/code/client/talk.js +10 -36
- package/kits/hub-game/code/client/tiers.js +505 -0
- package/kits/hub-game/code/client/together.js +144 -4
- package/kits/hub-game/code/client/wear.js +96 -14
- package/kits/hub-game/code/server/actors.js +5 -0
- package/kits/hub-game/code/server/grab.js +299 -13
- package/kits/hub-game/code/server/hub.js +162 -7
- package/kits/hub-game/code/server/manifest.js +80 -8
- package/kits/hub-game/code/server/npcs.js +21 -33
- package/kits/hub-game/code/server/party.js +75 -6
- package/kits/hub-game/code/server/say.js +8 -2
- package/kits/hub-game/code/server/wear.js +116 -11
- package/kits/hub-game/code/systems/hub.js +386 -21
- package/kits/hub-game/docs/contract.md +285 -19
- package/kits/hub-game/package.json +1 -1
- package/kits/hub-game/scripts/test-chat.mjs +32 -0
- package/kits/hub-game/scripts/test-grab-client.mjs +511 -0
- package/kits/hub-game/scripts/test-grab.mjs +857 -3
- package/kits/hub-game/scripts/test-hats.mjs +229 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +721 -0
- package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
- package/kits/hub-game/scripts/test-manifest.mjs +122 -1
- package/kits/hub-game/scripts/test-npcs.mjs +34 -24
- package/kits/hub-game/scripts/test-party.mjs +108 -4
- package/kits/hub-game/scripts/test-say.mjs +59 -2
- package/kits/hub-game/scripts/test-tiers.mjs +854 -0
- package/kits/hub-game/scripts/test-together.mjs +33 -1
- package/kits/hub-game/scripts/test-try-on.mjs +940 -0
- package/kits/physics-2d/CLAUDE.md +2 -0
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/scene.js +127 -0
- package/kits/physics-2d/package.json +1 -1
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +298 -0
- package/kits/physics-3d/CLAUDE.md +531 -0
- package/kits/physics-3d/behaviors/Player.jsx +437 -3
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/docs/pxmodel-format.md +4 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -1
- package/kits/physics-3d/engine3d/actornode.js +253 -0
- package/kits/physics-3d/engine3d/nearshadows.js +600 -0
- package/kits/physics-3d/engine3d/optimize.js +49 -0
- package/kits/physics-3d/engine3d/orbitinput.js +277 -0
- package/kits/physics-3d/engine3d/unchanged.js +153 -0
- package/kits/physics-3d/engine3d/world3d.js +525 -11
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
- package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
- package/kits/physics-3d/scripts/test-camera-follow.mjs +543 -0
- package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
- package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
- package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +667 -0
- package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
- package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
- package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
- package/kits/physics-3d/systems/physics3d.js +273 -9
- package/package.json +1 -1
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// Pointer, wheel and right-button input for a scene that set `"engine": {
|
|
2
|
+
// "camera": { "follow": "rigid", "control": "orbit" } }` (the kit's CLAUDE.md
|
|
3
|
+
// has the terms): the player turns, tilts and zooms the Player's camera.
|
|
4
|
+
// Scene3DPlayer's `attachInput` makes this the first time a pointer goes down
|
|
5
|
+
// under the key or a Player runs under it (`runtime.orbitInput()`), hands
|
|
6
|
+
// each pointer that goes down under the key to it, and leaves every other one
|
|
7
|
+
// to the handlers it always had. A runtime that never meets the key never
|
|
8
|
+
// makes it.
|
|
9
|
+
//
|
|
10
|
+
// - touch and pen: a drag that starts in the left half of the canvas is the
|
|
11
|
+
// movement stick (`runtime.moveInput`, as without the key); one that starts
|
|
12
|
+
// in the right half turns and tilts the camera; two fingers that both
|
|
13
|
+
// landed in the right half are a pinch, which zooms. The left half is the
|
|
14
|
+
// stick's: a finger that lands there is never part of a pinch.
|
|
15
|
+
// - mouse: a drag with any button turns and tilts; the wheel zooms.
|
|
16
|
+
// - a short tap that did not move is a jump, as without the key (a mouse's
|
|
17
|
+
// left button only).
|
|
18
|
+
//
|
|
19
|
+
// What the camera is asked for is added up on `runtime.lookInput` until the
|
|
20
|
+
// Player takes it (`consume`), once a frame.
|
|
21
|
+
|
|
22
|
+
// Radians of turn or tilt per CSS pixel dragged.
|
|
23
|
+
export const LOOK_RAD_PER_PX = 0.006;
|
|
24
|
+
// One notch of a mouse wheel moves the camera this much nearer or farther.
|
|
25
|
+
export const WHEEL_ZOOM_PER_NOTCH = 1.1;
|
|
26
|
+
const WHEEL_NOTCH_PX = 100;
|
|
27
|
+
// A wheel that reports lines: a notch is three of them.
|
|
28
|
+
const WHEEL_LINE_PX = WHEEL_NOTCH_PX / 3;
|
|
29
|
+
// The most one wheel event zooms (a flung trackpad reports hundreds of px).
|
|
30
|
+
const WHEEL_ZOOM_MOST = 1.5;
|
|
31
|
+
// Fingers closer than this count as this far apart, so a pinch that closes
|
|
32
|
+
// to nothing does not zoom without bound.
|
|
33
|
+
const PINCH_NEAREST_PX = 24;
|
|
34
|
+
// What is asked for and not yet taken stays within this.
|
|
35
|
+
const ZOOM_PENDING_MOST = 16;
|
|
36
|
+
|
|
37
|
+
const within = (value, least, most) => Math.max(least, Math.min(most, value));
|
|
38
|
+
|
|
39
|
+
// The stick a drag of (dx, dy) pixels holds: that over `radius`, no longer
|
|
40
|
+
// than 1.
|
|
41
|
+
export function stickFrom(dx, dy, radius) {
|
|
42
|
+
const len = Math.hypot(dx, dy) / radius;
|
|
43
|
+
const scale = len > 1 ? 1 / (len * radius) : 1 / radius;
|
|
44
|
+
return { x: dx * scale, y: dy * scale };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The pointers that are down and what each is doing (its role):
|
|
48
|
+
// 'tap' down, not yet moved past the slop
|
|
49
|
+
// 'stick' holds the movement stick
|
|
50
|
+
// 'look' turns and tilts the camera
|
|
51
|
+
// 'pinch' one of the two fingers zooming
|
|
52
|
+
// 'spent' nothing more until it lifts (the finger left over from a pinch,
|
|
53
|
+
// a second drag in the left half, a drag begun in the right half
|
|
54
|
+
// while two fingers pinch)
|
|
55
|
+
// No DOM here: `down`, `move`, `up` and `wheel` are handed plain numbers, so
|
|
56
|
+
// the roles can be tested. `stick` is `runtime.moveInput`'s object, `onTap`
|
|
57
|
+
// is called for a jump, `tuning` is attachInput's { stickRadius, tapSlop,
|
|
58
|
+
// tapMs }.
|
|
59
|
+
export function makeLookGestures(stick, onTap, tuning) {
|
|
60
|
+
const pointers = new Map();
|
|
61
|
+
let stickId = null;
|
|
62
|
+
let locked = false;
|
|
63
|
+
const asked = { yaw: 0, pitch: 0, zoom: 1 };
|
|
64
|
+
const taken = { yaw: 0, pitch: 0, zoom: 1, locked: false };
|
|
65
|
+
const holding = (role) => [...pointers.values()].filter((p) => p.role === role);
|
|
66
|
+
const letGoOfStick = () => {
|
|
67
|
+
stickId = null;
|
|
68
|
+
stick.active = false;
|
|
69
|
+
stick.x = 0;
|
|
70
|
+
stick.y = 0;
|
|
71
|
+
};
|
|
72
|
+
const drop = () => {
|
|
73
|
+
asked.yaw = 0;
|
|
74
|
+
asked.pitch = 0;
|
|
75
|
+
asked.zoom = 1;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// What `runtime.lookInput` is: the turn and tilt (radians) and the zoom (a
|
|
79
|
+
// factor on the camera's distance; under 1 is nearer) asked for since they
|
|
80
|
+
// were last taken.
|
|
81
|
+
const look = {
|
|
82
|
+
// Takes what was asked for. The object returned is the same one each
|
|
83
|
+
// time. After `lock` it takes nothing: what was asked is dropped, and
|
|
84
|
+
// `locked` says the camera is not the player's this frame.
|
|
85
|
+
consume() {
|
|
86
|
+
taken.locked = locked;
|
|
87
|
+
taken.yaw = locked ? 0 : asked.yaw;
|
|
88
|
+
taken.pitch = locked ? 0 : asked.pitch;
|
|
89
|
+
taken.zoom = locked ? 1 : asked.zoom;
|
|
90
|
+
locked = false;
|
|
91
|
+
drop();
|
|
92
|
+
return taken;
|
|
93
|
+
},
|
|
94
|
+
// The next `consume` takes nothing (the hub during a door trip: its
|
|
95
|
+
// camera, not the player's). Called again each frame it should hold.
|
|
96
|
+
lock() {
|
|
97
|
+
locked = true;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// What a pointer that has just moved past the slop becomes.
|
|
102
|
+
const roleOnDrag = (p) => {
|
|
103
|
+
if (p.mouse) return 'look';
|
|
104
|
+
if (p.left) return stickId === null ? 'stick' : 'spent';
|
|
105
|
+
return holding('pinch').length > 0 ? 'spent' : 'look';
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const zoomBy = (factor) => {
|
|
109
|
+
asked.zoom = within(asked.zoom * factor, 1 / ZOOM_PENDING_MOST, ZOOM_PENDING_MOST);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const apart = (a, b) => Math.max(PINCH_NEAREST_PX, Math.hypot(a.x - b.x, a.y - b.y));
|
|
113
|
+
|
|
114
|
+
const gestures = {
|
|
115
|
+
// Whether any pointer is down here.
|
|
116
|
+
busy: false,
|
|
117
|
+
look,
|
|
118
|
+
roleOf: (id) => pointers.get(id)?.role ?? null,
|
|
119
|
+
|
|
120
|
+
// `at` is { x, y, time, mouse, button, left }: where (CSS px), when (ms),
|
|
121
|
+
// whether it is a mouse and which button, and whether it is in the left
|
|
122
|
+
// half of the canvas.
|
|
123
|
+
down(id, at) {
|
|
124
|
+
const p = { x0: at.x, y0: at.y, x: at.x, y: at.y, time: at.time, moved: false, role: 'tap', mouse: at.mouse === true, button: at.button ?? 0, left: at.left === true };
|
|
125
|
+
if (!p.mouse && !p.left && holding('pinch').length === 0) {
|
|
126
|
+
// A second finger in the right half, beside one that landed there
|
|
127
|
+
// and is still a tap or turning the camera: a pinch. A finger in the
|
|
128
|
+
// left half is the stick's and is left out of it, on either side.
|
|
129
|
+
const others = [...pointers.values()].filter((o) => !o.mouse && !o.left && (o.role === 'tap' || o.role === 'look'));
|
|
130
|
+
if (others.length === 1) {
|
|
131
|
+
others[0].role = 'pinch';
|
|
132
|
+
p.role = 'pinch';
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
pointers.set(id, p);
|
|
136
|
+
gestures.busy = true;
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
// Returns whether the pointer is one that went down here.
|
|
140
|
+
move(id, x, y) {
|
|
141
|
+
const p = pointers.get(id);
|
|
142
|
+
if (!p) return false;
|
|
143
|
+
const from = { x: p.x, y: p.y };
|
|
144
|
+
if (!p.moved && Math.hypot(x - p.x0, y - p.y0) > tuning.tapSlop) {
|
|
145
|
+
p.moved = true;
|
|
146
|
+
if (p.role === 'tap') p.role = roleOnDrag(p);
|
|
147
|
+
if (p.role === 'stick') stickId = id;
|
|
148
|
+
}
|
|
149
|
+
if (p.role === 'pinch') {
|
|
150
|
+
const other = holding('pinch').find((o) => o !== p);
|
|
151
|
+
const before = other ? apart(from, other) : 0;
|
|
152
|
+
p.x = x;
|
|
153
|
+
p.y = y;
|
|
154
|
+
// Fingers moving apart bring the camera nearer.
|
|
155
|
+
if (other) zoomBy(before / apart(p, other));
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
p.x = x;
|
|
159
|
+
p.y = y;
|
|
160
|
+
if (p.role === 'stick') {
|
|
161
|
+
Object.assign(stick, stickFrom(x - p.x0, y - p.y0, tuning.stickRadius));
|
|
162
|
+
stick.active = true;
|
|
163
|
+
} else if (p.role === 'look') {
|
|
164
|
+
// Dragging right swings the view right; dragging up looks up.
|
|
165
|
+
asked.yaw += (x - from.x) * LOOK_RAD_PER_PX;
|
|
166
|
+
asked.pitch += (y - from.y) * LOOK_RAD_PER_PX;
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
// A pointer lifted, or cancelled (no tap then). Returns whether it is one
|
|
172
|
+
// that went down here.
|
|
173
|
+
up(id, time, cancelled) {
|
|
174
|
+
const p = pointers.get(id);
|
|
175
|
+
if (!p) return false;
|
|
176
|
+
pointers.delete(id);
|
|
177
|
+
gestures.busy = pointers.size > 0;
|
|
178
|
+
if (stickId === id) letGoOfStick();
|
|
179
|
+
// The finger left from a pinch neither turns the camera nor walks.
|
|
180
|
+
if (p.role === 'pinch') for (const other of holding('pinch')) other.role = 'spent';
|
|
181
|
+
// Only a finger that is still a tap jumps: never one of a pinch, nor
|
|
182
|
+
// one left spent.
|
|
183
|
+
const tapped = !cancelled && p.role === 'tap' && !p.moved && time - p.time < tuning.tapMs;
|
|
184
|
+
if (tapped && (!p.mouse || p.button === 0)) onTap();
|
|
185
|
+
return true;
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
// Every pointer is let go of at once, with no tap: the stick if one of
|
|
189
|
+
// them held it, what was asked of the camera and not yet taken, and a
|
|
190
|
+
// lock. For a scene load.
|
|
191
|
+
cancel() {
|
|
192
|
+
pointers.clear();
|
|
193
|
+
gestures.busy = false;
|
|
194
|
+
if (stickId !== null) letGoOfStick();
|
|
195
|
+
locked = false;
|
|
196
|
+
drop();
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
// A wheel event's deltaY and deltaMode: away from the player is nearer.
|
|
200
|
+
wheel(deltaY, deltaMode) {
|
|
201
|
+
const px = deltaY * (deltaMode === 1 ? WHEEL_LINE_PX : deltaMode === 2 ? WHEEL_NOTCH_PX : 1);
|
|
202
|
+
if (!Number.isFinite(px) || px === 0) return;
|
|
203
|
+
zoomBy(within(WHEEL_ZOOM_PER_NOTCH ** (px / WHEEL_NOTCH_PX), 1 / WHEEL_ZOOM_MOST, WHEEL_ZOOM_MOST));
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
return gestures;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// The look inputs this file made, so the engine acts on its own input only
|
|
210
|
+
// (`isOrbitLook`): something else on `runtime.lookInput` is left alone.
|
|
211
|
+
const ours = new WeakSet();
|
|
212
|
+
export const isOrbitLook = (look) => ours.has(look);
|
|
213
|
+
|
|
214
|
+
// The gestures on a canvas; this sets `runtime.lookInput`. The wheel and
|
|
215
|
+
// context-menu listeners are added by `runtime.lookInput.listen()`, which the
|
|
216
|
+
// Player calls every frame it runs under the key, and taken off by
|
|
217
|
+
// `runtime.lookInput.reset()`, which the physics system calls on every scene
|
|
218
|
+
// load and on dispose (`Physics3DSystem.reset`), with every pointer let go of
|
|
219
|
+
// (`cancel`). A tap already handed to attachInput and not yet taken stays
|
|
220
|
+
// with it across the load, as it does without the key. So from the first
|
|
221
|
+
// frame of a scene without the key there is no
|
|
222
|
+
// pointer, listener or pending look here, and a pointer held across any load
|
|
223
|
+
// does nothing more until it lifts: the handlers attachInput always had do
|
|
224
|
+
// not know it either.
|
|
225
|
+
export function attachOrbitInput(canvas, runtime, stick, onTap, tuning) {
|
|
226
|
+
const gestures = makeLookGestures(stick, onTap, tuning);
|
|
227
|
+
let listening = false;
|
|
228
|
+
const onWheel = (event) => {
|
|
229
|
+
event.preventDefault();
|
|
230
|
+
gestures.wheel(event.deltaY, event.deltaMode);
|
|
231
|
+
};
|
|
232
|
+
const onContextMenu = (event) => event.preventDefault();
|
|
233
|
+
const unlisten = () => {
|
|
234
|
+
if (!listening) return;
|
|
235
|
+
listening = false;
|
|
236
|
+
canvas.removeEventListener('wheel', onWheel);
|
|
237
|
+
canvas.removeEventListener('contextmenu', onContextMenu);
|
|
238
|
+
};
|
|
239
|
+
gestures.look.listen = () => {
|
|
240
|
+
if (listening || runtime.cameraControl !== 'orbit') return;
|
|
241
|
+
listening = true;
|
|
242
|
+
canvas.addEventListener('wheel', onWheel, { passive: false });
|
|
243
|
+
canvas.addEventListener('contextmenu', onContextMenu);
|
|
244
|
+
};
|
|
245
|
+
gestures.look.reset = () => {
|
|
246
|
+
gestures.cancel();
|
|
247
|
+
unlisten();
|
|
248
|
+
};
|
|
249
|
+
ours.add(gestures.look);
|
|
250
|
+
runtime.lookInput = gestures.look;
|
|
251
|
+
return {
|
|
252
|
+
// Whether a pointer that went down under the key is still down: its
|
|
253
|
+
// moves and its lift are this file's.
|
|
254
|
+
get busy() {
|
|
255
|
+
return gestures.busy;
|
|
256
|
+
},
|
|
257
|
+
down(event) {
|
|
258
|
+
canvas.focus();
|
|
259
|
+
const rect = canvas.getBoundingClientRect();
|
|
260
|
+
gestures.down(event.pointerId, {
|
|
261
|
+
x: event.clientX,
|
|
262
|
+
y: event.clientY,
|
|
263
|
+
time: performance.now(),
|
|
264
|
+
mouse: event.pointerType === 'mouse',
|
|
265
|
+
button: event.button,
|
|
266
|
+
left: event.clientX - rect.left < rect.width / 2,
|
|
267
|
+
});
|
|
268
|
+
canvas.setPointerCapture(event.pointerId);
|
|
269
|
+
},
|
|
270
|
+
move: (event) => gestures.move(event.pointerId, event.clientX, event.clientY),
|
|
271
|
+
up: (event) => gestures.up(event.pointerId, performance.now(), event.type === 'pointercancel'),
|
|
272
|
+
detach: () => {
|
|
273
|
+
gestures.cancel();
|
|
274
|
+
unlisten();
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// The terms of `"engine": { "skipUnchangedActors": true }`:
|
|
2
|
+
//
|
|
3
|
+
// With the key absent, a scene runs the engine exactly as before. With the key
|
|
4
|
+
// present, each frame equals what the engine would have produced without it
|
|
5
|
+
// PROVIDED the scene's components are plain stored data (no accessors, Proxies,
|
|
6
|
+
// toJSON, custom iterators) and three.js objects under actors are changed only
|
|
7
|
+
// through position/rotation/quaternion/scale/visible/parenting. A scene that does
|
|
8
|
+
// not meet that is unsupported under the key and must not set it.
|
|
9
|
+
//
|
|
10
|
+
// "Is this component still what it was?" answered without building a string.
|
|
11
|
+
// Used only by scenes that set `"engine": { "skipUnchangedActors": true }`.
|
|
12
|
+
//
|
|
13
|
+
// The engine decides whether to rebuild an actor's mesh or its physics body by
|
|
14
|
+
// comparing a signature: `JSON.stringify` of the components that shape it.
|
|
15
|
+
// Building that string for every actor on every frame costs more than drawing
|
|
16
|
+
// a small scene. `remember(value)` keeps what the string was built from, and
|
|
17
|
+
// `sameAs(value, memory)` says whether `JSON.stringify(value)` would come out
|
|
18
|
+
// the same again, by reading the value's own fields against the memory: the
|
|
19
|
+
// same object with the same prototype, the same keys in the same order, the
|
|
20
|
+
// same primitive under each. It says no more often than the string would (a
|
|
21
|
+
// different object holding equal values, a NaN), and the caller then builds
|
|
22
|
+
// the string and compares that, as it always did.
|
|
23
|
+
//
|
|
24
|
+
// Only plain data is read this way: objects whose prototype is Object's (or
|
|
25
|
+
// none) and arrays whose prototype is Array's, holding stored values. Anything
|
|
26
|
+
// that computes what it hands out is never "the same" and takes the string
|
|
27
|
+
// path every frame, read exactly as often and in the order it always was:
|
|
28
|
+
// an object or array with an accessor property, with a `toJSON` (its own or
|
|
29
|
+
// inherited, enumerable or not, stored or itself a getter), of any other class
|
|
30
|
+
// (a Date, a Map), and a bigint, which `JSON.stringify` throws on. `remember`
|
|
31
|
+
// and `sameAs` find that out from property descriptors and `in`, without
|
|
32
|
+
// calling a getter. An array is read by index up to its length, as JSON reads
|
|
33
|
+
// it, whatever its iterator does.
|
|
34
|
+
//
|
|
35
|
+
// That last paragraph is protection, not part of the terms: a component that is
|
|
36
|
+
// not plain stored data is unsupported under the key. Other reads of such a
|
|
37
|
+
// component elsewhere in the skipping path (a Transform's fields, `Shape.solid`)
|
|
38
|
+
// are not guarded, a Proxy cannot be told from the object it stands in for, and
|
|
39
|
+
// a stored field redefined as an accessor in place is read once here before
|
|
40
|
+
// the string path reads it again.
|
|
41
|
+
const NEVER = { never: true };
|
|
42
|
+
|
|
43
|
+
// `'toJSON' in value`, not `value.toJSON`: asking whether there is one, on the
|
|
44
|
+
// value or anything it inherits from, calls no getter.
|
|
45
|
+
function holdsOnlyStoredValues(value) {
|
|
46
|
+
if ('toJSON' in value) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
50
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
51
|
+
if (!('value' in descriptors[key])) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// How deep a value is read. Past this it is "never the same": the walk here
|
|
59
|
+
// goes down by calling itself, and a value JSON.stringify can still write may
|
|
60
|
+
// be deeper than a call stack allows. Components are a few levels deep.
|
|
61
|
+
const DEEPEST = 32;
|
|
62
|
+
|
|
63
|
+
export function remember(value, depth = 0) {
|
|
64
|
+
if (typeof value === 'bigint') {
|
|
65
|
+
return NEVER;
|
|
66
|
+
}
|
|
67
|
+
if (typeof value !== 'object' || value === null) {
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
if (depth >= DEEPEST) {
|
|
71
|
+
return NEVER;
|
|
72
|
+
}
|
|
73
|
+
const proto = Object.getPrototypeOf(value);
|
|
74
|
+
const isArray = Array.isArray(value);
|
|
75
|
+
const plain = isArray ? proto === Array.prototype : proto === Object.prototype || proto === null;
|
|
76
|
+
if (!plain || !holdsOnlyStoredValues(value)) {
|
|
77
|
+
return NEVER;
|
|
78
|
+
}
|
|
79
|
+
if (isArray) {
|
|
80
|
+
// By index up to `length`, which is how JSON reads an array; not by its
|
|
81
|
+
// iterator, which can be anyone's.
|
|
82
|
+
const values = [];
|
|
83
|
+
for (let i = 0; i < value.length; i++) {
|
|
84
|
+
values.push(remember(value[i], depth + 1));
|
|
85
|
+
}
|
|
86
|
+
return { of: value, proto, keys: null, values };
|
|
87
|
+
}
|
|
88
|
+
const keys = Object.keys(value);
|
|
89
|
+
return { of: value, proto, keys, values: keys.map((key) => remember(value[key], depth + 1)) };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function sameAs(value, memory) {
|
|
93
|
+
if (typeof value !== 'object' || value === null) {
|
|
94
|
+
// A primitive is never `===` a memory of an object, NEVER included, and
|
|
95
|
+
// NaN is not `===` itself.
|
|
96
|
+
return value === memory;
|
|
97
|
+
}
|
|
98
|
+
if (typeof memory !== 'object' || memory === null || memory.of !== value) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (Object.getPrototypeOf(value) !== memory.proto || 'toJSON' in value) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return memory.keys === null ? sameItems(value, memory.values) : sameFields(value, memory);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Most fields are primitives that are simply still equal; only what is not
|
|
108
|
+
// `===` (an object, a NaN, a changed value) is looked at further.
|
|
109
|
+
function sameFields(value, { keys, values }) {
|
|
110
|
+
let i = 0;
|
|
111
|
+
for (const key in value) {
|
|
112
|
+
if (key !== keys[i]) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
const now = value[key];
|
|
116
|
+
if (now !== values[i] && !sameAs(now, values[i])) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
i += 1;
|
|
120
|
+
}
|
|
121
|
+
return i === keys.length;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function sameItems(value, values) {
|
|
125
|
+
if (value.length !== values.length) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
for (let i = 0; i < values.length; i++) {
|
|
129
|
+
const now = value[i];
|
|
130
|
+
if (now !== values[i] && !sameAs(now, values[i])) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// An object that reads like `target` and writes down each read as `key, value`
|
|
138
|
+
// in `reads`, so a signature built from a lookup table (`sprites`, the deck's
|
|
139
|
+
// files) can be checked later by making the same reads again. A missing table
|
|
140
|
+
// reads as an empty one, which is what `table?.[key]` gives.
|
|
141
|
+
const EMPTY = Object.freeze({});
|
|
142
|
+
|
|
143
|
+
export function recordReads(target, reads) {
|
|
144
|
+
return new Proxy(target ?? EMPTY, {
|
|
145
|
+
get(table, key) {
|
|
146
|
+
const value = table[key];
|
|
147
|
+
if (typeof key === 'string') {
|
|
148
|
+
reads.push(key, value);
|
|
149
|
+
}
|
|
150
|
+
return value;
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
}
|