Files
kosmos-storage-audit/scripts/adapters/foundry-runtime.js
2026-04-20 20:48:37 +00:00

119 lines
4.0 KiB
JavaScript

import { analyzeStorage } from "../core/analyzer.js";
import { isMediaPath, normalizePath } from "../core/path-utils.js";
async function* walkFilePicker(storage, target = "", onProgress = null) {
onProgress?.({ phase: "files", label: format("KSA.Progress.BrowseStorage", { storage, path: target || "/" }) });
const result = await FilePicker.browse(storage, target);
for (const file of result.files ?? []) {
const path = normalizePath(file);
if (!isMediaPath(path)) continue;
yield { storage, path };
}
for (const directory of result.dirs ?? []) {
const path = normalizePath(directory);
yield* walkFilePicker(storage, path, onProgress);
}
}
async function* listFoundryFiles(onProgress = null) {
for (const storage of ["data", "public"]) {
if (!game.data.files.storages.includes(storage)) continue;
yield* walkFilePicker(storage, "", onProgress);
}
}
function worldCollectionEntries() {
if (!game.world) return [];
const entries = [];
for (const collection of game.collections ?? []) {
const docs = Array.from(collection.values?.() ?? []);
for (const doc of docs) {
entries.push({
sourceType: "world-document",
sourceScope: {
ownerType: "world",
ownerId: game.world.id,
systemId: game.world.system,
subtype: doc.documentName?.toLowerCase() ?? collection.documentName?.toLowerCase() ?? "document"
},
sourceLabel: `${doc.documentName ?? "Document"} ${doc.id}`,
sourceName: doc.name ?? null,
sourceUuid: doc.uuid ?? null,
value: doc.toObject ? doc.toObject() : doc
});
}
}
return entries;
}
function packageMetadataEntries() {
const entries = [];
for (const module of game.modules.values()) {
entries.push({
sourceType: "module-manifest",
sourceScope: { ownerType: "module", ownerId: module.id, subtype: "manifest" },
sourceLabel: `module.json ${module.id}`,
value: module.toObject ? module.toObject() : module
});
}
if (game.system) {
entries.push({
sourceType: "system-manifest",
sourceScope: { ownerType: "system", ownerId: game.system.id, subtype: "manifest" },
sourceLabel: `system.json ${game.system.id}`,
value: game.system.toObject ? game.system.toObject() : game.system
});
}
if (game.world) {
entries.push({
sourceType: "world-manifest",
sourceScope: { ownerType: "world", ownerId: game.world.id, systemId: game.world.system, subtype: "manifest" },
sourceLabel: `world.json ${game.world.id}`,
value: game.world.toObject ? game.world.toObject() : game.world
});
}
return entries;
}
async function* packagePackEntries(onProgress = null) {
for (const pack of game.packs.values()) {
const ownerType = pack.metadata.packageType;
const ownerId = pack.metadata.packageName;
if (!["module", "system"].includes(ownerType) || !ownerId) continue;
onProgress?.({ phase: "sources", label: format("KSA.Progress.ReadPack", { pack: pack.collection }), currentSource: pack.collection });
const documents = await pack.getDocuments();
for (const document of documents) {
yield {
sourceType: "package-pack-document",
sourceScope: {
ownerType,
ownerId,
subtype: `pack:${pack.collection}`
},
sourceLabel: `${pack.collection} ${document.id}`,
sourceName: document.name ?? null,
sourceUuid: document.uuid ?? null,
value: document.toObject ? document.toObject() : document
};
}
}
}
async function* listFoundrySources(onProgress = null) {
yield* worldCollectionEntries();
yield* packageMetadataEntries();
yield* packagePackEntries(onProgress);
}
export async function runRuntimeAnalysis({ onProgress }={}) {
return analyzeStorage({
listFiles: () => listFoundryFiles(onProgress),
listSources: () => listFoundrySources(onProgress),
onProgress
});
}
function format(key, data) {
return game.i18n?.format(key, data) ?? key;
}