Merge remote-tracking branch 'FabricMC/exp/1.5' into exp/1.5

# Conflicts:
#	.github/workflows/publish.yml
#	gradle/libs.versions.toml
#	src/main/java/net/fabricmc/loom/LoomGradleExtension.java
#	src/main/java/net/fabricmc/loom/api/LoomGradleExtensionAPI.java
#	src/main/java/net/fabricmc/loom/build/nesting/IncludedJarFactory.java
#	src/main/java/net/fabricmc/loom/configuration/decompile/SingleJarDecompileConfiguration.java
#	src/main/java/net/fabricmc/loom/configuration/providers/mappings/tiny/TinyJarInfo.java
#	src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java
#	src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/ProcessedNamedMinecraftProvider.java
#	src/main/java/net/fabricmc/loom/extension/LoomGradleExtensionApiImpl.java
#	src/main/java/net/fabricmc/loom/task/RemapTaskConfiguration.java
#	src/test/groovy/net/fabricmc/loom/test/unit/kotlin/KotlinRemapperClassloaderTest.groovy
This commit is contained in:
shedaniel
2023-12-26 18:39:12 +08:00
68 changed files with 1270 additions and 404 deletions

View File

@@ -55,6 +55,7 @@ import net.fabricmc.loom.configuration.providers.minecraft.mapped.NamedMinecraft
import net.fabricmc.loom.configuration.providers.minecraft.mapped.SrgMinecraftProvider;
import net.fabricmc.loom.extension.LoomFiles;
import net.fabricmc.loom.extension.MixinExtension;
import net.fabricmc.loom.extension.RemapperExtensionHolder;
import net.fabricmc.loom.util.ModPlatform;
import net.fabricmc.loom.util.download.DownloadBuilder;
@@ -143,6 +144,8 @@ public interface LoomGradleExtension extends LoomGradleExtensionAPI {
ListProperty<LibraryProcessorManager.LibraryProcessorFactory> getLibraryProcessors();
ListProperty<RemapperExtensionHolder> getRemapperExtensions();
// ===================
// Architectury Loom
// ===================

View File

@@ -46,6 +46,8 @@ import net.fabricmc.loom.api.decompilers.DecompilerOptions;
import net.fabricmc.loom.api.mappings.intermediate.IntermediateMappingsProvider;
import net.fabricmc.loom.api.mappings.layered.spec.LayeredMappingSpecBuilder;
import net.fabricmc.loom.api.processor.MinecraftJarProcessor;
import net.fabricmc.loom.api.remapping.RemapperExtension;
import net.fabricmc.loom.api.remapping.RemapperParameters;
import net.fabricmc.loom.configuration.ide.RunConfig;
import net.fabricmc.loom.configuration.ide.RunConfigSettings;
import net.fabricmc.loom.configuration.processors.JarProcessor;
@@ -226,6 +228,13 @@ public interface LoomGradleExtensionAPI {
Property<Boolean> getSplitModDependencies();
<T extends RemapperParameters> void addRemapperExtension(Class<RemapperExtension<T>> remapperExtensionClass, Class<T> parametersClass, Action<T> parameterAction);
/**
* @return The minecraft version, as a {@link Provider}.
*/
Provider<String> getMinecraftVersion();
// ===================
// Architectury Loom
// ===================

View File

@@ -0,0 +1,47 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.api.remapping;
import org.objectweb.asm.commons.Remapper;
/**
* Context for a {@link RemapperExtension}.
*/
public interface RemapperContext {
/**
* @return The {@link Remapper} instance
*/
Remapper remapper();
/**
* @return the source namespace
*/
String sourceNamespace();
/**
* @return the target namespace
*/
String targetNamespace();
}

View File

@@ -0,0 +1,53 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.api.remapping;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.objectweb.asm.ClassVisitor;
/**
* A remapper extension can be used to add extra processing to the remapping process.
*
* <p>Implementations of RemapperExtension's must have the following:
* A single constructor annotated with {@link Inject}, and taking a single argument of the parameters.
* Or a single constructor annotated with {@link Inject} taking no arguments, when the extension does not have any parameters.
*
* <p>Use {@link net.fabricmc.loom.api.LoomGradleExtensionAPI#addRemapperExtension(Class, Class, Action)} to register a remapper extension.
*
* @param <T> Parameter type for the extension. Should be {@link RemapperParameters.None} if the action does not have parameters.
*/
public interface RemapperExtension<T extends RemapperParameters> {
/**
* Return a {@link ClassVisitor} that will be used when remapping the given class.
*
* @param className The name of the class being remapped
* @param remapperContext The remapper context
* @param classVisitor The parent class visitor
* @return A {@link ClassVisitor} that will be used when remapping the given class, or the given {@code classVisitor} if no extra processing is required for this class.
*/
ClassVisitor insertVisitor(String className, RemapperContext remapperContext, ClassVisitor classVisitor);
}

View File

@@ -22,16 +22,21 @@
* SOFTWARE.
*/
package net.fabricmc.loom.kotlin.remapping;
package net.fabricmc.loom.api.remapping;
import kotlin.Metadata;
import kotlinx.metadata.jvm.KotlinClassMetadata;
import org.jetbrains.annotations.ApiStatus;
/**
* Similar story to JvmExtensionWrapper, lets abuse the fact that Java can call "internal" Kotlin APIs without reflection :).
* Marker interface for parameter objects to {@link RemapperExtension}s.
*
* <p>Design based off of Gradle's {@link org.gradle.workers.WorkParameters}.
*/
public record KotlinClassMetadataWrapper(KotlinClassMetadata metadata) {
public Metadata getAnnotationData() {
return metadata.getAnnotationData$kotlinx_metadata_jvm();
public interface RemapperParameters {
final class None implements RemapperParameters {
@ApiStatus.Internal
public static None INSTANCE = new None();
private None() {
}
}
}

View File

@@ -0,0 +1,68 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.api.remapping;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.tinyremapper.TinyRemapper;
/**
* A remapper extension, that has direct access to the TinyRemapper APIs.
*
* <p>This API is not stable and may change without notice.
*/
@ApiStatus.Experimental
public interface TinyRemapperExtension {
/**
* See: {@link TinyRemapper.Builder#extraAnalyzeVisitor(TinyRemapper.AnalyzeVisitorProvider)}.
*
* @return A {@link TinyRemapper.AnalyzeVisitorProvider} or {@code null}.
*/
@Nullable
default TinyRemapper.AnalyzeVisitorProvider getAnalyzeVisitorProvider() {
return null;
}
/**
* See: {@link TinyRemapper.Builder#extraPreApplyVisitor(TinyRemapper.ApplyVisitorProvider)}.
*
* @return A {@link TinyRemapper.ApplyVisitorProvider} or {@code null}.
*/
@Nullable
default TinyRemapper.ApplyVisitorProvider getPreApplyVisitor() {
return null;
}
/**
* See: {@link TinyRemapper.Builder#extraPostApplyVisitor(TinyRemapper.ApplyVisitorProvider)}.
*
* @return A {@link TinyRemapper.ApplyVisitorProvider} or {@code null}.
*/
@Nullable
default TinyRemapper.ApplyVisitorProvider getPostApplyVisitor() {
return null;
}
}

View File

@@ -174,7 +174,8 @@ public final class IncludedJarFactory {
}
LoomGradleExtension extension = LoomGradleExtension.get(project);
File tempDir = new File(extension.getFiles().getProjectBuildCache(), "temp/modprocessing");
String childName = "temp/modprocessing/%s/%s/%s/%s".formatted(metadata.group().replace(".", "/"), metadata.name(), metadata.version(), input.getName());
File tempDir = new File(extension.getFiles().getProjectBuildCache(), childName);
if (!tempDir.exists()) {
tempDir.mkdirs();
@@ -182,15 +183,15 @@ public final class IncludedJarFactory {
File tempFile = new File(tempDir, input.getName());
if (tempFile.exists()) {
tempFile.delete();
if (tempFile.exists() && FabricModJsonFactory.isModJar(tempFile)) {
return tempFile;
}
try {
FileUtils.copyFile(input, tempFile);
// TODO generate Quilt qmjs natively
ZipReprocessorUtil.appendZipEntry(tempFile, "fabric.mod.json", generateModForDependency(metadata).getBytes(StandardCharsets.UTF_8));
ZipReprocessorUtil.appendZipEntry(tempFile.toPath(), "fabric.mod.json", generateModForDependency(metadata).getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new UncheckedIOException("Failed to add dummy mod while including %s".formatted(input), e);
}

View File

@@ -312,8 +312,10 @@ public abstract class CompileConfiguration implements Runnable {
private void configureDecompileTasks(ConfigContext configContext) {
final LoomGradleExtension extension = configContext.extension();
extension.getMinecraftJarConfiguration().get().getDecompileConfigurationBiFunction()
.apply(configContext, extension.getNamedMinecraftProvider()).afterEvaluation();
extension.getMinecraftJarConfiguration().get()
.getDecompileConfigurationBiFunction()
.apply(configContext.project(), extension.getNamedMinecraftProvider())
.afterEvaluation();
}
private Path getLockFile() {

View File

@@ -25,10 +25,13 @@
package net.fabricmc.loom.configuration;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.xml.parsers.DocumentBuilder;
@@ -53,6 +56,8 @@ import org.w3c.dom.NodeList;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.util.download.DownloadException;
import net.fabricmc.loom.util.fmj.FabricModJson;
import net.fabricmc.loom.util.fmj.FabricModJsonFactory;
import net.fabricmc.loom.util.gradle.SourceSetHelper;
public abstract class FabricApiExtension {
@@ -104,7 +109,6 @@ public abstract class FabricApiExtension {
DataGenerationSettings settings = getProject().getObjects().newInstance(DataGenerationSettings.class);
settings.getOutputDirectory().set(getProject().file("src/main/generated"));
settings.getCreateRunConfiguration().convention(true);
settings.getCreateSourceSet().convention(true);
settings.getCreateSourceSet().convention(false);
settings.getStrictValidation().convention(false);
settings.getAddToResources().convention(true);
@@ -117,7 +121,9 @@ public abstract class FabricApiExtension {
if (settings.getAddToResources().get()) {
mainSourceSet.resources(files -> {
// Add the src/main/generated to the main sourceset's resources.
files.getSrcDirs().add(outputDirectory);
Set<File> srcDirs = new HashSet<>(files.getSrcDirs());
srcDirs.add(outputDirectory);
files.setSrcDirs(srcDirs);
});
}
@@ -133,14 +139,10 @@ public abstract class FabricApiExtension {
});
if (settings.getCreateSourceSet().get()) {
if (!settings.getModId().isPresent()) {
throw new IllegalStateException("DataGenerationSettings.getModId() must be set when using split sources.");
}
SourceSetContainer sourceSets = SourceSetHelper.getSourceSets(getProject());
// Create the new datagen sourceset, depend on the main sourceset.
sourceSets.create(DATAGEN_SOURCESET_NAME, sourceSet -> {
SourceSet dataGenSourceSet = sourceSets.create(DATAGEN_SOURCESET_NAME, sourceSet -> {
sourceSet.setCompileClasspath(
sourceSet.getCompileClasspath()
.plus(mainSourceSet.getOutput())
@@ -155,6 +157,20 @@ public abstract class FabricApiExtension {
extendsFrom(getProject(), sourceSet.getRuntimeClasspathConfigurationName(), mainSourceSet.getRuntimeClasspathConfigurationName());
});
settings.getModId().convention(getProject().provider(() -> {
try {
final FabricModJson fabricModJson = FabricModJsonFactory.createFromSourceSetsNullable(dataGenSourceSet);
if (fabricModJson == null) {
throw new RuntimeException("Could not find a fabric.mod.json file in the data source set or a value for DataGenerationSettings.getModId()");
}
return fabricModJson.getId();
} catch (IOException e) {
throw new org.gradle.api.UncheckedIOException("Failed to read mod id from the datagen source set.", e);
}
}));
extension.getMods().create(settings.getModId().get(), mod -> {
// Create a classpath group for this mod. Assume that the main sourceset is already in a group.
mod.sourceSet(DATAGEN_SOURCESET_NAME);
@@ -165,7 +181,7 @@ public abstract class FabricApiExtension {
if (settings.getCreateRunConfiguration().get()) {
extension.getRunConfigs().create("datagen", run -> {
run.name("Data Generation");
run.setConfigName("Data Generation");
run.inherit(extension.getRunConfigs().getByName("server"));
run.property("fabric-api.datagen");

View File

@@ -83,7 +83,8 @@ public abstract class LoomConfigurations implements Runnable {
registerNonTransitive(Constants.Configurations.LOADER_DEPENDENCIES, Role.RESOLVABLE);
registerNonTransitive(Constants.Configurations.MINECRAFT, Role.NONE);
registerNonTransitive(Constants.Configurations.INCLUDE, Role.RESOLVABLE);
// We don't need to make this non-transitive due to the way we resolve it. Also, doing so would break platform dependencies.
register(Constants.Configurations.INCLUDE, Role.RESOLVABLE);
registerNonTransitive(Constants.Configurations.MAPPING_CONSTANTS, Role.RESOLVABLE);
register(Constants.Configurations.NAMED_ELEMENTS, Role.CONSUMABLE).configure(configuration -> {

View File

@@ -30,8 +30,8 @@ import org.gradle.api.Project;
import org.gradle.api.artifacts.ConfigurationContainer;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.ConfigContext;
import net.fabricmc.loom.configuration.providers.mappings.MappingConfiguration;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
import net.fabricmc.loom.configuration.providers.minecraft.mapped.MappedMinecraftProvider;
import net.fabricmc.loom.task.GenerateSourcesTask;
import net.fabricmc.loom.util.Constants;
@@ -42,13 +42,15 @@ public abstract class DecompileConfiguration<T extends MappedMinecraftProvider>
protected final LoomGradleExtension extension;
protected final MappingConfiguration mappingConfiguration;
public DecompileConfiguration(ConfigContext configContext, T minecraftProvider) {
this.project = configContext.project();
public DecompileConfiguration(Project project, T minecraftProvider) {
this.project = project;
this.minecraftProvider = minecraftProvider;
this.extension = configContext.extension();
this.extension = LoomGradleExtension.get(project);
this.mappingConfiguration = extension.getMappingConfiguration();
}
public abstract String getTaskName(MinecraftJar.Type type);
public abstract void afterEvaluation();
protected final void configureUnpick(GenerateSourcesTask task, File unpickOutputJar) {

View File

@@ -27,8 +27,9 @@ package net.fabricmc.loom.configuration.decompile;
import java.io.File;
import java.util.List;
import org.gradle.api.Project;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.ConfigContext;
import net.fabricmc.loom.configuration.providers.forge.MinecraftPatchedProvider;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJarConfiguration;
@@ -38,8 +39,13 @@ import net.fabricmc.loom.task.GenerateSourcesTask;
import net.fabricmc.loom.util.Constants;
public class SingleJarDecompileConfiguration extends DecompileConfiguration<MappedMinecraftProvider> {
public SingleJarDecompileConfiguration(ConfigContext configContext, MappedMinecraftProvider minecraftProvider) {
super(configContext, minecraftProvider);
public SingleJarDecompileConfiguration(Project project, MappedMinecraftProvider minecraftProvider) {
super(project, minecraftProvider);
}
@Override
public String getTaskName(MinecraftJar.Type type) {
return "genSources";
}
@Override
@@ -47,10 +53,11 @@ public class SingleJarDecompileConfiguration extends DecompileConfiguration<Mapp
final List<MinecraftJar> minecraftJars = minecraftProvider.getMinecraftJars();
assert minecraftJars.size() == 1;
final MinecraftJar minecraftJar = minecraftJars.get(0);
final String taskBaseName = getTaskName(minecraftJar.getType());
LoomGradleExtension.get(project).getDecompilerOptions().forEach(options -> {
final String decompilerName = options.getFormattedName();
String taskName = "genSourcesWith" + decompilerName;
String taskName = "%sWith%s".formatted(taskBaseName, decompilerName);
// Decompiler will be passed to the constructor of GenerateSourcesTask
project.getTasks().register(taskName, GenerateSourcesTask.class, options).configure(task -> {
task.getInputJarName().set(minecraftJar.getName());
@@ -67,7 +74,7 @@ public class SingleJarDecompileConfiguration extends DecompileConfiguration<Mapp
});
});
project.getTasks().register("genSources", task -> {
project.getTasks().register(taskBaseName, task -> {
task.setDescription("Decompile minecraft using the default decompiler.");
task.setGroup(Constants.TaskGroup.FABRIC);

View File

@@ -27,19 +27,28 @@ package net.fabricmc.loom.configuration.decompile;
import java.io.File;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskProvider;
import net.fabricmc.loom.api.decompilers.DecompilerOptions;
import net.fabricmc.loom.configuration.ConfigContext;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
import net.fabricmc.loom.configuration.providers.minecraft.mapped.MappedMinecraftProvider;
import net.fabricmc.loom.task.GenerateSourcesTask;
import net.fabricmc.loom.util.Constants;
public final class SplitDecompileConfiguration extends DecompileConfiguration<MappedMinecraftProvider.Split> {
public SplitDecompileConfiguration(ConfigContext configContext, MappedMinecraftProvider.Split minecraftProvider) {
super(configContext, minecraftProvider);
public SplitDecompileConfiguration(Project project, MappedMinecraftProvider.Split minecraftProvider) {
super(project, minecraftProvider);
}
@Override
public String getTaskName(MinecraftJar.Type type) {
return switch (type) {
case COMMON -> "genCommonSources";
case CLIENT_ONLY -> "genClientSources";
default -> throw new AssertionError();
};
}
@Override

View File

@@ -113,8 +113,11 @@ public class RunConfig {
return e;
}
private static void populate(Project project, LoomGradleExtension extension, RunConfig runConfig, String environment) {
runConfig.configName += extension.isRootProject() ? "" : " (" + project.getPath() + ")";
private static void populate(Project project, LoomGradleExtension extension, RunConfig runConfig, String environment, boolean appendProjectPath) {
if (appendProjectPath && !extension.isRootProject()) {
runConfig.configName += " (" + project.getPath() + ")";
}
runConfig.eclipseProjectName = project.getExtensions().getByType(EclipseModel.class).getProject().getName();
runConfig.mainClass = "net.fabricmc.devlaunchinjector.Main";
@@ -170,9 +173,10 @@ public class RunConfig {
runDir = "run";
}
boolean appendProjectPath = settings.getAppendProjectPathToConfigName().get();
RunConfig runConfig = new RunConfig();
runConfig.configName = configName;
populate(project, extension, runConfig, environment);
populate(project, extension, runConfig, environment, appendProjectPath);
runConfig.ideaModuleName = IdeaUtils.getIdeaModuleName(new SourceSetReference(sourceSet, project));
runConfig.runDirIdeaUrl = "file://$PROJECT_DIR$/" + runDir;
runConfig.runDir = runDir;

View File

@@ -74,9 +74,20 @@ public class RunConfigSettings implements Named {
* The full name of the run configuration, i.e. 'Minecraft Client'.
*
* <p>By default this is determined from the base name.
*
* <p>Note: unless the project is the root project (or {@link #appendProjectPathToConfigName} is disabled),
* the project path will be appended automatically, e.g. 'Minecraft Client (:some:project)'.
*/
private String configName;
/**
* Whether to append the project path to the {@link #configName} when {@code project} isn't the root project.
*
* <p>Warning: could produce ambiguous run config names if disabled, unless used carefully in conjunction with
* {@link #configName}.
*/
private final Property<Boolean> appendProjectPathToConfigName;
/**
* The default main class of the run configuration.
*
@@ -129,6 +140,7 @@ public class RunConfigSettings implements Named {
public RunConfigSettings(Project project, String name) {
this.name = name;
this.project = project;
this.appendProjectPathToConfigName = project.getObjects().property(Boolean.class).convention(true);
this.extension = LoomGradleExtension.get(project);
this.ideConfigGenerated = extension.isRootProject();
this.mainClass = project.getObjects().property(String.class).convention(project.provider(() -> {
@@ -210,6 +222,10 @@ public class RunConfigSettings implements Named {
this.configName = name;
}
public Property<Boolean> getAppendProjectPathToConfigName() {
return appendProjectPathToConfigName;
}
public String getDefaultMainClass() {
return defaultMainClass;
}

View File

@@ -0,0 +1,145 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.configuration.ide.idea;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.mods.dependency.LocalMavenHelper;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
import net.fabricmc.loom.configuration.providers.minecraft.mapped.NamedMinecraftProvider;
// See: https://github.com/JetBrains/intellij-community/blob/a09b1b84ab64a699794c860bc96774766dd38958/plugins/gradle/java/src/util/GradleAttachSourcesProvider.java
record DownloadSourcesHook(Project project, Task task) {
public static final String INIT_SCRIPT_NAME = "ijDownloadSources";
private static final Pattern NOTATION_PATTERN = Pattern.compile("dependencyNotation = '(?<notation>.*)'");
private static final Logger LOGGER = LoggerFactory.getLogger(DownloadSourcesHook.class);
public static boolean hasInitScript(Project project) {
List<File> initScripts = project.getGradle().getStartParameter().getInitScripts();
for (File initScript : initScripts) {
if (initScript.getName().contains(INIT_SCRIPT_NAME)) {
return true;
}
}
return false;
}
void tryHook() {
List<File> initScripts = project.getGradle().getStartParameter().getInitScripts();
for (File initScript : initScripts) {
if (!initScript.getName().contains(INIT_SCRIPT_NAME)) {
continue;
}
try {
final String script = Files.readString(initScript.toPath(), StandardCharsets.UTF_8);
final String notation = parseInitScript(script);
if (notation == null) {
LOGGER.debug("failed to parse init script dependency");
continue;
}
final MinecraftJar.Type jarType = getJarType(notation);
if (jarType == null) {
LOGGER.debug("init script is trying to download sources for another Minecraft jar ({}) not used by this project ({})", notation, project.getPath());
continue;
}
String sourcesTaskName = getGenSourcesTaskName(jarType);
task.dependsOn(project.getTasks().named(sourcesTaskName));
LOGGER.info("Running genSources task: {} in project: {} for {}", sourcesTaskName, project.getPath(), notation);
break;
} catch (IOException e) {
// Ignore
}
}
}
@Nullable
private String parseInitScript(String script) {
if (!script.contains("IjDownloadTask")) {
// Failed some basic sanity checks.
return null;
}
// A little gross but should do the job nicely.
final Matcher matcher = NOTATION_PATTERN.matcher(script);
if (matcher.find()) {
return matcher.group("notation");
}
return null;
}
private String getGenSourcesTaskName(MinecraftJar.Type jarType) {
LoomGradleExtension extension = LoomGradleExtension.get(project);
return extension.getMinecraftJarConfiguration().get()
.getDecompileConfigurationBiFunction()
.apply(project, extension.getNamedMinecraftProvider())
.getTaskName(jarType);
}
// Return the jar type, or null when this jar isnt used by the project
@Nullable
private MinecraftJar.Type getJarType(String name) {
final LoomGradleExtension extension = LoomGradleExtension.get(project);
final NamedMinecraftProvider<?> minecraftProvider = extension.getNamedMinecraftProvider();
final List<MinecraftJar.Type> dependencyTypes = minecraftProvider.getDependencyTypes();
if (dependencyTypes.isEmpty()) {
throw new IllegalStateException();
}
for (MinecraftJar.Type type : dependencyTypes) {
final LocalMavenHelper mavenHelper = minecraftProvider.getMavenHelper(type).withClassifier("sources");
if (mavenHelper.getNotation().equals(name)) {
return type;
}
}
return null;
}
}

View File

@@ -24,40 +24,27 @@
package net.fabricmc.loom.configuration.ide.idea;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import org.gradle.StartParameter;
import org.gradle.TaskExecutionRequest;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.internal.DefaultTaskExecutionRequest;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.ide.RunConfigSettings;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJarConfiguration;
import net.fabricmc.loom.task.LoomTasks;
import net.fabricmc.loom.util.gradle.GradleUtils;
public abstract class IdeaConfiguration implements Runnable {
private static final String INIT_SCRIPT_NAME = "ijmiscinit";
private static final Pattern NOTATION_PATTERN = Pattern.compile("'net\\.minecraft:(?<name>.*):(.*):sources'");
@Inject
protected abstract Project getProject();
public void run() {
TaskProvider<IdeaSyncTask> ideaSyncTask = getProject().getTasks().register("ideaSyncTask", IdeaSyncTask.class, task -> {
getProject().getTasks().register("ideaSyncTask", IdeaSyncTask.class, task -> {
if (LoomGradleExtension.get(getProject()).getRunConfigs().stream().anyMatch(RunConfigSettings::isIdeConfigGenerated)) {
task.dependsOn(LoomTasks.getIDELaunchConfigureTaskName(getProject()));
} else {
@@ -65,11 +52,7 @@ public abstract class IdeaConfiguration implements Runnable {
}
});
getProject().getTasks().configureEach(task -> {
if (task.getName().equals("DownloadSources")) {
hookDownloadSources(getProject(), task);
}
});
hookDownloadSources();
if (!IdeaUtils.isIdeaSync()) {
return;
@@ -82,62 +65,27 @@ public abstract class IdeaConfiguration implements Runnable {
startParameter.setTaskRequests(taskRequests);
}
/*
"Parse" the init script enough to figure out what jar we are talking about.
private void hookDownloadSources() {
LoomGradleExtension extension = LoomGradleExtension.get(getProject());
Intelij code: https://github.com/JetBrains/intellij-community/blob/a09b1b84ab64a699794c860bc96774766dd38958/plugins/gradle/java/src/util/GradleAttachSourcesProvider.java
*/
private static void hookDownloadSources(Project project, Task task) {
List<File> initScripts = project.getGradle().getStartParameter().getInitScripts();
if (!extension.isRootProject()) {
return;
}
for (File initScript : initScripts) {
if (!initScript.getName().contains(INIT_SCRIPT_NAME)) {
continue;
if (!DownloadSourcesHook.hasInitScript(getProject())) {
return;
}
getProject().getTasks().configureEach(task -> {
if (task.getName().startsWith(DownloadSourcesHook.INIT_SCRIPT_NAME)) {
getProject().allprojects(subProject -> {
if (!GradleUtils.isLoomProject(subProject)) {
return;
}
new DownloadSourcesHook(subProject, task).tryHook();
});
}
try {
final String script = Files.readString(initScript.toPath(), StandardCharsets.UTF_8);
final String notation = parseInitScript(project, script);
if (notation != null) {
task.dependsOn(getGenSourcesTaskName(LoomGradleExtension.get(project), notation));
}
} catch (IOException e) {
// Ignore
}
}
}
@Nullable
private static String parseInitScript(Project project, String script) {
if (!script.contains("Attempt to download sources from")
|| !script.contains("downloadSources_")
|| !script.contains("'%s'".formatted(project.getPath()))) {
// Failed some basic sanity checks.
return null;
}
// A little gross but should do the job nicely.
final Matcher matcher = NOTATION_PATTERN.matcher(script);
if (matcher.find()) {
return matcher.group("name");
}
return null;
}
private static String getGenSourcesTaskName(LoomGradleExtension extension, String notation) {
final MinecraftJarConfiguration configuration = extension.getMinecraftJarConfiguration().get();
if (configuration == MinecraftJarConfiguration.SPLIT) {
if (notation.toLowerCase(Locale.ROOT).contains("minecraft-clientonly")) {
return "genClientOnlySources";
}
return "genCommonSources";
}
return "genSources";
});
}
}

View File

@@ -259,6 +259,10 @@ public class ModConfigurationRemapper {
@Nullable
public static Path findSources(Project project, ResolvedArtifact artifact) {
if (isCIBuild()) {
return null;
}
final DependencyHandler dependencies = project.getDependencies();
@SuppressWarnings("unchecked") ArtifactResolutionQuery query = dependencies.createArtifactResolutionQuery()

View File

@@ -59,6 +59,7 @@ import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.build.IntermediaryNamespaces;
import net.fabricmc.loom.configuration.mods.dependency.ModDependency;
import net.fabricmc.loom.configuration.providers.mappings.MappingConfiguration;
import net.fabricmc.loom.extension.RemapperExtensionHolder;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.LoggerFilter;
import net.fabricmc.loom.util.ModPlatform;
@@ -197,6 +198,10 @@ public class ModProcessor {
builder.extension(new MixinExtension(tag -> extension.isNeoForge() || remapMixins.contains(tag)));
}
for (RemapperExtensionHolder holder : extension.getRemapperExtensions().get()) {
holder.apply(builder, fromM, toM, project.getObjects());
}
final TinyRemapper remapper = builder.build();
for (Path minecraftJar : extension.getMinecraftJars(IntermediaryNamespaces.intermediaryNamespace(project))) {

View File

@@ -34,22 +34,7 @@ import java.nio.file.StandardCopyOption;
import org.jetbrains.annotations.Nullable;
public final class LocalMavenHelper {
private final String group;
private final String name;
private final String version;
@Nullable
private final String baseClassifier;
private final Path root;
public LocalMavenHelper(String group, String name, String version, @Nullable String classifier, Path root) {
this.group = group;
this.name = name;
this.version = version;
this.baseClassifier = classifier;
this.root = root;
}
public record LocalMavenHelper(String group, String name, String version, @Nullable String baseClassifier, Path root) {
public Path copyToMaven(Path artifact, @Nullable String classifier) throws IOException {
if (!artifact.getFileName().toString().endsWith(".jar")) {
throw new UnsupportedOperationException();
@@ -108,4 +93,8 @@ public final class LocalMavenHelper {
: String.format("%s-%s-%s.jar", name, version, classifier);
return getDirectory().resolve(fileName);
}
public LocalMavenHelper withClassifier(String classifier) {
return new LocalMavenHelper(group, name, version, classifier, root);
}
}

View File

@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2016-2022 FabricMC
* Copyright (c) 2016-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
@@ -122,7 +122,7 @@ public class MappingConfiguration {
final TinyJarInfo jarInfo = TinyJarInfo.get(inputJar);
jarInfo.minecraftVersionId().ifPresent(id -> {
if (!minecraftProvider.minecraftVersion().equals(id)) {
LOGGER.warn("The mappings (%s) were not build for minecraft version (%s) produce with caution.".formatted(dependency.getDepString(), minecraftProvider.minecraftVersion()));
LOGGER.warn("The mappings (%s) were not built for Minecraft version %s, proceed with caution.".formatted(dependency.getDepString(), minecraftProvider.minecraftVersion()));
}
});

View File

@@ -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
@@ -26,32 +26,52 @@ package net.fabricmc.loom.configuration.providers.mappings.tiny;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.jar.Manifest;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.format.MappingFormat;
public record TinyJarInfo(boolean v2, Optional<String> minecraftVersionId) {
private static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";
private static final String MANIFEST_VERSION_ID_ATTRIBUTE = "Minecraft-Version-Id";
public static TinyJarInfo get(Path jar) {
try {
return new TinyJarInfo(doesJarContainV2Mappings(jar), Optional.empty());
try (FileSystemUtil.Delegate delegate = FileSystemUtil.getJarFileSystem(jar)) {
return new TinyJarInfo(doesJarContainV2Mappings(delegate), getMinecraftVersionId(delegate));
} catch (IOException e) {
throw new UncheckedIOException("Failed to read tiny jar info", e);
}
}
private static boolean doesJarContainV2Mappings(Path path) throws IOException {
try (FileSystemUtil.Delegate delegate = FileSystemUtil.getJarFileSystem(path)) {
try (BufferedReader reader = Files.newBufferedReader(delegate.fs().getPath("mappings", "mappings.tiny"))) {
return MappingReader.detectFormat(reader) == MappingFormat.TINY_2_FILE;
}
private static boolean doesJarContainV2Mappings(FileSystemUtil.Delegate fs) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(fs.getPath("mappings", "mappings.tiny"))) {
return MappingReader.detectFormat(reader) == MappingFormat.TINY_2_FILE;
}
} catch (NoSuchFileException e) {
return false;
}
private static Optional<String> getMinecraftVersionId(FileSystemUtil.Delegate fs) throws IOException {
final Path manifestPath = fs.getPath(MANIFEST_PATH);
if (Files.exists(manifestPath)) {
final var manifest = new Manifest();
try (InputStream in = Files.newInputStream(manifestPath)) {
manifest.read(in);
}
final String minecraftVersionId = manifest.getMainAttributes().getValue(MANIFEST_VERSION_ID_ATTRIBUTE);
return Optional.ofNullable(minecraftVersionId);
}
return Optional.empty();
}
}

View File

@@ -31,14 +31,14 @@ import java.util.Objects;
public abstract sealed class MinecraftJar permits MinecraftJar.Client, MinecraftJar.ClientOnly, MinecraftJar.Common, MinecraftJar.Merged, MinecraftJar.Server {
private final Path path;
private final boolean merged, client, server;
private final String name;
private final Type type;
protected MinecraftJar(Path path, boolean merged, boolean client, boolean server, String name) {
protected MinecraftJar(Path path, boolean merged, boolean client, boolean server, Type type) {
this.path = Objects.requireNonNull(path);
this.merged = merged;
this.client = client;
this.server = server;
this.name = name;
this.type = type;
}
public Path getPath() {
@@ -62,16 +62,18 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
}
public String getName() {
return name;
return type.toString();
}
public Type getType() {
return type;
}
public abstract MinecraftJar forPath(Path path);
public static final class Merged extends MinecraftJar {
public static final String NAME = "merged";
public Merged(Path path) {
super(path, true, true, true, NAME);
super(path, true, true, true, Type.MERGED);
}
@Override
@@ -81,10 +83,8 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
}
public static final class Common extends MinecraftJar {
public static final String NAME = "common";
public Common(Path path) {
super(path, false, false, true, NAME);
super(path, false, false, true, Type.COMMON);
}
@Override
@@ -94,10 +94,8 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
}
public static final class Server extends MinecraftJar {
public static final String NAME = "server";
public Server(Path path) {
super(path, false, false, true, NAME);
super(path, false, false, true, Type.SERVER);
}
@Override
@@ -108,10 +106,8 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
// Un-split client jar
public static final class Client extends MinecraftJar {
public static final String NAME = "client";
public Client(Path path) {
super(path, false, true, false, NAME);
super(path, false, true, false, Type.CLIENT);
}
@Override
@@ -122,10 +118,8 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
// Split client jar
public static final class ClientOnly extends MinecraftJar {
public static final String NAME = "clientOnly";
public ClientOnly(Path path) {
super(path, false, true, false, NAME);
super(path, false, true, false, Type.CLIENT_ONLY);
}
@Override
@@ -133,4 +127,28 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
return new ClientOnly(path);
}
}
public enum Type {
// Merged jar
MERGED("merged"),
// Regular jars, not merged or split
SERVER("server"),
CLIENT("client"),
// Split jars
COMMON("common"),
CLIENT_ONLY("clientOnly");
private final String name;
Type(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
}

View File

@@ -91,7 +91,7 @@ public enum MinecraftJarConfiguration {
private final BiFunction<Project, MinecraftProvider, SrgMinecraftProvider<?>> srgMinecraftProviderBiFunction;
private final BiFunction<Project, MinecraftProvider, MojangMappedMinecraftProvider<?>> mojangMappedMinecraftProviderBiFunction;
private final BiFunction<NamedMinecraftProvider<?>, MinecraftJarProcessorManager, ProcessedNamedMinecraftProvider<?, ?>> processedNamedMinecraftProviderBiFunction;
private final BiFunction<ConfigContext, MappedMinecraftProvider, DecompileConfiguration<?>> decompileConfigurationBiFunction;
private final BiFunction<Project, MappedMinecraftProvider, DecompileConfiguration<?>> decompileConfigurationBiFunction;
private final List<String> supportedEnvironments;
@SuppressWarnings("unchecked") // Just a bit of a generic mess :)
@@ -102,7 +102,7 @@ public enum MinecraftJarConfiguration {
BiFunction<Project, M, SrgMinecraftProvider<M>> srgMinecraftProviderBiFunction,
BiFunction<Project, M, MojangMappedMinecraftProvider<M>> mojangMappedMinecraftProviderBiFunction,
BiFunction<P, MinecraftJarProcessorManager, ProcessedNamedMinecraftProvider<M, P>> processedNamedMinecraftProviderBiFunction,
BiFunction<ConfigContext, Q, DecompileConfiguration<?>> decompileConfigurationBiFunction,
BiFunction<Project, Q, DecompileConfiguration<?>> decompileConfigurationBiFunction,
List<String> supportedEnvironments
) {
this.minecraftProviderFunction = (Function<ConfigContext, MinecraftProvider>) minecraftProviderFunction;
@@ -111,7 +111,7 @@ public enum MinecraftJarConfiguration {
this.srgMinecraftProviderBiFunction = (BiFunction<Project, MinecraftProvider, SrgMinecraftProvider<?>>) (Object) srgMinecraftProviderBiFunction;
this.mojangMappedMinecraftProviderBiFunction = (BiFunction<Project, MinecraftProvider, MojangMappedMinecraftProvider<?>>) (Object) mojangMappedMinecraftProviderBiFunction;
this.processedNamedMinecraftProviderBiFunction = (BiFunction<NamedMinecraftProvider<?>, MinecraftJarProcessorManager, ProcessedNamedMinecraftProvider<?, ?>>) (Object) processedNamedMinecraftProviderBiFunction;
this.decompileConfigurationBiFunction = (BiFunction<ConfigContext, MappedMinecraftProvider, DecompileConfiguration<?>>) decompileConfigurationBiFunction;
this.decompileConfigurationBiFunction = (BiFunction<Project, MappedMinecraftProvider, DecompileConfiguration<?>>) decompileConfigurationBiFunction;
this.supportedEnvironments = supportedEnvironments;
}
@@ -131,7 +131,7 @@ public enum MinecraftJarConfiguration {
return processedNamedMinecraftProviderBiFunction;
}
public BiFunction<ConfigContext, MappedMinecraftProvider, DecompileConfiguration<?>> getDecompileConfigurationBiFunction() {
public BiFunction<Project, MappedMinecraftProvider, DecompileConfiguration<?>> getDecompileConfigurationBiFunction() {
return decompileConfigurationBiFunction;
}

View File

@@ -45,7 +45,7 @@ public abstract sealed class MinecraftSourceSets permits MinecraftSourceSets.Sin
return LoomGradleExtension.get(project).areEnvironmentSourceSetsSplit() ? Split.INSTANCE : Single.INSTANCE;
}
public abstract void applyDependencies(BiConsumer<String, String> consumer, List<String> targets);
public abstract void applyDependencies(BiConsumer<String, MinecraftJar.Type> consumer, List<MinecraftJar.Type> targets);
public abstract String getSourceSetForEnv(String env);
@@ -104,8 +104,8 @@ public abstract sealed class MinecraftSourceSets permits MinecraftSourceSets.Sin
private static final Single INSTANCE = new Single();
@Override
public void applyDependencies(BiConsumer<String, String> consumer, List<String> targets) {
for (String target : targets) {
public void applyDependencies(BiConsumer<String, MinecraftJar.Type> consumer, List<MinecraftJar.Type> targets) {
for (MinecraftJar.Type target : targets) {
consumer.accept(MINECRAFT_NAMED.compile(), target);
consumer.accept(MINECRAFT_NAMED.runtime(), target);
}
@@ -154,15 +154,15 @@ public abstract sealed class MinecraftSourceSets permits MinecraftSourceSets.Sin
private static final Split INSTANCE = new Split();
@Override
public void applyDependencies(BiConsumer<String, String> consumer, List<String> targets) {
public void applyDependencies(BiConsumer<String, MinecraftJar.Type> consumer, List<MinecraftJar.Type> targets) {
Preconditions.checkArgument(targets.size() == 2);
Preconditions.checkArgument(targets.contains(MinecraftJar.Common.NAME));
Preconditions.checkArgument(targets.contains(MinecraftJar.ClientOnly.NAME));
Preconditions.checkArgument(targets.contains(MinecraftJar.Type.COMMON));
Preconditions.checkArgument(targets.contains(MinecraftJar.Type.CLIENT_ONLY));
consumer.accept(MINECRAFT_COMMON_NAMED.runtime(), MinecraftJar.Common.NAME);
consumer.accept(MINECRAFT_CLIENT_ONLY_NAMED.runtime(), MinecraftJar.ClientOnly.NAME);
consumer.accept(MINECRAFT_COMMON_NAMED.compile(), MinecraftJar.Common.NAME);
consumer.accept(MINECRAFT_CLIENT_ONLY_NAMED.compile(), MinecraftJar.ClientOnly.NAME);
consumer.accept(MINECRAFT_COMMON_NAMED.runtime(), MinecraftJar.Type.COMMON);
consumer.accept(MINECRAFT_CLIENT_ONLY_NAMED.runtime(), MinecraftJar.Type.CLIENT_ONLY);
consumer.accept(MINECRAFT_COMMON_NAMED.compile(), MinecraftJar.Type.COMMON);
consumer.accept(MINECRAFT_CLIENT_ONLY_NAMED.compile(), MinecraftJar.Type.CLIENT_ONLY);
}
@Override

View File

@@ -28,22 +28,22 @@ import java.nio.file.Path;
import java.util.function.Function;
public enum SingleJarEnvType {
CLIENT(MinecraftJar.Client::new, MinecraftJar.Client.NAME),
SERVER(MinecraftJar.Server::new, MinecraftJar.Server.NAME);
CLIENT(MinecraftJar.Client::new, MinecraftJar.Type.CLIENT),
SERVER(MinecraftJar.Server::new, MinecraftJar.Type.SERVER);
private final Function<Path, MinecraftJar> jarFunction;
private final String name;
private final MinecraftJar.Type type;
SingleJarEnvType(Function<Path, MinecraftJar> jarFunction, String name) {
SingleJarEnvType(Function<Path, MinecraftJar> jarFunction, MinecraftJar.Type type) {
this.jarFunction = jarFunction;
this.name = name;
this.type = type;
}
public Function<Path, MinecraftJar> getJar() {
return jarFunction;
}
public String getName() {
return name;
public MinecraftJar.Type getType() {
return type;
}
}

View File

@@ -77,7 +77,8 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
public abstract List<RemappedJars> getRemappedJars();
public List<String> getDependencyTargets() {
// Returns a list of MinecraftJar.Type's that this provider exports to be used as a dependency
public List<MinecraftJar.Type> getDependencyTypes() {
return Collections.emptyList();
}
@@ -96,11 +97,11 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
}
if (context.applyDependencies()) {
final List<String> dependencyTargets = getDependencyTargets();
final List<MinecraftJar.Type> dependencyTargets = getDependencyTypes();
if (!dependencyTargets.isEmpty()) {
MinecraftSourceSets.get(getProject()).applyDependencies(
(configuration, name) -> getProject().getDependencies().add(configuration, getDependencyNotation(name)),
(configuration, type) -> getProject().getDependencies().add(configuration, getDependencyNotation(type)),
dependencyTargets
);
}
@@ -118,8 +119,8 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
}
@Override
public Path getJar(String name) {
return getMavenHelper(name).getOutputFile(null);
public Path getJar(MinecraftJar.Type type) {
return getMavenHelper(type).getOutputFile(null);
}
public enum MavenScope {
@@ -141,16 +142,16 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
public abstract MavenScope getMavenScope();
public LocalMavenHelper getMavenHelper(String name) {
return new LocalMavenHelper("net.minecraft", getName(name), getVersion(), null, getMavenScope().getRoot(extension));
public LocalMavenHelper getMavenHelper(MinecraftJar.Type type) {
return new LocalMavenHelper("net.minecraft", getName(type), getVersion(), null, getMavenScope().getRoot(extension));
}
protected String getName(String name) {
protected String getName(MinecraftJar.Type type) {
final String intermediateName = extension.getIntermediateMappingsProvider().getName();
var sj = new StringJoiner("-");
sj.add("minecraft");
sj.add(name);
sj.add(type.toString());
// Include the intermediate mapping name if it's not the default intermediary
if (!intermediateName.equals(IntermediaryMappingsProvider.NAME)) {
@@ -168,13 +169,13 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
return "%s-%s".formatted(extension.getMinecraftProvider().minecraftVersion(), extension.getMappingConfiguration().mappingsIdentifier());
}
protected String getDependencyNotation(String name) {
return "net.minecraft:%s:%s".formatted(getName(name), getVersion());
protected String getDependencyNotation(MinecraftJar.Type type) {
return "net.minecraft:%s:%s".formatted(getName(type), getVersion());
}
private boolean areOutputsValid(List<RemappedJars> remappedJars) {
for (RemappedJars remappedJar : remappedJars) {
if (!getMavenHelper(remappedJar.name()).exists(null)) {
if (!getMavenHelper(remappedJar.type()).exists(null)) {
return false;
}
}
@@ -224,7 +225,7 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
remapper.finish();
}
getMavenHelper(remappedJars.name()).savePom();
getMavenHelper(remappedJars.type()).savePom();
if (extension.isForgeLikeAndOfficial()) {
try (var serviceManager = new ScopedSharedServiceManager()) {
@@ -281,5 +282,9 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
public String name() {
return outputJar().getName();
}
public MinecraftJar.Type type() {
return outputJar().getType();
}
}
}

View File

@@ -38,14 +38,12 @@ public interface MappedMinecraftProvider {
List<MinecraftJar> getMinecraftJars();
interface ProviderImpl extends MappedMinecraftProvider {
Path getJar(String name);
Path getJar(MinecraftJar.Type type);
}
interface Merged extends ProviderImpl {
String MERGED = MinecraftJar.Merged.NAME;
default MinecraftJar getMergedJar() {
return new MinecraftJar.Merged(getJar(MERGED));
return new MinecraftJar.Merged(getJar(MinecraftJar.Type.MERGED));
}
@Override
@@ -55,15 +53,12 @@ public interface MappedMinecraftProvider {
}
interface Split extends ProviderImpl {
String COMMON = MinecraftJar.Common.NAME;
String CLIENT_ONLY = MinecraftJar.ClientOnly.NAME;
default MinecraftJar getCommonJar() {
return new MinecraftJar.Common(getJar(COMMON));
return new MinecraftJar.Common(getJar(MinecraftJar.Type.COMMON));
}
default MinecraftJar getClientOnlyJar() {
return new MinecraftJar.ClientOnly(getJar(CLIENT_ONLY));
return new MinecraftJar.ClientOnly(getJar(MinecraftJar.Type.CLIENT_ONLY));
}
@Override
@@ -75,12 +70,12 @@ public interface MappedMinecraftProvider {
interface SingleJar extends ProviderImpl {
SingleJarEnvType env();
default String envName() {
return env().getName();
default MinecraftJar.Type envType() {
return env().getType();
}
default MinecraftJar getEnvOnlyJar() {
return env().getJar().apply(getJar(envName()));
return env().getJar().apply(getJar(env().getType()));
}
@Override

View File

@@ -31,6 +31,7 @@ import org.gradle.api.Project;
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.configuration.providers.minecraft.MergedMinecraftProvider;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftProvider;
import net.fabricmc.loom.configuration.providers.minecraft.SingleJarEnvType;
import net.fabricmc.loom.configuration.providers.minecraft.SingleJarMinecraftProvider;
@@ -64,8 +65,8 @@ public abstract class NamedMinecraftProvider<M extends MinecraftProvider> extend
}
@Override
public List<String> getDependencyTargets() {
return List.of(MERGED);
public List<MinecraftJar.Type> getDependencyTypes() {
return List.of(MinecraftJar.Type.MERGED);
}
}
@@ -88,8 +89,8 @@ public abstract class NamedMinecraftProvider<M extends MinecraftProvider> extend
}
@Override
public List<String> getDependencyTargets() {
return List.of(CLIENT_ONLY, COMMON);
public List<MinecraftJar.Type> getDependencyTypes() {
return List.of(MinecraftJar.Type.CLIENT_ONLY, MinecraftJar.Type.COMMON);
}
}
@@ -117,8 +118,8 @@ public abstract class NamedMinecraftProvider<M extends MinecraftProvider> extend
}
@Override
public List<String> getDependencyTargets() {
return List.of(envName());
public List<MinecraftJar.Type> getDependencyTypes() {
return List.of(envType());
}
@Override

View File

@@ -88,7 +88,7 @@ public abstract class ProcessedNamedMinecraftProvider<M extends MinecraftProvide
final MinecraftJar outputJar = entry.getValue();
deleteSimilarJars(outputJar.getPath());
final LocalMavenHelper mavenHelper = getMavenHelper(minecraftJar.getName());
final LocalMavenHelper mavenHelper = getMavenHelper(minecraftJar.getType());
final Path outputPath = mavenHelper.copyToMaven(minecraftJar.getPath(), null);
assert outputJar.getPath().equals(outputPath);
@@ -97,8 +97,13 @@ public abstract class ProcessedNamedMinecraftProvider<M extends MinecraftProvide
}
}
@Override
public List<MinecraftJar.Type> getDependencyTypes() {
return parentMinecraftProvider.getDependencyTypes();
}
private void applyDependencies() {
final List<String> dependencyTargets = parentMinecraftProvider.getDependencyTargets();
final List<MinecraftJar.Type> dependencyTargets = getDependencyTypes();
if (dependencyTargets.isEmpty()) {
return;
@@ -125,14 +130,14 @@ public abstract class ProcessedNamedMinecraftProvider<M extends MinecraftProvide
}
@Override
protected String getName(String name) {
protected String getName(MinecraftJar.Type type) {
final String jarPrefix = parentMinecraftProvider.getMinecraftProvider().getJarPrefix();
// Hash the cache value so that we don't have to process the same JAR multiple times for many projects
return jarPrefix + "minecraft-%s-%s".formatted(name, jarProcessorManager.getJarHash());
return jarPrefix + "minecraft-%s-%s".formatted(type.toString(), jarProcessorManager.getJarHash());
}
@Override
public Path getJar(String name) {
public Path getJar(MinecraftJar.Type type) {
// Something has gone wrong if this gets called.
throw new UnsupportedOperationException();
}
@@ -154,7 +159,7 @@ public abstract class ProcessedNamedMinecraftProvider<M extends MinecraftProvide
}
private Path getProcessedPath(MinecraftJar minecraftJar) {
final LocalMavenHelper mavenHelper = getMavenHelper(minecraftJar.getName());
final LocalMavenHelper mavenHelper = getMavenHelper(minecraftJar.getType());
return mavenHelper.getOutputFile(null);
}

View File

@@ -53,6 +53,6 @@ public final class LoomFilesProjectImpl extends LoomFilesBaseImpl {
@Override
protected File getBuildDir() {
return project.getBuildDir();
return project.getLayout().getBuildDirectory().getAsFile().get();
}
}

View File

@@ -25,6 +25,7 @@
package net.fabricmc.loom.extension;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -38,9 +39,11 @@ import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.NamedDomainObjectList;
import org.gradle.api.Project;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
@@ -60,6 +63,8 @@ import net.fabricmc.loom.api.decompilers.DecompilerOptions;
import net.fabricmc.loom.api.mappings.intermediate.IntermediateMappingsProvider;
import net.fabricmc.loom.api.mappings.layered.spec.LayeredMappingSpecBuilder;
import net.fabricmc.loom.api.processor.MinecraftJarProcessor;
import net.fabricmc.loom.api.remapping.RemapperExtension;
import net.fabricmc.loom.api.remapping.RemapperParameters;
import net.fabricmc.loom.configuration.RemapConfigurations;
import net.fabricmc.loom.configuration.ide.RunConfig;
import net.fabricmc.loom.configuration.ide.RunConfigSettings;
@@ -73,6 +78,8 @@ import net.fabricmc.loom.configuration.providers.minecraft.MinecraftSourceSets;
import net.fabricmc.loom.task.GenerateSourcesTask;
import net.fabricmc.loom.util.DeprecationHelper;
import net.fabricmc.loom.util.ModPlatform;
import net.fabricmc.loom.util.fmj.FabricModJson;
import net.fabricmc.loom.util.fmj.FabricModJsonFactory;
import net.fabricmc.loom.util.gradle.SourceSetHelper;
/**
@@ -99,13 +106,12 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
private final Property<Boolean> splitEnvironmentalSourceSet;
private final InterfaceInjectionExtensionAPI interfaceInjectionExtension;
private final ModVersionParser versionParser;
private final NamedDomainObjectContainer<RunConfigSettings> runConfigs;
private final NamedDomainObjectContainer<DecompilerOptions> decompilers;
private final NamedDomainObjectContainer<ModSettings> mods;
private final NamedDomainObjectList<RemapConfigurationSettings> remapConfigurations;
private final ListProperty<MinecraftJarProcessor<?>> minecraftJarProcessors;
protected final ListProperty<RemapperExtensionHolder> remapperExtensions;
// A common mistake with layered mappings is to call the wrong `officialMojangMappings` method, use this to keep track of when we are building a layered mapping spec.
protected final ThreadLocal<Boolean> layeredSpecBuilderScope = ThreadLocal.withInitial(() -> false);
@@ -143,8 +149,6 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
this.intermediateMappingsProvider = project.getObjects().property(IntermediateMappingsProvider.class);
this.intermediateMappingsProvider.finalizeValueOnRead();
this.versionParser = new ModVersionParser(project);
this.deprecationHelper = new DeprecationHelper.ProjectBased(project);
this.runConfigs = project.container(RunConfigSettings.class,
@@ -174,6 +178,9 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
this.splitEnvironmentalSourceSet = project.getObjects().property(Boolean.class).convention(false);
this.splitEnvironmentalSourceSet.finalizeValueOnRead();
remapperExtensions = project.getObjects().listProperty(RemapperExtensionHolder.class);
remapperExtensions.finalizeValueOnRead();
// Enable dep iface injection by default
interfaceInjection(interfaceInjection -> {
interfaceInjection.getEnableDependencyInterfaceInjection().convention(true).finalizeValueOnRead();
@@ -292,7 +299,17 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
@Override
public String getModVersion() {
return versionParser.getModVersion();
try {
final FabricModJson fabricModJson = FabricModJsonFactory.createFromSourceSetsNullable(SourceSetHelper.getMainSourceSet(getProject()));
if (fabricModJson == null) {
throw new RuntimeException("Could not find a fabric.mod.json file in the main sourceset");
}
return fabricModJson.getModVersion();
} catch (IOException e) {
throw new UncheckedIOException("Failed to read mod version from main sourceset.", e);
}
}
@Override
@@ -430,6 +447,28 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
RemapConfigurations.setupForSourceSet(getProject(), sourceSet);
}
@Override
public <T extends RemapperParameters> void addRemapperExtension(Class<RemapperExtension<T>> remapperExtensionClass, Class<T> parametersClass, Action<T> parameterAction) {
final ObjectFactory objectFactory = getProject().getObjects();
final RemapperExtensionHolder holder;
if (parametersClass != RemapperParameters.None.class) {
T parameters = objectFactory.newInstance(parametersClass);
parameterAction.execute(parameters);
holder = objectFactory.newInstance(RemapperExtensionHolder.class, parameters);
} else {
holder = objectFactory.newInstance(RemapperExtensionHolder.class, RemapperParameters.None.INSTANCE);
}
holder.getRemapperExtensionClassName().set(remapperExtensionClass.getName());
remapperExtensions.add(holder);
}
@Override
public Provider<String> getMinecraftVersion() {
return getProject().provider(() -> LoomGradleExtension.get(getProject()).getMinecraftProvider().minecraftVersion());
}
@Override
public void silentMojangMappingsLicense() {
this.silentMojangMappingsLicense = true;

View File

@@ -288,6 +288,11 @@ public class LoomGradleExtensionImpl extends LoomGradleExtensionApiImpl implemen
return libraryProcessorFactories;
}
@Override
public ListProperty<RemapperExtensionHolder> getRemapperExtensions() {
return remapperExtensions;
}
@Override
protected <T extends IntermediateMappingsProvider> void configureIntermediateMappingsProviderInternal(T provider) {
provider.getMinecraftVersion().set(getProject().provider(() -> getMinecraftProvider().minecraftVersion()));

View File

@@ -1,76 +0,0 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.extension;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.JsonObject;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPluginExtension;
import net.fabricmc.loom.LoomGradlePlugin;
public class ModVersionParser {
private final Project project;
private String version = null;
public ModVersionParser(Project project) {
this.project = project;
}
public String getModVersion() {
if (version != null) {
return version;
}
File json = locateModJsonFile();
JsonObject jsonObject;
try (var reader = new FileReader(json)) {
jsonObject = LoomGradlePlugin.GSON.fromJson(reader, JsonObject.class);
} catch (IOException e) {
throw new RuntimeException("Failed to read fabric.mod.json file");
}
if (!jsonObject.has("version") || !jsonObject.get("version").isJsonPrimitive()) {
throw new UnsupportedOperationException("Could not find valid version in the fabric.mod.json file");
}
version = jsonObject.get("version").getAsString();
return version;
}
private File locateModJsonFile() {
return project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets()
.getByName("main")
.getResources()
.matching(patternFilterable -> patternFilterable.include("fabric.mod.json"))
.getSingleFile();
}
}

View File

@@ -0,0 +1,128 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.extension;
import javax.inject.Inject;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.commons.Remapper;
import net.fabricmc.loom.api.remapping.RemapperContext;
import net.fabricmc.loom.api.remapping.RemapperExtension;
import net.fabricmc.loom.api.remapping.RemapperParameters;
import net.fabricmc.loom.api.remapping.TinyRemapperExtension;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.api.TrClass;
public abstract class RemapperExtensionHolder {
// Null when RemapperParameters.None.class
private final RemapperParameters remapperParameters;
@Inject
public RemapperExtensionHolder(RemapperParameters remapperParameters) {
this.remapperParameters = remapperParameters;
}
@Input
public abstract Property<String> getRemapperExtensionClassName();
@Nested
@Optional
public RemapperParameters getRemapperParameters() {
return remapperParameters;
}
public void apply(TinyRemapper.Builder tinyRemapperBuilder, String sourceNamespace, String targetNamespace, ObjectFactory objectFactory) {
final RemapperExtension<?> remapperExtension = newInstance(objectFactory);
tinyRemapperBuilder.extraPostApplyVisitor(new RemapperExtensionImpl(remapperExtension, sourceNamespace, targetNamespace));
if (remapperExtension instanceof TinyRemapperExtension tinyRemapperExtension) {
final TinyRemapper.AnalyzeVisitorProvider analyzeVisitorProvider = tinyRemapperExtension.getAnalyzeVisitorProvider();
final TinyRemapper.ApplyVisitorProvider preApplyVisitorProvider = tinyRemapperExtension.getPreApplyVisitor();
final TinyRemapper.ApplyVisitorProvider postApplyVisitorProvider = tinyRemapperExtension.getPostApplyVisitor();
if (analyzeVisitorProvider != null) {
tinyRemapperBuilder.extraAnalyzeVisitor(analyzeVisitorProvider);
}
if (preApplyVisitorProvider != null) {
tinyRemapperBuilder.extraPreApplyVisitor(preApplyVisitorProvider);
}
if (postApplyVisitorProvider != null) {
tinyRemapperBuilder.extraPostApplyVisitor(postApplyVisitorProvider);
}
}
}
private RemapperExtension<?> newInstance(ObjectFactory objectFactory) {
try {
Class<? extends RemapperExtension> remapperExtensionClass = Class.forName(getRemapperExtensionClassName().get())
.asSubclass(RemapperExtension.class);
if (remapperParameters == RemapperParameters.None.INSTANCE) {
return objectFactory.newInstance(remapperExtensionClass);
}
return objectFactory.newInstance(remapperExtensionClass, remapperParameters);
} catch (Exception e) {
throw new RuntimeException("Failed to create remapper extension", e);
}
}
private static final class RemapperExtensionImpl implements TinyRemapper.ApplyVisitorProvider {
private final RemapperExtension<?> remapperExtension;
private final String sourceNamespace;
private final String targetNamespace;
@Nullable
private RemapperContext context;
private RemapperExtensionImpl(RemapperExtension<?> remapperExtension, String sourceNamespace, String targetNamespace) {
this.remapperExtension = remapperExtension;
this.sourceNamespace = sourceNamespace;
this.targetNamespace = targetNamespace;
}
@Override
public ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next) {
if (context == null) {
context = new RemapperContextImpl(cls.getEnvironment().getRemapper(), sourceNamespace, targetNamespace);
}
return remapperExtension.insertVisitor(cls.getName(), context, next);
}
}
private record RemapperContextImpl(Remapper remapper, String sourceNamespace, String targetNamespace) implements RemapperContext {
}
}

View File

@@ -226,7 +226,7 @@ public abstract class AbstractRemapJarTask extends Jar {
final ZipEntryCompression compression = getParameters().getEntryCompression().get();
if (isReproducibleFileOrder || !isPreserveFileTimestamps || compression != ZipEntryCompression.DEFLATED) {
ZipReprocessorUtil.reprocessZip(outputFile.toFile(), isReproducibleFileOrder, isPreserveFileTimestamps, compression);
ZipReprocessorUtil.reprocessZip(outputFile, isReproducibleFileOrder, isPreserveFileTimestamps, compression);
}
}
}

View File

@@ -478,13 +478,15 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
}
for (RemapParams.RefmapData refmapData : getParameters().getMixinData().get()) {
int transformed = ZipUtils.transformJson(JsonObject.class, outputFile, refmapData.mixinConfigs().stream().collect(Collectors.toMap(s -> s, s -> json -> {
if (!json.has("refmap")) {
json.addProperty("refmap", refmapData.refmapName());
}
if (ZipUtils.contains(outputFile, refmapData.refmapName())) {
int transformed = ZipUtils.transformJson(JsonObject.class, outputFile, refmapData.mixinConfigs().stream().collect(Collectors.toMap(s -> s, s -> json -> {
if (!json.has("refmap")) {
json.addProperty("refmap", refmapData.refmapName());
}
return json;
})));
return json;
})));
}
}
}
}

View File

@@ -24,7 +24,6 @@
package net.fabricmc.loom.task;
import java.io.File;
import java.util.Map;
import java.util.Set;
@@ -96,7 +95,7 @@ public abstract class RemapTaskConfiguration implements Runnable {
// Configure the default jar task
getTasks().named(JavaPlugin.JAR_TASK_NAME, AbstractArchiveTask.class).configure(task -> {
task.getArchiveClassifier().convention("dev");
task.getDestinationDirectory().set(new File(getProject().getBuildDir(), "devlibs"));
task.getDestinationDirectory().set(getProject().getLayout().getBuildDirectory().map(directory -> directory.dir("devlibs")));
});
getTasks().named(BasePlugin.ASSEMBLE_TASK_NAME).configure(task -> task.dependsOn(remapJarTask));
@@ -161,7 +160,7 @@ public abstract class RemapTaskConfiguration implements Runnable {
}
sourcesJarTask.getArchiveClassifier().convention("dev-sources");
sourcesJarTask.getDestinationDirectory().set(new File(getProject().getBuildDir(), "devlibs"));
sourcesJarTask.getDestinationDirectory().set(getProject().getLayout().getBuildDirectory().map(directory -> directory.dir("devlibs")));
task.getArchiveClassifier().convention("sources");
task.dependsOn(sourcesJarTask);

View File

@@ -42,12 +42,14 @@ import dev.architectury.tinyremapper.InputTag;
import dev.architectury.tinyremapper.TinyRemapper;
import org.gradle.api.Project;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.SourceSet;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.build.IntermediaryNamespaces;
import net.fabricmc.loom.build.mixin.AnnotationProcessorInvoker;
import net.fabricmc.loom.extension.RemapperExtensionHolder;
import net.fabricmc.loom.task.AbstractRemapJarTask;
import net.fabricmc.loom.util.gradle.GradleUtils;
import net.fabricmc.loom.util.gradle.SourceSetHelper;
@@ -95,7 +97,7 @@ public class TinyRemapperService implements SharedService {
mappings.add(gradleMixinMappingProvider(serviceManager, project.getGradle(), extension.getMappingConfiguration().mappingsIdentifier, from, to));
}
return new TinyRemapperService(mappings, !legacyMixin, kotlinClasspathService, extension.getKnownIndyBsms().get());
return new TinyRemapperService(mappings, !legacyMixin, kotlinClasspathService, extension.getKnownIndyBsms().get(), extension.getRemapperExtensions().get(), from, to, project.getObjects());
});
service.readClasspath(remapJarTask.getClasspath().getFiles().stream().map(File::toPath).filter(Files::exists).toList());
@@ -135,7 +137,7 @@ public class TinyRemapperService implements SharedService {
// Set to true once remapping has started, once set no inputs can be read.
private boolean isRemapping = false;
public TinyRemapperService(List<IMappingProvider> mappings, boolean useMixinExtension, @Nullable KotlinClasspath kotlinClasspath, Set<String> knownIndyBsms) {
private TinyRemapperService(List<IMappingProvider> mappings, boolean useMixinExtension, @Nullable KotlinClasspath kotlinClasspath, Set<String> knownIndyBsms, List<RemapperExtensionHolder> remapperExtensions, String sourceNamespace, String targetNamespace, ObjectFactory objectFactory) {
TinyRemapper.Builder builder = TinyRemapper.newRemapper().withKnownIndyBsm(knownIndyBsms);
for (IMappingProvider provider : mappings) {
@@ -151,6 +153,10 @@ public class TinyRemapperService implements SharedService {
builder.extension(kotlinRemapperClassloader.getTinyRemapperExtension());
}
for (RemapperExtensionHolder holder : remapperExtensions) {
holder.apply(builder, sourceNamespace, targetNamespace, objectFactory);
}
tinyRemapper = builder.build();
}

View File

@@ -80,7 +80,7 @@ public class SourceRemapper {
try {
logger.progress("remapping sources - " + source.getName());
remapSourcesInner(source, destination);
ZipReprocessorUtil.reprocessZip(destination, reproducibleFileOrder, preserveFileTimestamps);
ZipReprocessorUtil.reprocessZip(destination.toPath(), reproducibleFileOrder, preserveFileTimestamps);
// Set the remapped sources creation date to match the sources if we're likely succeeded in making it
destination.setLastModified(source.lastModified());

View File

@@ -24,12 +24,11 @@
package net.fabricmc.loom.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;
@@ -87,16 +86,19 @@ public class ZipReprocessorUtil {
return name1.compareTo(name2);
}
public static void reprocessZip(File file, boolean reproducibleFileOrder, boolean preserveFileTimestamps) throws IOException {
public static void reprocessZip(Path file, boolean reproducibleFileOrder, boolean preserveFileTimestamps) throws IOException {
reprocessZip(file, reproducibleFileOrder, preserveFileTimestamps, ZipEntryCompression.DEFLATED);
}
public static void reprocessZip(File file, boolean reproducibleFileOrder, boolean preserveFileTimestamps, ZipEntryCompression zipEntryCompression) throws IOException {
public static void reprocessZip(Path file, boolean reproducibleFileOrder, boolean preserveFileTimestamps, ZipEntryCompression zipEntryCompression) throws IOException {
if (!reproducibleFileOrder && preserveFileTimestamps) {
return;
}
try (var zipFile = new ZipFile(file)) {
final Path tempFile = file.resolveSibling(file.getFileName() + ".tmp");
try (var zipFile = new ZipFile(file.toFile());
var fileOutputStream = Files.newOutputStream(tempFile)) {
ZipEntry[] entries;
if (reproducibleFileOrder) {
@@ -108,9 +110,7 @@ public class ZipReprocessorUtil {
.toArray(ZipEntry[]::new);
}
final var outZip = new ByteArrayOutputStream(entries.length);
try (var zipOutputStream = new ZipOutputStream(outZip)) {
try (var zipOutputStream = new ZipOutputStream(fileOutputStream)) {
zipOutputStream.setMethod(zipOutputStreamCompressionMethod(zipEntryCompression));
for (ZipEntry entry : entries) {
@@ -125,11 +125,9 @@ public class ZipReprocessorUtil {
copyZipEntry(zipOutputStream, newEntry, zipFile.getInputStream(entry));
}
}
try (var fileOutputStream = new FileOutputStream(file)) {
outZip.writeTo(fileOutputStream);
}
}
Files.move(tempFile, file, StandardCopyOption.REPLACE_EXISTING);
}
/**
@@ -137,15 +135,20 @@ public class ZipReprocessorUtil {
* The new entry is added with a constant time stamp to ensure reproducibility.
* This method should only be used when a reproducible output is required, use {@link ZipUtils#add(Path, String, byte[])} normally.
*/
public static void appendZipEntry(File file, String path, byte[] data) throws IOException {
try (var zipFile = new ZipFile(file)) {
public static void appendZipEntry(Path file, String path, byte[] data) throws IOException {
final Path tempFile = file.resolveSibling(file.getFileName() + ".tmp");
try (var zipFile = new ZipFile(file.toFile());
var fileOutputStream = Files.newOutputStream(tempFile)) {
ZipEntry[] entries = zipFile.stream().toArray(ZipEntry[]::new);
final var outZip = new ByteArrayOutputStream(entries.length);
try (var zipOutputStream = new ZipOutputStream(outZip)) {
try (var zipOutputStream = new ZipOutputStream(fileOutputStream)) {
// Copy existing entries
for (ZipEntry entry : entries) {
if (entry.getName().equals(path)) {
throw new IllegalArgumentException("Zip file (%s) already contains entry (%s)".formatted(file.getFileName().toString(), path));
}
copyZipEntry(zipOutputStream, entry, zipFile.getInputStream(entry));
}
@@ -156,11 +159,9 @@ public class ZipReprocessorUtil {
zipOutputStream.write(data, 0, data.length);
zipOutputStream.closeEntry();
}
try (var fileOutputStream = new FileOutputStream(file)) {
outZip.writeTo(fileOutputStream);
}
}
Files.move(tempFile, file, StandardCopyOption.REPLACE_EXISTING);
}
private static void copyZipEntry(ZipOutputStream zipOutputStream, ZipEntry entry, InputStream inputStream) throws IOException {

View File

@@ -50,6 +50,10 @@ public abstract sealed class FabricModJson permits FabricModJsonV0, FabricModJso
return readString(jsonObject, "id");
}
public String getModVersion() {
return readString(jsonObject, "version");
}
@Nullable
public abstract JsonElement getCustom(String key);