Add caching of resourcepack data, to prevent excessive disk I/O

This commit is contained in:
tterrag1098
2017-05-30 01:01:08 -04:00
parent 558f3c0091
commit 82639855db
2 changed files with 107 additions and 73 deletions

View File

@@ -4,16 +4,20 @@ import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.tterrag.blur.util.ShaderResourcePack; import com.tterrag.blur.util.ShaderResourcePack;
import static com.tterrag.blur.Blur.*; import static com.tterrag.blur.Blur.*;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiChat; import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.renderer.EntityRenderer; import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.resources.IResourcePack; import net.minecraft.client.resources.IResourcePack;
import net.minecraft.client.resources.SimpleReloadableResourceManager;
import net.minecraft.client.shader.Shader; import net.minecraft.client.shader.Shader;
import net.minecraft.client.shader.ShaderGroup; import net.minecraft.client.shader.ShaderGroup;
import net.minecraft.client.shader.ShaderUniform; import net.minecraft.client.shader.ShaderUniform;
@@ -49,20 +53,26 @@ public class Blur {
private long start; private long start;
private int fadeTime; private int fadeTime;
public int radius; public int radius; // Store default so we don't trigger an extra reload
private int colorFirst, colorSecond; private int colorFirst, colorSecond;
@Nonnull
private ShaderResourcePack dummyPack = new ShaderResourcePack();
@SuppressWarnings("unchecked")
@EventHandler @EventHandler
public void preInit(FMLPreInitializationEvent event) { public void preInit(FMLPreInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(this);
// Add our dummy resourcepack // 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")); config = new Configuration(new File(event.getModConfigurationDirectory(), "blur.cfg"));
saveConfig(); saveConfig();
} }
@SuppressWarnings("null")
private void saveConfig() { private void saveConfig() {
blurExclusions = config.getStringList("guiExclusions", Configuration.CATEGORY_GENERAL, new String[] { 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."); 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( colorFirst = Integer.parseUnsignedInt(
config.getString("gradientStartColor", Configuration.CATEGORY_GENERAL, "75000000", "The start color of the background gradient. Given in ARGB hex."), config.getString("gradientStartColor", Configuration.CATEGORY_GENERAL, "75000000", "The start color of the background gradient. Given in ARGB hex."),
16 16

View File

@@ -1,70 +1,86 @@
package com.tterrag.blur.util; package com.tterrag.blur.util;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.IOException;
import java.util.Scanner; import java.io.InputStream;
import java.util.Set; import java.util.HashMap;
import java.util.Map;
import net.minecraft.client.resources.IResourcePack; import java.util.Scanner;
import net.minecraft.client.resources.data.IMetadataSection; import java.util.Set;
import net.minecraft.client.resources.data.MetadataSerializer;
import net.minecraft.util.ResourceLocation; import com.google.common.collect.ImmutableSet;
import com.tterrag.blur.Blur;
import com.google.common.collect.ImmutableSet;
import com.tterrag.blur.Blur; import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
public class ShaderResourcePack implements IResourcePack { import net.minecraft.client.resources.IResourcePack;
import net.minecraft.client.resources.data.IMetadataSection;
protected boolean validPath(ResourceLocation location) { import net.minecraft.client.resources.data.MetadataSerializer;
return location.getResourceDomain().equals("minecraft") && location.getResourcePath().startsWith("shaders/"); import net.minecraft.client.resources.data.PackMetadataSection;
} import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextComponentString;
@Override
public InputStream getInputStream(ResourceLocation location) throws IOException { public class ShaderResourcePack implements IResourcePack, IResourceManagerReloadListener {
if (validPath(location)) {
InputStream in = Blur.class.getResourceAsStream("/" + location.getResourcePath()); protected boolean validPath(ResourceLocation location) {
StringBuilder data = new StringBuilder(); return location.getResourceDomain().equals("minecraft") && location.getResourcePath().startsWith("shaders/");
Scanner scan = new Scanner(in); }
try {
while (scan.hasNextLine()) { private final Map<ResourceLocation, String> loadedData = new HashMap<>();
data.append(scan.nextLine().replaceAll("@radius@", Integer.toString(Blur.instance.radius))).append('\n');
} @Override
} finally { public InputStream getInputStream(ResourceLocation location) throws IOException {
scan.close(); if (validPath(location)) {
} String s = loadedData.computeIfAbsent(location, loc -> {
InputStream in = Blur.class.getResourceAsStream("/" + location.getResourcePath());
return new ByteArrayInputStream(data.toString().getBytes()); StringBuilder data = new StringBuilder();
} Scanner scan = new Scanner(in);
return null; try {
} while (scan.hasNextLine()) {
data.append(scan.nextLine().replaceAll("@radius@", Integer.toString(Blur.instance.radius))).append('\n');
@Override }
public boolean resourceExists(ResourceLocation location) { } finally {
return validPath(location) && Blur.class.getResource("/" + location.getResourcePath()) != null; scan.close();
} }
return data.toString();
@Override });
public Set<String> getResourceDomains() {
return ImmutableSet.of("minecraft"); return new ByteArrayInputStream(s.getBytes());
} }
throw new FileNotFoundException(location.toString());
@Override }
public <T extends IMetadataSection> T getPackMetadata(
MetadataSerializer metadataSerializer, String metadataSectionName) @Override
throws IOException { public boolean resourceExists(ResourceLocation location) {
return null; return validPath(location) && Blur.class.getResource("/" + location.getResourcePath()) != null;
} }
@Override @Override
public BufferedImage getPackImage() throws IOException { public Set<String> getResourceDomains() {
return null; return ImmutableSet.of("minecraft");
} }
@Override @SuppressWarnings("unchecked")
public String getPackName() { @Override
return "Blur dummy resource pack"; 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 {
throw new FileNotFoundException("pack.png");
}
@Override
public String getPackName() {
return "Blur dummy resource pack";
}
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
loadedData.clear();
}
}