castle-web-cli 0.4.229 → 0.4.231

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 (259) hide show
  1. package/dist/agent-prompts.d.ts +2 -0
  2. package/dist/agent-prompts.js +16 -8
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +21 -21
  5. package/dist/api.d.ts +28 -2
  6. package/dist/api.js +74 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +860 -3
  9. package/dist/castleJson.d.ts +44 -0
  10. package/dist/castleJson.js +131 -0
  11. package/dist/check.js +27 -1
  12. package/dist/contentPack.d.ts +49 -0
  13. package/dist/contentPack.js +273 -0
  14. package/dist/devExperiments.d.ts +21 -0
  15. package/dist/devExperiments.js +139 -0
  16. package/dist/devHub.d.ts +55 -0
  17. package/dist/devHub.js +187 -0
  18. package/dist/devSaveStorage.d.ts +36 -0
  19. package/dist/devSaveStorage.js +469 -0
  20. package/dist/devSessionChild.js +25 -13
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +237 -46
  23. package/dist/devSessionStorage.d.ts +9 -0
  24. package/dist/devSessionStorage.js +66 -1
  25. package/dist/experimentAssignment.d.ts +15 -0
  26. package/dist/experimentAssignment.js +41 -0
  27. package/dist/importCheckHook.d.ts +40 -0
  28. package/dist/importCheckHook.js +171 -0
  29. package/dist/importRegression.d.ts +15 -0
  30. package/dist/importRegression.js +100 -0
  31. package/dist/importTrust.d.ts +2 -0
  32. package/dist/importTrust.js +27 -0
  33. package/dist/importUpdateHook.d.ts +18 -0
  34. package/dist/importUpdateHook.js +31 -8
  35. package/dist/importUpdateHookRunner.js +19 -7
  36. package/dist/imports.d.ts +16 -0
  37. package/dist/imports.js +141 -31
  38. package/dist/index.js +3 -1
  39. package/dist/init.d.ts +3 -0
  40. package/dist/init.js +126 -9
  41. package/dist/install.js +4 -1
  42. package/dist/localPaths.js +4 -0
  43. package/dist/login.js +2 -2
  44. package/dist/platformDoc.js +3 -8
  45. package/dist/playPagePolicy.js +3 -1
  46. package/dist/remix.js +5 -0
  47. package/dist/routerTaskReport.d.ts +28 -0
  48. package/dist/routerTaskReport.js +56 -0
  49. package/dist/save-deck.d.ts +18 -0
  50. package/dist/save-deck.js +142 -6
  51. package/dist/serve.d.ts +8 -0
  52. package/dist/serve.js +99 -14
  53. package/dist/serverBundle.d.ts +1 -1
  54. package/dist/serverBundle.js +5 -1
  55. package/dist/webSourceSync.d.ts +2 -0
  56. package/dist/webSourceSync.js +2 -0
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +406 -53
  60. package/kits/base/sdk/agent.js +4 -4
  61. package/kits/base/sdk/avatar.d.ts +12 -0
  62. package/kits/base/sdk/avatar.js +23 -0
  63. package/kits/base/sdk/castle.d.ts +9 -0
  64. package/kits/base/sdk/castle.js +5 -0
  65. package/kits/base/sdk/commands.d.ts +223 -0
  66. package/kits/base/sdk/commands.js +16 -0
  67. package/kits/base/sdk/context.d.ts +6 -0
  68. package/kits/base/sdk/experiments.d.ts +15 -0
  69. package/kits/base/sdk/experiments.js +83 -0
  70. package/kits/base/sdk/hub.d.ts +85 -0
  71. package/kits/base/sdk/hub.js +107 -0
  72. package/kits/base/sdk/multiplayer.d.ts +45 -1
  73. package/kits/base/sdk/multiplayer.js +158 -8
  74. package/kits/base/sdk/save.d.ts +197 -0
  75. package/kits/base/sdk/save.js +525 -0
  76. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  77. package/kits/base/sdk/saveCoordinator.js +252 -0
  78. package/kits/base/sdk/server/hub.d.ts +169 -0
  79. package/kits/base/sdk/server/hub.js +125 -0
  80. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  81. package/kits/base/sdk/server/storage.d.ts +58 -4
  82. package/kits/base/sdk/server/storage.js +25 -5
  83. package/kits/base/sdk/server/wrapper.d.ts +36 -2
  84. package/kits/base/sdk/server/wrapper.js +92 -11
  85. package/kits/base/sdk/store.d.ts +3 -2
  86. package/kits/base/sdk/store.js +5 -0
  87. package/kits/base/sdk/transport.d.ts +2 -1
  88. package/kits/base/sdk/transport.js +13 -1
  89. package/kits/hub-game/.prettierrc +5 -0
  90. package/kits/hub-game/CLAUDE.md +149 -0
  91. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  92. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  93. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  94. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  95. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  96. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  97. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  98. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  99. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  100. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  101. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  102. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  103. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  104. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  105. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  106. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  107. package/kits/hub-game/castle.json +96 -0
  108. package/kits/hub-game/code/client/attention.js +133 -0
  109. package/kits/hub-game/code/client/avatar.js +288 -0
  110. package/kits/hub-game/code/client/cabinets.js +90 -0
  111. package/kits/hub-game/code/client/chat.js +135 -0
  112. package/kits/hub-game/code/client/chrome.js +1035 -0
  113. package/kits/hub-game/code/client/doorFrame.js +15 -0
  114. package/kits/hub-game/code/client/firstJoin.js +14 -0
  115. package/kits/hub-game/code/client/grab.js +376 -0
  116. package/kits/hub-game/code/client/hookLayer.js +236 -0
  117. package/kits/hub-game/code/client/idle.js +326 -0
  118. package/kits/hub-game/code/client/keys.js +48 -0
  119. package/kits/hub-game/code/client/pack.js +232 -0
  120. package/kits/hub-game/code/client/packFormat.js +397 -0
  121. package/kits/hub-game/code/client/say.js +127 -0
  122. package/kits/hub-game/code/client/sha256.js +80 -0
  123. package/kits/hub-game/code/client/sound.js +147 -0
  124. package/kits/hub-game/code/client/state.js +143 -0
  125. package/kits/hub-game/code/client/talk.js +233 -0
  126. package/kits/hub-game/code/client/tiers.js +480 -0
  127. package/kits/hub-game/code/client/together.js +440 -0
  128. package/kits/hub-game/code/client/wear.js +149 -0
  129. package/kits/hub-game/code/server/actors.js +627 -0
  130. package/kits/hub-game/code/server/blocks.js +103 -0
  131. package/kits/hub-game/code/server/cabinets.js +309 -0
  132. package/kits/hub-game/code/server/game.js +137 -0
  133. package/kits/hub-game/code/server/grab.js +248 -0
  134. package/kits/hub-game/code/server/history.js +96 -0
  135. package/kits/hub-game/code/server/hub.js +1998 -0
  136. package/kits/hub-game/code/server/idle.js +412 -0
  137. package/kits/hub-game/code/server/index.js +64 -0
  138. package/kits/hub-game/code/server/labels.js +59 -0
  139. package/kits/hub-game/code/server/manifest.js +554 -0
  140. package/kits/hub-game/code/server/npcs.js +733 -0
  141. package/kits/hub-game/code/server/party.js +439 -0
  142. package/kits/hub-game/code/server/place.js +43 -0
  143. package/kits/hub-game/code/server/say.js +83 -0
  144. package/kits/hub-game/code/server/wear.js +142 -0
  145. package/kits/hub-game/code/systems/hub.js +2347 -0
  146. package/kits/hub-game/docs/content-pack.md +151 -0
  147. package/kits/hub-game/docs/contract.md +1209 -0
  148. package/kits/hub-game/docs/style.md +137 -0
  149. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  150. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  151. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  152. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  153. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  154. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  155. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  156. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  157. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  158. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  159. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  160. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  161. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  162. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  163. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  164. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  165. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  166. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  167. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  168. package/kits/hub-game/eslint.config.js +20 -0
  169. package/kits/hub-game/fonts.generated.js +9 -0
  170. package/kits/hub-game/index.html +22 -0
  171. package/kits/hub-game/main.jsx +6 -0
  172. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  174. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  175. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  176. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  177. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  178. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  179. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  180. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  181. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  182. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  183. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  184. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  185. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  186. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  187. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  188. package/kits/hub-game/package-lock.json +365 -0
  189. package/kits/hub-game/package.json +32 -0
  190. package/kits/hub-game/scenes/main.scene +318 -0
  191. package/kits/hub-game/scripts/check.mjs +339 -0
  192. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  193. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  194. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  195. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  196. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  197. package/kits/hub-game/scripts/pack.mjs +192 -0
  198. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  199. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  200. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  201. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  202. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  203. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  204. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  205. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  206. package/kits/hub-game/scripts/test-door.mjs +56 -0
  207. package/kits/hub-game/scripts/test-grab-client.mjs +328 -0
  208. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  209. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  210. package/kits/hub-game/scripts/test-hub-trip.mjs +1518 -0
  211. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  212. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  213. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  214. package/kits/hub-game/scripts/test-manifest.mjs +270 -0
  215. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  216. package/kits/hub-game/scripts/test-npcs.mjs +724 -0
  217. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  218. package/kits/hub-game/scripts/test-party.mjs +369 -0
  219. package/kits/hub-game/scripts/test-say.mjs +149 -0
  220. package/kits/hub-game/scripts/test-tiers.mjs +666 -0
  221. package/kits/hub-game/scripts/test-together.mjs +215 -0
  222. package/kits/hub-game/theme.style +3 -0
  223. package/kits/multiplayer-3d/castle.json +5 -5
  224. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  225. package/kits/multiplayer-3d/code/server/players.js +4 -1
  226. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  227. package/kits/multiplayer-3d/code/server/world.js +20 -7
  228. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  229. package/kits/physics-2d/CLAUDE.md +2 -0
  230. package/kits/physics-2d/castle.json +2 -2
  231. package/kits/physics-2d/engine/art.js +13 -0
  232. package/kits/physics-2d/engine/assets.js +4 -2
  233. package/kits/physics-2d/engine/scene.js +117 -5
  234. package/kits/physics-2d/package.json +1 -1
  235. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
  236. package/kits/physics-3d/CLAUDE.md +285 -1
  237. package/kits/physics-3d/behaviors/Player.jsx +74 -1
  238. package/kits/physics-3d/castle.json +3 -3
  239. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -5
  240. package/kits/physics-3d/engine3d/actornode.js +253 -0
  241. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  242. package/kits/physics-3d/engine3d/materials.js +29 -1
  243. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  244. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  245. package/kits/physics-3d/engine3d/world3d.js +527 -5
  246. package/kits/physics-3d/package.json +1 -1
  247. package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
  248. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  249. package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
  250. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  251. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  252. package/kits/physics-3d/systems/physics3d.js +187 -5
  253. package/kits/real-time/castle.json +2 -2
  254. package/kits/real-time/code/client/connection.js +77 -7
  255. package/kits/real-time/code/client/gameHooks.js +63 -3
  256. package/kits/real-time/code/client/messages.js +7 -0
  257. package/kits/real-time/code/server/gameHooks.js +24 -0
  258. package/kits/real-time/code/server/session.js +390 -22
  259. package/package.json +2 -2
