Files
Foundry-VTT-Docker/resources/app/dist/database/documents/token.mjs

1 line
4.9 KiB
JavaScript
Raw Normal View History

2025-01-04 00:34:03 +01:00
import BaseToken from"../../../common/documents/token.mjs";import ServerDocumentMixin from"../backend/server-document.mjs";import{getProperty,getType,hasProperty,setProperty}from"../../../common/utils/helpers.mjs";export default class Token extends(ServerDocumentMixin(BaseToken)){static _migrationRegistry=[...super._migrationRegistry,{fn:migrateLightConfig,version:12},{fn:migrateNegativeLightRadius,version:12},{fn:migrateGradualLightToAttenuation,version:12},{fn:migrateTextureData,version:12},{fn:migrateSight,version:12},{fn:migrateActorData,version:12},{fn:migrateZeroAngles,version:12},{fn:migrate2x2HexShape,version:12},{fn:migrateNonsquareTextureFit,version:12},{fn:migrateNullVisionRanges,version:12},{fn:migrateOcclusionRadiusFlag,version:12},{fn:migrateNegativeLuminosity,version:"12.319"},{fn:migrateTokenRing,version:"12.320"}];async _preUpdate(t,e,i){if(!1===await super._preUpdate(t,e,i))return!1;("x"in t||"y"in t||"elevation"in t)&&(e._priorPosition??={},e._priorPosition[this.id]={x:this.x,y:this.y,elevation:this.elevation}),"_regions"in t&&(e._priorRegions??={},e._priorRegions[this.id]=this._regions)}_onUpdate(t,e,i){if(super._onUpdate(t,e,i),"object"==typeof e.worldTime){const{delta:t,...r}=e.worldTime;Number.isNumeric(t)&&db.Setting.advanceTime(t,r,i)}}async loadRelatedDocuments(){this.baseActor=await db.Actor.get(this.actorId),this.delta||await this.recreateActorDelta();for(const t of Object.values(this.delta.collections))t.initialize({full:!0});this.actorLink?this.actor=this.baseActor:this.actor=this.delta?.constructor.applyDelta(this.delta,this.baseActor)}async recreateActorDelta(){return this.updateSource({delta:new db.ActorDelta({_id:this.id},{parent:this}).toObject()}),this.save()}}function migrateLightConfig(t){let e=!1;const i={dimLight:"light.dim",brightLight:"light.bright",lightAngle:"light.angle",lightColor:"light.color",lightAlpha:"light.alpha",lightAnimation:"light.animation"};for(const[r,a]of Object.entries(i)){const i=Token._addDataFieldMigration(t,r,a);e||=i}return e}function migrateNegativeLightRadius(t){const e=t.light;if("Object"!==getType(e))return!1;let i=!1;return e.dim<0&&(e.dim=Math.abs(e.dim),i=!0),e.bright<0&&(e.bright=Math.abs(e.bright),i=!0),!!i&&(e.luminosity=-1*Math.abs(e.luminosity??.5),!0)}function migrateGradualLightToAttenuation(t){const e=t.light;return"Object"===getType(e)&&("gradual"in e&&(e.attenuation=e.gradual?.5:.3,delete e.gradual,!0))}function migrateTextureData(t){const e=Token._addDataFieldMigration(t,"img","texture.src"),i=Token._addDataFieldMigration(t,"tint","texture.tint");let r=!1,a=t.texture?.scaleX??1,n=t.texture?.scaleY??1;"scale"in t&&(a=n=t.scale,setProperty(t,"texture.scaleX",t.scale),setProperty(t,"texture.scaleY",t.scale),delete t.scale,r=!0);const o=Token._addDataFieldMigration(t,"mirrorX","texture.scaleX",(t=>t.mirrorX?-Math.abs(a):Math.abs(a))),s=Token._addDataFieldMigration(t,"mirrorY","texture.scaleY",(t=>t.mirrorY?-Math.abs(n):Math.abs(n)));return e||i||r||o||s}function migrateSight(t){const e=Token._addDataFieldMigration(t,"sightAngle","sight.angle"),i=Token._addDataFieldMigration(t,"vision","sight.enabled");let r=!1;if("dimSight"in t||"brightSight"in t){const e=t.dimSight??0,i=t.brightSight??0,a=Math.max(e,i);setProperty(t,"sight.range",a),delete t.dimSight,delete t.brightSight,setProperty(t,"sight.brightness",i>=e?1:0),r=!0}return e||i||r}function migrateActorData(t){return"actorData"in t&&("delta"in t||(t.delta=t.actorData,t._id&&"Object"===getType(t.delta)&&(t.delta._id=t._id)),delete t.actorData,!0)}function migrateZeroAngles(t){let e=!1;return"Object"===getType(t.light)&&0===t.light.angle&&(t.light.angle=360,e=!0),"Object"===getType(t.sight)&&0===t.sight.angle&&(t.sight.angle=360,e=!0),e}function migrate2x2HexShape(t){2===t.width&&2===t.height&&(t.hexagonalShape=CONST.TOKEN_HEXAGONAL_SHAPES.ELLIPSE_2)}function migrateNonsquareTextureFit(t){let e;const i=t.width||1,r=t.height||1;if(i<r)e="height";else{if(!(i>r))return!1;e="width"}return setProperty(t,"texture.fit",e),!0}function migrateNullVisionRanges(t){let e=!1;if("Object"===getType(t.sight