Skip to content

Commit

Permalink
Fix named attributes in legacy failing when reading back data from Mo…
Browse files Browse the repository at this point in the history
…dule Metadata
  • Loading branch information
shartte committed Jan 8, 2025
1 parent ddc4c21 commit 45b17f2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public abstract class ObfuscationExtension {
private final Configuration installerToolsRuntime;
private final FileCollection extraMixinMappings;

private final MinecraftMappings namedMappings;
private final MinecraftMappings srgMappings;

@Inject
public ObfuscationExtension(Project project,
Configuration autoRenamingToolRuntime,
Expand All @@ -41,6 +44,9 @@ public ObfuscationExtension(Project project,
this.autoRenamingToolRuntime = autoRenamingToolRuntime;
this.installerToolsRuntime = installerToolsRuntime;
this.extraMixinMappings = extraMixinMappings;

this.namedMappings = project.getObjects().named(MinecraftMappings.class, MinecraftMappings.NAMED);
this.srgMappings = project.getObjects().named(MinecraftMappings.class, MinecraftMappings.SRG);
}

private <T> Provider<T> assertConfigured(Provider<T> provider) {
Expand Down Expand Up @@ -123,7 +129,7 @@ public TaskProvider<RemapJar> reobfuscate(TaskProvider<? extends AbstractArchive
var config = configurations.getByName(configurationName);
// Mark the original configuration as NAMED to be able to disambiguate between it and the reobfuscated jar,
// this is used for example by the JarJar configuration.
config.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.NAMED);
config.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, namedMappings);

// Now create a reobf configuration
var reobfConfig = configurations.maybeCreate("reobf" + StringUtils.capitalize(configurationName));
Expand All @@ -132,7 +138,7 @@ public TaskProvider<RemapJar> reobfuscate(TaskProvider<? extends AbstractArchive
for (var attribute : config.getAttributes().keySet()) {
copyAttribute(project, attribute, config, reobfConfig);
}
reobfConfig.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.SRG);
reobfConfig.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, srgMappings);
project.getArtifacts().add(reobfConfig.getName(), reobf);

// Publish the reobf configuration instead of the original one to Maven
Expand All @@ -157,7 +163,7 @@ public Configuration createRemappingConfiguration(Configuration parent) {
var remappingConfig = project.getConfigurations().create("mod" + StringUtils.capitalize(parent.getName()), spec -> {
spec.setDescription("Configuration for dependencies of " + parent.getName() + " that needs to be remapped");
spec.attributes(attributeContainer -> {
attributeContainer.attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.SRG);
attributeContainer.attribute(MinecraftMappings.ATTRIBUTE, srgMappings);
});
spec.setCanBeConsumed(false);
spec.setCanBeResolved(false);
Expand All @@ -171,20 +177,20 @@ public Configuration createRemappingConfiguration(Configuration parent) {
if (dep instanceof ExternalModuleDependency externalModuleDependency) {
project.getDependencies().constraints(constraints -> {
constraints.add(parent.getName(), externalModuleDependency.getGroup() + ":" + externalModuleDependency.getName() + ":" + externalModuleDependency.getVersion(), c -> {
c.attributes(a -> a.attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.SRG));
c.attributes(a -> a.attribute(MinecraftMappings.ATTRIBUTE, srgMappings));
});
});
externalModuleDependency.setTransitive(false);
} else if (dep instanceof FileCollectionDependency fileCollectionDependency) {
project.getDependencies().constraints(constraints -> {
constraints.add(parent.getName(), fileCollectionDependency.getFiles(), c -> {
c.attributes(a -> a.attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.SRG));
c.attributes(a -> a.attribute(MinecraftMappings.ATTRIBUTE, srgMappings));
});
});
} else if (dep instanceof ProjectDependency projectDependency) {
project.getDependencies().constraints(constraints -> {
constraints.add(parent.getName(), projectDependency.getDependencyProject(), c -> {
c.attributes(a -> a.attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.SRG));
c.attributes(a -> a.attribute(MinecraftMappings.ATTRIBUTE, srgMappings));
});
});
projectDependency.setTransitive(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.neoforged.moddevgradle.legacyforge.internal;

import java.net.URI;
import java.util.stream.Stream;
import net.neoforged.minecraftdependencies.MinecraftDependenciesPlugin;
import net.neoforged.moddevgradle.internal.ArtifactNamingStrategy;
import net.neoforged.moddevgradle.internal.Branding;
Expand All @@ -23,6 +21,7 @@
import org.gradle.api.Project;
import org.gradle.api.artifacts.ProjectDependency;
import org.gradle.api.artifacts.type.ArtifactTypeDefinition;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.SourceSet;
Expand All @@ -32,6 +31,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.net.URI;
import java.util.stream.Stream;

@ApiStatus.Internal
public class LegacyForgeModDevPlugin implements Plugin<Project> {
private static final Logger LOG = LoggerFactory.getLogger(LegacyForgeModDevPlugin.class);
Expand All @@ -43,6 +46,15 @@ public class LegacyForgeModDevPlugin implements Plugin<Project> {
public static final String CONFIGURATION_TOOL_ART = "autoRenamingToolRuntime";
public static final String CONFIGURATION_TOOL_INSTALLERTOOLS = "installerToolsRuntime";

private final MinecraftMappings namedMappings;
private final MinecraftMappings srgMappings;

@Inject
public LegacyForgeModDevPlugin(ObjectFactory objectFactory) {
namedMappings = objectFactory.named(MinecraftMappings.class, MinecraftMappings.NAMED);
srgMappings = objectFactory.named(MinecraftMappings.class, MinecraftMappings.SRG);
}

@Override
public void apply(Project project) {
project.getPlugins().apply(JavaLibraryPlugin.class);
Expand Down Expand Up @@ -203,15 +215,15 @@ public void enable(Project project, LegacyForgeModdingSettings settings, LegacyF
parameters.getMinecraftDependencies().from(remapDeps);
});
params.getFrom()
.attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.NAMED)
.attribute(MinecraftMappings.ATTRIBUTE, namedMappings)
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE);
params.getTo()
.attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.SRG)
.attribute(MinecraftMappings.ATTRIBUTE, srgMappings)
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE);
});
}

