From 31e089993d2e8526bf77a64a34ed864bf5f94437 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Sun, 10 Jul 2022 22:14:41 +0800 Subject: [PATCH] update ImageCache check - update some variables, prevent NPE on font on reload --- forge-gui-mobile/src/forge/Forge.java | 9 ++-- .../src/forge/adventure/stage/MapStage.java | 4 +- .../src/forge/assets/FSkinFont.java | 42 +++++++++++++------ .../src/forge/assets/ImageCache.java | 32 +++++++------- 4 files changed, 54 insertions(+), 33 deletions(-) diff --git a/forge-gui-mobile/src/forge/Forge.java b/forge-gui-mobile/src/forge/Forge.java index bd2403e855c..ad1d79681db 100644 --- a/forge-gui-mobile/src/forge/Forge.java +++ b/forge-gui-mobile/src/forge/Forge.java @@ -786,10 +786,6 @@ public class Forge implements ApplicationListener { try { ImageCache.allowSingleLoad(); ForgeAnimation.advanceAll(); - if (needsUpdate) { - if (getAssets().manager.update()) - needsUpdate = false; - } Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear the screen. @@ -880,6 +876,11 @@ public class Forge implements ApplicationListener { } } } + //update here + if (needsUpdate) { + if (getAssets().manager.update()) + needsUpdate = false; + } graphics.end(); } catch (Exception ex) { graphics.end(); diff --git a/forge-gui-mobile/src/forge/adventure/stage/MapStage.java b/forge-gui-mobile/src/forge/adventure/stage/MapStage.java index df973dd5781..1dc759ef348 100644 --- a/forge-gui-mobile/src/forge/adventure/stage/MapStage.java +++ b/forge-gui-mobile/src/forge/adventure/stage/MapStage.java @@ -23,7 +23,6 @@ import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Array; -import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.Scaling; import forge.Forge; import forge.adventure.character.*; @@ -43,6 +42,7 @@ import forge.sound.SoundEffectType; import forge.sound.SoundSystem; +import java.util.HashMap; import java.util.Map; import static forge.adventure.util.Paths.MANA_ATLAS; @@ -69,7 +69,7 @@ public class MapStage extends GameStage { private final Vector2 oldPosition3 = new Vector2(); private final Vector2 oldPosition4 = new Vector2(); private boolean isLoadingMatch = false; - private ObjectMap mapFlags = new ObjectMap<>(); //Stores local map flags. These aren't available outside this map. + private HashMap mapFlags = new HashMap<>(); //Stores local map flags. These aren't available outside this map. private Dialog dialog; private Stage dialogStage; diff --git a/forge-gui-mobile/src/forge/assets/FSkinFont.java b/forge-gui-mobile/src/forge/assets/FSkinFont.java index 985baaec43e..1c5fa17d7ac 100644 --- a/forge-gui-mobile/src/forge/assets/FSkinFont.java +++ b/forge-gui-mobile/src/forge/assets/FSkinFont.java @@ -3,6 +3,7 @@ package forge.assets; import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.HashMap; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; @@ -21,7 +22,6 @@ import com.badlogic.gdx.graphics.glutils.PixmapTextureData; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.IntSet; -import com.badlogic.gdx.utils.ObjectMap; import forge.Forge; import forge.gui.FThreads; import forge.localinstance.properties.ForgeConstants; @@ -38,11 +38,7 @@ public class FSkinFont { private static final int MAX_FONT_SIZE_MANY_GLYPHS = 36; private static final String TTF_FILE = "font1.ttf"; - - private static final String commonCharacterSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" - + "nopqrstuvwxyz1234567890\"!?'.,;:()[]{}<>|/@\\^$-%+=#_&*\u2014" - + "\u2022ÁÉÍÓÚáéíóúÀÈÌÒÙàèìòùÑñÄËÏÖÜäëïöüẞß¿¡"; - private static ObjectMap langUniqueCharacterSet = new ObjectMap<>(); + private static HashMap langUniqueCharacterSet = new HashMap<>(); static { FileUtil.ensureDirectoryExists(ForgeConstants.FONTS_DIR); @@ -62,7 +58,8 @@ public class FSkinFont { public static FSkinFont forHeight(final float height) { int size = MIN_FONT_SIZE + 1; while (true) { - if (_get(size).getLineHeight() > height) { + FSkinFont f = _get(size); + if (f != null && f.getLineHeight() > height) { return _get(size - 1); } size++; @@ -120,6 +117,8 @@ public class FSkinFont { } public int computeVisibleGlyphs (CharSequence str, int start, int end, float availableWidth) { + if (font == null) + return 0; BitmapFontData data = font.getData(); int index = start; float width = 0; @@ -173,6 +172,9 @@ public class FSkinFont { return getBounds(str, 0, str.length()); } public TextBounds getBounds(CharSequence str, int start, int end) { + if (font == null) { + return new TextBounds(0f, 0f); + } BitmapFontData data = font.getData(); //int start = 0; //int end = str.length(); @@ -221,6 +223,9 @@ public class FSkinFont { } public TextBounds getMultiLineBounds(CharSequence str) { updateScale(); + if (font == null) { + return new TextBounds(0f, 0f); + } BitmapFontData data = font.getData(); int start = 0; float maxWidth = 0; @@ -240,6 +245,9 @@ public class FSkinFont { } public TextBounds getWrappedBounds(CharSequence str, float wrapWidth) { updateScale(); + if (font == null) { + return new TextBounds(0f, 0f); + } BitmapFontData data = font.getData(); if (wrapWidth <= 0) wrapWidth = Integer.MAX_VALUE; int start = 0; @@ -296,14 +304,20 @@ public class FSkinFont { return new TextBounds(maxWidth, data.capHeight + (numLines - 1) * data.lineHeight); } public float getAscent() { + if (font == null) + return 0f; updateScale(); return font.getAscent(); } public float getCapHeight() { + if (font == null) + return 0f; updateScale(); return font.getCapHeight(); } public float getLineHeight() { + if (font == null) + return 0f; updateScale(); return font.getLineHeight(); } @@ -316,8 +330,12 @@ public class FSkinFont { //update scale of font if needed private void updateScale() { - if (font.getScaleX() != scale) { - font.getData().setScale(scale); + try { + if (font.getScaleX() != scale) { + font.getData().setScale(scale); + } + } catch (Exception e) { + e.printStackTrace(); } } @@ -341,10 +359,10 @@ public class FSkinFont { if (langUniqueCharacterSet.containsKey(langCode)) { return langUniqueCharacterSet.get(langCode); } - StringBuilder characters = new StringBuilder(commonCharacterSet); + StringBuilder characters = new StringBuilder(FreeTypeFontGenerator.DEFAULT_CHARS); IntSet characterSet = new IntSet(); - for (int offset = 0; offset < commonCharacterSet.length();) { - final int codePoint = commonCharacterSet.codePointAt(offset); + for (int offset = 0; offset < FreeTypeFontGenerator.DEFAULT_CHARS.length();) { + final int codePoint = FreeTypeFontGenerator.DEFAULT_CHARS.codePointAt(offset); characterSet.add(codePoint); offset += Character.charCount(codePoint); } diff --git a/forge-gui-mobile/src/forge/assets/ImageCache.java b/forge-gui-mobile/src/forge/assets/ImageCache.java index 94236afaca6..403c7b94a75 100644 --- a/forge-gui-mobile/src/forge/assets/ImageCache.java +++ b/forge-gui-mobile/src/forge/assets/ImageCache.java @@ -18,6 +18,7 @@ package forge.assets; import java.io.File; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Queue; @@ -29,8 +30,6 @@ import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.TextureData; import com.badlogic.gdx.graphics.glutils.PixmapTextureData; -import com.badlogic.gdx.utils.ObjectMap; -import com.badlogic.gdx.utils.ObjectSet; import com.google.common.collect.EvictingQueue; import com.google.common.collect.Queues; import com.google.common.collect.Sets; @@ -75,7 +74,7 @@ import forge.util.ImageUtil; * @version $Id: ImageCache.java 24769 2014-02-09 13:56:04Z Hellfish $ */ public class ImageCache { - private static final ObjectSet missingIconKeys = new ObjectSet<>(); + private static final HashSet missingIconKeys = new HashSet<>(); private static List borderlessCardlistKey = FileUtil.readFile(ForgeConstants.BORDERLESS_CARD_LIST_FILE); static int maxCardCapacity = 400; //default card capacity static EvictingQueue q; @@ -98,7 +97,7 @@ public class ImageCache { public static final Texture defaultImage; public static FImage BlackBorder = FSkinImage.IMG_BORDER_BLACK; public static FImage WhiteBorder = FSkinImage.IMG_BORDER_WHITE; - private static final ObjectMap> imageBorder = new ObjectMap<>(1024); + private static final HashMap> imageBorder = new HashMap<>(1024); private static boolean imageLoaded, delayLoadRequested; public static void allowSingleLoad() { @@ -277,6 +276,9 @@ public class ImageCache { static Texture loadAsset(String imageKey, File file, boolean others) { if (file == null) return null; + Texture check = getAsset(imageKey, file, others); + if (check != null) + return check; if (!others) { syncQ.add(file.getPath()); cardsLoaded.add(file.getPath()); @@ -294,17 +296,17 @@ public class ImageCache { if (others) { return Forge.getAssets().manager.get(fileName, Texture.class, false); } else { - Texture t = Forge.getAssets().manager.get(fileName, Texture.class, false); + Texture cardTexture = Forge.getAssets().manager.get(fileName, Texture.class, false); //if full bordermasking is enabled, update the border color if (Forge.enableUIMask.equals("Full")) { boolean borderless = isBorderless(imageKey); - updateBorders(t.toString(), borderless ? Pair.of(Color.valueOf("#171717").toString(), false): isCloserToWhite(getpixelColor(t))); + updateBorders(cardTexture.toString(), borderless ? Pair.of(Color.valueOf("#171717").toString(), false): isCloserToWhite(getpixelColor(cardTexture))); //if borderless, generate new texture from the asset and store if (borderless) { - Forge.getAssets().generatedCards.put(imageKey, generateTexture(new FileHandle(file), t, Forge.isTextureFilteringEnabled())); + Forge.getAssets().generatedCards.put(imageKey, generateTexture(new FileHandle(file), cardTexture, Forge.isTextureFilteringEnabled())); } } - return t; + return cardTexture; } } static void unloadCardTextures(AssetManager manager) { @@ -421,10 +423,10 @@ public class ImageCache { return borderColor(t); } - public static Texture generateTexture(FileHandle fh, Texture t, boolean textureFilter) { - if (t == null || fh == null) - return t; - final Texture[] n = new Texture[1]; + public static Texture generateTexture(FileHandle fh, Texture cardTexture, boolean textureFilter) { + if (cardTexture == null || fh == null) + return cardTexture; + final Texture[] placeholder = new Texture[1]; FThreads.invokeInEdtNowOrLater(() -> { Pixmap pImage = new Pixmap(fh); int w = pImage.getWidth(); @@ -437,13 +439,13 @@ public class ImageCache { Format.RGBA8888, textureFilter, //use mipmaps false, true); - n[0] = new Texture(textureData); + placeholder[0] = new Texture(textureData); if (textureFilter) - n[0].setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear); + placeholder[0].setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear); pImage.dispose(); pMask.dispose(); }); - return n[0]; + return placeholder[0]; } public static Pixmap createRoundedRectangle(int width, int height, int cornerRadius, Color color) { Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);