From a99faad77c2d503275ae40ce39cae9ce5fd99690 Mon Sep 17 00:00:00 2001 From: supervj <64861570+supervj@users.noreply.github.com> Date: Thu, 11 Mar 2021 01:30:03 -0500 Subject: [PATCH] Code Cleanup and fix multiple migrations see above --- module/migration.js | 99 +++++++++------------------------------------ sw5e.js | 16 ++++---- system.json | 2 +- 3 files changed, 29 insertions(+), 88 deletions(-) diff --git a/module/migration.js b/module/migration.js index a44925f8..a628f346 100644 --- a/module/migration.js +++ b/module/migration.js @@ -268,10 +268,9 @@ function _updateNPCData(actor) { // check for flag.core, if not there is no compendium monster so exit const hasSource = actor?.flags?.core?.sourceId !== undefined; if (!hasSource) return actor; - // shortcut out if dataVersion flag is set to 1.2.4 + // shortcut out if dataVersion flag is set to 1.2.4 or higher const hasDataVersion = actor?.flags?.sw5e?.dataVersion !== undefined; - // TODO update check to do version checking - if ((hasDataVersion) && (actor.flags.sw5e.dataVersion === "1.2.4")) return actor; + if (hasDataVersion && (actor.flags.sw5e.dataVersion === "1.2.4" || isNewerVersion("1.2.4", actor.flags.sw5e.dataVersion))) return actor; // Check to see what the source of NPC is const sourceId = actor.flags.core.sourceId; const coreSource = sourceId.substr(0,sourceId.length-17); @@ -303,12 +302,12 @@ function _updateNPCData(actor) { } } - + // get actor to create new powers const liveActor = game.actors.get(actor._id); + // create the powers on the actor liveActor.createEmbeddedEntity("OwnedItem", newPowers); // set flag to check to see if migration has been done so we don't do it again. - // actor.flags.sw5e.dataVersion === "1.2.4"; liveActor.setFlag("sw5e", "dataVersion", "1.2.4"); }) } @@ -483,97 +482,38 @@ function _migrateItemClassPowerCasting(item, updateData) { async function _migrateItemPower(item, actor, updateData) { // if item is not a power shortcut out if (item.type !== "power") return updateData; + // check for flag.core, if not there is no compendium power so exit const hasSource = item?.flags?.core?.sourceId !== undefined; if (!hasSource) return updateData; - // shortcut out if dataVersion flag is set to 1.2.4 + + // shortcut out if dataVersion flag is set to 1.2.4 or higher const hasDataVersion = item?.flags?.sw5e?.dataVersion !== undefined; - // TODO update check to do version checking - if ((hasDataVersion) && (item.flags.sw5e.dataVersion === "1.2.4")) return updateData; + if (hasDataVersion && (item.flags.sw5e.dataVersion === "1.2.4" || isNewerVersion("1.2.4", item.flags.sw5e.dataVersion))) return updateData; + // Check to see what the source of Power is const sourceId = item.flags.core.sourceId; const coreSource = sourceId.substr(0, sourceId.length - 17); const core_id = sourceId.substr(sourceId.length - 16, 16); - if (coreSource === "Compendium.sw5e.techpowers") { - game.packs - .get("sw5e.techpowers") - .getEntity(core_id) - .then((corePower) => { - const coreData = corePower.data.data; - // copy Core Power Data over original Power - updateData["data"] = coreData; - updateData["flags"] = {"sw5e": {"dataVersion": "1.2.4"}}; - - // set flag to check to see if migration has been done so we don't do it again. - // item.flags.sw5e.dataVersion === "1.2.4"; - - return updateData; - }); - } - - if (coreSource === "Compendium.sw5e.forcepowers") { - const compendium = game.packs.get("sw5e.forcepowers"); - - const corePower = await compendium.getEntity(core_id) - const coreData = corePower.data.data; + + //if power type is not force or tech exit out + let powerType = "none"; + if (coreSource === "Compendium.sw5e.forcepowers") powerType = "sw5e.forcepowers"; + if (coreSource === "Compendium.sw5e.techpowers") powerType = "sw5e.techpowers"; + if (powerType === "none") return updateData; + game.packs.get(powerType).getEntity(core_id).then(corePower => { + const corePowerData = corePower.data.data; // copy Core Power Data over original Power - updateData["data"] = coreData; + updateData["data"] = corePowerData; updateData["flags"] = {"sw5e": {"dataVersion": "1.2.4"}}; - // set flag to check to see if migration has been done so we don't do it again. - // item.setFlag("sw5e", "dataVersion", "1.2.4"); - return updateData; - } + }) } /* -------------------------------------------- */ -/** - * @private - - function _migrateItemPower(item, actor, updateData) { - if (item.type === "power"){ - // check for flag.core, if not there is no compendium monster so exit - const hasSource = item?.flags?.core?.sourceId !== undefined; - if (!hasSource) return updateData; - // shortcut out if dataVersion flag is set to 1.2.4 - const hasDataVersion = item?.flags?.sw5e?.dataVersion !== undefined; - // TODO update check to do version checking - if ((hasDataVersion) && (item.flags.sw5e.dataVersion === "1.2.4")) return updateData; - // Check to see what the source of item is - const sourceId = item.flags.core.sourceId; - const coreSource = sourceId.substr(0,sourceId.length-17); - const core_id = sourceId.substr(sourceId.length-16,16); - if (coreSource === "Compendium.sw5e.forcepowers") { - game.packs.get("sw5e.forcepowers").getEntity(core_id).then(compPower => { - const powerData = compPower.data.data; - // Update all the item data - updateData["data"] = powerData; - - // set flag to check to see if migration has been done so we don't do it again. - item.flags.sw5e.dataVersion === "1.2.4"; - }); - - }else if (coreSource === "Compendium.sw5e.techpowers"){ - game.packs.get("sw5e.techpowers").getEntity(core_id).then(compPower => { - const powerData = compPower.data.data; - // Update all the item data - updateData["data"] = powerData; - - // set flag to check to see if migration has been done so we don't do it again. - item.flags.sw5e.dataVersion === "1.2.4"; - }) - } - - } - - return updateData; -} -*/ -/* -------------------------------------------- */ - /** * Delete the old data.attuned boolean * @private @@ -585,7 +525,6 @@ function _migrateItemAttunement(item, updateData) { return updateData; } - /* -------------------------------------------- */ /** diff --git a/sw5e.js b/sw5e.js index 3e626a6d..3413e708 100644 --- a/sw5e.js +++ b/sw5e.js @@ -196,16 +196,18 @@ Hooks.once("ready", function() { // Determine whether a system migration is required and feasible if ( !game.user.isGM ) return; const currentVersion = game.settings.get("sw5e", "systemMigrationVersion"); - const NEEDS_MIGRATION_VERSION = "R1-A1"; + const NEEDS_MIGRATION_VERSION = "1.2.4.R1-A4"; + // Check for R1 SW5E versions + const SW5E_NEEDS_MIGRATION_VERSION = "R1-A5"; const COMPATIBLE_MIGRATION_VERSION = 0.80; - const needsMigration = currentVersion && isNewerVersion(NEEDS_MIGRATION_VERSION, currentVersion); - //if ( !needsMigration ) return; + const needsMigration = currentVersion && (isNewerVersion(SW5E_NEEDS_MIGRATION_VERSION, currentVersion) || isNewerVersion(NEEDS_MIGRATION_VERSION, currentVersion)); + if ( !needsMigration ) return; // Perform the migration - //if ( currentVersion && isNewerVersion(COMPATIBLE_MIGRATION_VERSION, currentVersion) ) { - // const warning = `Your SW5e system data is from too old a Foundry version and cannot be reliably migrated to the latest version. The process will be attempted, but errors may occur.`; - // ui.notifications.error(warning, {permanent: true}); - //} + if ( currentVersion && isNewerVersion(COMPATIBLE_MIGRATION_VERSION, currentVersion) ) { + const warning = `Your SW5e system data is from too old a Foundry version and cannot be reliably migrated to the latest version. The process will be attempted, but errors may occur.`; + ui.notifications.error(warning, {permanent: true}); + } migrations.migrateWorld(); }); diff --git a/system.json b/system.json index c38206d8..b95e29d1 100644 --- a/system.json +++ b/system.json @@ -2,7 +2,7 @@ "name": "sw5e", "title": "SW 5th Edition", "description": "A comprehensive game system for running games of SW 5th Edition in the Foundry VTT environment.", - "version": "R1-A4", + "version": "1.2.4.R1-A5", "author": "Dev Team", "scripts": [], "esmodules": ["sw5e.js"],