@arcadia-eternity/content-sdk 4.8.0 → 4.8.1

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.
@@ -446,7 +446,6 @@ export type StaticPreparer = (input: {
446
446
  elementCatalog?: ReturnType<typeof readElementCatalog>;
447
447
  }>;
448
448
  export interface ContentWorkspaceOptions {
449
- includeBuiltin120?: boolean;
450
449
  allowShareDownloads?: boolean;
451
450
  bundledPackageRoot?: string;
452
451
  npmCache?: string;
@@ -490,7 +489,7 @@ export interface EnvironmentExport {
490
489
  }
491
490
  export interface WorkspaceSnapshot {
492
491
  format: 'arcadia.workspace-snapshot/2';
493
- builtin120EnvironmentId?: string;
492
+ builtinEnvironmentIds?: Readonly<Record<string, string>>;
494
493
  workspaceId: string;
495
494
  drafts: (EnvironmentDraft & {
496
495
  files?: {
@@ -519,7 +518,7 @@ export interface ContentWorkspace {
519
518
  readonly root: string;
520
519
  readonly workspaceId: string;
521
520
  readonly bootId: string;
522
- readonly builtin120EnvironmentId?: string;
521
+ readonly builtinEnvironmentIds?: Readonly<Record<string, string>>;
523
522
  listDrafts(): Promise<EnvironmentDraft[]>;
524
523
  getDraft(environmentId: string): Promise<EnvironmentDraft>;
525
524
  saveDraft(input: {
@@ -573,9 +572,12 @@ export interface ContentWorkspace {
573
572
  }): Promise<void>;
574
573
  cleanupStaging(): Promise<string[]>;
575
574
  }
575
+ export interface OwnedContentWorkspace extends ContentWorkspace {
576
+ registerBuiltinEnvironments(keys: string[], adopted: Record<string, string>): Promise<Readonly<Record<string, string>>>;
577
+ }
576
578
  export interface ContentWorkspaceConstructor {
577
579
  new (root: string, options?: ContentWorkspaceOptions): ContentWorkspace;
578
- open(directory: string, options?: ContentWorkspaceOptions): Promise<ContentWorkspace>;
580
+ open(directory: string, options?: ContentWorkspaceOptions): Promise<OwnedContentWorkspace>;
579
581
  }
580
582
  export interface EnvironmentMember {
581
583
  archiveIntegrity?: string;
@@ -605,5 +607,165 @@ export type RemoveEnvironmentPackageInput = {
605
607
  type PackageWorkspace = Pick<ContentWorkspace, 'listDrafts' | 'listInstallations' | 'saveDraft'>;
606
608
  export declare function addEnvironmentPackage(workspace: PackageWorkspace, input: AddEnvironmentPackageInput): Promise<EnvironmentDraft>;
607
609
  export declare function removeEnvironmentPackage(workspace: PackageWorkspace, input: RemoveEnvironmentPackageInput): Promise<EnvironmentDraft>;
608
- /** Materialize the shipped preset with the host's own ID and bundled base project. */
609
- export declare function prepareBuiltin120Environment(workspace: ContentWorkspace, projectDirectory: string, rules: unknown, validate: StaticPreparer): Promise<void>;
610
+ /** The host supplies this catalog from the application distribution. */
611
+ export declare function readShippedEnvironments(file: string | URL, bundledVersions: Record<string, string>): Promise<{
612
+ defaultEnvironment: string;
613
+ environments: {
614
+ id: string;
615
+ name: string;
616
+ packages: Record<string, string>;
617
+ sources: string[];
618
+ schemaVersion: 2;
619
+ rules: {
620
+ schemaVersion: 1 | 2;
621
+ mechanics?: {
622
+ skillSlots?: {
623
+ normalMax: number;
624
+ climaxMax: number;
625
+ climaxMin?: number | undefined;
626
+ } | undefined;
627
+ individualValueDefault?: number | undefined;
628
+ individualValues?: {
629
+ max: number;
630
+ } | undefined;
631
+ allowFaintSwitch?: boolean | undefined;
632
+ inheritance?: {
633
+ eligible: "all" | "off" | "species" | string[];
634
+ pool?: {
635
+ mode: "append" | "fallback" | "replace";
636
+ skills: string[];
637
+ } | undefined;
638
+ } | undefined;
639
+ enhancement?: {
640
+ mode: "active" | "forbidden" | "suppressed";
641
+ stages?: string[] | undefined;
642
+ species?: string[] | undefined;
643
+ } | undefined;
644
+ secondGenerationNatures?: "all" | "off" | "species" | string[] | undefined;
645
+ } | undefined;
646
+ skillParameters?: {
647
+ profile?: string | undefined;
648
+ groups?: Record<string, number> | undefined;
649
+ overrides?: {
650
+ skill: string;
651
+ useId: string;
652
+ parameter: string;
653
+ value: number;
654
+ }[] | undefined;
655
+ } | undefined;
656
+ matchups?: {
657
+ table?: string | undefined;
658
+ adjustments?: {
659
+ attack: string;
660
+ defense: string;
661
+ multiplier: number;
662
+ }[] | undefined;
663
+ } | undefined;
664
+ learnsetAdjustments?: {
665
+ species: string;
666
+ source: "inherited" | "normal";
667
+ skill: string;
668
+ action: "add" | "remove";
669
+ level?: number | undefined;
670
+ }[] | undefined;
671
+ tier?: "standard" | undefined;
672
+ allow?: {
673
+ species?: string[] | undefined;
674
+ skills?: string[] | undefined;
675
+ marks?: string[] | undefined;
676
+ } | undefined;
677
+ deny?: {
678
+ species?: string[] | undefined;
679
+ skills?: string[] | undefined;
680
+ marks?: string[] | undefined;
681
+ } | undefined;
682
+ team?: {
683
+ minSize?: number | undefined;
684
+ maxSize?: number | undefined;
685
+ uniqueSpecies?: boolean | undefined;
686
+ } | undefined;
687
+ constraints?: ({
688
+ id: string;
689
+ allOf: {
690
+ kind: "mark" | "skill" | "species";
691
+ ids: string[];
692
+ fromCollections?: string[] | undefined;
693
+ }[];
694
+ kind: "ban";
695
+ scope: "pet" | "team";
696
+ exceptWhen?: {
697
+ kind: "mark" | "skill" | "species";
698
+ ids: string[];
699
+ fromCollections?: string[] | undefined;
700
+ }[] | undefined;
701
+ exceptAny?: {
702
+ kind: "mark" | "skill" | "species";
703
+ ids: string[];
704
+ fromCollections?: string[] | undefined;
705
+ }[][] | undefined;
706
+ } | {
707
+ id: string;
708
+ allOf: {
709
+ kind: "mark" | "skill" | "species";
710
+ ids: string[];
711
+ fromCollections?: string[] | undefined;
712
+ }[];
713
+ kind: "memberLimit";
714
+ max: number;
715
+ })[] | undefined;
716
+ collections?: {
717
+ id: string;
718
+ kind: "mark" | "skill" | "species";
719
+ ids: string[];
720
+ }[] | undefined;
721
+ include?: string[] | undefined;
722
+ constraintEdits?: {
723
+ remove?: string[] | undefined;
724
+ replace?: ({
725
+ id: string;
726
+ allOf: {
727
+ kind: "mark" | "skill" | "species";
728
+ ids: string[];
729
+ fromCollections?: string[] | undefined;
730
+ }[];
731
+ kind: "ban";
732
+ scope: "pet" | "team";
733
+ exceptWhen?: {
734
+ kind: "mark" | "skill" | "species";
735
+ ids: string[];
736
+ fromCollections?: string[] | undefined;
737
+ }[] | undefined;
738
+ exceptAny?: {
739
+ kind: "mark" | "skill" | "species";
740
+ ids: string[];
741
+ fromCollections?: string[] | undefined;
742
+ }[][] | undefined;
743
+ } | {
744
+ id: string;
745
+ allOf: {
746
+ kind: "mark" | "skill" | "species";
747
+ ids: string[];
748
+ fromCollections?: string[] | undefined;
749
+ }[];
750
+ kind: "memberLimit";
751
+ max: number;
752
+ })[] | undefined;
753
+ clearExceptions?: string[] | undefined;
754
+ exceptions?: {
755
+ target: string;
756
+ allOf: {
757
+ kind: "mark" | "skill" | "species";
758
+ ids: string[];
759
+ fromCollections?: string[] | undefined;
760
+ }[];
761
+ }[] | undefined;
762
+ } | undefined;
763
+ };
764
+ requires: {
765
+ engine?: string | undefined;
766
+ sdk?: string | undefined;
767
+ capabilities?: string[] | undefined;
768
+ };
769
+ }[];
770
+ }>;
771
+ export declare function synchronizeShippedEnvironments(workspace: OwnedContentWorkspace, catalog: Awaited<ReturnType<typeof readShippedEnvironments>>, adopted: Record<string, string>, prepare: (document: EnvironmentDocument, expectedRevision: string | null) => Promise<unknown>): Promise<string>;
@@ -47,7 +47,6 @@ function exportRevision(draft, content, dependencies) {
47
47
  return sha256Hex(JSON.stringify([draft, content, dependencies]));
48
48
  }
49
49
  const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
50
- const builtin120IdPattern = new RegExp('^builtin-120-' + uuid.source.slice(1));
51
50
  const envId = /^[a-z0-9][a-z0-9_-]{0,79}$/;
52
51
  const json = (value) => JSON.stringify(value, null, 2) + '\n';
53
52
  const checkId = (id, pattern = uuid) => {
@@ -55,6 +54,21 @@ const checkId = (id, pattern = uuid) => {
55
54
  throw contentError('RESOURCE_ID_INVALID');
56
55
  return id;
57
56
  };
57
+ // 4.8.0 wrote a single slot in both workspace metadata and backups.
58
+ function readBuiltinEnvironmentIds(value) {
59
+ if ('builtinEnvironmentIds' in value && value.builtinEnvironmentIds !== undefined) {
60
+ const entries = value.builtinEnvironmentIds;
61
+ if (!isRecord(entries))
62
+ throw contentError('WORKSPACE_FORMAT_INVALID');
63
+ const ids = Object.entries(entries).map(([key, id]) => [checkId(key, envId), checkId(id, envId)]);
64
+ if (new Set(ids.map(([, id]) => id)).size !== ids.length)
65
+ throw contentError('WORKSPACE_FORMAT_INVALID');
66
+ return Object.fromEntries(ids);
67
+ }
68
+ return 'builtin120EnvironmentId' in value && value.builtin120EnvironmentId !== undefined
69
+ ? { 'training-120': checkId(value.builtin120EnvironmentId, new RegExp('^builtin-120-' + uuid.source.slice(1))) }
70
+ : {};
71
+ }
58
72
  async function regularDirectory(path) {
59
73
  await mkdir(path, { recursive: true, mode: 0o700 });
60
74
  const info = await lstat(path);
@@ -117,7 +131,7 @@ class ContentWorkspaceImplementation {
117
131
  root;
118
132
  bootId = randomUUID();
119
133
  workspaceId;
120
- builtin120EnvironmentId;
134
+ builtinEnvironmentIds = {};
121
135
  #queue = Promise.resolve();
122
136
  #closed = false;
123
137
  #controller = new AbortController();
@@ -161,11 +175,8 @@ class ContentWorkspaceImplementation {
161
175
  else {
162
176
  workspace.workspaceId = randomUUID();
163
177
  }
164
- if (metadata?.builtin120EnvironmentId !== undefined)
165
- workspace.builtin120EnvironmentId = checkId(metadata.builtin120EnvironmentId, builtin120IdPattern);
166
- else if (options.includeBuiltin120)
167
- workspace.builtin120EnvironmentId = 'builtin-120-' + randomUUID();
168
- if (!metadata || workspace.builtin120EnvironmentId !== metadata.builtin120EnvironmentId)
178
+ workspace.builtinEnvironmentIds = readBuiltinEnvironmentIds(metadata ?? {});
179
+ if (!metadata || 'builtin120EnvironmentId' in metadata)
169
180
  await workspace.#saveMetadata();
170
181
  // Recover an interrupted directory replacement before exposing projects.
171
182
  for (const name of await readdir(join(root, 'staging'))) {
@@ -190,11 +201,28 @@ class ContentWorkspaceImplementation {
190
201
  throw error;
191
202
  }
192
203
  }
193
- async #saveMetadata() {
204
+ registerBuiltinEnvironments(keys, adopted) {
205
+ return this.#exclusive(async () => {
206
+ const entries = keys.map(key => {
207
+ checkId(key, envId);
208
+ const previous = Object.hasOwn(this.builtinEnvironmentIds, key) ? this.builtinEnvironmentIds[key] : undefined;
209
+ const legacy = Object.hasOwn(adopted, key) ? checkId(adopted[key], envId) : undefined;
210
+ const id = previous ?? legacy ?? 'builtin-' + randomUUID();
211
+ return [key, id];
212
+ });
213
+ const ids = readBuiltinEnvironmentIds({ builtinEnvironmentIds: Object.fromEntries(entries) });
214
+ if (!isDeepStrictEqual(ids, this.builtinEnvironmentIds)) {
215
+ await this.#saveMetadata(ids);
216
+ this.builtinEnvironmentIds = ids;
217
+ }
218
+ return ids;
219
+ }, true);
220
+ }
221
+ async #saveMetadata(ids = this.builtinEnvironmentIds) {
194
222
  await writeFileAtomic(join(this.root, 'workspace.json'), json({
195
223
  format: 'arcadia.content-workspace/2',
196
224
  workspaceId: this.workspaceId,
197
- builtin120EnvironmentId: this.builtin120EnvironmentId,
225
+ builtinEnvironmentIds: ids,
198
226
  }));
199
227
  }
200
228
  async #assertOwner() {
@@ -635,7 +663,7 @@ class ContentWorkspaceImplementation {
635
663
  return {
636
664
  format: 'arcadia.workspace-snapshot/2',
637
665
  workspaceId: this.workspaceId,
638
- builtin120EnvironmentId: this.builtin120EnvironmentId,
666
+ builtinEnvironmentIds: this.builtinEnvironmentIds,
639
667
  drafts,
640
668
  installations,
641
669
  };
@@ -670,9 +698,7 @@ class ContentWorkspaceImplementation {
670
698
  !Array.isArray(snapshot.drafts) ||
671
699
  !Array.isArray(snapshot.installations))
672
700
  throw contentError('WORKSPACE_SNAPSHOT_INVALID');
673
- const builtin120EnvironmentId = snapshot.builtin120EnvironmentId === undefined
674
- ? this.builtin120EnvironmentId
675
- : checkId(snapshot.builtin120EnvironmentId, builtin120IdPattern);
701
+ const builtinEnvironmentIds = readBuiltinEnvironmentIds(snapshot);
676
702
  if ((await this.#listDrafts()).length)
677
703
  throw contentError('WORKSPACE_NOT_EMPTY');
678
704
  const drafts = new Map(snapshot.drafts.map(draft => {
@@ -730,7 +756,7 @@ class ContentWorkspaceImplementation {
730
756
  throw contentError('WORKSPACE_SNAPSHOT_INVALID');
731
757
  }
732
758
  this.workspaceId = mode === 'restore' ? checkId(snapshot.workspaceId, uuid) : this.workspaceId;
733
- this.builtin120EnvironmentId = builtin120EnvironmentId;
759
+ this.builtinEnvironmentIds = builtinEnvironmentIds;
734
760
  await this.#saveMetadata();
735
761
  return { workspaceId: this.workspaceId, installationIds: Object.fromEntries([...ids].map(id => [id, id])) };
736
762
  }, true);
@@ -976,30 +1002,40 @@ async function changeEnvironmentPackage(workspace, input) {
976
1002
  }
977
1003
  return workspace.saveDraft({ document: normalizeEnvironmentDocument(document), expectedRevision });
978
1004
  }
979
- /** Materialize the shipped preset with the host's own ID and bundled base project. */
980
- export async function prepareBuiltin120Environment(workspace, projectDirectory, rules, validate) {
981
- const id = workspace.builtin120EnvironmentId;
982
- if (!id)
983
- throw contentError('BUILTIN_ENVIRONMENT_ID_MISSING');
984
- const base = '@arcadia-eternity/data-pack-base';
985
- const { version } = await readJson(join(projectDirectory, 'node_modules', base, 'package.json'));
986
- const document = normalizeEnvironmentDocument({
987
- schemaVersion: 2,
988
- id,
989
- name: '120 资质 · 神魔化与二代',
990
- packages: { [base]: version },
991
- sources: [],
992
- rules,
993
- requires: {},
994
- });
995
- const current = await workspace.getDraft(id).catch(error => {
996
- if (errorCode(error) !== 'ENOENT')
997
- throw error;
998
- });
999
- if (current && isDeepStrictEqual(current.document, document)) {
1000
- const [installed] = await workspace.listInstallations(id);
1001
- if (installed?.status === 'ready' && installed.draftRevision === current.revision)
1002
- return;
1005
+ /** The host supplies this catalog from the application distribution. */
1006
+ export async function readShippedEnvironments(file, bundledVersions) {
1007
+ const schema = z
1008
+ .object({
1009
+ defaultEnvironment: z.string(),
1010
+ environments: z.array(EnvironmentDocumentV2Schema).min(1),
1011
+ })
1012
+ .strict();
1013
+ const catalog = schema.parse(JSON.parse(await readFile(file, 'utf8')));
1014
+ const keys = new Set(catalog.environments.map(doc => doc.id));
1015
+ if (keys.size !== catalog.environments.length || !keys.has(catalog.defaultEnvironment))
1016
+ throw contentError('SHIPPED_ENVIRONMENT_CATALOG_INVALID');
1017
+ for (const { packages } of catalog.environments)
1018
+ for (const [name, version] of Object.entries(bundledVersions))
1019
+ if (Object.hasOwn(packages, name)) {
1020
+ if (!semver.satisfies(version, packages[name]))
1021
+ throw contentError('SHIPPED_PACKAGE_VERSION_MISMATCH');
1022
+ packages[name] = version;
1023
+ }
1024
+ return catalog;
1025
+ }
1026
+ export async function synchronizeShippedEnvironments(workspace, catalog, adopted, prepare) {
1027
+ const ids = await workspace.registerBuiltinEnvironments(catalog.environments.map(doc => doc.id), adopted);
1028
+ const drafts = await workspace.listDrafts();
1029
+ for (const definition of catalog.environments) {
1030
+ const id = ids[definition.id];
1031
+ const document = { ...definition, id };
1032
+ const current = drafts.find(item => item.document.id === id);
1033
+ if (current && isDeepStrictEqual(current.document, document)) {
1034
+ const [installed] = await workspace.listInstallations(id);
1035
+ if (installed?.status === 'ready' && installed.draftRevision === current.revision)
1036
+ continue;
1037
+ }
1038
+ await prepare(document, current?.revision ?? null);
1003
1039
  }
1004
- await workspace.prepareEnvironment({ environmentId: id, document, expectedRevision: current?.revision ?? null, projectDirectory }, validate);
1040
+ return ids[catalog.defaultEnvironment];
1005
1041
  }
@@ -21,7 +21,7 @@
21
21
  "devDependencies": {
22
22
  "@types/node": "26.6.6",
23
23
  "typescript": "6.0.3",
24
- "@arcadia-eternity/content-sdk": "4.8.0",
24
+ "@arcadia-eternity/content-sdk": "4.8.1",
25
25
  "fflate": "0.8.3"
26
26
  },
27
27
  "arcadia": {
@@ -38,6 +38,6 @@
38
38
  "license": "MIT",
39
39
  "engines": {
40
40
  "node": ">=24 <25",
41
- "arcadia": "^4.8.0"
41
+ "arcadia": "^4.8.1"
42
42
  }
43
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcadia-eternity/content-sdk",
3
- "version": "4.8.0",
3
+ "version": "4.8.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "engines": {
@@ -63,10 +63,10 @@
63
63
  "zod": "4.6.7",
64
64
  "fflate": "0.8.3",
65
65
  "simple-git": "4.1.0",
66
- "@arcadia-eternity/battle": "4.8.0",
67
- "@arcadia-eternity/const": "4.8.0",
68
- "@arcadia-eternity/pack-loader": "4.8.0",
69
- "@arcadia-eternity/schema": "4.8.0"
66
+ "@arcadia-eternity/battle": "4.8.1",
67
+ "@arcadia-eternity/schema": "4.8.1",
68
+ "@arcadia-eternity/pack-loader": "4.8.1",
69
+ "@arcadia-eternity/const": "4.8.1"
70
70
  },
71
71
  "repository": {
72
72
  "type": "git",