|
| 1 | +package net.minecraftforge.gradle.internal; |
| 2 | + |
| 3 | +import net.minecraftforge.util.os.OS; |
| 4 | +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; |
| 5 | +import org.gradle.api.Action; |
| 6 | +import org.gradle.api.attributes.Attribute; |
| 7 | +import org.gradle.api.attributes.AttributeDisambiguationRule; |
| 8 | +import org.gradle.api.attributes.MultipleCandidatesDetails; |
| 9 | +import org.gradle.api.provider.SetProperty; |
| 10 | +import org.gradle.api.provider.ValueSource; |
| 11 | +import org.gradle.api.provider.ValueSourceParameters; |
| 12 | +import org.gradle.api.provider.ValueSourceSpec; |
| 13 | +import org.jspecify.annotations.NullUnmarked; |
| 14 | +import org.jspecify.annotations.Nullable; |
| 15 | + |
| 16 | +import javax.inject.Inject; |
| 17 | +import java.util.Comparator; |
| 18 | +import java.util.EnumSet; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +final class ForgeAttributes { |
| 22 | + static final class OperatingSystem { |
| 23 | + static final Attribute<String> ATTRIBUTE = Attribute.of("net.minecraftforge.native.operatingSystem", String.class); |
| 24 | + |
| 25 | + static abstract class DisambiguationRule implements AttributeDisambiguationRule<String> { |
| 26 | + private final OS current; |
| 27 | + |
| 28 | + @Inject |
| 29 | + public DisambiguationRule(OS current) { |
| 30 | + this.current = current; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void execute(MultipleCandidatesDetails<String> details) { |
| 35 | + var options = details.getCandidateValues().stream().map(OS::byKey).toList(); |
| 36 | + |
| 37 | + if (options.contains(current)) { |
| 38 | + details.closestMatch(current.key()); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + for (var os : options) { |
| 43 | + var fallback = execute(current, os); |
| 44 | + if (fallback != null) { |
| 45 | + details.closestMatch(fallback); |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @NullUnmarked |
| 52 | + private String execute(OS current, OS os) { |
| 53 | + return current == os |
| 54 | + ? os.key() |
| 55 | + : os != null ? execute(current, os.fallback()) : null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + abstract static class CurrentValue implements ValueSource<OS, CurrentValue.Parameters> { |
| 60 | + interface Parameters extends ValueSourceParameters { |
| 61 | + Action<? super ValueSourceSpec<Parameters>> DEFAULT = spec -> spec.getParameters().getAllowedOperatingSystems().set(Set.of(OS.WINDOWS, OS.MACOS, OS.LINUX)); |
| 62 | + |
| 63 | + SetProperty<OS> getAllowedOperatingSystems(); |
| 64 | + } |
| 65 | + |
| 66 | + @Inject |
| 67 | + public CurrentValue() { } |
| 68 | + |
| 69 | + @Override |
| 70 | + public @Nullable OS obtain() { |
| 71 | + var allowed = getParameters().getAllowedOperatingSystems().getOrElse(EnumSet.noneOf(OS.class)).toArray(new OS[0]); |
| 72 | + return allowed.length > 0 |
| 73 | + ? OS.current(allowed) |
| 74 | + : OS.current(); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + static final class MappingsChannel { |
| 80 | + static final Attribute<String> ATTRIBUTE = Attribute.of("net.minecraftforge.mappings.channel", String.class); |
| 81 | + |
| 82 | + static abstract class DisambiguationRule implements AttributeDisambiguationRule<String> { |
| 83 | + @Inject |
| 84 | + public DisambiguationRule() { } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void execute(MultipleCandidatesDetails<String> details) { |
| 88 | + var options = details.getCandidateValues(); |
| 89 | + if (options.contains("official")) { |
| 90 | + details.closestMatch("official"); |
| 91 | + } else if (options.contains("parchment")) { |
| 92 | + details.closestMatch("parchment"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + static final class MappingsVersion { |
| 99 | + static final Attribute<String> ATTRIBUTE = Attribute.of("net.minecraftforge.mappings.version", String.class); |
| 100 | + |
| 101 | + static final Comparator<String> COMPARATOR = Comparator.comparing(DefaultArtifactVersion::new); |
| 102 | + } |
| 103 | +} |
0 commit comments