From ec97b82ed607ae7f137a7c0aac2281296702fbe4 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Thu, 6 Jan 2022 15:25:52 -0800 Subject: [PATCH] Add Iris compatibility - Use the Iris API to detect shader rendering state - Remove CommandNameProviderEnum and use a Function argument instead --- build.gradle | 2 + .../jozufozu/flywheel/backend/Backend.java | 12 +- .../flywheel/backend/IrisShaderHandler.java | 51 ++++++++ .../flywheel/backend/OptifineHandler.java | 115 ------------------ .../flywheel/config/ConfigCommands.java | 16 +-- .../jozufozu/flywheel/config/FlwEngine.java | 5 +- .../jozufozu/flywheel/mixin/FrustumMixin.java | 4 +- .../flywheel/mixin/ShaderCloseMixin.java | 35 ------ src/main/resources/fabric.mod.json | 3 + src/main/resources/flywheel.mixins.json | 1 - 10 files changed, 70 insertions(+), 174 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/IrisShaderHandler.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java delete mode 100644 src/main/java/com/jozufozu/flywheel/mixin/ShaderCloseMixin.java diff --git a/build.gradle b/build.gradle index 53694c28a..48d33b3cd 100644 --- a/build.gradle +++ b/build.gradle @@ -57,6 +57,8 @@ dependencies { modCompileOnly 'curse.maven:starlight-521783:3554912' + modCompileOnly 'maven.modrinth:iris:1.18.x-v1.1.4' + implementation 'com.google.code.findbugs:jsr305:3.0.2' modCompileOnly 'maven.modrinth:indium:1.0.2-alpha1+mc1.18' modCompileOnly 'io.vram:frex-fabric-mc118:6.0.229' diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index 2285f5684..56d7ef009 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -1,6 +1,10 @@ package com.jozufozu.flywheel.backend; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import javax.annotation.Nullable; @@ -42,8 +46,6 @@ public class Backend { protected Backend() { loader = new Loader(this); - - OptifineHandler.init(); } /** @@ -97,8 +99,6 @@ public class Backend { } public void refresh() { - OptifineHandler.refresh(); - capabilities = GL.createCapabilities(); compat = new GlCompat(capabilities); @@ -165,7 +165,7 @@ public class Backend { FlwEngine preferredChoice = FlwConfig.get() .getEngine(); - boolean usingShaders = OptifineHandler.usingShaders(); + boolean usingShaders = IrisShaderHandler.isShaderPackInUse(); boolean canUseEngine = switch (preferredChoice) { case OFF -> true; case BATCHING -> !usingShaders; diff --git a/src/main/java/com/jozufozu/flywheel/backend/IrisShaderHandler.java b/src/main/java/com/jozufozu/flywheel/backend/IrisShaderHandler.java new file mode 100644 index 000000000..2ac8a70a9 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/IrisShaderHandler.java @@ -0,0 +1,51 @@ +package com.jozufozu.flywheel.backend; + +import net.fabricmc.loader.api.FabricLoader; +import net.irisshaders.iris.api.v0.IrisApi; + +public class IrisShaderHandler { + public static final boolean IRIS_LOADED = FabricLoader.getInstance().isModLoaded("iris"); + + private static final InternalHandler HANDLER; + + static { + if (IRIS_LOADED) { + HANDLER = new InternalHandlerImpl(); + } else { + HANDLER = new InternalHandler() {}; + } + } + + private IrisShaderHandler() { + } + + public static boolean isShaderPackInUse() { + return HANDLER.isShaderPackInUse(); + } + + public static boolean isRenderingShadowPass() { + return HANDLER.isRenderingShadowPass(); + } + + private interface InternalHandler { + default boolean isShaderPackInUse() { + return false; + }; + + default boolean isRenderingShadowPass() { + return false; + }; + } + + private static class InternalHandlerImpl implements InternalHandler { + @Override + public boolean isShaderPackInUse() { + return IrisApi.getInstance().isShaderPackInUse(); + }; + + @Override + public boolean isRenderingShadowPass() { + return IrisApi.getInstance().isRenderingShadowPass(); + }; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java b/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java deleted file mode 100644 index 51e60eebe..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.jozufozu.flywheel.backend; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.Optional; -import java.util.function.BooleanSupplier; - -import com.jozufozu.flywheel.util.Lazy; - -import net.minecraft.client.Minecraft; - -public class OptifineHandler { - public static final String OPTIFINE_ROOT_PACKAGE = "net.optifine"; - public static final String SHADER_PACKAGE = "net.optifine.shaders"; - - private static Package optifine; - private static OptifineHandler handler; - - private static final Lazy isShadowPass = Lazy.of(() -> { - try { - Class ofShaders = Class.forName("net.optifine.shaders.Shaders"); - Field field = ofShaders.getDeclaredField("isShadowPass"); - field.setAccessible(true); - return () -> { - try { - return field.getBoolean(null); - } catch (IllegalAccessException ignored) { - return false; - } - }; - } catch (Exception ignored) { - return () -> false; - } - }); - - public final boolean usingShaders; - - public OptifineHandler(boolean usingShaders) { - this.usingShaders = usingShaders; - } - - /** - * Get information about the current Optifine configuration. - * - * @return {@link Optional#empty()} if Optifine is not installed. - */ - public static Optional get() { - return Optional.ofNullable(handler); - } - - public static boolean optifineInstalled() { - return optifine != null; - } - - public static boolean usingShaders() { - return OptifineHandler.get() - .map(OptifineHandler::isUsingShaders) - .orElse(false); - } - - public static boolean isShadowPass() { - return isShadowPass.get().getAsBoolean(); - } - - public static void init() { - optifine = Package.getPackage(OPTIFINE_ROOT_PACKAGE); - - if (optifine == null) { - Backend.LOGGER.info("Optifine not detected."); - } else { - Backend.LOGGER.info("Optifine detected."); - - refresh(); - } - } - - public static void refresh() { - if (optifine == null) return; - - boolean shadersOff = areShadersDisabledInOptifineConfigFile(); - - handler = new OptifineHandler(!shadersOff); - } - - private static boolean areShadersDisabledInOptifineConfigFile() { - File dir = Minecraft.getInstance().gameDirectory; - - File shaderOptions = new File(dir, "optionsshaders.txt"); - - boolean shadersOff = true; - try (BufferedReader reader = new BufferedReader(new FileReader(shaderOptions))) { - - shadersOff = reader.lines() - .anyMatch(it -> { - String line = it.replaceAll("\\s", ""); - if (line.startsWith("shaderPack=")) { - String setting = line.substring("shaderPack=".length()); - - return setting.equals("OFF") || setting.equals("(internal)"); - } - return false; - }); - } catch (IOException e) { - Backend.LOGGER.info("No shader config found."); - } - return shadersOff; - } - - public boolean isUsingShaders() { - return usingShaders; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/config/ConfigCommands.java b/src/main/java/com/jozufozu/flywheel/config/ConfigCommands.java index 7bea0eb93..631c23592 100644 --- a/src/main/java/com/jozufozu/flywheel/config/ConfigCommands.java +++ b/src/main/java/com/jozufozu/flywheel/config/ConfigCommands.java @@ -1,6 +1,7 @@ package com.jozufozu.flywheel.config; import java.util.function.BiConsumer; +import java.util.function.Function; import org.jetbrains.annotations.NotNull; @@ -22,6 +23,7 @@ public final class ConfigCommands { ConfigCommandBuilder commandBuilder = new ConfigCommandBuilder("flywheel"); commandBuilder.addOption(config.engine, "backend", (builder, option) -> enumOptionCommand(builder, config, option, + FlwEngine::getShortName, (source, value) -> { source.sendFeedback(getEngineMessage(value)); }, @@ -67,19 +69,13 @@ public final class ConfigCommands { })); } - public static > void enumOptionCommand(LiteralArgumentBuilder builder, FlwConfig config, EnumOption option, BiConsumer displayAction, BiConsumer setAction) { + public static > void enumOptionCommand(LiteralArgumentBuilder builder, FlwConfig config, EnumOption option, Function nameFunc, BiConsumer displayAction, BiConsumer setAction) { builder.executes(context -> { displayAction.accept(context.getSource(), option.get()); return Command.SINGLE_SUCCESS; }); for (E constant : option.getEnumType().getEnumConstants()) { - String name; - if (constant instanceof CommandNameProviderEnum nameProvider) { - name = nameProvider.getCommandName(); - } else { - name = constant.name(); - } - builder.then(ClientCommandManager.literal(name) + builder.then(ClientCommandManager.literal(nameFunc.apply(constant)) .executes(context -> { option.set(constant); setAction.accept(context.getSource(), option.get()); @@ -132,8 +128,4 @@ public final class ConfigCommands { ClientCommandManager.DISPATCHER.register(command); } } - - public interface CommandNameProviderEnum { - String getCommandName(); - } } diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwEngine.java b/src/main/java/com/jozufozu/flywheel/config/FlwEngine.java index 61fee4797..f5665e1f4 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwEngine.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwEngine.java @@ -6,7 +6,7 @@ import java.util.Map; import javax.annotation.Nullable; -public enum FlwEngine implements ConfigCommands.CommandNameProviderEnum { +public enum FlwEngine { OFF("off", "Off"), BATCHING("batching", "Parallel Batching"), INSTANCING("instancing", "GL33 Instanced Arrays"), @@ -29,8 +29,7 @@ public enum FlwEngine implements ConfigCommands.CommandNameProviderEnum { this.properName = properName; } - @Override - public String getCommandName() { + public String getShortName() { return shortName; } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/FrustumMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/FrustumMixin.java index 7f7ceded0..adb69949e 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/FrustumMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/FrustumMixin.java @@ -5,7 +5,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.jozufozu.flywheel.backend.OptifineHandler; +import com.jozufozu.flywheel.backend.IrisShaderHandler; import com.jozufozu.flywheel.core.LastActiveCamera; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.fabric.event.FlywheelEvents; @@ -18,7 +18,7 @@ public class FrustumMixin { @Inject(method = "prepare", at = @At("TAIL")) private void onPrepare(double x, double y, double z, CallbackInfo ci) { - if (OptifineHandler.isShadowPass()) { + if (IrisShaderHandler.isRenderingShadowPass()) { FlywheelEvents.BEGIN_FRAME.invoker().handleEvent(new BeginFrameEvent(Minecraft.getInstance().level, LastActiveCamera.getActiveCamera(), (Frustum) (Object) this)); } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/ShaderCloseMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/ShaderCloseMixin.java deleted file mode 100644 index ea641e03f..000000000 --- a/src/main/java/com/jozufozu/flywheel/mixin/ShaderCloseMixin.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.jozufozu.flywheel.mixin; - -import javax.annotation.Nullable; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import com.jozufozu.flywheel.backend.OptifineHandler; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.screens.Screen; -import net.minecraft.client.gui.screens.VideoSettingsScreen; - -@Mixin(Minecraft.class) -public class ShaderCloseMixin { - - @Shadow - @Nullable - public Screen screen; - - @Inject(at = @At("HEAD"), method = "setScreen") - private void whenScreenChanges(Screen screen, CallbackInfo info) { - if (OptifineHandler.optifineInstalled() && screen instanceof VideoSettingsScreen) { - Screen old = this.screen; - if (old != null && old.getClass() - .getName() - .startsWith(OptifineHandler.SHADER_PACKAGE)) { - OptifineHandler.refresh(); - } - } - } -} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 50bba9c41..b4e42c927 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -34,5 +34,8 @@ "fabric": "*", "minecraft": "1.18.x", "java": ">=17" + }, + "breaks": { + "iris": "<1.1.4" } } diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 1af7bff1d..9be6ed2eb 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -19,7 +19,6 @@ "LevelRendererMixin", "PausedPartialTickAccessor", "RenderTexturesMixin", - "ShaderCloseMixin", "ShaderInstanceAccessor", "atlas.AtlasDataMixin", "atlas.SheetDataAccessor",