Skip to content

Commit

Permalink
Generalize UUID conversion in build/extract (foundryvtt#9414)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Aug 19, 2023
1 parent cd723e9 commit 7820a63
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 43 deletions.
86 changes: 49 additions & 37 deletions build/lib/compendium-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class CompendiumPack {
for (const item of docSource.items) {
item.effects = [];
item.system.schema = { version: MigrationRunnerBase.LATEST_SCHEMA_VERSION, lastMigration: null };
CompendiumPack.convertRuleUUIDs(item, { to: "ids", map: CompendiumPack.#namesToIds.Item });
CompendiumPack.convertUUIDs(item, { to: "ids", map: CompendiumPack.#namesToIds.Item });
}
}

Expand All @@ -274,7 +274,7 @@ class CompendiumPack {
}

// Convert uuids with names in GrantItem REs to well-formedness
CompendiumPack.convertRuleUUIDs(docSource, { to: "ids", map: CompendiumPack.#namesToIds.Item });
CompendiumPack.convertUUIDs(docSource, { to: "ids", map: CompendiumPack.#namesToIds.Item });
}

const replace = (match: string, packId: string, docType: string, docName: string): string => {
Expand Down Expand Up @@ -315,67 +315,74 @@ class CompendiumPack {
}

/** Convert UUIDs in REs to resemble links by name or back again */
static convertRuleUUIDs(
static convertUUIDs(
source: ItemSourcePF2e,
{ to, map }: { to: "ids" | "names"; map: Map<string, Map<string, string>> }
): void {
const convertOptions = { to: to === "ids" ? "id" : "name", map } as const;

if (itemIsOfType(source, "feat", "action") && source.system.selfEffect) {
source.system.selfEffect.uuid = CompendiumPack.convertUUID(source.system.selfEffect.uuid, convertOptions);
}

const hasUUIDChoices = (choices: object | string | undefined): choices is Record<string, { value: string }> =>
typeof choices === "object" &&
Object.values(choices ?? {}).every(
(c): c is { value: unknown } => typeof c.value === "string" && c.value.startsWith("Compendium.pf2e.")
);

const toNameRef = (uuid: string): string => {
const rules: REMaybeWithUUIDs[] = source.system.rules;

for (const rule of rules) {
if (rule.key === "Aura" && Array.isArray(rule.effects)) {
for (const effect of rule.effects) {
if (isObject<{ uuid?: unknown }>(effect) && typeof effect.uuid === "string") {
effect.uuid = this.convertUUID(effect.uuid, convertOptions);
}
}
} else if (tupleHasValue(["EphemeralEffect", "GrantItem"], rule.key) && typeof rule.uuid === "string") {
rule.uuid = this.convertUUID(rule.uuid, convertOptions);
} else if (rule.key === "ChoiceSet" && hasUUIDChoices(rule.choices)) {
for (const [key, choice] of Object.entries(rule.choices)) {
rule.choices[key].value = this.convertUUID(choice.value, convertOptions);
}
if ("selection" in rule && typeof rule.selection === "string") {
rule.selection = this.convertUUID(rule.selection, convertOptions);
}
}
}
}

static convertUUID<TUUID extends string>(uuid: TUUID, { to, map }: ConvertUUIDOptions): TUUID {
if (uuid.startsWith("Item.")) {
throw PackError(`World-item UUID found: ${uuid}`);
}

const toNameRef = (uuid: string): TUUID => {
const parts = uuid.split(".");
const [packId, _docType, docId] = parts.slice(2, 6);
const docName = map.get(packId)?.get(docId);
if (docName) {
return parts.slice(0, 4).concat(docName).join(".");
return parts.slice(0, 4).concat(docName).join(".") as TUUID;
} else {
console.debug(`Warning: Unable to find document name corresponding with ${uuid}`);
return uuid;
return uuid as TUUID;
}
};

const toIDRef = (uuid: string): string => {
const toIDRef = (uuid: string): TUUID => {
const match = /(?<=^Compendium\.pf2e\.)([^.]+)\.([^.]+)\.(.+)$/.exec(uuid);
const [, packId, _docType, docName] = match ?? [null, null, null, null];
const docId = map.get(packId ?? "")?.get(docName ?? "");
if (docName && docId) {
return uuid.replace(docName, docId);
return uuid.replace(docName, docId) as TUUID;
} else {
throw PackError(`Unable to resolve UUID in ${source.name}: ${uuid}`);
}
};

const convert = (uuid: string): string => {
if (uuid.startsWith("Item.")) {
throw PackError(`World-item UUID found: ${uuid}`);
throw Error("Unable to resolve UUID");
}
if (!uuid.startsWith("Compendium.pf2e.")) return uuid;
return to === "ids" ? toIDRef(uuid) : toNameRef(uuid);
};

const rules: REMaybeWithUUIDs[] = source.system.rules;

for (const rule of rules) {
if (rule.key === "Aura" && Array.isArray(rule.effects)) {
for (const effect of rule.effects) {
if (isObject<{ uuid?: unknown }>(effect) && typeof effect.uuid === "string") {
effect.uuid = convert(effect.uuid);
}
}
} else if (tupleHasValue(["EphemeralEffect", "GrantItem"], rule.key) && typeof rule.uuid === "string") {
rule.uuid = convert(rule.uuid);
} else if (rule.key === "ChoiceSet" && hasUUIDChoices(rule.choices)) {
for (const [key, choice] of Object.entries(rule.choices)) {
rule.choices[key].value = convert(choice.value);
}
if ("selection" in rule && typeof rule.selection === "string") {
rule.selection = convert(rule.selection);
}
}
}
if (!uuid.startsWith("Compendium.pf2e.")) return uuid;
return to === "id" ? toIDRef(uuid) : toNameRef(uuid);
}

async save(asJson?: boolean): Promise<number> {
Expand Down Expand Up @@ -456,4 +463,9 @@ class CompendiumPack {
}
}

interface ConvertUUIDOptions {
to: "id" | "name";
map: Map<string, Map<string, string>>;
}

export { CompendiumPack, PackError, PackMetadata, isItemSource, isActorSource, REMaybeWithUUIDs };
12 changes: 6 additions & 6 deletions build/lib/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class PackExtractor {

for (const source of packSources) {
// Remove or replace unwanted values from the document source
const preparedSource = this.#convertLinks(source, packDirectory);
const preparedSource = this.#convertUUIDs(source, packDirectory);
if ("items" in preparedSource && preparedSource.type === "npc" && !this.disablePresort) {
preparedSource.items = this.#sortDataItems(preparedSource);
} else if (!this.#folderPathMap.get(preparedSource.folder ?? "")) {
Expand Down Expand Up @@ -237,19 +237,19 @@ class PackExtractor {
}
}

#convertLinks(docSource: PackEntry, packName: string): PackEntry {
#convertUUIDs(docSource: PackEntry, packName: string): PackEntry {
this.#newDocIdMap[docSource._id!] = docSource.name;

const sanitized = this.#sanitizeDocument(docSource);
if (isActorSource(sanitized)) {
sanitized.items = sanitized.items.map((itemData) => {
CompendiumPack.convertRuleUUIDs(itemData, { to: "names", map: this.#idsToNames.Item });
return this.#sanitizeDocument(itemData, { isEmbedded: true });
sanitized.items = sanitized.items.map((itemSource) => {
CompendiumPack.convertUUIDs(itemSource, { to: "names", map: this.#idsToNames.Item });
return this.#sanitizeDocument(itemSource, { isEmbedded: true });
});
}

if (isItemSource(sanitized)) {
CompendiumPack.convertRuleUUIDs(sanitized, { to: "names", map: this.#idsToNames.Item });
CompendiumPack.convertUUIDs(sanitized, { to: "names", map: this.#idsToNames.Item });
}

const docJSON = JSON.stringify(sanitized).replace(/@Compendium\[/g, "@UUID[Compendium.");
Expand Down

0 comments on commit 7820a63

Please sign in to comment.