Skip to content

Commit

Permalink
Construct actors and items with proxies in preImportFromJSON (#14024)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Mar 1, 2024
1 parent 3cec0e9 commit 30c0bf8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/module/actor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ class ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | n

/** Assess and pre-process this JSON data, ensuring it's importable and fully migrated */
override async importFromJSON(json: string): Promise<this> {
const processed = await preImportJSON(this, json);
const processed = await preImportJSON(json);
return processed ? super.importFromJSON(processed) : this;
}

Expand Down
21 changes: 11 additions & 10 deletions src/module/doc-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { ActorPF2e } from "@actor";
import { ItemPF2e } from "@item";
import { ActorPF2e, ActorProxyPF2e } from "@actor";
import { ItemPF2e, ItemProxyPF2e } from "@item";
import type { TokenDocumentPF2e } from "@scene";
import { isObject } from "@util";
import * as R from "remeda";
import { CombatantPF2e } from "./encounter/index.ts";
import { MigrationList, MigrationRunner } from "./migration/index.ts";
import { MigrationRunnerBase } from "./migration/runner/base.ts";

/** Ensure that the import JSON is actually importable and that the data is fully migrated */
async function preImportJSON<TDocument extends ActorPF2e | ItemPF2e>(
document: TDocument,
json: string,
): Promise<string | null> {
async function preImportJSON(json: string): Promise<string | null> {
const source: unknown = JSON.parse(json);
if (!isObject<TDocument["_source"] & { data?: unknown }>(source)) return null;
if (!R.isPlainObject(source)) return null;
if ("data" in source) {
if ("items" in source) {
ActorPF2e.migrateData(source);
} else {
ItemPF2e.migrateData(source);
}
}
if (!isObject(source.system)) return null;
if (!R.isPlainObject(source.system) || !R.isPlainObject(source.system._migration)) {
return null;
}

const sourceSchemaVersion = Number(source.system?._migration?.version) || 0;
const worldSchemaVersion = MigrationRunnerBase.LATEST_SCHEMA_VERSION;
Expand All @@ -36,7 +35,9 @@ async function preImportJSON<TDocument extends ActorPF2e | ItemPF2e>(
return null;
}

const newDoc = new (document.constructor as ConstructorOf<TDocument>)(source, { parent: document.parent });
const Cls: ConstructorOf<ActorPF2e | ItemPF2e> =
"items" in source && Array.isArray(source.items) ? ActorProxyPF2e : ItemProxyPF2e;
const newDoc: ItemPF2e | ActorPF2e = new Cls(source);
const migrations = MigrationList.constructFromVersion(newDoc.schemaVersion);
await MigrationRunner.ensureSchemaVersion(newDoc, migrations);

Expand Down
2 changes: 1 addition & 1 deletion src/module/item/base/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ class ItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends Item

/** Assess and pre-process this JSON data, ensuring it's importable and fully migrated */
override async importFromJSON(json: string): Promise<this> {
const processed = await preImportJSON(this, json);
const processed = await preImportJSON(json);
return processed ? super.importFromJSON(processed) : this;
}

Expand Down

0 comments on commit 30c0bf8

Please sign in to comment.