mirror of
https://github.com/Motschen/Blur.git
synced 2025-12-16 11:45:09 +01:00
Add caching of resourcepack data, to prevent excessive disk I/O
This commit is contained in:
@@ -4,16 +4,20 @@ import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import com.tterrag.blur.util.ShaderResourcePack;
|
||||
|
||||
import static com.tterrag.blur.Blur.*;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiChat;
|
||||
import net.minecraft.client.renderer.EntityRenderer;
|
||||
import net.minecraft.client.resources.IResourcePack;
|
||||
import net.minecraft.client.resources.SimpleReloadableResourceManager;
|
||||
import net.minecraft.client.shader.Shader;
|
||||
import net.minecraft.client.shader.ShaderGroup;
|
||||
import net.minecraft.client.shader.ShaderUniform;
|
||||
@@ -49,20 +53,26 @@ public class Blur {
|
||||
private long start;
|
||||
private int fadeTime;
|
||||
|
||||
public int radius;
|
||||
public int radius; // Store default so we don't trigger an extra reload
|
||||
private int colorFirst, colorSecond;
|
||||
|
||||
@Nonnull
|
||||
private ShaderResourcePack dummyPack = new ShaderResourcePack();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
|
||||
// Add our dummy resourcepack
|
||||
((List<IResourcePack>)ReflectionHelper.getPrivateValue(Minecraft.class, Minecraft.getMinecraft(), "field_110449_ao", "defaultResourcePacks")).add(new ShaderResourcePack());
|
||||
((List<IResourcePack>)ReflectionHelper.getPrivateValue(Minecraft.class, Minecraft.getMinecraft(), "field_110449_ao", "defaultResourcePacks")).add(dummyPack);
|
||||
((SimpleReloadableResourceManager)Minecraft.getMinecraft().getResourceManager()).registerReloadListener(dummyPack);
|
||||
|
||||
config = new Configuration(new File(event.getModConfigurationDirectory(), "blur.cfg"));
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
@SuppressWarnings("null")
|
||||
private void saveConfig() {
|
||||
|
||||
blurExclusions = config.getStringList("guiExclusions", Configuration.CATEGORY_GENERAL, new String[] {
|
||||
@@ -71,7 +81,15 @@ public class Blur {
|
||||
|
||||
fadeTime = config.getInt("fadeTime", Configuration.CATEGORY_GENERAL, 200, 0, Integer.MAX_VALUE, "The time it takes for the blur to fade in, in ms.");
|
||||
|
||||
radius = config.getInt("radius", Configuration.CATEGORY_GENERAL, 12, 1, 100, "The radius of the blur effect. This controls how \"strong\" the blur is.");
|
||||
int r = config.getInt("radius", Configuration.CATEGORY_GENERAL, 12, 1, 100, "The radius of the blur effect. This controls how \"strong\" the blur is.");
|
||||
if (r != radius) {
|
||||
radius = r;
|
||||
dummyPack.onResourceManagerReload(Minecraft.getMinecraft().getResourceManager());
|
||||
if (Minecraft.getMinecraft().world != null) {
|
||||
Minecraft.getMinecraft().entityRenderer.stopUseShader();
|
||||
}
|
||||
}
|
||||
|
||||
colorFirst = Integer.parseUnsignedInt(
|
||||
config.getString("gradientStartColor", Configuration.CATEGORY_GENERAL, "75000000", "The start color of the background gradient. Given in ARGB hex."),
|
||||
16
|
||||
|
||||
@@ -2,28 +2,38 @@ package com.tterrag.blur.util;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.client.resources.IResourcePack;
|
||||
import net.minecraft.client.resources.data.IMetadataSection;
|
||||
import net.minecraft.client.resources.data.MetadataSerializer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.tterrag.blur.Blur;
|
||||
|
||||
public class ShaderResourcePack implements IResourcePack {
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.client.resources.IResourceManagerReloadListener;
|
||||
import net.minecraft.client.resources.IResourcePack;
|
||||
import net.minecraft.client.resources.data.IMetadataSection;
|
||||
import net.minecraft.client.resources.data.MetadataSerializer;
|
||||
import net.minecraft.client.resources.data.PackMetadataSection;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
|
||||
public class ShaderResourcePack implements IResourcePack, IResourceManagerReloadListener {
|
||||
|
||||
protected boolean validPath(ResourceLocation location) {
|
||||
return location.getResourceDomain().equals("minecraft") && location.getResourcePath().startsWith("shaders/");
|
||||
}
|
||||
|
||||
private final Map<ResourceLocation, String> loadedData = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream(ResourceLocation location) throws IOException {
|
||||
if (validPath(location)) {
|
||||
String s = loadedData.computeIfAbsent(location, loc -> {
|
||||
InputStream in = Blur.class.getResourceAsStream("/" + location.getResourcePath());
|
||||
StringBuilder data = new StringBuilder();
|
||||
Scanner scan = new Scanner(in);
|
||||
@@ -34,10 +44,12 @@ public class ShaderResourcePack implements IResourcePack {
|
||||
} finally {
|
||||
scan.close();
|
||||
}
|
||||
return data.toString();
|
||||
});
|
||||
|
||||
return new ByteArrayInputStream(data.toString().getBytes());
|
||||
return new ByteArrayInputStream(s.getBytes());
|
||||
}
|
||||
return null;
|
||||
throw new FileNotFoundException(location.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,16 +62,15 @@ public class ShaderResourcePack implements IResourcePack {
|
||||
return ImmutableSet.of("minecraft");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T extends IMetadataSection> T getPackMetadata(
|
||||
MetadataSerializer metadataSerializer, String metadataSectionName)
|
||||
throws IOException {
|
||||
return null;
|
||||
public <T extends IMetadataSection> T getPackMetadata(MetadataSerializer metadataSerializer, String metadataSectionName) throws IOException {
|
||||
return (T) new PackMetadataSection(new TextComponentString("Blur's default shaders"), 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedImage getPackImage() throws IOException {
|
||||
return null;
|
||||
throw new FileNotFoundException("pack.png");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,4 +78,9 @@ public class ShaderResourcePack implements IResourcePack {
|
||||
return "Blur dummy resource pack";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResourceManagerReload(IResourceManager resourceManager) {
|
||||
loadedData.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user