mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
Don't load same image multiple times
This commit is contained in:
@@ -15,6 +15,7 @@ import com.dfsek.terra.addons.image.sampler.ColorSampler;
|
||||
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
@@ -40,7 +41,8 @@ public class ImageLibraryAddon implements AddonInitializer {
|
||||
.register(addon, ConfigPackPreLoadEvent.class)
|
||||
.priority(10)
|
||||
.then(event -> {
|
||||
event.getPack().applyLoader(Image.class, new ImageLoader(event.getPack().getLoader()));
|
||||
ConfigPack pack = event.getPack();
|
||||
pack.applyLoader(Image.class, new ImageLoader(pack.getLoader(), pack));
|
||||
})
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<ColorSampler>>> colorSamplerRegistry = event.getPack().getOrCreateRegistry(
|
||||
|
||||
@@ -11,10 +11,13 @@ import org.slf4j.LoggerFactory;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.dfsek.terra.addons.image.image.BufferedImageWrapper;
|
||||
import com.dfsek.terra.addons.image.image.Image;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.Loader;
|
||||
import com.dfsek.terra.api.properties.Properties;
|
||||
|
||||
|
||||
public class ImageLoader implements TypeLoader<Image> {
|
||||
@@ -23,19 +26,32 @@ public class ImageLoader implements TypeLoader<Image> {
|
||||
|
||||
private final Loader files;
|
||||
|
||||
public ImageLoader(Loader files) {
|
||||
private final ConfigPack pack;
|
||||
|
||||
public ImageLoader(Loader files, ConfigPack pack) {
|
||||
this.files = files;
|
||||
this.pack = pack;
|
||||
if(!pack.getContext().has(ImageCache.class))
|
||||
pack.getContext().put(new ImageCache(new ConcurrentHashMap<>()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image load(@NotNull AnnotatedType t, @NotNull Object c, @NotNull ConfigLoader loader, DepthTracker depthTracker)
|
||||
throws LoadException {
|
||||
try {
|
||||
return new BufferedImageWrapper(ImageIO.read(files.get((String) c)));
|
||||
} catch(IllegalArgumentException e) {
|
||||
throw new LoadException("Unable to load image (image might be too large?)", e, depthTracker);
|
||||
} catch(IOException e) {
|
||||
throw new LoadException("Unable to load image", e, depthTracker);
|
||||
}
|
||||
return pack.getContext().get(ImageCache.class).map.computeIfAbsent((String) c, imagePath -> {
|
||||
try {
|
||||
return new BufferedImageWrapper(ImageIO.read(files.get(imagePath)));
|
||||
} catch(IllegalArgumentException e) {
|
||||
throw new LoadException("Unable to load image (image might be too large?)", e, depthTracker);
|
||||
} catch(IOException e) {
|
||||
throw new LoadException("Unable to load image", e, depthTracker);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Cache prevents configs from loading the same image multiple times into memory
|
||||
*/
|
||||
private record ImageCache(ConcurrentHashMap<String, Image> map) implements Properties {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user