@wildshard/shard-format 0.67.0 → 0.68.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/package.json +7 -19
  2. package/src/cli/corpus-deprecations.ts +41 -0
  3. package/src/cli/corpus.ts +300 -0
  4. package/src/cli/main.ts +32 -0
  5. package/src/format/cook-preparation.ts +22 -6
  6. package/src/format/deprecations.ts +132 -0
  7. package/src/format/empty-shardfile.json +56 -0
  8. package/src/format/empty-shardfile.ts +43 -0
  9. package/src/format/format-migrate-inventory.ts +142 -0
  10. package/src/format/format-migrate-rebuild.ts +269 -0
  11. package/src/format/format-migrate.ts +290 -0
  12. package/src/format/format-migration-journal.ts +129 -0
  13. package/src/format/format-migration-ops.ts +230 -0
  14. package/src/format/format-migration-prune.ts +106 -0
  15. package/src/format/format-migrations.ts +124 -0
  16. package/src/format/issue-codes.ts +3 -1
  17. package/src/format/journal-glyphs.json +12 -0
  18. package/src/format/memory-armed.json +1 -39
  19. package/src/format/memory-armed.ts +41 -8
  20. package/src/format/memory-epoch.ts +2 -2
  21. package/src/format/memory-line.ts +4 -3
  22. package/src/format/memoryAdmission.ts +1 -1
  23. package/src/format/policy-verdict.ts +95 -0
  24. package/src/format/runtime-tier.ts +3 -3
  25. package/src/format/shardSections.ts +11 -1
  26. package/src/format/shardfile-assets.ts +12 -8
  27. package/src/format/shardfile-stream-admit.ts +41 -16
  28. package/src/format/shardfile-stream-rows.ts +3 -1
  29. package/src/format/shardfile-stream.ts +12 -9
  30. package/src/format/shardfileValidation.ts +30 -18
  31. package/src/format/stateLineage.ts +1 -1
  32. package/src/format/version.ts +11 -13
  33. package/src/format/baked-terrain.golden.json +0 -2760
  34. package/src/format/colliders.golden.json +0 -1619
  35. package/src/format/forest-placement.golden.json +0 -4588
  36. package/src/format/geometry-layout.golden.json +0 -180
  37. package/src/format/interact-table.golden.json +0 -340
  38. package/src/format/item-catalogue.golden.json +0 -2310
  39. package/src/format/memory-ratchet.ts +0 -107
  40. package/src/format/small-laws.golden.json +0 -57
  41. package/src/format/terrain-field.golden.json +0 -3998
