refactor: resturcture files so that lincensing info can be bundled properly

This commit is contained in:
Johannes Loher 2022-01-31 15:13:32 +01:00
parent 699ba74840
commit 1aa284311f
484 changed files with 119 additions and 179 deletions

17
src/hooks/hooks.ts Normal file
View file

@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import registerForHotbarDropHook from "./hotbar-drop";
import registerForInitHook from "./init";
import registerForReadyHook from "./ready";
import registerForRenderHooks from "./render";
import registerForSetupHook from "./setup";
export default function registerForHooks(): void {
registerForHotbarDropHook();
registerForInitHook();
registerForReadyHook();
registerForRenderHooks();
registerForSetupHook();
}

48
src/hooks/hotbar-drop.ts Normal file
View file

@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { isCheck } from "../actor/actor-data-properties";
import { getGame } from "../helpers";
import { DS4Item } from "../item/item";
import { createRollCheckMacro } from "../macros/roll-check";
import { createRollItemMacro } from "../macros/roll-item";
import notifications from "../ui/notifications";
export default function registerForHotbarDropHook(): void {
Hooks.on("hotbarDrop", async (hotbar: Hotbar, data: HotbarDropData, slot: string) => {
switch (data.type) {
case "Item": {
if (!isItemDropData(data) || !("data" in data)) {
return notifications.warn(
getGame().i18n.localize("DS4.WarningMacrosCanOnlyBeCreatedForOwnedItems"),
);
}
const itemData = data.data;
if (!DS4Item.rollableItemTypes.includes(itemData.type)) {
return notifications.warn(
getGame().i18n.format("DS4.WarningItemIsNotRollable", {
name: itemData.name,
id: itemData._id,
type: itemData.type,
}),
);
}
return createRollItemMacro(itemData, slot);
}
case "Check": {
if (!("data" in data) || typeof data.data !== "string" || !isCheck(data.data)) {
return notifications.warn(getGame().i18n.localize("DS4.WarningInvalidCheckDropped"));
}
return createRollCheckMacro(data.data, slot);
}
}
});
}
type HotbarDropData = ActorSheet.DropData.Item | ({ type: string } & Partial<Record<string, unknown>>);
function isItemDropData(dropData: HotbarDropData): dropData is ActorSheet.DropData.Item {
return dropData.type === "Item";
}

89
src/hooks/init.ts Normal file
View file

@ -0,0 +1,89 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Oliver Rümpelein
// SPDX-FileCopyrightText: 2021 Gesina Schwalbe
//
// SPDX-License-Identifier: MIT
import { DS4ActiveEffect } from "../active-effect";
import { DS4Actor } from "../actor/actor";
import { DS4CharacterActorSheet } from "../actor/sheets/character-sheet";
import { DS4CreatureActorSheet } from "../actor/sheets/creature-sheet";
import { DS4ChatMessage } from "../chat-message";
import { DS4 } from "../config";
import { preloadFonts as preloadFonts } from "../fonts";
import registerHandlebarsHelpers from "../handlebars/handlebars-helpers";
import registerHandlebarsPartials from "../handlebars/handlebars-partials";
import { getGame } from "../helpers";
import { DS4Item } from "../item/item";
import { DS4ItemSheet } from "../item/item-sheet";
import logger from "../logger";
import { macros } from "../macros/macros";
import { migration } from "../migrations";
import { DS4Check } from "../rolls/check";
import { createCheckRoll } from "../rolls/check-factory";
import { DS4Roll } from "../rolls/roll";
import registerSlayingDiceModifier from "../rolls/slaying-dice-modifier";
import { registerSystemSettings } from "../settings";
export default function registerForInitHook(): void {
Hooks.once("init", init);
}
async function init() {
logger.info(`Initializing the DS4 Game System\n${DS4.ASCII}`);
getGame().ds4 = {
DS4Actor,
DS4Item,
DS4,
createCheckRoll,
migration,
macros,
};
CONFIG.DS4 = DS4;
CONFIG.Actor.documentClass = DS4Actor;
CONFIG.Item.documentClass = DS4Item;
CONFIG.ActiveEffect.documentClass = DS4ActiveEffect;
CONFIG.ChatMessage.documentClass = DS4ChatMessage;
CONFIG.Actor.typeLabels = DS4.i18n.actorTypes;
CONFIG.Item.typeLabels = DS4.i18n.itemTypes;
CONFIG.Dice.types.push(DS4Check);
CONFIG.Dice.terms.s = DS4Check;
CONFIG.Dice.rolls.unshift(DS4Roll);
registerSlayingDiceModifier();
registerSystemSettings();
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("ds4", DS4CharacterActorSheet, { types: ["character"], makeDefault: true });
Actors.registerSheet("ds4", DS4CreatureActorSheet, { types: ["creature"], makeDefault: true });
Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("ds4", DS4ItemSheet, { makeDefault: true });
preloadFonts();
await registerHandlebarsPartials();
registerHandlebarsHelpers();
}
declare global {
interface Game {
ds4: {
DS4Actor: typeof DS4Actor;
DS4Item: typeof DS4Item;
DS4: typeof DS4;
createCheckRoll: typeof createCheckRoll;
migration: typeof migration;
macros: typeof macros;
};
}
interface CONFIG {
DS4: typeof DS4;
}
}

