111 lines
3.7 KiB
JavaScript
111 lines
3.7 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: `Durchsuche ${storage}:${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}`,
|
|
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: `Lese Paket-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}`,
|
|
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
|
|
});
|
|
}
|