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