@@ -0,0 +1,56 @@
1
+ {
2
+ "accent": "sand",
3
+ "requires": {
4
+ "capabilities": [],
5
+ "commons": [],
6
+ "commonsWire": {},
7
+ "commonsCosts": {}
8
+ },
9
+ "budgets": {
10
+ "library": { "resident": 0, "compressed": 0 },
11
+ "sim": { "resident": 0, "compressed": 0 },
12
+ "overlap": 0
13
+ },
14
+ "entryways": [
15
+ { "edge": "north", "at": [0, 0, 250] },
16
+ { "edge": "east", "at": [250, 0, 0] },
17
+ { "edge": "south", "at": [0, 0, -250] },
18
+ { "edge": "west", "at": [-250, 0, 0] }
19
+ ],
20
+ "look": {
21
+ "families": [],
22
+ "grade": { "exposure": 0, "saturation": 1, "contrast": 1, "lut": null },
23
+ "clock": "engine",
24
+ "dayOverride": null,
25
+ "keys": []
26
+ },
27
+ "sim": {
28
+ "fixedHz": 60,
29
+ "scriptTickDivisor": 2,
30
+ "commandVersion": 0,
31
+ "snapshotVersion": 0,
32
+ "scripts": []
33
+ },
34
+ "state": {
35
+ "version": 1,
36
+ "sharedOwner": "host",
37
+ "playerKey": "actorId",
38
+ "shared": [],
39
+ "player": []
40
+ },
41
+ "authorCaps": { "players": 32, "speed": 15 },
42
+ "serverBudget": {
43
+ "tickMicros": 1000,
44
+ "memory": 1000000,
45
+ "entities": 100,
46
+ "commandsPerTick": 32
47
+ },
48
+ "edgeSamples": 257,
49
+ "edgeGrey": 0.5,
50
+ "edgeHeight": 0,
51
+ "files": [],
52
+ "tiles": [],
53
+ "library": [],
54
+ "critical": [],
55
+ "far": null
56
+ }
@@ -0,0 +1,43 @@
1
+ /** The one starting shard file (FORMAT-PRUNE FP11), admitted by the format's own parser, so a new project starts
2
+ * from a file the game accepts. Engine and sharddev `author.ts` re-export it. */
3
+ import { ENTRY_WIDTH } from './contentBounds.ts'
4
+ import start from './empty-shardfile.json' with { type: 'json' }
5
+ import { parseShardfile } from './shardSchema.ts'
6
+ import { SHARDFILE_VERSION } from './version.ts'
7
+
8
+ import type { Shardfile } from './shardSchema.ts'
9
+
10
+ /** Each starting door is as wide as the format's entry width. */
11
+ function widened(
12
+ row: Readonly<{ edge: string; at: readonly number[] }>,
13
+ ): object {
14
+ return { edge: row.edge, at: row.at, width: ENTRY_WIDTH }
15
+ }
16
+
17
+ /** A valid empty world; add baked content before shipping a playable shard. */
18
+ function emptyShardfile(identity: Readonly<Shardfile['identity']>): Shardfile {
19
+ const { edgeSamples, edgeGrey, edgeHeight, requires, entryways, ...rest } =
20
+ start
21
+ const edge = {
22
+ heights: Array.from({ length: edgeSamples }, (): number => edgeHeight),
23
+ colours: Array.from({ length: edgeSamples }, (): number[] => [
24
+ edgeGrey,
25
+ edgeGrey,
26
+ edgeGrey,
27
+ ]),
28
+ roadHeight: edgeHeight,
29
+ }
30
+ return parseShardfile({
31
+ ...rest,
32
+ version: SHARDFILE_VERSION,
33
+ identity,
34
+ requires: { sdk: SHARDFILE_VERSION, ...requires },
35
+ entryways: entryways.map(
36
+ (row: Readonly<{ edge: string; at: readonly number[] }>): object =>
37
+ widened(row),
38
+ ),
39
+ edge: { north: edge, south: edge, east: edge, west: edge },
40
+ })
41
+ }
42
+
43
+ export { emptyShardfile }
@@ -0,0 +1,142 @@
1
+ /** The migration chain's view of an original's inventory (SHARD-COMPAT CP1). It reads `shard.json` as plain JSON, never
2
+ * through the current schema, so an older shape is read before it is migrated. */
3
+ import { readCanonicalJson } from './shardfileValidation.ts'
4
+
5
+ type Files = ReadonlyMap<string, Uint8Array>
6
+ type Hash = (bytes: Readonly<Uint8Array>) => string
7
+ type JsonRecord = Record<string, unknown>
8
+
9
+ interface Listed {
10
+ readonly path: string
11
+ readonly hash: string
12
+ readonly bytes: number
13
+ }
14
+
15
+ const INDEX = 'shard.json'
16
+ const RECEIPT = 'validation.json'
17
+
18
+ /** A plain JSON object, kept by reference so a step's edit lands in the copy. */
19
+ function record(value: unknown, where: string): JsonRecord {
20
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
21
+ throw new TypeError(`Migration input needs an object at ${where}`)
22
+ }
23
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- checked a plain object just above; edits stay in place
24
+ return value as JsonRecord
25
+ }
26
+ function rows(value: unknown): readonly JsonRecord[] {
27
+ if (!Array.isArray(value)) {
28
+ return []
29
+ }
30
+ return value.map((row: unknown): JsonRecord =>
31
+ record(row, 'an inventory row'),
32
+ )
33
+ }
34
+ function listed(row: Readonly<JsonRecord>, size = 'bytes'): Listed {
35
+ const { path, hash } = row
36
+ const bytes = row[size]
37
+ if (
38
+ typeof path !== 'string' ||
39
+ typeof hash !== 'string' ||
40
+ typeof bytes !== 'number'
41
+ ) {
42
+ throw new TypeError('Migration input has a malformed inventory row')
43
+ }
44
+ return { path, hash, bytes }
45
+ }
46
+ function listedRows(value: unknown, size = 'bytes'): readonly Listed[] {
47
+ return rows(value).map((row: Readonly<JsonRecord>): Listed =>
48
+ listed(row, size),
49
+ )
50
+ }
51
+ function optionalRecord(value: unknown, where: string): JsonRecord {
52
+ return value === undefined || value === null ? {} : record(value, where)
53
+ }
54
+
55
+ /** Every byte the index and its cook file's mounts list, packs aside. */
56
+ function inventory(
57
+ index: Readonly<JsonRecord>,
58
+ cook: Readonly<JsonRecord> | undefined,
59
+ ): readonly Listed[] {
60
+ const admission = optionalRecord(index['admission'], 'admission')
61
+ const runtime = optionalRecord(index['runtime'], 'runtime')
62
+ const cookRow =
63
+ index['cook'] === undefined ? [] : [listed(record(index['cook'], 'cook'))]
64
+ return [
65
+ ...listedRows(index['sections']),
66
+ ...listedRows(index['schemas']),
67
+ ...listedRows(index['files'], 'compressed'),
68
+ ...listedRows(admission['server']),
69
+ ...listedRows(runtime['files']),
70
+ ...cookRow,
71
+ ...listedRows(cook?.['mounts']),
72
+ ]
73
+ }
74
+
75
+ /** Refuse a missing, changed or unlisted byte before any step runs. A tarball may leave a pack out, since its members
76
+ * ship anyway; a carried pack is checked like any listed file. */
77
+ function verifyInventory(
78
+ files: Files,
79
+ {
80
+ index,
81
+ cook,
82
+ }: Readonly<{
83
+ index: Readonly<JsonRecord>
84
+ cook: Readonly<JsonRecord> | undefined
85
+ }>,
86
+ hash: Hash,
87
+ ): void {
88
+ const carried = listedRows(index['packs']).filter(
89
+ (row: Readonly<Listed>): boolean => files.has(row.path),
90
+ )
91
+ const seen = new Set([INDEX, RECEIPT])
92
+ for (const entry of [...inventory(index, cook), ...carried]) {
93
+ const bytes = files.get(entry.path)
94
+ if (bytes?.length !== entry.bytes || hash(bytes) !== entry.hash) {
95
+ throw new Error(`Original hash or size mismatch: ${entry.path}`)
96
+ }
97
+ seen.add(entry.path)
98
+ }
99
+ for (const path of files.keys()) {
100
+ if (!seen.has(path)) {
101
+ throw new Error(`Unlisted original file: ${path}`)
102
+ }
103
+ }
104
+ }
105
+
106
+ function readJson(files: Files, path: string): unknown {
107
+ const bytes = files.get(path)
108
+ if (bytes === undefined) {
109
+ throw new Error(`Original lacks ${path}`)
110
+ }
111
+ return readCanonicalJson(bytes)
112
+ }
113
+
114
+ /** Each section's payload by name. */
115
+ function readSections(
116
+ files: Files,
117
+ index: Readonly<JsonRecord>,
118
+ ): Map<string, unknown> {
119
+ const sections = new Map<string, unknown>()
120
+ for (const row of rows(index['sections'])) {
121
+ if (typeof row['name'] === 'string') {
122
+ sections.set(row['name'], readJson(files, listed(row).path))
123
+ }
124
+ }
125
+ return sections
126
+ }
127
+
128
+ export {
129
+ INDEX,
130
+ RECEIPT,
131
+ type Files,
132
+ type Hash,
133
+ type JsonRecord,
134
+ type Listed,
135
+ listed,
136
+ listedRows,
137
+ readJson,
138
+ readSections,
139
+ record,
140
+ rows,
141
+ verifyInventory,
142
+ }
@@ -0,0 +1,269 @@
1
+ // oxlint-disable typescript/prefer-readonly-parameter-types -- the copy's index and files are rebuilt in place
2
+ /** The migration chain rebuilds a copy's inventory after its steps (SHARD-COMPAT CP1): the chain, never a step, writes
3
+ * section hashes and sizes, packs, the declared source hash, JSON cook mounts with their rows and fence, and a
4
+ * data-only copy's re-sealed author preparation. */
5
+ import { readCookData } from './cook-data.ts'
6
+ import { cookPreparationBytes } from './cook-preparation.ts'
7
+ import {
8
+ listed,
9
+ listedRows,
10
+ readJson,
11
+ record,
12
+ rows,
13
+ } from './format-migrate-inventory.ts'
14
+ import { assembleOutputShardfile } from './outputIndex.ts'
15
+ import { validationSourceBytes } from './validationReceipt.ts'
16
+
17
+ import type { Hash, JsonRecord, Listed } from './format-migrate-inventory.ts'
18
+ import type { MigratingShard } from './format-migrations.ts'
19
+ import type { OutputIndex } from './outputIndex.ts'
20
+
21
+ /** The copy being written: its index (edited in place) and its files (edited in place). */
22
+ interface Copy {
23
+ readonly index: JsonRecord
24
+ readonly out: Map<string, Uint8Array>
25
+ readonly hash: Hash
26
+ }
27
+
28
+ /** How the copy's cook is rebuilt: each JSON mount's text as read (a changed one is written again), and whether the
29
+ * original's preparation is re-sealed (prepared, and no step changes what renders). */
30
+ interface CookPlan {
31
+ readonly mountText: ReadonlyMap<string, string>
32
+ readonly reseal: boolean
33
+ }
34
+
35
+ const NO_BYTES = 0
36
+
37
+ /** A step may rewrite or delete a section; adding one waits for FE8's moveSection. */
38
+ function refuseNewSections(
39
+ copy: Readonly<Copy>,
40
+ shard: Readonly<MigratingShard>,
41
+ ): void {
42
+ const names = new Set(
43
+ rows(copy.index['sections']).map((row: Readonly<JsonRecord>): string =>
44
+ String(row['name']),
45
+ ),
46
+ )
47
+ for (const name of shard.sections.keys()) {
48
+ if (!names.has(name)) {
49
+ throw new Error(`A new section needs FE8's moveSection: ${name}`)
50
+ }
51
+ }
52
+ }
53
+
54
+ /** Write one section again, or drop it; the old and new hash when it stays. */
55
+ function rewriteSection(
56
+ copy: Readonly<Copy>,
57
+ shard: Readonly<MigratingShard>,
58
+ row: JsonRecord,
59
+ ): readonly [string, string] | undefined {
60
+ const old = listed(row)
61
+ const name = String(row['name'])
62
+ if (!shard.sections.has(name)) {
63
+ copy.out.delete(old.path)
64
+ return undefined
65
+ }
66
+ const bytes = validationSourceBytes(shard.sections.get(name))
67
+ const now = copy.hash(bytes)
68
+ copy.out.set(old.path, bytes)
69
+ Object.assign(row, { hash: now, bytes: bytes.length })
70
+ return [old.hash, now]
71
+ }
72
+
73
+ /** Write each section again and return each old section hash's new one; a section a step deleted leaves. */
74
+ function rebuildSections(
75
+ copy: Readonly<Copy>,
76
+ shard: Readonly<MigratingShard>,
77
+ ): Map<string, string> {
78
+ refuseNewSections(copy, shard)
79
+ const swapped = new Map<string, string>()
80
+ const kept: JsonRecord[] = []
81
+ for (const row of rows(copy.index['sections'])) {
82
+ const moved = rewriteSection(copy, shard, row)
83
+ if (moved !== undefined) {
84
+ swapped.set(...moved)
85
+ kept.push(row)
86
+ }
87
+ }
88
+ copy.index['sections'] = kept
89
+ return swapped
90
+ }
91
+
92
+ /** Every pack member's bytes by hash: sections as rebuilt, schemas and files as they are. */
93
+ function members(copy: Readonly<Copy>): Map<string, Uint8Array> {
94
+ const found = new Map<string, Uint8Array>()
95
+ const all: readonly Listed[] = [
96
+ ...listedRows(copy.index['sections']),
97
+ ...listedRows(copy.index['schemas']),
98
+ ...listedRows(copy.index['files'], 'compressed'),
99
+ ]
100
+ for (const row of all) {
101
+ const bytes = copy.out.get(row.path)
102
+ if (bytes !== undefined) {
103
+ found.set(row.hash, bytes)
104
+ }
105
+ }
106
+ return found
107
+ }
108
+
109
+ function joinParts(
110
+ parts: readonly Readonly<Uint8Array>[],
111
+ hash: Hash,
112
+ ): { joined: Uint8Array; entries: JsonRecord[] } {
113
+ const joined = new Uint8Array(
114
+ parts.reduce(
115
+ (total: number, part: Readonly<Uint8Array>): number =>
116
+ total + part.length,
117
+ NO_BYTES,
118
+ ),
119
+ )
120
+ let offset = NO_BYTES
121
+ const entries = parts.map((part: Readonly<Uint8Array>): JsonRecord => {
122
+ const entry = { hash: hash(part), offset, bytes: part.length }
123
+ joined.set(part, offset)
124
+ offset += part.length
125
+ return entry
126
+ })
127
+ return { joined, entries }
128
+ }
129
+
130
+ /** Concatenate each pack again from its members, a rewritten section by its new bytes. A pack the original carried is
131
+ * written again; one it left out stays out, listed with its new identity. */
132
+ function rebuildPacks(
133
+ copy: Readonly<Copy>,
134
+ swapped: ReadonlyMap<string, string>,
135
+ ): void {
136
+ const found = members(copy)
137
+ for (const pack of rows(copy.index['packs'])) {
138
+ const old = listed(pack)
139
+ const parts = rows(pack['entries']).map(
140
+ (entry: Readonly<JsonRecord>): Uint8Array => {
141
+ const was = String(entry['hash'])
142
+ const bytes = found.get(swapped.get(was) ?? was)
143
+ if (bytes === undefined) {
144
+ throw new Error(
145
+ `Pack member outside its listed inventory: ${old.path}`,
146
+ )
147
+ }
148
+ return bytes
149
+ },
150
+ )
151
+ const { joined, entries } = joinParts(parts, copy.hash)
152
+ const packHash = copy.hash(joined)
153
+ const path = `blobs/pack/${packHash}.pack`
154
+ if (copy.out.delete(old.path)) {
155
+ copy.out.set(path, joined)
156
+ }
157
+ Object.assign(pack, { path, hash: packHash, bytes: joined.length, entries })
158
+ }
159
+ }
160
+
161
+ /** The declared source hash over the copy's assembled shard, as the validator computes it. */
162
+ function rebuildSourceHash(
163
+ copy: Readonly<Copy>,
164
+ shard: Readonly<MigratingShard>,
165
+ ): void {
166
+ const source = assembleOutputShardfile(copy.index, shard.sections)
167
+ record(copy.index['build'], 'build')['sourceHash'] = copy.hash(
168
+ validationSourceBytes(source),
169
+ )
170
+ }
171
+
172
+ /** The copy's cook file, read from the copy as rebuilt so far. */
173
+ function cookFile(copy: Readonly<Copy>): { path: string; raw: JsonRecord } {
174
+ const { path } = listed(record(copy.index['cook'], 'cook'))
175
+ return { path, raw: record(readJson(copy.out, path), 'cook.json') }
176
+ }
177
+
178
+ /** Write the cook file again and its index row. */
179
+ function writeCook(copy: Readonly<Copy>, path: string, raw: JsonRecord): void {
180
+ const bytes = validationSourceBytes(raw)
181
+ copy.out.set(path, bytes)
182
+ Object.assign(record(copy.index['cook'], 'cook'), {
183
+ hash: copy.hash(bytes),
184
+ bytes: bytes.length,
185
+ })
186
+ }
187
+
188
+ /** Write each JSON mount a step changed; the old hash to the new row, by path. */
189
+ function writeMounts(
190
+ copy: Readonly<Copy>,
191
+ shard: Readonly<MigratingShard>,
192
+ mountText: ReadonlyMap<string, string>,
193
+ ): Map<string, Readonly<{ hash: string; bytes: number }>> {
194
+ const swapped = new Map<string, Readonly<{ hash: string; bytes: number }>>()
195
+ for (const [path, value] of shard.mounts) {
196
+ const text = JSON.stringify(value)
197
+ if (text !== mountText.get(path)) {
198
+ const bytes = new TextEncoder().encode(text)
199
+ copy.out.set(path, bytes)
200
+ swapped.set(path, { hash: copy.hash(bytes), bytes: bytes.length })
201
+ }
202
+ }
203
+ return swapped
204
+ }
205
+
206
+ /** Point each cook row at its mount's new bytes and each fence entry at the new hash. */
207
+ function swapCookRows(
208
+ raw: JsonRecord,
209
+ swapped: ReadonlyMap<string, Readonly<{ hash: string; bytes: number }>>,
210
+ ): void {
211
+ const hashes = new Map<string, string>()
212
+ for (const row of rows(raw['mounts'])) {
213
+ const now = swapped.get(String(row['path']))
214
+ if (now !== undefined) {
215
+ hashes.set(String(row['hash']), now.hash)
216
+ Object.assign(row, now)
217
+ }
218
+ }
219
+ const { fence } = raw
220
+ if (Array.isArray(fence)) {
221
+ raw['fence'] = fence.map((row: unknown): unknown =>
222
+ typeof row === 'string' ? (hashes.get(row) ?? row) : row,
223
+ )
224
+ }
225
+ }
226
+
227
+ /** Write each changed JSON mount, then its cook rows and its fence entry by the new hash. */
228
+ function rebuildMounts(
229
+ copy: Readonly<Copy>,
230
+ shard: Readonly<MigratingShard>,
231
+ mountText: ReadonlyMap<string, string>,
232
+ ): void {
233
+ const swapped = writeMounts(copy, shard, mountText)
234
+ if (swapped.size === NO_BYTES) {
235
+ return
236
+ }
237
+ const { path, raw } = cookFile(copy)
238
+ swapCookRows(raw, swapped)
239
+ writeCook(copy, path, raw)
240
+ }
241
+
242
+ /** Re-seal a prepared file's input hash over the rebuilt index and mounts; its fence and programs stand. */
243
+ function resealPreparation(copy: Readonly<Copy>): void {
244
+ const { path, raw } = cookFile(copy)
245
+ const cook = readCookData(raw)
246
+ const renderer = 'renderer' in cook ? cook.renderer : undefined
247
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the rebuilt index, read only for its preparation bytes
248
+ const index = copy.index as unknown as OutputIndex
249
+ raw['inputHash'] = copy.hash(
250
+ cookPreparationBytes(index, cook.mounts, renderer),
251
+ )
252
+ writeCook(copy, path, raw)
253
+ }
254
+
255
+ /** Sections, packs and the source hash, then the cook's JSON mounts and a data-only copy's re-sealed preparation. */
256
+ function rebuildInventory(
257
+ copy: Readonly<Copy>,
258
+ shard: Readonly<MigratingShard>,
259
+ cook: Readonly<CookPlan>,
260
+ ): void {
261
+ rebuildPacks(copy, rebuildSections(copy, shard))
262
+ rebuildSourceHash(copy, shard)
263
+ rebuildMounts(copy, shard, cook.mountText)
264
+ if (cook.reseal) {
265
+ resealPreparation(copy)
266
+ }
267
+ }
268
+
269
+ export { type CookPlan, type Copy, rebuildInventory }