From f8d1c9d5ba24aebd89fd8e222aa08c1c972ed602 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Fri, 22 Aug 2025 01:31:16 +0200 Subject: [PATCH] Added confirmation on import of old character data --- lang/en.json | 4 ++- module/documents/actor.mjs | 42 +++++++++++++++++------- module/helpers/utils.mjs | 11 +++++++ module/systemRegistration/migrations.mjs | 13 ++------ 4 files changed, 46 insertions(+), 24 deletions(-) diff --git a/lang/en.json b/lang/en.json index 7daf6b14..6a34037d 100755 --- a/lang/en.json +++ b/lang/en.json @@ -193,7 +193,9 @@ "companionLevelup": { "confirmTitle": "Companion Levelup", "confirmText": "Would you like to level up your companion {name} by {levelChange} levels at this time? (You can do it manually later)" - } + }, + "InvalidOldCharacterImportTitle": "Old Character Import", + "InvalidOldCharacterImportText": "Character data exported prior to system version 1.1 will not generate a complete character. Do you wish to continue?" }, "Companion": { "FIELDS": { diff --git a/module/documents/actor.mjs b/module/documents/actor.mjs index 4a6d2d67..e1dd93af 100644 --- a/module/documents/actor.mjs +++ b/module/documents/actor.mjs @@ -1,7 +1,7 @@ import { emitAsGM, GMUpdateEvent } from '../systemRegistration/socket.mjs'; import { LevelOptionType } from '../data/levelTier.mjs'; import DHFeature from '../data/item/feature.mjs'; -import { damageKeyToNumber } from '../helpers/utils.mjs'; +import { damageKeyToNumber, versionCompare } from '../helpers/utils.mjs'; import DhCompanionLevelUp from '../applications/levelup/companionLevelup.mjs'; export default class DhpActor extends Actor { @@ -27,7 +27,7 @@ export default class DhpActor extends Actor { /** @inheritDoc */ static migrateData(source) { - if(source.system?.attack && !source.system.attack.type) source.system.attack.type = "attack"; + if (source.system?.attack && !source.system.attack.type) source.system.attack.type = 'attack'; return super.migrateData(source); } @@ -571,19 +571,15 @@ export default class DhpActor extends Actor { if (armorSlotResult) { const { modifiedDamage, armorSpent, stressSpent } = armorSlotResult; updates.find(u => u.key === 'hitPoints').value = modifiedDamage; - if(armorSpent) { + if (armorSpent) { const armorUpdate = updates.find(u => u.key === 'armor'); - if(armorUpdate) - armorUpdate.value += armorSpent; - else - updates.push({ value: armorSpent, key: 'armor' }); + if (armorUpdate) armorUpdate.value += armorSpent; + else updates.push({ value: armorSpent, key: 'armor' }); } - if(stressSpent) { + if (stressSpent) { const stressUpdate = updates.find(u => u.key === 'stress'); - if(stressUpdate) - stressUpdate.value += stressSpent; - else - updates.push({ value: stressSpent, key: 'stress' }); + if (stressUpdate) stressUpdate.value += stressSpent; + else updates.push({ value: stressSpent, key: 'stress' }); } } } @@ -753,4 +749,26 @@ export default class DhpActor extends Actor { } } } + + /** @inheritdoc */ + async importFromJSON(json) { + if (!this.type === 'character') return await super.importFromJSON(json); + + if (!CONST.WORLD_DOCUMENT_TYPES.includes(this.documentName)) { + throw new Error('Only world Documents may be imported'); + } + + const parsedJSON = JSON.parse(json); + if (versionCompare(parsedJSON._stats.systemVersion, '1.1.0')) { + const confirmed = await foundry.applications.api.DialogV2.confirm({ + window: { + title: game.i18n.localize('DAGGERHEART.ACTORS.Character.InvalidOldCharacterImportTitle') + }, + content: game.i18n.localize('DAGGERHEART.ACTORS.Character.InvalidOldCharacterImportText') + }); + if (!confirmed) return; + } + + return await super.importFromJSON(json); + } } diff --git a/module/helpers/utils.mjs b/module/helpers/utils.mjs index 6f4e5a26..c8f4186f 100644 --- a/module/helpers/utils.mjs +++ b/module/helpers/utils.mjs @@ -420,3 +420,14 @@ export async function createEmbeddedItemsWithEffects(actor, baseData) { export const slugify = name => { return name.toLowerCase().replaceAll(' ', '-').replaceAll('.', ''); }; + +export const versionCompare = (current, target) => { + const currentSplit = current.split('.').map(x => Number.parseInt(x)); + const targetSplit = target.split('.').map(x => Number.parseInt(x)); + for (var i = 0; i < currentSplit.length; i++) { + if (currentSplit[i] < targetSplit[i]) return true; + if (currentSplit[i] > targetSplit[i]) return false; + } + + return false; +}; diff --git a/module/systemRegistration/migrations.mjs b/module/systemRegistration/migrations.mjs index 54ebc816..e84018fa 100644 --- a/module/systemRegistration/migrations.mjs +++ b/module/systemRegistration/migrations.mjs @@ -1,3 +1,5 @@ +import { versionCompare } from '../helpers/utils.mjs'; + export async function runMigrations() { let lastMigrationVersion = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.LastMigrationVersion); if (!lastMigrationVersion) lastMigrationVersion = '1.0.6'; @@ -37,14 +39,3 @@ export async function runMigrations() { await game.settings.set(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.LastMigrationVersion, lastMigrationVersion); } - -const versionCompare = (current, target) => { - const currentSplit = current.split('.').map(x => Number.parseInt(x)); - const targetSplit = target.split('.').map(x => Number.parseInt(x)); - for (var i = 0; i < currentSplit.length; i++) { - if (currentSplit[i] < targetSplit[i]) return true; - if (currentSplit[i] > targetSplit[i]) return false; - } - - return false; -};