Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Java Agents published by the platform #220

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.gradle.api.attributes.Usage;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.Directory;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class ModDevRunWorkflow {
private final ModuleDependency gameLibrariesDependency;
private final Configuration additionalClasspath;
private final Configuration userDevConfigOnly;
private final Configuration javaAgent;

/**
* @param gameLibrariesDependency A module dependency that represents the library dependencies of the game.
Expand All @@ -74,6 +76,7 @@ private ModDevRunWorkflow(Project project,
@Nullable ModuleDependency modulePathDependency,
@Nullable ModuleDependency runTypesConfigDependency,
@Nullable ModuleDependency testFixturesDependency,
@Nullable ModuleDependency javaAgentDependency,
ModuleDependency gameLibrariesDependency,
DomainObjectCollection<RunModel> runs,
VersionCapabilitiesInternal versionCapabilities) {
Expand All @@ -97,6 +100,16 @@ private ModDevRunWorkflow(Project project,
}
});

javaAgent = configurations.create("neoForgeJavaAgent", spec -> {
spec.setDescription("Any jar in this configuration will be added as a javagent to the Minecraft and JUnit command lines in this project.");
spec.setCanBeResolved(true);
spec.setCanBeConsumed(false);
spec.setTransitive(false); // Java agents can't load transitive dependencies
if (javaAgentDependency != null) {
spec.getDependencies().add(javaAgentDependency);
}
});

additionalClasspath = configurations.create("additionalRuntimeClasspath", spec -> {
spec.setDescription("Contains dependencies of every run, that should not be considered boot classpath modules.");
spec.setCanBeResolved(true);
Expand Down Expand Up @@ -149,6 +162,7 @@ public static ModDevRunWorkflow create(Project project,
dependencies.modulePathDependency(),
dependencies.runTypesConfigDependency(),
dependencies.testFixturesDependency(),
dependencies.javaAgentDependency(),
dependencies.gameLibrariesDependency(),
runs,
versionCapabilites);
Expand Down Expand Up @@ -198,7 +212,8 @@ public void configureTesting(Provider<ModModel> testedMod, Provider<Set<ModModel
legacyClassPath.getDependencies().add(gameLibrariesDependency);
addClientResources(project, legacyClassPath, artifactsWorkflow.createArtifacts());
},
artifactsWorkflow.downloadAssets().flatMap(DownloadAssets::getAssetPropertiesFile));
artifactsWorkflow.downloadAssets().flatMap(DownloadAssets::getAssetPropertiesFile),
javaAgent);
}
}

Expand Down Expand Up @@ -404,7 +419,8 @@ static void setupTestTask(Project project,
Provider<Directory> argFileDir,
Consumer<Configuration> configureModulePath,
Consumer<Configuration> configureLegacyClasspath,
Provider<RegularFile> assetPropertiesFile) {
Provider<RegularFile> assetPropertiesFile,
FileCollection javaAgent) {
var gameDirectory = new File(project.getProjectDir(), JUNIT_GAME_DIR);

var ideIntegration = IdeIntegration.of(project, branding);
Expand Down Expand Up @@ -455,6 +471,7 @@ static void setupTestTask(Project project,
task.getProgramArgsFile().set(programArgsFile);
task.getLog4jConfigFile().set(log4j2ConfigFile);
task.getRunTypeTemplatesSource().from(runTemplatesSourceFile);
task.getJavaAgent().from(javaAgent);
task.getModules().from(neoForgeModDevModules);
task.getLegacyClasspathFile().set(writeLcpTask.get().getLegacyClasspathFile());
task.getAssetProperties().set(assetPropertiesFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public record ModdingDependencies(
@Nullable ModuleDependency neoFormDependency,
@Nullable String neoFormDependencyNotation,
ModuleDependency gameLibrariesDependency,
@Nullable ModuleDependency javaAgentDependency,
@Nullable ModuleDependency modulePathDependency,
@Nullable ModuleDependency runTypesConfigDependency,
@Nullable ModuleDependency testFixturesDependency) {
Expand All @@ -34,12 +35,19 @@ public static ModdingDependencies create(ModuleDependency neoForge,
.capabilities(caps -> caps.requireCapability("net.neoforged:neoforge-moddev-test-fixtures"));
}

ModuleDependency javaAgentDependency = null;
if (versionCapabilities.javaAgent()) {
javaAgentDependency = neoForge.copy()
.capabilities(caps -> caps.requireCapability("net.neoforged:neoforge-javaagent"));
}

return new ModdingDependencies(
neoForge,
neoForgeNotation,
neoForm,
neoFormNotation,
librariesDependency,
javaAgentDependency,
modulePathDependency,
runTypesDataDependency,
testFixturesDependency);
Expand All @@ -57,6 +65,7 @@ public static ModdingDependencies createVanillaOnly(ModuleDependency neoForm, St
librariesDependency,
null,
null,
null,
null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public static void setupTestTask(Project project,
argFileDir,
configureModulePath,
configureAdditionalClasspath,
assetPropertiesFile);
assetPropertiesFile,
project.files());
}

public static void runTaskOnProjectSync(Project project, Object task) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ abstract class PrepareRunOrTest extends DefaultTask {
@InputFiles
abstract ConfigurableFileCollection getModules();

@Classpath
@InputFiles
abstract ConfigurableFileCollection getJavaAgent();

@Input
public abstract MapProperty<String, String> getSystemProperties();

Expand Down Expand Up @@ -224,6 +228,10 @@ private UserDevConfig getSimulatedUserDevConfigForVanilla() {
private void writeJvmArguments(UserDevRunType runConfig, Map<String, String> additionalProperties) throws IOException {
var lines = new ArrayList<String>();

for (var file : getJavaAgent()) {
lines.add("-javaagent:" + file.getAbsolutePath());
}

lines.addAll(getInterpolatedJvmArgs(runConfig));

var userJvmArgs = getJvmArguments().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
* @param javaVersion Which Java version Vanilla uses to compile and run.
* @param splitDataRuns Whether Vanilla has separate main classes for generating client and server data.
* @param testFixtures If the NeoForge version for this Minecraft version supports test fixtures.
* @param modLocatorRework Has the rework of FML mod locators that allow the client resources to use any jar filename. If false, the client resource jar must be called "client-extra".
* @param javaAgent If true, the NeoForge dependency supplies a java agent capability.
*/
public record VersionCapabilitiesInternal(String minecraftVersion, int javaVersion, boolean splitDataRuns,
boolean testFixtures, boolean modLocatorRework) implements VersionCapabilities, Serializable {
boolean testFixtures, boolean modLocatorRework,
boolean javaAgent) implements VersionCapabilities, Serializable {

private static final Logger LOG = LoggerFactory.getLogger(VersionCapabilitiesInternal.class);

Expand All @@ -26,6 +29,8 @@ public record VersionCapabilitiesInternal(String minecraftVersion, int javaVersi
// Strips NeoForm timestamp suffixes OR dynamic version markers
private static final Pattern NEOFORM_PATTERN = Pattern.compile("^(.*)-(?:\\+|\\d{8}\\.\\d{6})$");

// TODO: Wrong but for testing it's 1.20.1 for now
private static final int MC_1_21_4_INDEX = getReferenceVersionIndex("1.21.1");
private static final int MC_24W45A_INDEX = getReferenceVersionIndex("24w45a");
private static final int MC_1_20_5_INDEX = getReferenceVersionIndex("1.20.5");
private static final int MC_24W14A_INDEX = getReferenceVersionIndex("24w14a");
Expand Down Expand Up @@ -56,8 +61,9 @@ public static VersionCapabilitiesInternal ofVersionIndex(int versionIndex, Strin
var splitData = hasSplitDataEntrypoints(versionIndex);
var testFixtures = hasTestFixtures(versionIndex);
var modLocatorRework = hasModLocatorRework(versionIndex);
var javaAgent = hasJavaAgent(versionIndex);

return new VersionCapabilitiesInternal(minecraftVersion, javaVersion, splitData, testFixtures, modLocatorRework);
return new VersionCapabilitiesInternal(minecraftVersion, javaVersion, splitData, testFixtures, modLocatorRework, javaAgent);
}

static int getJavaVersion(int versionIndex) {
Expand All @@ -84,6 +90,10 @@ static boolean hasModLocatorRework(int versionIndex) {
return versionIndex <= MC_1_20_5_INDEX;
}

static boolean hasJavaAgent(int versionIndex) {
return versionIndex <= MC_1_21_4_INDEX;
}

static int indexOfNeoForgeVersion(String version) {
// NeoForge omits the "1." at the start of the Minecraft version and just adds an incrementing last digit
var matcher = NEOFORGE_PATTERN.matcher(version);
Expand Down Expand Up @@ -168,6 +178,7 @@ public VersionCapabilitiesInternal withMinecraftVersion(String minecraftVersion)
javaVersion,
splitDataRuns,
testFixtures,
modLocatorRework);
modLocatorRework,
javaAgent);
}
}
Loading