11
src/hooks/ready.ts Normal file
View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { migration } from "../migrations";
export default function registerForReadyHook(): void {
Hooks.once("ready", () => {
migration.migrate();
});
}

28
src/hooks/render.ts Normal file
View file

@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Gesina Schwalbe
//
// SPDX-License-Identifier: MIT
/**
* @remarks The render hooks of all classes in the class hierarchy are called, so e.g. for a {@link Dialog}, both the
* "renderDialog" hook and the "renderApplication" hook are called (in this order).
*/
export default function registerForRenderHooks(): void {
["renderApplication", "renderActorSheet", "renderItemSheet"].forEach((hook) => {
Hooks.on(hook, selectTargetInputOnFocus);
});
}
/**
* Select the text of input elements in given application when focused via an on focus listener.
*
* @param app - The application in which to activate the listener.
* @param html - The {@link JQuery} representing the HTML of the application.
*/
function selectTargetInputOnFocus(app: Application, html: JQuery) {
$(html)
.find("input")
.on("focus", (ev: JQuery.FocusEvent<HTMLInputElement>) => {
ev.currentTarget.select();
});
}

36
src/hooks/setup.ts Normal file
View file

@ -0,0 +1,36 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Oliver Rümpelein
// SPDX-FileCopyrightText: 2021 Gesina Schwalbe
// SPDX-FileCopyrightText: 2021 Siegfried Krug
//
// SPDX-License-Identifier: MIT
import { DS4 } from "../config";
import { getGame } from "../helpers";
export default function registerForSetupHooks(): void {
Hooks.once("setup", () => {
localizeAndSortConfigObjects();
});
}
/**
* Localizes all objects in {@link DS4.i18n} and sorts them unless they are explicitly excluded.
*/
function localizeAndSortConfigObjects() {
const noSort = ["attributes", "traits", "combatValues", "creatureSizeCategories"];
const localizeObject = <T extends { [s: string]: string }>(obj: T, sort = true): T => {
const localized = Object.entries(obj).map(([key, value]): [string, string] => {
return [key, getGame().i18n.localize(value)];
});
if (sort) localized.sort((a, b) => a[1].localeCompare(b[1]));
return Object.fromEntries(localized) as T;
};
DS4.i18n = Object.fromEntries(
Object.entries(DS4.i18n).map(([key, value]) => {
return [key, localizeObject(value, !noSort.includes(key))];
}),
) as typeof DS4.i18n;
}