From 92110d50075d4b6ff7ee12392c44865d4c4aa65a Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Fri, 30 Jun 2023 16:18:07 +0300 Subject: [PATCH 1/3] Add a mod settings container to RunConfigSettings Closes #144. --- .../configuration/ide/RunConfigSettings.java | 40 ++++++++++++++- .../providers/forge/ForgeRunsProvider.java | 21 ++++++-- .../extension/LoomGradleExtensionApiImpl.java | 2 +- .../task/launch/GenerateDLIConfigTask.java | 5 +- .../forge/ForgeRunConfigTest.groovy | 51 +++++++++++++++++++ 5 files changed, 109 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/configuration/ide/RunConfigSettings.java b/src/main/java/net/fabricmc/loom/configuration/ide/RunConfigSettings.java index 527dea775..1536ae46c 100644 --- a/src/main/java/net/fabricmc/loom/configuration/ide/RunConfigSettings.java +++ b/src/main/java/net/fabricmc/loom/configuration/ide/RunConfigSettings.java @@ -34,13 +34,18 @@ import java.util.Objects; import java.util.function.Function; +import javax.inject.Inject; + +import org.gradle.api.Action; import org.gradle.api.Named; +import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.Project; import org.gradle.api.provider.Property; import org.gradle.api.tasks.SourceSet; import org.jetbrains.annotations.ApiStatus; import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.api.ModSettings; import net.fabricmc.loom.configuration.providers.forge.ForgeRunTemplate; import net.fabricmc.loom.configuration.providers.forge.ForgeRunsProvider; import net.fabricmc.loom.configuration.providers.minecraft.MinecraftSourceSets; @@ -49,7 +54,8 @@ import net.fabricmc.loom.util.Platform; import net.fabricmc.loom.util.gradle.SourceSetHelper; -public final class RunConfigSettings implements Named { +@ApiStatus.NonExtendable +public class RunConfigSettings implements Named { /** * Arguments for the JVM, such as system properties. */ @@ -114,9 +120,13 @@ public final class RunConfigSettings implements Named { private final Project project; private final LoomGradleExtension extension; + + // Architectury private final List evaluateLater = new ArrayList<>(); private boolean evaluated = false; + private final NamedDomainObjectContainer mods; + @Inject public RunConfigSettings(Project project, String baseName) { this.baseName = baseName; this.project = project; @@ -127,6 +137,7 @@ public RunConfigSettings(Project project, String baseName) { Objects.requireNonNull(defaultMainClass, "Run config " + baseName + " must specify default main class"); return RunConfig.getMainClass(environment, extension, defaultMainClass); })); + this.mods = project.getObjects().domainObjectContainer(ModSettings.class); setSource(p -> { final String sourceSetName = MinecraftSourceSets.get(p).getSourceSetForEnv(getEnvironment()); @@ -378,7 +389,7 @@ public void forgeTemplate(String templateName) { ForgeRunTemplate template = runsProvider.getTemplates().findByName(templateName); if (template != null) { - template.applyTo(this, runsProvider); + template.applyTo(this, runsProvider.getResolver(this)); } else { project.getLogger().warn("Could not find Forge run template with name '{}'", templateName); } @@ -415,4 +426,29 @@ public boolean isIdeConfigGenerated() { public void setIdeConfigGenerated(boolean ideConfigGenerated) { this.ideConfigGenerated = ideConfigGenerated; } + + /** + * {@return a container of mod settings for this run configuration} + * + *

