@wildshard/engine 0.1.17 → 0.1.19

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 (343) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/budgets/shard-size-baseline.json +10 -4
  3. package/index.html +6 -4
  4. package/lint/engine-sides.json +21 -0
  5. package/lint/host-callback-syntax.mjs +2 -1
  6. package/lint/host-callbacks.mjs +3 -3
  7. package/lint/host-files.mjs +5 -4
  8. package/lint/no-base64.mjs +3 -1
  9. package/lint/ratchet.json +0 -68
  10. package/lint/ratchet.mjs +1 -4
  11. package/lint/shard-imports.mjs +6 -7
  12. package/lint/shard-source.d.mts +9 -0
  13. package/lint/shard-source.mjs +91 -0
  14. package/lint/shardfile-boundaries.mjs +10 -8
  15. package/lint/sim-closure.mjs +6 -3
  16. package/lint/wildshard-plugin.d.ts +4 -0
  17. package/lint/wildshard-plugin.js +22 -23
  18. package/package.json +25 -16
  19. package/scripts/agent-sync.mjs +27 -8
  20. package/scripts/app-build/appCook.ts +8 -2
  21. package/scripts/app-build/catalogue.ts +10 -7
  22. package/scripts/app-build/gen.ts +22 -2
  23. package/scripts/app-build/isolation.d.mts +10 -0
  24. package/scripts/app-build/isolation.mjs +47 -0
  25. package/scripts/app-build/outputProducts.d.mts +3 -0
  26. package/scripts/app-build/outputProducts.mjs +35 -0
  27. package/scripts/app-build/previousDeploy.d.mts +2 -0
  28. package/scripts/app-build/previousDeploy.mjs +10 -1
  29. package/scripts/app-build/serviceWorker.ts +10 -3
  30. package/scripts/app-build/splitOutput.mjs +4 -2
  31. package/scripts/app-build/standaloneOutput.d.mts +1 -1
  32. package/scripts/app-build/standaloneOutput.mjs +5 -3
  33. package/scripts/app-build/titlePreloads.d.mts +8 -0
  34. package/scripts/app-build/titlePreloads.mjs +69 -0
  35. package/scripts/app-build/worldMapAtlas.ts +10 -6
  36. package/scripts/app-build-pipeline.mjs +34 -3
  37. package/scripts/built-product.mjs +1 -1
  38. package/scripts/cell-mem-bench.mjs +81 -23
  39. package/scripts/check-custom-sections-zero.mjs +1 -1
  40. package/scripts/check-devserver.mjs +3 -1
  41. package/scripts/check-models.mjs +2 -0
  42. package/scripts/check-paths.allow.json +0 -115
  43. package/scripts/check-paths.mjs +2 -2
  44. package/scripts/check-platform-ratchets.mjs +2 -3
  45. package/scripts/chromium-keychain-check.mjs +4 -2
  46. package/scripts/chromium-keychain.d.mts +1 -1
  47. package/scripts/chromium-keychain.mjs +3 -1
  48. package/scripts/ci-static.sh +1 -1
  49. package/scripts/committed-fast-checks.mjs +0 -2
  50. package/scripts/content-sw-proof.mjs +2 -1
  51. package/scripts/cook-worker.mjs +9 -3
  52. package/scripts/cook.mjs +20 -3
  53. package/scripts/devices.d.mts +4 -0
  54. package/scripts/devices.mjs +39 -0
  55. package/scripts/devserver-client.d.mts +4 -2
  56. package/scripts/devserver-client.mjs +25 -8
  57. package/scripts/docs-inputs-proof.mjs +6 -6
  58. package/scripts/doctor-proof.mjs +1 -1
  59. package/scripts/e2e-faults.mjs +159 -0
  60. package/scripts/e7-voice-staging-proof.mjs +1 -1
  61. package/scripts/gate-related-tests.mjs +1 -1
  62. package/scripts/git-source-view.mjs +1 -1
  63. package/scripts/goldens/capture.mjs +17 -5
  64. package/scripts/grid-crossing-nightly.mjs +59 -0
  65. package/scripts/grid-crossing-qa.mjs +44 -0
  66. package/scripts/grid-crossing-ratchet.d.mts +6 -0
  67. package/scripts/grid-crossing-ratchet.mjs +52 -0
  68. package/scripts/grid-crossing-report.mjs +122 -0
  69. package/scripts/grid-crossing-stages.mjs +47 -0
  70. package/scripts/grid-crossing-tour.mjs +277 -0
  71. package/scripts/grid-crossing-walk-proof.mjs +136 -18
  72. package/scripts/host-ruler/roots.mjs +2 -2
  73. package/scripts/host-ruler.d.mts +91 -0
  74. package/scripts/host-ruler.mjs +565 -0
  75. package/scripts/live-prepush.py +3 -5
  76. package/scripts/loading-flow/benchmax.mjs +3 -1
  77. package/scripts/look-loop/test_look_loop.py +1 -38
  78. package/scripts/memory-proof.mjs +213 -0
  79. package/scripts/mmo-bots.mjs +8 -2
  80. package/scripts/net-lag-proof.mjs +219 -0
  81. package/scripts/night-market-kit-budget.mjs +2 -2
  82. package/scripts/offline-proof-rules.d.mts +5 -3
  83. package/scripts/offline-proof-rules.mjs +34 -7
  84. package/scripts/offline-proof.mjs +88 -18
  85. package/scripts/{shard-platform-output.mjs → output-reader.mjs} +1 -1
  86. package/scripts/page-release/content.d.mts +9 -1
  87. package/scripts/page-release/content.mjs +131 -17
  88. package/scripts/page-release/flow.mjs +36 -10
  89. package/scripts/page-release/stamp-content.mjs +24 -0
  90. package/scripts/page-release.mjs +28 -6
  91. package/scripts/page-size.mjs +2 -1
  92. package/scripts/pin-packs.ts +1 -1
  93. package/scripts/pins.ts +4 -4
  94. package/scripts/precommit-checks.mjs +1 -7
  95. package/scripts/precommit-jobs.mjs +0 -1
  96. package/scripts/precommit-source.py +1 -9
  97. package/scripts/program-discovery.mjs +1 -1
  98. package/scripts/ring-cook.mjs +2 -1
  99. package/scripts/ring-step.ts +1 -1
  100. package/scripts/script-scenario-metrics.mjs +1 -1
  101. package/scripts/shard-output-content.d.mts +1 -1
  102. package/scripts/shard-output-content.mjs +2 -2
  103. package/scripts/shard-outputs.mjs +1 -1
  104. package/scripts/shard-public.mjs +1 -1
  105. package/scripts/shard-wire-meter.mjs +24 -9
  106. package/scripts/side-parity.mjs +4 -2
  107. package/scripts/standalone-smoke.mjs +8 -3
  108. package/scripts/tier-textures-proof.mjs +101 -0
  109. package/scripts/tier-textures.mjs +78 -0
  110. package/scripts/tile-bake-report.mjs +1 -1
  111. package/scripts/tile-bakes.mjs +1 -1
  112. package/scripts/title-assets.generated.json +2 -0
  113. package/scripts/tsconfig.json +1 -2
  114. package/scripts/vendor/basisu/LICENSE +201 -0
  115. package/scripts/vendor/basisu/basis_encoder.cjs +2 -0
  116. package/scripts/vendor/basisu/basis_encoder.wasm +0 -0
  117. package/scripts/vitest-push-guards.config.ts +1 -1
  118. package/scripts/vps/harden-apply.sh +4 -1
  119. package/scripts/vps/harden-prometheus.sh +176 -0
  120. package/scripts/vps/harden.sh +16 -0
  121. package/scripts/vps/rebuild.sh +18 -0
  122. package/scripts/vps/wildshard-config +84 -24
  123. package/scripts/worker-client-play.mjs +7 -2
  124. package/scripts/worker-client-proof.mjs +1 -1
  125. package/scripts/worker-client-slug.mjs +44 -0
  126. package/scripts/worktree-reap.mjs +5 -5
  127. package/src/.oxlintrc.json +3 -182
  128. package/src/client/app/entry.ts +47 -12
  129. package/src/client/engine/boot/bootCache.ts +2 -2
  130. package/src/client/engine/boot/bootTrace.ts +12 -4
  131. package/src/client/engine/boot/browserChunkRecovery.ts +4 -0
  132. package/src/client/engine/boot/clearDownloads.ts +5 -2
  133. package/src/client/engine/boot/contentRead.ts +11 -0
  134. package/src/client/engine/boot/gpuFiles.ts +4 -2
  135. package/src/client/engine/boot/lastEnd.ts +99 -12
  136. package/src/client/engine/boot/parsedPages.ts +127 -58
  137. package/src/client/engine/boot/texturePack.ts +2 -2
  138. package/src/client/engine/build.d.ts +2 -0
  139. package/src/client/engine/core/Game.ts +18 -0
  140. package/src/client/engine/core/bootstrap.ts +4 -1
  141. package/src/client/engine/debug/probe.ts +18 -1
  142. package/src/client/engine/fx/LightPool.ts +41 -1
  143. package/src/client/engine/net/netPlayer.ts +19 -2
  144. package/src/client/engine/net/otherPlayers.ts +55 -8
  145. package/src/client/engine/net/predictor.ts +33 -9
  146. package/src/client/engine/net/redial.ts +29 -6
  147. package/src/client/engine/pwa/cacheNames.ts +58 -1
  148. package/src/client/engine/pwa/sw.js +53 -14
  149. package/src/client/engine/render/dataLook.ts +152 -40
  150. package/src/client/engine/render/families/detail.ts +89 -0
  151. package/src/client/engine/render/families/pbr.ts +7 -1
  152. package/src/client/engine/render/look.ts +6 -0
  153. package/src/client/engine/render/lookLight.ts +103 -11
  154. package/src/client/engine/render/lookRender.ts +105 -0
  155. package/src/client/engine/render/precompile.ts +20 -14
  156. package/src/client/engine/saves/indexed.ts +2 -2
  157. package/src/client/engine/telemetry/browserErrors.ts +28 -4
  158. package/src/client/engine/telemetry/deviceSnapshot.ts +155 -0
  159. package/src/client/engine/telemetry/staleBuildReport.ts +22 -1
  160. package/src/client/engine/ui/debugOptions.ts +1 -1
  161. package/src/client/engine/ui/lookLightOption.ts +10 -6
  162. package/src/client/engine/ui/namePrompt.ts +7 -0
  163. package/src/client/game/boot/audio.generated.ts +114 -216
  164. package/src/client/game/boot/bytes.generated.ts +118 -238
  165. package/src/client/game/boot/placedSpawn.ts +35 -0
  166. package/src/client/game/boot/spawnDownload.ts +25 -6
  167. package/src/client/game/boot/versions.generated.ts +7 -7
  168. package/src/client/game/devScene/hook.ts +291 -0
  169. package/src/client/game/grid/area.ts +38 -0
  170. package/src/client/game/grid/cellLoading.ts +12 -2
  171. package/src/client/game/grid/live.ts +134 -12
  172. package/src/client/game/grid/liveSession.ts +47 -5
  173. package/src/client/game/grid/mapAtlas.ts +18 -4
  174. package/src/client/game/grid/menu.ts +4 -0
  175. package/src/client/game/grid/minimapBlend.ts +15 -5
  176. package/src/client/game/grid/neighbourPanels.ts +10 -1
  177. package/src/client/game/grid/oneWayIn.ts +5 -1
  178. package/src/client/game/grid/placedCards.ts +55 -0
  179. package/src/client/game/grid/placedCells.ts +233 -0
  180. package/src/client/game/grid/placedContent.ts +85 -0
  181. package/src/client/game/grid/prefetch.ts +9 -1
  182. package/src/client/game/grid/productInputs.ts +3 -1
  183. package/src/client/game/grid/productLeases.ts +4 -1
  184. package/src/client/game/grid/products.ts +8 -4
  185. package/src/client/game/grid/regionalRuntime.ts +6 -0
  186. package/src/client/game/grid/regionalWorld.ts +15 -2
  187. package/src/client/game/grid/roadCull.ts +44 -14
  188. package/src/client/game/grid/roadLook.ts +24 -15
  189. package/src/client/game/grid/roadLookPlatform.ts +4 -2
  190. package/src/client/game/grid/roadResources.ts +24 -0
  191. package/src/client/game/grid/roadSign.worker.ts +4 -3
  192. package/src/client/game/grid/roadSignPaint.ts +16 -5
  193. package/src/client/game/grid/roadSignPreparation.ts +4 -3
  194. package/src/client/game/grid/roadSignScale.ts +8 -0
  195. package/src/client/game/grid/roadTileBacking.ts +13 -3
  196. package/src/client/game/grid/roadTileCapture.worker.ts +3 -1
  197. package/src/client/game/grid/roadTileCaptureWorker.ts +2 -1
  198. package/src/client/game/grid/roadTileWrite.worker.ts +4 -2
  199. package/src/client/game/grid/roadTileWriter.ts +2 -1
  200. package/src/client/game/grid/screenReports.ts +39 -0
  201. package/src/client/game/grid/session.ts +29 -4
  202. package/src/client/game/grid/shardfileFar.ts +9 -3
  203. package/src/client/game/grid/softWallLook.ts +25 -8
  204. package/src/client/game/mainMenu.ts +6 -2
  205. package/src/client/game/plugin/context.ts +2 -1
  206. package/src/client/game/plugin/followedMovers.ts +7 -1
  207. package/src/client/game/quest/figures.ts +2 -0
  208. package/src/client/game/session/finish.ts +11 -1
  209. package/src/client/game/shard/titleList.ts +6 -2
  210. package/src/client/game/shardfile/assetCache.ts +4 -4
  211. package/src/client/game/shardfile/client.ts +4 -1
  212. package/src/client/game/shardfile/clientAssets.ts +9 -5
  213. package/src/client/game/shardfile/clientLibrary.ts +1 -1
  214. package/src/client/game/shardfile/clientWater.ts +51 -9
  215. package/src/client/game/shardfile/clientWorker.ts +21 -8
  216. package/src/client/game/shardfile/look.ts +8 -3
  217. package/src/client/game/shardfile/product.ts +34 -7
  218. package/src/client/game/shardfile/productChecks.ts +2 -2
  219. package/src/client/game/shardfile/productCost.ts +2 -2
  220. package/src/client/game/shardfile/readAsset.ts +2 -2
  221. package/src/client/game/shardfile/reflectingWater.ts +73 -0
  222. package/src/client/game/shardfile/seatGrace.ts +9 -0
  223. package/src/client/game/shardfile/skinPlayback.ts +1 -1
  224. package/src/client/game/systems/npc/figureRows.ts +9 -0
  225. package/src/client/game/titleDeck.ts +4 -3
  226. package/src/client/host/online/doctor.ts +2 -0
  227. package/src/client/host/online/linkScreens.ts +30 -4
  228. package/src/client/host/online/shell.ts +14 -0
  229. package/src/client/host/standalone/shell.ts +4 -0
  230. package/src/cook/game/astc6Encode.ts +55 -0
  231. package/src/cook/game/astc6Wasm.ts +176 -0
  232. package/src/cook/game/cookJob.ts +30 -6
  233. package/src/cook/game/cookWorker.ts +38 -11
  234. package/src/cook/game/entryTotals.ts +51 -24
  235. package/src/cook/game/shardCook.ts +67 -9
  236. package/src/cook/game/spawnParts.ts +33 -8
  237. package/src/cook/game/tierTextures.ts +349 -0
  238. package/src/e2e/smoke/game.ts +19 -14
  239. package/src/e2e/smoke/page-globals.d.ts +9 -8
  240. package/src/e2e/smoke/template2.spec.ts +8 -7
  241. package/src/server/engine/ai/bossContenders.ts +27 -3
  242. package/src/server/engine/ai/phases.ts +1 -1
  243. package/src/server/engine/entities/creatureRefill.ts +9 -2
  244. package/src/server/engine/net/ledgerBook.ts +2 -0
  245. package/src/server/engine/net/room.ts +15 -0
  246. package/src/server/engine/sim/respawn.ts +8 -0
  247. package/src/server/game/boss/arenaRoomEntry.ts +5 -1
  248. package/src/server/game/boss/arenaRoomNative.ts +2 -0
  249. package/src/server/game/boss/native.ts +2 -1
  250. package/src/server/game/boss/nativeWake.ts +9 -5
  251. package/src/server/game/boss/openArenaNative.ts +2 -2
  252. package/src/server/game/boss/openArenaRoom.ts +3 -1
  253. package/src/server/game/grid/room.ts +44 -3
  254. package/src/server/game/grid/simulation.ts +23 -0
  255. package/src/server/game/population/mixedRoster.ts +11 -3
  256. package/src/server/game/population/nativeBake.ts +8 -3
  257. package/src/server/game/population/nativeReturns.ts +6 -3
  258. package/src/server/game/population/roster.ts +5 -3
  259. package/src/server/game/quest/eliteDrops.ts +15 -4
  260. package/src/server/game/quest/nativeChapter.ts +1 -1
  261. package/src/server/game/quest/nativeComposition.ts +5 -1
  262. package/src/server/game/riding/nativeTaming.ts +10 -6
  263. package/src/server/game/riding/wildRefill.ts +3 -2
  264. package/src/server/game/shardfile/bossRow.ts +4 -2
  265. package/src/server/game/shardfile/homeKeeper.ts +4 -2
  266. package/src/server/host/bake/headlessWorker.ts +1 -1
  267. package/src/server/host/dedicated/cellHost.ts +98 -16
  268. package/src/server/host/dedicated/cellProtocol.ts +16 -1
  269. package/src/server/host/dedicated/cellWorker.ts +8 -5
  270. package/src/server/host/dedicated/dedicatedRoom.ts +16 -6
  271. package/src/server/host/dedicated/gridCells.ts +15 -6
  272. package/src/server/host/dedicated/loopLag.ts +21 -0
  273. package/src/server/host/dedicated/main.ts +13 -5
  274. package/src/server/host/dedicated/packReader.ts +3 -3
  275. package/src/server/host/dedicated/placementFile.ts +4 -19
  276. package/src/server/host/dedicated/roadRoom.ts +34 -4
  277. package/src/server/host/dedicated/serverMetrics.ts +126 -0
  278. package/src/server/host/dedicated/socketPump.ts +21 -4
  279. package/src/server/host/dedicated/socketTransport.ts +2 -1
  280. package/src/server/host/dedicated/workerTable.ts +15 -4
  281. package/src/server/host/dedicated/world.ts +95 -8
  282. package/src/server/host/headless.ts +1 -1
  283. package/src/server/host/headlessRuntime.ts +1 -1
  284. package/src/server/host/standalone/roadWorld.ts +15 -3
  285. package/src/server/host/standalone/serverHost.ts +4 -1
  286. package/src/server/host/standalone/serverProduct.ts +41 -11
  287. package/src/shared/engine/ai/BossBrain.ts +8 -0
  288. package/src/shared/engine/net/deviceWord.ts +138 -0
  289. package/src/shared/engine/net/socketFrames.ts +7 -4
  290. package/src/shared/game/boss/wake.ts +6 -0
  291. package/src/shared/game/devColliders.ts +47 -0
  292. package/src/shared/game/eliteSystem.ts +8 -0
  293. package/src/shared/game/grid/allocator.ts +3 -0
  294. package/src/shared/game/grid/catalogueSlot.ts +7 -0
  295. package/src/shared/game/grid/placementRows.ts +36 -0
  296. package/src/shared/game/plugin/bossRecord.ts +3 -2
  297. package/src/shared/game/shardfile/ktx2Targets.ts +23 -3
  298. package/src/shared/game/shardfile/outputDescriptor.ts +9 -5
  299. package/src/shared/game/shardfile/packReads.ts +28 -3
  300. package/src/shared/game/shardfile/serverPack.ts +9 -0
  301. package/src/shared/game/systems/looks/trampleField.ts +2 -0
  302. package/src/shared/host/formatJsonSchema.ts +2 -2
  303. package/src/tsconfig.json +1 -27
  304. package/lint/host-reviews.json +0 -14
  305. package/lint/listed-view-code.json +0 -13
  306. package/lint/platform-pending-users.json +0 -2520
  307. package/lint/platform-pending-users.md +0 -18
  308. package/scripts/cell-mem/metrics-preload.mjs +0 -65
  309. package/scripts/check-closed-components.d.mts +0 -7
  310. package/scripts/check-closed-components.mjs +0 -87
  311. package/scripts/g571-hosts.d.mts +0 -3
  312. package/scripts/g571-hosts.mjs +0 -54
  313. package/scripts/host-ruler/register-migrate.mjs +0 -242
  314. package/scripts/listed-view-code.d.mts +0 -7
  315. package/scripts/listed-view-code.mjs +0 -93
  316. package/scripts/look-loop/fit-lut.py +0 -142
  317. package/scripts/look-loop/palette-delta.py +0 -183
  318. package/scripts/look-loop/regions/driftwood-isle.json +0 -25
  319. package/scripts/look-loop/regions/far-reach.json +0 -225
  320. package/scripts/look-loop/regions/open-plot-night-market.json +0 -855
  321. package/scripts/look-loop/regions/pine-hollow.json +0 -22
  322. package/scripts/look-loop/regions/sunscar-dunes.json +0 -281
  323. package/scripts/platform-chargebacks.d.mts +0 -9
  324. package/scripts/platform-chargebacks.mjs +0 -135
  325. package/scripts/shard-component-spans.d.mts +0 -15
  326. package/scripts/shard-component-spans.mjs +0 -18
  327. package/scripts/shard-components.d.mts +0 -16
  328. package/scripts/shard-components.mjs +0 -363
  329. package/scripts/shard-platform-hosts.d.mts +0 -2
  330. package/scripts/shard-platform-hosts.mjs +0 -93
  331. package/scripts/shard-platform-sections.d.mts +0 -2
  332. package/scripts/shard-platform-sections.mjs +0 -9
  333. package/scripts/shard-platform.d.mts +0 -76
  334. package/scripts/shard-platform.mjs +0 -1072
  335. package/src/.oxfmtrc.json +0 -48
  336. package/src/scripts/ask-new.sh +0 -29
  337. package/src/scripts/asks.mjs +0 -142
  338. package/src/scripts/herdr-prompt.sh +0 -34
  339. package/src/scripts/herdr-question-open.sh +0 -67
  340. package/src/scripts/md-wrap.mjs +0 -229
  341. /package/scripts/{shard-platform-format.d.mts → node-format.d.mts} +0 -0
  342. /package/scripts/{shard-platform-format.mjs → node-format.mjs} +0 -0
  343. /package/scripts/{shard-platform-output.d.mts → output-reader.d.mts} +0 -0
