-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
hooks.js
452 lines (396 loc) · 14.1 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import Tokenizer from "./tokenizer/Tokenizer.js";
import DirectoryPicker from "./libs/DirectoryPicker.js";
import Utils from "./libs/Utils.js";
import logger from "./libs/logger.js";
import View from "./tokenizer/View.js";
import AutoTokenize from "./tokenizer/AutoTokenize.js";
import CONSTANTS from "./constants.js";
import { registerSettings } from "./settings.js";
export function init() {
registerSettings();
}
function getAvatarKey() {
let dataEditField;
switch (game.system.id) {
case "yzecoriolis": {
if (foundry.utils.isNewerVersion("3.2.0", game.system.version)) {
dataEditField = "system.keyArt";
} else {
dataEditField = "img";
}
break;
}
default:
dataEditField = "img";
}
return dataEditField;
}
function getAvatarPath(actor) {
const key = getAvatarKey();
return foundry.utils.getProperty(actor, key);
}
/**
* Launch the tokenizer
* Options include
* name: name to use as part of filename identifier
* type: pc, npc - defaults to pc
* avatarFilename: current avatar image - defaults to null/mystery man
* tokenFilename: current tokenImage - defaults to null/mystery man
* isWildCard: is wildcard token?
* any other items needed in callback function, options will be passed to callback, with filenames updated to new references
* @param {*} options
* @param {*} callback function to pass return object to
*/
function launchTokenizer(options, callback) {
if (!game.user.can("FILES_UPLOAD")) {
ui.notifications.warn(game.i18n.localize(`${CONSTANTS.MODULE_ID}.requires-upload-permission`));
if (game.settings.get(CONSTANTS.MODULE_ID, "disable-player")) return;
}
game.canvas.layers.forEach((layer) => {
layer._copy = [];
});
logger.debug("Tokenizer options", options);
const tokenizer = new Tokenizer(options, callback);
tokenizer.render(true);
}
async function updateActor(tokenizerResponse) {
logger.debug("Updating Actor, tokenizer data", tokenizerResponse);
const dateTag = `${+new Date()}`;
// updating the avatar filename
const update = {};
const avatarKey = getAvatarKey();
update[avatarKey] = tokenizerResponse.avatarFilename.split("?")[0] + "?" + dateTag;
if (!tokenizerResponse.actor.prototypeToken.randomImg) {
// for non-wildcard tokens, we set the token img now
const tokenPath = tokenizerResponse.tokenFilename.split("?")[0] + "?" + dateTag;
foundry.utils.setProperty(update, "prototypeToken.texture.src", tokenPath);
} else if (tokenizerResponse.actor.prototypeToken.texture.src.indexOf("*") === -1) {
// if it is a wildcard and it isn't get like one, we change that
const actorName = tokenizerResponse.actor.name.replace(/[^\w.]/gi, "_").replace(/__+/g, "");
const options = DirectoryPicker.parse(tokenizerResponse.tokenUploadDirectory);
// set it to a wildcard we can actually use
const imageFormat = game.settings.get(CONSTANTS.MODULE_ID, "image-save-type");
const message = game.i18n.format("vtta-tokenizer.notification.wildcard", { path: tokenizerResponse.actor.prototypeToken.texture.src });
ui.notifications.info(message);
update.token = {
img: `${options.current}/${actorName}.Token-*.${imageFormat}`,
};
}
if (game.settings.get(CONSTANTS.MODULE_ID, "auto-apply-dynamic-token-ring")) {
if (game.release.generation < 12) {
if (update.prototypeToken) {
foundry.utils.setProperty(update.prototypeToken, "flags.dnd5e.tokenRing.enabled", true);
}
if (update.token) {
foundry.utils.setProperty(update.token, "flags.dnd5e.tokenRing.enabled", true);
}
} else {
if (update.prototypeToken) {
foundry.utils.setProperty(update.prototypeToken, "ring.enabled", true);
}
if (update.token) {
foundry.utils.setProperty(update.token, "ring.enabled", true);
}
}
}
logger.debug("Updating with", update);
await tokenizerResponse.actor.update(update);
// if there is a scene token, lets update it
if (tokenizerResponse.token) {
tokenizerResponse.token.update(update.prototypeToken);
}
}
function getActorType(actor) {
if (["character", "pc"].includes(actor.type)) {
// forbidden lands support
if (foundry.utils.getProperty(actor, "system.subtype.type") === "npc") {
return "npc";
} else {
return "pc";
}
} else {
return "npc";
}
}
function tokenizeActor(actor) {
const options = {
actor: actor,
name: actor.name,
type: getActorType(actor),
disposition: actor.prototypeToken.disposition,
avatarFilename: getAvatarPath(actor),
tokenFilename: actor.prototypeToken.texture.src,
isWildCard: actor.prototypeToken.randomImg,
};
launchTokenizer(options, updateActor);
}
function tokenizeSceneToken(doc) {
const options = {
actor: doc.actor,
token: doc.token,
name: doc.token.name,
type: getActorType(doc.actor),
disposition: doc.token.disposition,
avatarFilename: getAvatarPath(doc.actor),
tokenFilename: doc.token.texture.src,
nameSuffix: `${doc.token.id}`,
};
launchTokenizer(options, updateActor);
}
function tokenizeDoc(doc) {
if (doc.token) {
tokenizeSceneToken(doc);
} else {
tokenizeActor(doc);
}
}
async function updateSceneTokenImg(actor) {
const updates = await Promise.all(actor.getActiveTokens().map(async (t) => {
const newToken = await actor.getTokenDocument();
const tokenUpdate = {
_id: t.id,
"texture.src": newToken.texture.src,
};
return tokenUpdate;
}));
if (updates.length) canvas.scene.updateEmbeddedDocuments("Token", updates);
}
export async function autoToken(actor, options) {
const defaultOptions = {
actor: actor,
name: actor.name,
type: getActorType(actor),
disposition: actor.prototypeToken.disposition,
avatarFilename: getAvatarPath(actor),
tokenFilename: actor.prototypeToken.texture.src,
isWildCard: actor.prototypeToken.randomImg,
auto: true,
updateActor: true,
};
const mergedOptions = foundry.utils.mergeObject(defaultOptions, options);
const tokenizer = new Tokenizer(mergedOptions, updateActor);
// create mock elements to generate images in
const tokenizerHtml = `<div class="token" id="tokenizer-token-parent"><h1>${game.i18n.localize("vtta-tokenizer.label.token")}</h1><div class="view" id="tokenizer-token"></div>`;
let doc = Utils.htmlToDoc(tokenizerHtml);
let tokenView = doc.querySelector(".token > .view");
// get the target filename for the token
const nameSuffix = tokenizer.tokenOptions.nameSuffix ? tokenizer.tokenOptions.nameSuffix : "";
const targetFilename = await tokenizer._getFilename("Token", nameSuffix);
tokenizer.tokenFileName = targetFilename;
// create a Token View
tokenizer.Token = new View(tokenizer, game.settings.get(CONSTANTS.MODULE_ID, "token-size"), tokenView);
// Add the actor image and frame to the token view
await tokenizer._initToken(tokenizer.tokenOptions.tokenFilename);
// upload result to foundry
const dataResult = await tokenizer.Token.get("blob");
await tokenizer.updateToken(dataResult);
// update actor
if (mergedOptions.updateActor) {
await updateActor(tokenizer.tokenOptions);
}
return tokenizer.tokenOptions.tokenFilename;
}
function fixUploadLocation() {
// Set base character upload folder.
const characterUploads = game.settings.get(CONSTANTS.MODULE_ID, "image-upload-directory");
const npcUploads = game.settings.get(CONSTANTS.MODULE_ID, "npc-image-upload-directory");
if (game.user.isGM) {
DirectoryPicker.verifyPath(DirectoryPicker.parse(characterUploads));
DirectoryPicker.verifyPath(DirectoryPicker.parse(npcUploads));
}
if (characterUploads != "" && npcUploads == "") game.settings.set(CONSTANTS.MODULE_ID, "npc-image-upload-directory", characterUploads);
}
function getActorSheetHeaderButtons(app, buttons) {
if (
// don't enable if user can't upload
!game.user.can("FILES_UPLOAD")
// and the player setting is disabled
&& game.settings.get(CONSTANTS.MODULE_ID, "disable-player")
) {
return;
}
const titleLink = game.settings.get(CONSTANTS.MODULE_ID, "title-link");
if (!titleLink) return;
const doc = (app.token) ? app : app.document;
buttons.unshift({
label: "Tokenizer",
icon: "far fa-user-circle",
class: CONSTANTS.MODULE_ID,
onclick: () => tokenizeDoc(doc),
});
}
function linkTidySheets() {
const api = game.modules.get('tidy5e-sheet')?.api;
if (!api) return;
api.config.actorPortrait.registerMenuCommands([
{
label: game.i18n.localize("vtta-tokenizer.module-name"),
iconClass: "fas fa-user-circle",
tooltip: game.i18n.localize("vtta-tokenizer.label.open"),
enabled: (params) => params.actor.type !== "vehicle",
execute: (params) => {
const token = foundry.utils.getProperty(params, "context.options.token");
const doc = token
? { actor: params.actor, token }
: params.actor;
logger.debug("Calling Tokenizer via Tidy5e sheet", { params, token, doc });
tokenizeDoc(doc);
},
},
]);
}
function linkDefaultSheets() {
let sheetNames = Object.values(CONFIG.Actor.sheetClasses)
.reduce((arr, classes) => {
return arr.concat(Object.values(classes).map((c) => c.cls));
}, [])
.map((cls) => cls.name);
// register tokenizer on all character (npc and pc) sheets
sheetNames.forEach((sheetName) => {
Hooks.on("render" + sheetName, (app, html, data) => {
if (game.user) {
const doc = (app.token) ? app : app.document;
const disableAvatarClickGlobal = game.settings.get(CONSTANTS.MODULE_ID, "disable-avatar-click");
const disableAvatarClickUser = game.settings.get(CONSTANTS.MODULE_ID, "disable-avatar-click-user");
const disableAvatarClick = disableAvatarClickUser === "global"
? disableAvatarClickGlobal
: disableAvatarClickUser === "default";
const dataEditField = getAvatarKey();
$(html)
.find(`[data-edit="${dataEditField}"], [data-edit="prototypeToken.texture.src"]`)
.each((index, element) => {
// deactivating the original FilePicker click
$(element).off("click");
// replace it with Tokenizer OR FilePicker click
$(element).on("click", (event) => {
const launchTokenizer
= (!disableAvatarClick && !event.shiftKey) // avatar click not disabled, and not shift key
|| (disableAvatarClick && event.shiftKey); // avatar click disabled, and shift key
if (launchTokenizer) {
event.stopPropagation();
tokenizeDoc(doc);
event.preventDefault();
} else {
// showing the filepicker
const current = data.actor ? data.actor[dataEditField] : data[dataEditField];
const dir = Utils.dirPath(current);
new FilePicker({
type: "image",
current,
callback: (path) => {
event.currentTarget.src = path;
app._onSubmit(event);
},
top: app.position.top + 40,
left: app.position.left + 10,
}).browse(dir);
}
});
});
}
});
});
}
function linkSheets() {
if (
// don't enable if user can't upload
!game.user.can("FILES_UPLOAD")
// and the player setting is disabled
&& game.settings.get(CONSTANTS.MODULE_ID, "disable-player")
) {
return;
}
linkTidySheets();
linkDefaultSheets();
}
function exposeAPI() {
const API = {
launch: launchTokenizer,
launchTokenizer,
tokenizeActor,
tokenizeSceneToken,
tokenizeDoc,
updateSceneTokenImg,
autoToken,
};
window.Tokenizer = API;
game.modules.get(CONSTANTS.MODULE_ID).api = API;
}
export function ready() {
logger.info("Ready Hook Called");
fixUploadLocation();
linkSheets();
exposeAPI();
}
Hooks.on('getActorDirectoryEntryContext', (html, entryOptions) => {
if (
// don't enable if user can't upload
!game.user.can("FILES_UPLOAD")
// and the player setting is disabled
&& game.settings.get(CONSTANTS.MODULE_ID, "disable-player")
) {
return;
}
entryOptions.push({
name: "Tokenizer",
callback: (li) => {
const docId = $(li).attr("data-document-id")
? $(li).attr("data-document-id")
: $(li).attr("data-actor-id")
? $(li).attr("data-actor-id")
: $(li).attr("data-entity-id");
if (docId) {
const doc = game.actors.get(docId);
logger.debug(`Tokenizing ${doc.name}`);
tokenizeActor(doc);
}
},
icon: '<i class="fas fa-user-circle"></i>',
condition: () => {
return game.user.can("FILES_UPLOAD")
|| !game.settings.get(CONSTANTS.MODULE_ID, "disable-player");
}
});
if (!game.user.isGM) return;
entryOptions.push({
name: `${CONSTANTS.MODULE_ID}.apply-prototype-to-scene`,
callback: (li) => {
const docId = $(li).attr("data-document-id")
? $(li).attr("data-document-id")
: $(li).attr("data-actor-id")
? $(li).attr("data-actor-id")
: $(li).attr("data-entity-id");
if (docId) {
const doc = game.actors.get(docId);
logger.debug(`Updating ${doc.name} scene tokens for:`, doc);
updateSceneTokenImg(doc);
}
},
icon: '<i class="fas fa-user-circle"></i>',
condition: () => {
return game.user.can("FILES_UPLOAD");
}
});
});
Hooks.on("getCompendiumDirectoryEntryContext", (html, contextOptions) => {
if (!game.user.isGM) return;
contextOptions.push({
name: `${CONSTANTS.MODULE_ID}.compendium.auto-tokenize`,
callback: (li) => {
const pack = $(li).attr("data-pack");
const compendium = game.packs.get(pack);
if (compendium) {
const auto = new AutoTokenize(compendium);
auto.render(true);
}
},
condition: (li) => {
const pack = $(li).attr("data-pack");
const compendium = game.packs.get(pack);
const isActor = compendium.metadata.type === "Actor";
return isActor;
},
icon: '<i class="fas fa-user-circle"></i>',
});
});
Hooks.on('getActorSheetHeaderButtons', getActorSheetHeaderButtons);