mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 04:08:01 +00:00
Improved performance w/ cached content for ImageKeys.hasImage
THe content of the setFolder is cached, filtering only images and key prefixes. Working on prefixes only to get rid of all .full or .fullborder possible variations.
This commit is contained in:
@@ -2,13 +2,11 @@ package forge;
|
||||
|
||||
import forge.item.PaperCard;
|
||||
import forge.util.FileUtil;
|
||||
import forge.util.ImageUtil;
|
||||
import forge.util.TextUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public final class ImageKeys {
|
||||
public static final String CARD_PREFIX = "c:";
|
||||
@@ -68,9 +66,8 @@ public final class ImageKeys {
|
||||
}
|
||||
|
||||
public static File getImageFile(String key) {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
if (StringUtils.isEmpty(key))
|
||||
return null;
|
||||
}
|
||||
|
||||
final String dir;
|
||||
final String filename;
|
||||
@@ -220,14 +217,35 @@ public final class ImageKeys {
|
||||
|
||||
//shortcut for determining if a card image exists for a given card
|
||||
//should only be called from PaperCard.hasImage()
|
||||
static HashMap<String, HashSet<String>> cachedContent=new HashMap<>();
|
||||
public static boolean hasImage(PaperCard pc) {
|
||||
Boolean editionHasImage = editionImageLookup.get(pc.getEdition());
|
||||
if (editionHasImage == null) {
|
||||
String setFolder = getSetFolder(pc.getEdition());
|
||||
editionHasImage = FileUtil.isDirectoryWithFiles(CACHE_CARD_PICS_DIR + setFolder);
|
||||
editionImageLookup.put(pc.getEdition(), editionHasImage);
|
||||
if (editionHasImage){
|
||||
File f = new File(CACHE_CARD_PICS_DIR + setFolder); // no need to check this, otherwise editionHasImage would be false!
|
||||
HashSet<String> setFolderContent = new HashSet<>();
|
||||
for (String filename : Arrays.asList(f.list())) {
|
||||
// TODO: should this use FILE_EXTENSIONS ?
|
||||
if (!filename.endsWith(".jpg") && !filename.endsWith(".png"))
|
||||
continue; // not image - not interested
|
||||
setFolderContent.add(filename.split("\\.")[0]); // get rid of any full or fullborder
|
||||
}
|
||||
cachedContent.put(setFolder, setFolderContent);
|
||||
}
|
||||
}
|
||||
String[] keyParts = pc.getImageKeyFromSet().split(File.separator);
|
||||
HashSet<String> content = cachedContent.getOrDefault(keyParts[0], null);
|
||||
//avoid checking for file if edition doesn't have any images
|
||||
return editionHasImage && getImageFile(ImageUtil.getImageKey(pc, false, true)) != null;
|
||||
return editionHasImage && hitCache(content, keyParts[1]);
|
||||
}
|
||||
|
||||
private static boolean hitCache(HashSet<String> cache, String filename){
|
||||
if (cache == null || cache.isEmpty())
|
||||
return false;
|
||||
final String keyPrefix = filename.split("\\.")[0];
|
||||
return cache.contains(keyPrefix);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user