87 lines
4.0 KiB
Java
87 lines
4.0 KiB
Java
import java.io.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.*;
|
|
import java.util.*;
|
|
import java.util.stream.Stream;
|
|
import java.util.zip.*;
|
|
|
|
public final class BuildStandaloneClientJar {
|
|
public static void main(String[] args) throws Exception {
|
|
Path classesDir = Paths.get(args[0]);
|
|
Path resourcesDir = Paths.get(args[1]);
|
|
String thirdPartyPathList = args[2];
|
|
Path outJar = Paths.get(args[3]);
|
|
Files.createDirectories(outJar.getParent());
|
|
|
|
Path tmpJar = outJar.resolveSibling(outJar.getFileName().toString() + ".tmp");
|
|
Set<String> written = new HashSet<>();
|
|
byte[] buf = new byte[32768];
|
|
int classCount = 0, resCount = 0, libCount = 0;
|
|
|
|
try (ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(tmpJar), 65536))) {
|
|
written.add("META-INF/MANIFEST.MF");
|
|
zout.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
|
|
zout.write("Manifest-Version: 1.0\r\nMain-Class: net.minecraft.client.main.Main\r\n\r\n".getBytes(StandardCharsets.UTF_8));
|
|
zout.closeEntry();
|
|
|
|
if (Files.isDirectory(classesDir)) {
|
|
List<Path> clsFiles;
|
|
try (Stream<Path> s = Files.walk(classesDir)) {
|
|
clsFiles = s.filter(p -> Files.isRegularFile(p) && p.toString().endsWith(".class")).sorted().toList();
|
|
}
|
|
for (Path p : clsFiles) {
|
|
classCount++;
|
|
String rel = classesDir.relativize(p).toString().replace('\\', '/');
|
|
if (!written.add(rel)) continue;
|
|
zout.putNextEntry(new ZipEntry(rel));
|
|
Files.copy(p, zout);
|
|
zout.closeEntry();
|
|
}
|
|
}
|
|
|
|
if (Files.isDirectory(resourcesDir)) {
|
|
List<Path> resFiles;
|
|
try (Stream<Path> s = Files.walk(resourcesDir)) {
|
|
resFiles = s.filter(Files::isRegularFile).sorted().toList();
|
|
}
|
|
for (Path p : resFiles) {
|
|
resCount++;
|
|
String rel = resourcesDir.relativize(p).toString().replace('\\', '/');
|
|
if (!written.add(rel)) continue;
|
|
zout.putNextEntry(new ZipEntry(rel));
|
|
Files.copy(p, zout);
|
|
zout.closeEntry();
|
|
}
|
|
}
|
|
|
|
for (String entry : thirdPartyPathList.split(File.pathSeparator)) {
|
|
if (entry == null || entry.trim().isEmpty()) continue;
|
|
Path jarPath = Paths.get(entry.trim());
|
|
if (!Files.isRegularFile(jarPath)) continue;
|
|
try (ZipFile ztp = new ZipFile(jarPath.toFile())) {
|
|
Enumeration<? extends ZipEntry> en = ztp.entries();
|
|
while (en.hasMoreElements()) {
|
|
ZipEntry ze = en.nextElement();
|
|
String nm = ze.getName();
|
|
if (nm.endsWith("/")) continue;
|
|
if (nm.startsWith("META-INF/") && (nm.endsWith(".SF") || nm.endsWith(".RSA") || nm.endsWith(".DSA"))) continue;
|
|
if (!written.add(nm)) continue;
|
|
libCount++;
|
|
ZipEntry ne = new ZipEntry(nm);
|
|
if (ze.getTime() != -1) ne.setTime(ze.getTime());
|
|
zout.putNextEntry(ne);
|
|
try (InputStream in = ztp.getInputStream(ze)) {
|
|
int n;
|
|
while ((n = in.read(buf)) != -1) zout.write(buf, 0, n);
|
|
}
|
|
zout.closeEntry();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Files.move(tmpJar, outJar, StandardCopyOption.REPLACE_EXISTING);
|
|
System.out.println("Built standalone runtime JAR: " + outJar + " (" + Files.size(outJar)
|
|
+ " bytes; " + classCount + " compiled classes, " + resCount + " resources, " + libCount + " bundled library entries)");
|
|
}
|
|
}
|