Fixed some error in migration scene/actor

This commit is contained in:
Vlyan
2021-06-19 19:29:45 +02:00
parent 6d226a86d0
commit eddb99b9e7

View File

@@ -74,6 +74,9 @@ export class MigrationL5r5e {
if (!foundry.utils.isObjectEmpty(updateData)) { if (!foundry.utils.isObjectEmpty(updateData)) {
console.log(`L5R5E | Migrating Scene entity ${scene.name}`); console.log(`L5R5E | Migrating Scene entity ${scene.name}`);
await scene.update(updateData); await scene.update(updateData);
// If we do not do this, then synthetic token actors remain in cache
// with the un-updated actorData.
scene.tokens.contents.forEach((t) => (t._actor = null));
} }
} catch (err) { } catch (err) {
err.message = `L5R5E | Failed L5R5e system migration for Scene ${scene.name}: ${err.message}`; err.message = `L5R5E | Failed L5R5e system migration for Scene ${scene.name}: ${err.message}`;
@@ -187,24 +190,36 @@ export class MigrationL5r5e {
* @return {Object} The updateData to apply * @return {Object} The updateData to apply
*/ */
static _migrateSceneData(scene, options = { force: false }) { static _migrateSceneData(scene, options = { force: false }) {
const tokens = foundry.utils.duplicate(scene.tokens); const tokens = scene.tokens.map((token) => {
return { const t = token.toJSON();
tokens: tokens.map((t) => { if (!t.actorId || t.actorLink) {
if (!t.actorId || t.actorLink || !t.actorData.data) { t.actorData = {};
t.actorData = {}; } else if (!game.actors.has(t.actorId)) {
return t; t.actorId = null;
} t.actorData = {};
const token = new Token(t); } else if (!t.actorLink) {
if (!token.actor) { const actorData = foundry.utils.duplicate(t.actorData);
t.actorId = null; actorData.type = token.actor?.type;
t.actorData = {}; const update = MigrationL5r5e._migrateActorData(actorData, options);
} else if (!t.actorLink) { ["items", "effects"].forEach((embeddedName) => {
const updateData = MigrationL5r5e._migrateActorData(token.data.actorData, options); if (!update[embeddedName]?.length) {
t.actorData = foundry.utils.mergeObject(token.data.actorData, updateData); return;
} }
return t; const updates = new Map(update[embeddedName].map((u) => [u._id, u]));
}), t.actorData[embeddedName].forEach((original) => {
}; const update = updates.get(original._id);
if (update) {
foundry.utils.mergeObject(original, update);
}
});
delete update[embeddedName];
});
foundry.utils.mergeObject(t.actorData, update);
}
return t;
});
return { tokens };
} }
/** /**
@@ -218,6 +233,8 @@ export class MigrationL5r5e {
const updateData = {}; const updateData = {};
const actorData = actor.data; const actorData = actor.data;
// We need to be careful for unlinked tokens, only the diff is store in "data".
// ***** Start of 1.1.0 ***** // ***** Start of 1.1.0 *****
if (options?.force || MigrationL5r5e.needUpdate("1.1.0")) { if (options?.force || MigrationL5r5e.needUpdate("1.1.0")) {
// Add "Prepared" in actor // Add "Prepared" in actor
@@ -242,18 +259,19 @@ export class MigrationL5r5e {
// ***** Start of 1.3.0 ***** // ***** Start of 1.3.0 *****
if (options?.force || MigrationL5r5e.needUpdate("1.3.0")) { if (options?.force || MigrationL5r5e.needUpdate("1.3.0")) {
// PC/NPC removed notes useless props "value" // PC/NPC removed notes useless props "value"
updateData["data.notes"] = actorData.notes.value; if (actorData.notes?.value) {
updateData["data.notes"] = actorData.notes.value;
}
// NPC have now more thant a Strength and a Weakness // NPC have now more thant a Strength and a Weakness
if (actor.type === "npc" && actorData.rings_affinities) { if (actor.type === "npc" && actorData.rings_affinities?.strength) {
updateData["data.rings_affinities." + actorData.rings_affinities.strength.ring] = const aff = actorData.rings_affinities;
actorData.rings_affinities.strength.value; updateData["data.rings_affinities." + aff.strength.ring] = aff.strength.value;
updateData["data.rings_affinities." + actorData.rings_affinities.weakness.ring] = updateData["data.rings_affinities." + aff.weakness.ring] = aff.weakness.value;
actorData.rings_affinities.weakness.value;
// Delete old keys : not working :'( // Delete old keys
updateData["-=data.rings_affinities.strength"] = null; updateData["data.rings_affinities.-=strength"] = null;
updateData["-=data.rings_affinities.weakness"] = null; updateData["data.rings_affinities.-=weakness"] = null;
} }
} }
// ***** End of 1.3.0 ***** // ***** End of 1.3.0 *****