Skip to content

Commit

Permalink
Fix[JNA]: use agent to replace class at runtime
Browse files Browse the repository at this point in the history
Fixes duplicate module package on recent Forge versions.
  • Loading branch information
khanhduytran0 committed Jan 25, 2025
1 parent 4a789ab commit 6eb7ecd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
14 changes: 12 additions & 2 deletions JavaApp/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Prerequisite variables
SOURCEDIR := src
OUTPUTDIR := build

SOURCES := $(shell find $(SOURCEDIR) -type f -name '*.java')
CLASSES := $(SOURCES:$(SOURCEDIR)/%.java=$(OUTPUTDIR)/%.class)

TARGETS := $(shell find $(SOURCEDIR) -mindepth 1 -maxdepth 1 -type d)
TARGETS := $(TARGETS:$(SOURCEDIR)/%=$(OUTPUTDIR)/%.jar)

# Add all jars to classpath and allow compiling with private API
JCFLAGS := -cp "$(subst $() $(),:,$(wildcard $(SOURCEDIR)/*/):$(wildcard libs/*/*.jar))" -XDignore.symbol.file

all: $(OUTPUTDIR)/launcher.jar $(OUTPUTDIR)/lwjgl.jar
all: $(TARGETS)

# Rules for building java class
$(CLASSES): $(OUTPUTDIR)/%.class: $(SOURCEDIR)/%.java
Expand All @@ -22,7 +26,7 @@ $(CLASSES): $(OUTPUTDIR)/%.class: $(SOURCEDIR)/%.java
$(OUTPUTDIR)/launcher.jar: $(CLASSES)
@set -e
@echo "Creating $@"
$(BOOTJDK)/jar -cf $@ -C $(basename $@) .
@$(BOOTJDK)/jar -cf $@ -C $(basename $@) .

$(OUTPUTDIR)/lwjgl.jar: $(CLASSES)
@set -e
Expand All @@ -32,6 +36,12 @@ $(OUTPUTDIR)/lwjgl.jar: $(CLASSES)
@cp -R $(OUTPUTDIR)/lwjgl/* $(OUTPUTDIR)/lwjgl_lib/
@$(BOOTJDK)/jar -cf $@ -C $(OUTPUTDIR)/lwjgl_lib .

$(OUTPUTDIR)/patchjna_agent.jar: $(CLASSES)
@set -e
@mv $(basename $@)/com/sun/jna/Platform.class $(basename $@)/com/sun/jna/Platform.class.patch
@echo "Creating $@"
@$(BOOTJDK)/jar -cfm $@ $(SOURCEDIR)/patchjna_agent.txt -C $(basename $@) .

clean:
rm -rf $(OUTPUTDIR)

Expand Down
2 changes: 2 additions & 0 deletions JavaApp/src/patchjna_agent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Agent-Class: net.kdt.patchjna.PatchJNAAgent
Premain-Class: net.kdt.patchjna.PatchJNAAgent
32 changes: 32 additions & 0 deletions JavaApp/src/patchjna_agent/net/kdt/patchjna/PatchJNAAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.kdt.patchjna;

import java.io.*;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.security.ProtectionDomain;

public class PatchJNAAgent implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
byte[] transformeredByteCode = classfileBuffer;
if (className.equals("com/sun/jna/Platform")) {
System.out.println("PatchJNAAgent: Replacing class");
try {
InputStream inputStream = PatchJNAAgent.class.getClassLoader().getResourceAsStream("com/sun/jna/Platform.class.patch");
transformeredByteCode = new byte[inputStream.available()];
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readFully(transformeredByteCode);
} catch (Exception e) {
e.printStackTrace();
}
}
return transformeredByteCode;
}

public static void premain(String args, Instrumentation instrumentation) {
System.out.println("PatchJNAAgent: premain called");
instrumentation.addTransformer(new PatchJNAAgent());
}
}
1 change: 1 addition & 0 deletions Natives/JavaLauncher.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ int launchJVM(NSString *username, id launchTarget, int width, int height, int mi
}

NSString *librariesPath = [NSString stringWithFormat:@"%@/libs", NSBundle.mainBundle.bundlePath];
margv[++margc] = [NSString stringWithFormat:@"-javaagent:%@/patchjna_agent.jar=", librariesPath].UTF8String;
if(getPrefBool(@"general.cosmetica")) {
margv[++margc] = [NSString stringWithFormat:@"-javaagent:%@/arc_dns_injector.jar=23.95.137.176", librariesPath].UTF8String;
}
Expand Down

0 comments on commit 6eb7ecd

Please sign in to comment.