From 543d0a3d107edbead60d27aec9b0e4a740e64465 Mon Sep 17 00:00:00 2001 From: Space Walker <48224626+SpaceWalkerRS@users.noreply.github.com> Date: Sun, 29 Dec 2024 16:57:45 +0100 Subject: [PATCH] create backup jars for legacy merged mapped minecraft providers (#1230) * create backup jars for legacy merged mapped mc providers * only merge jars if remapping occurred * Cleanup + add logger * Fixes --------- Co-authored-by: modmuss50 --- .../AbstractMappedMinecraftProvider.java | 71 +++++++++++++------ .../mapped/IntermediaryMinecraftProvider.java | 33 +++++++-- .../mapped/NamedMinecraftProvider.java | 32 +++++++-- .../ProcessedNamedMinecraftProvider.java | 10 ++- 4 files changed, 111 insertions(+), 35 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java index e55c9928d..bf3e0791c 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java @@ -37,6 +37,8 @@ import java.util.function.Function; import org.gradle.api.Project; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.api.mappings.layered.MappingsNamespace; @@ -56,6 +58,8 @@ import net.fabricmc.tinyremapper.TinyRemapper; public abstract class AbstractMappedMinecraftProvider implements MappedMinecraftProvider.ProviderImpl { + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMappedMinecraftProvider.class); + protected final M minecraftProvider; private final Project project; protected final LoomGradleExtension extension; @@ -68,8 +72,18 @@ public AbstractMappedMinecraftProvider(Project project, M minecraftProvider) { public abstract MappingsNamespace getTargetNamespace(); + /** + * @return A list of jars that should be remapped + */ public abstract List getRemappedJars(); + /** + * @return A list of output jars that this provider generates + */ + public List getOutputJars() { + return getRemappedJars(); + } + // Returns a list of MinecraftJar.Type's that this provider exports to be used as a dependency public List getDependencyTypes() { return Collections.emptyList(); @@ -85,7 +99,7 @@ public List provide(ProvideContext context) throws Exception { throw new IllegalStateException("No remapped jars provided"); } - if (!areOutputsValid(remappedJars) || context.refreshOutputs() || !hasBackupJars(minecraftJars)) { + if (shouldRefreshOutputs(context)) { try { remapInputs(remappedJars, context.configContext()); createBackupJars(minecraftJars); @@ -116,16 +130,6 @@ public static Path getBackupJarPath(MinecraftJar minecraftJar) { return outputJarPath.resolveSibling(outputJarPath.getFileName() + ".backup"); } - protected boolean hasBackupJars(List minecraftJars) { - for (MinecraftJar minecraftJar : minecraftJars) { - if (!Files.exists(getBackupJarPath(minecraftJar))) { - return false; - } - } - - return true; - } - protected void createBackupJars(List minecraftJars) throws IOException { for (MinecraftJar minecraftJar : minecraftJars) { Files.copy(minecraftJar.getPath(), getBackupJarPath(minecraftJar), StandardCopyOption.REPLACE_EXISTING); @@ -193,14 +197,34 @@ protected String getDependencyNotation(MinecraftJar.Type type) { return "net.minecraft:%s:%s".formatted(getName(type), getVersion()); } - private boolean areOutputsValid(List remappedJars) { - for (RemappedJars remappedJar : remappedJars) { - if (!getMavenHelper(remappedJar.type()).exists(null)) { - return false; + protected boolean shouldRefreshOutputs(ProvideContext context) { + if (context.refreshOutputs()) { + LOGGER.info("Refreshing outputs for mapped jar, as refresh outputs was requested"); + return true; + } + + final List outputJars = getOutputJars(); + + if (outputJars.isEmpty()) { + throw new IllegalStateException("No output jars provided"); + } + + for (OutputJar outputJar : outputJars) { + if (!getMavenHelper(outputJar.type()).exists(null)) { + LOGGER.info("Refreshing outputs for mapped jar, as {} does not exist", outputJar.outputJar()); + return true; } } - return true; + for (OutputJar outputJar : outputJars) { + if (!Files.exists(getBackupJarPath(outputJar.outputJar()))) { + LOGGER.info("Refreshing outputs for mapped jar, as backup jar does not exist for {}", outputJar.outputJar()); + return true; + } + } + + LOGGER.debug("All outputs are up to date"); + return false; } private void remapInputs(List remappedJars, ConfigContext configContext) throws IOException { @@ -274,7 +298,15 @@ public M getMinecraftProvider() { return minecraftProvider; } - public record RemappedJars(Path inputJar, MinecraftJar outputJar, MappingsNamespace sourceNamespace, Path... remapClasspath) { + public sealed interface OutputJar permits RemappedJars, SimpleOutputJar { + MinecraftJar outputJar(); + + default MinecraftJar.Type type() { + return outputJar().getType(); + } + } + + public record RemappedJars(Path inputJar, MinecraftJar outputJar, MappingsNamespace sourceNamespace, Path... remapClasspath) implements OutputJar { public Path outputJarPath() { return outputJar().getPath(); } @@ -282,9 +314,8 @@ public Path outputJarPath() { public String name() { return outputJar().getName(); } + } - public MinecraftJar.Type type() { - return outputJar().getType(); - } + public record SimpleOutputJar(MinecraftJar outputJar) implements OutputJar { } } diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/IntermediaryMinecraftProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/IntermediaryMinecraftProvider.java index 70a1424ea..3d3045d32 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/IntermediaryMinecraftProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/IntermediaryMinecraftProvider.java @@ -78,18 +78,30 @@ public LegacyMergedImpl(Project project, LegacyMergedMinecraftProvider minecraft @Override public List provide(ProvideContext context) throws Exception { + final List minecraftJars = List.of(getMergedJar()); + + // this check must be done before the client and server impls are provided + // because the merging only needs to happen if the remapping step is run + final boolean refreshOutputs = client.shouldRefreshOutputs(context) + || server.shouldRefreshOutputs(context) + || this.shouldRefreshOutputs(context); + // Map the client and server jars separately server.provide(context); client.provide(context); - // then merge them - MergedMinecraftProvider.mergeJars( - client.getEnvOnlyJar().toFile(), - server.getEnvOnlyJar().toFile(), - getMergedJar().toFile() - ); + if (refreshOutputs) { + // then merge them + MergedMinecraftProvider.mergeJars( + client.getEnvOnlyJar().toFile(), + server.getEnvOnlyJar().toFile(), + getMergedJar().toFile() + ); - return List.of(getMergedJar()); + createBackupJars(minecraftJars); + } + + return minecraftJars; } @Override @@ -98,6 +110,13 @@ public List getRemappedJars() { throw new UnsupportedOperationException("LegacyMergedImpl does not support getRemappedJars"); } + @Override + public List getOutputJars() { + return List.of( + new SimpleOutputJar(getMergedJar()) + ); + } + @Override public List getDependencyTypes() { return List.of(MinecraftJar.Type.MERGED); diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/NamedMinecraftProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/NamedMinecraftProvider.java index f2753609b..02077a983 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/NamedMinecraftProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/NamedMinecraftProvider.java @@ -85,17 +85,28 @@ public LegacyMergedImpl(Project project, LegacyMergedMinecraftProvider minecraft @Override public List provide(ProvideContext context) throws Exception { final ProvideContext childContext = context.withApplyDependencies(false); + final List minecraftJars = List.of(getMergedJar()); + + // this check must be done before the client and server impls are provided + // because the merging only needs to happen if the remapping step is run + final boolean refreshOutputs = client.shouldRefreshOutputs(childContext) + || server.shouldRefreshOutputs(childContext) + || this.shouldRefreshOutputs(childContext); // Map the client and server jars separately server.provide(childContext); client.provide(childContext); - // then merge them - MergedMinecraftProvider.mergeJars( - client.getEnvOnlyJar().toFile(), - server.getEnvOnlyJar().toFile(), - getMergedJar().toFile() - ); + if (refreshOutputs) { + // then merge them + MergedMinecraftProvider.mergeJars( + client.getEnvOnlyJar().toFile(), + server.getEnvOnlyJar().toFile(), + getMergedJar().toFile() + ); + + createBackupJars(minecraftJars); + } getMavenHelper(MinecraftJar.Type.MERGED).savePom(); @@ -106,7 +117,7 @@ public List provide(ProvideContext context) throws Exception { ); } - return List.of(getMergedJar()); + return minecraftJars; } @Override @@ -115,6 +126,13 @@ public List getRemappedJars() { throw new UnsupportedOperationException("LegacyMergedImpl does not support getRemappedJars"); } + @Override + public List getOutputJars() { + return List.of( + new SimpleOutputJar(getMergedJar()) + ); + } + @Override public List getDependencyTypes() { return List.of(MinecraftJar.Type.MERGED); diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/ProcessedNamedMinecraftProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/ProcessedNamedMinecraftProvider.java index 9a5f28c1d..1987f9e7a 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/ProcessedNamedMinecraftProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/ProcessedNamedMinecraftProvider.java @@ -65,7 +65,7 @@ public List provide(ProvideContext context) throws Exception { parentMinecraftProvider.provide(context.withApplyDependencies(false)); - boolean requiresProcessing = context.refreshOutputs() || !hasBackupJars(minecraftJars) || parentMinecraftJars.stream() + boolean requiresProcessing = shouldRefreshOutputs(context) || parentMinecraftJars.stream() .map(this::getProcessedPath) .anyMatch(jarProcessorManager::requiresProcessingJar); @@ -81,6 +81,14 @@ public List provide(ProvideContext context) throws Exception { return List.copyOf(minecraftJarOutputMap.values()); } + @Override + public List getOutputJars() { + return parentMinecraftProvider.getMinecraftJars().stream() + .map(this::getProcessedJar) + .map(SimpleOutputJar::new) + .toList(); + } + @Override public MavenScope getMavenScope() { return MavenScope.LOCAL;