If non-empty, this container will override the + * {@linkplain net.fabricmc.loom.api.LoomGradleExtensionAPI#getMods global container} + * declared in the {@code loom} extension. + * + *

This method is currently only available on Forge. + */ + @ApiStatus.Experimental + public NamedDomainObjectContainer getMods() { + ModPlatform.assertPlatform(project, ModPlatform.FORGE); + return mods; + } + + /** + * Configures the {@linkplain #getMods mods} of this run configuration. + * + *

This method is currently only available on Forge. + */ + @ApiStatus.Experimental + public void mods(Action> action) { + action.execute(getMods()); + } } diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeRunsProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeRunsProvider.java index b1cb485fb..d08cf12fb 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeRunsProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeRunsProvider.java @@ -1,7 +1,7 @@ /* * This file is part of fabric-loom, licensed under the MIT License (MIT). * - * Copyright (c) 2022 FabricMC + * Copyright (c) 2022-2023 FabricMC * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -39,17 +39,20 @@ import com.google.common.collect.MultimapBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.NamedDomainObjectSet; import org.gradle.api.Project; +import org.jetbrains.annotations.Nullable; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.api.ModSettings; +import net.fabricmc.loom.configuration.ide.RunConfigSettings; import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.DependencyDownloader; import net.fabricmc.loom.util.gradle.SourceSetHelper; import net.fabricmc.loom.util.gradle.SourceSetReference; -public class ForgeRunsProvider implements ConfigValue.Resolver { +public class ForgeRunsProvider { private final Project project; private final LoomGradleExtension extension; private final JsonObject json; @@ -79,8 +82,11 @@ public static ForgeRunsProvider create(Project project) { return new ForgeRunsProvider(project, json); } - @Override - public String resolve(ConfigValue.Variable variable) { + public ConfigValue.Resolver getResolver(@Nullable RunConfigSettings runConfig) { + return variable -> resolve(runConfig, variable); + } + + private String resolve(@Nullable RunConfigSettings runConfig, ConfigValue.Variable variable) { String key = variable.name(); String string = '{' + key + '}'; @@ -128,8 +134,13 @@ public String resolve(ConfigValue.Variable variable) { } else if (key.equals("source_roots")) { // Use a set-valued multimap for deduplicating paths. Multimap modClasses = MultimapBuilder.hashKeys().linkedHashSetValues().build(); + NamedDomainObjectContainer mods = extension.getMods(); + + if (runConfig != null && !runConfig.getMods().isEmpty()) { + mods = runConfig.getMods(); + } - for (ModSettings mod : extension.getMods()) { + for (ModSettings mod : mods) { // Note: In Forge 1.16.5, resources have to come first to find mods.toml for (SourceSetReference modSourceSet : mod.getModSourceSets().get()) { File resourcesDir = modSourceSet.sourceSet().getOutput().getResourcesDir(); diff --git a/src/main/java/net/fabricmc/loom/extension/LoomGradleExtensionApiImpl.java b/src/main/java/net/fabricmc/loom/extension/LoomGradleExtensionApiImpl.java index 805fb3300..13f06d208 100644 --- a/src/main/java/net/fabricmc/loom/extension/LoomGradleExtensionApiImpl.java +++ b/src/main/java/net/fabricmc/loom/extension/LoomGradleExtensionApiImpl.java @@ -138,7 +138,7 @@ protected LoomGradleExtensionApiImpl(Project project, LoomFiles directories) { this.deprecationHelper = new DeprecationHelper.ProjectBased(project); this.runConfigs = project.container(RunConfigSettings.class, - baseName -> new RunConfigSettings(project, baseName)); + baseName -> project.getObjects().newInstance(RunConfigSettings.class, project, baseName)); this.decompilers = project.getObjects().domainObjectContainer(DecompilerOptions.class); this.mods = project.getObjects().domainObjectContainer(ModSettings.class); this.remapConfigurations = project.getObjects().namedDomainObjectList(RemapConfigurationSettings.class); diff --git a/src/main/java/net/fabricmc/loom/task/launch/GenerateDLIConfigTask.java b/src/main/java/net/fabricmc/loom/task/launch/GenerateDLIConfigTask.java index b568a0787..3bc064d49 100644 --- a/src/main/java/net/fabricmc/loom/task/launch/GenerateDLIConfigTask.java +++ b/src/main/java/net/fabricmc/loom/task/launch/GenerateDLIConfigTask.java @@ -142,14 +142,15 @@ public void run() throws IOException { } ForgeRunsProvider forgeRunsProvider = getExtension().getForgeRunsProvider(); + ConfigValue.Resolver configResolver = forgeRunsProvider.getResolver(null); for (ForgeRunTemplate template : forgeRunsProvider.getTemplates()) { for (ConfigValue argument : template.args()) { - launchConfig.argument(template.name(), argument.resolve(forgeRunsProvider)); + launchConfig.argument(template.name(), argument.resolve(configResolver)); } for (Map.Entry property : template.props().entrySet()) { - launchConfig.property(template.name(), property.getKey(), property.getValue().resolve(forgeRunsProvider)); + launchConfig.property(template.name(), property.getKey(), property.getValue().resolve(configResolver)); } } } diff --git a/src/test/groovy/net/fabricmc/loom/test/integration/forge/ForgeRunConfigTest.groovy b/src/test/groovy/net/fabricmc/loom/test/integration/forge/ForgeRunConfigTest.groovy index 17ed87fbf..a083a4950 100644 --- a/src/test/groovy/net/fabricmc/loom/test/integration/forge/ForgeRunConfigTest.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/integration/forge/ForgeRunConfigTest.groovy @@ -69,4 +69,55 @@ class ForgeRunConfigTest extends Specification implements GradleProjectTestTrait '1.16.5' | "36.2.4" | 'net.minecraftforge.userdev.LaunchTesting' '1.14.4' | "28.2.23" | 'net.minecraftforge.userdev.LaunchTesting' } + + def "verify mod classes"() { + setup: + def gradle = gradleProject(project: "forge/simple", version: DEFAULT_GRADLE) + gradle.buildGradle.text = gradle.buildGradle.text.replace('@MCVERSION@', '1.19.4') + .replace('@FORGEVERSION@', "45.0.43") + .replace('@MAPPINGS@', 'loom.officialMojangMappings()') + gradle.buildGradle << ''' + sourceSets { + testMod {} + } + + loom { + runs { + testMod { + client() + mods { + main { sourceSet 'main' } + testMod { sourceSet 'testMod' } + } + } + } + } + + tasks.register('verifyRunConfigs') { + doLast { + def client = loom.runs.client + client.evaluateNow() + def clientClasses = client.environmentVariables.get('MOD_CLASSES') + if (!clientClasses.contains('main%%')) { + throw new AssertionError("MOD_CLASSES=clientClasses missing main classes") + } else if (clientClasses.contains('testMod%%')) { + throw new AssertionError("MOD_CLASSES=$clientClasses containing test mod classes") + } + + def testMod = loom.runs.testMod + testMod.evaluateNow() + def testModClasses = testMod.environmentVariables.get('MOD_CLASSES') + if (!testModClasses.contains('main%%') || !testModClasses.contains('testMod%%')) { + throw new AssertionError("MOD_CLASSES=$testModClasses missing required entries") + } + } + } + '''.stripIndent() + + when: + def result = gradle.run(task: "verifyRunConfigs") + + then: + result.task(":verifyRunConfigs").outcome == SUCCESS + } } From 89a4927504b05462309df7b0081f13ada80da431 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Fri, 30 Jun 2023 16:26:35 +0300 Subject: [PATCH 2/3] Fix code format --- .../loom/test/integration/forge/ForgeRunConfigTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/groovy/net/fabricmc/loom/test/integration/forge/ForgeRunConfigTest.groovy b/src/test/groovy/net/fabricmc/loom/test/integration/forge/ForgeRunConfigTest.groovy index a083a4950..102480140 100644 --- a/src/test/groovy/net/fabricmc/loom/test/integration/forge/ForgeRunConfigTest.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/integration/forge/ForgeRunConfigTest.groovy @@ -100,7 +100,7 @@ class ForgeRunConfigTest extends Specification implements GradleProjectTestTrait def clientClasses = client.environmentVariables.get('MOD_CLASSES') if (!clientClasses.contains('main%%')) { throw new AssertionError("MOD_CLASSES=clientClasses missing main classes") - } else if (clientClasses.contains('testMod%%')) { + } else if (clientClasses.contains('testMod%%')) { throw new AssertionError("MOD_CLASSES=$clientClasses containing test mod classes") } From df299aae04740c24564c97e9dd3d9b0019d5201c Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Sat, 12 Aug 2023 01:29:22 +0300 Subject: [PATCH 3/3] Match upstream --- .../net/fabricmc/loom/configuration/ide/RunConfigSettings.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/net/fabricmc/loom/configuration/ide/RunConfigSettings.java b/src/main/java/net/fabricmc/loom/configuration/ide/RunConfigSettings.java index 4eadbd758..0e7f97a87 100644 --- a/src/main/java/net/fabricmc/loom/configuration/ide/RunConfigSettings.java +++ b/src/main/java/net/fabricmc/loom/configuration/ide/RunConfigSettings.java @@ -54,7 +54,6 @@ import net.fabricmc.loom.util.Platform; import net.fabricmc.loom.util.gradle.SourceSetHelper; -@ApiStatus.NonExtendable public class RunConfigSettings implements Named { /** * Arguments for the JVM, such as system properties.