@@ -0,0 +1,397 @@
1
+ // The content pack's format (docs/content-pack.md): its version, its budgets,
2
+ // and the one validator both sides run -- the exporter before a pack is
3
+ // written (scripts/pack.mjs) and the town's engine before one is mounted
4
+ // (pack.js). Plain JavaScript with no imports, so node and the browser read it
5
+ // the same way.
6
+ //
7
+ // Validation never evaluates anything: a pack is JSON, and every check here
8
+ // is on its shape and its sizes.
9
+
10
+ export const PACK_FORMAT = 'castle.hub-pack';
11
+ export const PACK_VERSION = 1;
12
+ // The hub contract versions this engine can mount content for.
13
+ export const PACK_CONTRACTS = [1];
14
+
15
+ export const PACK_LIMITS = {
16
+ bytes: 4 * 1024 * 1024,
17
+ files: 600,
18
+ mediaBytes: 3 * 1024 * 1024,
19
+ // A place's sounds (`audio`, contract v1.6 `idle.sounds`): the same caps
20
+ // the kit's check puts on a deck's sounds, inside `bytes` and `files`.
21
+ audioBytes: 1024 * 1024,
22
+ audioFileBytes: 256 * 1024,
23
+ sceneBytes: 1024 * 1024,
24
+ sceneActors: 1500,
25
+ places: 16,
26
+ // Meshes one model may expand to once its repeats, scatters, mirrors, tips
27
+ // and model references are applied, and all of one scene's models together:
28
+ // a few hundred bytes of nested repeats can otherwise ask for a billion.
29
+ modelLeaves: 20000,
30
+ sceneLeaves: 150000,
31
+ };
32
+
33
+ // The most copies each modifier can make (physics-3d pxmodel.js, scatter.js,
34
+ // paths.js, branches.js); the estimate below takes the worst case.
35
+ const REPEAT_MAX = 64;
36
+ const SPREAD_MAX = 200;
37
+ const TIPS_MAX = 800;
38
+ const MODEL_DEPTH_MAX = 4;
39
+
40
+ // What a pack may hold: data the engine reads, never code.
41
+ export const TEXT_EXTS = ['.scene', '.pxmodel', '.pxart', '.sprite'];
42
+ export const MEDIA_TYPES = {
43
+ '.png': 'image/png',
44
+ '.jpg': 'image/jpeg',
45
+ '.jpeg': 'image/jpeg',
46
+ '.gif': 'image/gif',
47
+ '.webp': 'image/webp',
48
+ };
49
+ // The sounds a place's `idle.sounds` may name (manifest.js SOUND_EXTS), by
50
+ // their exact, lower-case extension. A pack's `audio` holds only these, and
51
+ // only files some place in the pack lists. An engine older than `audio`
52
+ // ignores the key: the game is silent there, and nothing refuses the pack.
53
+ export const AUDIO_TYPES = {
54
+ '.mp3': 'audio/mpeg',
55
+ '.wav': 'audio/wav',
56
+ '.m4a': 'audio/mp4',
57
+ };
58
+ const SAFE_PATH = /^[A-Za-z0-9_@-][A-Za-z0-9_./@ -]*$/;
59
+ const COMPONENT_NAME = /^[A-Z][A-Za-z0-9]{0,47}$/;
60
+ const SHA256 = /^[0-9a-f]{64}$/;
61
+ const BASE64 = /^[A-Za-z0-9+/]*={0,2}$/;
62
+
63
+ export function extOf(path) {
64
+ const dot = path.lastIndexOf('.');
65
+ return dot < 0 ? '' : path.slice(dot).toLowerCase();
66
+ }
67
+
68
+ export function isPackPath(path) {
69
+ return (
70
+ typeof path === 'string' &&
71
+ path.length <= 200 &&
72
+ SAFE_PATH.test(path) &&
73
+ !path.split('/').some((part) => part === '..' || part === '.' || part === '')
74
+ );
75
+ }
76
+
77
+ // `{ errors }`: an empty list is a pack this engine may mount. `byteLength` is
78
+ // the size of the document as fetched (or written), when the caller knows it.
79
+ export function validatePack(pack, { byteLength = null } = {}) {
80
+ const errors = [];
81
+ if (!isObject(pack)) {
82
+ return { errors: ['a pack is a JSON object'] };
83
+ }
84
+ if (pack.format !== PACK_FORMAT) {
85
+ errors.push(`format is ${JSON.stringify(pack.format)}, not ${PACK_FORMAT}`);
86
+ }
87
+ if (pack.version !== PACK_VERSION) {
88
+ errors.push(`pack version ${JSON.stringify(pack.version)} is not one this engine reads (${PACK_VERSION})`);
89
+ }
90
+ if (!PACK_CONTRACTS.includes(pack.contract)) {
91
+ errors.push(`hub contract ${JSON.stringify(pack.contract)} is not one this engine reads (${PACK_CONTRACTS.join(', ')})`);
92
+ }
93
+ if (byteLength !== null && byteLength > PACK_LIMITS.bytes) {
94
+ errors.push(`the pack is ${byteLength} bytes, over ${PACK_LIMITS.bytes}`);
95
+ }
96
+ if (errors.length) {
97
+ return { errors };
98
+ }
99
+ const files = isObject(pack.files) ? pack.files : null;
100
+ const media = isObject(pack.media) ? pack.media : {};
101
+ if (!files) {
102
+ return { errors: ['files is missing'] };
103
+ }
104
+ if (pack.audio !== undefined && !isObject(pack.audio)) {
105
+ return { errors: ['audio is not an object'] };
106
+ }
107
+ const audio = pack.audio ?? {};
108
+ const paths = Object.keys(files);
109
+ if (paths.length + Object.keys(media).length + Object.keys(audio).length > PACK_LIMITS.files) {
110
+ errors.push(`over ${PACK_LIMITS.files} files`);
111
+ }
112
+ for (const path of paths) {
113
+ const entry = files[path];
114
+ if (!isPackPath(path) || !TEXT_EXTS.includes(extOf(path))) {
115
+ errors.push(`${JSON.stringify(path)} is not a file a pack may hold`);
116
+ } else if (!isObject(entry) || typeof entry.text !== 'string' || !SHA256.test(entry.sha256 ?? '')) {
117
+ errors.push(`${path}: an entry is { sha256, text }`);
118
+ }
119
+ }
120
+ let mediaBytes = 0;
121
+ for (const [path, entry] of Object.entries(media)) {
122
+ const type = MEDIA_TYPES[extOf(path)];
123
+ if (!isPackPath(path) || !type) {
124
+ errors.push(`${JSON.stringify(path)} is not media a pack may hold`);
125
+ } else if (!isObject(entry) || entry.type !== type || typeof entry.base64 !== 'string' || !BASE64.test(entry.base64) || !SHA256.test(entry.sha256 ?? '')) {
126
+ errors.push(`${path}: a media entry is { sha256, type: ${type}, base64 }`);
127
+ } else {
128
+ mediaBytes += Math.floor((entry.base64.length * 3) / 4);
129
+ }
130
+ }
131
+ if (mediaBytes > PACK_LIMITS.mediaBytes) {
132
+ errors.push(`${mediaBytes} bytes of media, over ${PACK_LIMITS.mediaBytes}`);
133
+ }
134
+ checkAudio(audio, isObject(pack.places) ? pack.places : {}, errors);
135
+ if (errors.length) {
136
+ return { errors };
137
+ }
138
+ const places = isObject(pack.places) ? Object.entries(pack.places) : [];
139
+ if (!places.length || places.length > PACK_LIMITS.places || !isObject(pack.places.main)) {
140
+ errors.push(`places must name a main place and at most ${PACK_LIMITS.places} in all`);
141
+ }
142
+ for (const [id, place] of places) {
143
+ const scene = isObject(place) ? place.scene : null;
144
+ if (typeof scene !== 'string' || !files[scene]) {
145
+ errors.push(`place ${id}: its scene ${JSON.stringify(scene)} is not in the pack`);
146
+ } else if (typeof place.title !== 'string' || !place.title) {
147
+ errors.push(`place ${id}: no title`);
148
+ }
149
+ }
150
+ const leaves = makeLeafCounter(files);
151
+ const placeScenes = new Set(places.map(([, place]) => place?.scene));
152
+ for (const path of paths) {
153
+ const text = files[path].text;
154
+ if (extOf(path) === '.scene') {
155
+ checkScene(path, text, errors, leaves, placeScenes.has(path), files);
156
+ } else if (extOf(path) === '.pxmodel') {
157
+ const n = leaves(path);
158
+ if (n === null) {
159
+ errors.push(`${path} is not a model`);
160
+ } else if (n > PACK_LIMITS.modelLeaves) {
161
+ errors.push(`${path} expands to over ${PACK_LIMITS.modelLeaves} meshes`);
162
+ }
163
+ }
164
+ }
165
+ return { errors };
166
+ }
167
+
168
+ // The pack's sounds: each a file some place's `idle.sounds` lists, of the type
169
+ // its extension names, inline as base64, within the per-file and total caps.
170
+ // The bytes are never decoded as audio here; the browser does that.
171
+ function checkAudio(audio, places, errors) {
172
+ const listed = new Set();
173
+ for (const place of Object.values(places)) {
174
+ const sounds = isObject(place) && isObject(place.idle) && Array.isArray(place.idle.sounds) ? place.idle.sounds : [];
175
+ for (const file of sounds) {
176
+ listed.add(file);
177
+ }
178
+ }
179
+ let audioBytes = 0;
180
+ for (const [path, entry] of Object.entries(audio)) {
181
+ const ext = Object.keys(AUDIO_TYPES).find((e) => path.endsWith(e));
182
+ const type = ext ? AUDIO_TYPES[ext] : null;
183
+ if (!isPackPath(path) || !type) {
184
+ errors.push(`${JSON.stringify(path)} is not audio a pack may hold`);
185
+ } else if (!listed.has(path)) {
186
+ errors.push(`${path}: no place's idle.sounds lists it`);
187
+ } else if (!isObject(entry) || entry.type !== type || typeof entry.base64 !== 'string' || !BASE64.test(entry.base64) || !SHA256.test(entry.sha256 ?? '')) {
188
+ errors.push(`${path}: an audio entry is { sha256, type: ${type}, base64 }`);
189
+ } else {
190
+ // Decoded size: padding ('=') is not data.
191
+ const padding = entry.base64.endsWith('==') ? 2 : entry.base64.endsWith('=') ? 1 : 0;
192
+ const bytes = Math.floor((entry.base64.length * 3) / 4) - padding;
193
+ if (bytes > PACK_LIMITS.audioFileBytes) {
194
+ errors.push(`${path} is ${bytes} bytes, over ${PACK_LIMITS.audioFileBytes}`);
195
+ }
196
+ audioBytes += bytes;
197
+ }
198
+ }
199
+ if (audioBytes > PACK_LIMITS.audioBytes) {
200
+ errors.push(`${audioBytes} bytes of audio, over ${PACK_LIMITS.audioBytes}`);
201
+ }
202
+ }
203
+
204
+ // How many meshes a model file expands to, worst case: each part counts once
205
+ // (a model part counts as the model it names), times every copy its own
206
+ // modifiers and its ancestors' make. Null for a file that is not a model.
207
+ // Saturates just past the budget, so a hostile file costs no more to measure.
208
+ function makeLeafCounter(files) {
209
+ const memo = new Map();
210
+ const cap = PACK_LIMITS.modelLeaves + 1;
211
+ const count = (path, chain) => {
212
+ if (memo.has(path)) {
213
+ return memo.get(path);
214
+ }
215
+ const model = parseJson(files[path]?.text ?? '');
216
+ if (!isObject(model) || !Array.isArray(model.parts)) {
217
+ return null;
218
+ }
219
+ const parts = normalizedParts(model.parts);
220
+ const children = new Map();
221
+ for (const part of parts) {
222
+ const parent = typeof part.parent === 'string' ? part.parent : '';
223
+ children.set(parent, [...(children.get(parent) ?? []), part]);
224
+ }
225
+ const byId = new Map(parts.map((part) => [part.id, part]));
226
+ const subtree = (part, seen) => {
227
+ if (seen.has(part) || seen.size > parts.length) {
228
+ return 0;
229
+ }
230
+ const inner = new Set(seen).add(part);
231
+ let n = 1;
232
+ if (part.kind === 'model' && typeof part.src === 'string' && chain.length < MODEL_DEPTH_MAX) {
233
+ const ref = modelRefPath(part.src, path);
234
+ n = chain.includes(ref) || !files[ref] ? 0 : (count(ref, [...chain, ref]) ?? 0);
235
+ }
236
+ for (const child of children.get(part.id) ?? []) {
237
+ n = Math.min(cap, n + subtree(child, inner));
238
+ }
239
+ const tips = typeof part.tips === 'string' ? byId.get(part.tips) : null;
240
+ if (tips && tips !== part) {
241
+ n = Math.min(cap, n + TIPS_MAX * subtree(tips, inner));
242
+ }
243
+ return Math.min(cap, n * copies(part));
244
+ };
245
+ let total = 0;
246
+ for (const root of children.get('') ?? []) {
247
+ total = Math.min(cap, total + subtree(root, new Set()));
248
+ }
249
+ memo.set(path, total);
250
+ return total;
251
+ };
252
+ return (path) => count(path, [path]);
253
+ }
254
+
255
+ // Parts the way the renderer reads them (physics-3d pxmodel.js `parseModel`):
256
+ // missing ids numbered, duplicate ids suffixed, a parent that does not
257
+ // resolve or that cycles dropped to the root. Counting any other way would
258
+ // let a part the renderer roots go uncounted.
259
+ function normalizedParts(raw) {
260
+ const parts = raw.map((part, index) => {
261
+ const p = isObject(part) ? { ...part } : {};
262
+ p.id = typeof p.id === 'string' && p.id ? p.id : `part-${index + 1}`;
263
+ p.parent = typeof p.parent === 'string' ? p.parent : '';
264
+ return p;
265
+ });
266
+ const seen = new Set();
267
+ for (const part of parts) {
268
+ while (seen.has(part.id)) {
269
+ part.id = `${part.id}-2`;
270
+ }
271
+ seen.add(part.id);
272
+ }
273
+ const byId = new Map(parts.map((part) => [part.id, part]));
274
+ for (const part of parts) {
275
+ if (part.parent && !byId.has(part.parent)) {
276
+ part.parent = '';
277
+ }
278
+ }
279
+ for (const part of parts) {
280
+ const visited = new Set([part.id]);
281
+ let current = part;
282
+ while (current.parent) {
283
+ if (visited.has(current.parent)) {
284
+ part.parent = '';
285
+ break;
286
+ }
287
+ visited.add(current.parent);
288
+ current = byId.get(current.parent) ?? { parent: '' };
289
+ }
290
+ }
291
+ return parts;
292
+ }
293
+
294
+ function copies(part) {
295
+ const amount = (value, max, fallback) => Math.max(1, Math.min(max, Math.round(Number(value ?? fallback)) || 1));
296
+ let n = 1;
297
+ if (isObject(part.scatter)) {
298
+ n = 1 + amount(part.scatter.count, SPREAD_MAX, 10);
299
+ } else if (isObject(part.repeat)) {
300
+ n = part.repeat.along ? amount(part.repeat.count, SPREAD_MAX, 10) : amount(part.repeat.count, REPEAT_MAX, 1);
301
+ }
302
+ return ['x', 'y', 'z'].includes(part.mirror) ? n * 2 : n;
303
+ }
304
+
305
+ // The pack path a model part's `src` names, from the model at `fromPath`
306
+ // (physics-3d modelrefs.js `resolveModelRef`).
307
+ function modelRefPath(src, fromPath) {
308
+ const ref = src.replace(/^\.\//, '');
309
+ if (ref.startsWith('@imports/')) {
310
+ return ref.slice(1);
311
+ }
312
+ if (ref.startsWith('imports/')) {
313
+ return ref;
314
+ }
315
+ return (/^(imports\/[^/]+\/)/.exec(fromPath)?.[1] ?? '') + ref;
316
+ }
317
+
318
+ // A scene the runtime can load (physics-2d `SceneRuntime.load`): an object
319
+ // whose `actors` is a list of `{ id, components }`, every component an object
320
+ // under a component name. Checked before anyone leaves, since a scene that
321
+ // throws while loading would strand the player between places.
322
+ // A blueprint's template actors have no id; a place's actors do.
323
+ function checkScene(path, text, errors, leaves, isPlace, files) {
324
+ if (text.length > PACK_LIMITS.sceneBytes) {
325
+ errors.push(`${path} is over ${PACK_LIMITS.sceneBytes} bytes`);
326
+ return;
327
+ }
328
+ const scene = parseJson(text);
329
+ if (!isObject(scene) || (scene.actors !== undefined && !Array.isArray(scene.actors))) {
330
+ errors.push(`${path} is not a scene`);
331
+ return;
332
+ }
333
+ const actors = scene.actors ?? [];
334
+ if (actors.length > PACK_LIMITS.sceneActors) {
335
+ errors.push(`${path} has ${actors.length} actors, over ${PACK_LIMITS.sceneActors}`);
336
+ }
337
+ let meshes = 0;
338
+ for (const actor of actors) {
339
+ const shapeError = actorShapeError(actor, isPlace);
340
+ if (shapeError) {
341
+ errors.push(`${path}: ${shapeError}`);
342
+ return;
343
+ }
344
+ // A blueprint actor draws its template's model unless it names its own.
345
+ const model = actor.components?.Model?.file ?? blueprintModel(actor.blueprint, path, files);
346
+ meshes += typeof model === 'string' ? (leaves(model.replace(/^@(?=imports\/)/, '')) ?? 0) : 1;
347
+ }
348
+ if (meshes > PACK_LIMITS.sceneLeaves) {
349
+ errors.push(`${path} draws over ${PACK_LIMITS.sceneLeaves} meshes`);
350
+ }
351
+ }
352
+
353
+ // The model a blueprint's template draws (physics-2d blueprint.js resolves
354
+ // the ref against the file it is written in, `@imports/` naming an import).
355
+ function blueprintModel(ref, fromPath, files) {
356
+ if (typeof ref !== 'string') {
357
+ return null;
358
+ }
359
+ const clean = ref.replace(/^\.\//, '');
360
+ const key = clean.startsWith('@imports/') ? clean.slice(1) : (/^(imports\/[^/]+\/)/.exec(fromPath)?.[1] ?? '') + clean;
361
+ const template = parseJson(files[key]?.text ?? '');
362
+ const file = isObject(template) && Array.isArray(template.actors) ? template.actors[0]?.components?.Model?.file : null;
363
+ // The template's own refs resolve against the blueprint's deck.
364
+ return typeof file === 'string' ? modelRefPath(file, key) : null;
365
+ }
366
+
367
+ function actorShapeError(actor, needsId) {
368
+ // A blueprint actor may leave its components to the blueprint.
369
+ const components = isObject(actor) && actor.components === undefined && typeof actor.blueprint === 'string' ? {} : actor?.components;
370
+ if (!isObject(actor) || !isObject(components) || (needsId && (typeof actor.id !== 'string' || !actor.id))) {
371
+ return needsId ? 'an actor is { id, components }' : 'an actor is { components }';
372
+ }
373
+ if (actor.blueprint !== undefined && typeof actor.blueprint !== 'string') {
374
+ return `actor ${actor.id}: blueprint is a path`;
375
+ }
376
+ for (const [name, props] of Object.entries(components)) {
377
+ if (!COMPONENT_NAME.test(name)) {
378
+ return `${JSON.stringify(name)} is not a component name`;
379
+ }
380
+ if (!isObject(props)) {
381
+ return `actor ${actor.id}: component ${name} is not an object`;
382
+ }
383
+ }
384
+ return null;
385
+ }
386
+
387
+ function parseJson(text) {
388
+ try {
389
+ return JSON.parse(text);
390
+ } catch {
391
+ return undefined;
392
+ }
393
+ }
394
+
395
+ function isObject(value) {
396
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
397
+ }
@@ -0,0 +1,127 @@
1
+ // Quick reactions and phrases (contract §6, v1.11), the client's half: the
2
+ // kit's emoji and canned lines, the bubbles over the players who said one,
3
+ // and the short feed of what was just said. Every word here is the kit's: a
4
+ // `hub-said` carries an id, never text.
5
+
6
+ import { hub } from './state.js';
7
+
8
+ export const REACTIONS = ['👍', '😂', '❤️', '😮', '👋', '🎉'];
9
+ const PHRASES = {
10
+ here: { text: 'Over here!', town: true, game: true },
11
+ play: { text: "Let's play", town: true, game: false },
12
+ wait: { text: 'Wait for me', town: true, game: true },
13
+ yes: { text: 'Yes!', town: true, game: true },
14
+ no: { text: 'Not now', town: true, game: true },
15
+ gg: { text: 'Good game!', town: false, game: true },
16
+ again: { text: 'Again?', town: false, game: true },
17
+ back: { text: 'Back to town?', town: false, game: true },
18
+ };
19
+ // A bubble stays this long, and a feed line this long; the feed keeps a few.
20
+ const BUBBLE_MS = 4000;
21
+ const FEED_MS = 9000;
22
+ const FEED_MAX = 3;
23
+ const BUBBLE_Y = 2.25;
24
+
25
+ // The words of a phrase said in this place: "Let's play Tile Fall" names the
26
+ // door the speaker stands by, when they stand by one.
27
+ function wordsOf(said) {
28
+ const phrase = PHRASES[said.p];
29
+ if (!phrase) {
30
+ return null;
31
+ }
32
+ if (said.p === 'play') {
33
+ const title = typeof said.door === 'string' ? hub.doors.find((d) => d.id === said.door)?.title : null;
34
+ return title ? `Let's play ${title}` : "Let's play something";
35
+ }
36
+ return phrase.text;
37
+ }
38
+
39
+ export function makeSay() {
40
+ // playerId -> { text, emoji, until }, and [{ text, until }]
41
+ const bubbles = new Map();
42
+ let feed = [];
43
+
44
+ return {
45
+ // The phrases this place offers: [[id, words]].
46
+ phrases() {
47
+ const game = Boolean(hub.place?.result);
48
+ return Object.entries(PHRASES)
49
+ .filter(([, p]) => (game ? p.game : p.town))
50
+ .map(([id, p]) => [id, id === 'play' ? "Let's play…" : p.text]);
51
+ },
52
+
53
+ // Takes what was said since the last frame (`hub.saidEvents`). `nameOf`
54
+ // is a player's name, or null for one this client does not know;
55
+ // `onHere(playerId)` hears an "Over here!", `onPlay(playerId, name,
56
+ // door)` a "Let's play" that names a door.
57
+ frame(now, selfId, nameOf, onHere = () => {}, onPlay = () => {}) {
58
+ for (const said of hub.saidEvents.splice(0)) {
59
+ const mine = said.player === selfId;
60
+ const name = mine ? 'You' : nameOf(said.player);
61
+ if (!name) {
62
+ continue;
63
+ }
64
+ const emoji = Number.isInteger(said.r) ? REACTIONS[said.r] : null;
65
+ const text = emoji ? null : wordsOf(said);
66
+ if (!emoji && !text) {
67
+ continue;
68
+ }
69
+ bubbles.set(said.player, { text, emoji, until: now + BUBBLE_MS });
70
+ if (text) {
71
+ feed.push({ text: `${name}: ${text}`, until: now + FEED_MS });
72
+ feed = feed.slice(-FEED_MAX);
73
+ if (said.p === 'here' && !mine) {
74
+ onHere(said.player);
75
+ }
76
+ // "Let's play Tile Fall" names a door: the listener can be taken there.
77
+ if (said.p === 'play' && !mine && typeof said.door === 'string') {
78
+ onPlay(said.player, name, said.door);
79
+ }
80
+ }
81
+ }
82
+ for (const [playerId, b] of bubbles) {
83
+ if (now >= b.until) {
84
+ bubbles.delete(playerId);
85
+ }
86
+ }
87
+ feed = feed.filter((line) => now < line.until);
88
+ },
89
+
90
+ // The bubbles over players on screen, as the chrome's label items. An
91
+ // emoji and a line are different labels (the chrome styles a label once).
92
+ labels(scene, local, cam, view, project, selfId) {
93
+ const out = [];
94
+ const mp = scene.multiplayer;
95
+ for (const [playerId, b] of bubbles) {
96
+ const actorId = playerId === selfId ? local?.id : mp?.avatars?.actors?.get(playerId);
97
+ const at = actorId ? scene.getActor(actorId)?.components.Transform : null;
98
+ const screen = at ? project(cam, at.x, at.y + BUBBLE_Y, at.z, view) : null;
99
+ if (screen) {
100
+ out.push(b.emoji
101
+ ? { id: `react:${playerId}`, x: screen.x, y: screen.y, text: b.emoji, emote: true }
102
+ : { id: `said:${playerId}`, x: screen.x, y: screen.y, text: b.text, speech: true });
103
+ }
104
+ }
105
+ return out;
106
+ },
107
+
108
+ // A line of free-text chat (`chat.js`): over its speaker when they are a
109
+ // player here, and in the feed; another player's line can be reported.
110
+ chat(now, playerId, name, message, bubbleMs) {
111
+ if (playerId) {
112
+ bubbles.set(playerId, { text: message.text, emoji: null, until: now + bubbleMs });
113
+ }
114
+ feed.push({ text: `${name}: ${message.text}`, until: now + FEED_MS + bubbleMs, id: message.mine ? null : (message.id ?? null) });
115
+ feed = feed.slice(-FEED_MAX);
116
+ },
117
+
118
+ // The feed's lines, oldest first: `{ text, id }`, `id` a chat message
119
+ // that may be reported, or null.
120
+ feed: () => feed.map((line) => ({ text: line.text, id: line.id ?? null })),
121
+
122
+ clear() {
123
+ bubbles.clear();
124
+ feed = [];
125
+ },
126
+ };
127
+ }
@@ -0,0 +1,80 @@
1
+ // SHA-256 for verifying a content pack against the hash its door names
2
+ // (pack.js). Plain JavaScript with no imports, so a test can run it in node.
3
+
4
+ // SHA-256 as hex. `crypto.subtle` exists only in a secure context; a phone
5
+ // playing over a LAN address is not one, so a plain implementation stands in.
6
+ export async function sha256Hex(bytes) {
7
+ if (globalThis.crypto?.subtle) {
8
+ const digest = await globalThis.crypto.subtle.digest('SHA-256', bytes);
9
+ return toHex(new Uint8Array(digest));
10
+ }
11
+ return toHex(sha256Plain(bytes));
12
+ }
13
+
14
+ function toHex(bytes) {
15
+ let out = '';
16
+ for (const b of bytes) {
17
+ out += b.toString(16).padStart(2, '0');
18
+ }
19
+ return out;
20
+ }
21
+
22
+ const K = new Uint32Array([
23
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,
24
+ 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
25
+ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
26
+ 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
27
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08,
28
+ 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
29
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
30
+ ]);
31
+
32
+ function sha256Plain(bytes) {
33
+ const len = bytes.length;
34
+ const padded = new Uint8Array(((len + 9 + 63) >> 6) << 6);
35
+ padded.set(bytes);
36
+ padded[len] = 0x80;
37
+ const view = new DataView(padded.buffer);
38
+ view.setUint32(padded.length - 8, Math.floor(len / 0x20000000));
39
+ view.setUint32(padded.length - 4, (len << 3) >>> 0);
40
+ const h = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]);
41
+ const w = new Uint32Array(64);
42
+ const rotr = (x, n) => (x >>> n) | (x << (32 - n));
43
+ for (let off = 0; off < padded.length; off += 64) {
44
+ for (let i = 0; i < 16; i++) {
45
+ w[i] = view.getUint32(off + i * 4);
46
+ }
47
+ for (let i = 16; i < 64; i++) {
48
+ const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
49
+ const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
50
+ w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
51
+ }
52
+ let [a, b, c, d, e, f, g, hh] = h;
53
+ for (let i = 0; i < 64; i++) {
54
+ const t1 = (hh + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i]) >>> 0;
55
+ const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0;
56
+ hh = g;
57
+ g = f;
58
+ f = e;
59
+ e = (d + t1) >>> 0;
60
+ d = c;
61
+ c = b;
62
+ b = a;
63
+ a = (t1 + t2) >>> 0;
64
+ }
65
+ h[0] += a;
66
+ h[1] += b;
67
+ h[2] += c;
68
+ h[3] += d;
69
+ h[4] += e;
70
+ h[5] += f;
71
+ h[6] += g;
72
+ h[7] += hh;
73
+ }
74
+ const out = new Uint8Array(32);
75
+ const outView = new DataView(out.buffer);
76
+ for (let i = 0; i < 8; i++) {
77
+ outView.setUint32(i * 4, h[i]);
78
+ }
79
+ return out;
80
+ }