Introduce gradle-plugin and mod-tools modules. Add ZomboidForge Gradle plugin for building, checking, and packaging mods. Include support for local test servers, automated game jar downloads, and mod descriptor generation. Add mod-tools for runtime mod checks and validations.
This commit is contained in:
1 parent
68cd9fc4f9
commit
063ad8a3e1
27 files changed
+1976
-30
No files matched your search
@@ -0,0 +1,42 @@
|
||||
// Build-time checks for mods, run by the ZomboidForge Gradle plugin in a separate Java 25
|
||||
// process. Published as one self-contained jar (dev.overlord.zomboidforge:mod-tools) without
|
||||
// dependencies, so a mod's build only needs the ZomboidForge Maven repository.
|
||||
|
||||
plugins {
|
||||
id("zomboidforge.java-conventions")
|
||||
id("zomboidforge.publishing")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":api"))
|
||||
implementation(project(":core"))
|
||||
implementation(project(":patcher"))
|
||||
implementation(libs.bundles.asm)
|
||||
|
||||
testImplementation(platform(libs.junit.bom))
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testRuntimeOnly(libs.junit.platform.launcher)
|
||||
}
|
||||
|
||||
val toolJar = tasks.register<Jar>("toolJar") {
|
||||
description = "The mod tools with every runtime dependency, for publishing."
|
||||
archiveBaseName = "mod-tools"
|
||||
archiveClassifier = "all"
|
||||
manifest.attributes("Main-Class" to "dev.overlord.zomboidforge.tools.ModCheck")
|
||||
|
||||
from(sourceSets.main.map { it.output })
|
||||
// Mapping the classpath to zip trees loses its task dependencies; the input restores them.
|
||||
inputs.files(configurations.runtimeClasspath)
|
||||
from(configurations.runtimeClasspath.map { classpath -> classpath.map { zipTree(it) } })
|
||||
exclude("META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA", "META-INF/versions/*/module-info.class", "module-info.class")
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("modTools") {
|
||||
artifactId = "mod-tools"
|
||||
artifact(toolJar) { classifier = null }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
package dev.overlord.zomboidforge.tools;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.core.config.TomlTree;
|
||||
import dev.overlord.zomboidforge.core.mods.GameClasses;
|
||||
import dev.overlord.zomboidforge.core.mods.ModDescriptor;
|
||||
import dev.overlord.zomboidforge.core.mods.ModDiscovery;
|
||||
import dev.overlord.zomboidforge.core.mods.ModLoadException;
|
||||
import dev.overlord.zomboidforge.core.mods.ModVerifier;
|
||||
import dev.overlord.zomboidforge.core.mods.ModVersion;
|
||||
import dev.overlord.zomboidforge.patcher.GameVersionReader;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
/**
|
||||
* Checks a built mod jar the way the server does when it loads the mod, plus what only a
|
||||
* build can check:
|
||||
*
|
||||
* <ol>
|
||||
* <li>{@code mod.toml} and the default {@code config.toml} in the jar are valid;</li>
|
||||
* <li>the entry point is in the jar, extends {@code Mod}, is not abstract, and has a public
|
||||
* constructor without parameters;</li>
|
||||
* <li>every mod jar the mod compiles against is declared in {@code dependencies} with a
|
||||
* minimum version it satisfies;</li>
|
||||
* <li>the game jar is one of the declared {@code game} versions;</li>
|
||||
* <li>{@link ModVerifier}: game use requires {@code game} versions, every used game class
|
||||
* and member exists in the game jar, no ZomboidForge internals.</li>
|
||||
* </ol>
|
||||
*
|
||||
* <pre>
|
||||
* java -jar mod-tools.jar --jar <mod.jar> [--game-jar <projectzomboid.jar>]
|
||||
* [--library <jar>]... [--dependency <mod jar>]... [--report <file>]
|
||||
* </pre>
|
||||
*
|
||||
* A mod that declares {@code game} versions needs {@code --game-jar}; without it, game classes
|
||||
* count as unknown, which is enough for a mod that does not compile against the game.
|
||||
*
|
||||
* Exit code 0 when the mod passes (the report file is written then), 1 with every problem on
|
||||
* standard error, 2 for invalid arguments.
|
||||
*/
|
||||
public final class ModCheck
|
||||
{
|
||||
private static final String MOD_CLASS = "dev/overlord/zomboidforge/api/Mod";
|
||||
|
||||
/** The command-line arguments. */
|
||||
public record Arguments(Path jar, @Nullable Path gameJar, List<Path> libraries, List<Path> dependencies, @Nullable Path report)
|
||||
{
|
||||
public Arguments
|
||||
{
|
||||
libraries = List.copyOf(libraries);
|
||||
dependencies = List.copyOf(dependencies);
|
||||
}
|
||||
|
||||
private static final Set<String> OPTIONS = Set.of("--jar", "--game-jar", "--report", "--library", "--dependency");
|
||||
|
||||
/** @throws IllegalArgumentException for unknown, missing, repeated, or incomplete options */
|
||||
public static Arguments parse(String[] args)
|
||||
{
|
||||
Map<String, List<Path>> values = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < args.length; i += 2)
|
||||
{
|
||||
if (!Arguments.OPTIONS.contains(args[i])) throw new IllegalArgumentException("Unknown option '" + args[i] + "'.");
|
||||
if (i + 1 >= args.length) throw new IllegalArgumentException("'" + args[i] + "' needs a value.");
|
||||
|
||||
values.computeIfAbsent(args[i], option -> new ArrayList<>()).add(Path.of(args[i + 1]));
|
||||
}
|
||||
|
||||
return new Arguments(
|
||||
Arguments.single(values, "--jar"),
|
||||
Arguments.optional(values, "--game-jar"),
|
||||
values.getOrDefault("--library", List.of()),
|
||||
values.getOrDefault("--dependency", List.of()),
|
||||
Arguments.optional(values, "--report")
|
||||
);
|
||||
}
|
||||
|
||||
private static Path single(Map<String, List<Path>> values, String option)
|
||||
{
|
||||
List<Path> given = values.getOrDefault(option, List.of());
|
||||
|
||||
if (given.size() != 1) throw new IllegalArgumentException("'" + option + "' is required once.");
|
||||
|
||||
return given.getFirst();
|
||||
}
|
||||
|
||||
private static @Nullable Path optional(Map<String, List<Path>> values, String option)
|
||||
{
|
||||
List<Path> given = values.getOrDefault(option, List.of());
|
||||
|
||||
if (given.size() > 1) throw new IllegalArgumentException("'" + option + "' may be given once.");
|
||||
|
||||
return given.isEmpty() ? null : given.getFirst();
|
||||
}
|
||||
}
|
||||
|
||||
/** What a check found: the mod's descriptor (when readable), the game version, and every problem. */
|
||||
public record Result(@Nullable ModDescriptor descriptor, @Nullable GameVersion gameVersion, List<String> problems)
|
||||
{
|
||||
public Result { problems = List.copyOf(problems); }
|
||||
|
||||
public boolean passed() { return this.problems.isEmpty(); }
|
||||
}
|
||||
|
||||
private ModCheck() {}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Arguments arguments;
|
||||
|
||||
try
|
||||
{
|
||||
arguments = Arguments.parse(args);
|
||||
}
|
||||
catch (IllegalArgumentException e)
|
||||
{
|
||||
System.err.println("[ZomboidForge] " + e.getMessage());
|
||||
System.exit(2);
|
||||
return;
|
||||
}
|
||||
|
||||
Result result = ModCheck.check(arguments);
|
||||
ModDescriptor descriptor = result.descriptor();
|
||||
|
||||
if (!result.passed() || descriptor == null)
|
||||
{
|
||||
System.err.println("[ZomboidForge] The mod in '" + arguments.jar().getFileName() + "' would not load:");
|
||||
result.problems().forEach(problem -> System.err.println(" - " + problem));
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
String against = result.gameVersion() == null ? " (no game jar: the mod declares no game versions)" : " against Project Zomboid " + result.gameVersion();
|
||||
String summary = "Mod '" + descriptor.id() + "' " + descriptor.version() + " passed the checks" + against + ".";
|
||||
System.out.println("[ZomboidForge] " + summary);
|
||||
|
||||
Path report = arguments.report();
|
||||
if (report == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
Files.writeString(report, summary + System.lineSeparator());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.err.println("[ZomboidForge] Cannot write '" + report + "': " + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/** Runs every check; never throws for problems of the mod. */
|
||||
public static Result check(Arguments arguments)
|
||||
{
|
||||
List<String> problems = new ArrayList<>();
|
||||
ModDescriptor descriptor = ModCheck.descriptor(arguments.jar(), problems);
|
||||
|
||||
if (descriptor == null) return new Result(null, null, problems);
|
||||
|
||||
ModCheck.checkDefaultConfig(arguments.jar(), problems);
|
||||
ModCheck.checkEntrypoint(arguments.jar(), descriptor, problems);
|
||||
|
||||
List<ModDiscovery.Candidate> candidates = new ArrayList<>();
|
||||
|
||||
for (Path dependency : arguments.dependencies())
|
||||
{
|
||||
ModDescriptor declared = ModCheck.descriptor(dependency, problems);
|
||||
if (declared == null) continue;
|
||||
|
||||
ModVersion minimum = descriptor.dependencies().get(declared.id());
|
||||
|
||||
if (minimum == null)
|
||||
problems.add("The mod compiles against '" + declared.id() + "' (" + dependency.getFileName() + "), which 'mod.toml' does not list in 'dependencies'.");
|
||||
else if (declared.version().compareTo(minimum) < 0)
|
||||
problems.add("The mod compiles against '" + declared.id() + "' " + declared.version() + ", older than the declared minimum " + minimum + ".");
|
||||
|
||||
candidates.add(ModCheck.candidate(dependency, List.of(), declared));
|
||||
}
|
||||
|
||||
candidates.add(ModCheck.candidate(arguments.jar(), arguments.libraries(), descriptor));
|
||||
|
||||
Path gameJar = arguments.gameJar();
|
||||
|
||||
if (gameJar == null)
|
||||
{
|
||||
if (!descriptor.game().isEmpty()) problems.add("The mod declares " + descriptor.game() + ", so the check needs '--game-jar'.");
|
||||
|
||||
ModCheck.verify(candidates, GameClasses.none(), null, problems);
|
||||
return new Result(descriptor, null, problems);
|
||||
}
|
||||
|
||||
GameVersion gameVersion = ModCheck.gameVersion(gameJar, problems);
|
||||
if (gameVersion == null) return new Result(descriptor, null, problems);
|
||||
|
||||
if (!descriptor.game().isEmpty() && descriptor.game().stream().noneMatch(gameVersion::matches))
|
||||
problems.add("The game jar '" + gameJar + "' is Project Zomboid " + gameVersion + ", but the mod declares " + descriptor.game() + "; the check needs a game jar of a declared version.");
|
||||
|
||||
try (GameClasses game = GameClasses.open(gameJar))
|
||||
{
|
||||
ModCheck.verify(candidates, game, gameVersion, problems);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
problems.add("Cannot read the game jar '" + gameJar + "': " + e.getMessage());
|
||||
}
|
||||
|
||||
return new Result(descriptor, gameVersion, problems);
|
||||
}
|
||||
|
||||
private static void verify(List<ModDiscovery.Candidate> candidates, GameClasses game, @Nullable GameVersion version, List<String> problems)
|
||||
{
|
||||
try { ModVerifier.verify(candidates, game, version); }
|
||||
catch (ModLoadException e) { problems.addAll(e.problems()); }
|
||||
}
|
||||
|
||||
private static ModDiscovery.Candidate candidate(Path jar, List<Path> libraries, ModDescriptor descriptor)
|
||||
{
|
||||
return new ModDiscovery.Candidate(jar.toAbsolutePath().getParent(), jar, libraries, descriptor, true, Map.of());
|
||||
}
|
||||
|
||||
private static @Nullable ModDescriptor descriptor(Path jar, List<String> problems)
|
||||
{
|
||||
String toml = ModCheck.entry(jar, ModDescriptor.PATH, problems);
|
||||
|
||||
if (toml == null)
|
||||
{
|
||||
problems.add("'" + jar.getFileName() + "' contains no '" + ModDescriptor.PATH + "'.");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return ModDescriptor.parse(toml);
|
||||
}
|
||||
catch (ModLoadException e)
|
||||
{
|
||||
e.problems().forEach(problem -> problems.add("'" + jar.getFileName() + "': mod.toml: " + problem));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkDefaultConfig(Path jar, List<String> problems)
|
||||
{
|
||||
String toml = ModCheck.entry(jar, ModDiscovery.DEFAULT_CONFIG, problems);
|
||||
if (toml == null) return;
|
||||
|
||||
Map<String, Object> settings;
|
||||
|
||||
try
|
||||
{
|
||||
settings = TomlTree.parse(toml);
|
||||
}
|
||||
catch (IllegalArgumentException e)
|
||||
{
|
||||
problems.add("'" + ModDiscovery.DEFAULT_CONFIG + "': " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(settings.get("forge") instanceof Map<?, ?> forge)) return;
|
||||
|
||||
for (Map.Entry<?, ?> entry : forge.entrySet())
|
||||
{
|
||||
if (!"enabled".equals(entry.getKey()))
|
||||
problems.add("'" + ModDiscovery.DEFAULT_CONFIG + "': unknown key 'forge." + entry.getKey() + "'; [forge] only has 'enabled'.");
|
||||
else if (!(entry.getValue() instanceof Boolean))
|
||||
problems.add("'" + ModDiscovery.DEFAULT_CONFIG + "': 'forge.enabled' must be true or false.");
|
||||
}
|
||||
}
|
||||
|
||||
/** The entry point and its superclasses inside the jar must lead to {@code Mod}. */
|
||||
private static void checkEntrypoint(Path jar, ModDescriptor descriptor, List<String> problems)
|
||||
{
|
||||
String entrypoint = descriptor.entrypoint();
|
||||
ClassNode type = ModCheck.classNode(jar, entrypoint.replace('.', '/'), problems);
|
||||
|
||||
if (type == null)
|
||||
{
|
||||
problems.add("The entry point '" + entrypoint + "' is not in '" + jar.getFileName() + "'.");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((type.access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE)) != 0) problems.add("The entry point '" + entrypoint + "' is abstract.");
|
||||
|
||||
boolean constructor = type.methods.stream().anyMatch(ModCheck::isPublicNoArgConstructor);
|
||||
if (!constructor || (type.access & Opcodes.ACC_PUBLIC) == 0) problems.add("The entry point '" + entrypoint + "' needs to be public, with a public constructor without parameters.");
|
||||
|
||||
ClassNode current = type;
|
||||
|
||||
for (int depth = 0; depth < 32; depth++)
|
||||
{
|
||||
String superName = current.superName;
|
||||
|
||||
if (ModCheck.MOD_CLASS.equals(superName)) return;
|
||||
if (superName == null) break;
|
||||
|
||||
ClassNode parent = ModCheck.classNode(jar, superName, problems);
|
||||
if (parent == null) break;
|
||||
|
||||
current = parent;
|
||||
}
|
||||
|
||||
problems.add("The entry point '" + entrypoint + "' does not extend 'dev.overlord.zomboidforge.api.Mod'.");
|
||||
}
|
||||
|
||||
private static boolean isPublicNoArgConstructor(MethodNode method)
|
||||
{
|
||||
return method.name.equals("<init>") && method.desc.equals("()V") && (method.access & Opcodes.ACC_PUBLIC) != 0;
|
||||
}
|
||||
|
||||
private static @Nullable GameVersion gameVersion(Path gameJar, List<String> problems)
|
||||
{
|
||||
try (JarFile file = new JarFile(gameJar.toFile()))
|
||||
{
|
||||
ZipEntry entry = file.getEntry(GameVersionReader.CORE_CLASS + ".class");
|
||||
|
||||
if (entry == null)
|
||||
{
|
||||
problems.add("'" + gameJar + "' is not a Project Zomboid jar: it has no '" + GameVersionReader.CORE_CLASS + "'.");
|
||||
return null;
|
||||
}
|
||||
|
||||
try (InputStream in = file.getInputStream(entry)) { return GameVersionReader.read(in.readAllBytes()); }
|
||||
}
|
||||
catch (IOException | RuntimeException e)
|
||||
{
|
||||
problems.add("Cannot read the Project Zomboid version from '" + gameJar + "': " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static @Nullable ClassNode classNode(Path jar, String internalName, List<String> problems)
|
||||
{
|
||||
byte[] bytes = ModCheck.bytes(jar, internalName + ".class", problems);
|
||||
if (bytes == null) return null;
|
||||
|
||||
ClassNode node = new ClassNode();
|
||||
new ClassReader(bytes).accept(node, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static @Nullable String entry(Path jar, String name, List<String> problems)
|
||||
{
|
||||
byte[] bytes = ModCheck.bytes(jar, name, problems);
|
||||
return bytes == null ? null : new String(bytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static byte @Nullable [] bytes(Path jar, String name, List<String> problems)
|
||||
{
|
||||
try (JarFile file = new JarFile(jar.toFile()))
|
||||
{
|
||||
ZipEntry entry = file.getEntry(name);
|
||||
if (entry == null) return null;
|
||||
|
||||
try (InputStream in = file.getInputStream(entry)) { return in.readAllBytes(); }
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
problems.add("Cannot read '" + jar + "': " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Build-time checks for mods, run by the ZomboidForge Gradle plugin.
|
||||
*
|
||||
* @noinspection UnstableApiUsage, RedundantSuppression
|
||||
*/
|
||||
@NotNullByDefault
|
||||
package dev.overlord.zomboidforge.tools;
|
||||
|
||||
import org.jetbrains.annotations.NotNullByDefault;
|
||||
Reference in new issue
Block a user