Skip to content

Commit

Permalink
create backup jars for legacy merged mapped minecraft providers (#1230)
Browse files Browse the repository at this point in the history
* create backup jars for legacy merged mapped mc providers

* only merge jars if remapping occurred

* Cleanup + add logger

* Fixes

---------

Co-authored-by: modmuss50 <[email protected]>
  • Loading branch information
SpaceWalkerRS and modmuss50 authored Dec 29, 2024
1 parent 44ca4b8 commit 543d0a3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -56,6 +58,8 @@
import net.fabricmc.tinyremapper.TinyRemapper;

public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvider> implements MappedMinecraftProvider.ProviderImpl {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMappedMinecraftProvider.class);

protected final M minecraftProvider;
private final Project project;
protected final LoomGradleExtension extension;
Expand All @@ -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<RemappedJars> getRemappedJars();

/**
* @return A list of output jars that this provider generates
*/
public List<? extends OutputJar> getOutputJars() {
return getRemappedJars();
}

// 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();
Expand All @@ -85,7 +99,7 @@ public List<MinecraftJar> 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);
Expand Down Expand Up @@ -116,16 +130,6 @@ public static Path getBackupJarPath(MinecraftJar minecraftJar) {
return outputJarPath.resolveSibling(outputJarPath.getFileName() + ".backup");
}

protected boolean hasBackupJars(List<MinecraftJar> minecraftJars) {
for (MinecraftJar minecraftJar : minecraftJars) {
if (!Files.exists(getBackupJarPath(minecraftJar))) {
return false;
}
}

return true;
}

protected void createBackupJars(List<MinecraftJar> minecraftJars) throws IOException {
for (MinecraftJar minecraftJar : minecraftJars) {
Files.copy(minecraftJar.getPath(), getBackupJarPath(minecraftJar), StandardCopyOption.REPLACE_EXISTING);
Expand Down Expand Up @@ -193,14 +197,34 @@ 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.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<? extends OutputJar> 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> remappedJars, ConfigContext configContext) throws IOException {
Expand Down Expand Up @@ -274,17 +298,24 @@ 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();
}

public String name() {
return outputJar().getName();
}
}

public MinecraftJar.Type type() {
return outputJar().getType();
}
public record SimpleOutputJar(MinecraftJar outputJar) implements OutputJar {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,30 @@ public LegacyMergedImpl(Project project, LegacyMergedMinecraftProvider minecraft

@Override
public List<MinecraftJar> provide(ProvideContext context) throws Exception {
final List<MinecraftJar> 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
Expand All @@ -98,6 +110,13 @@ public List<RemappedJars> getRemappedJars() {
throw new UnsupportedOperationException("LegacyMergedImpl does not support getRemappedJars");
}

@Override
public List<? extends OutputJar> getOutputJars() {
return List.of(
new SimpleOutputJar(getMergedJar())
);
}

@Override
public List<MinecraftJar.Type> getDependencyTypes() {
return List.of(MinecraftJar.Type.MERGED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,28 @@ public LegacyMergedImpl(Project project, LegacyMergedMinecraftProvider minecraft
@Override
public List<MinecraftJar> provide(ProvideContext context) throws Exception {
final ProvideContext childContext = context.withApplyDependencies(false);
final List<MinecraftJar> 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();

Expand All @@ -106,7 +117,7 @@ public List<MinecraftJar> provide(ProvideContext context) throws Exception {
);
}

return List.of(getMergedJar());
return minecraftJars;
}

@Override
Expand All @@ -115,6 +126,13 @@ public List<RemappedJars> getRemappedJars() {
throw new UnsupportedOperationException("LegacyMergedImpl does not support getRemappedJars");
}

@Override
public List<? extends OutputJar> getOutputJars() {
return List.of(
new SimpleOutputJar(getMergedJar())
);
}

@Override
public List<MinecraftJar.Type> getDependencyTypes() {
return List.of(MinecraftJar.Type.MERGED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public List<MinecraftJar> 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);

Expand All @@ -81,6 +81,14 @@ public List<MinecraftJar> provide(ProvideContext context) throws Exception {
return List.copyOf(minecraftJarOutputMap.values());
}

@Override
public List<? extends OutputJar> getOutputJars() {
return parentMinecraftProvider.getMinecraftJars().stream()
.map(this::getProcessedJar)
.map(SimpleOutputJar::new)
.toList();
}

@Override
public MavenScope getMavenScope() {
return MavenScope.LOCAL;
Expand Down

0 comments on commit 543d0a3

Please sign in to comment.