@@ -23,6 +23,8 @@ import {readTar} from '@wildshard/format/tar';
23
23
  import type {ServerPack, ServerPackPart} from '../../shared/game/shardfile/serverPack';
24
24
  import {serverReadsFile} from '../../shared/game/shardfile/outputDescriptor';
25
25
  import {entryReadSet} from './entryReadSet';
26
+ import {phoneTier, type Astc6Encode} from './tierTextures';
27
+ import {wasmAstc6} from './astc6Wasm';
26
28
  import {createPartEncoder, packable, writeSpawnParts, type CookedMember, type PartEncoder} from './spawnParts';
27
29
 
28
30
  export type CookTier = 'phone' | 'desktop';
@@ -159,12 +161,45 @@ export interface ShardHalf {
159
161
  /** The mounts of one manifest. */
160
162
  readonly mountRows: (tier: CookTier, set: CookSet) => readonly SpawnFile[];
161
163
  readonly read: (url: string) => Uint8Array;
164
+ /** S12d: the phone tier's own files (`phoneTier`), each at its content-named URL; none with the tiers off. */
165
+ readonly tierFiles: readonly SpawnFile[];
166
+ /** The discovery document as this cook serves it (the phone tier's table when the tiers are on), and its row. */
167
+ readonly discovery: {readonly row: SpawnFile; readonly bytes: Uint8Array} | undefined;
162
168
  readonly stableKey: (url: string) => string | undefined;
169
+ /** A mount's bytes as this cook serves it: the discovery document carries the phone tier's table (S12b). */
170
+ readonly mountBytes: (row: CookMount) => Uint8Array;
171
+ }
172
+
173
+ /** S12b: what the cook does with a shard's textures per tier. `phone`: give each phone row of the KTX2 table its own
174
+ * trimmed file (`phoneTier`), listed in the phone manifests in place of the file it stands in for. Off by default until
175
+ * the cook's own 6×6 encode (S12c) can replace the hand-made sets. `encode` (S12c): cook each phone row from its desktop
176
+ * master as ASTC 6×6 (`basisuAstc6`), what the phone reads once the hand-made sets are gone. */
177
+ export interface CookTiers {readonly phone?: boolean; readonly encode?: Astc6Encode
178
+ /** S12d: run the encodes a cook will ask for on worker threads first (`wasmAstc6`'s `record` and `flush`). */
179
+ readonly prepare?: {readonly record: Astc6Encode; readonly flush: () => Promise<void>}}
180
+
181
+ /** S12d: the phone tier is on by default, 6×6 by basisu's WebAssembly build: no child process, nothing to install, so
182
+ * the box's cook job and CI's bake cook what `cook --phone-astc6` did. `cacheDir` keeps encodes between cooks; a cook
183
+ * job passes none (its files can't be trusted by the next job). */
184
+ export async function defaultCookTiers(cacheDir?: string): Promise<CookTiers> {
185
+ const lanes = process.env['COOK_CPU_SLOTS'] === undefined ? undefined : Math.max(1, Number(process.env['COOK_CPU_SLOTS']) - 1);
186
+ const astc6 = await wasmAstc6({...(cacheDir === undefined ? {} : {cacheDir}), ...(lanes === undefined || !Number.isSafeInteger(lanes) ? {} : {lanes})});
187
+ return {phone: true, encode: astc6.encode, prepare: astc6};
188
+ }
189
+
190
+ /** `shardHalf` with its encodes run in parallel first, when the tiers can. */
191
+ export async function preparedShardHalf(shard: OpenShardfile, wireBytes: WireBytes, warn: ((line: string) => void) | undefined, folder: string, tiers: CookTiers): Promise<ShardHalf> {
192
+ if (tiers.phone === true && tiers.prepare !== undefined) {
193
+ // Only the encodes' inputs matter here: no file of this pass is sized or kept.
194
+ shardHalf(shard, () => 0, undefined, folder, {phone: true, encode: tiers.prepare.record});
195
+ await tiers.prepare.flush();
196
+ }
197
+ return shardHalf(shard, wireBytes, warn, folder, tiers);
163
198
  }
