mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-04-02 05:27:43 -05:00
Convert AWs on Forge to ATs in RemapJarTask (#24)
* Aw2At * Hook Aw2At up to remapJar * Checkstyle * Checkstyle, part II * Now it's Spotless' turn * Remap the resulting AT * you see, this is why mojmap is bad * Inject(method = "<init>") * Use file systems because ZipUtil is bad * Make it false by default * Add integ tests for aw2at * spotless please * fix year
This commit is contained in:
@@ -35,7 +35,6 @@ import org.gradle.api.plugins.PluginAware;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import net.fabricmc.loom.extension.LoomFiles;
|
||||
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
import net.fabricmc.loom.util.MirrorUtil;
|
||||
|
||||
|
||||
51
src/main/java/net/fabricmc/loom/api/ForgeExtensionAPI.java
Normal file
51
src/main/java/net/fabricmc/loom/api/ForgeExtensionAPI.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.api;
|
||||
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.provider.SetProperty;
|
||||
|
||||
/**
|
||||
* This is the forge extension api available exposed to build scripts.
|
||||
*/
|
||||
// TODO: Move other forge-related configuration here
|
||||
public interface ForgeExtensionAPI {
|
||||
/**
|
||||
* If true, {@linkplain LoomGradleExtensionAPI#getAccessWidenerPath() the project access widener file}
|
||||
* will be remapped to an access transformer file if set.
|
||||
*
|
||||
* @return the property
|
||||
*/
|
||||
Property<Boolean> getConvertAccessWideners();
|
||||
|
||||
/**
|
||||
* A set of additional access widener files that will be converted to access transformers
|
||||
* {@linkplain #getConvertAccessWideners() if enabled}. The files are specified as paths in jar files
|
||||
* (e.g. {@code path/to/my_aw.accesswidener}).
|
||||
*
|
||||
* @return the property
|
||||
*/
|
||||
SetProperty<String> getExtraAccessWideners();
|
||||
}
|
||||
@@ -248,4 +248,16 @@ public interface LoomGradleExtensionAPI {
|
||||
void setUseFabricMixin(boolean useFabricMixin);
|
||||
|
||||
List<Consumer<RunConfig>> getSettingsPostEdit();
|
||||
|
||||
/**
|
||||
* Gets the Forge extension used to configure Forge details.
|
||||
* Note that (for now) some Forge configuration is instead in this interface -
|
||||
* this is due to change in the future.
|
||||
*
|
||||
* @return the Forge extension
|
||||
* @throws UnsupportedOperationException if running on another platform
|
||||
*/
|
||||
ForgeExtensionAPI getForge();
|
||||
|
||||
void forge(Action<ForgeExtensionAPI> action);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.gradle.api.Project;
|
||||
import org.gradle.api.Task;
|
||||
import org.gradle.api.UnknownTaskException;
|
||||
import org.gradle.api.plugins.JavaPlugin;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
|
||||
import org.gradle.api.tasks.bundling.Jar;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
@@ -45,6 +46,7 @@ import net.fabricmc.loom.task.RemapAllSourcesTask;
|
||||
import net.fabricmc.loom.task.RemapJarTask;
|
||||
import net.fabricmc.loom.task.RemapSourcesJarTask;
|
||||
import net.fabricmc.loom.util.SourceRemapper;
|
||||
import net.fabricmc.loom.util.aw2at.Aw2At;
|
||||
|
||||
public class RemapConfiguration {
|
||||
private static final String DEFAULT_JAR_TASK_NAME = JavaPlugin.JAR_TASK_NAME;
|
||||
@@ -89,6 +91,16 @@ public class RemapConfiguration {
|
||||
remapJarTask.getRemapAccessWidener().set(true);
|
||||
|
||||
project.getArtifacts().add("archives", remapJarTask);
|
||||
|
||||
if (extension.isForge()) {
|
||||
Property<Boolean> convertAws = extension.getForge().getConvertAccessWideners();
|
||||
convertAws.finalizeValue();
|
||||
|
||||
if (convertAws.get()) {
|
||||
Aw2At.setup(project, remapJarTask);
|
||||
remapJarTask.getRemapAccessWidener().set(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
remapJarTask.dependsOn(jarTask);
|
||||
|
||||
@@ -280,7 +280,7 @@ public final class RunConfigSettings implements Named {
|
||||
public void client() {
|
||||
startFirstThread();
|
||||
environment("client");
|
||||
defaultMainClass(getExtension().isForge() ? Constants.ForgeUserDev.LAUNCH_TESTING : Constants.Knot.KNOT_CLIENT);
|
||||
defaultMainClass(getExtension().isForge() ? Constants.Forge.LAUNCH_TESTING : Constants.Knot.KNOT_CLIENT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -289,7 +289,7 @@ public final class RunConfigSettings implements Named {
|
||||
public void server() {
|
||||
programArg("nogui");
|
||||
environment("server");
|
||||
defaultMainClass(getExtension().isForge() ? Constants.ForgeUserDev.LAUNCH_TESTING : Constants.Knot.KNOT_SERVER);
|
||||
defaultMainClass(getExtension().isForge() ? Constants.Forge.LAUNCH_TESTING : Constants.Knot.KNOT_SERVER);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -297,7 +297,7 @@ public final class RunConfigSettings implements Named {
|
||||
*/
|
||||
public void data() {
|
||||
environment("data");
|
||||
defaultMainClass(getExtension().isForge() ? Constants.ForgeUserDev.LAUNCH_TESTING : Constants.Knot.KNOT_SERVER);
|
||||
defaultMainClass(getExtension().isForge() ? Constants.Forge.LAUNCH_TESTING : Constants.Knot.KNOT_SERVER);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -152,7 +152,7 @@ public class MinecraftPatchedProvider extends DependencyProvider {
|
||||
SourceSet main = getProject().getConvention().findPlugin(JavaPluginConvention.class).getSourceSets().getByName("main");
|
||||
|
||||
for (File srcDir : main.getResources().getSrcDirs()) {
|
||||
File projectAt = new File(srcDir, "META-INF/accesstransformer.cfg");
|
||||
File projectAt = new File(srcDir, Constants.Forge.ACCESS_TRANSFORMER_PATH);
|
||||
|
||||
if (projectAt.exists()) {
|
||||
this.projectAts.add(projectAt);
|
||||
@@ -373,8 +373,8 @@ public class MinecraftPatchedProvider extends DependencyProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private static void visitMojmap(MappingVisitor visitor, LoomGradleExtension extension) {
|
||||
GradleMappingContext context = new GradleMappingContext(extension.getForgeProvider().getProject(), "tmp-mojmap");
|
||||
private static void visitMojmap(MappingVisitor visitor, Project project) {
|
||||
GradleMappingContext context = new GradleMappingContext(project, "tmp-mojmap");
|
||||
|
||||
try {
|
||||
FileUtils.deleteDirectory(context.workingDirectory("/"));
|
||||
@@ -391,12 +391,12 @@ public class MinecraftPatchedProvider extends DependencyProvider {
|
||||
}
|
||||
}
|
||||
|
||||
public static Path getMojmapTsrg(LoomGradleExtension extension) throws IOException {
|
||||
public static Path getMojmapTsrg(Project project, LoomGradleExtension extension) throws IOException {
|
||||
Path path = extension.getMinecraftProvider().dir("forge").toPath().resolve("mojmap.tsrg");
|
||||
|
||||
if (Files.notExists(path)) {
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
|
||||
Tsrg2Utils.writeTsrg(visitor -> visitMojmap(visitor, extension),
|
||||
Tsrg2Utils.writeTsrg(visitor -> visitMojmap(visitor, project),
|
||||
MappingNamespace.NAMED.stringValue(), false, writer);
|
||||
}
|
||||
}
|
||||
@@ -404,12 +404,12 @@ public class MinecraftPatchedProvider extends DependencyProvider {
|
||||
return path;
|
||||
}
|
||||
|
||||
public static Path getMojmapTsrg2(LoomGradleExtension extension) throws IOException {
|
||||
public static Path getMojmapTsrg2(Project project, LoomGradleExtension extension) throws IOException {
|
||||
Path path = extension.getMinecraftProvider().dir("forge").toPath().resolve("mojmap.tsrg2");
|
||||
|
||||
if (Files.notExists(path)) {
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
|
||||
Tsrg2Utils.writeTsrg2(visitor -> visitMojmap(visitor, extension), writer);
|
||||
Tsrg2Utils.writeTsrg2(visitor -> visitMojmap(visitor, project), writer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,7 +426,7 @@ public class MinecraftPatchedProvider extends DependencyProvider {
|
||||
"--left",
|
||||
getExtension().getMcpConfigProvider().getMappings().toAbsolutePath().toString(),
|
||||
"--right",
|
||||
getMojmapTsrg(getExtension()).toAbsolutePath().toString(),
|
||||
getMojmapTsrg(getProject(), getExtension()).toAbsolutePath().toString(),
|
||||
"--classes",
|
||||
"--output",
|
||||
out.toAbsolutePath().toString()
|
||||
@@ -585,7 +585,7 @@ public class MinecraftPatchedProvider extends DependencyProvider {
|
||||
|
||||
for (File jar : ImmutableList.of(getForgeJar(), getForgeUserdevJar(), minecraftMergedPatchedSrgJar)) {
|
||||
try (FileSystemUtil.FileSystemDelegate fs = FileSystemUtil.getJarFileSystem(jar, false)) {
|
||||
Path atPath = fs.get().getPath("META-INF/accesstransformer.cfg");
|
||||
Path atPath = fs.get().getPath(Constants.Forge.ACCESS_TRANSFORMER_PATH);
|
||||
|
||||
if (Files.exists(atPath)) {
|
||||
File tmpFile = File.createTempFile("at-conf", ".cfg");
|
||||
|
||||
@@ -116,6 +116,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
|
||||
if (getExtension().shouldGenerateSrgTiny()) {
|
||||
return MappingsCache.INSTANCE.get(tinyMappingsWithSrg);
|
||||
}
|
||||
|
||||
throw new UnsupportedOperationException("Not running with Forge support / Tiny srg support.");
|
||||
}
|
||||
|
||||
@@ -230,7 +231,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
|
||||
protected Path getMojmapSrgFileIfPossible() {
|
||||
try {
|
||||
LoomGradleExtension extension = getExtension();
|
||||
return MinecraftPatchedProvider.getMojmapTsrg2(extension);
|
||||
return MinecraftPatchedProvider.getMojmapTsrg2(getProject(), extension);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
@@ -453,7 +454,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
|
||||
"intermediary", "official");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not merge mappings from " + intermediaryMappings.toString()
|
||||
+ " with mappings from " + yarnMappings, e);
|
||||
+ " with mappings from " + yarnMappings, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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 javax.inject.Inject;
|
||||
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.provider.SetProperty;
|
||||
|
||||
import net.fabricmc.loom.api.ForgeExtensionAPI;
|
||||
|
||||
public class ForgeExtensionImpl implements ForgeExtensionAPI {
|
||||
private final Property<Boolean> convertAccessWideners;
|
||||
private final SetProperty<String> extraAccessWideners;
|
||||
|
||||
@Inject
|
||||
public ForgeExtensionImpl(Project project) {
|
||||
convertAccessWideners = project.getObjects().property(Boolean.class).convention(false);
|
||||
extraAccessWideners = project.getObjects().setProperty(String.class).empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Property<Boolean> getConvertAccessWideners() {
|
||||
return convertAccessWideners;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SetProperty<String> getExtraAccessWideners() {
|
||||
return extraAccessWideners;
|
||||
}
|
||||
}
|
||||
@@ -43,11 +43,12 @@ import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.Dependency;
|
||||
import org.gradle.api.file.ConfigurableFileCollection;
|
||||
import org.gradle.api.file.RegularFileProperty;
|
||||
import org.gradle.api.plugins.JavaPluginConvention;
|
||||
import org.gradle.api.provider.ListProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.plugins.JavaPluginConvention;
|
||||
import org.gradle.api.tasks.SourceSet;
|
||||
|
||||
import net.fabricmc.loom.api.ForgeExtensionAPI;
|
||||
import net.fabricmc.loom.api.LoomGradleExtensionAPI;
|
||||
import net.fabricmc.loom.api.MixinApExtensionAPI;
|
||||
import net.fabricmc.loom.api.decompilers.LoomDecompiler;
|
||||
@@ -257,10 +258,7 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
|
||||
@SuppressWarnings("Convert2Lambda")
|
||||
@Override
|
||||
public void localMods(Action<SourceSetConsumer> action) {
|
||||
if (!isForge()) {
|
||||
throw new UnsupportedOperationException("Not running with Forge support.");
|
||||
}
|
||||
|
||||
ModPlatform.assertPlatform(this, ModPlatform.FORGE);
|
||||
action.execute(new SourceSetConsumer() {
|
||||
@Override
|
||||
public void add(Object... sourceSets) {
|
||||
@@ -283,10 +281,7 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
|
||||
@SuppressWarnings("Convert2Lambda")
|
||||
@Override
|
||||
public void dataGen(Action<DataGenConsumer> action) {
|
||||
if (!isForge()) {
|
||||
throw new UnsupportedOperationException("Not running with Forge support.");
|
||||
}
|
||||
|
||||
ModPlatform.assertPlatform(this, ModPlatform.FORGE);
|
||||
action.execute(new DataGenConsumer() {
|
||||
@Override
|
||||
public void mod(String... modIds) {
|
||||
@@ -339,6 +334,11 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
|
||||
return settingsPostEdit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forge(Action<ForgeExtensionAPI> action) {
|
||||
action.execute(getForge());
|
||||
}
|
||||
|
||||
// This is here to ensure that LoomGradleExtensionApiImpl compiles without any unimplemented methods
|
||||
private final class EnsureCompile extends LoomGradleExtensionApiImpl {
|
||||
private EnsureCompile() {
|
||||
@@ -380,5 +380,10 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
|
||||
protected String getMinecraftVersion() {
|
||||
throw new RuntimeException("Yeah... something is really wrong");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForgeExtensionAPI getForge() {
|
||||
throw new RuntimeException("Yeah... something is really wrong");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.google.common.base.Suppliers;
|
||||
import org.cadixdev.lorenz.MappingSet;
|
||||
import org.cadixdev.mercury.Mercury;
|
||||
import org.gradle.api.NamedDomainObjectProvider;
|
||||
@@ -41,15 +42,18 @@ import org.gradle.api.artifacts.Configuration;
|
||||
import org.gradle.api.file.ConfigurableFileCollection;
|
||||
|
||||
import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.api.ForgeExtensionAPI;
|
||||
import net.fabricmc.loom.configuration.InstallerData;
|
||||
import net.fabricmc.loom.configuration.LoomDependencyManager;
|
||||
import net.fabricmc.loom.configuration.processors.JarProcessorManager;
|
||||
import net.fabricmc.loom.util.ModPlatform;
|
||||
|
||||
public class LoomGradleExtensionImpl extends LoomGradleExtensionApiImpl implements LoomGradleExtension {
|
||||
private final Project project;
|
||||
private final MixinApExtension mixinApExtension;
|
||||
private final LoomFiles loomFiles;
|
||||
private final ConfigurableFileCollection unmappedMods;
|
||||
private final Supplier<ForgeExtensionAPI> forgeExtension;
|
||||
|
||||
private final Set<File> mixinMappings = Collections.synchronizedSet(new HashSet<>());
|
||||
private final MappingSet[] srcMappingCache = new MappingSet[2];
|
||||
@@ -67,6 +71,7 @@ public class LoomGradleExtensionImpl extends LoomGradleExtensionApiImpl implemen
|
||||
this.mixinApExtension = project.getObjects().newInstance(MixinApExtensionImpl.class, project);
|
||||
this.loomFiles = files;
|
||||
this.unmappedMods = project.files();
|
||||
this.forgeExtension = Suppliers.memoize(() -> isForge() ? project.getObjects().newInstance(ForgeExtensionImpl.class, project) : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -175,4 +180,10 @@ public class LoomGradleExtensionImpl extends LoomGradleExtensionApiImpl implemen
|
||||
protected String getMinecraftVersion() {
|
||||
return getMinecraftProvider().minecraftVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForgeExtensionAPI getForge() {
|
||||
ModPlatform.assertPlatform(this, ModPlatform.FORGE);
|
||||
return forgeExtension.get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import org.gradle.api.provider.ListProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.tasks.SourceSet;
|
||||
|
||||
import net.fabricmc.loom.api.ForgeExtensionAPI;
|
||||
import net.fabricmc.loom.api.LoomGradleExtensionAPI;
|
||||
import net.fabricmc.loom.api.MixinApExtensionAPI;
|
||||
import net.fabricmc.loom.api.decompilers.LoomDecompiler;
|
||||
@@ -273,4 +274,16 @@ public class MinecraftGradleExtension implements LoomGradleExtensionAPI {
|
||||
reportDeprecation();
|
||||
return parent.getSettingsPostEdit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForgeExtensionAPI getForge() {
|
||||
reportDeprecation();
|
||||
return parent.getForge();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forge(Action<ForgeExtensionAPI> action) {
|
||||
reportDeprecation();
|
||||
parent.forge(action);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,14 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
@@ -61,12 +64,15 @@ import dev.architectury.refmapremapper.remapper.SimpleReferenceRemapper;
|
||||
import dev.architectury.tinyremapper.IMappingProvider;
|
||||
import dev.architectury.tinyremapper.TinyRemapper;
|
||||
import dev.architectury.tinyremapper.TinyUtils;
|
||||
import org.cadixdev.at.AccessTransformSet;
|
||||
import org.cadixdev.at.io.AccessTransformFormats;
|
||||
import org.gradle.api.Action;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.Configuration;
|
||||
import org.gradle.api.file.FileCollection;
|
||||
import org.gradle.api.file.RegularFileProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.provider.SetProperty;
|
||||
import org.gradle.api.tasks.Input;
|
||||
import org.gradle.api.tasks.InputFile;
|
||||
import org.gradle.api.tasks.TaskAction;
|
||||
@@ -90,9 +96,12 @@ import net.fabricmc.loom.configuration.JarManifestConfiguration;
|
||||
import net.fabricmc.loom.configuration.accesswidener.AccessWidenerJarProcessor;
|
||||
import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
import net.fabricmc.loom.util.FileSystemUtil;
|
||||
import net.fabricmc.loom.util.SourceRemapper;
|
||||
import net.fabricmc.loom.util.TinyRemapperMappingsHelper;
|
||||
import net.fabricmc.loom.util.ZipReprocessorUtil;
|
||||
import net.fabricmc.loom.util.aw2at.Aw2At;
|
||||
import net.fabricmc.lorenztiny.TinyMappingsReader;
|
||||
import net.fabricmc.mapping.tree.ClassDef;
|
||||
import net.fabricmc.mapping.tree.FieldDef;
|
||||
import net.fabricmc.mapping.tree.MethodDef;
|
||||
@@ -109,6 +118,7 @@ public class RemapJarTask extends Jar {
|
||||
private final List<Action<TinyRemapper.Builder>> remapOptions = new ArrayList<>();
|
||||
private final Property<String> fromM;
|
||||
private final Property<String> toM;
|
||||
private final SetProperty<String> atAccessWideners;
|
||||
public JarRemapper jarRemapper;
|
||||
private FileCollection classpath;
|
||||
private final Set<Object> nestedPaths = new LinkedHashSet<>();
|
||||
@@ -121,6 +131,7 @@ public class RemapJarTask extends Jar {
|
||||
remapAccessWidener = getProject().getObjects().property(Boolean.class);
|
||||
fromM = getProject().getObjects().property(String.class);
|
||||
toM = getProject().getObjects().property(String.class);
|
||||
atAccessWideners = getProject().getObjects().setProperty(String.class).empty();
|
||||
fromM.set("named");
|
||||
toM.set(SourceRemapper.intermediary(getProject()));
|
||||
// false by default, I have no idea why I have to do it for this property and not the other one
|
||||
@@ -142,6 +153,8 @@ public class RemapJarTask extends Jar {
|
||||
if (singleRemap) {
|
||||
jarRemapper.remap(getProject());
|
||||
}
|
||||
|
||||
convertAwToAt();
|
||||
}
|
||||
|
||||
private ReferenceRemapper createReferenceRemapper(LoomGradleExtension extension, String from, String to) throws IOException {
|
||||
@@ -442,6 +455,53 @@ public class RemapJarTask extends Jar {
|
||||
.toArray(Path[]::new);
|
||||
}
|
||||
|
||||
private void convertAwToAt() throws IOException {
|
||||
if (!this.atAccessWideners.isPresent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> atAccessWideners = this.atAccessWideners.get();
|
||||
|
||||
if (atAccessWideners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AccessTransformSet at = AccessTransformSet.create();
|
||||
File jar = getArchiveFile().get().getAsFile();
|
||||
|
||||
try (FileSystemUtil.FileSystemDelegate fileSystem = FileSystemUtil.getJarFileSystem(jar, false)) {
|
||||
FileSystem fs = fileSystem.get();
|
||||
Path atPath = fs.getPath(Constants.Forge.ACCESS_TRANSFORMER_PATH);
|
||||
|
||||
if (Files.exists(atPath)) {
|
||||
throw new FileAlreadyExistsException("Jar " + jar + " already contains an access transformer - cannot convert AWs!");
|
||||
}
|
||||
|
||||
for (String aw : atAccessWideners) {
|
||||
Path awPath = fs.getPath(aw);
|
||||
|
||||
if (Files.notExists(awPath)) {
|
||||
throw new NoSuchFileException("Could not find AW '" + aw + "' to convert into AT!");
|
||||
}
|
||||
|
||||
try (InputStream in = Files.newInputStream(awPath)) {
|
||||
at.merge(Aw2At.toAccessTransformSet(in));
|
||||
}
|
||||
|
||||
Files.delete(awPath);
|
||||
}
|
||||
|
||||
LoomGradleExtension extension = LoomGradleExtension.get(getProject());
|
||||
TinyTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
|
||||
TinyMappingsReader reader = new TinyMappingsReader(mappings, fromM.get(), toM.get());
|
||||
at = at.remap(reader.read());
|
||||
|
||||
try (Writer writer = Files.newBufferedWriter(atPath)) {
|
||||
AccessTransformFormats.FML.write(writer, at);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@InputFile
|
||||
public RegularFileProperty getInput() {
|
||||
return input;
|
||||
@@ -462,6 +522,19 @@ public class RemapJarTask extends Jar {
|
||||
return remapAccessWidener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the jar paths to the access wideners that will be converted to ATs for Forge runtime.
|
||||
* If you specify multiple files, they will be merged into one.
|
||||
*
|
||||
* <p>The specified files will be converted and removed from the final jar.
|
||||
*
|
||||
* @return the property containing access widener paths in the final jar
|
||||
*/
|
||||
@Input
|
||||
public SetProperty<String> getAtAccessWideners() {
|
||||
return atAccessWideners;
|
||||
}
|
||||
|
||||
public void remapOptions(Action<TinyRemapper.Builder> action) {
|
||||
this.remapOptions.add(action);
|
||||
}
|
||||
|
||||
@@ -149,10 +149,11 @@ public class Constants {
|
||||
}
|
||||
}
|
||||
|
||||
public static final class ForgeUserDev {
|
||||
public static final class Forge {
|
||||
public static final String LAUNCH_TESTING = "net.minecraftforge.userdev.LaunchTesting";
|
||||
public static final String ACCESS_TRANSFORMER_PATH = "META-INF/accesstransformer.cfg";
|
||||
|
||||
private ForgeUserDev() {
|
||||
private Forge() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,31 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
@@ -24,7 +24,29 @@
|
||||
|
||||
package net.fabricmc.loom.util;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.gradle.api.GradleException;
|
||||
import org.gradle.api.Project;
|
||||
|
||||
import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.api.LoomGradleExtensionAPI;
|
||||
|
||||
public enum ModPlatform {
|
||||
FABRIC,
|
||||
FORGE,
|
||||
FORGE;
|
||||
|
||||
public static void assertPlatform(Project project, ModPlatform platform) {
|
||||
assertPlatform(LoomGradleExtension.get(project), platform);
|
||||
}
|
||||
|
||||
public static void assertPlatform(LoomGradleExtensionAPI extension, ModPlatform platform) {
|
||||
extension.getPlatform().finalizeValue();
|
||||
|
||||
if (extension.getPlatform().get() != platform) {
|
||||
String msg = "Loom is not running on %s.%nYou can switch to it by adding 'loom.platform = %s' to your gradle.properties";
|
||||
String name = platform.name().toLowerCase(Locale.ROOT);
|
||||
throw new GradleException(String.format(msg, name, name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
122
src/main/java/net/fabricmc/loom/util/aw2at/Aw2At.java
Normal file
122
src/main/java/net/fabricmc/loom/util/aw2at/Aw2At.java
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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.util.aw2at;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.cadixdev.at.AccessChange;
|
||||
import org.cadixdev.at.AccessTransform;
|
||||
import org.cadixdev.at.AccessTransformSet;
|
||||
import org.cadixdev.at.ModifierChange;
|
||||
import org.cadixdev.bombe.type.signature.MethodSignature;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.tasks.SourceSet;
|
||||
import org.gradle.api.tasks.SourceSetContainer;
|
||||
|
||||
import net.fabricmc.accesswidener.AccessWidenerReader;
|
||||
import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.task.RemapJarTask;
|
||||
|
||||
/**
|
||||
* Converts AW files to AT files.
|
||||
*
|
||||
* @author Juuz
|
||||
*/
|
||||
public final class Aw2At {
|
||||
public static void setup(Project project, RemapJarTask remapJar) {
|
||||
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
||||
|
||||
if (extension.getAccessWidenerPath().isPresent()) {
|
||||
// Find the relative AW file name
|
||||
String awName = null;
|
||||
Path awPath = extension.getAccessWidenerPath().get().getAsFile().toPath();
|
||||
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
|
||||
SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
|
||||
boolean found = false;
|
||||
|
||||
for (File srcDir : main.getResources().getSrcDirs()) {
|
||||
Path srcDirPath = srcDir.toPath().toAbsolutePath();
|
||||
|
||||
if (awPath.startsWith(srcDirPath)) {
|
||||
awName = srcDirPath.relativize(awPath).toString().replace(File.separator, "/");
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
awName = awPath.getFileName().toString();
|
||||
}
|
||||
|
||||
remapJar.getAtAccessWideners().add(awName);
|
||||
}
|
||||
|
||||
remapJar.getAtAccessWideners().addAll(extension.getForge().getExtraAccessWideners());
|
||||
}
|
||||
|
||||
public static AccessTransformSet toAccessTransformSet(InputStream in) throws IOException {
|
||||
AccessTransformSet atSet = AccessTransformSet.create();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
|
||||
new AccessWidenerReader(new AccessWidenerReader.Visitor() {
|
||||
@Override
|
||||
public void visitClass(String name, AccessWidenerReader.AccessType access) {
|
||||
atSet.getOrCreateClass(name).merge(toAt(access));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMethod(String owner, String name, String descriptor, AccessWidenerReader.AccessType access) {
|
||||
atSet.getOrCreateClass(owner).mergeMethod(MethodSignature.of(name, descriptor), toAt(access));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitField(String owner, String name, String descriptor, AccessWidenerReader.AccessType access) {
|
||||
atSet.getOrCreateClass(owner).mergeField(name, toAt(access));
|
||||
}
|
||||
}).read(reader);
|
||||
}
|
||||
|
||||
return atSet;
|
||||
}
|
||||
|
||||
private static AccessTransform toAt(AccessWidenerReader.AccessType access) {
|
||||
return switch (access) {
|
||||
// FIXME: This behaviour doesn't match what the actual AW does for methods.
|
||||
// - accessible makes the method final if it was private
|
||||
// - extendable makes the method protected if it was (package-)private
|
||||
// Neither of these can be achieved with Forge ATs without using bytecode analysis.
|
||||
// However, this might be good enough for us. (The effects only apply in prod.)
|
||||
case ACCESSIBLE -> AccessTransform.of(AccessChange.PUBLIC);
|
||||
case EXTENDABLE -> AccessTransform.of(AccessChange.PUBLIC, ModifierChange.REMOVE);
|
||||
case MUTABLE -> AccessTransform.of(ModifierChange.REMOVE);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ import java.util.function.UnaryOperator;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.gradle.api.logging.Logger;
|
||||
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
import net.fabricmc.loom.util.function.CollectionUtil;
|
||||
import net.fabricmc.mapping.tree.TinyTree;
|
||||
|
||||
@@ -51,7 +52,7 @@ import net.fabricmc.mapping.tree.TinyTree;
|
||||
public final class AtRemapper {
|
||||
public static void remap(Logger logger, Path jar, TinyTree mappings) throws IOException {
|
||||
try (FileSystem fs = FileSystems.newFileSystem(URI.create("jar:" + jar.toUri()), ImmutableMap.of("create", false))) {
|
||||
Path atPath = fs.getPath("META-INF/accesstransformer.cfg");
|
||||
Path atPath = fs.getPath(Constants.Forge.ACCESS_TRANSFORMER_PATH);
|
||||
|
||||
if (Files.exists(atPath)) {
|
||||
String atContent = Files.readString(atPath);
|
||||
|
||||
@@ -1,3 +1,27 @@
|
||||
/*
|
||||
* 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.util.srg;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
Reference in New Issue
Block a user