update prevent NPE

This commit is contained in:
Anthony Calosa
2024-10-26 06:23:14 +08:00
parent e6e6a6e0a4
commit 4f23a6026e

View File

@@ -95,6 +95,26 @@ public class ImageCache {
cardsLoaded = new HashSet<>(cl); cardsLoaded = new HashSet<>(cl);
} }
private Set<String> getCardsLoaded() {
if (cardsLoaded == null) {
cardsLoaded = new HashSet<>(400);
}
return cardsLoaded;
}
private EvictingQueue<String> getQ() {
if (q == null) {
q = EvictingQueue.create(400);
}
return q;
}
private Queue<String> getSyncQ() {
if (syncQ == null)
syncQ = Queues.synchronizedQueue(getQ());
return syncQ;
}
public static Texture getDefaultImage() { public static Texture getDefaultImage() {
return Forge.getAssets().getDefaultImage(); return Forge.getAssets().getDefaultImage();
} }
@@ -119,14 +139,14 @@ public class ImageCache {
public static void disposeTextures() { public static void disposeTextures() {
CardRenderer.clearcardArtCache(); CardRenderer.clearcardArtCache();
//unload all cardsLoaded //unload all cardsLoaded
if (cardsLoaded != null) { try {
for (String fileName : cardsLoaded) { for (String fileName : getCardsLoaded()) {
if (Forge.getAssets().manager().get(fileName, Texture.class, false) != null) { if (Forge.getAssets().manager().get(fileName, Texture.class, false) != null) {
Forge.getAssets().manager().unload(fileName); Forge.getAssets().manager().unload(fileName);
} }
} }
cardsLoaded.clear(); } catch (Exception ignored) {}
} getCardsLoaded().clear();
((Forge) Gdx.app.getApplicationListener()).needsUpdate = true; ((Forge) Gdx.app.getApplicationListener()).needsUpdate = true;
} }
@@ -136,8 +156,8 @@ public class ImageCache {
public static void updateSynqCount(File file, int count) { public static void updateSynqCount(File file, int count) {
if (file == null) if (file == null)
return; return;
syncQ.add(file.getPath()); getSyncQ().add(file.getPath());
cardsLoaded.add(file.getPath()); getCardsLoaded().add(file.getPath());
counter += count; counter += count;
} }
@@ -298,8 +318,8 @@ public class ImageCache {
return check; return check;
if (!others) { if (!others) {
//update first before clearing //update first before clearing
syncQ.add(file.getPath()); getSyncQ().add(file.getPath());
cardsLoaded.add(file.getPath()); getCardsLoaded().add(file.getPath());
unloadCardTextures(false); unloadCardTextures(false);
} }
String fileName = file.getPath(); String fileName = file.getPath();
@@ -350,8 +370,8 @@ public class ImageCache {
Forge.getAssets().manager().unload(asset); Forge.getAssets().manager().unload(asset);
} }
} }
syncQ.clear(); getSyncQ().clear();
cardsLoaded.clear(); getCardsLoaded().clear();
counter = 0; counter = 0;
CardRenderer.clearcardArtCache(); CardRenderer.clearcardArtCache();
} catch (Exception e) { } catch (Exception e) {
@@ -360,19 +380,19 @@ public class ImageCache {
return; return;
} }
} }
if (cardsLoaded.size() <= maxCardCapacity) if (getCardsLoaded().size() <= maxCardCapacity)
return; return;
//get latest images from syncQ //get latest images from syncQ
Set<String> newQ = Sets.newHashSet(syncQ); Set<String> newQ = Sets.newHashSet(getSyncQ());
//get all images not in newQ (cards to unload) //get all images not in newQ (cards to unload)
Set<String> toUnload = Sets.difference(cardsLoaded, newQ); Set<String> toUnload = Sets.difference(getCardsLoaded(), newQ);
//unload from assetmanager to save RAM //unload from assetmanager to save RAM
try { try {
for (String asset : toUnload) { for (String asset : toUnload) {
if (Forge.getAssets().manager().get(asset, Texture.class, false) != null) { if (Forge.getAssets().manager().get(asset, Texture.class, false) != null) {
Forge.getAssets().manager().unload(asset); Forge.getAssets().manager().unload(asset);
} }
cardsLoaded.remove(asset); getCardsLoaded().remove(asset);
} }
//clear cachedArt since this is dependant to the loaded texture //clear cachedArt since this is dependant to the loaded texture
CardRenderer.clearcardArtCache(); CardRenderer.clearcardArtCache();