[P1] Fixing crash that can occur when failing to loading variant sprites. (#4315)
This commit is contained in:
parent
f16309fd06
commit
b17b1d6e7e
|
@ -428,38 +428,26 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
|
||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
if (this.shiny) {
|
if (this.shiny) {
|
||||||
const populateVariantColors = (key: string, back: boolean = false): Promise<void> => {
|
const populateVariantColors = (isBackSprite: boolean = false): Promise<void> => {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const battleSpritePath = this.getBattleSpriteAtlasPath(back, ignoreOverride).replace("variant/", "").replace(/_[1-3]$/, "");
|
const battleSpritePath = this.getBattleSpriteAtlasPath(isBackSprite, ignoreOverride).replace("variant/", "").replace(/_[1-3]$/, "");
|
||||||
let config = variantData;
|
let config = variantData;
|
||||||
const useExpSprite = this.scene.experimentalSprites && this.scene.hasExpSprite(this.getBattleSpriteKey(back, ignoreOverride));
|
const useExpSprite = this.scene.experimentalSprites && this.scene.hasExpSprite(this.getBattleSpriteKey(isBackSprite, ignoreOverride));
|
||||||
battleSpritePath.split("/").map(p => config ? config = config[p] : null);
|
battleSpritePath.split("/").map(p => config ? config = config[p] : null);
|
||||||
const variantSet: VariantSet = config as VariantSet;
|
const variantSet: VariantSet = config as VariantSet;
|
||||||
if (variantSet && variantSet[this.variant] === 1) {
|
if (variantSet && variantSet[this.variant] === 1) {
|
||||||
if (variantColorCache.hasOwnProperty(key)) {
|
const cacheKey = this.getBattleSpriteKey(isBackSprite);
|
||||||
return resolve();
|
if (!variantColorCache.hasOwnProperty(cacheKey)) {
|
||||||
|
this.populateVariantColorCache(cacheKey, useExpSprite, battleSpritePath);
|
||||||
}
|
}
|
||||||
this.scene.cachedFetch(`./images/pokemon/variant/${useExpSprite ? "exp/" : ""}${battleSpritePath}.json`).
|
|
||||||
then(res => {
|
|
||||||
// Prevent the JSON from processing if it failed to load
|
|
||||||
if (!res.ok) {
|
|
||||||
console.error(`Could not load ${res.url}!`);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
return res.json();
|
|
||||||
}).then(c => {
|
|
||||||
variantColorCache[key] = c;
|
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
if (this.isPlayer()) {
|
if (this.isPlayer()) {
|
||||||
Promise.all([ populateVariantColors(this.getBattleSpriteKey(false)), populateVariantColors(this.getBattleSpriteKey(true), true) ]).then(() => updateFusionPaletteAndResolve());
|
Promise.all([ populateVariantColors(false), populateVariantColors(true) ]).then(() => updateFusionPaletteAndResolve());
|
||||||
} else {
|
} else {
|
||||||
populateVariantColors(this.getBattleSpriteKey(false)).then(() => updateFusionPaletteAndResolve());
|
populateVariantColors(false).then(() => updateFusionPaletteAndResolve());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
updateFusionPaletteAndResolve();
|
updateFusionPaletteAndResolve();
|
||||||
|
@ -472,6 +460,45 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gracefully handle errors loading a variant sprite. Log if it fails and attempt to fall back on
|
||||||
|
* non-experimental sprites before giving up.
|
||||||
|
*
|
||||||
|
* @param cacheKey the cache key for the variant color sprite
|
||||||
|
* @param attemptedSpritePath the sprite path that failed to load
|
||||||
|
* @param useExpSprite was the attempted sprite experimental
|
||||||
|
* @param battleSpritePath the filename of the sprite
|
||||||
|
* @param optionalParams any additional params to log
|
||||||
|
*/
|
||||||
|
fallbackVariantColor(cacheKey: string, attemptedSpritePath: string, useExpSprite: boolean, battleSpritePath: string, ...optionalParams: any[]) {
|
||||||
|
console.warn(`Could not load ${attemptedSpritePath}!`, ...optionalParams);
|
||||||
|
if (useExpSprite) {
|
||||||
|
this.populateVariantColorCache(cacheKey, false, battleSpritePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to process variant sprite.
|
||||||
|
*
|
||||||
|
* @param cacheKey the cache key for the variant color sprite
|
||||||
|
* @param useExpSprite should the experimental sprite be used
|
||||||
|
* @param battleSpritePath the filename of the sprite
|
||||||
|
*/
|
||||||
|
populateVariantColorCache(cacheKey: string, useExpSprite: boolean, battleSpritePath: string) {
|
||||||
|
const spritePath = `./images/pokemon/variant/${useExpSprite ? "exp/" : ""}${battleSpritePath}.json`;
|
||||||
|
this.scene.cachedFetch(spritePath).then(res => {
|
||||||
|
// Prevent the JSON from processing if it failed to load
|
||||||
|
if (!res.ok) {
|
||||||
|
return this.fallbackVariantColor(cacheKey, res.url, useExpSprite, battleSpritePath, res.status, res.statusText);
|
||||||
|
}
|
||||||
|
return res.json();
|
||||||
|
}).catch(error => {
|
||||||
|
this.fallbackVariantColor(cacheKey, spritePath, useExpSprite, battleSpritePath, error);
|
||||||
|
}).then(c => {
|
||||||
|
variantColorCache[cacheKey] = c;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
getFormKey(): string {
|
getFormKey(): string {
|
||||||
if (!this.species.forms.length || this.species.forms.length <= this.formIndex) {
|
if (!this.species.forms.length || this.species.forms.length <= this.formIndex) {
|
||||||
return "";
|
return "";
|
||||||
|
|
Loading…
Reference in New Issue