@wildshard/engine 0.1.17 → 0.1.18

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.
Files changed (83) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/lint/engine-sides.json +7 -0
  3. package/lint/host-callback-syntax.mjs +2 -1
  4. package/lint/host-callbacks.mjs +3 -3
  5. package/lint/host-files.mjs +5 -4
  6. package/lint/no-base64.mjs +3 -1
  7. package/lint/shard-imports.mjs +6 -7
  8. package/lint/shard-source.d.mts +9 -0
  9. package/lint/shard-source.mjs +91 -0
  10. package/lint/shardfile-boundaries.mjs +10 -8
  11. package/lint/sim-closure.mjs +6 -3
  12. package/lint/wildshard-plugin.d.ts +4 -0
  13. package/lint/wildshard-plugin.js +22 -23
  14. package/package.json +9 -2
  15. package/scripts/app-build/outputProducts.d.mts +3 -0
  16. package/scripts/app-build/outputProducts.mjs +31 -0
  17. package/scripts/app-build-pipeline.mjs +3 -0
  18. package/scripts/check-devserver.mjs +1 -1
  19. package/scripts/chromium-keychain-check.mjs +4 -2
  20. package/scripts/chromium-keychain.mjs +3 -1
  21. package/scripts/cook.mjs +7 -3
  22. package/scripts/devices.d.mts +4 -0
  23. package/scripts/devices.mjs +39 -0
  24. package/scripts/devserver-client.d.mts +4 -2
  25. package/scripts/devserver-client.mjs +25 -8
  26. package/scripts/doctor-proof.mjs +1 -1
  27. package/scripts/e2e-faults.mjs +159 -0
  28. package/scripts/grid-crossing-walk-proof.mjs +14 -3
  29. package/scripts/memory-proof.mjs +158 -0
  30. package/scripts/offline-proof-rules.d.mts +4 -2
  31. package/scripts/offline-proof-rules.mjs +21 -5
  32. package/scripts/offline-proof.mjs +15 -6
  33. package/scripts/tier-textures-proof.mjs +101 -0
  34. package/scripts/tier-textures.mjs +60 -0
  35. package/scripts/tsconfig.json +1 -0
  36. package/scripts/vps/wildshard-config +79 -23
  37. package/scripts/worker-client-play.mjs +1 -0
  38. package/scripts/worker-client-proof.mjs +1 -1
  39. package/scripts/worker-client-slug.mjs +44 -0
  40. package/src/client/app/entry.ts +37 -10
  41. package/src/client/engine/boot/parsedPages.ts +127 -58
  42. package/src/client/engine/build.d.ts +2 -0
  43. package/src/client/engine/core/Game.ts +18 -0
  44. package/src/client/engine/core/bootstrap.ts +4 -1
  45. package/src/client/engine/debug/probe.ts +18 -1
  46. package/src/client/engine/fx/LightPool.ts +41 -1
  47. package/src/client/engine/pwa/sw.js +12 -3
  48. package/src/client/engine/render/precompile.ts +20 -14
  49. package/src/client/engine/telemetry/browserErrors.ts +4 -1
  50. package/src/client/engine/telemetry/deviceSnapshot.ts +155 -0
  51. package/src/client/game/boot/audio.generated.ts +0 -102
  52. package/src/client/game/boot/bytes.generated.ts +42 -60
  53. package/src/client/game/boot/versions.generated.ts +0 -2
  54. package/src/client/game/grid/area.ts +38 -0
  55. package/src/client/game/grid/live.ts +28 -2
  56. package/src/client/game/grid/liveSession.ts +14 -3
  57. package/src/client/game/grid/menu.ts +4 -0
  58. package/src/client/game/grid/neighbourPanels.ts +10 -1
  59. package/src/client/game/grid/placedCards.ts +55 -0
  60. package/src/client/game/grid/placedCells.ts +131 -0
  61. package/src/client/game/grid/regionalRuntime.ts +6 -0
  62. package/src/client/game/grid/regionalWorld.ts +15 -2
  63. package/src/client/game/grid/roadCull.ts +44 -14
  64. package/src/client/game/grid/roadTileBacking.ts +13 -3
  65. package/src/client/game/grid/session.ts +9 -0
  66. package/src/client/game/grid/shardfileFar.ts +9 -3
  67. package/src/client/game/session/finish.ts +5 -1
  68. package/src/client/game/shard/titleList.ts +6 -2
  69. package/src/client/game/shardfile/clientWorker.ts +2 -1
  70. package/src/client/game/titleDeck.ts +4 -3
  71. package/src/client/host/online/shell.ts +14 -0
  72. package/src/cook/game/shardCook.ts +38 -8
  73. package/src/cook/game/tierTextures.ts +276 -0
  74. package/src/e2e/smoke/game.ts +19 -14
  75. package/src/e2e/smoke/page-globals.d.ts +9 -8
  76. package/src/e2e/smoke/template2.spec.ts +8 -7
  77. package/src/server/host/dedicated/placementFile.ts +1 -17
  78. package/src/server/host/dedicated/socketTransport.ts +2 -1
  79. package/src/server/host/dedicated/world.ts +30 -1
  80. package/src/shared/engine/net/deviceWord.ts +138 -0
  81. package/src/shared/engine/net/socketFrames.ts +7 -4
  82. package/src/shared/game/grid/catalogueSlot.ts +7 -0
  83. package/src/shared/game/grid/placementRows.ts +33 -0
