mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-03-28 04:07:01 -05:00
Split custom RemapJarTask logic into a new class
This commit is contained in:
@@ -166,6 +166,7 @@ spotless {
|
||||
java {
|
||||
licenseHeaderFile(rootProject.file("HEADER")).yearSeparator("-")
|
||||
targetExclude("**/loom/util/DownloadUtil.java", "**/loom/util/FileSystemUtil.java")
|
||||
targetExclude("**/dev/architectury/**")
|
||||
}
|
||||
|
||||
groovy {
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package dev.architectury.loom.extensions;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
import org.cadixdev.at.AccessTransformSet;
|
||||
import org.cadixdev.at.io.AccessTransformFormats;
|
||||
import org.cadixdev.lorenz.MappingSet;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.provider.SetProperty;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.fabricmc.loom.task.service.MappingsService;
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
import net.fabricmc.loom.util.FileSystemUtil;
|
||||
import net.fabricmc.loom.util.LfWriter;
|
||||
import net.fabricmc.loom.util.aw2at.Aw2At;
|
||||
import net.fabricmc.loom.util.service.UnsafeWorkQueueHelper;
|
||||
import net.fabricmc.lorenztiny.TinyMappingsReader;
|
||||
|
||||
public final class ModBuildExtensions {
|
||||
public static Set<String> readMixinConfigsFromManifest(File jarFile) {
|
||||
try (JarFile jar = new JarFile(jarFile)) {
|
||||
@Nullable Manifest manifest = jar.getManifest();
|
||||
|
||||
if (manifest != null) {
|
||||
Attributes attributes = manifest.getMainAttributes();
|
||||
String mixinConfigs = attributes.getValue(Constants.Forge.MIXIN_CONFIGS_MANIFEST_KEY);
|
||||
|
||||
if (mixinConfigs != null) {
|
||||
return Set.of(mixinConfigs.split(","));
|
||||
}
|
||||
}
|
||||
|
||||
return Set.of();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Could not read mixin configs from jar " + jarFile.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void convertAwToAt(SetProperty<String> atAccessWidenersProperty, Path outputFile, Property<String> mappingBuildServiceUuid) throws IOException {
|
||||
if (!atAccessWidenersProperty.isPresent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> atAccessWideners = atAccessWidenersProperty.get();
|
||||
|
||||
if (atAccessWideners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AccessTransformSet at = AccessTransformSet.create();
|
||||
|
||||
try (FileSystemUtil.Delegate fileSystem = FileSystemUtil.getJarFileSystem(outputFile, false)) {
|
||||
FileSystem fs = fileSystem.get();
|
||||
Path atPath = fs.getPath(Constants.Forge.ACCESS_TRANSFORMER_PATH);
|
||||
|
||||
if (Files.exists(atPath)) {
|
||||
throw new FileAlreadyExistsException("Jar " + outputFile + " 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 (BufferedReader reader = Files.newBufferedReader(awPath, StandardCharsets.UTF_8)) {
|
||||
at.merge(Aw2At.toAccessTransformSet(reader));
|
||||
}
|
||||
|
||||
Files.delete(awPath);
|
||||
}
|
||||
|
||||
MappingsService service = UnsafeWorkQueueHelper.get(mappingBuildServiceUuid, MappingsService.class);
|
||||
|
||||
try (TinyMappingsReader reader = new TinyMappingsReader(service.getMemoryMappingTree(), service.getFromNamespace(), service.getToNamespace())) {
|
||||
MappingSet mappingSet = reader.read();
|
||||
at = at.remap(mappingSet);
|
||||
}
|
||||
|
||||
try (Writer writer = new LfWriter(Files.newBufferedWriter(atPath))) {
|
||||
AccessTransformFormats.FML.write(writer, at);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,19 +24,12 @@
|
||||
|
||||
package net.fabricmc.loom.task;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Serializable;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -47,9 +40,6 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
@@ -59,11 +49,9 @@ import javax.inject.Inject;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import dev.architectury.loom.extensions.ModBuildExtensions;
|
||||
import dev.architectury.tinyremapper.OutputConsumerPath;
|
||||
import dev.architectury.tinyremapper.TinyRemapper;
|
||||
import org.cadixdev.at.AccessTransformSet;
|
||||
import org.cadixdev.at.io.AccessTransformFormats;
|
||||
import org.cadixdev.lorenz.MappingSet;
|
||||
import org.gradle.api.artifacts.Configuration;
|
||||
import org.gradle.api.file.ConfigurableFileCollection;
|
||||
import org.gradle.api.file.FileCollection;
|
||||
@@ -79,7 +67,6 @@ import org.gradle.api.tasks.Internal;
|
||||
import org.gradle.api.tasks.SourceSet;
|
||||
import org.gradle.api.tasks.TaskAction;
|
||||
import org.gradle.api.tasks.TaskDependency;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -100,16 +87,12 @@ import net.fabricmc.loom.task.service.MappingsService;
|
||||
import net.fabricmc.loom.task.service.TinyRemapperService;
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
import net.fabricmc.loom.util.ExceptionUtil;
|
||||
import net.fabricmc.loom.util.FileSystemUtil;
|
||||
import net.fabricmc.loom.util.LfWriter;
|
||||
import net.fabricmc.loom.util.ModPlatform;
|
||||
import net.fabricmc.loom.util.ModUtils;
|
||||
import net.fabricmc.loom.util.Pair;
|
||||
import net.fabricmc.loom.util.SidedClassVisitor;
|
||||
import net.fabricmc.loom.util.ZipUtils;
|
||||
import net.fabricmc.loom.util.aw2at.Aw2At;
|
||||
import net.fabricmc.loom.util.service.UnsafeWorkQueueHelper;
|
||||
import net.fabricmc.lorenztiny.TinyMappingsReader;
|
||||
|
||||
public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
@InputFiles
|
||||
@@ -273,7 +256,7 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
}
|
||||
|
||||
if (allMixinConfigs == null && getReadMixinConfigsFromManifest().get()) {
|
||||
allMixinConfigs = readMixinConfigsFromManifest();
|
||||
allMixinConfigs = ModBuildExtensions.readMixinConfigsFromManifest(getInputFile().get().getAsFile());
|
||||
}
|
||||
|
||||
if (allMixinConfigs == null) {
|
||||
@@ -309,27 +292,6 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<String> readMixinConfigsFromManifest() {
|
||||
File inputJar = getInputFile().get().getAsFile();
|
||||
|
||||
try (JarFile jar = new JarFile(inputJar)) {
|
||||
@Nullable Manifest manifest = jar.getManifest();
|
||||
|
||||
if (manifest != null) {
|
||||
Attributes attributes = manifest.getMainAttributes();
|
||||
String mixinConfigs = attributes.getValue(Constants.Forge.MIXIN_CONFIGS_MANIFEST_KEY);
|
||||
|
||||
if (mixinConfigs != null) {
|
||||
return Set.of(mixinConfigs.split(","));
|
||||
}
|
||||
}
|
||||
|
||||
return Set.of();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Could not read mixin configs from input jar", e);
|
||||
}
|
||||
}
|
||||
|
||||
public interface RemapParams extends AbstractRemapParams {
|
||||
ConfigurableFileCollection getNestedJars();
|
||||
|
||||
@@ -381,7 +343,7 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
|
||||
addRefmaps();
|
||||
addNestedJars();
|
||||
convertAwToAt();
|
||||
ModBuildExtensions.convertAwToAt(getParameters().getAtAccessWideners(), outputFile, getParameters().getMappingBuildServiceUuid());
|
||||
|
||||
if (getParameters().getPlatform().get() != ModPlatform.FORGE) {
|
||||
modifyJarManifest();
|
||||
@@ -455,55 +417,6 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
ZipUtils.replace(outputFile, accessWidenerFile.path(), remapped);
|
||||
}
|
||||
|
||||
private void convertAwToAt() throws IOException {
|
||||
if (!this.getParameters().getAtAccessWideners().isPresent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> atAccessWideners = this.getParameters().getAtAccessWideners().get();
|
||||
|
||||
if (atAccessWideners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AccessTransformSet at = AccessTransformSet.create();
|
||||
File jar = outputFile.toFile();
|
||||
|
||||
try (FileSystemUtil.Delegate 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 (BufferedReader reader = Files.newBufferedReader(awPath, StandardCharsets.UTF_8)) {
|
||||
at.merge(Aw2At.toAccessTransformSet(reader));
|
||||
}
|
||||
|
||||
Files.delete(awPath);
|
||||
}
|
||||
|
||||
MappingsService service = UnsafeWorkQueueHelper.get(getParameters().getMappingBuildServiceUuid(), MappingsService.class);
|
||||
|
||||
try (TinyMappingsReader reader = new TinyMappingsReader(service.getMemoryMappingTree(), service.getFromNamespace(), service.getToNamespace())) {
|
||||
MappingSet mappingSet = reader.read();
|
||||
at = at.remap(mappingSet);
|
||||
}
|
||||
|
||||
try (Writer writer = new LfWriter(Files.newBufferedWriter(atPath))) {
|
||||
AccessTransformFormats.FML.write(writer, at);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] remapAccessWidener(byte[] input) {
|
||||
int version = AccessWidenerReader.readVersion(input);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user