Refactor to just "Blur"

This commit is contained in:
tterrag1098
2017-05-24 21:38:13 -04:00
parent 2bf5f6c838
commit 8696849722
6 changed files with 20 additions and 18 deletions

View File

@@ -0,0 +1,143 @@
package com.tterrag.blur;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import com.google.common.base.Throwables;
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.shader.Shader;
import net.minecraft.client.shader.ShaderGroup;
import net.minecraft.client.shader.ShaderUniform;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.RenderTickEvent;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
@Mod(modid = MODID, name = MOD_NAME, version = VERSION, acceptedMinecraftVersions = "[1.9, 1.12)", clientSideOnly = true, guiFactory = "com.tterrag.blurbg.config.BlurGuiFactory")
public class Blur {
public static final String MODID = "blur";
public static final String MOD_NAME = "Blur";
public static final String VERSION = "@VERSION@";
@Instance
public static Blur instance;
public Configuration config;
private String[] blurExclusions;
private Field _listShaders;
private long start;
private int fadeTime;
private int colorFirst, colorSecond;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(this);
config = new Configuration(new File(event.getModConfigurationDirectory(), "blur.cfg"));
saveConfig();
}
private void saveConfig() {
blurExclusions = config.getStringList("guiExclusions", Configuration.CATEGORY_GENERAL, new String[] {
GuiChat.class.getName(),
}, "A list of classes to be excluded from the blur shader.");
fadeTime = config.getInt("fadeTime", Configuration.CATEGORY_GENERAL, 200, 0, Integer.MAX_VALUE, "The time it takes for the blur to fade in, in ms.");
colorFirst = Integer.parseUnsignedInt(
config.getString("gradientStartColor", Configuration.CATEGORY_GENERAL, "66000000", "The start color of the background gradient. Given in ARGB hex."),
16
);
colorSecond = Integer.parseUnsignedInt(
config.getString("gradientEndColor", Configuration.CATEGORY_GENERAL, "66000000", "The end color of the background gradient. Given in ARGB hex."),
16
);
config.save();
}
@SubscribeEvent
public void onConfigChanged(OnConfigChangedEvent event) {
if (event.getModID().equals(MODID)) {
saveConfig();
}
}
@SuppressWarnings("null")
@SubscribeEvent
public void onGuiChange(GuiOpenEvent event) {
if (_listShaders == null) {
_listShaders = ReflectionHelper.findField(ShaderGroup.class, "field_148031_d", "listShaders");
}
if (Minecraft.getMinecraft().world != null) {
EntityRenderer er = Minecraft.getMinecraft().entityRenderer;
if (!er.isShaderActive() && event.getGui() != null && !ArrayUtils.contains(blurExclusions, event.getGui().getClass().getName())) {
er.loadShader(new ResourceLocation("shaders/post/fade_in_blur.json"));
start = System.currentTimeMillis();
} else if (er.isShaderActive() && event.getGui() == null) {
er.stopUseShader();
}
}
}
private float getProgress() {
return Math.min((System.currentTimeMillis() - start) / (float) fadeTime, 1);
}
@SuppressWarnings("null")
@SubscribeEvent
public void onRenderTick(RenderTickEvent event) {
if (event.phase == Phase.END && Minecraft.getMinecraft().currentScreen != null && Minecraft.getMinecraft().entityRenderer.isShaderActive()) {
ShaderGroup sg = Minecraft.getMinecraft().entityRenderer.getShaderGroup();
try {
@SuppressWarnings("unchecked")
List<Shader> shaders = (List<Shader>) _listShaders.get(sg);
for (Shader s : shaders) {
ShaderUniform su = s.getShaderManager().getShaderUniform("Progress");
if (su != null) {
su.set(getProgress());
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
Throwables.propagate(e);
}
}
}
public static int getBackgroundColor(boolean second) {
int color = second ? instance.colorSecond : instance.colorFirst;
int a = color >> 24;
int r = (color >> 16) & 0xFF;
int b = (color >> 8) & 0xFF;
int g = color & 0xFF;
float prog = instance.getProgress();
a *= prog;
r *= prog;
g *= prog;
b *= prog;
return a << 24 | r << 16 | b << 8 | g;
}
}

View File

@@ -0,0 +1,33 @@
package com.tterrag.blur;
import java.util.Map;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
@IFMLLoadingPlugin.SortingIndex(Integer.MAX_VALUE)
public class BlurPlugin implements IFMLLoadingPlugin {
@Override
public String[] getASMTransformerClass() {
return new String[] { "com.tterrag.blur.BlurTransformer" };
}
@Override
public String getModContainerClass() {
return null;
}
@Override
public String getSetupClass() {
return null;
}
@Override
public void injectData(Map<String, Object> data) {
}
@Override
public String getAccessTransformerClass() {
return null;
}
}

View File

@@ -0,0 +1,79 @@
package com.tterrag.blur;
import java.util.Iterator;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import net.minecraft.launchwrapper.IClassTransformer;
public class BlurTransformer implements IClassTransformer {
private static final String GUI_SCREEN_CLASS_NAME = "net.minecraft.client.gui.GuiScreen";
private static final String DRAW_WORLD_BAGKGROUND_METHOD = "drawWorldBackground";
private static final String DRAW_WORLD_BAGKGROUND_METHOD_OBF = "func_146270_b";
private static final String BLUR_MAIN_CLASS = "com/tterrag/blur/Blur";
private static final String COLOR_HOOK_METHOD_NAME = "getBackgroundColor";
private static final String COLOR_HOOK_METHOD_DESC = "(Z)I";
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (transformedName.equals(GUI_SCREEN_CLASS_NAME)) {
System.out.println("Transforming Class [" + transformedName + "], Method [" + DRAW_WORLD_BAGKGROUND_METHOD + "]");
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
Iterator<MethodNode> methods = classNode.methods.iterator();
while (methods.hasNext()) {
MethodNode m = methods.next();
if (m.name.equals(DRAW_WORLD_BAGKGROUND_METHOD) || m.name.equals(DRAW_WORLD_BAGKGROUND_METHOD_OBF)) {
for (int i = 0; i < m.instructions.size(); i++) {
AbstractInsnNode next = m.instructions.get(i);
// if (next.getOpcode() == Opcodes.INVOKEVIRTUAL && ((MethodInsnNode)next).name.equals(DRAW_GRADIENT_RECT_METHOD_NAME)) {
// while (!(next instanceof LabelNode)) {
// m.instructions.remove(next);
// next = m.instructions.get(--i);
// }
// break;
// }
if (next.getOpcode() == Opcodes.LDC) {
System.out.println("Modifying GUI background darkness... ");
AbstractInsnNode colorHook = new MethodInsnNode(Opcodes.INVOKESTATIC, BLUR_MAIN_CLASS, COLOR_HOOK_METHOD_NAME, COLOR_HOOK_METHOD_DESC, false);
AbstractInsnNode colorHook2 = colorHook.clone(null);
// Replace LDC with hooks
m.instructions.set(next, colorHook);
m.instructions.set(colorHook.getNext(), colorHook2);
// Load boolean constants for method param
m.instructions.insertBefore(colorHook, new InsnNode(Opcodes.ICONST_0));
m.instructions.insertBefore(colorHook2, new InsnNode(Opcodes.ICONST_1));
break;
}
}
break;
}
}
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classNode.accept(cw);
System.out.println("Transforming " + transformedName + " Finished.");
return cw.toByteArray();
}
return basicClass;
}
}

View File

@@ -0,0 +1,15 @@
package com.tterrag.blur.config;
import com.tterrag.blur.Blur;
import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.config.GuiConfig;
public class BlurConfigGui extends GuiConfig {
public BlurConfigGui(GuiScreen parentScreen) {
super(parentScreen, new ConfigElement(Blur.instance.config.getCategory(Configuration.CATEGORY_GENERAL)).getChildElements(), Blur.MODID, false, false, Blur.MODID + ".config.title");
}
}

View File

@@ -0,0 +1,30 @@
package com.tterrag.blur.config;
import java.util.Set;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.fml.client.IModGuiFactory;
public class BlurGuiFactory implements IModGuiFactory {
@Override
public void initialize(Minecraft minecraftInstance) {}
@Override
public Class<? extends GuiScreen> mainConfigGuiClass() {
return BlurConfigGui.class;
}
@Override
public Set<RuntimeOptionCategoryElement> runtimeGuiCategories() {
return null;
}
@Override
@Deprecated
public RuntimeOptionGuiHandler getHandlerFor(RuntimeOptionCategoryElement element) {
return null;
}
}