88import groovy .transform .NamedParam ;
99import groovy .transform .NamedParams ;
1010import groovy .transform .NamedVariant ;
11- import net .minecraftforge .accesstransformers .gradle .AccessTransformersContainer ;
11+ import net .minecraftforge .accesstransformers .gradle .ArtifactAccessTransformer ;
1212import net .minecraftforge .gradleutils .shared .Closures ;
1313import org .gradle .api .Action ;
14- import org .gradle .api .InvalidUserDataException ;
15- import org .gradle .api .NamedDomainObjectProvider ;
1614import org .gradle .api .Project ;
17- import org .gradle .api .artifacts .Configuration ;
1815import org .gradle .api .artifacts .Dependency ;
19- import org .gradle .api .artifacts .DependencyScopeConfiguration ;
2016import org .gradle .api .artifacts .ExternalModuleDependency ;
17+ import org .gradle .api .artifacts .type .ArtifactTypeDefinition ;
2118import org .gradle .api .attributes .Attribute ;
19+ import org .gradle .api .attributes .AttributeContainer ;
20+ import org .gradle .api .attributes .Category ;
2221import org .gradle .api .file .ProjectLayout ;
23- import org .gradle .api .file .RegularFile ;
2422import org .gradle .api .file .RegularFileProperty ;
2523import org .gradle .api .model .ObjectFactory ;
26- import org .gradle .api .plugins .ExtensionAware ;
27- import org .gradle .api .plugins .ExtraPropertiesExtension .UnknownPropertyException ;
2824import org .gradle .api .provider .Property ;
2925import org .gradle .api .provider .Provider ;
3026import org .gradle .api .provider .ProviderFactory ;
3632import javax .inject .Inject ;
3733import java .io .File ;
3834import java .util .Map ;
35+ import java .util .Objects ;
3936
4037abstract class MinecraftDependencyImpl implements MinecraftDependencyInternal {
41- private static final String AT_COUNT_NAME = "fg_minecraft_atcontainers " ;
38+ private static final String AT_COUNT_NAME = "__fg_minecraft_atcontainers " ;
4239
4340 private @ UnknownNullability Provider <ExternalModuleDependency > delegate ;
4441
@@ -87,32 +84,28 @@ void resolve() {
8784 this .getDelegate ().get ();
8885 }
8986
87+ Action <? super AttributeContainer > addAttributes () {
88+ return attributes -> {
89+ attributes .attribute (MinecraftExtension .Attributes .os , this .getObjects ().named (OperatingSystemFamily .class , OperatingSystem .current ().getFamilyName ()));
90+ attributes .attributeProvider (MinecraftExtension .Attributes .mappingsChannel , mappings .map (MinecraftMappings ::channel ));
91+ attributes .attributeProvider (MinecraftExtension .Attributes .mappingsVersion , mappings .map (MinecraftMappings ::version ));
92+ };
93+ }
94+
9095 @ Override
9196 public void handle (SourceSet sourceSet ) {
9297 var configurations = this .project .getConfigurations ();
9398 var dependency = this .getDelegate ().get ();
9499
95- NamedDomainObjectProvider <DependencyScopeConfiguration > minecraftDependencyConstraints ;
96- try {
97- minecraftDependencyConstraints = configurations .dependencyScope ("minecraftDependencyConstraints" , configuration -> {
98- configuration .setDescription ("Transient dependency constraints for Minecraft dependencies." );
99- });
100- } catch (InvalidUserDataException e ) {
101- minecraftDependencyConstraints = configurations .named ("minecraftDependencyConstraints" , DependencyScopeConfiguration .class );
102- }
103-
104- configurations .getByName (sourceSet .getCompileClasspathConfigurationName ()).extendsFrom (minecraftDependencyConstraints .get ());
105- configurations .getByName (sourceSet .getRuntimeClasspathConfigurationName ()).extendsFrom (minecraftDependencyConstraints .get ());
106-
107- minecraftDependencyConstraints .get ().getDependencyConstraints ().add (this .project .getDependencies ().getConstraints ().create (dependency .getModule ().toString (), constraint -> {
108- constraint .because ("Accounts for mappings used and natives variants" );
109-
110- constraint .attributes (attributes -> {
111- attributes .attribute (MinecraftExtension .Attributes .os , this .getObjects ().named (OperatingSystemFamily .class , OperatingSystem .current ().getFamilyName ()));
112- attributes .attributeProvider (MinecraftExtension .Attributes .mappingsChannel , mappings .map (MinecraftMappings ::channel ));
113- attributes .attributeProvider (MinecraftExtension .Attributes .mappingsVersion , mappings .map (MinecraftMappings ::version ));
114- });
115- }));
100+ Util .forEachClasspath (configurations , sourceSet , configuration ->
101+ configuration .getResolutionStrategy ().dependencySubstitution (s -> {
102+ var moduleSelector = "%s:%s" .formatted (dependency .getModule (), dependency .getVersion ());
103+ var module = s .module (moduleSelector );
104+ s .substitute (module )
105+ .using (s .variant (module , variant -> variant .attributes (this .addAttributes ())))
106+ .because ("Accounts for mappings used and natives variants" );
107+ })
108+ );
116109 }
117110
118111 @ Override
@@ -153,22 +146,25 @@ public void mappings(
153146 }
154147
155148 static abstract class WithAccessTransformersImpl extends MinecraftDependencyImpl implements WithAccessTransformers {
156- private final AccessTransformersContainer atContainer ;
149+ private final RegularFileProperty atFile = this . getObjects (). fileProperty () ;
157150 private final Property <String > atPath = this .getObjects ().property (String .class );
158151
152+ private final Attribute <Boolean > attribute ;
153+
159154 @ Inject
160155 public WithAccessTransformersImpl (Project project ) {
161156 super (project );
162- this .atContainer = AccessTransformersContainer .register (project , it -> { });
157+ this .attribute = this .registerTransform ();
158+
163159 this .atPath .convention (project .getExtensions ().getByType (MinecraftExtensionForProjectWithAccessTransformers .class ).getAccessTransformers ());
164160 }
165161
166162 @ Override
167- Provider < ExternalModuleDependency > setDelegate ( Object dependencyNotation , Closure <?> closure ) {
168- return super . setDelegate ( dependencyNotation , Closures .< Dependency > consumer ( dependency -> {
169- Closures . invoke ( closure , dependency );
170- this . atContainer . configure ( dependency );
171- })) ;
163+ Action <? super AttributeContainer > addAttributes ( ) {
164+ return attributes -> {
165+ super . addAttributes (). execute ( attributes );
166+ attributes . attribute ( this . attribute , true );
167+ };
172168 }
173169
174170 @ Override
@@ -180,37 +176,61 @@ public void handle(SourceSet sourceSet) {
180176
181177 var itor = sourceSet .getResources ().getSrcDirs ().iterator ();
182178 if (itor .hasNext ()) {
183- this .getAccessTransformer () .convention (this .getProjectLayout ().file (this .getProviders ().provider (() -> new File (itor .next (), this .atPath .get ()))).get ());
179+ this .atFile .convention (this .getProjectLayout ().file (this .getProviders ().provider (() -> new File (itor .next (), this .atPath .get ()))).get ());
184180 } else {
185181 // weird edge case where a source set might not have any resources???
186182 // in which case, just best guess the location for accesstransformer.cfg
187- this .getAccessTransformer () .convention (this .getProjectLayout ().getProjectDirectory ().file (this .getProviders ().provider (() -> "src/%s/resources/%s" .formatted (sourceSet .getName (), this .atPath .get ()))).get ());
183+ this .atFile .convention (this .getProjectLayout ().getProjectDirectory ().file (this .getProviders ().provider (() -> "src/%s/resources/%s" .formatted (sourceSet .getName (), this .atPath .get ()))).get ());
188184 }
189185 }
190186
191- private int getAtContainerCount () {
192- try {
193- //noinspection DataFlowIssue
194- return (int ) this .project .getExtensions ().getExtraProperties ().get (AT_COUNT_NAME );
195- } catch (UnknownPropertyException ignored ) {
196- this .setAtContainerCount (0 );
197- return 0 ;
198- }
199- }
187+ private Attribute <Boolean > registerTransform () {
188+ var dependencies = this .project .getDependencies ();
189+
190+ var attribute = Attribute .of ("net.minecraftforge.gradle.accesstransformers.automatic." + this .getIndex (), Boolean .class );
191+
192+ dependencies .attributesSchema (attributesSchema -> attributesSchema .attribute (attribute ));
193+
194+ dependencies .getArtifactTypes ().named (
195+ ArtifactTypeDefinition .JAR_TYPE ,
196+ type -> type .getAttributes ().attribute (attribute , false )
197+ );
198+
199+ dependencies .registerTransform (ArtifactAccessTransformer .class , spec -> {
200+ spec .parameters (ArtifactAccessTransformer .Parameters .defaults (project , parameters -> {
201+ parameters .getConfig ().set (this .atFile );
202+ this .project .afterEvaluate (p ->
203+ ArtifactAccessTransformer .validateConfig (this .project , this .getDelegate ().get (), this .atFile )
204+ );
205+ }));
206+
207+ spec .getFrom ()
208+ .attribute (ArtifactTypeDefinition .ARTIFACT_TYPE_ATTRIBUTE , ArtifactTypeDefinition .JAR_TYPE )
209+ .attribute (Category .CATEGORY_ATTRIBUTE , this .getObjects ().named (Category .class , Category .LIBRARY ))
210+ .attribute (attribute , false );
200211
201- private void setAtContainerCount (int count ) {
202- this .project .getExtensions ().getExtraProperties ().set (AT_COUNT_NAME , count );
212+ spec .getTo ()
213+ .attribute (ArtifactTypeDefinition .ARTIFACT_TYPE_ATTRIBUTE , ArtifactTypeDefinition .JAR_TYPE )
214+ .attribute (Category .CATEGORY_ATTRIBUTE , this .getObjects ().named (Category .class , Category .LIBRARY ))
215+ .attribute (attribute , true );
216+ });
217+
218+ return attribute ;
203219 }
204220
205- private int postIncrementContainerCount () {
206- int ret = this .getAtContainerCount ();
207- this .setAtContainerCount (ret + 1 );
208- return ret ;
221+ private int getIndex () {
222+ var ext = this .project .getGradle ().getExtensions ().getExtraProperties ();
223+
224+ int index = ext .has (AT_COUNT_NAME )
225+ ? (int ) Objects .requireNonNull (ext .get (AT_COUNT_NAME ), "Internal extra property can never be null!" ) + 1
226+ : 0 ;
227+ ext .set (AT_COUNT_NAME , index );
228+ return index ;
209229 }
210230
211231 @ Override
212232 public RegularFileProperty getAccessTransformer () {
213- return this .atContainer . getOptions (). getConfig () ;
233+ return this .atFile ;
214234 }
215235
216236 @ Override
@@ -225,10 +245,5 @@ public void setAccessTransformer(boolean accessTransformer) {
225245 else
226246 this .atPath .unsetConvention ().unset ();
227247 }
228-
229- @ Override
230- public void accessTransformer (Action <? super AccessTransformersContainer .Options > options ) {
231- this .atContainer .options (options );
232- }
233248 }
234249}
0 commit comments