Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Fixes #43 - Support for upcoming Optifine Versions
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Jul 19, 2019
1 parent fea6962 commit 65e13f7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
build/
out/
classes/
libs/

# idea

Expand Down
49 changes: 3 additions & 46 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ dependencies {
modCompile "net.fabricmc:fabric-loader:${project.loader_version}"

modCompile 'net.fabricmc.fabric-api:fabric-api:0.3.0+build.191'

modCompile 'com.github.Chocohead:Fabric-ASM:c4ad22d'
include 'com.github.Chocohead:Fabric-ASM:c4ad22d'
modCompile 'com.github.Chocohead:Fabric-ASM:9af0ebe992'
include 'com.github.Chocohead:Fabric-ASM:9af0ebe992'

//Used to handle the zip processing
compile 'org.zeroturnaround:zt-zip:1.13'
Expand All @@ -54,9 +54,6 @@ dependencies {
include 'net.fabricmc:stitch:0.2.1.61'
}

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

task unzip(type: Copy) {
def zipFile = file("${project.buildDir}/libs/${archivesBaseName}-${version}.jar")
def outputDir = file("${buildDir}/unpacked/dist")
Expand All @@ -69,46 +66,6 @@ task unzip(type: Copy) {
into outputDir
}

task modevJar (type: Jar, dependsOn: unzip) {
classifier = 'moddev'

def tempFile = file("${buildDir}/temp/fabric.mod.json")
tempFile.mkdirs()
tempFile.delete()

def json = new JsonSlurper().parseText(file("src/main/resources/fabric.mod.json").text)
json.jars = configurations.include.resolve().stream().map{f -> new FileHolder("META-INF/jars/" + f.getName())}.toArray()
tempFile.write JsonOutput.toJson(json)
from tempFile

from (sourceSets.main.output) {
exclude "fabric.mod.json"
exclude '*.jar'
}
}

afterEvaluate { project ->
project.configurations.include.resolve().stream().forEach{f ->
def file = file("${buildDir}/unpacked/dist/META-INF/jars/${f.getName()}")
projects.logger.lifecycle(file.getName() + ":" + f.getName())
modevJar.from (file) {
into ("META-INF/jars")
}
}
}


class FileHolder {
String file

FileHolder(String file) {
this.file = file
}
}

modevJar.dependsOn remapJar
build.dependsOn modevJar

processResources {
inputs.property "version", project.version

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ minecraft_version=1.14.3
yarn_mappings=1.14.3+build.9
loader_version=0.4.8+build.155

mod_version = 0.4.0
mod_version = 0.4.2-test1
maven_group = me.modmuss50
archives_base_name = optifabric
11 changes: 11 additions & 0 deletions src/main/java/me/modmuss50/optifabric/mod/OptifineInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.chocohead.mm.api.ClassTinkerers;
import me.modmuss50.optifabric.patcher.ASMUtils;
import me.modmuss50.optifabric.patcher.ChunkRendererFix;
import me.modmuss50.optifabric.patcher.ClassCache;
import net.fabricmc.loader.api.FabricLoader;
import org.objectweb.asm.Opcodes;
Expand All @@ -21,11 +22,15 @@ public class OptifineInjector {

ClassCache classCache;

String chunkRenderer;

public OptifineInjector(ClassCache classCache) {
this.classCache = classCache;
}

public void setup() throws IOException {
chunkRenderer = FabricLoader.getInstance().getMappingResolver().mapClassName("intermediary", "net.minecraft.class_851").replaceAll("\\.", "/");

classCache.getClasses().forEach(s -> ClassTinkerers.addTransformation(s.replaceAll("/", ".").substring(0, s.length() - 6), transformer));
}

Expand All @@ -34,6 +39,12 @@ public void setup() throws IOException {

//I cannot imagine this being very good at all
ClassNode source = getSourceClassNode(target);

//Patch the class to fix
if(target.name.equals(chunkRenderer)){
ChunkRendererFix.fix(source);
}

target.methods = source.methods;
target.fields = source.fields;
target.interfaces = source.interfaces;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package me.modmuss50.optifabric.patcher;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.MappingResolver;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.lib.tree.*;

public class ChunkRendererFix {

//This removes the small change that optifine made to ChunkRenderer that is only required by forge
public static void fix(ClassNode classNode){
MappingResolver mappingResolver = FabricLoader.getInstance().getMappingResolver();


for(MethodNode methodNode : classNode.methods){
for (int i = 0; i < methodNode.instructions.size(); i++) {
AbstractInsnNode insnNode = methodNode.instructions.get(i);
if(insnNode instanceof MethodInsnNode){
MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
if(methodInsnNode.getOpcode() == Opcodes.INVOKEVIRTUAL){
if(methodInsnNode.name.equals("renderBlock")){

/*
class_2680 - net/minecraft/block/BlockState
class_2338 - net/minecraft/util/math/BlockPos
class_1920 - net/minecraft/world/ExtendedBlockView
class_287 - net/minecraft/client/render/BufferBuilder
*/

String desc = "(Lclass_2680;Lclass_2338;Lclass_1920;Lclass_287;Ljava/util/Random;)Z";
String name = mappingResolver.mapMethodName("intermediary", "net.minecraft.class_776", "method_3355", desc.replaceAll("Lclass_", "Lnet/minecraft/class_"));

//Horrible but works
desc = desc.replace("class_2680", mappingResolver.mapClassName("intermediary", "net.minecraft.class_2680").replaceAll("\\.", "/"));
desc = desc.replace("class_2338", mappingResolver.mapClassName("intermediary", "net.minecraft.class_2338").replaceAll("\\.", "/"));
desc = desc.replace("class_1920", mappingResolver.mapClassName("intermediary", "net.minecraft.class_1920").replaceAll("\\.", "/"));
desc = desc.replace("class_287", mappingResolver.mapClassName("intermediary", "net.minecraft.class_287").replaceAll("\\.", "/"));


System.out.println(String.format("Replacement `renderBlock` call: %s.%s", name, desc));

//tesselateBlock.(Lnet.minecraft.block.BlockState;Lnet.minecraft.util.math.BlockPos;Lclass_1920;Lnet.minecraft.client.render.BufferBuilder;Ljava/util/Random;)Z
//tesselateBlock.(Lnet/minecraft/block/BlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/world/ExtendedBlockView;Lnet/minecraft/client/render/BufferBuilder;Ljava/util/Random;)Z"


//Replaces the method call with the vanilla one, this calls down to the same method just without the forge model data
methodInsnNode.name = name;
methodInsnNode.desc = desc;


//Remove the model data call
methodNode.instructions.remove(methodNode.instructions.get(i -1));
}
}
}
}
}
}

}

0 comments on commit 65e13f7

Please sign in to comment.