Files
tts-workshop/packages/extract/src/refs.ts
T

60 lines
1.5 KiB
TypeScript

import type { AssetRef, TTSMod, TTSObject } from '@tts/shared';
import { markParent, traverseMod } from './traverse.js';
/**
* Extract every external asset reference from a single object.
*/
export function extractRefs(o: TTSObject): AssetRef[] {
const refs: AssetRef[] = [];
const ownerGuid = o.GUID;
if (o.CustomPDF?.PDFUrl) {
refs.push({ kind: 'pdf', url: o.CustomPDF.PDFUrl, ownerGuid });
}
if (o.CustomDeck) {
for (const deck of Object.values(o.CustomDeck)) {
if (deck.FaceURL) {
refs.push({ kind: 'deckFace', url: deck.FaceURL, ownerGuid });
}
if (deck.BackURL) {
refs.push({ kind: 'deckBack', url: deck.BackURL, ownerGuid });
}
}
}
if (o.CustomImage?.ImageURL) {
refs.push({ kind: 'image', url: o.CustomImage.ImageURL, ownerGuid });
}
if (o.CustomImage?.ImageSecondaryURL) {
refs.push({
kind: 'imageSecondary',
url: o.CustomImage.ImageSecondaryURL,
ownerGuid,
});
}
return refs;
}
/**
* Collect all asset references across a whole save, deduped by URL.
*/
export function collectRefs(mod: TTSMod): AssetRef[] {
for (const state of mod.ObjectStates) {
markParent(state);
}
const seen = new Set<string>();
const refs: AssetRef[] = [];
for (const o of traverseMod(mod)) {
for (const ref of extractRefs(o)) {
if (!seen.has(ref.url)) {
seen.add(ref.url);
refs.push(ref);
}
}
}
return refs;
}
export { markParent, traverseMod } from './traverse.js';