Skip to content

Commit

Permalink
Actually unbundle the server jar. Oops
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Jun 20, 2024
1 parent 2efea20 commit c5ae507
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
3 changes: 2 additions & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ minivan {

//for local testing of minivan; makes it not cache anything
//refreshDependencies = true
explainHashes = true

//explainHashes = true

accessWideners "./botania_xplat.accesswidener"
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/agency/highlysuspect/minivan/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package agency.highlysuspect.minivan;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class Util {
public static void cpy(InputStream in, OutputStream out) throws IOException {
byte[] shuttle = new byte[4096];
int read;
while((read = in.read(shuttle)) != -1) out.write(shuttle, 0, read);
}

public static void cpy(InputStream in, Path out) throws IOException {
try(OutputStream outS = new BufferedOutputStream(Files.newOutputStream(out))) {
cpy(in, outS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ public Result getMinecraft() throws Exception {

log.info("found vanilla dependency: {}", lib.getArtifactName());
}

//Remap client and server using official names
String minecraftPrefix = "minecraft-" + MinivanPlugin.filenameSafe(version);

//Unbundle server
Unbundler serverUnbundler = new Unbundler(project, vanillaJars.server, minecraftPrefix + "-server-unbundled{HASH}.jar");
serverUnbundler.dependsOn(vanillaJarFetcher);
Path unbundledServer = serverUnbundler.unbundle();

//Remap client and server using official names
RemapperPrg clientMapper = new RemapperPrg(project, vanillaJars.client, vanillaJars.clientMappings, minecraftPrefix + "-client-mapped{HASH}.jar");
RemapperPrg serverMapper = new RemapperPrg(project, vanillaJars.server, vanillaJars.serverMappings, minecraftPrefix + "-server-mapped{HASH}.jar");
RemapperPrg serverMapper = new RemapperPrg(project, unbundledServer, vanillaJars.serverMappings, minecraftPrefix + "-server-mapped{HASH}.jar");
clientMapper.dependsOn(vanillaJarFetcher);
serverMapper.dependsOn(vanillaJarFetcher);
serverMapper.dependsOn(serverUnbundler);

Path clientMapped = clientMapper.remap();
Path serverMapped = serverMapper.remap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ protected Path getOrCreate(Path p, ThrowyConsumer<Path> creator) throws Exceptio
}

protected Path getOrCreate(String p, ThrowyConsumer<Path> creator) throws Exception {
return getOrCreate(cacheDir().resolve(p), creator);
return getOrCreate(resolve(p), creator);
}

protected Path resolve(String p) throws IOException {
return cacheDir().resolve(p);
}

protected interface ThrowyConsumer<T> {
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/agency/highlysuspect/minivan/prov/Unbundler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package agency.highlysuspect.minivan.prov;


import agency.highlysuspect.minivan.Util;
import org.gradle.api.Project;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Unbundler extends MiniProvider {
public Unbundler(Project project, Path bundledJar, String unbundledJarName) {
super(project);
this.bundledJar = bundledJar;
this.unbundledJarName = unbundledJarName;

//cachebusting from before the unbundler existed
props.set("unbundlerInPipeline", "true");
}

final Path bundledJar;
final String unbundledJarName;

public Path unbundle() throws Exception {
Path unbundled = getOrCreate(subst(unbundledJarName), unbundledJar -> {
log.lifecycle("Unbundling {} to {}...", bundledJar, unbundledJar);

try(ZipFile zf = new ZipFile(bundledJar.toFile())) {
Optional<? extends ZipEntry> innerJar = zf.stream()
.filter(e -> e.getName().startsWith("META-INF/versions/") && e.getName().endsWith(".jar"))
.findFirst();

if(innerJar.isPresent()) {
log.lifecycle("\\-> Found inner jar: {}", innerJar.get());
InputStream in = zf.getInputStream(innerJar.get());
Util.cpy(in, unbundledJar);
} else {
log.lifecycle("\\-> Did not find any inner jar to unbundle.");
//No bundling
//TODO dont copuy the jar if it doesn't need to be unbundled
Files.copy(bundledJar, unbundledJar);
}
}
});

log.info("unbundled: {}", unbundled);

return unbundled;
}
}

0 comments on commit c5ae507

Please sign in to comment.