2026-09-06 18:22:00 +03:00
|
|
|
package online.astradev.packs;
|
|
|
|
|
|
2026-09-06 18:28:23 +03:00
|
|
|
import java.io.BufferedOutputStream;
|
2026-09-06 19:32:43 +03:00
|
|
|
import java.io.File;
|
2026-09-06 18:28:23 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
2026-09-06 18:22:00 +03:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2026-09-06 18:28:23 +03:00
|
|
|
import java.nio.file.Files;
|
2026-09-06 19:20:55 +03:00
|
|
|
import java.nio.file.LinkOption;
|
2026-09-06 18:28:23 +03:00
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
|
|
import java.security.DigestInputStream;
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.util.HexFormat;
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
import java.util.zip.ZipOutputStream;
|
2026-09-06 18:22:00 +03:00
|
|
|
|
2026-09-06 18:36:18 +03:00
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
|
|
2026-09-06 19:20:55 +03:00
|
|
|
/** Archives the shared content directory, keeping the current and one previous ZIP. */
|
2026-09-06 18:22:00 +03:00
|
|
|
public final class PackBuilder {
|
|
|
|
|
public record Pack(Path path, String sha1, int files, long bytes) {}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private final PackConfig config;
|
2026-09-06 18:28:23 +03:00
|
|
|
|
|
|
|
|
public PackBuilder(PackConfig config) {
|
|
|
|
|
this.config = config;
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
|
|
|
|
|
public Pack build(Path root, String resourceVersion) throws IOException {
|
2026-09-06 19:20:55 +03:00
|
|
|
Path contents = config.contents(root);
|
2026-09-06 19:32:43 +03:00
|
|
|
if (!Files.isDirectory(contents)) {
|
|
|
|
|
throw new IOException("Resource directory is missing: " + contents);
|
|
|
|
|
}
|
|
|
|
|
for (Path parent = contents; parent != null; parent = parent.getParent()) {
|
|
|
|
|
if (Files.isSymbolicLink(parent)) {
|
|
|
|
|
throw new IOException("Symbolic links are not supported: " + parent);
|
|
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
Path output = config.storage(root).resolve("output");
|
|
|
|
|
Files.createDirectories(output);
|
|
|
|
|
Path temporary = Files.createTempFile(output, "build-", ".tmp");
|
|
|
|
|
try {
|
|
|
|
|
int files = writeArchive(contents, temporary, resourceVersion);
|
2026-09-06 19:32:43 +03:00
|
|
|
String sha1 = sha1(temporary);
|
2026-09-06 19:20:55 +03:00
|
|
|
Path destination = output.resolve(sha1 + ".zip");
|
|
|
|
|
boolean created = !Files.exists(destination);
|
|
|
|
|
if (created) {
|
|
|
|
|
Files.move(temporary, destination, StandardCopyOption.ATOMIC_MOVE);
|
2026-09-06 19:32:43 +03:00
|
|
|
} else if (!sha1(destination).equals(sha1)) {
|
2026-09-06 19:20:55 +03:00
|
|
|
throw new IOException("Published ZIP was modified: " + destination);
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
Pack pack = new Pack(destination, sha1, files, Files.size(destination));
|
|
|
|
|
try {
|
|
|
|
|
saveCurrent(output, pack);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
if (created) {
|
|
|
|
|
try {
|
|
|
|
|
Files.deleteIfExists(destination);
|
|
|
|
|
} catch (IOException cleanupError) {
|
|
|
|
|
e.addSuppressed(cleanupError);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
return pack;
|
|
|
|
|
} finally {
|
|
|
|
|
Files.deleteIfExists(temporary);
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 19:20:55 +03:00
|
|
|
private int writeArchive(Path contents, Path target, String resourceVersion) throws IOException {
|
|
|
|
|
int files = 0;
|
|
|
|
|
long totalBytes = 0;
|
|
|
|
|
try (var paths = Files.walk(contents);
|
|
|
|
|
var zip = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(target)))) {
|
2026-09-06 18:36:18 +03:00
|
|
|
for (Path file : paths.sorted().toList()) {
|
|
|
|
|
if (Files.isSymbolicLink(file)) {
|
2026-09-06 19:20:55 +03:00
|
|
|
throw new IOException("Symbolic links are not supported: " + file);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
if (!Files.isRegularFile(file)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-09-06 19:32:43 +03:00
|
|
|
String name = contents.relativize(file).toString().replace(File.separatorChar, '/');
|
2026-09-06 19:20:55 +03:00
|
|
|
if (!name.startsWith("assets/") && !name.equals("pack.png")) {
|
|
|
|
|
continue;
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 19:32:43 +03:00
|
|
|
if (name.contains("\\") || name.contains(":")) {
|
|
|
|
|
throw new IOException("Invalid resource name: " + name);
|
|
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
if (++files > config.build.maxFiles) {
|
|
|
|
|
throw new IOException("Too many resources");
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 19:20:55 +03:00
|
|
|
ZipEntry entry = new ZipEntry(name);
|
|
|
|
|
entry.setTime(0);
|
|
|
|
|
zip.putNextEntry(entry);
|
2026-09-06 19:32:43 +03:00
|
|
|
long limit = Math.min(config.build.maxFileMiB * 1024L * 1024L,
|
|
|
|
|
config.build.maxTotalMiB * 1024L * 1024L - totalBytes);
|
|
|
|
|
totalBytes += copyResource(file, zip, limit);
|
2026-09-06 19:20:55 +03:00
|
|
|
zip.closeEntry();
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
ZipEntry metadata = new ZipEntry("pack.mcmeta");
|
|
|
|
|
metadata.setTime(0);
|
|
|
|
|
zip.putNextEntry(metadata);
|
|
|
|
|
zip.write(metadata(resourceVersion));
|
|
|
|
|
zip.closeEntry();
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
return files + 1;
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
private static long copyResource(Path file, OutputStream output, long limit) throws IOException {
|
|
|
|
|
byte[] buffer = new byte[8192];
|
|
|
|
|
long size = 0;
|
|
|
|
|
try (var input = Files.newInputStream(file)) {
|
|
|
|
|
int read;
|
|
|
|
|
while ((read = input.read(buffer)) != -1) {
|
|
|
|
|
size += read;
|
|
|
|
|
if (size > limit) {
|
|
|
|
|
throw new IOException("Resource exceeds configured size limit: " + file);
|
|
|
|
|
}
|
|
|
|
|
output.write(buffer, 0, read);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-06 19:20:55 +03:00
|
|
|
private byte[] metadata(String resourceVersion) {
|
|
|
|
|
var version = JsonParser.parseString(resourceVersion);
|
2026-09-06 18:36:18 +03:00
|
|
|
JsonObject pack = new JsonObject();
|
|
|
|
|
pack.add("min_format", version);
|
|
|
|
|
pack.add("max_format", version.deepCopy());
|
|
|
|
|
pack.addProperty("description", config.build.description);
|
|
|
|
|
JsonObject metadata = new JsonObject();
|
|
|
|
|
metadata.add("pack", pack);
|
2026-09-06 19:20:55 +03:00
|
|
|
return PackConfig.JSON.toJson(metadata).getBytes(StandardCharsets.UTF_8);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-06 19:20:55 +03:00
|
|
|
private static void saveCurrent(Path output, Pack pack) throws IOException {
|
|
|
|
|
Path manifest = output.resolve("current.json");
|
|
|
|
|
String previous = null;
|
|
|
|
|
if (Files.exists(manifest)) {
|
|
|
|
|
JsonObject old = readManifest(manifest);
|
|
|
|
|
String current = old.get("sha1").getAsString();
|
|
|
|
|
if (!current.equals(pack.sha1())) {
|
|
|
|
|
previous = current;
|
|
|
|
|
} else if (old.has("previousSha1")) {
|
|
|
|
|
previous = old.get("previousSha1").getAsString();
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
}
|
|
|
|
|
JsonObject info = new JsonObject();
|
|
|
|
|
info.addProperty("sha1", pack.sha1());
|
|
|
|
|
info.addProperty("files", pack.files());
|
|
|
|
|
if (previous != null) {
|
|
|
|
|
info.addProperty("previousSha1", previous);
|
|
|
|
|
}
|
|
|
|
|
// Only remove generated hash-named ZIPs, never user content or unrelated files.
|
|
|
|
|
try (var paths = Files.list(output)) {
|
|
|
|
|
for (Path path : paths.toList()) {
|
|
|
|
|
String name = path.getFileName().toString();
|
|
|
|
|
if (name.matches("[a-f0-9]{40}\\.zip") && !name.equals(pack.sha1() + ".zip")
|
|
|
|
|
&& (previous == null || !name.equals(previous + ".zip"))
|
|
|
|
|
&& Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS)) {
|
|
|
|
|
Files.delete(path);
|
|
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
PackFiles.atomicWrite(manifest, PackConfig.JSON.toJson(info).getBytes(StandardCharsets.UTF_8));
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-06 19:20:55 +03:00
|
|
|
public static Pack latest(Path root, PackConfig config) throws IOException {
|
2026-09-06 18:36:18 +03:00
|
|
|
Path output = config.storage(root).resolve("output");
|
2026-09-06 19:20:55 +03:00
|
|
|
JsonObject info = readManifest(output.resolve("current.json"));
|
|
|
|
|
String sha1 = info.get("sha1").getAsString();
|
|
|
|
|
Path path = output.resolve(sha1 + ".zip");
|
2026-09-06 19:32:43 +03:00
|
|
|
if (!sha1(path).equals(sha1)) {
|
2026-09-06 19:20:55 +03:00
|
|
|
throw new IOException("Stored resource pack hash mismatch");
|
|
|
|
|
}
|
|
|
|
|
return new Pack(path, sha1, info.get("files").getAsInt(), Files.size(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static JsonObject readManifest(Path path) throws IOException {
|
2026-09-06 18:36:18 +03:00
|
|
|
try {
|
2026-09-06 19:20:55 +03:00
|
|
|
JsonObject info = JsonParser.parseString(Files.readString(path)).getAsJsonObject();
|
|
|
|
|
if (!info.get("sha1").getAsString().matches("[a-f0-9]{40}")
|
|
|
|
|
|| (info.has("previousSha1") && !info.get("previousSha1").getAsString().matches("[a-f0-9]{40}"))
|
|
|
|
|
|| info.get("files").getAsInt() < 1) {
|
|
|
|
|
throw new IOException("Invalid resource pack manifest: " + path);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
return info;
|
2026-09-06 18:36:18 +03:00
|
|
|
} catch (RuntimeException e) {
|
2026-09-06 19:20:55 +03:00
|
|
|
throw new IOException("Invalid resource pack manifest: " + path, e);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
private static String sha1(Path file) throws IOException {
|
2026-09-06 18:22:00 +03:00
|
|
|
try {
|
2026-09-06 19:32:43 +03:00
|
|
|
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
2026-09-06 18:36:18 +03:00
|
|
|
try (var input = new DigestInputStream(Files.newInputStream(file), digest)) {
|
|
|
|
|
input.transferTo(OutputStream.nullOutputStream());
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
return HexFormat.of().formatHex(digest.digest());
|
2026-09-06 18:28:23 +03:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
|
throw new AssertionError(e);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|