Download noise natives and language guides instead of bundling them in the Bukkit jar

This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 16:29:20 -05:00
1 parent 1d23587b90
commit 714b4499b0
10 files changed
+952 -8

No files matched your search

@@ -0,0 +1,170 @@
import org.gradle.api.GradleException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Pins the VolmLib noise natives the Bukkit jar leaves out. The runtime downloads the binary for its platform and
* accepts it only when the size and SHA-256 recorded here match; the git blob id lets it fetch the exact bytes by
* content even when the build had no VolmLib checkout to name a commit.
*/
public final class NoiseNativeArtifacts {
public static final String MANIFEST = "META-INF/iris/noise-natives.properties";
public static final String RESOURCE_PREFIX = "art/arcane/volmlib/native/";
public static final String SOURCE_PATH = "shared/native/noise/prebuilt";
private NoiseNativeArtifacts() {
}
public static List<String> generate(Collection<File> jars, File volmLibDirectory, File output) throws IOException {
Map<String, NativeFile> natives = collect(jars);
if (natives.isEmpty()) {
throw new GradleException("No VolmLib noise natives under " + RESOURCE_PREFIX + " in " + jars);
}
List<String> warnings = new ArrayList<>();
String commit = volmLibDirectory == null ? "" : commit(volmLibDirectory, natives, warnings);
Properties manifest = new Properties();
manifest.setProperty("commit", commit);
manifest.setProperty("source", SOURCE_PATH);
manifest.setProperty("platforms", String.join(",", natives.keySet()));
for (NativeFile file : natives.values()) {
manifest.setProperty(file.platform() + ".file", file.name());
manifest.setProperty(file.platform() + ".size", String.valueOf(file.size()));
manifest.setProperty(file.platform() + ".sha256", file.sha256());
manifest.setProperty(file.platform() + ".blob", file.blob());
}
Files.createDirectories(output.toPath().getParent());
try (Writer writer = Files.newBufferedWriter(output.toPath(), StandardCharsets.UTF_8)) {
manifest.store(writer, null);
}
return warnings;
}
public static void verify(File artifact) throws IOException {
try (JarFile jar = new JarFile(artifact)) {
if (jar.getJarEntry(MANIFEST) == null) {
throw new GradleException(artifact.getName() + " is missing " + MANIFEST);
}
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if (name.startsWith(RESOURCE_PREFIX) && !name.endsWith("/")) {
throw new GradleException(artifact.getName() + " bundles " + name
+ "; the Bukkit jar downloads noise natives through " + MANIFEST);
}
}
}
}
static String sha256(byte[] bytes) {
return HexFormat.of().formatHex(digest("SHA-256", bytes));
}
static String blob(byte[] bytes) {
byte[] header = ("blob " + bytes.length + "\0").getBytes(StandardCharsets.US_ASCII);
byte[] object = new byte[header.length + bytes.length];
System.arraycopy(header, 0, object, 0, header.length);
System.arraycopy(bytes, 0, object, header.length, bytes.length);
return HexFormat.of().formatHex(digest("SHA-1", object));
}
private static Map<String, NativeFile> collect(Collection<File> jars) throws IOException {
Map<String, NativeFile> natives = new TreeMap<>();
for (File file : jars) {
if (!file.isFile()) {
continue;
}
try (JarFile jar = new JarFile(file)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (entry.isDirectory() || !name.startsWith(RESOURCE_PREFIX)) {
continue;
}
String[] parts = name.substring(RESOURCE_PREFIX.length()).split("/");
if (parts.length != 2) {
continue;
}
byte[] bytes;
try (InputStream input = jar.getInputStream(entry)) {
bytes = input.readAllBytes();
}
NativeFile resolved = new NativeFile(parts[0], parts[1], bytes.length, sha256(bytes), blob(bytes));
NativeFile previous = natives.putIfAbsent(parts[0], resolved);
if (previous != null && !previous.equals(resolved)) {
throw new GradleException("Conflicting noise natives for " + parts[0] + " in " + jars);
}
}
}
}
return natives;
}
private static String commit(File checkout, Map<String, NativeFile> natives, List<String> warnings) {
String commit = git(checkout, "log", "-1", "--format=%H", "--", SOURCE_PATH);
if (commit == null || commit.isEmpty()) {
warnings.add("Noise natives: no commit touches " + SOURCE_PATH + " in " + checkout + "; downloads fall back to the blob id");
return "";
}
for (NativeFile file : natives.values()) {
String path = commit + ":" + SOURCE_PATH + "/" + file.platform() + "/" + file.name();
if (!file.blob().equals(git(checkout, "rev-parse", path))) {
warnings.add("Noise natives: " + file.platform() + " differs from " + commit.substring(0, 7)
+ " (uncommitted rebuild?); downloads fall back to the blob id");
return "";
}
}
String remotes = git(checkout, "branch", "-r", "--contains", commit);
if (remotes == null || remotes.isEmpty()) {
warnings.add("Noise natives: " + commit.substring(0, 7) + " is not on any remote branch; downloads fail until it is pushed");
}
return commit;
}
private static String git(File directory, String... arguments) {
List<String> command = new ArrayList<>(List.of("git", "-C", directory.getAbsolutePath()));
command.addAll(List.of(arguments));
try {
Process process = new ProcessBuilder(command).redirectError(ProcessBuilder.Redirect.DISCARD).start();
String output = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8).trim();
if (!process.waitFor(30, TimeUnit.SECONDS) || process.exitValue() != 0) {
return null;
}
return output;
} catch (IOException failure) {
return null;
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
return null;
}
}
private static byte[] digest(String algorithm, byte[] bytes) {
try {
return MessageDigest.getInstance(algorithm).digest(bytes);
} catch (NoSuchAlgorithmException missing) {
throw new IllegalStateException(missing);
}
}
private record NativeFile(String platform, String name, long size, String sha256, String blob) {
}
}
@@ -0,0 +1,142 @@
import org.gradle.api.GradleException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class NoiseNativeArtifactsTest {
private static final String LINUX = "art/arcane/volmlib/native/linux-x86_64/libvolmlib_noise.so";
private static final String WINDOWS = "art/arcane/volmlib/native/windows-x86_64/volmlib_noise.dll";
private static final byte[] LINUX_BYTES = "linux-native".getBytes(StandardCharsets.UTF_8);
private static final byte[] WINDOWS_BYTES = "windows-native".getBytes(StandardCharsets.UTF_8);
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void blobMatchesGitHashObject() throws Exception {
File file = temporaryFolder.newFile("blob.bin");
Files.write(file.toPath(), LINUX_BYTES);
assertEquals(git(temporaryFolder.getRoot(), "hash-object", file.getAbsolutePath()), NoiseNativeArtifacts.blob(LINUX_BYTES));
}
@Test
public void generateRecordsEveryPlatformWithoutCommitWhenNoCheckoutIsKnown() throws Exception {
File output = temporaryFolder.getRoot().toPath().resolve("out/noise-natives.properties").toFile();
List<String> warnings = NoiseNativeArtifacts.generate(List.of(volmLibJar()), null, output);
Properties manifest = read(output);
assertEquals("", manifest.getProperty("commit"));
assertEquals("linux-x86_64,windows-x86_64", manifest.getProperty("platforms"));
assertEquals("libvolmlib_noise.so", manifest.getProperty("linux-x86_64.file"));
assertEquals(String.valueOf(LINUX_BYTES.length), manifest.getProperty("linux-x86_64.size"));
assertEquals(NoiseNativeArtifacts.sha256(LINUX_BYTES), manifest.getProperty("linux-x86_64.sha256"));
assertEquals(NoiseNativeArtifacts.blob(WINDOWS_BYTES), manifest.getProperty("windows-x86_64.blob"));
assertTrue(warnings.isEmpty());
}
@Test
public void generatePinsTheLastCommitThatHoldsTheSameBytes() throws Exception {
File checkout = checkout(LINUX_BYTES, WINDOWS_BYTES);
File output = temporaryFolder.newFile("pinned.properties");
NoiseNativeArtifacts.generate(List.of(volmLibJar()), checkout, output);
assertEquals(git(checkout, "rev-parse", "HEAD"), read(output).getProperty("commit"));
}
@Test
public void generateDropsTheCommitWhenTheCheckoutBytesDiffer() throws Exception {
File checkout = checkout("stale".getBytes(StandardCharsets.UTF_8), WINDOWS_BYTES);
File output = temporaryFolder.newFile("stale.properties");
List<String> warnings = NoiseNativeArtifacts.generate(List.of(volmLibJar()), checkout, output);
assertEquals("", read(output).getProperty("commit"));
assertFalse(warnings.isEmpty());
}
@Test
public void generateRejectsJarsWithoutNatives() throws Exception {
File empty = jar("empty.jar", Map.of("plugin.yml", new byte[]{1}));
File output = temporaryFolder.newFile("none.properties");
assertThrows(GradleException.class, () -> NoiseNativeArtifacts.generate(List.of(empty), null, output));
}
@Test
public void verifyRejectsBundledNativesAndMissingManifest() throws Exception {
File manifest = temporaryFolder.newFile("manifest.properties");
NoiseNativeArtifacts.generate(List.of(volmLibJar()), null, manifest);
byte[] manifestBytes = Files.readAllBytes(manifest.toPath());
NoiseNativeArtifacts.verify(jar("clean.jar", Map.of(NoiseNativeArtifacts.MANIFEST, manifestBytes)));
assertThrows(GradleException.class, () -> NoiseNativeArtifacts.verify(
jar("bundled.jar", Map.of(NoiseNativeArtifacts.MANIFEST, manifestBytes, LINUX, LINUX_BYTES))));
assertThrows(GradleException.class, () -> NoiseNativeArtifacts.verify(
jar("unpinned.jar", Map.of("plugin.yml", new byte[]{1}))));
}
private File volmLibJar() throws IOException {
return jar("volmlib-shared.jar", Map.of(LINUX, LINUX_BYTES, WINDOWS, WINDOWS_BYTES,
"art/arcane/volmlib/util/Noise.class", new byte[]{(byte) 0xCA, (byte) 0xFE}));
}
private File checkout(byte[] linux, byte[] windows) throws Exception {
File root = temporaryFolder.newFolder();
Path prebuilt = root.toPath().resolve(NoiseNativeArtifacts.SOURCE_PATH);
Files.createDirectories(prebuilt.resolve("linux-x86_64"));
Files.createDirectories(prebuilt.resolve("windows-x86_64"));
Files.write(prebuilt.resolve("linux-x86_64/libvolmlib_noise.so"), linux);
Files.write(prebuilt.resolve("windows-x86_64/volmlib_noise.dll"), windows);
git(root, "init", "-q");
git(root, "add", ".");
git(root, "-c", "user.name=test", "-c", "user.email=test@example.com", "commit", "-q", "-m", "natives");
return root;
}
private File jar(String name, Map<String, byte[]> entries) throws IOException {
File file = new File(temporaryFolder.getRoot(), name);
try (JarOutputStream output = new JarOutputStream(new FileOutputStream(file))) {
for (Map.Entry<String, byte[]> entry : new TreeMap<>(entries).entrySet()) {
output.putNextEntry(new JarEntry(entry.getKey()));
output.write(entry.getValue());
output.closeEntry();
}
}
return file;
}
private static Properties read(File file) throws IOException {
Properties properties = new Properties();
try (Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
properties.load(reader);
}
return properties;
}
private static String git(File directory, String... arguments) throws Exception {
List<String> command = new ArrayList<>(List.of("git", "-C", directory.getAbsolutePath()));
command.addAll(List.of(arguments));
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
String output = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8).trim();
assertEquals(output, 0, process.waitFor());
return output;
}
}