Skip to content

Commit

Permalink
Merge pull request #103 from LostLuma/overlay-optimization-opt-out
Browse files Browse the repository at this point in the history
Add overlay optimization opt out method
  • Loading branch information
LostLuma authored Sep 2, 2023
2 parents 747b431 + 9e36ecd commit de50a9a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dynamic_fps.impl.util.HudInfoRenderer;
import dynamic_fps.impl.util.KeyMappingHandler;
import dynamic_fps.impl.util.Logging;
import dynamic_fps.impl.util.ModCompatibility;
import dynamic_fps.impl.util.OptionsHolder;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
Expand Down Expand Up @@ -49,8 +50,7 @@ public class DynamicFPSMod implements ClientModInitializer {
// inactive.
private static boolean hasRenderedLastFrame = false;

// rrls removes the loading overlay entirely, so we can not apply our things
private static final boolean OVERLAY_OPTIMIZATION_ACTIVE = !FabricLoader.getInstance().isModLoaded("rrls");
private static final boolean OVERLAY_OPTIMIZATION_ACTIVE = !ModCompatibility.disableOverlayOptimization();

private static final KeyMappingHandler toggleForcedKeyBinding = new KeyMappingHandler(
translationKey("key", "toggle_forced"),
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dynamic_fps/impl/mixin/ScreenMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import dynamic_fps.impl.util.DynamicFPSScreen;
import dynamic_fps.impl.util.ScreenOptimizationCompat;
import dynamic_fps.impl.util.ModCompatibility;
import net.minecraft.client.gui.screens.Screen;

@Mixin(Screen.class)
Expand All @@ -32,12 +32,12 @@ public class ScreenMixin implements DynamicFPSScreen {
private void onInit(CallbackInfo callbackInfo) {
String name = this.getClass().getName();

this.dynamic_fps$hasOptedOut = ScreenOptimizationCompat.isOptedOut(name);
this.dynamic_fps$hasOptedOut = ModCompatibility.isScreenOptedOut(name);

// Allow other mods to opt out on behalf of vanilla screens
// That Dynamic FPS forced to opt in via its own mod metadata.
if (!this.dynamic_fps$hasOptedOut) {
this.dynamic_fps$canOptimize = ScreenOptimizationCompat.isOptedIn(name);
this.dynamic_fps$canOptimize = ModCompatibility.isScreenOptedIn(name);
}
}

Expand Down
83 changes: 83 additions & 0 deletions src/main/java/dynamic_fps/impl/util/ModCompatibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package dynamic_fps.impl.util;

import java.util.HashSet;
import java.util.Set;

import org.jetbrains.annotations.Nullable;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.CustomValue;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.fabricmc.loader.api.metadata.CustomValue.CvObject;
import net.fabricmc.loader.api.metadata.CustomValue.CvType;

public class ModCompatibility {
private static boolean disableOverlayOptimization = false;

private static Set<String> optedInScreens = new HashSet<>();
private static Set<String> optedOutScreens = new HashSet<>();

static {
FabricLoader.getInstance().getAllMods().forEach(ModCompatibility::parseModMetadata);
}

public static boolean disableOverlayOptimization() {
return disableOverlayOptimization;
}

public static boolean isScreenOptedIn(String className) {
return optedInScreens.contains(className);
}

public static boolean isScreenOptedOut(String className) {
return optedOutScreens.contains(className);
}

private static void parseModMetadata(ModContainer mod) {
CvObject root;
ModMetadata data = mod.getMetadata();

if (data.getId().equals("rrls")) {
disableOverlayOptimization = true;
}

try {
root = data.getCustomValue("dynamic_fps").getAsObject();
} catch (ClassCastException | NullPointerException e) {
return; // Object is either missing or is of an invalid type
}

parseOverlayOverride(root.get("optimized_overlay"));

parseScreenOverrides(root.get("optimized_screens"), "enabled", optedInScreens);
parseScreenOverrides(root.get("optimized_screens"), "disabled", optedOutScreens);
}

private static void parseOverlayOverride(@Nullable CustomValue value) {
if (value != null && value.getType() == CvType.BOOLEAN && !value.getAsBoolean()) {
disableOverlayOptimization = true;
}
}

private static void parseScreenOverrides(@Nullable CustomValue parent, String type, Set<String> set) {
if (parent == null || parent.getType() != CvType.OBJECT) {
return;
}

var values = parent.getAsObject().get(type);

if (values == null || values.getType() != CvType.ARRAY) {
return;
}

var resolver = FabricLoader.getInstance().getMappingResolver();

values.getAsArray().forEach(value -> {
if (value.getType() == CvType.STRING) {
// Translate from intermediary to runtime names for vanilla screens
set.add(resolver.mapClassName("intermediary", value.getAsString()));
}
});
}
}
61 changes: 0 additions & 61 deletions src/main/java/dynamic_fps/impl/util/ScreenOptimizationCompat.java

This file was deleted.

0 comments on commit de50a9a

Please sign in to comment.