diff --git a/build.gradle.kts b/build.gradle.kts index 67e2b30a..d9646e87 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,6 +3,10 @@ plugins { id("com.gtnewhorizons.gtnhconvention") } +minecraft { + //extraRunJvmArguments.add("-Dgtnhlib.dumpClass=true") +} + // Add a Java 17 sourceset for including code optimized for newer Java versions val java17ToolchainSpec: JavaToolchainSpec.() -> Unit = { vendor = JvmVendorSpec.AZUL diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLibConfig.java b/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLibConfig.java index ac00a31d..ffea69d7 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLibConfig.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLibConfig.java @@ -9,6 +9,10 @@ public class GTNHLibConfig { @Config.DefaultBoolean(false) public static boolean ignoreNEIVersion; + @Config.Comment("Font rendering replacements") + @Config.DefaultBoolean(true) + public static boolean enableFontRendererMixin; + @Config.Comment("Larger values take more RAM, but require less model rebuilding (may reduce lag spikes). Unless you are very short on RAM, reducing this is not advised.") @Config.DefaultInt(1000) @Config.RangeInt(min = 1, max = 1_000_000) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/asm/ASMUtil.java b/src/main/java/com/gtnewhorizon/gtnhlib/asm/ASMUtil.java index 4f78bd11..b02f887b 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/asm/ASMUtil.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/asm/ASMUtil.java @@ -17,6 +17,9 @@ import org.objectweb.asm.util.ASMifier; import org.objectweb.asm.util.Textifier; import org.objectweb.asm.util.TraceClassVisitor; +// This class might be loaded by different class loaders, +// it should not reference any code from the main mod. +// See {@link com.gtnewhorizon.gtnhlib.core.shared.package-info} public class ASMUtil { private static final Logger logger = LogManager.getLogger("ASM GTNH"); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/asm/ClassConstantPoolParser.java b/src/main/java/com/gtnewhorizon/gtnhlib/asm/ClassConstantPoolParser.java index 11620445..47446ab3 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/asm/ClassConstantPoolParser.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/asm/ClassConstantPoolParser.java @@ -29,6 +29,9 @@ import java.util.Arrays; * Using this class to search for a (single) String reference is > 40 times faster than parsing a class with a * ClassReader + ClassNode while using way less RAM */ +// This class might be loaded by different class loaders, +// it should not reference any code from the main mod. +// See {@link com.gtnewhorizon.gtnhlib.core.shared.package-info} public class ClassConstantPoolParser { enum ConstantTypes { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java index 62654a54..2186586c 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java @@ -9,71 +9,40 @@ import net.minecraft.launchwrapper.Launch; import org.spongepowered.asm.launch.GlobalProperties; import org.spongepowered.asm.service.mojang.MixinServiceLaunchWrapper; -import com.google.common.eventbus.EventBus; -import com.google.common.eventbus.Subscribe; -import com.gtnewhorizon.gtnhlib.GTNHLib; import com.gtnewhorizon.gtnhlib.GTNHLibConfig; -import com.gtnewhorizon.gtnhlib.Tags; -import com.gtnewhorizon.gtnhlib.client.tooltip.LoreHolderDiscoverer; import com.gtnewhorizon.gtnhlib.config.ConfigException; import com.gtnewhorizon.gtnhlib.config.ConfigurationManager; -import com.gtnewhorizon.gtnhlib.eventbus.EventBusUtil; import com.gtnewhorizon.gtnhlib.mixins.Mixins; import com.gtnewhorizon.gtnhmixins.IEarlyMixinLoader; import com.gtnewhorizon.gtnhmixins.builders.IMixins; -import cpw.mods.fml.common.DummyModContainer; -import cpw.mods.fml.common.LoadController; -import cpw.mods.fml.common.ModMetadata; -import cpw.mods.fml.common.event.FMLConstructionEvent; import cpw.mods.fml.relauncher.FMLLaunchHandler; import cpw.mods.fml.relauncher.IFMLLoadingPlugin; @IFMLLoadingPlugin.MCVersion("1.7.10") -@IFMLLoadingPlugin.TransformerExclusions({ "com.gtnewhorizon.gtnhlib.asm.", "com.gtnewhorizon.gtnhlib.core.transformer", - "com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager", +@IFMLLoadingPlugin.TransformerExclusions({ "it.unimi.dsi.fastutil", "com.gtnewhorizon.gtnhlib.asm", + "com.gtnewhorizon.gtnhlib.core", "com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager", "com.gtnewhorizon.gtnhlib.client.renderer.CapturingTessellator" }) -@IFMLLoadingPlugin.SortingIndex(-1000) -public class GTNHLibCore extends DummyModContainer implements IFMLLoadingPlugin, IEarlyMixinLoader { +public class GTNHLibCore implements IFMLLoadingPlugin, IEarlyMixinLoader { - public static final String[] DEFAULT_TRANSFORMERS = new String[] { - "com.gtnewhorizon.gtnhlib.core.transformer.EventBusSubTransformer" }; + private static Boolean isObf; - static { + public GTNHLibCore() { try { ConfigurationManager.registerConfig(GTNHLibConfig.class); } catch (ConfigException e) { - GTNHLib.LOG.error("Failed to register GTNHLib config!", e); + throw new RuntimeException(e); } } - public GTNHLibCore() { - super(new ModMetadata()); - ModMetadata md = getMetadata(); - md.autogenerated = true; - md.modId = md.name = "GTNHLib Core"; - md.parent = "gtnhlib"; - md.version = Tags.VERSION; - } - @Override public String[] getASMTransformerClass() { - if (!FMLLaunchHandler.side().isClient() - || Launch.blackboard.getOrDefault("gtnhlib.rfbPluginLoaded", Boolean.FALSE) == Boolean.TRUE) { - // Don't need any transformers if we're not on the client, or the RFB Plugin was loaded - return DEFAULT_TRANSFORMERS; - } - // Directly add this to the MixinServiceLaunchWrapper tweaker's list of Tweak Classes - List mixinTweakClasses = GlobalProperties.get(MixinServiceLaunchWrapper.BLACKBOARD_KEY_TWEAKCLASSES); - if (mixinTweakClasses != null) { - mixinTweakClasses.add(MixinCompatHackTweaker.class.getName()); - } - return DEFAULT_TRANSFORMERS; + return new String[] { "com.gtnewhorizon.gtnhlib.core.fml.transformers.EventBusSubTransformer" }; } @Override public String getModContainerClass() { - return "com.gtnewhorizon.gtnhlib.core.GTNHLibCore"; + return "com.gtnewhorizon.gtnhlib.core.GTNHLibCoreModContainer"; } @Override @@ -82,7 +51,24 @@ public class GTNHLibCore extends DummyModContainer implements IFMLLoadingPlugin, } @Override - public void injectData(Map data) {} + public void injectData(Map data) { + isObf = (Boolean) data.get("runtimeDeobfuscationEnabled"); + if (FMLLaunchHandler.side().isClient()) { + boolean isGTNHLibRFBLoaded = (boolean) Launch.blackboard + .getOrDefault("gtnhlib.rfbPluginLoaded", Boolean.FALSE); + if (!isGTNHLibRFBLoaded) { + // If rfb isn't loaded we need to register the TessellatorRedirectorTransformer + // transformer, however this transformer needs to run late in the transformer + // chain, after mixins but before LWJGl3ify. If we were to register it normally + // in getASMTransformerClass() it would be sorted at index 0 which we do not want. + // So we instead register it inside an ITweaker that gets run by mixins. + List tweaks = GlobalProperties.get(MixinServiceLaunchWrapper.BLACKBOARD_KEY_TWEAKCLASSES); + if (tweaks != null) { + tweaks.add("com.gtnewhorizon.gtnhlib.core.fml.tweakers.LateTransformerRegistrationTweaker"); + } + } + } + } @Override public String getAccessTransformerClass() { @@ -100,16 +86,16 @@ public class GTNHLibCore extends DummyModContainer implements IFMLLoadingPlugin, } @Override - public boolean registerBus(EventBus bus, LoadController controller) { - bus.register(this); - return true; + public String toString() { + // Needed because the EarlyMixins loader uses the + // Object.toString() method to indentify coremods... + return "GTNHLib Core"; } - @Subscribe - public void construct(FMLConstructionEvent event) { - EventBusUtil.harvestData(event.getASMHarvestedData()); - if (event.getSide().isClient()) { - LoreHolderDiscoverer.harvestData(event.getASMHarvestedData()); + public static boolean isObf() { + if (isObf == null) { + throw new IllegalStateException("Obfuscation state has been accessed too early!"); } + return isObf; } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCoreModContainer.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCoreModContainer.java new file mode 100644 index 00000000..87830c93 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCoreModContainer.java @@ -0,0 +1,39 @@ +package com.gtnewhorizon.gtnhlib.core; + +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; +import com.gtnewhorizon.gtnhlib.Tags; +import com.gtnewhorizon.gtnhlib.client.tooltip.LoreHolderDiscoverer; +import com.gtnewhorizon.gtnhlib.eventbus.EventBusUtil; + +import cpw.mods.fml.common.DummyModContainer; +import cpw.mods.fml.common.LoadController; +import cpw.mods.fml.common.ModMetadata; +import cpw.mods.fml.common.event.FMLConstructionEvent; + +@SuppressWarnings("UnstableApiUsage") +public class GTNHLibCoreModContainer extends DummyModContainer { + + public GTNHLibCoreModContainer() { + super(new ModMetadata()); + ModMetadata md = getMetadata(); + md.autogenerated = true; + md.modId = md.name = "GTNHLib Core"; + md.parent = "gtnhlib"; + md.version = Tags.VERSION; + } + + @Override + public boolean registerBus(EventBus bus, LoadController controller) { + bus.register(this); + return true; + } + + @Subscribe + public void construct(FMLConstructionEvent event) { + EventBusUtil.harvestData(event.getASMHarvestedData()); + if (event.getSide().isClient()) { + LoreHolderDiscoverer.harvestData(event.getASMHarvestedData()); + } + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/MixinCompatHackTweaker.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/MixinCompatHackTweaker.java deleted file mode 100644 index d5fcad66..00000000 --- a/src/main/java/com/gtnewhorizon/gtnhlib/core/MixinCompatHackTweaker.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.gtnewhorizon.gtnhlib.core; - -import java.io.File; -import java.util.List; - -import net.minecraft.launchwrapper.ITweaker; -import net.minecraft.launchwrapper.Launch; -import net.minecraft.launchwrapper.LaunchClassLoader; - -import com.gtnewhorizon.gtnhlib.core.transformer.TessellatorRedirectorTransformer; - -import cpw.mods.fml.relauncher.FMLLaunchHandler; - -public class MixinCompatHackTweaker implements ITweaker { - - @Override - public void acceptOptions(List args, File gameDir, File assetsDir, String profile) { - // no-op - } - - @Override - public void injectIntoClassLoader(LaunchClassLoader classLoader) { - // no-op - } - - @Override - public String getLaunchTarget() { - return null; - } - - @Override - public String[] getLaunchArguments() { - if (FMLLaunchHandler.side().isClient()) { - final boolean rfbLoaded = Launch.blackboard.getOrDefault("gtnhlib.rfbPluginLoaded", Boolean.FALSE) - == Boolean.TRUE; - - if (!rfbLoaded) { - // Run after Mixins, but before LWJGl3ify - Launch.classLoader.registerTransformer(TessellatorRedirectorTransformer.class.getName()); - } - } - - return new String[0]; - } -} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/transformer/EventBusSubTransformer.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/fml/transformers/EventBusSubTransformer.java similarity index 90% rename from src/main/java/com/gtnewhorizon/gtnhlib/core/transformer/EventBusSubTransformer.java rename to src/main/java/com/gtnewhorizon/gtnhlib/core/fml/transformers/EventBusSubTransformer.java index 49106101..bc33f239 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/core/transformer/EventBusSubTransformer.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/fml/transformers/EventBusSubTransformer.java @@ -1,9 +1,12 @@ -package com.gtnewhorizon.gtnhlib.core.transformer; +package com.gtnewhorizon.gtnhlib.core.fml.transformers; import static com.gtnewhorizon.gtnhlib.eventbus.EventBusUtil.DEBUG_EVENT_BUS; import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import net.minecraft.launchwrapper.IClassTransformer; @@ -17,6 +20,7 @@ import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.MethodNode; +import com.gtnewhorizon.gtnhlib.core.shared.GTNHLibClassDump; import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber; import com.gtnewhorizon.gtnhlib.eventbus.EventBusUtil; import com.gtnewhorizon.gtnhlib.eventbus.MethodInfo; @@ -26,9 +30,6 @@ import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.relauncher.FMLLaunchHandler; import cpw.mods.fml.relauncher.SideOnly; -import it.unimi.dsi.fastutil.objects.Object2ObjectMap; -import it.unimi.dsi.fastutil.objects.Object2ObjectMaps; -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import it.unimi.dsi.fastutil.objects.ObjectSet; @@ -60,7 +61,7 @@ public class EventBusSubTransformer implements IClassTransformer { boolean changed = false; // Processing all of this from the ASMDataTable is way too slow for (MethodNode mn : cn.methods) { - Object2ObjectMap usableAnnotations = getUsableAnnotations(mn.visibleAnnotations); + Map usableAnnotations = getUsableAnnotations(mn.visibleAnnotations); if (usableAnnotations.isEmpty()) continue; if (!matchesSide(usableAnnotations.get(SIDEONLY_DESC))) { @@ -133,15 +134,17 @@ public class EventBusSubTransformer implements IClassTransformer { if (changed) { final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cn.accept(cw); - return cw.toByteArray(); + final byte[] transformedBytes = cw.toByteArray(); + GTNHLibClassDump.dumpClass(transformedName, basicClass, transformedBytes, this); + return transformedBytes; } return basicClass; } - private static Object2ObjectMap getUsableAnnotations(List annotations) { - if (annotations == null) return Object2ObjectMaps.emptyMap(); - Object2ObjectMap usable = new Object2ObjectOpenHashMap<>(); + private static Map getUsableAnnotations(List annotations) { + if (annotations == null) return Collections.emptyMap(); + Map usable = new HashMap<>(); for (AnnotationNode ann : annotations) { if (ANNOTATIONS.contains(ann.desc)) { usable.put(ann.desc, ann); @@ -151,7 +154,7 @@ public class EventBusSubTransformer implements IClassTransformer { } @SuppressWarnings("BooleanMethodIsAlwaysInverted") - private boolean matchesSide(AnnotationNode side) { + private static boolean matchesSide(AnnotationNode side) { if (side == null) return true; for (int x = 0; x < side.values.size() - 1; x += 2) { Object key = side.values.get(x); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/fml/transformers/TessellatorRedirectorTransformer.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/fml/transformers/TessellatorRedirectorTransformer.java new file mode 100644 index 00000000..cd087f51 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/fml/transformers/TessellatorRedirectorTransformer.java @@ -0,0 +1,50 @@ +package com.gtnewhorizon.gtnhlib.core.fml.transformers; + +import net.minecraft.launchwrapper.IClassTransformer; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.ClassNode; + +import com.gtnewhorizon.gtnhlib.core.GTNHLibCore; +import com.gtnewhorizon.gtnhlib.core.shared.GTNHLibClassDump; +import com.gtnewhorizon.gtnhlib.core.shared.transformers.TessellatorRedirector; + +/** IClassTransformer wrapper for {@link TessellatorRedirector} */ +public class TessellatorRedirectorTransformer implements IClassTransformer { + + private final TessellatorRedirector inner; + private final String[] exclusions; + + public TessellatorRedirectorTransformer() { + inner = new TessellatorRedirector(GTNHLibCore.isObf()); + exclusions = inner.getTransformerExclusions(); + } + + @Override + public byte[] transform(String name, String transformedName, byte[] basicClass) { + if (basicClass == null) return null; + + for (String exclusion : exclusions) { + if (transformedName.startsWith(exclusion)) return basicClass; + } + + if (!inner.shouldTransform(basicClass)) { + return basicClass; + } + + // Keep in sync with com.gtnewhorizons.angelica.loading.rfb.RedirectorTransformerWrapper + final ClassReader cr = new ClassReader(basicClass); + final ClassNode cn = new ClassNode(); + cr.accept(cn, 0); + final boolean changed = inner.transformClassNode(transformedName, cn); + if (changed) { + final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); + cn.accept(cw); + final byte[] transformedBytes = cw.toByteArray(); + GTNHLibClassDump.dumpClass(transformedName, basicClass, transformedBytes, this); + return transformedBytes; + } + return basicClass; + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/fml/tweakers/LateTransformerRegistrationTweaker.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/fml/tweakers/LateTransformerRegistrationTweaker.java new file mode 100644 index 00000000..be2b6ba7 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/fml/tweakers/LateTransformerRegistrationTweaker.java @@ -0,0 +1,42 @@ +package com.gtnewhorizon.gtnhlib.core.fml.tweakers; + +import java.io.File; +import java.util.List; + +import net.minecraft.launchwrapper.ITweaker; +import net.minecraft.launchwrapper.Launch; +import net.minecraft.launchwrapper.LaunchClassLoader; + +import cpw.mods.fml.relauncher.FMLRelaunchLog; + +// The ITweaker needs to be in a standalone package +// because FML adds the whole package to the class +// loader exclusion and that causes really hard to +// find bugs, approximately 2 hours since last commit.... +public class LateTransformerRegistrationTweaker implements ITweaker { + + @Override + public void acceptOptions(List args, File gameDir, File assetsDir, String profile) { + // no-op + } + + @Override + public void injectIntoClassLoader(LaunchClassLoader classLoader) { + // no-op + } + + @Override + public String getLaunchTarget() { + return null; + } + + @Override + public String[] getLaunchArguments() { + // We register this transformer here so that it + // runs after Mixins, but before LWJGl3ify + String transformer = "com.gtnewhorizon.gtnhlib.core.fml.transformers.TessellatorRedirectorTransformer"; + FMLRelaunchLog.finer("Registering transformer %s", transformer); + Launch.classLoader.registerTransformer(transformer); + return new String[0]; + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/rfb/GTNHLibRfbPlugin.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/rfb/GTNHLibRfbPlugin.java similarity index 60% rename from src/main/java/com/gtnewhorizon/gtnhlib/rfb/GTNHLibRfbPlugin.java rename to src/main/java/com/gtnewhorizon/gtnhlib/core/rfb/GTNHLibRfbPlugin.java index 8c1beb70..27b584f0 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/rfb/GTNHLibRfbPlugin.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/rfb/GTNHLibRfbPlugin.java @@ -1,10 +1,11 @@ -package com.gtnewhorizon.gtnhlib.rfb; +package com.gtnewhorizon.gtnhlib.core.rfb; import net.minecraft.launchwrapper.Launch; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import com.gtnewhorizon.gtnhlib.core.rfb.transformers.RFBTessellatorRedirector; import com.gtnewhorizons.retrofuturabootstrap.api.PluginContext; import com.gtnewhorizons.retrofuturabootstrap.api.RetroFuturaBootstrap; import com.gtnewhorizons.retrofuturabootstrap.api.RfbClassTransformer; @@ -19,11 +20,13 @@ public class GTNHLibRfbPlugin implements RfbPlugin { @Override public @NotNull RfbClassTransformer @Nullable [] makeTransformers() { - final boolean isServer = (null - == RetroFuturaBootstrap.API.launchClassLoader().findClassMetadata("net.minecraft.client.main.Main")); + boolean isServer = RetroFuturaBootstrap.API.launchClassLoader() + .findClassMetadata("net.minecraft.client.main.Main") == null; if (isServer) { return null; } - return new RfbClassTransformer[] { new TessellatorRedirectorTransformerWrapper() }; + boolean isObf = RetroFuturaBootstrap.API.launchClassLoader().findClassMetadata("net.minecraft.world.World") + == null; + return new RfbClassTransformer[] { new RFBTessellatorRedirector(isObf) }; } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/rfb/TessellatorRedirectorTransformerWrapper.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/rfb/transformers/RFBTessellatorRedirector.java similarity index 72% rename from src/main/java/com/gtnewhorizon/gtnhlib/rfb/TessellatorRedirectorTransformerWrapper.java rename to src/main/java/com/gtnewhorizon/gtnhlib/core/rfb/transformers/RFBTessellatorRedirector.java index 63149a4b..7ca05ae2 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/rfb/TessellatorRedirectorTransformerWrapper.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/rfb/transformers/RFBTessellatorRedirector.java @@ -1,4 +1,4 @@ -package com.gtnewhorizon.gtnhlib.rfb; +package com.gtnewhorizon.gtnhlib.core.rfb.transformers; import java.util.jar.Manifest; @@ -6,15 +6,20 @@ import org.intellij.lang.annotations.Pattern; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import com.gtnewhorizon.gtnhlib.core.transformer.TessellatorRedirectorTransformer; +import com.gtnewhorizon.gtnhlib.core.shared.GTNHLibClassDump; +import com.gtnewhorizon.gtnhlib.core.shared.transformers.TessellatorRedirector; import com.gtnewhorizons.retrofuturabootstrap.api.ClassNodeHandle; import com.gtnewhorizons.retrofuturabootstrap.api.ExtensibleClassLoader; import com.gtnewhorizons.retrofuturabootstrap.api.RfbClassTransformer; -/** RFB wrapper for {@link TessellatorRedirectorTransformer} */ -public class TessellatorRedirectorTransformerWrapper implements RfbClassTransformer { +/** RfbClassTransformer wrapper for {@link TessellatorRedirector} */ +public class RFBTessellatorRedirector implements RfbClassTransformer { - public final TessellatorRedirectorTransformer inner = new TessellatorRedirectorTransformer(); + private final TessellatorRedirector inner; + + public RFBTessellatorRedirector(boolean isObf) { + inner = new TessellatorRedirector(isObf); + } @Pattern("[a-z0-9-]+") @Override @@ -34,7 +39,7 @@ public class TessellatorRedirectorTransformerWrapper implements RfbClassTransfor @Override public @NotNull String @Nullable [] additionalExclusions() { - return TessellatorRedirectorTransformer.getTransformerExclusions().toArray(new String[0]); + return inner.getTransformerExclusions(); } @Override @@ -48,7 +53,7 @@ public class TessellatorRedirectorTransformerWrapper implements RfbClassTransfor // If a class is already a transformed ClassNode, conservatively continue processing. return true; } - return inner.shouldRfbTransform(classNode.getOriginalBytes()); + return inner.shouldTransform(classNode.getOriginalBytes()); } @Override @@ -57,6 +62,7 @@ public class TessellatorRedirectorTransformerWrapper implements RfbClassTransfor final boolean changed = inner.transformClassNode(className, classNode.getNode()); if (changed) { classNode.computeMaxs(); + GTNHLibClassDump.dumpRFBClass(className, classNode, this); } } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/GTNHLibClassDump.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/GTNHLibClassDump.java new file mode 100644 index 00000000..3e98b774 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/GTNHLibClassDump.java @@ -0,0 +1,25 @@ +package com.gtnewhorizon.gtnhlib.core.shared; + +import com.gtnewhorizon.gtnhlib.asm.ASMUtil; +import com.gtnewhorizons.retrofuturabootstrap.api.ClassNodeHandle; + +public class GTNHLibClassDump { + + private static final boolean DUMP_CLASS = Boolean.getBoolean("gtnhlib.dumpClass"); + + public static void dumpClass(String className, byte[] originalBytes, byte[] transformedBytes, Object transformer) { + if (DUMP_CLASS) { + ASMUtil.saveAsRawClassFile(originalBytes, className + "_PRE", transformer); + ASMUtil.saveAsRawClassFile(transformedBytes, className + "_POST", transformer); + } + } + + public static void dumpRFBClass(String className, ClassNodeHandle classNode, Object transformer) { + if (DUMP_CLASS) { + final byte[] originalBytes = classNode.getOriginalBytes(); + final byte[] transformedBytes = classNode.computeBytes(); + ASMUtil.saveAsRawClassFile(originalBytes, className + "_PRE", transformer); + ASMUtil.saveAsRawClassFile(transformedBytes, className + "_POST", transformer); + } + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/package-info.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/package-info.java new file mode 100644 index 00000000..e104a238 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/package-info.java @@ -0,0 +1,6 @@ +/** + * Package that contains classes that may be loaded both by RFB and FML classloaders, the code contained in this package + * should not reference any code from outside of this package to avoid causing hard to debug classloading issues unless + * the code is known to be safe with different classloaders. + */ +package com.gtnewhorizon.gtnhlib.core.shared; diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/transformers/TessellatorRedirector.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/transformers/TessellatorRedirector.java new file mode 100644 index 00000000..538138ce --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/shared/transformers/TessellatorRedirector.java @@ -0,0 +1,66 @@ +package com.gtnewhorizon.gtnhlib.core.shared.transformers; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; + +import com.gtnewhorizon.gtnhlib.asm.ClassConstantPoolParser; + +public final class TessellatorRedirector { + + private static final String TessellatorClass = "net/minecraft/client/renderer/Tessellator"; + + private final ClassConstantPoolParser cstPoolParser; + private final String OpenGlHelper$glBlendFunc; + private final String Tessellator$instance; + + public TessellatorRedirector(boolean isObf) { + cstPoolParser = new ClassConstantPoolParser(TessellatorClass); + OpenGlHelper$glBlendFunc = isObf ? "func_148821_a" : "glBlendFunc"; + Tessellator$instance = isObf ? "field_78398_a" : "instance"; + } + + public String[] getTransformerExclusions() { + return new String[] { "org.lwjgl", "com.gtnewhorizons.angelica.glsm.", "com.gtnewhorizons.angelica.transform", + "me.eigenraven.lwjgl3ify", "com.gtnewhorizon.gtnhlib", "net.minecraft.client.renderer.Tessellator" }; + } + + public boolean shouldTransform(byte[] basicClass) { + return cstPoolParser.find(basicClass, true); + } + + /** + * @return Was the class changed? + */ + public boolean transformClassNode(String className, ClassNode cn) { + if (cn == null) { + return false; + } + boolean changed = false; + for (MethodNode mn : cn.methods) { + if ("net.minecraft.client.renderer.OpenGlHelper".equals(className) + && mn.name.equals(OpenGlHelper$glBlendFunc)) { + continue; + } + for (AbstractInsnNode node : mn.instructions.toArray()) { + if (node.getOpcode() == Opcodes.GETSTATIC && node instanceof FieldInsnNode fNode) { + if (TessellatorClass.equals(fNode.owner) && fNode.name.equals(Tessellator$instance)) { + mn.instructions.set( + node, + new MethodInsnNode( + Opcodes.INVOKESTATIC, + "com/gtnewhorizon/gtnhlib/client/renderer/TessellatorManager", + "get", + "()Lnet/minecraft/client/renderer/Tessellator;", + false)); + changed = true; + } + } + } + } + return changed; + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/transformer/TessellatorRedirectorTransformer.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/transformer/TessellatorRedirectorTransformer.java deleted file mode 100644 index b2b508d3..00000000 --- a/src/main/java/com/gtnewhorizon/gtnhlib/core/transformer/TessellatorRedirectorTransformer.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.gtnewhorizon.gtnhlib.core.transformer; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import net.minecraft.launchwrapper.IClassTransformer; - -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.FieldInsnNode; -import org.objectweb.asm.tree.MethodInsnNode; -import org.objectweb.asm.tree.MethodNode; - -import com.gtnewhorizon.gtnhlib.asm.ClassConstantPoolParser; - -import lombok.val; - -public class TessellatorRedirectorTransformer implements IClassTransformer { - - private static final String TessellatorClass = "net/minecraft/client/renderer/Tessellator"; - - private static final ClassConstantPoolParser cstPoolParser = new ClassConstantPoolParser(TessellatorClass); - - private static final List TransformerExclusions = Arrays.asList( - "org.lwjgl", - "com.gtnewhorizons.angelica.glsm.", - "com.gtnewhorizons.angelica.transform", - "me.eigenraven.lwjgl3ify", - "com.gtnewhorizon.gtnhlib", - "net.minecraft.client.renderer.Tessellator"); - - public static List getTransformerExclusions() { - return Collections.unmodifiableList(TransformerExclusions); - } - - public boolean shouldRfbTransform(byte[] basicClass) { - return cstPoolParser.find(basicClass, true); - } - - @Override - public byte[] transform(String name, String transformedName, byte[] basicClass) { - if (basicClass == null) return null; - - for (val exclusion : TransformerExclusions) { - if (transformedName.startsWith(exclusion)) return basicClass; - } - - if (!cstPoolParser.find(basicClass, true)) { - return basicClass; - } - - // Keep in sync with com.gtnewhorizons.angelica.loading.rfb.RedirectorTransformerWrapper - final ClassReader cr = new ClassReader(basicClass); - final ClassNode cn = new ClassNode(); - cr.accept(cn, 0); - final boolean changed = transformClassNode(transformedName, cn); - if (changed) { - final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - cn.accept(cw); - return cw.toByteArray(); - } - return basicClass; - } - - /** - * @return Was the class changed? - */ - public boolean transformClassNode(String transformedName, ClassNode cn) { - if (cn == null) { - return false; - } - boolean changed = false; - for (MethodNode mn : cn.methods) { - if (transformedName.equals("net.minecraft.client.renderer.OpenGlHelper") - && (mn.name.equals("glBlendFunc") || mn.name.equals("func_148821_a"))) { - continue; - } - for (AbstractInsnNode node : mn.instructions.toArray()) { - if (node.getOpcode() == Opcodes.GETSTATIC && node instanceof FieldInsnNode fNode) { - if ((fNode.name.equals("field_78398_a") || fNode.name.equals("instance")) - && fNode.owner.equals(TessellatorClass)) { - // package com.gtnewhorizon.gtnhlib.client.renderer; - mn.instructions.set( - node, - new MethodInsnNode( - Opcodes.INVOKESTATIC, - "com/gtnewhorizon/gtnhlib/client/renderer/TessellatorManager", - "get", - "()Lnet/minecraft/client/renderer/Tessellator;", - false)); - changed = true; - } - } - } - - } - - return changed; - } - -} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java index 3d854b39..1361900d 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java @@ -1,8 +1,6 @@ package com.gtnewhorizon.gtnhlib.mixins; -import static com.gtnewhorizon.gtnhlib.GTNHLibConfig.autoTextureLoading; - -import com.gtnewhorizon.gtnhlib.util.EarlyConfig; +import com.gtnewhorizon.gtnhlib.GTNHLibConfig; import com.gtnewhorizon.gtnhmixins.builders.IMixins; import com.gtnewhorizon.gtnhmixins.builders.MixinBuilder; @@ -26,10 +24,10 @@ public enum Mixins implements IMixins { .setApplyIf(() -> Boolean.parseBoolean(System.getProperty("gtnhlib.debugtextures", "false")))), BRIGADIER(Side.COMMON, "MixinCommandHandler", "MixinCommandHelp"), FONT_RENDERER(new MixinBuilder("Font rendering replacements").addClientMixins("MixinFontRenderer") - .setPhase(Phase.EARLY).setApplyIf(() -> EarlyConfig.enableFontRendererMixin)), + .setPhase(Phase.EARLY).setApplyIf(() -> GTNHLibConfig.enableFontRendererMixin)), MODEL_TEXTURE_LOADING(new MixinBuilder("Automatically load model textures") .addClientMixins("models.MixinFileResourcePack", "models.MixinFolderResourcePack").setPhase(Phase.EARLY) - .setApplyIf(() -> autoTextureLoading)); + .setApplyIf(() -> GTNHLibConfig.autoTextureLoading)); private final MixinBuilder builder; diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinFontRenderer.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinFontRenderer.java index e02d0f54..e0d24d02 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinFontRenderer.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinFontRenderer.java @@ -34,6 +34,7 @@ public abstract class MixinFontRenderer implements IFontParameters { * @author DeathFuel * @reason (as above) */ + @SuppressWarnings("visibility") @Overwrite public int sizeStringToWidth(String str, int wrapWidth) { return FontRendering.sizeStringToWidth(str, wrapWidth, (FontRenderer) (Object) this); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/util/EarlyConfig.java b/src/main/java/com/gtnewhorizon/gtnhlib/util/EarlyConfig.java deleted file mode 100644 index 51119a36..00000000 --- a/src/main/java/com/gtnewhorizon/gtnhlib/util/EarlyConfig.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.gtnewhorizon.gtnhlib.util; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Reader; -import java.io.Writer; -import java.util.Properties; - -import net.minecraft.launchwrapper.Launch; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -// Based on EarlyConfig.java from Hodgepodge -public final class EarlyConfig { - - private static final Logger LOGGER = LogManager.getLogger("GTNHLibEarly"); - - public static final boolean enableFontRendererMixin; - - static { - Properties config = new Properties(); - File configLocation = new File(Launch.minecraftHome, "config/gtnhlibEarly.properties"); - try (Reader r = new BufferedReader(new FileReader(configLocation))) { - config.load(r); - } catch (FileNotFoundException e) { - LOGGER.debug("No existing configuration file. Will use defaults"); - } catch (IOException e) { - LOGGER.error("Error reading configuration file. Will use defaults", e); - } - enableFontRendererMixin = Boolean.parseBoolean(config.getProperty("enableFontRendererMixin", "true")); - config.setProperty("enableFontRendererMixin", String.valueOf(enableFontRendererMixin)); - try (Writer r = new BufferedWriter(new FileWriter(configLocation))) { - config.store(r, "Config file for anything that's loaded before GTNHLibConfig"); - } catch (IOException e) { - LOGGER.error("Error writing configuration file", e); - } - } -} diff --git a/src/main/resources/META-INF/rfb-plugin/gtnhlib.properties b/src/main/resources/META-INF/rfb-plugin/gtnhlib.properties index 0e4005a7..6e526478 100644 --- a/src/main/resources/META-INF/rfb-plugin/gtnhlib.properties +++ b/src/main/resources/META-INF/rfb-plugin/gtnhlib.properties @@ -1,8 +1,8 @@ name=GTNHLib version=${version} additionalVersions= -className=com.gtnewhorizon.gtnhlib.rfb.GTNHLibRfbPlugin -transformerExclusions=com.gtnewhorizon.gtnhlib.core.transformer,com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager,com.gtnewhorizon.gtnhlib.client.renderer.CapturingTessellator +className=com.gtnewhorizon.gtnhlib.core.rfb.GTNHLibRfbPlugin +transformerExclusions=com.gtnewhorizon.gtnhlib.core.shared,com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager,com.gtnewhorizon.gtnhlib.client.renderer.CapturingTessellator versionConstraints= loadBefore= loadAfter=