2026-09-06 18:22:00 +03:00
|
|
|
package online.astradev.packs;
|
|
|
|
|
|
2026-09-06 18:28:23 +03:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.net.http.HttpClient;
|
|
|
|
|
import java.net.http.HttpRequest;
|
|
|
|
|
import java.net.http.HttpResponse;
|
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;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
|
|
import java.security.DigestInputStream;
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.HexFormat;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
import java.util.SortedMap;
|
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
import java.util.zip.ZipFile;
|
|
|
|
|
import java.util.zip.ZipOutputStream;
|
2026-09-06 18:22:00 +03:00
|
|
|
|
|
|
|
|
/** Deterministic, bounded build. Immutable published ZIPs remain valid during later builds. */
|
|
|
|
|
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 record Asset(byte[] data, String owner) {}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private final PackConfig config;
|
|
|
|
|
private final SortedMap<String, Asset> assets = new TreeMap<>();
|
|
|
|
|
private long total;
|
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 18:28:23 +03:00
|
|
|
assets.clear();
|
|
|
|
|
total = 0;
|
2026-09-06 18:22:00 +03:00
|
|
|
for (String source : config.build.sources) {
|
|
|
|
|
Path path = root.resolve(source).toAbsolutePath().normalize();
|
|
|
|
|
if (!Files.exists(path)) throw new IOException("Source does not exist: " + path);
|
2026-09-06 18:28:23 +03:00
|
|
|
if (Files.isDirectory(path)) directory(path);
|
|
|
|
|
else archive(path, false);
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
if (config.build.scanModJars && Files.isDirectory(root.resolve("mods"))) {
|
|
|
|
|
try (var files = Files.list(root.resolve("mods"))) {
|
2026-09-06 18:28:23 +03:00
|
|
|
for (Path jar : files.filter(p -> p.toString().endsWith(".jar")).sorted().toList())
|
|
|
|
|
archive(jar, true);
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
JsonObject pack = new JsonObject();
|
|
|
|
|
// 26.2 uses major/minor resource-pack versions.
|
|
|
|
|
JsonElement version = JsonParser.parseString(resourceVersion);
|
2026-09-06 18:28:23 +03:00
|
|
|
pack.add("min_format", version);
|
|
|
|
|
pack.add("max_format", version.deepCopy());
|
2026-09-06 18:22:00 +03:00
|
|
|
pack.addProperty("description", config.build.description);
|
2026-09-06 18:28:23 +03:00
|
|
|
JsonObject meta = new JsonObject();
|
|
|
|
|
meta.add("pack", pack);
|
|
|
|
|
assets.put(
|
|
|
|
|
"pack.mcmeta",
|
|
|
|
|
new Asset(
|
|
|
|
|
PackConfig.JSON.toJson(meta).getBytes(StandardCharsets.UTF_8),
|
|
|
|
|
"AstraPacks"));
|
2026-09-06 18:22:00 +03:00
|
|
|
Path output = config.storage(root).resolve("output");
|
|
|
|
|
Files.createDirectories(output);
|
|
|
|
|
Path temp = Files.createTempFile(output, "build-", ".tmp");
|
|
|
|
|
try {
|
2026-09-06 18:28:23 +03:00
|
|
|
try (var zip =
|
|
|
|
|
new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(temp)))) {
|
2026-09-06 18:22:00 +03:00
|
|
|
for (var item : assets.entrySet()) {
|
2026-09-06 18:28:23 +03:00
|
|
|
ZipEntry entry = new ZipEntry(item.getKey());
|
|
|
|
|
entry.setTime(0);
|
|
|
|
|
zip.putNextEntry(entry);
|
|
|
|
|
zip.write(item.getValue().data());
|
|
|
|
|
zip.closeEntry();
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String hash = hash(temp, "SHA-1");
|
|
|
|
|
Path destination = output.resolve(hash + ".zip");
|
2026-09-06 18:28:23 +03:00
|
|
|
if (!Files.exists(destination))
|
|
|
|
|
Files.move(temp, destination, StandardCopyOption.ATOMIC_MOVE);
|
2026-09-06 18:22:00 +03:00
|
|
|
else if (!hash(destination, "SHA-1").equals(hash))
|
|
|
|
|
throw new IOException("Published ZIP was modified: " + destination);
|
2026-09-06 18:28:23 +03:00
|
|
|
PackFiles.atomicWrite(
|
|
|
|
|
output.resolve("current.json"),
|
|
|
|
|
PackConfig.JSON
|
|
|
|
|
.toJson(Map.of("sha1", hash, "files", assets.size()))
|
|
|
|
|
.getBytes(StandardCharsets.UTF_8));
|
2026-09-06 18:22:00 +03:00
|
|
|
return new Pack(destination, hash, assets.size(), Files.size(destination));
|
2026-09-06 18:28:23 +03:00
|
|
|
} finally {
|
|
|
|
|
Files.deleteIfExists(temp);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public static Pack previous(Path root, PackConfig config) throws IOException {
|
|
|
|
|
Path output = config.storage(root).resolve("output");
|
|
|
|
|
try {
|
2026-09-06 18:28:23 +03:00
|
|
|
JsonObject info =
|
|
|
|
|
JsonParser.parseString(Files.readString(output.resolve("current.json")))
|
|
|
|
|
.getAsJsonObject();
|
2026-09-06 18:22:00 +03:00
|
|
|
String sha1 = info.get("sha1").getAsString();
|
|
|
|
|
if (!sha1.matches("[a-f0-9]{40}")) throw new IOException("Invalid pack hash");
|
|
|
|
|
Path path = output.resolve(sha1 + ".zip");
|
2026-09-06 18:28:23 +03:00
|
|
|
if (!hash(path, "SHA-1").equals(sha1))
|
|
|
|
|
throw new IOException("Stored resource pack hash mismatch");
|
2026-09-06 18:22:00 +03:00
|
|
|
return new Pack(path, sha1, info.get("files").getAsInt(), Files.size(path));
|
2026-09-06 18:28:23 +03:00
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
throw new IOException("Invalid previous build", e);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private void directory(Path root) throws IOException {
|
|
|
|
|
if (Files.isSymbolicLink(root)) throw new IOException("Symlink source: " + root);
|
|
|
|
|
try (var walk = Files.walk(root)) {
|
|
|
|
|
for (Path p : walk.sorted().toList()) {
|
|
|
|
|
if (Files.isSymbolicLink(p)) throw new IOException("Symlink resource: " + p);
|
|
|
|
|
if (!Files.isRegularFile(p)) continue;
|
|
|
|
|
String name = root.relativize(p).toString().replace('\\', '/');
|
|
|
|
|
// A source is a pack root, or a directory of named pack roots.
|
|
|
|
|
if (!isResource(name)) {
|
|
|
|
|
int slash = name.indexOf('/');
|
2026-09-06 18:28:23 +03:00
|
|
|
if (slash >= 0 && isResource(name.substring(slash + 1)))
|
|
|
|
|
name = name.substring(slash + 1);
|
|
|
|
|
else if (name.endsWith(".zip")) {
|
|
|
|
|
archive(p, false);
|
|
|
|
|
continue;
|
|
|
|
|
} else continue;
|
|
|
|
|
}
|
|
|
|
|
try (var in = Files.newInputStream(p)) {
|
|
|
|
|
add(name, in, p.toString());
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private void archive(Path path, boolean mod) throws IOException {
|
|
|
|
|
try (var zip = new ZipFile(path.toFile())) {
|
|
|
|
|
String prefix = "";
|
|
|
|
|
if (mod) {
|
|
|
|
|
var descriptor = zip.getEntry("astra-pack.properties");
|
|
|
|
|
if (descriptor == null) return;
|
|
|
|
|
Properties props = new Properties();
|
2026-09-06 18:28:23 +03:00
|
|
|
try (var in = zip.getInputStream(descriptor)) {
|
|
|
|
|
props.load(in);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
String schema = props.getProperty("schema", "");
|
|
|
|
|
if (schema.equals("2")) prefix = "astra-resourcepack/";
|
|
|
|
|
else throw new IOException("Invalid AstraPacks bundle descriptor: " + path);
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
for (var entries = zip.entries(); entries.hasMoreElements(); ) {
|
2026-09-06 18:22:00 +03:00
|
|
|
ZipEntry entry = entries.nextElement();
|
|
|
|
|
if (entry.isDirectory() || !entry.getName().startsWith(prefix)) continue;
|
|
|
|
|
String name = entry.getName().substring(prefix.length());
|
|
|
|
|
if (!isResource(name)) continue;
|
2026-09-06 18:28:23 +03:00
|
|
|
try (var in = zip.getInputStream(entry)) {
|
|
|
|
|
add(name, in, path + "!/" + entry.getName());
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
|
|
|
|
private static boolean isResource(String name) {
|
|
|
|
|
return name.startsWith("assets/") || name.equals("pack.png");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private void add(String name, InputStream in, String owner) throws IOException {
|
|
|
|
|
PackFiles.safe(Path.of("."), name);
|
|
|
|
|
int limit = config.build.maxFileMiB * 1024 * 1024;
|
|
|
|
|
byte[] bytes = in.readNBytes(limit + 1);
|
|
|
|
|
if (bytes.length > limit) throw new IOException("Resource exceeds maxFileMiB: " + owner);
|
|
|
|
|
total += bytes.length;
|
2026-09-06 18:28:23 +03:00
|
|
|
if (total > config.build.maxTotalMiB * 1024L * 1024L)
|
|
|
|
|
throw new IOException("Sources exceed maxTotalMiB");
|
2026-09-06 18:22:00 +03:00
|
|
|
Asset old = assets.get(name);
|
2026-09-06 18:28:23 +03:00
|
|
|
if (old != null
|
|
|
|
|
&& !Arrays.equals(bytes, old.data())
|
|
|
|
|
&& config.build.conflictPolicy.equals("error"))
|
|
|
|
|
throw new IOException(
|
|
|
|
|
"Conflicting resource " + name + " from " + old.owner() + " and " + owner);
|
2026-09-06 18:22:00 +03:00
|
|
|
assets.put(name, new Asset(bytes, owner));
|
|
|
|
|
if (assets.size() > config.build.maxFiles) throw new IOException("Too many resources");
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public static String hash(Path file, String algorithm) throws IOException {
|
|
|
|
|
try {
|
|
|
|
|
MessageDigest digest = MessageDigest.getInstance(algorithm);
|
2026-09-06 18:28:23 +03:00
|
|
|
try (var in = new DigestInputStream(Files.newInputStream(file), digest)) {
|
|
|
|
|
in.transferTo(OutputStream.nullOutputStream());
|
|
|
|
|
}
|
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
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
static void verifyExternal(PackConfig config, PackBuilder.Pack pack) throws IOException {
|
|
|
|
|
if (!config.hosting.mode.equals("no_host") || !config.delivery.sendOnJoin) return;
|
2026-09-06 18:28:23 +03:00
|
|
|
try (HttpClient client =
|
|
|
|
|
HttpClient.newBuilder()
|
|
|
|
|
.connectTimeout(Duration.ofSeconds(10))
|
|
|
|
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
|
|
|
|
.build()) {
|
|
|
|
|
HttpRequest request =
|
|
|
|
|
HttpRequest.newBuilder(PackConfig.validateUrl(config.hosting.externalUrl))
|
|
|
|
|
.timeout(Duration.ofSeconds(30))
|
|
|
|
|
.GET()
|
|
|
|
|
.build();
|
2026-09-06 18:22:00 +03:00
|
|
|
// A bounded subscriber prevents an external host from consuming arbitrary memory/disk.
|
2026-09-06 18:28:23 +03:00
|
|
|
HttpResponse<byte[]> response =
|
|
|
|
|
client.send(
|
|
|
|
|
request,
|
|
|
|
|
HttpResponse.BodyHandlers.limiting(
|
|
|
|
|
HttpResponse.BodyHandlers.ofByteArray(), pack.bytes()));
|
|
|
|
|
if (response.statusCode() != 200)
|
|
|
|
|
throw new IOException("External pack HTTP " + response.statusCode());
|
|
|
|
|
String hash =
|
|
|
|
|
HexFormat.of()
|
|
|
|
|
.formatHex(MessageDigest.getInstance("SHA-1").digest(response.body()));
|
|
|
|
|
if (!hash.equals(pack.sha1()))
|
|
|
|
|
throw new IOException(
|
|
|
|
|
"External ZIP differs from built ZIP. Upload "
|
|
|
|
|
+ pack.path()
|
|
|
|
|
+ ", then /astrapacks publish");
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
throw new IOException(e);
|
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
|
throw new AssertionError(e);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|