Skip to content

Commit

Permalink
Better Than AT
Browse files Browse the repository at this point in the history
  • Loading branch information
kappa-maintainer committed Mar 14, 2024
1 parent 60ffe64 commit f277a12
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 32 deletions.
22 changes: 13 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ minecraft {

mappings channel: mappings_channel, version: mappings_version

accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
//accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')

legacy {
fixClasspath = true
Expand Down Expand Up @@ -116,9 +116,9 @@ repositories {
}

dependencies {
minecraft "com.cleanroommc:cleanroom:15.24.0.3030"
implementation "top.outlands:foundation:0.10.0"
implementation 'org.javassist:javassist:3.30.2-GA'
minecraft "com.cleanroommc:cleanroom:15.24.0.3031"
//implementation "top.outlands:foundation:0.10.0"
//implementation 'org.javassist:javassist:3.30.2-GA'

//Charset Lib
compileOnly(fg.deobf("curse.maven:charset-284523:3656492"))
Expand Down Expand Up @@ -162,7 +162,11 @@ dependencies {
//EnderCore
compileOnly(fg.deobf("com.enderio.core:EnderCore:1.12.2-0.5.78:core"))
//runtimeOnly(fg.deobf("com.enderio.core:EnderCore:1.12.2-0.5.78:core"))


//TickCentral
compileOnly(fg.deobf("curse.maven:tickcentral-377201:3140670"))
//runtimeOnly(fg.deobf("curse.maven:lag-goggles-283525:4493884"))

//Lag Goggles
compileOnly(fg.deobf("curse.maven:lag-goggles-283525:4493884"))
//runtimeOnly(fg.deobf("curse.maven:lag-goggles-283525:4493884"))
Expand All @@ -184,10 +188,10 @@ dependencies {
annotationProcessor 'com.google.guava:guava:33.0.0-jre'
annotationProcessor 'com.google.code.gson:gson:2.10.1'

annotationProcessor ('com.cleanroommc:sponge-mixin:0.17.2+mixin.0.8.5')
implementation 'com.cleanroommc:sponge-mixin:0.17.6+mixin.0.8.5'
annotationProcessor ('com.cleanroommc:sponge-mixin:0.17.6+mixin.0.8.5')
//implementation 'com.cleanroommc:sponge-mixin:0.17.6+mixin.0.8.5'
annotationProcessor ("io.github.llamalad7:mixinextras-common:0.3.5")
implementation "io.github.llamalad7:mixinextras-common:0.3.5"
//implementation "io.github.llamalad7:mixinextras-common:0.3.5"
}


Expand All @@ -201,7 +205,7 @@ jar {
//'TweakClass': 'org.spongepowered.asm.launch.MixinTweaker',
'FMLCorePlugin': 'com.cleanroommc.FugueLoadingPlugin',
'ForceLoadAsMod': true,
'FMLAT': 'accesstransformer.cfg',
//'FMLAT': 'accesstransformer.cfg',
'Implementation-Title': project.name,
'Implementation-Version': mod_version,
'Implementation-Vendor': mod_authors,
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mappings_version=39-1.12
mod_id=fugue
mod_name=Fugue
mod_main_class=Fugue
mod_version=0.9.0
mod_version=0.9.4
mod_base_package=com.cleanroommc.fugue
mod_authors=kappa_maintainer
mod_description=A mod that patch dead mods for Cleanroom
16 changes: 13 additions & 3 deletions src/main/java/com/cleanroommc/Fugue.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.*;
import net.minecraftforge.fml.common.network.NetworkCheckHandler;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

@Mod(
modid = Reference.MOD_ID,
name = Reference.MOD_NAME,
useMetadata = true,
version = Reference.MOD_VERSION,
dependencies = Reference.MOD_DEPENDENCIES
dependencies = Reference.MOD_DEPENDENCIES,
acceptableRemoteVersions = "*"
)
public class Fugue {

Expand All @@ -21,7 +26,12 @@ public class Fugue {
@Instance(Reference.MOD_ID)
public static Fugue _instance;
public Fugue() {}


@NetworkCheckHandler
public boolean checker(Map<String, String> mods, Side side) {
return true;
}
/*
@EventHandler
public static void preInit(FMLPreInitializationEvent event) {
}
Expand All @@ -44,5 +54,5 @@ public void onServerStarted(FMLServerStartedEvent event) {
@EventHandler
public void onServerStopping(FMLServerStoppingEvent event) {
}
}*/
}
12 changes: 12 additions & 0 deletions src/main/java/com/cleanroommc/FugueLoadingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.cleanroommc.config.FugueConfig;
import com.cleanroommc.transformer.*;
import com.cleanroommc.transformer.universal.*;
import net.minecraft.launchwrapper.IClassNameTransformer;
import net.minecraft.launchwrapper.Launch;
import net.minecraftforge.common.config.ConfigManager;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
import top.outlands.foundation.IExplicitTransformer;
Expand All @@ -14,6 +16,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@IFMLLoadingPlugin.MCVersion("1.12.2")
@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -60,6 +63,12 @@ public class FugueLoadingPlugin implements IFMLLoadingPlugin, IEarlyMixinLoader
},
new InitializerTransformer()
);
TransformerDelegate.registerExplicitTransformerByInstance(
new String[]{
"com.github.terminatornl.tickcentral.api.ClassSniffer",
},
new ClassSnifferTransformer()
);
}
if (FugueConfig.enableLP){
TransformerDelegate.registerExplicitTransformerByInstance(
Expand Down Expand Up @@ -131,6 +140,9 @@ public class FugueLoadingPlugin implements IFMLLoadingPlugin, IEarlyMixinLoader
if (FugueConfig.remapReflectionTargets.length > 0) {
TransformerDelegate.registerExplicitTransformerByInstance(FugueConfig.remapReflectionTargets, new RemapSunReflectionTransformer());
}
if (!FugueConfig.finalRemovingTargets.isEmpty()) {
TransformerDelegate.registerExplicitTransformerByInstance(FugueConfig.finalRemovingTargets.keySet().toArray(new String[0]), new FinalStripperTransformer(FugueConfig.finalRemovingTargets));
}

}

Expand Down
33 changes: 32 additions & 1 deletion src/main/java/com/cleanroommc/config/FugueConfig.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.cleanroommc.config;

import com.cleanroommc.Reference;
import com.google.common.collect.Maps;
import net.minecraftforge.common.config.Config;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Config(modid = Reference.MOD_ID, name = Reference.MOD_ID)
@Config.RequiresMcRestart
public class FugueConfig {
Expand Down Expand Up @@ -130,7 +136,8 @@ public class FugueConfig {
As a workaround, These targets will be redirected to new API.""")
public static String[] remapLWTargets = new String[] {
"zone.rong.loliasm.common.crashes.ModIdentifier",
"zone.rong.loliasm.LoliReflector"
"zone.rong.loliasm.LoliReflector",
"com.github.terminatornl.tickcentral.asm.Compatibility"
};

@Config.Comment(
Expand All @@ -143,4 +150,28 @@ public class FugueConfig {
"quaternary.botaniatweaks.modules.shared.lib.NiceTryMap",
};

@Config.Comment(
"""
Target field's final modifier will be removed. No checks will be preformed before removal.
All fields with same name will be targeted.
Format: S:"foo.bar.classname"=field1|filed2""")
public static Map<String, String> finalRemovingTargets = Stream.of(new String[][] {
//VintageFix
{"net.minecraftforge.event.terraingen.BiomeEvent$BiomeColor", "originalColor"},
//Snow Real Magic
{"net.minecraft.item.ItemBlock", "field_150939_a"}, //block
//EntityDistance
{"net.minecraft.client.gui.GuiOptions", "field_146441_g"}, //lastScreen
{"net.minecraft.client.gui.GuiOptions", "field_146443_h"}, //settings
{"net.minecraft.entity.EntityTracker", "field_72793_b"}, //entries
{"net.minecraft.entity.EntityTracker", "field_72794_c"}, //trackedEntityHashTable
{"net.minecraft.entity.EntityTracker", "field_72795_a"}, //world
{"net.minecraft.entity.EntityTrackerEntry", "field_73130_b"}, //range
{"net.minecraft.entity.EntityTrackerEntry", "field_73132_a"}, //trackedEntity
{"net.minecraft.entity.EntityTrackerEntry", "field_187262_f"}, //maxRange
{"net.minecraft.entity.EntityTrackerEntry", "field_73131_c"}, //updateFrequency
{"net.minecraft.entity.EntityTrackerEntry", "field_73143_t"}, //sendVelocityUpdates
{"net.minecraft.world.World", "field_72996_f",}, //loadedEntityList
}).collect(Collectors.toMap(data -> data[0], data -> data[1]));

}
31 changes: 31 additions & 0 deletions src/main/java/com/cleanroommc/helper/HookHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.cleanroommc.Fugue;
import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.objectweb.asm.Opcodes;
import top.outlands.foundation.TransformerDelegate;
import top.outlands.foundation.boot.ActualClassLoader;
import top.outlands.foundation.boot.TransformerHolder;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -19,6 +23,8 @@ public static boolean isInterface (int opcode) {
return opcode == Opcodes.INVOKEINTERFACE;
}

public static List<IClassTransformer> transformers;

@SuppressWarnings("deprecation")
public static String byGetResource() {
Class<?> clazz = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass();
Expand Down Expand Up @@ -55,4 +61,29 @@ public static List<IClassTransformer> getTransformers() {
public static InputStream open(URL instance) throws IOException {
throw new IOException("Connection blocked by Fugue!");
}

public static Field getField(Class<?> clazz, String name) throws NoSuchFieldException {
if (!clazz.equals(LaunchClassLoader.class)) return clazz.getField(name);
if (name.equals("transformers") || name.equals("renameTransformer")) {
HookHelper.transformers = TransformerDelegate.getTransformers();
return HookHelper.class.getDeclaredField(name);
} else {
return ActualClassLoader.class.getDeclaredField(name);
}
}


public static byte[] redirectGetClassByte(LaunchClassLoader instance, String s) throws IOException {
byte[] bytes = null;
boolean failed = false;
try {
bytes = instance.getClassBytes(s);
} catch (IOException e) {
failed = true;
}
if (failed || bytes == null) {
bytes = instance.findResource(s.replace(".", "/")).openStream().readAllBytes();
}
return bytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.cleanroommc.transformer;

import com.cleanroommc.Fugue;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.expr.ExprEditor;
import javassist.expr.MethodCall;
import top.outlands.foundation.IExplicitTransformer;

import java.io.ByteArrayInputStream;

public class ClassSnifferTransformer implements IExplicitTransformer {
@Override
public byte[] transform(byte[] bytes) {
try {
CtClass cc = ClassPool.getDefault().makeClass(new ByteArrayInputStream(bytes));
cc.instrument(new ExprEditor(){
@Override
public void edit(MethodCall call) throws CannotCompileException {
if (call.getClassName().equals("org.apache.logging.log4j.Logger") && call.getMethodName().equals("warn")) {
call.replace("{}");
}
}
});
bytes = cc.toBytecode();
} catch (Throwable t) {
Fugue.LOGGER.error(t);
}
return bytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.cleanroommc.transformer.universal;

import com.cleanroommc.Fugue;
import javassist.ClassPool;
import javassist.CtClass;
import top.outlands.foundation.IExplicitTransformer;

import java.io.ByteArrayInputStream;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Map;

public class FinalStripperTransformer implements IExplicitTransformer {
private final Map<String, String> targets;
public FinalStripperTransformer(Map<String, String> targets) {
this.targets = targets;
}
@Override
public byte[] transform(byte[] bytes) {
try {
CtClass cc = ClassPool.getDefault().makeClass(new ByteArrayInputStream(bytes));
String[] fields = targets.get(cc.getName()).split("\\|");
Arrays.stream(cc.getFields()).filter(ctField ->
Arrays.stream(fields).anyMatch(f ->
ctField.getName().equals(f)
)
).forEach(ctField2 ->
{
ctField2.setModifiers(ctField2.getModifiers() & ~Modifier.FINAL);
Fugue.LOGGER.debug("Stripping final modifier of {} from {}", ctField2.getName(), ctField2.getDeclaringClass().getName());
});
bytes = cc.toBytecode();
} catch (Throwable t) {
Fugue.LOGGER.error(t);
}
return bytes;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.transformer.universal;

import com.cleanroommc.Fugue;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
Expand Down Expand Up @@ -28,10 +29,11 @@ public byte[] transform(byte[] bytes) {
methodNode.instructions.insert(abstractInsnNode, new MethodInsnNode(methodInsnNode.getOpcode(), "top/outlands/foundation/boot/ActualClassLoader", methodInsnNode.name, methodInsnNode.desc));
methodNode.instructions.remove(abstractInsnNode);
}
}
} else if (abstractInsnNode instanceof LdcInsnNode ldcInsnNode) {
if (ldcInsnNode.cst instanceof Type && ((Type) ldcInsnNode.cst).getDescriptor().equals("Lnet/minecraft/launchwrapper/LaunchClassLoader;")) {
ldcInsnNode.cst = Type.getType(ActualClassLoader.class);
} else if (methodInsnNode.getOpcode() == Opcodes.INVOKEVIRTUAL &&
methodInsnNode.owner.equals("java/lang/Class") && (methodInsnNode.name.equals("getDeclaredField") || methodInsnNode.name.equals("getField"))) {

methodNode.instructions.insert(abstractInsnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "com/cleanroommc/helper/HookHelper", "getField", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;"));
methodNode.instructions.remove(abstractInsnNode);
}
}
}));
Expand Down
14 changes: 0 additions & 14 deletions src/main/resources/META-INF/accesstransformer.cfg

This file was deleted.

0 comments on commit f277a12

Please sign in to comment.