diff options
author | shedaniel <daniel@shedaniel.me> | 2021-12-02 01:04:35 +0800 |
---|---|---|
committer | shedaniel <daniel@shedaniel.me> | 2021-12-02 01:04:35 +0800 |
commit | d3b041a9dbae8a84ce49dfcd1442eb2277c7b32e (patch) | |
tree | e9409547bc7ec7752e910a3883836716a17bd28c /src/main/java | |
parent | 152d2801e46c570db3c0a83ae46ca5364f53ee44 (diff) | |
download | architectury-loom-d3b041a9dbae8a84ce49dfcd1442eb2277c7b32e.tar.gz architectury-loom-d3b041a9dbae8a84ce49dfcd1442eb2277c7b32e.tar.bz2 architectury-loom-d3b041a9dbae8a84ce49dfcd1442eb2277c7b32e.zip |
Fix support for Forge 1.18
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'src/main/java')
6 files changed, 170 insertions, 85 deletions
diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/MinecraftProviderImpl.java b/src/main/java/net/fabricmc/loom/configuration/providers/MinecraftProviderImpl.java index 1967bb72..c8987bfe 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/MinecraftProviderImpl.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/MinecraftProviderImpl.java @@ -52,6 +52,7 @@ import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.MirrorUtil; import net.fabricmc.loom.util.DownloadUtil; import net.fabricmc.loom.util.HashedDownloadUtil; +import net.fabricmc.loom.util.ZipUtils; import net.fabricmc.stitch.merge.JarMerger; public class MinecraftProviderImpl extends DependencyProvider implements MinecraftProvider { @@ -67,6 +68,7 @@ public class MinecraftProviderImpl extends DependencyProvider implements Minecra public File minecraftServerJar; // The extracted server jar from the boostrap, only exists in >=21w39a public File minecraftExtractedServerJar; + private Boolean isNewerThan21w39a; private File minecraftMergedJar; private File versionManifestJson; private File experimentalVersionsJson; @@ -332,6 +334,26 @@ public class MinecraftProviderImpl extends DependencyProvider implements Minecra } } + public File getMinecraftServerJar() { + if (isNewerThan21w39a()) { + try { + return getServerJarToMerge(getProject().getLogger()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return minecraftServerJar; + } + + public boolean isNewerThan21w39a() { + if (isNewerThan21w39a != null) { + return isNewerThan21w39a; + } + + return isNewerThan21w39a = ZipUtils.contains(minecraftServerJar.toPath(), "META-INF/versions.list"); + } + public File getMergedJar() { return minecraftMergedJar; } diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeUserdevProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeUserdevProvider.java index a0d247b5..fecfa4d3 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeUserdevProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeUserdevProvider.java @@ -135,7 +135,6 @@ public class ForgeUserdevProvider extends DependencyProvider { } } - // TODO: Read launch configs from the JSON too // TODO: Should I copy the patches from here as well? // That'd require me to run the "MCP environment" fully up to merging. for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject("runs").entrySet()) { diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/forge/McpConfigProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/forge/McpConfigProvider.java index ae6e0e87..e357a15d 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/forge/McpConfigProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/forge/McpConfigProvider.java @@ -24,20 +24,30 @@ package net.fabricmc.loom.configuration.providers.forge; +import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; import java.io.Reader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; +import java.util.List; import java.util.function.Consumer; +import java.util.jar.Attributes; +import java.util.jar.Manifest; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; import com.google.gson.Gson; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import org.gradle.api.Project; +import org.gradle.api.file.FileCollection; import net.fabricmc.loom.configuration.DependencyProvider; import net.fabricmc.loom.util.Constants; +import net.fabricmc.loom.util.DependencyDownloader; import net.fabricmc.loom.util.ZipUtils; public class McpConfigProvider extends DependencyProvider { @@ -46,6 +56,7 @@ public class McpConfigProvider extends DependencyProvider { private Path mappings; private Boolean official; private String mappingsPath; + private RemapAction remapAction; public McpConfigProvider(Project project) { super(project); @@ -70,6 +81,22 @@ public class McpConfigProvider extends DependencyProvider { official = json.has("official") && json.getAsJsonPrimitive("official").getAsBoolean(); mappingsPath = json.get("data").getAsJsonObject().get("mappings").getAsString(); + + if (json.has("functions")) { + JsonObject functions = json.getAsJsonObject("functions"); + + if (functions.has("rename")) { + remapAction = new ConfigDefinedRemapAction(getProject(), functions.getAsJsonObject("rename")); + } + } + + if (remapAction == null) { + throw new RuntimeException("Could not find remap action, this is probably a version Architectury Loom does not support!"); + } + } + + public RemapAction getRemapAction() { + return remapAction; } private void init(String version) throws IOException { @@ -111,4 +138,95 @@ public class McpConfigProvider extends DependencyProvider { public String getTargetConfig() { return Constants.Configurations.MCP_CONFIG; } + + public interface RemapAction { + FileCollection getClasspath(); + + String getMainClass(); + + List<String> getArgs(Path input, Path output, Path mappings, FileCollection libraries); + } + + public static class ConfigDefinedRemapAction implements RemapAction { + private final Project project; + private final String name; + private final File mainClasspath; + private final FileCollection classpath; + private final List<String> args; + private boolean hasLibraries; + + public ConfigDefinedRemapAction(Project project, JsonObject json) { + this.project = project; + this.name = json.get("version").getAsString(); + this.mainClasspath = DependencyDownloader.download(project, this.name, false, true) + .getSingleFile(); + this.classpath = DependencyDownloader.download(project, this.name, true, true); + this.args = StreamSupport.stream(json.getAsJsonArray("args").spliterator(), false) + .map(JsonElement::getAsString) + .collect(Collectors.toList()); + for (int i = 1; i < this.args.size(); i++) { + if (this.args.get(i).equals("{libraries}")) { + this.args.remove(i); + this.args.remove(i - 1); + this.hasLibraries = true; + break; + } + } + } + + @Override + public FileCollection getClasspath() { + return classpath; + } + + @Override + public String getMainClass() { + try { + byte[] manifestBytes = ZipUtils.unpackNullable(mainClasspath.toPath(), "META-INF/MANIFEST.MF"); + + if (manifestBytes == null) { + throw new RuntimeException("Could not find MANIFEST.MF in " + mainClasspath + "!"); + } + + Manifest manifest = new Manifest(new ByteArrayInputStream(manifestBytes)); + Attributes attributes = manifest.getMainAttributes(); + String value = attributes.getValue(Attributes.Name.MAIN_CLASS); + + if (value == null) { + throw new RuntimeException("Could not find main class in " + mainClasspath + "!"); + } else { + return value; + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public List<String> getArgs(Path input, Path output, Path mappings, FileCollection libraries) { + List<String> args = this.args.stream() + .map(str -> { + return switch (str) { + case "{input}" -> input.toAbsolutePath().toString(); + case "{output}" -> output.toAbsolutePath().toString(); + case "{mappings}" -> mappings.toAbsolutePath().toString(); + default -> str; + }; + }) + .collect(Collectors.toList()); + + if (hasLibraries) { + for (File file : libraries) { + args.add("-e=" + file.getAbsolutePath()); + } + } + + return args; + } + + @Override + public String toString() { + return this.name; + } + } } diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java index 02a5cce4..a74b49d0 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java @@ -87,6 +87,7 @@ import org.objectweb.asm.tree.ClassNode; import net.fabricmc.loom.configuration.DependencyProvider; import net.fabricmc.loom.configuration.providers.MinecraftProviderImpl; +import net.fabricmc.loom.extension.LoomGradleExtensionImpl; import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.DependencyDownloader; import net.fabricmc.loom.util.FileSystemUtil; @@ -338,20 +339,17 @@ public class MinecraftPatchedProvider extends DependencyProvider { private void createSrgJars(Logger logger) throws Exception { MinecraftProviderImpl minecraftProvider = getExtension().getMinecraftProvider(); - String dep = getExtension().isForgeAndOfficial() ? Constants.Dependencies.VIGNETTE + Constants.Dependencies.Versions.VIGNETTE - : Constants.Dependencies.SPECIAL_SOURCE + Constants.Dependencies.Versions.SPECIAL_SOURCE + ":shaded"; - FileCollection classpath = DependencyDownloader.download(getProject(), dep, true, true); - produceSrgJar(getExtension().isForgeAndOfficial(), minecraftProvider.minecraftClientJar.toPath(), minecraftProvider.minecraftServerJar.toPath(), classpath); + produceSrgJar(getExtension().isForgeAndOfficial(), minecraftProvider.minecraftClientJar.toPath(), minecraftProvider.getMinecraftServerJar().toPath()); } - private void produceSrgJar(boolean official, Path clientJar, Path serverJar, FileCollection classpath) throws IOException { + private void produceSrgJar(boolean official, Path clientJar, Path serverJar) throws IOException { Path tmpSrg = getToSrgMappings(); Set<File> mcLibs = getProject().getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES).resolve(); ThreadingUtils.run(() -> { - Files.copy(SpecialSourceExecutor.produceSrgJar(getExtension().isForgeAndNotOfficial(), getProject(), "client", classpath, mcLibs, clientJar, tmpSrg), minecraftClientSrgJar.toPath()); + Files.copy(SpecialSourceExecutor.produceSrgJar(getExtension().getMcpConfigProvider().getRemapAction(), getProject(), "client", mcLibs, clientJar, tmpSrg), minecraftClientSrgJar.toPath()); }, () -> { - Files.copy(SpecialSourceExecutor.produceSrgJar(getExtension().isForgeAndNotOfficial(), getProject(), "server", classpath, mcLibs, serverJar, tmpSrg), minecraftServerSrgJar.toPath()); + Files.copy(SpecialSourceExecutor.produceSrgJar(getExtension().getMcpConfigProvider().getRemapAction(), getProject(), "server", mcLibs, serverJar, tmpSrg), minecraftServerSrgJar.toPath()); }); } @@ -481,8 +479,9 @@ public class MinecraftPatchedProvider extends DependencyProvider { } private void accessTransformForge(Logger logger) throws Exception { + MinecraftProviderImpl minecraftProvider = getExtension().getMinecraftProvider(); List<File> toDelete = new ArrayList<>(); - String atDependency = Constants.Dependencies.ACCESS_TRANSFORMERS + Constants.Dependencies.Versions.ACCESS_TRANSFORMERS; + String atDependency = Constants.Dependencies.ACCESS_TRANSFORMERS + (minecraftProvider.isNewerThan21w39a() ? Constants.Dependencies.Versions.ACCESS_TRANSFORMERS_NEW : Constants.Dependencies.Versions.ACCESS_TRANSFORMERS); FileCollection classpath = DependencyDownloader.download(getProject(), atDependency); Stopwatch stopwatch = Stopwatch.createStarted(); @@ -655,7 +654,7 @@ public class MinecraftPatchedProvider extends DependencyProvider { // Copy resources MinecraftProviderImpl minecraftProvider = getExtension().getMinecraftProvider(); copyNonClassFiles(minecraftProvider.minecraftClientJar, minecraftMergedPatchedSrgJar); - copyNonClassFiles(minecraftProvider.minecraftServerJar, minecraftMergedPatchedSrgJar); + copyNonClassFiles(minecraftProvider.getMinecraftServerJar(), minecraftMergedPatchedSrgJar); } } diff --git a/src/main/java/net/fabricmc/loom/util/Constants.java b/src/main/java/net/fabricmc/loom/util/Constants.java index 65c3a666..8160b361 100644 --- a/src/main/java/net/fabricmc/loom/util/Constants.java +++ b/src/main/java/net/fabricmc/loom/util/Constants.java @@ -107,8 +107,6 @@ public class Constants { public static final String JAVAX_ANNOTATIONS = "com.google.code.findbugs:jsr305:"; // I hate that I have to add these. public static final String FORGE_RUNTIME = "dev.architectury:architectury-loom-runtime:"; public static final String ACCESS_TRANSFORMERS = "net.minecraftforge:accesstransformers:"; - public static final String SPECIAL_SOURCE = "net.md-5:SpecialSource:"; - public static final String VIGNETTE = "net.minecraftforge.lex:vignette:"; private Dependencies() { } @@ -124,8 +122,7 @@ public class Constants { public static final String JAVAX_ANNOTATIONS = "3.0.2"; public static final String FORGE_RUNTIME = "1.1.3"; public static final String ACCESS_TRANSFORMERS = "3.0.1"; - public static final String SPECIAL_SOURCE = "1.8.3"; - public static final String VIGNETTE = "0.2.0.10"; + public static final String ACCESS_TRANSFORMERS_NEW = "8.0.5"; private Versions() { } diff --git a/src/main/java/net/fabricmc/loom/util/srg/SpecialSourceExecutor.java b/src/main/java/net/fabricmc/loom/util/srg/SpecialSourceExecutor.java index 13ffa72a..64ccd6c8 100644 --- a/src/main/java/net/fabricmc/loom/util/srg/SpecialSourceExecutor.java +++ b/src/main/java/net/fabricmc/loom/util/srg/SpecialSourceExecutor.java @@ -30,8 +30,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -44,6 +42,7 @@ import org.gradle.api.logging.LogLevel; import org.gradle.api.logging.configuration.ShowStacktrace; import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.configuration.providers.forge.McpConfigProvider.RemapAction; import net.fabricmc.loom.util.FileSystemUtil; import net.fabricmc.loom.util.ThreadingUtils; @@ -58,7 +57,7 @@ public class SpecialSourceExecutor { return string; } - public static Path produceSrgJar(boolean specialSource, Project project, String side, FileCollection classpath, Set<File> mcLibs, Path officialJar, Path mappings) + public static Path produceSrgJar(RemapAction remapAction, Project project, String side, Set<File> mcLibs, Path officialJar, Path mappings) throws Exception { Set<String> filter = Files.readAllLines(mappings, StandardCharsets.UTF_8).stream() .filter(s -> !s.startsWith("\t")) @@ -105,79 +104,30 @@ public class SpecialSourceExecutor { Files.deleteIfExists(output); stopwatch = Stopwatch.createStarted(); - if (specialSource) { - String[] args = new String[] { - "--in-jar", - stripped.toAbsolutePath().toString(), - "--out-jar", - output.toAbsolutePath().toString(), - "--srg-in", - mappings.toAbsolutePath().toString() - }; - - project.getLogger().lifecycle(":remapping minecraft (SpecialSource, " + side + ", official -> srg)"); - - Path workingDir = tmpDir(); - - project.javaexec(spec -> { - spec.setArgs(Arrays.asList(args)); - spec.setClasspath(classpath); - spec.workingDir(workingDir.toFile()); - spec.getMainClass().set("net.md_5.specialsource.SpecialSource"); - - // if running with INFO or DEBUG logging - if (project.getGradle().getStartParameter().getShowStacktrace() != ShowStacktrace.INTERNAL_EXCEPTIONS - || project.getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) < 0) { - spec.setStandardOutput(System.out); - spec.setErrorOutput(System.err); - } else { - spec.setStandardOutput(NullOutputStream.NULL_OUTPUT_STREAM); - spec.setErrorOutput(NullOutputStream.NULL_OUTPUT_STREAM); - } - }).rethrowFailure().assertNormalExitValue(); - - project.getLogger().lifecycle(":remapped minecraft (SpecialSource, " + side + ", official -> srg) in " + stopwatch.stop()); - } else { - List<String> args = new ArrayList<>(Arrays.asList( - "--jar-in", - stripped.toAbsolutePath().toString(), - "--jar-out", - output.toAbsolutePath().toString(), - "--mapping-format", - "tsrg2", - "--mappings", - mappings.toAbsolutePath().toString(), - "--create-inits", - "--fix-param-annotations" - )); - - for (File file : mcLibs) { - args.add("-e=" + file.getAbsolutePath()); - } + List<String> args = remapAction.getArgs(stripped, output, mappings, project.files(mcLibs)); - project.getLogger().lifecycle(":remapping minecraft (Vignette, " + side + ", official -> mojang)"); + project.getLogger().lifecycle(":remapping minecraft (" + remapAction + ", " + side + ", official -> mojang)"); - Path workingDir = tmpDir(); + Path workingDir = tmpDir(); - project.javaexec(spec -> { - spec.setArgs(args); - spec.setClasspath(classpath); - spec.workingDir(workingDir.toFile()); - spec.getMainClass().set("org.cadixdev.vignette.VignetteMain"); + project.javaexec(spec -> { + spec.setArgs(args); + spec.setClasspath(remapAction.getClasspath()); + spec.workingDir(workingDir.toFile()); + spec.getMainClass().set(remapAction.getMainClass()); - // if running with INFO or DEBUG logging - if (project.getGradle().getStartParameter().getShowStacktrace() != ShowStacktrace.INTERNAL_EXCEPTIONS - || project.getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) < 0) { - spec.setStandardOutput(System.out); - spec.setErrorOutput(System.err); - } else { - spec.setStandardOutput(NullOutputStream.NULL_OUTPUT_STREAM); - spec.setErrorOutput(NullOutputStream.NULL_OUTPUT_STREAM); - } - }).rethrowFailure().assertNormalExitValue(); + // if running with INFO or DEBUG logging + if (project.getGradle().getStartParameter().getShowStacktrace() != ShowStacktrace.INTERNAL_EXCEPTIONS + || project.getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) < 0) { + spec.setStandardOutput(System.out); + spec.setErrorOutput(System.err); + } else { + spec.setStandardOutput(NullOutputStream.NULL_OUTPUT_STREAM); + spec.setErrorOutput(NullOutputStream.NULL_OUTPUT_STREAM); + } + }).rethrowFailure().assertNormalExitValue(); - project.getLogger().lifecycle(":remapped minecraft (Vignette, " + side + ", official -> mojang) in " + stopwatch.stop()); - } + project.getLogger().lifecycle(":remapped minecraft (" + remapAction + ", " + side + ", official -> mojang) in " + stopwatch.stop()); Files.deleteIfExists(stripped); |