fix Adventure RewardActor crash

- additional check for ImageUtil
This commit is contained in:
Anthony Calosa
2022-12-12 15:41:25 +08:00
parent 80663295e7
commit e17e3628fd
3 changed files with 54 additions and 17 deletions

View File

@@ -18,14 +18,45 @@ public class ImageUtil {
if (imageKey == null || imageKey.length() < 2) { if (imageKey == null || imageKey.length() < 2) {
return null; return null;
} }
if (imageKey.startsWith(ImageKeys.CARD_PREFIX))
key = imageKey.substring(ImageKeys.CARD_PREFIX.length());
else if (imageKey.startsWith(ImageKeys.TOKEN_PREFIX))
key = imageKey.substring(ImageKeys.TOKEN_PREFIX.length());
else if (imageKey.startsWith(ImageKeys.ICON_PREFIX))
key = imageKey.substring(ImageKeys.ICON_PREFIX.length());
else if (imageKey.startsWith(ImageKeys.BOOSTER_PREFIX))
key = imageKey.substring(ImageKeys.BOOSTER_PREFIX.length());
else if (imageKey.startsWith(ImageKeys.FATPACK_PREFIX))
key = imageKey.substring(ImageKeys.FATPACK_PREFIX.length());
else if (imageKey.startsWith(ImageKeys.BOOSTERBOX_PREFIX))
key = imageKey.substring(ImageKeys.BOOSTERBOX_PREFIX.length());
else if (imageKey.startsWith(ImageKeys.PRECON_PREFIX))
key = imageKey.substring(ImageKeys.PRECON_PREFIX.length());
else if (imageKey.startsWith(ImageKeys.TOURNAMENTPACK_PREFIX))
key = imageKey.substring(ImageKeys.TOURNAMENTPACK_PREFIX.length());
else if (imageKey.startsWith(ImageKeys.ADVENTURECARD_PREFIX))
key = imageKey.substring(ImageKeys.ADVENTURECARD_PREFIX.length());
else if (imageKey.contains(".full")) {//no prefix found, construct a valid key if imageKey is art imagekey.
key = transformKey(imageKey);
} else //try anyway...
key = imageKey;
key = imageKey.substring(2);
PaperCard cp = StaticData.instance().getCommonCards().getCard(key); PaperCard cp = StaticData.instance().getCommonCards().getCard(key);
if (cp == null) { if (cp == null) {
cp = StaticData.instance().getVariantCards().getCard(key); cp = StaticData.instance().getVariantCards().getCard(key);
} }
return cp; return cp;
} }
public static String transformKey(String imageKey) {
String key;
String edition= imageKey.substring(0, imageKey.indexOf("/"));
String artIndex = imageKey.substring(imageKey.indexOf("/")+1, imageKey.indexOf(".")).replaceAll("[^0-9]", "");
String name = artIndex.isEmpty() ? imageKey.substring(imageKey.indexOf("/")+1, imageKey.indexOf(".")) : imageKey.substring(imageKey.indexOf("/")+1, imageKey.indexOf(artIndex));
key = name + "|" + edition;
if (!artIndex.isEmpty())
key += "|" + artIndex;
return key;
}
public static String getImageRelativePath(PaperCard cp, String face, boolean includeSet, boolean isDownloadUrl) { public static String getImageRelativePath(PaperCard cp, String face, boolean includeSet, boolean isDownloadUrl) {
final String nameToUse = cp == null ? null : getNameToUse(cp, face); final String nameToUse = cp == null ? null : getNameToUse(cp, face);

View File

@@ -61,7 +61,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
final int preview_h = 680; final int preview_h = 680;
static TextureRegion backTexture; static TextureRegion backTexture;
Texture image, T; Texture image, T, Talt;
Graphics graphics; Graphics graphics;
Texture generatedTooltip = null; //Storage for a generated tooltip. To dispose of on exit. Texture generatedTooltip = null; //Storage for a generated tooltip. To dispose of on exit.
boolean needsToBeDisposed; boolean needsToBeDisposed;
@@ -102,6 +102,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
@Override @Override
public void onImageFetched() { public void onImageFetched() {
ImageCache.clear();
String imageKey = reward.getCard().getImageKey(false); String imageKey = reward.getCard().getImageKey(false);
PaperCard card = ImageUtil.getPaperCardFromImageKey(imageKey); PaperCard card = ImageUtil.getPaperCardFromImageKey(imageKey);
imageKey = card.getCardImageKey(); imageKey = card.getCardImageKey();
@@ -216,10 +217,15 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
} }
} else if (!ImageCache.imageKeyFileExists(reward.getCard().getImageKey(false))) { } else if (!ImageCache.imageKeyFileExists(reward.getCard().getImageKey(false))) {
//Cannot find an image file, set up a rendered card until (if) a file is downloaded. //Cannot find an image file, set up a rendered card until (if) a file is downloaded.
T = renderPlaceholder(getGraphics(), reward.getCard()); //Now we can render the card. T = renderPlaceholder(new Graphics(), reward.getCard(), false); //Now we can render the card.
setCardImage(T); setCardImage(T);
loaded = false; loaded = false;
fetcher.fetchImage(reward.getCard().getImageKey(false), this); fetcher.fetchImage(reward.getCard().getImageKey(false), this);
if (reward.getCard().hasBackFace()) {
if (!ImageCache.imageKeyFileExists(reward.getCard().getImageKey(true))) {
fetcher.fetchImage(reward.getCard().getImageKey(true), this);
}
}
} }
} }
break; break;
@@ -325,17 +331,16 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
if (!reward.getCard().hasBackFace()) if (!reward.getCard().hasBackFace())
return; return;
Texture alt = ImageCache.getImage(reward.getCard().getImageKey(true), false); Texture alt = ImageCache.getImage(reward.getCard().getImageKey(true), false);
PaperCard altCard = ImageUtil.getPaperCardFromImageKey(reward.getCard().getCardAltImageKey());
if (GuiBase.isAndroid() || Forge.hasGamepad()) { if (GuiBase.isAndroid() || Forge.hasGamepad()) {
if (alternate) { if (alternate) {
if (alt != null) { if (alt != null) {
holdTooltip.tooltip_actor.clear(); holdTooltip.tooltip_actor.clear();
holdTooltip.tooltip_actor.add(new RewardImage(processDrawable(alt))); holdTooltip.tooltip_actor.add(new RewardImage(processDrawable(alt)));
} else { } else {
if (T == null) if (Talt == null)
T = renderPlaceholder(getGraphics(), altCard); Talt = renderPlaceholder(new Graphics(), reward.getCard(), true);
holdTooltip.tooltip_actor.clear(); holdTooltip.tooltip_actor.clear();
holdTooltip.tooltip_actor.add(new RewardImage(processDrawable(T))); holdTooltip.tooltip_actor.add(new RewardImage(processDrawable(Talt)));
} }
} else { } else {
if (toolTipImage != null) { if (toolTipImage != null) {
@@ -349,9 +354,9 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
if (alt != null) { if (alt != null) {
tooltip.setActor(new RewardImage(processDrawable(alt))); tooltip.setActor(new RewardImage(processDrawable(alt)));
} else { } else {
if (T == null) if (Talt == null)
T = renderPlaceholder(getGraphics(), altCard); Talt = renderPlaceholder(new Graphics(), reward.getCard(), true);
tooltip.setActor(new RewardImage(processDrawable(T))); tooltip.setActor(new RewardImage(processDrawable(Talt)));
} }
} else { } else {
if (toolTipImage != null) if (toolTipImage != null)
@@ -441,7 +446,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
} }
} }
private Texture renderPlaceholder(Graphics g, PaperCard card) { //Use CardImageRenderer to output a Texture. private Texture renderPlaceholder(Graphics g, PaperCard card, boolean alternate) { //Use CardImageRenderer to output a Texture.
if (renderedCount++ == 0) { if (renderedCount++ == 0) {
//The first time we find a card that has no art, render one out of view to fully initialize CardImageRenderer. //The first time we find a card that has no art, render one out of view to fully initialize CardImageRenderer.
g.begin(preview_w, preview_h); g.begin(preview_w, preview_h);
@@ -456,7 +461,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
g.begin(preview_w, preview_h); g.begin(preview_w, preview_h);
g.setProjectionMatrix(m); g.setProjectionMatrix(m);
g.startClip(); g.startClip();
CardImageRenderer.drawCardImage(g, CardView.getCardForUi(card), false, 0, 0, preview_w, preview_h, CardRenderer.CardStackPosition.Top, Forge.allowCardBG, true); CardImageRenderer.drawCardImage(g, CardView.getCardForUi(card), alternate, 0, 0, preview_w, preview_h, CardRenderer.CardStackPosition.Top, Forge.allowCardBG, true);
g.end(); g.end();
g.endClip(); g.endClip();
//Rendering ends here. Create a new Pixmap to Texture with mipmaps, otherwise will render as full black. //Rendering ends here. Create a new Pixmap to Texture with mipmaps, otherwise will render as full black.
@@ -623,7 +628,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
drawCard(batch, image, x, width); drawCard(batch, image, x, width);
} else if (!loaded) { } else if (!loaded) {
if (T == null) if (T == null)
T = renderPlaceholder(getGraphics(), reward.getCard()); T = renderPlaceholder(getGraphics(), reward.getCard(), false);
drawCard(batch, T, x, width); drawCard(batch, T, x, width);
} }
} else if (image != null) { } else if (image != null) {
@@ -637,7 +642,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
batch.end(); batch.end();
shaderRoundRect.bind(); shaderRoundRect.bind();
shaderRoundRect.setUniformf("u_resolution", image.getWidth(), image.getHeight()); shaderRoundRect.setUniformf("u_resolution", image.getWidth(), image.getHeight());
shaderRoundRect.setUniformf("edge_radius", (image.getHeight()/image.getWidth())*20); shaderRoundRect.setUniformf("edge_radius", (image.getHeight() / image.getWidth()) * 20);
shaderRoundRect.setUniformf("u_gray", sold ? 1f : 0f); shaderRoundRect.setUniformf("u_gray", sold ? 1f : 0f);
batch.setShader(shaderRoundRect); batch.setShader(shaderRoundRect);
batch.begin(); batch.begin();
@@ -816,6 +821,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
shown = false; shown = false;
} }
} }
class RewardImage extends Image { class RewardImage extends Image {
public RewardImage(TextureRegionDrawable processDrawable) { public RewardImage(TextureRegionDrawable processDrawable) {
setDrawable(processDrawable); setDrawable(processDrawable);
@@ -829,7 +835,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
if (getDrawable() instanceof TextureRegionDrawable) { if (getDrawable() instanceof TextureRegionDrawable) {
Texture t = ((TextureRegionDrawable) getDrawable()).getRegion().getTexture(); Texture t = ((TextureRegionDrawable) getDrawable()).getRegion().getTexture();
if (t != null) { if (t != null) {
float x = GuiBase.isAndroid() || Forge.hasGamepad() ? Scene.getIntendedWidth() / 2 - holdTooltip.tooltip_actor.getWidth() / 2: tooltip.getActor().getImageX(); float x = GuiBase.isAndroid() || Forge.hasGamepad() ? Scene.getIntendedWidth() / 2 - holdTooltip.tooltip_actor.getWidth() / 2 : tooltip.getActor().getImageX();
float y = GuiBase.isAndroid() || Forge.hasGamepad() ? Scene.getIntendedHeight() / 2 - holdTooltip.tooltip_actor.getHeight() / 2 : tooltip.getActor().getImageY(); float y = GuiBase.isAndroid() || Forge.hasGamepad() ? Scene.getIntendedHeight() / 2 - holdTooltip.tooltip_actor.getHeight() / 2 : tooltip.getActor().getImageY();
float w = GuiBase.isAndroid() || Forge.hasGamepad() ? holdTooltip.tooltip_actor.getPrefWidth() : tooltip.getActor().getPrefWidth(); float w = GuiBase.isAndroid() || Forge.hasGamepad() ? holdTooltip.tooltip_actor.getPrefWidth() : tooltip.getActor().getPrefWidth();
float h = GuiBase.isAndroid() || Forge.hasGamepad() ? holdTooltip.tooltip_actor.getPrefHeight() : tooltip.getActor().getPrefHeight(); float h = GuiBase.isAndroid() || Forge.hasGamepad() ? holdTooltip.tooltip_actor.getPrefHeight() : tooltip.getActor().getPrefHeight();
@@ -837,7 +843,7 @@ public class RewardActor extends Actor implements Disposable, ImageFetcher.Callb
batch.end(); batch.end();
shaderRoundRect.bind(); shaderRoundRect.bind();
shaderRoundRect.setUniformf("u_resolution", t.getWidth(), t.getHeight()); shaderRoundRect.setUniformf("u_resolution", t.getWidth(), t.getHeight());
shaderRoundRect.setUniformf("edge_radius", (t.getHeight()/t.getWidth())*ImageCache.getRadius(t)); shaderRoundRect.setUniformf("edge_radius", (t.getHeight() / t.getWidth()) * ImageCache.getRadius(t));
shaderRoundRect.setUniformf("u_gray", sold ? 0.8f : 0f); shaderRoundRect.setUniformf("u_gray", sold ? 0.8f : 0f);
batch.setShader(shaderRoundRect); batch.setShader(shaderRoundRect);
batch.begin(); batch.begin();

View File

@@ -44,7 +44,7 @@ public class VAvatar extends FDisplayObject {
avatarAnimation = new AvatarAnimation(); avatarAnimation = new AvatarAnimation();
} }
private class AvatarAnimation extends ForgeAnimation { private class AvatarAnimation extends ForgeAnimation {
private static final float DURATION = 0.6f; private static final float DURATION = 1.2f;
private float progress = 0; private float progress = 0;
Texture splatter = FSkin.splatter; Texture splatter = FSkin.splatter;