Skip to content

Commit 794fa20

Browse files
authored
Redo how the mod configurations in legacy MDG are remapped, fixing source downloads (#249)
1 parent fb5dd9f commit 794fa20

File tree

4 files changed

+24
-77
lines changed

4 files changed

+24
-77
lines changed

src/legacy/java/net/neoforged/moddevgradle/legacyforge/dsl/ObfuscationExtension.java

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.util.List;
44
import java.util.Objects;
55
import javax.inject.Inject;
6-
import net.neoforged.moddevgradle.legacyforge.internal.LegacyForgeModDevPlugin;
76
import net.neoforged.moddevgradle.legacyforge.internal.MinecraftMappings;
8-
import net.neoforged.moddevgradle.legacyforge.internal.SrgMappingsRule;
97
import net.neoforged.moddevgradle.legacyforge.tasks.RemapJar;
108
import net.neoforged.moddevgradle.legacyforge.tasks.RemapOperation;
119
import org.apache.commons.lang3.StringUtils;
@@ -16,7 +14,6 @@
1614
import org.gradle.api.artifacts.ExternalModuleDependency;
1715
import org.gradle.api.artifacts.FileCollectionDependency;
1816
import org.gradle.api.artifacts.ProjectDependency;
19-
import org.gradle.api.artifacts.type.ArtifactTypeDefinition;
2017
import org.gradle.api.attributes.Attribute;
2118
import org.gradle.api.component.AdhocComponentWithVariants;
2219
import org.gradle.api.component.ConfigurationVariantDetails;
@@ -36,7 +33,6 @@ public abstract class ObfuscationExtension {
3633
private final FileCollection extraMixinMappings;
3734

3835
private final MinecraftMappings namedMappings;
39-
private final MinecraftMappings srgMappings;
4036

4137
@Inject
4238
public ObfuscationExtension(Project project,
@@ -49,7 +45,6 @@ public ObfuscationExtension(Project project,
4945
this.extraMixinMappings = extraMixinMappings;
5046

5147
this.namedMappings = project.getObjects().named(MinecraftMappings.class, MinecraftMappings.NAMED);
52-
this.srgMappings = project.getObjects().named(MinecraftMappings.class, MinecraftMappings.SRG);
5348
}
5449

5550
private <T> Provider<T> assertConfigured(Provider<T> provider) {
@@ -139,9 +134,12 @@ public TaskProvider<RemapJar> reobfuscate(TaskProvider<? extends AbstractArchive
139134
reobfConfig.setDescription("The artifacts remapped to intermediate (SRG) Minecraft names for use in a production environment");
140135
reobfConfig.getArtifacts().clear(); // If this is called multiple times...
141136
for (var attribute : config.getAttributes().keySet()) {
142-
copyAttribute(project, attribute, config, reobfConfig);
137+
// Don't copy the mappings attribute because we don't want to leak it in the published metadata
138+
// and there is no way to unset it later.
139+
if (attribute != MinecraftMappings.ATTRIBUTE) {
140+
copyAttribute(project, attribute, config, reobfConfig);
141+
}
143142
}
144-
reobfConfig.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, srgMappings);
145143
project.getArtifacts().add(reobfConfig.getName(), reobf);
146144

147145
// Publish the reobf configuration instead of the original one to Maven
@@ -166,7 +164,7 @@ public Configuration createRemappingConfiguration(Configuration parent) {
166164
var remappingConfig = project.getConfigurations().create("mod" + StringUtils.capitalize(parent.getName()), spec -> {
167165
spec.setDescription("Configuration for dependencies of " + parent.getName() + " that needs to be remapped");
168166
spec.setCanBeConsumed(false);
169-
spec.setCanBeResolved(true);
167+
spec.setCanBeResolved(false);
170168
spec.setTransitive(false);
171169

172170
// Unfortunately, if we simply try to make the parent extend this config, transformations will not run because the parent doesn't request remapped deps
@@ -175,19 +173,12 @@ public Configuration createRemappingConfiguration(Configuration parent) {
175173
// Additionally, we force dependencies to be non-transitive since we cannot apply the attribute hack to transitive dependencies.
176174
spec.withDependencies(dependencies -> dependencies.forEach(dep -> {
177175
if (dep instanceof ExternalModuleDependency externalModuleDependency) {
176+
project.getDependencies().constraints(constraints -> {
177+
constraints.add(parent.getName(), externalModuleDependency.getGroup() + ":" + externalModuleDependency.getName() + ":" + externalModuleDependency.getVersion(), c -> {
178+
c.attributes(a -> a.attribute(MinecraftMappings.ATTRIBUTE, namedMappings));
179+
});
180+
});
178181
externalModuleDependency.setTransitive(false);
179-
180-
// artifact dependencies need to be forced to be from our custom artifact type to be able
181-
// to inherit the mapping attribute (and be remapped). Module metadata doesn't apply here.
182-
for (var artifact : externalModuleDependency.getArtifacts()) {
183-
artifact.setType(LegacyForgeModDevPlugin.ARTIFACT_TYPE_SRG_JAR);
184-
}
185-
186-
// This rule ensures that this external module will be enriched with the attribute MAPPINGS=SRG
187-
project.getDependencies().getComponents().withModule(
188-
dep.getGroup() + ":" + dep.getName(), SrgMappingsRule.class, cfg -> {
189-
cfg.params(srgMappings);
190-
});
191182
} else if (dep instanceof FileCollectionDependency fileCollectionDependency) {
192183
project.getDependencies().constraints(constraints -> {
193184
constraints.add(parent.getName(), fileCollectionDependency.getFiles(), c -> {
@@ -204,16 +195,7 @@ public Configuration createRemappingConfiguration(Configuration parent) {
204195
}
205196
}));
206197
});
207-
208-
var remappedDep = project.getDependencyFactory().create(
209-
remappingConfig.getIncoming().artifactView(view -> {
210-
view.attributes(a -> a.attribute(MinecraftMappings.ATTRIBUTE, namedMappings));
211-
// Forcing resolution to JAR here allows our SRG_JAR artifact transform to work
212-
view.attributes(a -> a.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE));
213-
}).getFiles());
214-
remappedDep.because("Remapped mods from " + remappingConfig.getName());
215-
216-
parent.getDependencies().add(remappedDep);
198+
parent.extendsFrom(remappingConfig);
217199

218200
return remappingConfig;
219201
}

src/legacy/java/net/neoforged/moddevgradle/legacyforge/internal/LegacyForgeModDevPlugin.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public class LegacyForgeModDevPlugin implements Plugin<Project> {
4444

4545
public static final String CONFIGURATION_TOOL_ART = "autoRenamingToolRuntime";
4646
public static final String CONFIGURATION_TOOL_INSTALLERTOOLS = "installerToolsRuntime";
47-
public static final String ARTIFACT_TYPE_SRG_JAR = "srgJar";
4847

4948
private final MinecraftMappings namedMappings;
5049
private final MinecraftMappings srgMappings;
@@ -221,26 +220,14 @@ public void enable(Project project, LegacyForgeModdingSettings settings, LegacyF
221220
.attribute(MinecraftMappings.ATTRIBUTE, namedMappings)
222221
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE);
223222
});
224-
// This second copy of the transform is used for remapping artifact dependencies (i.e. on classifiers)
225-
// that circumvent the variant system.
226-
project.getDependencies().registerTransform(RemappingTransform.class, params -> {
227-
params.parameters(parameters -> {
228-
obf.configureSrgToNamedOperation(parameters.getRemapOperation());
229-
parameters.getMinecraftDependencies().from(remapDeps);
230-
});
231-
params.getFrom()
232-
.attribute(MinecraftMappings.ATTRIBUTE, srgMappings)
233-
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ARTIFACT_TYPE_SRG_JAR);
234-
params.getTo()
235-
.attribute(MinecraftMappings.ATTRIBUTE, namedMappings)
236-
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE);
237-
});
238223
}
239224

240225
private void configureDependencyRemapping(Project project, ObfuscationExtension obf) {
241226
// JarJar cross-project dependencies are packaged into the final jar and should be remapped
242227
// We must however do this without affecting external dependencies since those are usually already in the
243228
// right namespace.
229+
// For cross-project dependencies, both the named (with the named attribute) and obfuscated (with no attribute)
230+
// variants are available. Requesting the srg attribute seemingly excludes the named variant from the selection.
244231
var sourceSets = ExtensionUtils.getSourceSets(project);
245232
sourceSets.all(sourceSet -> {
246233
var configurationName = sourceSet.getTaskName(null, "jarJar");
@@ -257,15 +244,18 @@ private void configureDependencyRemapping(Project project, ObfuscationExtension
257244

258245
project.getDependencies().attributesSchema(schema -> {
259246
var attr = schema.attribute(MinecraftMappings.ATTRIBUTE);
260-
attr.getDisambiguationRules().add(MappingsDisambiguationRule.class, actionConfiguration -> {
261-
actionConfiguration.params(namedMappings);
247+
// Add a disambiguation rule that will prefer named variants.
248+
// This is needed for cross-project dependencies: in that setting both the named (with the named attribute)
249+
// and obfuscated (with no attribute) variants are available, and we want to choose named by default.
250+
attr.getDisambiguationRules().add(MappingsDisambiguationRule.class, config -> {
251+
config.params(namedMappings);
262252
});
263253
});
264-
265-
// custom artifact type used to force remapping of artifact dependencies (which circumvent variant selection)
266-
project.getDependencies().getArtifactTypes().create(ARTIFACT_TYPE_SRG_JAR, artifactType -> {
267-
artifactType.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, srgMappings);
254+
// Give every single jar the srg mappings attribute so it can be force-remapped by requesting named
255+
project.getDependencies().getArtifactTypes().named(ArtifactTypeDefinition.JAR_TYPE, type -> {
256+
type.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, srgMappings);
268257
});
258+
269259
obf.createRemappingConfiguration(project.getConfigurations().getByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME));
270260
obf.createRemappingConfiguration(project.getConfigurations().getByName(JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME));
271261
obf.createRemappingConfiguration(project.getConfigurations().getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME));

src/legacy/java/net/neoforged/moddevgradle/legacyforge/internal/MinecraftMappings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public interface MinecraftMappings extends Named {
99
String NAMED = "named";
1010
String SRG = "srg";
1111

12-
Attribute<MinecraftMappings> ATTRIBUTE = Attribute.of("net.neoforged.moddevgradle.legacy.minecraft_mappings", MinecraftMappings.class);
12+
Attribute<MinecraftMappings> ATTRIBUTE = Attribute.of("net.neoforged.moddevgradle.legacy.minecraft_mappings.v2", MinecraftMappings.class);
1313
}

src/legacy/java/net/neoforged/moddevgradle/legacyforge/internal/SrgMappingsRule.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)