PatchProvider: Extract env-specific patch files on demand (#311)

This commit is contained in:
Juuz
2025-11-30 21:49:10 +02:00
committed by GitHub
parent fffef91cef
commit ec2faea53c
2 changed files with 36 additions and 15 deletions

View File

@@ -31,40 +31,61 @@ import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.loom.configuration.DependencyInfo; import net.fabricmc.loom.configuration.DependencyInfo;
import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.FileSystemUtil; import net.fabricmc.loom.util.FileSystemUtil;
public class PatchProvider extends DependencyProvider { public class PatchProvider extends DependencyProvider {
public Path clientPatches; private final Path projectCacheFolder;
public Path serverPatches; private Path installerJar;
private @Nullable Path clientPatches;
private @Nullable Path serverPatches;
public PatchProvider(Project project) { public PatchProvider(Project project) {
super(project); super(project);
this.projectCacheFolder = ForgeProvider.getForgeCache(project);
} }
@Override @Override
public void provide(DependencyInfo dependency) throws Exception { public void provide(DependencyInfo dependency) throws Exception {
init(); init();
installerJar = dependency.resolveFile().orElseThrow(() -> new RuntimeException("Could not resolve Forge installer")).toPath();
}
if (Files.notExists(clientPatches) || Files.notExists(serverPatches) || refreshDeps()) { public Path extractClientPatches() {
getProject().getLogger().info(":extracting forge patches"); if (clientPatches == null) {
clientPatches = projectCacheFolder.resolve("patches-client.lzma");
extractPatches(clientPatches, "client.lzma");
}
Path installerJar = dependency.resolveFile().orElseThrow(() -> new RuntimeException("Could not resolve Forge installer")).toPath(); return clientPatches;
}
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(installerJar, false)) { public Path extractServerPatches() {
Files.copy(fs.getPath("data", "client.lzma"), clientPatches, StandardCopyOption.REPLACE_EXISTING); if (serverPatches == null) {
Files.copy(fs.getPath("data", "server.lzma"), serverPatches, StandardCopyOption.REPLACE_EXISTING); serverPatches = projectCacheFolder.resolve("patches-server.lzma");
} extractPatches(serverPatches, "server.lzma");
}
return serverPatches;
}
private void extractPatches(Path targetPath, String name) {
if (Files.exists(targetPath) && !refreshDeps()) {
// No need to extract
return;
}
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(installerJar, false)) {
Files.copy(fs.getPath("data", name), targetPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new UncheckedIOException(e);
} }
} }
private void init() { private void init() {
final Path projectCacheFolder = ForgeProvider.getForgeCache(getProject());
clientPatches = projectCacheFolder.resolve("patches-client.lzma");
serverPatches = projectCacheFolder.resolve("patches-server.lzma");
try { try {
Files.createDirectories(projectCacheFolder); Files.createDirectories(projectCacheFolder);
} catch (IOException e) { } catch (IOException e) {

View File

@@ -655,8 +655,8 @@ public class MinecraftPatchedProvider {
} }
public enum Type { public enum Type {
CLIENT_ONLY("client", "client", (patch, userdev) -> patch.clientPatches), CLIENT_ONLY("client", "client", (patch, userdev) -> patch.extractClientPatches()),
SERVER_ONLY("server", "server", (patch, userdev) -> patch.serverPatches), SERVER_ONLY("server", "server", (patch, userdev) -> patch.extractServerPatches()),
MERGED("merged", "joined", (patch, userdev) -> userdev.getJoinedPatches()); MERGED("merged", "joined", (patch, userdev) -> userdev.getJoinedPatches());
private final String id; private final String id;