From b7a7ba9e9a92d78e1b1f15a7cb3ee49a6361db49 Mon Sep 17 00:00:00 2001 From: Wagyourtail Date: Thu, 6 Jun 2024 21:55:59 -0500 Subject: [PATCH] fix agent gradle inject --- expect-platform-test/build.gradle | 22 +++++++++++++++++-- .../unimined/expect/ExpectPlatformAgent.java | 7 ++++-- .../expect/ExpectPlatformExtension.kt | 11 ++++++---- .../unimined/expect/TransformPlatform.java | 8 +++++-- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/expect-platform-test/build.gradle b/expect-platform-test/build.gradle index c481720..c1372b8 100644 --- a/expect-platform-test/build.gradle +++ b/expect-platform-test/build.gradle @@ -9,7 +9,9 @@ buildscript { } } dependencies { - classpath "xyz.wagyourtail.unimined.expect-platform:expect-platform:1.0.2" + classpath "xyz.wagyourtail.unimined.expect-platform:expect-platform:1.0.3" + classpath "org.ow2.asm:asm:9.7" + classpath "org.ow2.asm:asm-commons:9.7" classpath "org.ow2.asm:asm-tree:9.7" } } @@ -87,7 +89,23 @@ tasks.register('runAgentA', JavaExec) { mainClass = 'xyz.wagyourtail.ept.Main' group = 'ept' - expectPlatform.insertAgent(delegate as JavaExecSpec, "a") + expectPlatform.insertAgent(delegate, "a") +} + +tasks.register('runAgentB', JavaExec) { + classpath = sourceSets.b.runtimeClasspath + sourceSets.main.runtimeClasspath + mainClass = 'xyz.wagyourtail.ept.Main' + group = 'ept' + + expectPlatform.insertAgent(delegate, "b") +} + +tasks.register('runAgentC', JavaExec) { + classpath = sourceSets.c.runtimeClasspath + sourceSets.main.runtimeClasspath + mainClass = 'xyz.wagyourtail.ept.Main' + group = 'ept' + + expectPlatform.insertAgent(delegate, "c") } tasks.register('jarA', ExpectPlatformJar) { diff --git a/src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java b/src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java index 14c7663..2ebd7c8 100644 --- a/src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java +++ b/src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java @@ -16,10 +16,13 @@ public class ExpectPlatformAgent { public static void premain(String args, Instrumentation inst) { + System.out.println("[ExpectPlatformAgent] Starting agent"); + System.out.println("[ExpectPlatformAgent] Platform: " + platform); + System.out.println("[ExpectPlatformAgent] Remap: " + remap); if (platform == null) { throw new IllegalStateException("-D" + EXPECT_PLATFORM + " not set"); } - inst.addTransformer(new ExpectPlatformTransformer()); + inst.addTransformer(new ExpectPlatformTransformer(), inst.isRetransformClassesSupported()); } public static void agentmain(String args, Instrumentation inst) { @@ -35,7 +38,7 @@ public byte[] transform(ClassLoader loader, String className, Class classBein ClassNode classNode = new ClassNode(); reader.accept(classNode, 0); - transformPlatform.transform(classNode); + classNode = transformPlatform.transform(classNode); ClassWriter writer = new ClassWriter(reader, 0); classNode.accept(writer); diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt index 468f944..a7ec88b 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt @@ -3,9 +3,12 @@ package xyz.wagyourtail.unimined.expect import groovy.lang.Closure import groovy.lang.DelegatesTo import org.gradle.api.Project +import org.gradle.api.Task import org.gradle.api.artifacts.Configuration import org.gradle.api.attributes.Attribute import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.JavaExec +import org.gradle.api.tasks.TaskProvider import org.gradle.process.JavaExecSpec import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformParams import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformTransform @@ -64,10 +67,10 @@ abstract class ExpectPlatformExtension(val project: Project) { } } -// @JvmOverloads -// fun insertAgent(spec: JavaExecSpec, platformName: String, remap: Map = emptyMap()) { -// spec.jvmArgs("-javaagent:${agentJar.absolutePath}", "-Dexpect.platform=${platformName}", "-Dexpect.remap=${TransformPlatform.mapToString(remap)}") -// } + @JvmOverloads + fun insertAgent(spec: JavaExecSpec, platformName: String, remap: Map = emptyMap()) { + spec.jvmArgs("-javaagent:${agentJar.absolutePath}", "-Dexpect.platform=${platformName}", "-Dexpect.remap=${TransformPlatform.mapToString(remap)}") + } val agentJar by lazy { val config = project.configurations.detachedConfiguration(project.dependencies.create(agentDep)) diff --git a/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java b/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java index e654706..2dd2601 100644 --- a/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java +++ b/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java @@ -175,7 +175,9 @@ private void getCurrentTarget(ClassNode classNode) { AbstractInsnNode insnNode = iterator.next(); if (insnNode.getOpcode() == Opcodes.INVOKESTATIC) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; - if (methodInsnNode.owner.equals("xyz/wagyourtail/unimined/expect/Target") && methodInsnNode.name.equals("getCurrentTarget")) { + if (methodInsnNode.owner.equals("xyz/wagyourtail/unimined/expect/Target") && + methodInsnNode.name.equals("getCurrentTarget") + ) { iterator.set(new LdcInsnNode(platformName)); } } @@ -196,7 +198,9 @@ public static String mapToString(Map map) { for (Map.Entry entry : map.entrySet()) { sb.append(entry.getKey()).append(";=;").append(entry.getValue()).append(";|;"); } - sb.setLength(sb.length() - 3); + if (sb.length() > 0) { + sb.setLength(sb.length() - 3); + } return sb.toString(); }