Move GameVersion and its tests to the api module and add prefix matching (42, 42.21, 42.21.0). Add the abstract Mod base class with ModContext, bound through a ScopedValue while the mod is constructed. Add mod discovery, mod.toml parsing (night-config TOML), validation of API and game versions and dependencies, dependency ordering, and per-mod class loaders in core.mods. Add Forge to load mods and call their lifecycle methods from ServerHooks, and ConsoleLogger for log output. Add tests for versions, descriptors, discovery, resolution, and loading.
This commit is contained in:
1 parent
837367047c
commit
df4d58a279
46 files changed
+1846
-131
No files matched your search
@@ -3,3 +3,9 @@
|
||||
plugins {
|
||||
id("zomboidforge.java-conventions")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation(platform(libs.junit.bom))
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testRuntimeOnly(libs.junit.platform.launcher)
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** Project Zomboid version, e.g. 42.21.0 ({@code Core.getGameAndBuildVersion()}). */
|
||||
public record GameVersion(int major, int minor, int build) implements Comparable<GameVersion>
|
||||
{
|
||||
private static final Pattern VERSION = Pattern.compile("(\\d+)\\.(\\d+)(?:\\.(\\d+))?");
|
||||
private static final Pattern PREFIX = Pattern.compile("(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?");
|
||||
|
||||
private static final Comparator<GameVersion> ORDER = Comparator
|
||||
.comparingInt(GameVersion::major)
|
||||
.thenComparingInt(GameVersion::minor)
|
||||
.thenComparingInt(GameVersion::build);
|
||||
|
||||
/** Parses "42.21.0", or "42.21" as 42.21.0. */
|
||||
public static GameVersion parse(String text)
|
||||
{
|
||||
Matcher matcher = GameVersion.VERSION.matcher(text.trim());
|
||||
if (!matcher.matches()) throw new IllegalArgumentException("Not a Project Zomboid version: '" + text + "'.");
|
||||
|
||||
int build = matcher.group(3) == null
|
||||
? 0
|
||||
: Integer.parseInt(matcher.group(3));
|
||||
|
||||
return new GameVersion(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), build);
|
||||
}
|
||||
|
||||
/** Whether {@code text} is a version prefix for {@link #matches}: "42", "42.21", or "42.21.0". */
|
||||
public static boolean isPrefix(String text) { return GameVersion.PREFIX.matcher(text.trim()).matches(); }
|
||||
|
||||
/**
|
||||
* Whether this version starts with {@code prefix}: "42" matches every 42.x.y, "42.21" every
|
||||
* 42.21.x, "42.21.0" only 42.21.0.
|
||||
*/
|
||||
public boolean matches(String prefix)
|
||||
{
|
||||
Matcher matcher = GameVersion.PREFIX.matcher(prefix.trim());
|
||||
if (!matcher.matches()) throw new IllegalArgumentException("Not a Project Zomboid version prefix: '" + prefix + "'.");
|
||||
|
||||
return GameVersion.partMatches(matcher.group(1), this.major)
|
||||
&& GameVersion.partMatches(matcher.group(2), this.minor)
|
||||
&& GameVersion.partMatches(matcher.group(3), this.build);
|
||||
}
|
||||
|
||||
public boolean isAtLeast(int major, int minor) { return this.compareTo(new GameVersion(major, minor, 0)) >= 0; }
|
||||
|
||||
public boolean isBefore(int major, int minor) { return this.compareTo(new GameVersion(major, minor, 0)) < 0; }
|
||||
|
||||
@Override
|
||||
public int compareTo(GameVersion other) { return GameVersion.ORDER.compare(this, other); }
|
||||
|
||||
@Override
|
||||
public String toString() { return this.major + "." + this.minor + "." + this.build; }
|
||||
|
||||
/** A missing part of a prefix matches any value. */
|
||||
private static boolean partMatches(@Nullable String part, int value) { return part == null || Integer.parseInt(part) == value; }
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import dev.overlord.zomboidforge.api.internal.ModBinding;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Base class of every mod. The class named as {@code entrypoint} in {@code mod.toml} extends it
|
||||
* and has a public constructor without parameters.
|
||||
*
|
||||
* <p>ZomboidForge creates one instance per mod on the main thread, in dependency order, while
|
||||
* the server starts. The methods of this class work from the constructor on, so field
|
||||
* initializers may use them.
|
||||
*
|
||||
* <p>Lifecycle, in this order:
|
||||
* <ol>
|
||||
* <li>{@link #onLoad()}: registration and setup. The server database, Lua, and the world do
|
||||
* not exist yet.</li>
|
||||
* <li>{@link #onServerStarted()}: the world is loaded and the network port is open.</li>
|
||||
* <li>{@link #onStopping()}: the server stops; the world has not been saved yet.</li>
|
||||
* </ol>
|
||||
* An exception thrown by the constructor or {@link #onLoad()} stops the server.
|
||||
*/
|
||||
public abstract class Mod
|
||||
{
|
||||
private final ModContext context;
|
||||
|
||||
/** @throws IllegalStateException when the instance is not created by ZomboidForge */
|
||||
protected Mod()
|
||||
{
|
||||
if (!ModBinding.CONTEXT.isBound())
|
||||
throw new IllegalStateException("'" + this.getClass().getName() + "' can only be created by ZomboidForge.");
|
||||
|
||||
this.context = ModBinding.CONTEXT.get();
|
||||
}
|
||||
|
||||
/** The id from {@code mod.toml}. */
|
||||
public final String id() { return this.context.id(); }
|
||||
|
||||
/** The version from {@code mod.toml}. */
|
||||
public final String version() { return this.context.version(); }
|
||||
|
||||
/** Called once on the main thread, after the instance is created. */
|
||||
@SuppressWarnings("RedundantThrows")
|
||||
public void onLoad() throws Exception {}
|
||||
|
||||
/**
|
||||
* Called once on the main thread, after the world is loaded, the network port is open, and
|
||||
* RCON is initialized; directly before the main loop starts.
|
||||
*/
|
||||
public void onServerStarted() {}
|
||||
|
||||
/**
|
||||
* Called once on the shutdown thread when the server stops, before the world is saved.
|
||||
* The main thread may still be finishing a tick. Mods are stopped in reverse load order.
|
||||
*/
|
||||
public void onStopping() {}
|
||||
|
||||
/** The running Project Zomboid version. */
|
||||
protected final GameVersion gameVersion() { return this.context.gameVersion(); }
|
||||
|
||||
/** Logger whose messages go into the Project Zomboid log, marked with the mod's id. */
|
||||
protected final System.Logger logger() { return this.context.logger(); }
|
||||
|
||||
/** The mod's folder, {@code <forge>/mods/<id>}. */
|
||||
protected final Path directory() { return this.context.directory(); }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* What ZomboidForge provides to one mod. Mods reach it through the methods of {@link Mod};
|
||||
* ZomboidForge implements it.
|
||||
*/
|
||||
public interface ModContext
|
||||
{
|
||||
/** The mod's id from {@code mod.toml}. */
|
||||
String id();
|
||||
|
||||
/** The mod's version from {@code mod.toml}. */
|
||||
String version();
|
||||
|
||||
/** The running Project Zomboid version. */
|
||||
GameVersion gameVersion();
|
||||
|
||||
/** Logger whose messages go into the Project Zomboid log, marked with the mod's id. */
|
||||
System.Logger logger();
|
||||
|
||||
/** The mod's folder, {@code <forge>/mods/<id>}. */
|
||||
Path directory();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package dev.overlord.zomboidforge.api.internal;
|
||||
|
||||
import dev.overlord.zomboidforge.api.ModContext;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus.Internal;
|
||||
|
||||
/**
|
||||
* The context of the mod being created. ZomboidForge binds it while it calls a mod's
|
||||
* constructor; the {@code Mod} constructor reads it.
|
||||
*/
|
||||
@Internal
|
||||
public final class ModBinding
|
||||
{
|
||||
public static final ScopedValue<ModContext> CONTEXT = ScopedValue.newInstance();
|
||||
|
||||
private ModBinding() {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Connection between the API and the ZomboidForge runtime. Not part of the mod API.
|
||||
*
|
||||
* @noinspection UnstableApiUsage, RedundantSuppression
|
||||
*/
|
||||
@NotNullByDefault
|
||||
package dev.overlord.zomboidforge.api.internal;
|
||||
|
||||
import org.jetbrains.annotations.NotNullByDefault;
|
||||
@@ -0,0 +1,56 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class GameVersionTest
|
||||
{
|
||||
@Test
|
||||
void parsesTwoAndThreePartVersions()
|
||||
{
|
||||
assertEquals(new GameVersion(42, 21, 0), GameVersion.parse("42.21"));
|
||||
assertEquals(new GameVersion(42, 21, 3), GameVersion.parse("42.21.3"));
|
||||
assertThrows(IllegalArgumentException.class, () -> GameVersion.parse("42"));
|
||||
assertThrows(IllegalArgumentException.class, () -> GameVersion.parse("b42.21"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void prefixMatchesEveryMissingPart()
|
||||
{
|
||||
GameVersion version = new GameVersion(42, 21, 3);
|
||||
|
||||
assertTrue(version.matches("42"));
|
||||
assertTrue(version.matches("42.21"));
|
||||
assertTrue(version.matches("42.21.3"));
|
||||
assertFalse(version.matches("41"));
|
||||
assertFalse(version.matches("42.20"));
|
||||
assertFalse(version.matches("42.21.0"));
|
||||
assertFalse(version.matches("4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedPrefixes()
|
||||
{
|
||||
GameVersion version = new GameVersion(42, 21, 3);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> version.matches(""));
|
||||
assertThrows(IllegalArgumentException.class, () -> version.matches("42."));
|
||||
assertThrows(IllegalArgumentException.class, () -> version.matches("42.21.3.1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void comparesNumerically()
|
||||
{
|
||||
GameVersion version = new GameVersion(42, 21, 0);
|
||||
|
||||
assertTrue(version.isAtLeast(42, 9));
|
||||
assertTrue(version.isAtLeast(42, 21));
|
||||
assertFalse(version.isAtLeast(42, 22));
|
||||
assertTrue(version.isBefore(43, 0));
|
||||
assertFalse(version.isBefore(42, 21));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user