From 394a6399d8e9fe5a54d4cf2e6274aecb00bf4b77 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sat, 4 Dec 2021 12:06:03 +0100 Subject: [PATCH] Update to Foundry V9 --- src/module.json | 4 +-- src/module/hooks/init.js | 9 +++-- src/module/hooks/renderTokenConfig.js | 2 +- src/module/hooks/updateScene.js | 2 +- src/module/hooks/updateToken.js | 6 ++-- src/module/wrappers/token.js | 36 +++++++++---------- ...ss-dependent-vision-config-form-groups.hbs | 4 +-- 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/module.json b/src/module.json index f4e7387..46fc0b0 100644 --- a/src/module.json +++ b/src/module.json @@ -10,8 +10,8 @@ "email": "johannes.loher@fg4f.de" } ], - "minimumCoreVersion": "0.8.8", - "compatibleCoreVersion": "0.8.9", + "minimumCoreVersion": "9.236", + "compatibleCoreVersion": "9", "esmodules": ["module/darkness-dependent-vision.js"], "languages": [ { diff --git a/src/module/hooks/init.js b/src/module/hooks/init.js index 5823778..4e4d0e3 100644 --- a/src/module/hooks/init.js +++ b/src/module/hooks/init.js @@ -7,7 +7,7 @@ import registerHandlebarsPartials from '../handlebars-partials'; import logger from '../logger'; import registerSettings from '../setiings'; import { libWrapper } from '../shims/libWrapperShim'; -import { getBrightRadius, getDimRadius, updateSource } from '../wrappers/token'; +import { getBrightRadius, getDimRadius, updateVisionSource } from '../wrappers/token'; export default function registerForInitHook() { Hooks.on('init', onInit); @@ -30,7 +30,12 @@ async function onInit() { logger.warn(`Failed to override ${brightRadiusTarget}, some things might not work correctly:`, e); } - libWrapper.register(packageName, 'Token.prototype.updateSource', updateSource, 'WRAPPER'); + const updateVisionSourceTarget = 'Token.prototype.updateVisionSource'; + try { + libWrapper.register(packageName, updateVisionSourceTarget, updateVisionSource, 'OVERRIDE'); + } catch (e) { + logger.warn(`Failed to override ${updateVisionSourceTarget}, some things might not work correctly:`, e); + } registerSettings(); await registerHandlebarsPartials(); diff --git a/src/module/hooks/renderTokenConfig.js b/src/module/hooks/renderTokenConfig.js index cf6d08b..8aa3763 100644 --- a/src/module/hooks/renderTokenConfig.js +++ b/src/module/hooks/renderTokenConfig.js @@ -16,6 +16,6 @@ async function onRenderTokenConfig(app, html, data) { 'modules/darkness-dependent-vision/templates/darkness-dependent-vision-config-form-groups.hbs', data, ); - html.find(`div[data-tab="vision"] .form-group input[name="brightSight"]`).parent().after(contents); + html.find(`div[data-tab="vision"] .form-group`).last().after(contents); app.setPosition({ height: 'auto' }); } diff --git a/src/module/hooks/updateScene.js b/src/module/hooks/updateScene.js index f9690d1..8b9b4eb 100644 --- a/src/module/hooks/updateScene.js +++ b/src/module/hooks/updateScene.js @@ -8,6 +8,6 @@ export default function registerForUpdateSceneHook() { function onUpdateScene(scene, change) { if (change.darkness != null) { - scene.getEmbeddedCollection('Token').forEach((tokenDocument) => tokenDocument.object.updateSource()); + scene.getEmbeddedCollection('Token').forEach((tokenDocument) => tokenDocument.object.updateVisionSource()); } } diff --git a/src/module/hooks/updateToken.js b/src/module/hooks/updateToken.js index b83c687..ebe601a 100644 --- a/src/module/hooks/updateToken.js +++ b/src/module/hooks/updateToken.js @@ -7,13 +7,13 @@ export default function registerForUpdateTokenHook() { } function onUpdateToken(token, change) { - const shouldUpdateSource = [ + const shouldUpdateVisionSource = [ 'dimVisionDarknessMin', 'dimVisionDarknessMax', 'brightVisionDarknessMin', 'brightVisionDarknessMax', ].some((flagKey) => `flags.darkness-dependent-vision.${flagKey}` in foundry.utils.flattenObject(change)); - if (shouldUpdateSource) { - token.object.updateSource(); + if (shouldUpdateVisionSource) { + token.object.updateVisionSource(); } } diff --git a/src/module/wrappers/token.js b/src/module/wrappers/token.js index 51c8e4a..ed1acb6 100644 --- a/src/module/wrappers/token.js +++ b/src/module/wrappers/token.js @@ -25,23 +25,19 @@ export function getBrightRadius() { } /** - * Update the light and vision source objects associated with this Token - * @typedef {({defer, deleted, noUpdateFog}?: {defer?: boolean, deleted?: boolean, noUpdateFog?: boolean}) => void} UpdateSourceFunction - * @param {UpdateSourceFunction} wrapped The function that is wrapped by this function and needs to be called next in the chain - * @param {boolean} [defer] Defer refreshing the SightLayer to manually call that refresh later. - * @param {boolean} [deleted] Indicate that this light source has been deleted. - * @param {boolean} [noUpdateFog] Never update the Fog exploration progress for this update. + * Update an Token vision source associated for this token. + * @param {boolean} [defer] Defer refreshing the LightingLayer to manually call that refresh later. + * @param {boolean} [deleted] Indicate that this vision source has been deleted. + * @param {boolean} [skipUpdateFog] Never update the Fog exploration progress for this update. */ -export function updateSource(wrapped, { defer = false, deleted = false, noUpdateFog = false } = {}) { - wrapped({ defer, deleted, noUpdateFog }); - - // Prepare some common data +export function updateVisionSource({ defer = false, deleted = false, skipUpdateFog = false } = {}) { + // Prepare data const origin = this.getSightOrigin(); const sourceId = this.sourceId; const d = canvas.dimensions; - - // Update vision source const isVisionSource = this._isVisionSource(); + + // Initialize vision source if (isVisionSource && !deleted) { const dimSight = getDimVision.call(this); const brightSight = getBrightVision.call(this); @@ -56,14 +52,16 @@ export function updateSource(wrapped, { defer = false, deleted = false, noUpdate rotation: this.data.rotation, }); canvas.sight.sources.set(sourceId, this.vision); - if (!defer) { - this.vision.drawLight(); - canvas.sight.refresh({ noUpdateFog }); - } - } else { - canvas.sight.sources.delete(sourceId); - if (isVisionSource && !defer) canvas.sight.refresh(); } + + // Remove vision source + else canvas.sight.sources.delete(sourceId); + + // Schedule a perception update + if (!defer && (isVisionSource || deleted)) + canvas.perception.schedule({ + sight: { refresh: true, skipUpdateFog }, + }); } /** diff --git a/src/templates/darkness-dependent-vision-config-form-groups.hbs b/src/templates/darkness-dependent-vision-config-form-groups.hbs index 7278b5b..e0d00e3 100644 --- a/src/templates/darkness-dependent-vision-config-form-groups.hbs +++ b/src/templates/darkness-dependent-vision-config-form-groups.hbs @@ -4,7 +4,7 @@ SPDX-FileCopyrightText: 2021 Johannes Loher SPDX-License-Identifier: MIT --}} -
+
@@ -19,7 +19,7 @@ SPDX-License-Identifier: MIT

{{ localize "DarknessDependentVision.DimVisionDarknessRangeHint" }}

-
+