migrate ikonis feature storage to a local actor cache and patch getEmbeddedDocument for virtual feature resolution

This commit is contained in:
CPTN Cosmo 2026-04-26 18:27:08 +02:00
parent 5ac4a0af17
commit 40cf69e061

View file

@ -164,26 +164,23 @@ export function patchDhCharacter(DhCharacter) {
const featureClone = feature.clone({ parent: this.parent }, { keepId: true });
// Set system type to 'ikonis' so the original sheetLists ignores it
// This prevents features from appearing twice on the sheet
featureClone.system.type = "ikonis";
// Use unique ID per weapon to prevent overwriting same-tech on different weapons
// Use unique ID per weapon
const virtualId = `ikonis-${item.id}-${feature.id}`;
Object.defineProperty(featureClone, "id", { value: virtualId, enumerable: true });
// ALWAYS inject into memory collection so it's resolvable
this.parent.items.set(virtualId, featureClone);
// Store in a local cache on the actor for fast lookup by our patch
if (!this.parent._ikonisCache) this.parent._ikonisCache = new Map();
this.parent._ikonisCache.set(virtualId, featureClone);
// ONLY add to the sheet list if the weapon is equipped
if (isEquipped) {
ikonisFeatures.push(featureClone);
console.log(`DH-Ikonis | Resolved ${type} feature: ${feature.name} for ${item.name} (Equipped)`);
}
}
};
if (bondedUuid) processFeature(bondedUuid, "bonded");
const allAugs = getAugments();
for (const id of installedIds) {
const aug = allAugs.find(a => String(a.id) === String(id));
@ -193,9 +190,6 @@ export function patchDhCharacter(DhCharacter) {
if (ikonisFeatures.length > 0) {
const uniqueFeatures = Array.from(new Set(ikonisFeatures));
console.log(`DH-Ikonis | Injecting ${uniqueFeatures.length} features for ${this.parent.name}`);
// Nest within features so they show up on the Features tab
if (!lists.features) lists.features = {};
lists.features["Ikonis Augments"] = {
title: "Ikonis Augments",
@ -208,6 +202,22 @@ export function patchDhCharacter(DhCharacter) {
},
configurable: true
});
// Patch getEmbeddedDocument to resolve our virtual features
// This is the most compatible way to make fromUuid work without patching it directly
const originalGetEmbedded = Actor.prototype.getEmbeddedDocument;
Actor.prototype.getEmbeddedDocument = function(embeddedName, id, options) {
if (embeddedName === "Item" && typeof id === "string" && id.startsWith("ikonis-")) {
// First check the cache we populated during sheetLists
if (this._ikonisCache?.has(id)) return this._ikonisCache.get(id);
// If not in cache, trigger the getter to populate it
// Accessing system.sheetLists will run our patch above
const lists = this.system.sheetLists;
if (this._ikonisCache?.has(id)) return this._ikonisCache.get(id);
}
return originalGetEmbedded.call(this, embeddedName, id, options);
};
}
export function patchDHWeapon() {