164
199
 
165
200
  /** The shard half of every manifest: what the app build's `write` and the standalone cook share. `folder` is where the
166
201
  * product is served; a catalogue names it by slug, and today's `_template` folder is the one that differs (`template`). */
167
- export function shardHalf(shard: OpenShardfile, wireBytes: WireBytes, warn: (line: string) => void = () => undefined, folder: string = shard.slug): ShardHalf {
202
+ export function shardHalf(shard: OpenShardfile, wireBytes: WireBytes, warn: (line: string) => void = () => undefined, folder: string = shard.slug, tiers: CookTiers = {}): ShardHalf {
168
203
  const prefix = `/shardfiles/${folder}/`, {index, source, sections} = shard, named = namedPlaces(sections);
169
204
  // D5-4: a preparation-only preview lists the default set alone (no stored fence), so `wildshard prepare` sees the boot's
170
205
  // own reads beyond it. Listing the whole level there made every recorded fence the whole level (Pine, Nalati).
@@ -187,17 +222,32 @@ export function shardHalf(shard: OpenShardfile, wireBytes: WireBytes, warn: (lin
187
222
  sources.set(headerURL, () => shard.header);
188
223
  const header = {url: headerURL, bytes: shard.header.length, hash: headerHash, wireBytes: wireBytes(`${prefix}shard.json`, shard.header, headerHash)};
189
224
  // C3: a page reads this before it knows the product, and a cold offline entry reads it too.
190
- const document = shard.cook?.mounts.find(row => row.url === `${prefix}discovery.json`), discovery: readonly SpawnFile[] = document === undefined ? [] : [{
191
- url: `${document.url}?v=${document.hash.slice(0, 8)}`, bytes: document.bytes, hash: document.hash, wireBytes: wireBytes(document.url, shard.read(document.path), document.hash)}];
192
- for (const row of discovery) if (document !== undefined) sources.set(row.url, () => shard.read(document.path));
193
- const mounts = downloaded(shard), mountURLs = new Set(mounts.map(row => row.url)), onTier = mountTiers(mountURLs);
225
+ const document = shard.cook?.mounts.find(row => row.url === `${prefix}discovery.json`), mounts = downloaded(shard);
226
+ // S12b: the phone tier's files, made from the KTX2 table's own targets; the discovery document names them.
227
+ const onTier = mountTiers(new Set(mounts.map(row => row.url))), mountByURL = new Map(mounts.map(row => [row.url, row])), mountOf = (url: string): Uint8Array | undefined => { const row = mountByURL.get(url); return row === undefined ? undefined : shard.read(row.path); };
228
+ const phone = tiers.phone === true && document !== undefined ? phoneTier(shard.read(document.path), mountOf, undefined, tiers.encode) : undefined;
229
+ const documentBytes = phone?.discovery ?? (document === undefined ? undefined : shard.read(document.path));
230
+ const discovery: readonly SpawnFile[] = document === undefined || documentBytes === undefined ? [] : [(() => {
231
+ const hash = phone === undefined ? document.hash : sha256(documentBytes);
232
+ return {url: `${document.url}?v=${hash.slice(0, 8)}`, bytes: documentBytes.length, hash, wireBytes: wireBytes(document.url, documentBytes, hash)};
233
+ })()];
234
+ for (const row of discovery) if (documentBytes !== undefined) sources.set(row.url, () => documentBytes);
194
235
  const mountRow = new Map(mounts.map(row => {
195
236
  const url = CONTENT_NAMED.test(row.url) || /-[a-f0-9]{8}\.[a-z0-9]+$/u.test(row.url) ? row.url : `${row.url}?v=${row.hash.slice(0, 8)}`;
196
237
  sources.set(url, () => shard.read(row.path));
197
238
  return [row.url, () => ({url, bytes: row.bytes, hash: row.hash, wireBytes: wireBytes(row.url, shard.read(row.path), row.hash)})] as const;
198
239
  }));
199
- return {shard, header, read: url => { const read = sources.get(url); if (read === undefined) throw new Error(`Spawn file has no source: ${url}`); return read(); },
240
+ const tierRows = new Map([...phone?.files ?? []].map(([from, files]) => [from, files.map(file => {
241
+ const hash = sha256(file.bytes);
242
+ sources.set(file.url, () => file.bytes);
243
+ return {url: file.url, bytes: file.bytes.length, hash, wireBytes: wireBytes(file.url, file.bytes, hash)};
244
+ })]));
245
+ /** A phone list names each trimmed target's phone files in its place, and the target too while a kept row still reads it. */
246
+ const tiered = (tier: CookTier, url: string): readonly string[] => tier !== 'phone' || !tierRows.has(url) ? [url] : phone?.kept.has(url) === true ? [url, `tier:${url}`] : [`tier:${url}`];
247
+ return {shard, header, tierFiles: [...tierRows.values()].flat(), discovery: discovery[0] === undefined || documentBytes === undefined ? undefined : {row: discovery[0], bytes: documentBytes},
248
+ read: url => { const read = sources.get(url); if (read === undefined) throw new Error(`Spawn file has no source: ${url}`); return read(); },
200
249
  stableKey: url => stableKeys.get(url),
250
+ mountBytes: row => (row.url === document?.url && documentBytes !== undefined ? documentBytes : shard.read(row.path)),
201
251
  productRows: (tier, set) => {
202
252
  const selected: SpawnFile[] = [header, ...discovery];
203
253
  for (const row of index.sections) selected.push(productFile(row.path, row.bytes, row.hash, `section:${row.name}`));
@@ -208,7 +258,13 @@ export function shardHalf(shard: OpenShardfile, wireBytes: WireBytes, warn: (lin
208
258
  },
209
259
  mountRows: (tier, set) => {
210
260
  const urls = wholeSet(tier, set) ? mounts.map(row => row.url).filter(url => onTier(url, tier)) : fence.mounts;
211
- return urls.flatMap(url => { const row = mountRow.get(url); return row === undefined ? [] : [row()]; });
261
+ // A phone file whose target no phone list names (a fence shard's file read past its fence) rides in the phone's whole
262
+ // set: no other tier's part holds it, and it costs less than the target a phone read from a desktop part before.
263
+ const listed = new Set(urls), unlisted = tier === 'phone' && set === 'whole' ? [...tierRows.keys()].filter(url => !listed.has(url)).map(url => `tier:${url}`) : [];
264
+ return [...urls.flatMap(url => tiered(tier, url)), ...unlisted].flatMap(url => {
265
+ if (url.startsWith('tier:')) return tierRows.get(url.slice(5)) ?? [];
266
+ const row = mountRow.get(url); return row === undefined ? [] : [row()];
267
+ });
212
268
  }};
213
269
  }
214
270
 
@@ -300,6 +356,8 @@ export interface CookShardOptions {
300
356
  readonly warn?: (line: string) => void;
301
357
  /** The renderer fingerprint to cook for; this engine's own unless a build names it. */
302
358
  readonly renderer?: string;
359
+ /** S12b: per-tier texture files; `defaultCookTiers()` (the phone tier, 6×6) unless a cook names its own. `{}` turns them off. */
360
+ readonly tiers?: CookTiers;
303
361
  }
304
362
 
305
363
  /** `cook <shardfile> → <packs>`: the shard's half of every manifest, its parts, and its loose mounts at their URLs. */
@@ -313,7 +371,7 @@ export async function cookShard(tarball: Uint8Array, out: string, options: CookS
313
371
  async function cookWith(tarball: Uint8Array, out: string, options: CookShardOptions, encoder: PartEncoder): Promise<CookReport> {
314
372
  const started = options.now(), shard = openShardfile(tarball, options.renderer === undefined ? {} : {renderer: options.renderer});
315
373
  if (shard.preparation === 'stale') options.warn?.(stalePreparationLine(shard.slug));
316
- const half = shardHalf(shard, options.wireBytes, options.warn);
374
+ const half = await preparedShardHalf(shard, options.wireBytes, options.warn, shard.slug, options.tiers ?? await defaultCookTiers());
317
375
  const before = encoder.stats();
318
376
  const table = COOK_TIERS.flatMap(tier => COOK_SETS.map(set => ({tier, set, rows: [...half.productRows(tier, set), ...half.mountRows(tier, set)]})));
319
377
  const whole = table.filter(row => row.set === 'whole');
@@ -337,7 +395,7 @@ async function cookWith(tarball: Uint8Array, out: string, options: CookShardOpti
337
395
  const loose = (half.shard.cook?.mounts ?? []).filter(row => !packable(row.url) || !row.url.startsWith('/assets/'));
338
396
  for (const row of loose) {
339
397
  const file = join(out, row.url.slice(1));
340
- mkdirSync(dirname(file), {recursive: true}); writeFileSync(file, half.shard.read(row.path));
398
+ mkdirSync(dirname(file), {recursive: true}); writeFileSync(file, half.mountBytes(row));
341
399
  }
342
400
  // C3: a title or menu `<img>` reads a shard's images by the URLs its level manifest names, before any fetch layer exists.
343
401
  const images = half.shard.index.files.flatMap(row => row.name?.startsWith('ui/images/') === true ? [row.path] : []);
@@ -25,6 +25,21 @@ import {SPAWN_PART_ALIGN, spawnCodeListPath, splitSpawnManifest, type SpawnCodeA
25
25
  type SpawnPart} from '../../shared/game/shardfile/spawnManifest';
26
26
 
27
27
  export const PART_BYTES = 12_000_000, PART_TARGET = 10, OVER_FETCH = 0.1;
28
+ /** A part's size rule: at most `max` raw unless one file alone is bigger, which then stands alone (never past
29
+ * PART_BYTES), and about `target` on average (DL18's key boundaries). */
30
+ export interface PartLimits {readonly max: number; readonly target: number}
31
+ /** A part's average decoded size: a key boundary falls after a member with probability size / PART_TARGET_BYTES (DL18). */
32
+ export const PART_TARGET_BYTES = 9_000_000;
33
+ /** Files no public (phone) entry reads keep the 12 MB rule: a desktop's whole level (Pine 333 MB) in 4 MiB parts would
34
+ * pass the manifest's 128 parts. */
35
+ export const WHOLE_PART_LIMITS: PartLimits = {max: PART_BYTES, target: PART_TARGET_BYTES};
36
+ /** S12e: what a phone entry reads goes in parts of at most 4 MiB, about 3 MiB on average, the boot pack's own cap
37
+ * (`boot-pack-parts.mjs` PART_MAX). Measured on Pine rev2 with the phone tier on: a one-texture edit re-downloads
38
+ * 3.28 MB of phone parts on average (9.19 MB at 12 MB, 3.77 at 5 MB), for 45 phone parts instead of 17 and the same
39
+ * wire bytes (69.24 MB against 69.20). */
40
+ export const PART_LIMITS: PartLimits = {max: 4 << 20, target: 3 << 20};
41
+ /** Under `readSpawnManifest`'s 128 parts, with room for the key boundaries' spread around their average. */
42
+ const MANIFEST_PARTS = 96;
28
43
  export interface SpawnPartInput {readonly rows: readonly SpawnFile[]; readonly public: boolean; readonly desktop?: boolean}
29
44
  /** `desktop`: a desktop list holds these files too. S3: it never shares a part with a phone's own, or it fetches them all. */
30
45
  interface Group {readonly key: string; readonly members: ReadonlySet<number>; readonly desktop: boolean; readonly urls: string[]; bytes: number}
@@ -91,8 +106,6 @@ export function spawnPartGroups(manifests: readonly SpawnPartInput[]): Group[] {
91
106
  return [...groups.values()];
92
107
  }
93
108
 
94
- /** A part's average decoded size: a key boundary falls after a member with probability size / PART_TARGET_BYTES (DL18). */
95
- export const PART_TARGET_BYTES = 9_000_000;
96
109
  /** DL18 (sp-x2's real pair 5adf2b0 -> fe210797ab: one Nine edit rewrote 41 bodies, and hashed placement scattered them
97
110
  * over 20 of 25 parts). Members are ordered by their readable stable key, so the files one generator writes (keys under
98
111
  * one section path or name prefix) sit together, and a part ends where the keys say: after a member whose key hash falls
@@ -101,7 +114,7 @@ export const PART_TARGET_BYTES = 9_000_000;
101
114
  * cut moves) instead of a scatter. Sections and schemas (`section|` keys) and build assets (`volatile|`) each form their
102
115
  * own run, so a rebuild's restamped files never share a part with content, and setup's first reads never sit behind
103
116
  * the build assets. */
104
- export function placeByKey(urls: readonly string[], size: (url: string) => number, key: (url: string) => string): string[][] {
117
+ export function placeByKey(urls: readonly string[], size: (url: string) => number, key: (url: string) => string, limits: PartLimits = WHOLE_PART_LIMITS): string[][] {
105
118
  const order = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);
106
119
  const sorted = [...urls].sort((a, b) => order(key(a), key(b)) || order(a, b));
107
120
  const parts: string[][] = []; let current: string[] = [], bytes = 0, run = '';
@@ -109,9 +122,9 @@ export function placeByKey(urls: readonly string[], size: (url: string) => numbe
109
122
  for (const url of sorted) {
110
123
  const name = key(url), kind = name.slice(0, name.indexOf('|') + 1);
111
124
  if (kind !== run) { cut(); run = kind; }
112
- if (current.length > 0 && align(bytes) + size(url) > PART_BYTES) cut();
125
+ if (current.length > 0 && align(bytes) + size(url) > limits.max) cut();
113
126
  current.push(url); bytes = align(bytes) + size(url);
114
- if (Number.parseInt(createHash('sha256').update(name).digest('hex').slice(0, 8), 16) / 2 ** 32 < size(url) / PART_TARGET_BYTES) cut();
127
+ if (bytes >= limits.max || Number.parseInt(createHash('sha256').update(name).digest('hex').slice(0, 8), 16) / 2 ** 32 < size(url) / limits.target) cut();
115
128
  }
116
129
  cut();
117
130
  return parts;
@@ -136,7 +149,7 @@ export interface CookedMember {readonly url: string; readonly bytes: number; rea
136
149
  * stay what they were; a cooked file only stops being written twice. */
137
150
  export async function writeSpawnParts(outDir: string, manifests: readonly SpawnPartInput[], read: (url: string) => Uint8Array,
138
151
  stableKey: (url: string) => string | undefined = () => undefined, encoder: PartEncoder = createPartEncoder(),
139
- cooked: ReadonlyMap<string, CookedMember> = new Map()): Promise<SpawnPart[][]> {
152
+ cooked: ReadonlyMap<string, CookedMember> = new Map(), limits: PartLimits = PART_LIMITS): Promise<SpawnPart[][]> {
140
153
  const placed = new Map<string, {url: string; bytes: number; hash: string; offset: number}>(cooked), wire = new Map<string, number>();
141
154
  for (const member of cooked.values()) wire.set(member.url, member.wireBytes);
142
155
  mkdirSync(join(outDir, 'assets/spawn/parts'), {recursive: true});
@@ -149,6 +162,8 @@ export async function writeSpawnParts(outDir: string, manifests: readonly SpawnP
149
162
  return key;
150
163
  };
151
164
  const inflight = new Set<Promise<void>>(), bodies = new Map<string, readonly string[]>();
165
+ // A shard cook's part is a body too: the same bytes under another URL of one manifest must not take its name.
166
+ for (const [member, at] of cooked) bodies.set(at.hash, [...bodies.get(at.hash) ?? [], member]);
152
167
  const write = async (batch: readonly string[]): Promise<void> => {
153
168
  const length = batch.reduce((at, url) => align(at) + (sizes.get(url) ?? 0), 0);
154
169
  let body = new Uint8Array(length); const offsets = new Map<string, number>(); let cursor = 0;
@@ -175,12 +190,22 @@ export async function writeSpawnParts(outDir: string, manifests: readonly SpawnP
175
190
  inflight.add(job); void job.finally(() => { inflight.delete(job); }).catch(() => undefined);
176
191
  };
177
192
  try {
178
- for (const group of spawnPartGroups(manifests)) {
193
+ const groups = spawnPartGroups(manifests);
194
+ // A manifest names at most 128 parts. Estimate each one's count under the phone rule (its share of every group it
195
+ // touches); a shard whose desktop shares most of its files with its phone (no tier table) keeps the 12 MB rule.
196
+ const groupOf = new Map(groups.flatMap(group => group.urls.map(url => [url, group] as const)));
197
+ const estimate = (input: SpawnPartInput): number => {
198
+ const touched = new Set(input.rows.flatMap(row => { const group = groupOf.get(row.url); return group === undefined ? [] : [group]; }));
199
+ return [...touched].reduce((sum, group) => sum + Math.ceil(group.bytes / (group.key.startsWith('P:') ? limits.target : WHOLE_PART_LIMITS.target)), 0);
200
+ };
201
+ const phoneRule = manifests.every(input => estimate(input) <= MANIFEST_PARTS) ? limits : WHOLE_PART_LIMITS;
202
+ for (const group of groups) {
179
203
  for (const url of group.urls) {
180
204
  const bytes = sizes.get(url) ?? 0;
181
205
  if (bytes > PART_BYTES) throw new Error(`Spawn file larger than a part (${bytes} > ${PART_BYTES}): ${url}`);
182
206
  }
183
- for (const batch of placeByKey(group.urls.filter(url => !cooked.has(url)), url => sizes.get(url) ?? 0, keyOf)) await write(batch);
207
+ const rule = group.key.startsWith('P:') ? phoneRule : WHOLE_PART_LIMITS;
208
+ for (const batch of placeByKey(group.urls.filter(url => !cooked.has(url)), url => sizes.get(url) ?? 0, keyOf, rule)) await write(batch);
184
209
  }
185
210
  await Promise.all(inflight);
186
211
  } finally { await encoder.close(); }
@@ -0,0 +1,349 @@
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 GltfTexture {source?: number; extensions?: {KHR_texture_basisu?: {source?: number}}}
99
+ interface Gltf {materials?: {normalTexture?: {index: number}}[]; textures?: GltfTexture[]; accessors?: {bufferView?: number; sparse?: {indices: {bufferView: number}; values: {bufferView: number}}}[]; bufferViews?: GltfView[]; buffers?: {byteLength: number; uri?: string}[]; images?: {bufferView?: number; mimeType?: string; uri?: string}[]}
100
+ interface Glb {readonly json: Gltf; readonly bin: Uint8Array}
101
+
102
+ function readGlb(bytes: Uint8Array): Glb | undefined {
103
+ if (bytes.length < 20) return undefined;
104
+ const view = viewOf(bytes);
105
+ if (view.getUint32(0, true) !== GLB_MAGIC || view.getUint32(16, true) !== JSON_CHUNK) return undefined;
106
+ const jsonLength = view.getUint32(12, true), json = JSON.parse(new TextDecoder().decode(bytes.subarray(20, 20 + jsonLength))) as Gltf;
107
+ const binAt = 20 + jsonLength;
108
+ if (binAt + 8 > bytes.length || view.getUint32(binAt + 4, true) !== BIN_CHUNK) return {json, bin: new Uint8Array()};
109
+ return {json, bin: bytes.subarray(binAt + 8, binAt + 8 + view.getUint32(binAt, true))};
110
+ }
111
+
112
+ function writeGlb(json: Gltf, bin: Uint8Array): Uint8Array {
113
+ const text = new TextEncoder().encode(JSON.stringify(json)), jsonLength = Math.ceil(text.length / 4) * 4, binLength = Math.ceil(bin.length / 4) * 4;
114
+ const out = new Uint8Array(12 + 8 + jsonLength + (binLength > 0 ? 8 + binLength : 0)), view = viewOf(out);
115
+ view.setUint32(0, GLB_MAGIC, true); view.setUint32(4, 2, true); view.setUint32(8, out.length, true);
116
+ view.setUint32(12, jsonLength, true); view.setUint32(16, JSON_CHUNK, true);
117
+ out.fill(0x20, 20, 20 + jsonLength); out.set(text, 20);
118
+ if (binLength > 0) { view.setUint32(20 + jsonLength, binLength, true); view.setUint32(24 + jsonLength, BIN_CHUNK, true); out.set(bin, 28 + jsonLength); }
119
+ return out;
120
+ }
121
+
122
+ export interface GlbImage {readonly index: number; readonly mimeType: string; readonly bytes: Uint8Array}
123
+
124
+ /** Every image a GLB carries in its own buffer, in image order. */
125
+ export function glbImages(bytes: Uint8Array): readonly GlbImage[] {
126
+ const glb = readGlb(bytes);
127
+ if (glb === undefined) return [];
128
+ return (glb.json.images ?? []).flatMap((image, index) => {
129
+ const view = image.bufferView === undefined ? undefined : glb.json.bufferViews?.[image.bufferView];
130
+ if (view?.buffer !== 0) return [];
131
+ const at = view.byteOffset ?? 0;
132
+ return [{index, mimeType: image.mimeType ?? '', bytes: glb.bin.subarray(at, at + view.byteLength)}];
133
+ });
134
+ }
135
+
136
+ /** The GLB with each embedded image swapped for `replace`'s answer (the same image when it answers the same bytes). Every
137
+ * other range of the buffer keeps its bytes and moves down by whole 16-byte steps. Overlapping or shared image views
138
+ * are left alone: the same bytes come back. */
139
+ export function withImages(bytes: Uint8Array, replace: (image: GlbImage) => Uint8Array): Uint8Array {
140
+ const glb = readGlb(bytes);
141
+ if (glb === undefined) return bytes;
142
+ const views = glb.json.bufferViews ?? [];
143
+ const cuts = new Map<number, Uint8Array>();
144
+ for (const image of glbImages(bytes)) {
145
+ const viewIndex = glb.json.images?.[image.index]?.bufferView;
146
+ if (viewIndex === undefined) continue;
147
+ const next = replace(image);
148
+ if (next !== image.bytes && next.length < image.bytes.length) cuts.set(viewIndex, next);
149
+ }
150
+ if (cuts.size === 0) return bytes;
151
+ // A cut view must be used by images alone and overlap nothing else in buffer 0, or moving the rest would tear it.
152
+ const ranges = views.flatMap((view, index) => {
153
+ const own = view.buffer === 0 ? [{index, at: view.byteOffset ?? 0, end: (view.byteOffset ?? 0) + view.byteLength}] : [];
154
+ const meshopt = view.extensions?.EXT_meshopt_compression;
155
+ return meshopt?.buffer === 0 ? [...own, {index: -1, at: meshopt.byteOffset ?? 0, end: (meshopt.byteOffset ?? 0) + meshopt.byteLength}] : own;
156
+ });
157
+ const imageViews = (glb.json.images ?? []).map(image => image.bufferView);
158
+ for (const viewIndex of cuts.keys()) {
159
+ const cut = ranges.find(range => range.index === viewIndex);
160
+ const shared = imageViews.filter(view => view === viewIndex).length > 1
161
+ || (glb.json.accessors ?? []).some(accessor => accessor.bufferView === viewIndex || accessor.sparse?.indices.bufferView === viewIndex || accessor.sparse?.values.bufferView === viewIndex);
162
+ if (cut === undefined || shared || ranges.some(range => range !== cut && range.at < cut.end && cut.at < range.end)) cuts.delete(viewIndex);
163
+ }
164
+ if (cuts.size === 0) return bytes;
165
+
166
+ const order = [...cuts.keys()].sort((a, b) => (views[a]?.byteOffset ?? 0) - (views[b]?.byteOffset ?? 0));
167
+ const parts: Uint8Array[] = [];
168
+ const moves: {at: number; shift: number}[] = [];
169
+ let read = 0, shift = 0;
170
+ for (const viewIndex of order) {
171
+ const view = views[viewIndex], next = cuts.get(viewIndex);
172
+ if (view === undefined || next === undefined) continue;
173
+ const at = view.byteOffset ?? 0, end = at + view.byteLength;
174
+ // Shrink by whole 16-byte steps, so every later range keeps its alignment; the tail of the last step is zeros.
175
+ const removed = Math.floor((view.byteLength - next.length) / 16) * 16, padded = new Uint8Array(view.byteLength - removed);
176
+ padded.set(next);
177
+ parts.push(glb.bin.subarray(read, at), padded);
178
+ view.byteOffset = at - shift;
179
+ view.byteLength = next.length;
180
+ shift += removed;
181
+ moves.push({at: end, shift});
182
+ read = end;
183
+ }
184
+ parts.push(glb.bin.subarray(read));
185
+ const shiftAt = (offset: number): number => moves.reduce((total, move) => (offset >= move.at ? move.shift : total), 0);
186
+ views.forEach((view, index) => {
187
+ if (view.buffer === 0 && !cuts.has(index)) view.byteOffset = (view.byteOffset ?? 0) - shiftAt(view.byteOffset ?? 0);
188
+ const meshopt = view.extensions?.EXT_meshopt_compression;
189
+ if (meshopt?.buffer === 0) meshopt.byteOffset = (meshopt.byteOffset ?? 0) - shiftAt(meshopt.byteOffset ?? 0);
190
+ });
191
+ const bin = new Uint8Array(parts.reduce((total, part) => total + part.length, 0));
192
+ let at = 0;
193
+ for (const part of parts) { bin.set(part, at); at += part.length; }
194
+ const buffer = glb.json.buffers?.[0];
195
+ if (buffer !== undefined) buffer.byteLength = bin.length;
196
+ return writeGlb(glb.json, bin);
197
+ }
198
+
199
+ /** S12c: how a texture's texels read, so a re-encode keeps them: colour (sRGB), linear data, or a normal map. */
200
+ export type TextureRole = 'srgb' | 'linear' | 'normal';
201
+ /** S12c: a UASTC KTX2, already at its tier size, as ASTC 6×6 with the same levels; undefined when it can't. Injected, so
202
+ * this module stays bytes in, bytes out: the cook's is `basisuAstc6` (`astc6Encode.ts`), cached by input hash. */
203
+ export type Astc6Encode = (ktx2: Uint8Array, role: TextureRole) => Uint8Array | undefined;
204
+
205
+ /** The KTX2's DFD transfer function: 2 is sRGB, 1 linear. */
206
+ function transferOf(bytes: Uint8Array): number {
207
+ const view = viewOf(bytes), dfdAt = view.getUint32(48, true), dfdLength = view.getUint32(52, true);
208
+ return dfdLength >= 16 && dfdAt + 15 <= bytes.length ? (bytes[dfdAt + 14] ?? 0) : 0;
209
+ }
210
+
211
+ /** A KTX2 at one tier: trimmed to `maxSize`, then, with `encode`, a UASTC one re-encoded as ASTC 6×6 (S4's phone format,
212
+ * 16/36 of 4×4's GPU bytes). Raw ASTC is only trimmed; BasisLZ (ETC1S) keeps its bytes, as S4's data planes did. */
213
+ export function tierKtx2(bytes: Uint8Array, maxSize: number, encode?: Astc6Encode, normal = false): Uint8Array {
214
+ const trimmed = trimKtx2(bytes, maxSize);
215
+ if (encode === undefined || !isKtx2(trimmed)) return trimmed;
216
+ const info = ktx2Info(trimmed);
217
+ // Only a plain 2D texture the trim brought to its tier size: a one-level master stays as large as it was, and an array or cube
218
+ // map would lose its layers in a 2D encode.
219
+ const view = viewOf(trimmed), plain = view.getUint32(28, true) <= 1 && view.getUint32(32, true) <= 1 && view.getUint32(36, true) === 1;
220
+ if (info.vkFormat !== 0 || info.scheme === BASIS_LZ || !plain || Math.max(info.width, info.height) > maxSize) return trimmed;
221
+ return encode(trimmed, normal ? 'normal' : transferOf(trimmed) === 2 ? 'srgb' : 'linear') ?? trimmed;
222
+ }
223
+
224
+ /** The images a GLB's materials sample as normal maps. */
225
+ function normalImages(json: Gltf): ReadonlySet<number> {
226
+ const out = new Set<number>();
227
+ for (const material of json.materials ?? []) {
228
+ const texture = material.normalTexture === undefined ? undefined : json.textures?.[material.normalTexture.index];
229
+ const source = texture?.source ?? texture?.extensions?.KHR_texture_basisu?.source;
230
+ if (source !== undefined) out.add(source);
231
+ }
232
+ return out;
233
+ }
234
+
235
+ /** A GLB at one tier: each embedded KTX2 image at most `maxSize` on its largest side, and 6×6 with `encode` (`tierKtx2`). */
236
+ export function tierGlb(bytes: Uint8Array, maxSize: number, encode?: Astc6Encode): Uint8Array {
237
+ const normals = encode === undefined ? new Set<number>() : normalImages(readGlb(bytes)?.json ?? {});
238
+ return withImages(bytes, image => (image.mimeType === 'image/ktx2' ? tierKtx2(image.bytes, maxSize, encode, normals.has(image.index)) : image.bytes));
239
+ }
240
+
241
+ /** GPU memory a KTX2 takes once transcoded, mips included: ASTC 4×4 or BC7 for UASTC (1 byte a texel), its own block
242
+ * size for raw ASTC, ETC2/BC1 for ETC1S (half a byte). An estimate for reports, never a gate's only input. */
243
+ export function ktx2GpuBytes(bytes: Uint8Array): number {
244
+ const info = ktx2Info(bytes);
245
+ const block = ASTC_BLOCKS[info.vkFormat];
246
+ let total = 0;
247
+ for (let level = 0; level < Math.max(1, info.levels); level++) {
248
+ const width = Math.max(1, info.width >> level), height = Math.max(1, info.height >> level);
249
+ 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;
250
+ }
251
+ return total;
252
+ }
253
+
254
+ /** 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
255
+ * 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
256
+ * 1024 rows, kept on purpose; every other row is 512 on a phone. */
257
+ export const PHONE_TIER_MAX = 512;
258
+ /** S12d: the cap a kept row (a `#layer` twin, a horizon) is encoded under: none it reaches, so it keeps its own size and only
259
+ * its encoding changes to 6×6, as S4's hand-made set had it. */
260
+ export const KEPT_ENCODE_MAX = 16_384;
261
+ export function phoneTierSize(source: string): number | undefined {
262
+ return source.endsWith('#layer') || /\/horizon\//u.test(source) ? undefined : PHONE_TIER_MAX;
263
+ }
264
+
265
+ /** One phone file the cook made: `from` is the table target it stands in for, `url` its own content-named URL. */
266
+ export interface TierFile {readonly from: string; readonly url: string; readonly bytes: Uint8Array}
267
+ export interface PhoneTier {
268
+ /** The discovery document with each trimmed row's phone target moved to its phone file; desktop maps keep their bytes. */
269
+ readonly discovery: Uint8Array;
270
+ /** Each table target the phone reads at another size: its phone files (one a size, nearly always one). */
271
+ readonly files: ReadonlyMap<string, readonly TierFile[]>;
272
+ /** 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. */
273
+ readonly kept: ReadonlySet<string>;
274
+ }
275
+
276
+ type Json = null | boolean | number | string | readonly Json[] | {readonly [key: string]: Json};
277
+ const isMap = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value);
278
+ const hex8 = (bytes: Uint8Array): string => createHash('sha256').update(bytes).digest('hex').slice(0, 8);
279
+ /** S4's normal-map names (`bake-ktx2.mjs` classOf): a loose KTX2 row the 6×6 encode reads as a normal map. */
280
+ const NORMAL_NAME = /(?:^|[_\-.])(?:nor|nor_gl|normal|nrm)(?:[_\-.]|$)/iu;
281
+
282
+ /** The desktop target that holds a phone row's master: the same source, or its name without the `.phone` / `-phone` mark. */
283
+ export function masterOf(desktop: Readonly<Record<string, unknown>> | undefined, source: string): string | undefined {
284
+ if (desktop === undefined) return undefined;
285
+ for (const key of [source, source.replace(/\.phone\./u, '.'), source.replace(/-phone\./u, '.')]) {
286
+ const target = desktop[key];
287
+ if (typeof target === 'string') return target;
288
+ }
289
+ return undefined;
290
+ }
291
+
292
+ /** The phone tier of a shard's KTX2 table (`discovery.json` `native.ktx2`, any shape: `pageModeKtx2` args, `GPU_FILES`, a bare
293
+ * 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
294
+ * smaller moves to `<name>.t<size>-<hash8>.<ext>` beside it. Undefined when nothing gets smaller, so the cook keeps every
295
+ * byte of today's product.
296
+ *
297
+ * S12c: with `encode`, a row is cooked from its desktop master (`masterOf`, the sibling `desktop` map) when it has one,
298
+ * trimmed and re-encoded as ASTC 6×6 (`tierKtx2`, `tierGlb`): what the phone reads once the hand-made 512 sets are gone
299
+ * (S12d). That file stands in for the row's target whatever its size against today's hand-made file. */
300
+ export function phoneTier(discovery: Uint8Array, read: (url: string) => Uint8Array | undefined, size: (source: string) => number | undefined = phoneTierSize, encode?: Astc6Encode): PhoneTier | undefined {
301
+ const document = JSON.parse(new TextDecoder().decode(discovery)) as Json;
302
+ const native = isMap(document) && isMap(document['native']) ? document['native'] : undefined;
303
+ if (native?.['ktx2'] === undefined) return undefined;
304
+ const phoneMaps: {phone: Record<string, unknown>; desktop: Record<string, unknown> | undefined}[] = [];
305
+ const stringMap = (item: unknown): item is Record<string, unknown> => isMap(item) && Object.values(item).every(target => typeof target === 'string');
306
+ const walk = (value: unknown): void => {
307
+ if (Array.isArray(value)) { for (const item of value) walk(item); return; }
308
+ if (!isMap(value)) return;
309
+ for (const [key, item] of Object.entries(value)) {
310
+ if (key === 'phone' && stringMap(item)) phoneMaps.push({phone: item, desktop: stringMap(value['desktop']) ? value['desktop'] : undefined});
311
+ else walk(item);
312
+ }
313
+ };
314
+ walk(native['ktx2']);
315
+ const made = new Map<string, TierFile | null>(), files = new Map<string, TierFile[]>(), kept = new Set<string>();
316
+ let encodes = 0;
317
+ const counted: Astc6Encode | undefined = encode === undefined ? undefined : (bytes, role) => { const out = encode(bytes, role); if (out !== undefined) encodes++; return out; };
318
+ // A GLB's master is the same source's desktop file only: a `.phone` GLB holds a lighter mesh, not just smaller images.
319
+ const sameMaster = (desktop: Readonly<Record<string, unknown>> | undefined, source: string): string | undefined => { const target = desktop?.[source]; return typeof target === 'string' ? target : undefined; };
320
+ for (const {phone: map, desktop} of phoneMaps) for (const [source, target] of Object.entries(map)) {
321
+ if (typeof target !== 'string') continue;
322
+ // S12d: a row kept at its size is still encoded 6×6 from its own file when the cook encodes, which retires the hand sets.
323
+ const sized = size(source), own = sized === undefined && counted !== undefined, max = sized ?? (own ? KEPT_ENCODE_MAX : undefined);
324
+ const match = /^(.*?)(?:-[a-f0-9]{8})?\.(glb|ktx2)$/u.exec(target), held = max === undefined || match === null ? undefined : read(target);
325
+ // S12d: a shard that dropped its hand-made phone set (Sky Reach) names a target its product no longer carries; the row's own
326
+ // source, the file desktops load, is then its master, and the cook makes the phone's file from it (`phoneRowSources`).
327
+ const absent = held === undefined && match !== null && max !== undefined, bytes = held ?? (absent && source.endsWith(`.${match[2] ?? ''}`) ? read(source) : undefined);
328
+ if (max === undefined || match === null || bytes === undefined) { kept.add(target); continue; }
329
+ const ext = match[2] ?? '', master = encode === undefined || own ? undefined : (ext === 'glb' ? sameMaster(desktop, source) : masterOf(desktop, source));
330
+ const masterBytes = master?.endsWith(`.${ext}`) === true ? read(master) : undefined;
331
+ const key = `${target}<${masterBytes === undefined ? target : master ?? target}@${max}`;
332
+ let file = made.get(key);
333
+ if (file === undefined) {
334
+ // A file the trim can't read keeps today's bytes: one odd texture never stops a cook.
335
+ const normal = NORMAL_NAME.test(source.split('/').pop() ?? '');
336
+ const cook = (input: Uint8Array): Uint8Array => { try { return ext === 'glb' ? tierGlb(input, max, counted) : tierKtx2(input, max, counted, normal); } catch { return input; } };
337
+ // A master stands in only when the 6×6 encode ran on it; else the row's own file is cooked, as S12b does.
338
+ const encodedBefore = encodes;
339
+ const cookedMaster = masterBytes === undefined ? undefined : cook(masterBytes);
340
+ const trimmed = cookedMaster !== undefined && encodes > encodedBefore ? cookedMaster : cook(bytes), fromMaster = trimmed === cookedMaster;
341
+ file = fromMaster || absent || trimmed.length < bytes.length ? {from: target, url: `${match[1] ?? ''}.t${max}-${hex8(trimmed)}.${ext}`, bytes: trimmed} : null;
342
+ made.set(key, file);
343
+ if (file !== null) files.set(target, [...files.get(target) ?? [], file]);
344
+ }
345
+ if (file === null) kept.add(target); else map[source] = file.url;
346
+ }
347
+ if (files.size === 0) return undefined;
348
+ return {discovery: new TextEncoder().encode(JSON.stringify(document)), files, kept};
349
+ }