@@ -0,0 +1,276 @@
1
+ /** S12 (J330): cook-side tier masters, first slice. A shard holds one master per texture at the largest size any tier
2
+ * wants; the cook gives each tier its own size by dropping the KTX2 mip levels above that tier's largest. No re-encode:
3
+ * every level a tier keeps is the master's own bytes, so a phone draws the master's own pixels at every distance where
4
+ * the master's top level was minified anyway, which is most of what a phone draws.
5
+ *
6
+ * Cuttable: UASTC (`vkFormat` 0) and raw ASTC, with Zstandard or no supercompression, whose levels stand alone.
7
+ * BasisLZ (ETC1S) shares a codebook across levels, so it stays as it is until the cook re-encodes (S12c).
8
+ *
9
+ * GLBs carry their images inside (57 of Pine's 102 MB of `.glb`). `glbImages` takes them out, and `tierGlb` puts each
10
+ * back at the tier's size, moving every other buffer view and meshopt stream by whole 16-byte steps so no alignment
11
+ * changes. Pure bytes in, bytes out: no file system, no shardfile. `phoneTier` (S12b) gives a shard's KTX2 table its phone
12
+ * files, which `shardHalf` lists in the phone manifests. */
13
+ // oxlint-disable-next-line import/no-nodejs-modules -- The cook is a Node process (J28).
14
+ import {createHash} from 'node:crypto';
15
+
16
+ const KTX2_ID = [0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a];
17
+ const LEVEL_INDEX = 80;
18
+ const BASIS_LZ = 1;
19
+ const GLB_MAGIC = 0x46546c67, JSON_CHUNK = 0x4e4f534a, BIN_CHUNK = 0x004e4942;
20
+
21
+ /** Vulkan ASTC formats (UNORM and SRGB pairs) to their block size. */
22
+ const ASTC_BLOCKS: Readonly<Record<number, readonly [number, number]>> = {
23
+ 157: [4, 4], 158: [4, 4], 159: [5, 4], 160: [5, 4], 161: [5, 5], 162: [5, 5], 163: [6, 5], 164: [6, 5],
24
+ 165: [6, 6], 166: [6, 6], 167: [8, 5], 168: [8, 5], 169: [8, 6], 170: [8, 6], 171: [8, 8], 172: [8, 8],
25
+ };
26
+
27
+ export interface Ktx2Info {
28
+ readonly vkFormat: number;
29
+ readonly width: number;
30
+ readonly height: number;
31
+ readonly levels: number;
32
+ readonly scheme: number;
33
+ }
34
+
35
+ const viewOf = (bytes: Uint8Array): DataView => new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
36
+
37
+ export function isKtx2(bytes: Uint8Array): boolean {
38
+ return bytes.length >= LEVEL_INDEX && KTX2_ID.every((byte, at) => bytes[at] === byte);
39
+ }
40
+
41
+ export function ktx2Info(bytes: Uint8Array): Ktx2Info {
42
+ if (!isKtx2(bytes)) throw new Error('Not a KTX2 file');
43
+ const view = viewOf(bytes);
44
+ return {vkFormat: view.getUint32(12, true), width: view.getUint32(20, true), height: view.getUint32(24, true), levels: view.getUint32(40, true), scheme: view.getUint32(44, true)};
45
+ }
46
+
47
+ /** The same texture with its largest side at most `maxSize`, or the same bytes when there is nothing it can cut. */
48
+ export function trimKtx2(bytes: Uint8Array, maxSize: number): Uint8Array {
49
+ if (!isKtx2(bytes)) return bytes;
50
+ const view = viewOf(bytes), info = ktx2Info(bytes);
51
+ const depth = view.getUint32(28, true), layers = view.getUint32(32, true), faces = view.getUint32(36, true);
52
+ // Only block formats whose 16-byte blocks keep a level's alignment: UASTC (`vkFormat` 0) and ASTC.
53
+ if ((info.vkFormat !== 0 && ASTC_BLOCKS[info.vkFormat] === undefined) || info.scheme === BASIS_LZ || info.levels <= 1 || depth > 1 || layers > 1 || faces !== 1) return bytes;
54
+ let drop = 0;
55
+ while (drop < info.levels - 1 && Math.max(info.width >> drop, info.height >> drop) > maxSize) drop++;
56
+ if (drop === 0) return bytes;
57
+
58
+ const levels = info.levels - drop, dfdAt = view.getUint32(48, true), dfdLength = view.getUint32(52, true);
59
+ const kvdAt = view.getUint32(56, true), kvdLength = view.getUint32(60, true);
60
+ const sgdAt = Number(view.getBigUint64(64, true)), sgdLength = Number(view.getBigUint64(72, true));
61
+ const kept = Array.from({length: levels}, (_, level) => {
62
+ const entry = LEVEL_INDEX + 24 * (level + drop);
63
+ const at = Number(view.getBigUint64(entry, true)), length = Number(view.getBigUint64(entry + 8, true));
64
+ return {body: bytes.subarray(at, at + length), uncompressed: view.getBigUint64(entry + 16, true)};
65
+ });
66
+ // Unsupercompressed levels sit on 16 bytes (every cuttable block is 16 bytes, and 16 is a multiple of 4); compressed ones on 1.
67
+ const levelAlign = info.scheme === 0 ? 16 : 1;
68
+ const align = (at: number, to: number): number => Math.ceil(at / to) * to;
69
+ const newDfd = LEVEL_INDEX + 24 * levels, newKvd = kvdLength === 0 ? 0 : newDfd + dfdLength;
70
+ let at = newDfd + dfdLength + kvdLength;
71
+ const newSgd = sgdLength === 0 ? 0 : align(at, 8);
72
+ if (sgdLength > 0) at = newSgd + sgdLength;
73
+ const offsets: number[] = [];
74
+ for (let level = levels - 1; level >= 0; level--) { at = align(at, levelAlign); offsets[level] = at; at += kept[level]?.body.length ?? 0; }
75
+
76
+ const out = new Uint8Array(at), write = viewOf(out);
77
+ out.set(bytes.subarray(0, LEVEL_INDEX));
78
+ write.setUint32(20, Math.max(1, info.width >> drop), true);
79
+ write.setUint32(24, info.height === 0 ? 0 : Math.max(1, info.height >> drop), true);
80
+ write.setUint32(40, levels, true);
81
+ write.setUint32(48, newDfd, true);
82
+ write.setUint32(56, newKvd, true);
83
+ write.setBigUint64(64, BigInt(newSgd), true);
84
+ out.set(bytes.subarray(dfdAt, dfdAt + dfdLength), newDfd);
85
+ if (kvdLength > 0) out.set(bytes.subarray(kvdAt, kvdAt + kvdLength), newKvd);
86
+ if (sgdLength > 0) out.set(bytes.subarray(sgdAt, sgdAt + sgdLength), newSgd);
87
+ kept.forEach(({body, uncompressed}, level) => {
88
+ const entry = LEVEL_INDEX + 24 * level, offset = offsets[level] ?? 0;
89
+ write.setBigUint64(entry, BigInt(offset), true);
90
+ write.setBigUint64(entry + 8, BigInt(body.length), true);
91
+ write.setBigUint64(entry + 16, uncompressed, true);
92
+ out.set(body, offset);
93
+ });
94
+ return out;
95
+ }
96
+
97
+ interface GltfView {buffer: number; byteOffset?: number; byteLength: number; extensions?: {EXT_meshopt_compression?: {buffer: number; byteOffset?: number; byteLength: number}}}
98
+ interface Gltf {accessors?: {bufferView?: number; sparse?: {indices: {bufferView: number}; values: {bufferView: number}}}[]; bufferViews?: GltfView[]; buffers?: {byteLength: number; uri?: string}[]; images?: {bufferView?: number; mimeType?: string; uri?: string}[]}
99
+ interface Glb {readonly json: Gltf; readonly bin: Uint8Array}
100
+
101
+ function readGlb(bytes: Uint8Array): Glb | undefined {
102
+ if (bytes.length < 20) return undefined;
103
+ const view = viewOf(bytes);
104
+ if (view.getUint32(0, true) !== GLB_MAGIC || view.getUint32(16, true) !== JSON_CHUNK) return undefined;
105
+ const jsonLength = view.getUint32(12, true), json = JSON.parse(new TextDecoder().decode(bytes.subarray(20, 20 + jsonLength))) as Gltf;
106
+ const binAt = 20 + jsonLength;
107
+ if (binAt + 8 > bytes.length || view.getUint32(binAt + 4, true) !== BIN_CHUNK) return {json, bin: new Uint8Array()};
108
+ return {json, bin: bytes.subarray(binAt + 8, binAt + 8 + view.getUint32(binAt, true))};
109
+ }
110
+
111
+ function writeGlb(json: Gltf, bin: Uint8Array): Uint8Array {
112
+ const text = new TextEncoder().encode(JSON.stringify(json)), jsonLength = Math.ceil(text.length / 4) * 4, binLength = Math.ceil(bin.length / 4) * 4;
113
+ const out = new Uint8Array(12 + 8 + jsonLength + (binLength > 0 ? 8 + binLength : 0)), view = viewOf(out);
114
+ view.setUint32(0, GLB_MAGIC, true); view.setUint32(4, 2, true); view.setUint32(8, out.length, true);
115
+ view.setUint32(12, jsonLength, true); view.setUint32(16, JSON_CHUNK, true);
116
+ out.fill(0x20, 20, 20 + jsonLength); out.set(text, 20);
117
+ if (binLength > 0) { view.setUint32(20 + jsonLength, binLength, true); view.setUint32(24 + jsonLength, BIN_CHUNK, true); out.set(bin, 28 + jsonLength); }
118
+ return out;
119
+ }
120
+
121
+ export interface GlbImage {readonly index: number; readonly mimeType: string; readonly bytes: Uint8Array}
122
+
123
+ /** Every image a GLB carries in its own buffer, in image order. */
124
+ export function glbImages(bytes: Uint8Array): readonly GlbImage[] {
125
+ const glb = readGlb(bytes);
126
+ if (glb === undefined) return [];
127
+ return (glb.json.images ?? []).flatMap((image, index) => {
128
+ const view = image.bufferView === undefined ? undefined : glb.json.bufferViews?.[image.bufferView];
129
+ if (view?.buffer !== 0) return [];
130
+ const at = view.byteOffset ?? 0;
131
+ return [{index, mimeType: image.mimeType ?? '', bytes: glb.bin.subarray(at, at + view.byteLength)}];
132
+ });
133
+ }
134
+
135
+ /** The GLB with each embedded image swapped for `replace`'s answer (the same image when it answers the same bytes). Every
136
+ * other range of the buffer keeps its bytes and moves down by whole 16-byte steps. Overlapping or shared image views
137
+ * are left alone: the same bytes come back. */
138
+ export function withImages(bytes: Uint8Array, replace: (image: GlbImage) => Uint8Array): Uint8Array {
139
+ const glb = readGlb(bytes);
140
+ if (glb === undefined) return bytes;
141
+ const views = glb.json.bufferViews ?? [];
142
+ const cuts = new Map<number, Uint8Array>();
143
+ for (const image of glbImages(bytes)) {
144
+ const viewIndex = glb.json.images?.[image.index]?.bufferView;
145
+ if (viewIndex === undefined) continue;
146
+ const next = replace(image);
147
+ if (next !== image.bytes && next.length < image.bytes.length) cuts.set(viewIndex, next);
148
+ }
149
+ if (cuts.size === 0) return bytes;
150
+ // A cut view must be used by images alone and overlap nothing else in buffer 0, or moving the rest would tear it.
151
+ const ranges = views.flatMap((view, index) => {
152
+ const own = view.buffer === 0 ? [{index, at: view.byteOffset ?? 0, end: (view.byteOffset ?? 0) + view.byteLength}] : [];
153
+ const meshopt = view.extensions?.EXT_meshopt_compression;
154
+ return meshopt?.buffer === 0 ? [...own, {index: -1, at: meshopt.byteOffset ?? 0, end: (meshopt.byteOffset ?? 0) + meshopt.byteLength}] : own;
155
+ });
156
+ const imageViews = (glb.json.images ?? []).map(image => image.bufferView);
157
+ for (const viewIndex of cuts.keys()) {
158
+ const cut = ranges.find(range => range.index === viewIndex);
159
+ const shared = imageViews.filter(view => view === viewIndex).length > 1
160
+ || (glb.json.accessors ?? []).some(accessor => accessor.bufferView === viewIndex || accessor.sparse?.indices.bufferView === viewIndex || accessor.sparse?.values.bufferView === viewIndex);
161
+ if (cut === undefined || shared || ranges.some(range => range !== cut && range.at < cut.end && cut.at < range.end)) cuts.delete(viewIndex);
162
+ }
163
+ if (cuts.size === 0) return bytes;
164
+
165
+ const order = [...cuts.keys()].sort((a, b) => (views[a]?.byteOffset ?? 0) - (views[b]?.byteOffset ?? 0));
166
+ const parts: Uint8Array[] = [];
167
+ const moves: {at: number; shift: number}[] = [];
168
+ let read = 0, shift = 0;
169
+ for (const viewIndex of order) {
170
+ const view = views[viewIndex], next = cuts.get(viewIndex);
171
+ if (view === undefined || next === undefined) continue;
172
+ const at = view.byteOffset ?? 0, end = at + view.byteLength;
173
+ // Shrink by whole 16-byte steps, so every later range keeps its alignment; the tail of the last step is zeros.
174
+ const removed = Math.floor((view.byteLength - next.length) / 16) * 16, padded = new Uint8Array(view.byteLength - removed);
175
+ padded.set(next);
176
+ parts.push(glb.bin.subarray(read, at), padded);
177
+ view.byteOffset = at - shift;
178
+ view.byteLength = next.length;
179
+ shift += removed;
180
+ moves.push({at: end, shift});
181
+ read = end;
182
+ }
183
+ parts.push(glb.bin.subarray(read));
184
+ const shiftAt = (offset: number): number => moves.reduce((total, move) => (offset >= move.at ? move.shift : total), 0);
185
+ views.forEach((view, index) => {
186
+ if (view.buffer === 0 && !cuts.has(index)) view.byteOffset = (view.byteOffset ?? 0) - shiftAt(view.byteOffset ?? 0);
187
+ const meshopt = view.extensions?.EXT_meshopt_compression;
188
+ if (meshopt?.buffer === 0) meshopt.byteOffset = (meshopt.byteOffset ?? 0) - shiftAt(meshopt.byteOffset ?? 0);
189
+ });
190
+ const bin = new Uint8Array(parts.reduce((total, part) => total + part.length, 0));
191
+ let at = 0;
192
+ for (const part of parts) { bin.set(part, at); at += part.length; }
193
+ const buffer = glb.json.buffers?.[0];
194
+ if (buffer !== undefined) buffer.byteLength = bin.length;
195
+ return writeGlb(glb.json, bin);
196
+ }
197
+
198
+ /** A GLB at one tier: each embedded KTX2 image at most `maxSize` on its largest side. */
199
+ export function tierGlb(bytes: Uint8Array, maxSize: number): Uint8Array {
200
+ return withImages(bytes, image => (image.mimeType === 'image/ktx2' ? trimKtx2(image.bytes, maxSize) : image.bytes));
201
+ }
202
+
203
+ /** GPU memory a KTX2 takes once transcoded, mips included: ASTC 4×4 or BC7 for UASTC (1 byte a texel), its own block
204
+ * size for raw ASTC, ETC2/BC1 for ETC1S (half a byte). An estimate for reports, never a gate's only input. */
205
+ export function ktx2GpuBytes(bytes: Uint8Array): number {
206
+ const info = ktx2Info(bytes);
207
+ const block = ASTC_BLOCKS[info.vkFormat];
208
+ let total = 0;
209
+ for (let level = 0; level < Math.max(1, info.levels); level++) {
210
+ const width = Math.max(1, info.width >> level), height = Math.max(1, info.height >> level);
211
+ total += block === undefined ? Math.ceil(width / 4) * Math.ceil(height / 4) * (info.scheme === BASIS_LZ ? 8 : 16) : Math.ceil(width / block[0]) * Math.ceil(height / block[1]) * 16;
212
+ }
213
+ return total;
214
+ }
215
+
216
+ /** S12b: the largest side a phone draws from one KTX2 table row, or undefined to keep the file as it is. The table's source
217
+ * key is the author's hint (D5): a `#layer` twin (a terrain layer, tiled at a texel a metre) and a horizon strip are S4's
218
+ * 1024 rows, kept on purpose; every other row is 512 on a phone. */
219
+ export const PHONE_TIER_MAX = 512;
220
+ export function phoneTierSize(source: string): number | undefined {
221
+ return source.endsWith('#layer') || /\/horizon\//u.test(source) ? undefined : PHONE_TIER_MAX;
222
+ }
223
+
224
+ /** One phone file the cook made: `from` is the table target it stands in for, `url` its own content-named URL. */
225
+ export interface TierFile {readonly from: string; readonly url: string; readonly bytes: Uint8Array}
226
+ export interface PhoneTier {
227
+ /** The discovery document with each trimmed row's phone target moved to its phone file; desktop maps keep their bytes. */
228
+ readonly discovery: Uint8Array;
229
+ /** Each table target the phone reads at another size: its phone files (one a size, nearly always one). */
230
+ readonly files: ReadonlyMap<string, readonly TierFile[]>;
231
+ /** Table targets a phone row still names as they are (kept by size, unchanged, or not this shard's file), so a phone list keeps them. */
232
+ readonly kept: ReadonlySet<string>;
233
+ }
234
+
235
+ type Json = null | boolean | number | string | readonly Json[] | {readonly [key: string]: Json};
236
+ const isMap = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value);
237
+ const hex8 = (bytes: Uint8Array): string => createHash('sha256').update(bytes).digest('hex').slice(0, 8);
238
+
239
+ /** The phone tier of a shard's KTX2 table (`discovery.json` `native.ktx2`, any shape: `pageModeKtx2` args, `GPU_FILES`, a bare
240
+ * tier map). Every `phone` map's GLB and KTX2 target that `read` can read is trimmed to its row's size; a target that gets
241
+ * smaller moves to `<name>.t<size>-<hash8>.<ext>` beside it. Undefined when nothing gets smaller, so the cook keeps every
242
+ * byte of today's product. */
243
+ export function phoneTier(discovery: Uint8Array, read: (url: string) => Uint8Array | undefined, size: (source: string) => number | undefined = phoneTierSize): PhoneTier | undefined {
244
+ const document = JSON.parse(new TextDecoder().decode(discovery)) as Json;
245
+ const native = isMap(document) && isMap(document['native']) ? document['native'] : undefined;
246
+ if (native?.['ktx2'] === undefined) return undefined;
247
+ const phoneMaps: Record<string, unknown>[] = [];
248
+ const walk = (value: unknown): void => {
249
+ if (Array.isArray(value)) { for (const item of value) walk(item); return; }
250
+ if (!isMap(value)) return;
251
+ for (const [key, item] of Object.entries(value)) {
252
+ if (key === 'phone' && isMap(item) && Object.values(item).every(target => typeof target === 'string')) phoneMaps.push(item);
253
+ else walk(item);
254
+ }
255
+ };
256
+ walk(native['ktx2']);
257
+ const made = new Map<string, TierFile | null>(), files = new Map<string, TierFile[]>(), kept = new Set<string>();
258
+ for (const map of phoneMaps) for (const [source, target] of Object.entries(map)) {
259
+ if (typeof target !== 'string') continue;
260
+ const max = size(source), match = /^(.*?)(?:-[a-f0-9]{8})?\.(glb|ktx2)$/u.exec(target), bytes = max === undefined || match === null ? undefined : read(target);
261
+ if (max === undefined || match === null || bytes === undefined) { kept.add(target); continue; }
262
+ const key = `${target}@${max}`;
263
+ let file = made.get(key);
264
+ if (file === undefined) {
265
+ // A file the trim can't read keeps today's bytes: one odd texture never stops a cook.
266
+ let trimmed = bytes;
267
+ try { trimmed = match[2] === 'glb' ? tierGlb(bytes, max) : trimKtx2(bytes, max); } catch { trimmed = bytes; }
268
+ file = trimmed.length < bytes.length ? {from: target, url: `${match[1] ?? ''}.t${max}-${hex8(trimmed)}.${match[2] ?? ''}`, bytes: trimmed} : null;
269
+ made.set(key, file);
270
+ if (file !== null) files.set(target, [...files.get(target) ?? [], file]);
271
+ }
272
+ if (file === null) kept.add(target); else map[source] = file.url;
273
+ }
274
+ if (files.size === 0) return undefined;
275
+ return {discovery: new TextEncoder().encode(JSON.stringify(document)), files, kept};
276
+ }
@@ -73,22 +73,26 @@ async function snapshot(page: Page): Promise<Snapshot> {
73
73
  if (probe === undefined) {
74
74
  throw new Error('no game probe')
75
75
  }
76
- const state = probe.state()
76
+ const area = probe.area()
77
77
  return {
78
- pos: state.player.pos,
78
+ pos: probe.state().player.pos,
79
79
  door: globalThis.__workerClientRow?.('blender.door.open'),
80
- solid:
81
- probe.shard.shardfile
82
- ?.targets()
83
- .some((target): boolean => target.solid) ?? false,
84
- flags: state.quest.adventure?.quest ?? [],
80
+ solid: area?.targets.some((target): boolean => target.solid) ?? false,
81
+ flags: area?.flags ?? [],
85
82
  }
86
83
  })
