Skip to content

Commit

Permalink
Fixed compatibility with recent 1.7.10 Optifine versions
Browse files Browse the repository at this point in the history
- Added the getLocalVarIndex method to look for the correct index (necessary since Optifine changed it along with names in E7)
- Replaced the static 14 index with the dynamic "var14" (or "frustrum") one inside the if checks
- Upped the version number to 1.4.1

Basically ported what i did with Betweenlands so it works with any version of Optifine or even without it. Again, huge thanks to makamys for helping me understand how variable adresses work.
  • Loading branch information
HRudyPlayZ committed Nov 19, 2021
1 parent dcfd9f2 commit 67830d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {

apply plugin: 'forge'

version = "1.4.0"
version = "1.4.1"
group= "com.pau101.fairylights"
archivesBaseName = "fairylights"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
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.FieldNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;
import org.objectweb.asm.tree.*;

public class FairyLightsClassTransformer implements IClassTransformer {
private Logger logger = Logger.getLogger("FairyLightsClassTransformer");
Expand Down Expand Up @@ -46,9 +40,25 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
return basicClass;
}

private int getLocalVarIndex(List<LocalVariableNode> list, String name, String name2) {
// Returns the index of a specific local variable name from the method. This is required for compatibility with any Optifine version.
// It uses a list which you can create in the specific method using method.localVariables, and two names since for some dumb reasons,
// The game can access varX fine with the latest Optifine but not the internal name, and the opposite otherwise.

for (int i = 0; i < list.size(); i += 1) {
LocalVariableNode object = list.get(i);
if (object.name.equals(name) || object.name.equals(name2)) return object.index;
}
return -1;

}

private boolean transformFrustumStore(boolean obf, List<MethodNode> methods, String entityRendererOwner, String frustumDesc) {
boolean replacedASTORE8 = false;
for (MethodNode method : methods) {
List localVarList = method.localVariables;
int var14index = getLocalVarIndex(localVarList, "var14", "frustrum");

if ("(FJ)V".equals(method.desc)) {
InsnList instructions = method.instructions;
for (int i = 0; i < instructions.size(); i++) {
Expand All @@ -57,15 +67,16 @@ private boolean transformFrustumStore(boolean obf, List<MethodNode> methods, Str
// replace all things trying to get the frustum from the
// stack frame to getting the frustum from the class
// field
if (instruction.getOpcode() == Opcodes.ALOAD && ((VarInsnNode) instruction).var == 14) {
if (instruction.getOpcode() == Opcodes.ALOAD && ((VarInsnNode) instruction).var == var14index) {
((VarInsnNode) instruction).var = 0;
instructions.insert(instruction, new FieldInsnNode(Opcodes.GETFIELD, entityRendererOwner, fieldNameAddition, frustumDesc));
}

} else {
// instead of storing the newly created frustum into
// slot 14 of the method stack frame, put in the added
// class field for external access
if (instruction.getOpcode() == Opcodes.ASTORE && ((VarInsnNode) instruction).var == 14) {
if (instruction.getOpcode() == Opcodes.ASTORE && ((VarInsnNode) instruction).var == var14index) {
InsnList newInsn = new InsnList();
instructions.insertBefore(instructions.get(i - 3), new VarInsnNode(Opcodes.ALOAD, 0));
instructions.set(instruction, new FieldInsnNode(Opcodes.PUTFIELD, entityRendererOwner, fieldNameAddition, frustumDesc));
Expand Down

0 comments on commit 67830d0

Please sign in to comment.