forked from FabricMC/fabric-loom
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev/1.6' into feature/forge
- Loading branch information
Showing
3 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/dev/architectury/loom/neoforge/LaunchHandlerPatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package dev.architectury.loom.neoforge; | ||
|
||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
/* | ||
* Patches the Minecraft.class check in FML's CommonUserdevLaunchHandler | ||
* to refer to a class that is found in any mapping set (Main.class). | ||
* | ||
* See https://github.com/architectury/architectury-loom/issues/212 | ||
*/ | ||
public final class LaunchHandlerPatcher extends ClassVisitor { | ||
private static final String INPUT_CLASS_FILE = "net/minecraft/client/Minecraft.class"; | ||
private static final String OUTPUT_CLASS_FILE = "net/minecraft/client/main/Main.class"; | ||
|
||
public LaunchHandlerPatcher(ClassVisitor next) { | ||
super(Opcodes.ASM9, next); | ||
} | ||
|
||
@Override | ||
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { | ||
return new MethodPatcher(super.visitMethod(access, name, descriptor, signature, exceptions)); | ||
} | ||
|
||
private static final class MethodPatcher extends MethodVisitor { | ||
MethodPatcher(MethodVisitor next) { | ||
super(Opcodes.ASM9, next); | ||
} | ||
|
||
@Override | ||
public void visitLdcInsn(Object value) { | ||
if (INPUT_CLASS_FILE.equals(value)) { | ||
value = OUTPUT_CLASS_FILE; | ||
} | ||
|
||
super.visitLdcInsn(value); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/dev/architectury/loom/util/ClassVisitorUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dev.architectury.loom.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.function.UnaryOperator; | ||
|
||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.ClassWriter; | ||
|
||
import net.fabricmc.loom.util.ExceptionUtil; | ||
|
||
public final class ClassVisitorUtil { | ||
public static void rewriteClassFile(Path path, UnaryOperator<ClassVisitor> visitorFactory) throws IOException { | ||
try { | ||
final byte[] inputBytes = Files.readAllBytes(path); | ||
final var reader = new ClassReader(inputBytes); | ||
final var writer = new ClassWriter(0); | ||
reader.accept(visitorFactory.apply(writer), 0); | ||
final byte[] outputBytes = writer.toByteArray(); | ||
|
||
if (!Arrays.equals(inputBytes, outputBytes)) { | ||
Files.write(path, outputBytes); | ||
} | ||
} catch (IOException e) { | ||
throw ExceptionUtil.createDescriptiveWrapper(IOException::new, "Failed to patch " + path, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters