Fix[JNA]: use agent to replace class at runtime
Fixes duplicate module package on recent Forge versions.
This commit is contained in:
1 parent
4a789ab98c
commit
6eb7ecdde3
5 files changed
+47
-2
No files matched your search
+12
-2
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
Agent-Class: net.kdt.patchjna.PatchJNAAgent
|
||||
Premain-Class: net.kdt.patchjna.PatchJNAAgent
|
||||
File renamed without changes.
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user