Skip to content

Commit

Permalink
WIP CodeChickenCore and NEI Compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Aug 18, 2023
1 parent 6296e9e commit 42e2ec9
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 93 deletions.
Binary file removed lib/CodeChickenCore-0.6.16.jar
Binary file not shown.
Binary file added lib/CodeChickenCore-0.8.1.3.jar
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions mod-compat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ dependencies {
// add the intermediary jar of the mod in rootfolder/lib with a dummy fabric.mod.json file in it
// if the file is named like "filename-fileversion.jar" then import it this way:
// modCompileOnly(group: "local", name: "filename", version: "fileversion")
modCompileOnly(group: "local", name: "CodeChickenCore", version: "0.6.16")
modCompileOnly(group: "local", name: "NotEnoughItems", version: "1.4.3.6")
modCompileOnly(group: "local", name: "CodeChickenCore", version: "0.8.1.3")
modCompileOnly(group: "local", name: "NotEnoughItems", version: "1.4.7.1")
}

base {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ public void registerVisitors(VisitorInfos visitorInfos) {
@Override
public void afterRemap() {
Mixins.addConfiguration("fabricated-forge.mods.mixins.json");
Utils.TRANSFORMER_EXCLUSIONS.add("fr.catcore.fabricatedforge.compat.BetterClassWriter");
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package fr.catcore.fabricatedforge.compat.mixin.codechickencore;

import codechicken.core.asm.ObfuscationManager;
import codechicken.core.asm.ObfuscationMappings;
import fr.catcore.fabricatedforge.Constants;
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;

@Mixin(ObfuscationManager.ClassMapping.class)
@Mixin(ObfuscationMappings.ClassMapping.class)
public class ClassMappingMixin {
@Shadow(remap = false) public String classname;
@Shadow(remap = false) public String s_class;

@Inject(method = "<init>", remap = false, at = @At("RETURN"))
private void remapClassName(String classname, CallbackInfo ci) {
if (!this.classname.contains(".")) {
this.classname = Constants.getRemappedClassName(this.classname);
}
this.s_class = Constants.getRemappedClassName(this.s_class).replace(".", "/");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.catcore.fabricatedforge.compat.mixin.codechickencore;

import codechicken.core.asm.ClassOverrider;
import codechicken.core.asm.ObfuscationManager;
import codechicken.core.asm.ObfuscationMappings;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -12,8 +12,8 @@
@Mixin(ClassOverrider.class)
public class ClassOverriderMixin {
@Inject(method = "overrideBytes", remap = false, at = @At("HEAD"), cancellable = true)
private static void disableClassOverwriting(String name, byte[] bytes, ObfuscationManager.ClassMapping classMapping, File location, CallbackInfoReturnable<byte[]> cir) {
if (classMapping.classname.equals(name)) {
private static void disableClassOverwriting(String name, byte[] bytes, ObfuscationMappings.ClassMapping classMapping, File location, CallbackInfoReturnable<byte[]> cir) {
if (classMapping.isClass(name)) {
System.out.println("[ClassOverrider] Canceled class overwrite of " + name);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.catcore.fabricatedforge.compat.mixin.codechickencore;

import codechicken.core.asm.ObfuscationMappings;
import fr.catcore.fabricatedforge.Constants;
import net.fabricmc.tinyremapper.extension.mixin.common.data.Pair;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ObfuscationMappings.DescriptorMapping.class)
public class DescriptorMappingMixin {

@Shadow(remap = false) public String s_owner;

@Shadow(remap = false) public String s_name;

@Shadow(remap = false) public String s_desc;

@Inject(method = "<init>(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", remap = false, at = @At("RETURN"))
private void remap(String declaringclass, String methodname, String descriptor, CallbackInfo ci) {
this.remap();
}

@Inject(method = "<init>(Ljava/lang/String;Lcodechicken/core/asm/ObfuscationMappings$DescriptorMapping;)V", remap = false, at = @At("RETURN"))
private void remap(String owner, ObfuscationMappings.DescriptorMapping descmap, CallbackInfo ci) {
this.remap();
}

@Unique
private void remap() {
if (!this.s_owner.contains("/")) {
this.s_owner = Constants.getRemappedClassName(this.s_owner).replace(".", "/");
}
if (this.s_desc.startsWith("(")) {
Pair<String, String> pair = Constants.getRemappedMethodName(this.s_owner, this.s_name, this.s_desc);
this.s_name = pair.first();
this.s_desc = Constants.remapMethodDescriptor(pair.second());
} else {
Pair<String, String> pair = Constants.getRemappedFieldName(this.s_owner, this.s_name, this.s_desc);
this.s_name = pair.first();
this.s_desc = Constants.remapIndividualType(pair.second());
}
}

// /**
// * @author CatCore
// * @reason fix match because of the way methods are added to classes
// */
// @Overwrite(remap = false)
// public boolean matches(MethodNode node) {
// String className = this.getClassNameForMethod();
//
// System.out.println(this.s_owner + " -> " + className);
//
// return this.s_name.equals(node.name) && this.s_desc.equals(node.desc);
// }
//
// /**
// * @author CatCore
// * @reason fix match because of the way methods are added to classes
// */
// @Overwrite(remap = false)
// public boolean matches(MethodInsnNode node) {
// String className = this.getClassNameForMethod();
//
// System.out.println(this.s_name + " -> " + className + " : " + node.owner);
//
// return className.equals(node.owner) && this.s_name.equals(node.name) && this.s_desc.equals(node.desc);
// }
//
// @Unique
// private String getClassNameForMethod() {
// if (this.s_owner.equals("net/minecraft/class_197") && (
// this.s_name.equals("canBeReplacedByLeaves")
// || this.s_name.equals("isAirBlock")
// )) {
// return "fr/catcore/fabricatedforge/mixininterface/IBlock";
// }
//
// return this.s_name;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FeatureHackTransformerMixin {
remap = false,
at = @At(value = "INVOKE",
remap = false,
target = "Lcodechicken/core/asm/ObfuscationManager$MethodMapping;<init>(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"))
target = "Lcodechicken/core/asm/ObfuscationMappings$DescriptorMapping;<init>(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", ordinal = 0))
private String fixMethodArg(String arg) {
return "(Lnet/minecraft/class_1069;)V";
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ public void render(int par1, int par2, float par3) {
PlayerInventory var15 = this.field_1229.playerEntity.inventory;
ItemStack var16 = this.touchDragStack == null ? var15.getCursorStack() : this.touchDragStack;
if (var16 != null) {
int var8 = this.touchDragStack == null ? 8 : 0;
byte var18 = 8;
int var9 = this.touchDragStack == null ? 8 : 16;
if (this.touchDragStack != null && this.touchIsRightClickDrag) {
var16 = var16.copy();
var16.count = MathHelper.ceil((float)var16.count / 2.0F);
}

this.method_4264(var16, par1 - var4 - var8, par2 - var5 - var8);
this.method_4264(var16, par1 - var4 - var18, par2 - var5 - var9);
}

if (this.touchDropReturningStack != null) {
Expand All @@ -163,7 +164,10 @@ public void render(int par1, int par2, float par3) {
DiffuseLighting.enableNormally();
}

@ModifyConstant(method = "method_4264", constant = {@Constant(floatValue = 200.0F, ordinal = 0), @Constant(floatValue = 200.0F, ordinal = 1)})
@ModifyConstant(method = "method_4264", constant = {
@Constant(floatValue = 200.0F, ordinal = 0),
@Constant(floatValue = 200.0F, ordinal = 1)
})
private float changeConstants(float constant) {
return 500.0F;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import codechicken.core.asm.ASMHelper;
import codechicken.core.asm.ClassHeirachyManager;
import codechicken.core.asm.InstructionComparator;
import codechicken.core.asm.ObfuscationManager;
import codechicken.core.asm.ObfuscationMappings;
import codechicken.nei.asm.NEITransformer;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
Expand All @@ -20,11 +20,13 @@ public class NEITransformerMixin {
*/
@Overwrite(remap = false)
public byte[] transformer001(String name, byte[] bytes) {
ObfuscationManager.ClassMapping classmap = new ObfuscationManager.ClassMapping("auy");
if (ClassHeirachyManager.classExtends(name, classmap.classname, bytes)) {
ObfuscationMappings.ClassMapping classmap = new ObfuscationMappings.ClassMapping("avf");

if (ClassHeirachyManager.classExtends(name, classmap.javaClass(), bytes)) {
ClassNode node = ASMHelper.createClassNode(bytes);
ObfuscationManager.MethodMapping methodmap = new ObfuscationManager.MethodMapping("aue", "c", "()V");
ObfuscationManager.MethodMapping supermap = new ObfuscationManager.MethodMapping(node.superName, methodmap);

ObfuscationMappings.DescriptorMapping methodmap = new ObfuscationMappings.DescriptorMapping("aul", "c", "()V");
ObfuscationMappings.DescriptorMapping supermap = new ObfuscationMappings.DescriptorMapping(node.superName, methodmap);
InsnList supercall = new InsnList();
supercall.add(new VarInsnNode(25, 0));
supercall.add(supermap.toInsn(183));
Expand All @@ -34,7 +36,7 @@ public byte[] transformer001(String name, byte[] bytes) {
InsnList importantNodeList = InstructionComparator.getImportantList(methodnode.instructions);
if (!InstructionComparator.insnListMatches(importantNodeList, supercall, 0)) {
methodnode.instructions.insertBefore(methodnode.instructions.getFirst(), supercall);
System.out.println("Inserted super call into " + name + "." + supermap.name);
System.out.println("Inserted super call into " + name + "." + supermap.s_name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ accessible field net/minecraft/client/gui/screen/ingame/HandledScreen y I
accessible method net/minecraft/client/gui/screen/ingame/HandledScreen getSlotAt (II)Lnet/minecraft/inventory/slot/Slot;
extendable method net/minecraft/client/gui/screen/ingame/HandledScreen getSlotAt (II)Lnet/minecraft/inventory/slot/Slot;
accessible method net/minecraft/client/gui/screen/ingame/HandledScreen onMouseClick (Lnet/minecraft/inventory/slot/Slot;III)V
accessible method net/minecraft/client/gui/screen/ingame/HandledScreen drawSlot (Lnet/minecraft/inventory/slot/Slot;)V
extendable method net/minecraft/client/gui/screen/ingame/HandledScreen drawSlot (Lnet/minecraft/inventory/slot/Slot;)V

accessible field net/minecraft/client/class_469 connection Lnet/minecraft/network/Connection;
accessible field net/minecraft/entity/EntityType CLASS_ID_MAP Ljava/util/Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
"codechickencore.ClassHeirachyManagerMixin",
"codechickencore.ClassMappingMixin",
"codechickencore.ClassOverriderMixin",
"codechickencore.DescriptorMappingMixin",
"codechickencore.FeatureHackTransformerMixin",
"codechickencore.FieldMappingMixin",
"codechickencore.MethodMappingMixin",
"nei.FurnaceRecipeHandlerMixin",
"nei.NEITransformerMixin",
"nei.TMIUninstallerMixin"
],
"client": [
"nei.HandledScreenMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/fr/catcore/fabricatedforge/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.fabricmc.mapping.tree.FieldDef;
import net.fabricmc.mapping.tree.MethodDef;
import net.fabricmc.tinyremapper.extension.mixin.common.data.Pair;
import org.objectweb.asm.Type;

import java.io.File;
import java.util.Objects;
Expand Down Expand Up @@ -105,4 +106,49 @@ public static Pair<String, String> getRemappedFieldName(String owner, String fie

return Pair.of(fieldName, fieldDesc);
}

public static String remapMethodDescriptor(String desc) {
Type methodDescType = Type.getType(desc);

Type[] argTypes = methodDescType.getArgumentTypes();
Type returnType = methodDescType.getReturnType();

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("(");

for (Type type : argTypes) {
stringBuilder.append(remapIndividualType(type.getDescriptor()));
}

stringBuilder.append(")");

stringBuilder.append(remapIndividualType(returnType.getDescriptor()));

return stringBuilder.toString();
}

public static String remapIndividualType(String desc) {
Type type = Type.getType(desc);

if (type.getSort() == Type.OBJECT || type.getSort() == 12) {
String className = type.getClassName();

String newClassName = getRemappedClassName(className);

if (!Objects.equals(className, newClassName)) {
type = Type.getType("L" + newClassName.replace(".", "/") + ";");
}
} else if (type.getSort() == Type.ARRAY) {
StringBuilder arrayType = new StringBuilder(remapIndividualType(type.getElementType().getDescriptor()));

for (int i = 1; i < type.getDimensions() + 1; i++) {
arrayType.insert(0, "[");
}

type = Type.getType(arrayType.toString());
}

return type.getDescriptor();
}
}

0 comments on commit 42e2ec9

Please sign in to comment.