87
84
  }
88
85
 
89
- /** Title, Shard Select, Blender Template, Enter: the way a player starts (J233). */
86
+ /** Title, Shard Select, Blender Template, Enter: the way a player starts (J233). A preview's Shard Select enters the grid
87
+ * (J311), so the page is `platform.grid` and the probe's area is the cell the player is in (T2E-1b). */
90
88
  async function enter(page: Page): Promise<void> {
91
89
  await page.locator('.ws-main-select').waitFor()
90
+ // A reload in play lands on the title under the drop card ("Unexpectedly back at the title", dropModal.ts): a player
91
+ // dismisses it, as here
92
+ const dismiss = page.locator('#wsdrop .dismiss')
93
+ if ((await dismiss.count()) > 0) {
94
+ await dismiss.click()
95
+ }
92
96
  // Blender Template is a Developer shard. The switch is remembered across a reload.
93
97
  const developer = page.locator('.ws-main-dev')
94
98
  if ((await developer.getAttribute('aria-checked')) !== 'true') {
@@ -119,7 +123,9 @@ async function enter(page: Page): Promise<void> {
119
123
  await page.waitForFunction((): boolean => {
120
124
  const probe = globalThis.__wildshard
121
125
  return (
122
- probe?.shard.slug === 'blender-template' &&
126
+ probe?.area()?.slug === 'blender-template' &&
127
+ // The cell's door panels are drawn: its targets are what steps 2, 3 and 7 read
128
+ (probe.area()?.targets.length ?? 0) > 0 &&
123
129
  probe.app.state === 'play' &&
124
130
  (probe.world?.game.frameCount ?? 0) > 5
125
131
  )
@@ -160,9 +166,9 @@ async function doorOpens(page: Page): Promise<void> {
160
166
  await page.waitForFunction(
161
167
  (): boolean =>
162
168
  globalThis.__workerClientRow?.('blender.door.open') === 1 &&
163
- globalThis.__wildshard?.shard.shardfile
164
- ?.targets()
165
- .every((target): boolean => !target.solid) === true,
169
+ globalThis.__wildshard
170
+ ?.area()
171
+ ?.targets.every((target): boolean => !target.solid) === true,
166
172
  undefined,
167
173
  STEP,
168
174
  )
@@ -172,8 +178,7 @@ async function doorOpens(page: Page): Promise<void> {
172
178
  async function earned(page: Page, flag: string): Promise<void> {
173
179
  await page.waitForFunction(
174
180
  (name): boolean =>
175
- globalThis.__wildshard?.state().quest.adventure?.quest?.includes(name) ===
176
- true,
181
+ globalThis.__wildshard?.area()?.flags.includes(name) === true,
177
182
  flag,
178
183
  STEP,
179
184
  )
@@ -5,19 +5,20 @@
5
5
  type SmokeVec3 = Readonly<Record<'x' | 'y' | 'z', number>>
6
6
 
7
7
  interface SmokeProbe {
8
- readonly shard: {
8
+ /** The area the player is in: on a grid page the current cell (T2E-1b), null between cells. */
9
+ readonly area: () => {
9
10
  readonly slug: string
10
- readonly shardfile?: {
11
- readonly targets: () => readonly { readonly solid: boolean }[]
12
- }
13
- }
11
+ readonly instance: string | null
12
+ readonly targets: readonly {
13
+ readonly panel: string
14
+ readonly solid: boolean
15
+ }[]
16
+ readonly flags: readonly string[]
17
+ } | null
14
18
  readonly app: { readonly state: string }
15
19
  readonly world?: { readonly game: { readonly frameCount: number } }
16
20
  readonly state: () => {
17
21
  readonly player: { readonly pos: SmokeVec3; readonly health: number }
18
- readonly quest: {
19
- readonly adventure?: { readonly quest?: readonly string[] }
20
- }
21
22
  }
22
23
  }
23
24
 
@@ -115,16 +115,17 @@ test('Template 2: title, walk, door, guardian, reload', async ({
115
115
  await hideAndSave(page)
116
116
  await page.reload()
117
117
  await enter(page)
118
+ // The server's flags reach the page with its first frames
119
+ await Promise.all(
120
+ ['blender.door', 'blender.guardian', 'blender.complete'].map(
121
+ async (flag): Promise<void> => {
122
+ await earned(page, flag)
123
+ },
124
+ ),
125
+ )
118
126
  const back = await snapshot(page)
119
127
  expect(back.door).toBe(1)
120
128
  expect(back.solid).toBe(false)
121
- expect(back.flags).toEqual(
122
- expect.arrayContaining([
123
- 'blender.door',
124
- 'blender.guardian',
125
- 'blender.complete',
126
- ]),
127
- )
128
129
  lap('reload')
129
130
  })
130
131
 
@@ -2,30 +2,14 @@
2
2
  // writes this file, signed with its own placement key; the release key stays engine CI's, because it vouches for the server bundle the box
3
3
  // runs. A placement can only add a cell on an empty spot, for a shard whose server pack this build's book already holds: it never moves the
4
4
  // build, the bundle or the save epoch, and a new serial here starts no grace, so players in every other cell stay connected.
5
- import * as v from 'valibot';
6
5
  import { Scope } from '@wildshard/engine/app/scope';
7
6
  import { fetchSignedText, readSigned } from './releaseFile';
7
+ import { PlacementSchema, type PlacementFile } from '@wildshard/engine/game/grid/placementRows';
8
8
  import type { PlacedCell } from './gridCells';
9
9
 
10
10
  /** How often the server looks, in ms: the release file's minute, so a placement is open inside SA33's two minutes. */
11
11
  const POLL_MS = 60_000;
12
12
  const WHAT = 'placement file';
13
- const id = v.pipe(v.string(), v.regex(/^_?[a-z0-9]+(?:-[a-z0-9]+)*$/u), v.maxLength(64));
14
- const spot = v.pipe(v.number(), v.integer());
15
-
16
- /** What a placement file says: every cell the API placed on one channel, in its grid's order. */
17
- export interface PlacementFile {
18
- /** Which kind of signed file this is, so a body can never be read as another kind's. */
19
- readonly kind: 'placement';
20
- readonly channel: 'production' | 'staging';
21
- /** Rises with every write; an older file is never believed (J179). */
22
- readonly serial: number;
23
- readonly cells: readonly PlacedCell[];
24
- }
25
- /** A placement file's body, once its signature has held. The spot's bounds are `placeCell`'s. */
26
- export const PlacementSchema: v.GenericSchema<PlacementFile> = v.object({ kind: v.literal('placement'), channel: v.picklist(['production', 'staging']), serial: v.pipe(v.number(), v.integer(), v.minValue(0)),
27
- cells: v.pipe(v.array(v.object({ instance: id, slug: id, cell: v.tuple([spot, spot]) })), v.maxLength(49)) });
28
-
29
13
  /** The file's text as what it says, under the placement key (SPKI DER in hex). */
30
14
  export function readPlacementFile(text: string, publicKey: string): PlacementFile { return readSigned(text, publicKey, PlacementSchema, WHAT); }
31
15
  /** The placement file at `url`, an `https:` bucket or a `file:` path. */
@@ -1,7 +1,7 @@
1
1
  import { Scope } from '@wildshard/engine/app/scope';
2
2
  import { diagnosticNow } from '@wildshard/engine/core/clock';
3
3
  import { CLIENT_FLOOD_DEBT, CLIENT_MESSAGES_PER_TICK, MAX_CLIENT_MESSAGES, MAX_CLIENT_MESSAGE_BYTES } from '@wildshard/engine/net/room';
4
- import { channelPrefix, closeCode, closeReason, controlPrefix, decodePing, encodePong, FRAME_SKIP_BYTES, framed, PING_FLOOR_MS, SILENCE_MS, SOCKET_PING, SOCKET_PLAYTEST, SOCKET_READY, SOCKET_RELIABLE, SOCKET_UNRELIABLE, SOCKET_VOICE, STALL_CLOSE_BYTES, type SocketControl } from '@wildshard/engine/net/socketFrames';
4
+ import { channelPrefix, closeCode, closeReason, controlPrefix, decodePing, encodePong, FRAME_SKIP_BYTES, framed, PING_FLOOR_MS, SILENCE_MS, SOCKET_DEVICE, SOCKET_PING, SOCKET_PLAYTEST, SOCKET_READY, SOCKET_RELIABLE, SOCKET_UNRELIABLE, SOCKET_VOICE, STALL_CLOSE_BYTES, type SocketControl } from '@wildshard/engine/net/socketFrames';
5
5
  import type { NetChannel, Transport, TransportStats } from '@wildshard/engine/net/transport';
6
6
  import type { RawData, ServerOptions, WebSocket } from 'ws';
7
7
 
@@ -105,6 +105,7 @@ export class SocketTransport implements Transport {
105
105
  else if (prefix === SOCKET_READY) for (const listener of this.controls) listener('ready', body);
106
106
  else if (prefix === SOCKET_PLAYTEST) for (const listener of this.controls) listener('playtest', body);
107
107
  else if (prefix === SOCKET_VOICE) for (const listener of this.controls) listener('voice', body);
108
+ else if (prefix === SOCKET_DEVICE) for (const listener of this.controls) listener('device', body);
108
109
  else this.close('refused');
109
110
  }
110
111
  private ping(bytes: Buffer): void {
@@ -16,6 +16,7 @@ import { NetIdentitySchema } from '@wildshard/engine/net/identity';
16
16
  import { decodeFirstMessage } from '@wildshard/wire/handshake';
17
17
  import { WIRE_MINOR } from '@wildshard/wire/major';
18
18
  import { encodeCountdown, type WorldWord } from '@wildshard/engine/net/socketFrames';
19
+ import { deviceLine, readDeviceWord, type DeviceSnapshot, type DeviceWho } from '@wildshard/engine/net/deviceWord';
19
20
  import {CorrectionSchema} from '@wildshard/engine/net/measurements';
20
21
  import type {PlaytestRow} from './playtest';
21
22
  import type { ChatReport } from '@wildshard/engine/net/chat';
@@ -145,6 +146,8 @@ export class World {
145
146
  private readonly ends = new Set<SocketTransport>();
146
147
  /** Links whose player quit (`ready` with `LEAVE_FLAG`) before a body was theirs. */
147
148
  private readonly quits = new WeakSet<SocketTransport>();
149
+ /** DEVICE-TELEMETRY DT4: each socket's device word and the player its hello named, until the one `client.device` line is written. */
150
+ private readonly devices = new WeakMap<SocketTransport, { said?: DeviceSnapshot; who?: DeviceWho; done: boolean }>();
148
151
  /** Clients loading: each holds a seat, and has no body yet. */
149
152
  private readonly pending = new Map<string, PendingSeat>();
150
153
  /** Who has a body, by link or by hold, and in which cell. */
@@ -324,11 +327,35 @@ export class World {
324
327
  // GO4: the door's first word, before any hello: how many cells it hosts, and (opus-route2) the shard each runs, so a one-shard
325
328
  // page joins only a world that opens its shard. D7 keeps `/health` private on the rooms address, and a
326
329
  // grid page asks this on a socket of its own (`worldWord`). No build, serial or player data.
327
- end.control('world', new TextEncoder().encode(JSON.stringify({ cells: this.links.size, shards: [...new Set([...this.links.keys()].map(cellShard))] } satisfies WorldWord)).buffer);
330
+ // DEVICE-TELEMETRY DT3: `device: 1` says this door reads the client's device word; a client sends it only then.
331
+ end.control('world', new TextEncoder().encode(JSON.stringify({ cells: this.links.size, shards: [...new Set([...this.links.keys()].map(cellShard))], device: 1 } satisfies WorldWord)).buffer);
332
+ end.onControl((control, bytes) => { if (control === 'device') this.deviceWord(end, bytes); });
328
333
  const timer = this.scope.timeout(this.options.helloMs ?? HELLO_MS, () => { this.refuse('hello-timeout'); end.close('refused'); });
329
334
  const off = end.onMessage((_channel, bytes) => { off(); this.scope.cancelTimer(timer); void this.joining(end, bytes, goto); });
330
335
  end.onClose(() => { off(); this.scope.cancelTimer(timer); });
331
336
  }
337
+ /** DT4: a client's device word, untrusted: one per socket, read and bounded by `readDeviceWord`. A junk or oversized word is counted
338
+ * and dropped, and the connect goes on. */
339
+ private deviceWord(end: SocketTransport, bytes: ArrayBuffer): void {
340
+ const seen = this.devices.get(end) ?? { done: false };
341
+ if (seen.said !== undefined || seen.done) return;
342
+ const said = readDeviceWord(bytes);
343
+ if (said === null) { this.refuse('device-word'); this.options.log({ at: 'client.device-refused', bytes: bytes.byteLength }); seen.done = true; this.devices.set(end, seen); return; }
344
+ seen.said = said; this.devices.set(end, seen); this.deviceSeen(end);
345
+ }
346
+ /** DT4: the hello named this socket's player. */
347
+ private devicePlayer(end: SocketTransport, who: DeviceWho): void {
348
+ const seen = this.devices.get(end) ?? { done: false };
349
+ seen.who ??= who; this.devices.set(end, seen); this.deviceSeen(end);
350
+ }
351
+ /** DT4: one `client.device` journal line per connect, once both the word and the hello's player are in. Privacy: the player id the
352
+ * hello already carries and device characteristics only; never the address, which the door keeps for its limits alone. */
353
+ private deviceSeen(end: SocketTransport): void {
354
+ const seen = this.devices.get(end);
355
+ if (seen === undefined || seen.done || seen.said === undefined || seen.who === undefined) return;
356
+ seen.done = true;
357
+ this.options.log(deviceLine(seen.who, seen.said));
358
+ }
332
359
  private async joining(end: SocketTransport, bytes: ArrayBuffer, goto: string | null): Promise<void> {
333
360
  try { await this.hello(end, bytes, goto); }
334
361
  catch (error) { if (!end.isOpen) return; this.options.log({ at: 'world.join', error: String(error) }); this.refuse(error instanceof SaveNewerError ? 'save-newer' : 'join-failed'); end.close(error instanceof SaveQuarantinedError ? 'refused' : 'retry'); }
@@ -368,6 +395,8 @@ export class World {
368
395
  const said = v.safeParse(NetIdentitySchema, { player: hello.player, name: hello.name });
369
396
  if (!said.success) { this.refuse('identity'); end.close('refused'); return; }
370
397
  const device = said.output.player, typed = cleanName(said.output.name), body = bodyId(this.options.secret, device), { saves } = this.options, holding = this.seated.get(device);
398
+ // DT4: the Name everyone sees and the secret-keyed body id; never the raw player id, which is a bearer credential
399
+ this.devicePlayer(end, { name: this.names.get(device) ?? typed, body });
371
400
  // GC15: a seat mid-crossing is in neither cell for the door to ask: the client dials again in a moment.
372
401
  if (this.crossings.has(device)) { end.close('retry'); return; }
373
402
  if (holding !== undefined) {