Skip to content

Commit

Permalink
fix: optimize chroma for Patcher's Optimized Font Renderer and fixes #33
Browse files Browse the repository at this point in the history


remove: deprecated chroma feature
  • Loading branch information
Fix3dll committed Nov 7, 2024
1 parent f824b42 commit aa14c3f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public class ConfigValues {
@Deprecated
private final MutableFloat oldChromaSpeed = new MutableFloat(0.19354838F); // 2.0
private final MutableObject<EnumUtils.ChromaMode> chromaMode = new MutableObject<>(EnumUtils.ChromaMode.FADE);
private final MutableFloat chromaFadeWidth = new MutableFloat(0.22580644F); // 10° Hue
private final MutableObject<DiscordStatus> discordDetails = new MutableObject<>(DiscordStatus.LOCATION);
private final MutableObject<DiscordStatus> discordStatus = new MutableObject<>(DiscordStatus.AUTO_STATUS);
private final MutableObject<DiscordStatus> discordAutoDefault = new MutableObject<>(DiscordStatus.NONE);
Expand Down Expand Up @@ -211,7 +210,6 @@ public void loadValues() {
deserializeNumber(healingCircleOpacity, "healingCircleOpacity", float.class);
deserializeNumber(chromaSize, "chromaSize", float.class);
deserializeEnumValueFromOrdinal(chromaMode, "chromaMode");
deserializeNumber(chromaFadeWidth, "chromaFadeWidth", float.class);
deserializeEnumValueFromOrdinal(discordStatus, "discordStatus");
deserializeEnumValueFromOrdinal(discordDetails, "discordDetails");
deserializeEnumValueFromOrdinal(discordAutoDefault, "discordAutoDefault");
Expand Down Expand Up @@ -974,14 +972,6 @@ public void setChromaMode(EnumUtils.ChromaMode chromaMode) {
this.chromaMode.setValue(chromaMode);
}

public void setChromaFadeWidth(float chromaFadeWidth) {
this.chromaFadeWidth.setValue(chromaFadeWidth);
}

public float getChromaFadeWidth() {
return chromaFadeWidth.getValue();
}

public void setDiscordDetails(DiscordStatus discordDetails) {
this.discordDetails.setValue(discordDetails);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ public enum Feature {
TEXT_STYLE(-1, "settings.textStyle", null, false),
CHROMA_SPEED(-1, "settings.chromaSpeed", null, false),
CHROMA_MODE(-1, "settings.chromaMode", null, false),
@Deprecated CHROMA_FADE_WIDTH(-1, "settings.chromaFadeWidth", null, false),
CHROMA_SIZE(-1, "settings.chromaSize", null, false),
CHROMA_SATURATION(-1, "settings.chromaSaturation", null, false),
CHROMA_BRIGHTNESS(-1, "settings.chromaBrightness", null, false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
import net.minecraft.client.renderer.GlStateManager;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;

import java.util.LinkedHashMap;
import java.util.Map;

public class FontRendererHook {

private static final SkyblockColor CHROMA_COLOR = new SkyblockColor(0xFFFFFFFF).setColorAnimation(SkyblockColor.ColorAnimation.CHROMA);
private static final DrawStateFontRenderer DRAW_CHROMA = new DrawStateFontRenderer(CHROMA_COLOR);
private static final SkyblockColor CHROMA_COLOR_SHADOW = new SkyblockColor(0xFF555555).setColorAnimation(SkyblockColor.ColorAnimation.CHROMA);
private static final DrawStateFontRenderer DRAW_CHROMA_SHADOW = new DrawStateFontRenderer(CHROMA_COLOR_SHADOW);
private static final MaxSizeHashMap<String, Boolean> stringsWithChroma = new MaxSizeHashMap<>(1000);
private static DrawStateFontRenderer currentDrawState = null;
private static boolean modInitialized = false;
private static final int CHROMA_FORMAT_INDEX = 22;
private static final int WHITE_FORMAT_INDEX = 15;
private static boolean turnAllText = false;
private static Feature fontFeature = null;

public static void beforeRenderChar() {
if (!shouldRenderChroma()) return;
Expand All @@ -43,57 +40,41 @@ public static void setupFeatureFont(Feature feature) {

ConfigValues config = SkyblockAddons.getInstance().getConfigValues();
if (config.getChromaMode() == EnumUtils.ChromaMode.FADE && config.getChromaFeatures().contains(feature)) {
DRAW_CHROMA.setupMulticolorFeature(config.getGuiScale(feature));
DRAW_CHROMA_SHADOW.setupMulticolorFeature(config.getGuiScale(feature));
fontFeature = feature;
}
}

public static void endFeatureFont() {
DRAW_CHROMA.endMulticolorFeature();
DRAW_CHROMA_SHADOW.endMulticolorFeature();
}

/**
* Called in patcher code to stop patcher optimization and do vanilla render
* @param s string to render
* @return true to override
*/
public static boolean shouldOverridePatcher(String s) {
if (shouldRenderChroma()) {
//return chromaStrings.get(s) == null || chromaStrings.get(s);
if (stringsWithChroma.get(s) != null) {
return stringsWithChroma.get(s);
}
// Check if there is a "§z" colorcode in the string and cache it
boolean hasChroma = false;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '§') {
i++;
if (i < s.length() && (s.charAt(i) == 'z' || s.charAt(i) == 'Z')) {
hasChroma = true;
break;
}
}
}
stringsWithChroma.put(s, hasChroma);
return hasChroma;
} else {
return false;
}
fontFeature = null;
}

/**
* Called to save the current shader state
*/
public static void beginRenderString(boolean shadow) {
if (modInitialized && SkyblockAddons.getInstance().getUtils().isOnSkyblock()) {
currentDrawState = shadow ? DRAW_CHROMA_SHADOW : DRAW_CHROMA;
currentDrawState.loadFeatureColorEnv();

boolean allChroma = Feature.TURN_ALL_TEXTS_CHROMA.isEnabled();
if (allChroma || currentDrawState.isUsingShader()) {
float rgb = shadow ? 0.2f : 1f;
GlStateManager.color(rgb, rgb, rgb, ColorUtils.getAlpha());
ConfigValues config = SkyblockAddons.getInstance().getConfigValues();

if (allChroma || fontFeature != null) {
if (fontFeature != null) {
DRAW_CHROMA.setupMulticolorFeature(config.getGuiScale(fontFeature));
DRAW_CHROMA_SHADOW.setupMulticolorFeature(config.getGuiScale(fontFeature));
}

currentDrawState = shadow ? DRAW_CHROMA_SHADOW : DRAW_CHROMA;
currentDrawState.loadFeatureColorEnv();
} else {
DRAW_CHROMA.endMulticolorFeature();
DRAW_CHROMA_SHADOW.endMulticolorFeature();
}

if (allChroma || (currentDrawState != null && currentDrawState.isUsingShader())) {
// There is no need to force the white color if there is no fading
if (config.getChromaMode() == EnumUtils.ChromaMode.FADE) {
float rgb = shadow ? 0.2f : 1f;
GlStateManager.color(rgb, rgb, rgb, ColorUtils.getAlpha());
}
if (allChroma) {
setupFeatureFont(Feature.TURN_ALL_TEXTS_CHROMA);
turnAllText = true;
Expand All @@ -114,11 +95,20 @@ public static void restoreChromaState() {
/**
* Called to turn chroma on
*/
public static boolean toggleChromaOn(int formatIndex) {
if (shouldRenderChroma() && formatIndex == CHROMA_FORMAT_INDEX) {
public static boolean toggleChromaOn(int formatIndex, boolean shadow) {
if (!modInitialized || !SkyblockAddons.getInstance().getUtils().isOnSkyblock()) {
return false;
}

if (formatIndex == CHROMA_FORMAT_INDEX) {
if (currentDrawState == null) {
currentDrawState = shadow ? DRAW_CHROMA_SHADOW : DRAW_CHROMA;
currentDrawState.loadFeatureColorEnv();
}
currentDrawState.newColorEnv().bindActualColor();
return true;
}

return false;
}

Expand All @@ -128,6 +118,7 @@ public static boolean toggleChromaOn(int formatIndex) {
public static void endRenderString() {
if (shouldRenderChroma()) {
currentDrawState.endColorEnv();
currentDrawState = null;

if (turnAllText && !Feature.TURN_ALL_TEXTS_CHROMA.isEnabled()) {
endFeatureFont();
Expand All @@ -145,23 +136,6 @@ public static int forceWhiteColor(int formatIndex) {
return formatIndex;
}

/**
* HashMap with upper limit on storage size. Used to enforce the font renderer cache not getting too large over time
*/
public static class MaxSizeHashMap<K, V> extends LinkedHashMap<K, V> {
private final int maxSize;

public MaxSizeHashMap(int maxSize) {
super();
this.maxSize = maxSize;
}

@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > maxSize;
}
}

/**
* Called by {@link SkyblockAddons#postInit(FMLPostInitializationEvent)}
* Fixes NPE caused by Splash Screen, calling FontRendererHook before SBA is loaded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void insertRestoreChromaState(CallbackInfo ci) {
*/
@Inject(method = "renderStringAtPos", at = @At(value = "INVOKE", target = "Ljava/lang/String;indexOf(I)I", ordinal = 0, shift = At.Shift.BY, by = 2), locals = LocalCapture.CAPTURE_FAILHARD)
public void toggleChromaCondition(String text, boolean shadow, CallbackInfo ci, int i, char c0, int i1) {
if (FontRendererHook.toggleChromaOn(i1)) {
if (FontRendererHook.toggleChromaOn(i1, shadow)) {
this.resetStyles();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PatcherFontRendererTransformer {

@Inject(method = "renderStringAtPos(Ljava/lang/String;Z)Z", at = @At("HEAD"), cancellable = true)
public void overridePatcherFontRenderer(String string, boolean shadow, CallbackInfoReturnable<Boolean> cir) {
if (FontRendererHook.shouldRenderChroma()) {
if (FontRendererHook.shouldRenderChroma() || string.contains("§z") || string.contains("§Z")) {
cir.cancel();
cir.setReturnValue(false);
}
Expand Down

0 comments on commit aa14c3f

Please sign in to comment.