From 30c0bf88946e8b069a91af03e3408150e8fc6f88 Mon Sep 17 00:00:00 2001 From: Shark that walks like a man <106829671+stwlam@users.noreply.github.com> Date: Fri, 1 Mar 2024 02:59:41 -0600 Subject: [PATCH] Construct actors and items with proxies in `preImportFromJSON` (#14024) --- src/module/actor/base.ts | 2 +- src/module/doc-helpers.ts | 21 +++++++++++---------- src/module/item/base/document.ts | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/module/actor/base.ts b/src/module/actor/base.ts index 3b87a24c099..4db42a26524 100644 --- a/src/module/actor/base.ts +++ b/src/module/actor/base.ts @@ -1655,7 +1655,7 @@ class ActorPF2e { - const processed = await preImportJSON(this, json); + const processed = await preImportJSON(json); return processed ? super.importFromJSON(processed) : this; } diff --git a/src/module/doc-helpers.ts b/src/module/doc-helpers.ts index e1a1215d245..f4758c19325 100644 --- a/src/module/doc-helpers.ts +++ b/src/module/doc-helpers.ts @@ -1,18 +1,15 @@ -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( - document: TDocument, - json: string, -): Promise { +async function preImportJSON(json: string): Promise { const source: unknown = JSON.parse(json); - if (!isObject(source)) return null; + if (!R.isPlainObject(source)) return null; if ("data" in source) { if ("items" in source) { ActorPF2e.migrateData(source); @@ -20,7 +17,9 @@ async function preImportJSON( 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; @@ -36,7 +35,9 @@ async function preImportJSON( return null; } - const newDoc = new (document.constructor as ConstructorOf)(source, { parent: document.parent }); + const Cls: ConstructorOf = + "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); diff --git a/src/module/item/base/document.ts b/src/module/item/base/document.ts index faca9801bdb..3cd05741bb5 100644 --- a/src/module/item/base/document.ts +++ b/src/module/item/base/document.ts @@ -503,7 +503,7 @@ class ItemPF2e extends Item /** Assess and pre-process this JSON data, ensuring it's importable and fully migrated */ override async importFromJSON(json: string): Promise { - const processed = await preImportJSON(this, json); + const processed = await preImportJSON(json); return processed ? super.importFromJSON(processed) : this; }