Clean up reflection, add resource pack image and reload listener

This commit is contained in:
tterrag
2018-12-12 14:41:22 -05:00
parent 20fdff17ff
commit 12c5daa5f6
6 changed files with 87 additions and 43 deletions

View File

@@ -27,12 +27,10 @@ minecraft {
}
dependencies {
minecraft "com.mojang:minecraft:18w49a"
mappings "net.fabricmc:yarn:18w49a.10"
modCompile "net.fabricmc:fabric-loader:0.2.0.62"
// Fabric API. This is technically optional, but you probably want it anyway.
modCompile "net.fabricmc:fabric:0.1.0.37"
minecraft "com.mojang:minecraft:18w50a"
mappings "net.fabricmc:yarn:18w50a.2"
modCompile "net.fabricmc:fabric-loader:0.2.0.64"
modCompile "net.fabricmc:fabric:0.1.1.47"
implementation 'com.google.code.findbugs:jsr305:3.0.2'
}

View File

@@ -1,5 +1,6 @@
package com.tterrag.blur;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
@@ -7,9 +8,10 @@ import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import com.google.common.base.Throwables;
import com.tterrag.blur.mixin.MixinWorldRenderer;
import com.tterrag.blur.util.ReflectionHelper;
import com.tterrag.blur.util.ShaderResourcePack;
import net.fabricmc.api.ModInitializer;
@@ -20,6 +22,8 @@ import net.minecraft.client.gl.Shader;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.resource.ClientResourcePackContainer;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.resource.ReloadableResourceManager;
import net.minecraft.resource.ResourcePackCompatibility;
import net.minecraft.resource.ResourcePackContainer;
import net.minecraft.resource.ResourcePackContainer.Factory;
@@ -52,36 +56,35 @@ public class Blur implements ModInitializer {
public static Blur instance;
public Blur() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
Field _rps;
try {
_rps = MinecraftClient.class.getDeclaredField("field_1715");
} catch (NoSuchFieldException e) {
_rps = MinecraftClient.class.getDeclaredField("resourcePackContainerManager");
}
_rps.setAccessible(true);
ResourcePackContainerManager<ClientResourcePackContainer> rps = (ResourcePackContainerManager<ClientResourcePackContainer>)_rps.get(MinecraftClient.getInstance());
ResourcePackContainerManager<ClientResourcePackContainer> rps = ReflectionHelper.getValue(MinecraftClient.class, MinecraftClient.getInstance(), "field_1715", "resourcePackContainerManager");
rps.addCreator(new ResourcePackCreator() {
@Override
public <T extends ResourcePackContainer> void registerContainer(Map<String, T> var1, Factory<T> factory) {
T var3 = (T) new ClientResourcePackContainer("blur", true, () -> dummyPack, new StringTextComponent(dummyPack.getName()), new StringTextComponent(dummyPack.getName()), ResourcePackCompatibility.COMPATIBLE, SortingDirection.BOTTOM, true, null);
if (var3 != null) {
var1.put("blur", var3);
}
}
@Override
public <T extends ResourcePackContainer> void registerContainer(Map<String, T> var1, Factory<T> factory) {
NativeImage img = null;
try {
img = NativeImage.fromInputStream(dummyPack.openRoot("pack.png"));
} catch (IOException e) {
LogManager.getLogger().error("Could not load blur's pack.png", e);
}
@SuppressWarnings("unchecked")
T var3 = (T) new ClientResourcePackContainer("blur", true, () -> dummyPack,
new StringTextComponent(dummyPack.getName()), new StringTextComponent("Default shaders for Blur"),
ResourcePackCompatibility.COMPATIBLE, SortingDirection.BOTTOM, true, img);
if (var3 != null) {
var1.put("blur", var3);
}
}
});
instance = this;
}
@Override
public void onInitialize() {
// Add our dummy resourcepack
// ((ReloadableResourceManager)MinecraftClient.getInstance().getResourceManager()).addListener(dummyPack);
// config = new Configuration(new File(event.getModConfigurationDirectory(), "blur.cfg"));
// saveConfig();
public void onInitialize() {}
public void registerReloadListeners(ReloadableResourceManager manager) {
manager.addListener(dummyPack);
}
/* private void saveConfig() {
@@ -123,12 +126,7 @@ public class Blur implements ModInitializer {
*/
public void onGuiChange(Gui newGui) {
if (_listShaders == null) {
try {
_listShaders = class_279.class.getDeclaredField("field_1497");
_listShaders.setAccessible(true);
} catch (NoSuchFieldException | SecurityException e) {
throw new RuntimeException(e);
}
_listShaders = ReflectionHelper.getField(class_279.class, "field_1497");
}
if (MinecraftClient.getInstance().world != null) {
WorldRenderer er = MinecraftClient.getInstance().worldRenderer;
@@ -159,7 +157,7 @@ public class Blur implements ModInitializer {
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
Throwables.propagate(e);
throw new RuntimeException(e);
}
}
}

View File

@@ -4,27 +4,40 @@ import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.tterrag.blur.Blur;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Gui;
import net.minecraft.resource.ReloadableResourceManager;
@Mixin(MinecraftClient.class)
public class MixinMinecraftClient {
@Inject(method = "openGui(Lnet/minecraft/client/gui/Gui;)V",
at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;currentGui:Lnet/minecraft/client/gui/Gui;", opcode = Opcodes.PUTFIELD, ordinal = 0))
at = @At(value = "FIELD",
target = "net/minecraft/client/MinecraftClient.currentGui : Lnet/minecraft/client/gui/Gui;",
opcode = Opcodes.PUTFIELD))
public void onGuiOpen(Gui newGui, CallbackInfo info) {
Blur.instance.onGuiChange(newGui);
}
@Inject(method = "method_1523(Z)V",
at = @At(value = "INVOKE", target = "net/minecraft/client/toast/ToastManager.draw()V"),
at = @At(value = "INVOKE",
target = "net/minecraft/client/toast/ToastManager.draw()V"),
require = 1)
public void onPostRenderTick(CallbackInfo info) {
Blur.instance.onPostRenderTick();
}
@Inject(method = "init()V",
at = @At(value = "FIELD",
target = "net/minecraft/client/MinecraftClient.resourceManager : Lnet/minecraft/resource/ReloadableResourceManager;",
opcode = Opcodes.PUTFIELD,
shift = Shift.AFTER))
public void onResourceManagerAssign(CallbackInfo info) {
Blur.instance.registerReloadListeners((ReloadableResourceManager) MinecraftClient.getInstance().getResourceManager());
}
}

View File

@@ -0,0 +1,36 @@
package com.tterrag.blur.util;
import java.lang.reflect.Field;
import java.util.Arrays;
public class ReflectionHelper {
@SuppressWarnings("unchecked")
public static <T> T getValue(Class<?> cls, Object instance, String...names) {
try {
return (T) getField(cls, names).get(instance);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static Field getField(Class<?> cls, String... names) {
for (String name : names) {
Field f = getFieldInternal(cls, name);
if (f != null) {
return f;
}
}
throw new IllegalArgumentException("Could not find any of fields " + Arrays.toString(names) + " on class " + cls);
}
private static Field getFieldInternal(Class<?> cls, String name) {
try {
Field f = cls.getDeclaredField(name);
f.setAccessible(true);
return f;
} catch (NoSuchFieldException | SecurityException e) {
return null;
}
}
}

View File

@@ -75,7 +75,7 @@ public class ShaderResourcePack implements ResourcePack, ResourceReloadListener
@Override
public String getName() {
return "Blur dummy resource pack";
return "Blur Shaders";
}
@Override
@@ -88,12 +88,11 @@ public class ShaderResourcePack implements ResourcePack, ResourceReloadListener
@Override
public InputStream openRoot(String var1) throws IOException {
return open(ResourceType.ASSETS, new Identifier(var1));
return Blur.class.getResourceAsStream("/assets/blur/" + var1);
}
@Override
public Collection<Identifier> findResources(ResourceType var1, String var2, int var3, Predicate<String> var4) {
return Collections.emptyList();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 KiB