private static void configureDependencyRemapping(Project project, ObfuscationExtension obf) {
private void configureDependencyRemapping(Project project, ObfuscationExtension obf) {
// JarJar cross-project dependencies are packaged into the final jar and should be remapped
// We must however do this without affecting external dependencies since those are usually already in the
// right namespace.
Expand All @@ -222,17 +234,21 @@ private static void configureDependencyRemapping(Project project, ObfuscationExt
dependencies.forEach(dep -> {
if (dep instanceof ProjectDependency projectDependency) {
projectDependency.attributes(a -> {
a.attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.SRG);
a.attribute(MinecraftMappings.ATTRIBUTE, srgMappings);
});
}
});
});
});

project.getDependencies().attributesSchema(schema -> schema.attribute(MinecraftMappings.ATTRIBUTE).getDisambiguationRules().add(MappingsDisambiguationRule.class));
project.getDependencies().attributesSchema(schema -> {
schema.attribute(MinecraftMappings.ATTRIBUTE).getDisambiguationRules().add(MappingsDisambiguationRule.class, spec -> {
spec.params(namedMappings);
});
});
project.getDependencies().getArtifactTypes().named("jar", a -> {
// By default all produced artifacts are NAMED
a.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, MinecraftMappings.NAMED);
a.getAttributes().attribute(MinecraftMappings.ATTRIBUTE, namedMappings);
});

obf.createRemappingConfiguration(project.getConfigurations().getByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
import org.gradle.api.attributes.AttributeDisambiguationRule;
import org.gradle.api.attributes.MultipleCandidatesDetails;

import javax.inject.Inject;

/**
* This disambiguation rule will prefer NAMED over SRG when both are present.
*/
class MappingsDisambiguationRule implements AttributeDisambiguationRule<MinecraftMappings> {
private final MinecraftMappings named;

@Inject
MappingsDisambiguationRule(MinecraftMappings named) {
this.named = named;
}

@Override
public void execute(MultipleCandidatesDetails<MinecraftMappings> details) {
var consumerValue = details.getConsumerValue();
if (consumerValue == null) {
if (details.getCandidateValues().contains(MinecraftMappings.NAMED)) {
details.closestMatch(MinecraftMappings.NAMED);
if (details.getCandidateValues().contains(named)) {
details.closestMatch(named);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public enum MinecraftMappings implements Named {
NAMED,
SRG;
public interface MinecraftMappings extends Named {
String NAMED = "named";
String SRG = "srg";

public static final Attribute<MinecraftMappings> ATTRIBUTE = Attribute.of("net.neoforged.moddevgradle.legacy.minecraft_mappings", MinecraftMappings.class);

@Override
public String getName() {
return name().toLowerCase(Locale.ROOT);
}
Attribute<MinecraftMappings> ATTRIBUTE = Attribute.of("net.neoforged.moddevgradle.legacy.minecraft_mappings", MinecraftMappings.class);
}

0 comments on commit 45b17f2

Please sign in to comment.