Add BackupStoragePatch to wrap save folder backups, ensuring storage writes are paused during backups. Introduce ModStorage and ModTables for per-mod database management, supporting schema migrations and table versioning. Add ForgeInstance as a static reference to server-side Forge. Include relevant tests.

This commit is contained in:
Overlord committed 2026-10-02 13:57:50 +02:00
1 parent c56046469e
commit 68cd9fc4f9
31 files changed
+2124 -42

No files matched your search

@@ -1,6 +1,7 @@
package dev.overlord.zomboidforge.api;
import dev.overlord.zomboidforge.api.internal.ModBinding;
import dev.overlord.zomboidforge.api.storage.ModStorage;
import java.nio.file.Path;
@@ -80,4 +81,7 @@ public abstract class Mod
/** Virtual threads for blocking work such as HTTP calls or file I/O. */
protected final AsyncExecutor async() { return this.context.async(); }
/** The mod's tables in the world's {@code zomboidforge.db}; not on the main thread once the server runs. */
protected final ModStorage storage() { return this.context.storage(); }
}
@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import dev.overlord.zomboidforge.api.storage.ModStorage;
import java.nio.file.Path;
/**
@@ -37,4 +39,7 @@ public interface ModContext
/** Virtual threads for blocking work such as HTTP calls or file I/O. */
AsyncExecutor async();
/** The mod's tables in the world's {@code zomboidforge.db}. */
ModStorage storage();
}
@@ -0,0 +1,34 @@
package dev.overlord.zomboidforge.api.storage;
import java.util.function.Supplier;
/**
* The mod's tables in {@code zomboidforge.db}, next to the world's save. Every table belongs to
* the mod; other mods cannot reach it.
*
* <p>A record declares a table's columns: {@code String}, {@code long}/{@code int},
* {@code double}/{@code float}, {@code boolean}, {@code byte[]}, {@code UUID}, {@code Instant},
* and enums. Primitive components cannot be null. Table and column names are {@code snake_case}
* ({@code lastPaid} is stored as {@code last_paid}).
*
* <p>Calls block. On the main thread they are only allowed during startup ({@code onLoad});
* once the server runs they throw an {@link IllegalStateException}, so database I/O never costs
* tick time. Use {@code modThread()} or {@code async()}.
*/
public interface ModStorage
{
/** Opens the table {@code name} with schema version 1 and no migrations. */
<R extends Record> Table<R> table(String name, Class<R> type);
/** A table with a schema version and migrations; see {@link TableDefinition}. */
<R extends Record> TableDefinition<R> define(String name, Class<R> type);
/**
* Runs {@code work} in one transaction: writes of this thread inside it are committed
* together, or not at all when {@code work} throws.
*/
void transaction(Runnable work);
/** Like {@link #transaction(Runnable)}, returning the result of {@code work}. */
<T> T transaction(Supplier<T> work);
}
@@ -0,0 +1,35 @@
package dev.overlord.zomboidforge.api.storage;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
/**
* One row of a table in its stored form, given to migrations: the key and the columns by
* their {@code snake_case} names. Integers and booleans are stored as {@code Long}, other
* numbers as {@code Double}, texts (also UUIDs, instants, and enum constants) as
* {@code String}, bytes as {@code byte[]}.
*/
public interface Row
{
String key();
/** The names of the stored columns. */
Set<String> columns();
/** The stored value, or {@code null} when it is null. @throws IllegalArgumentException for an unknown column */
@Nullable Object get(String column);
@Nullable String getString(String column);
/** @throws IllegalStateException when the value is null */
long getLong(String column);
/** @throws IllegalStateException when the value is null */
double getDouble(String column);
/** @throws IllegalStateException when the value is null */
boolean getBoolean(String column);
byte @Nullable [] getBytes(String column);
}
@@ -0,0 +1,14 @@
package dev.overlord.zomboidforge.api.storage;
import java.io.Serial;
/** A storage operation failed: a database error, or a table that does not match its record. */
public final class StorageException extends RuntimeException
{
@Serial
private static final long serialVersionUID = 1L;
public StorageException(String message) { super(message); }
public StorageException(String message, Throwable cause) { super(message, cause); }
}
@@ -0,0 +1,52 @@
package dev.overlord.zomboidforge.api.storage;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
/**
* Rows of type {@code R}, each under a {@code String} key. Every write commits immediately,
* unless it runs inside {@link ModStorage#transaction}. Calls block; on the main thread they
* are only allowed during startup.
*/
public interface Table<R extends Record>
{
/** Stores {@code value} under {@code key}, replacing an existing row. */
void set(String key, R value);
/** Stores every entry in one transaction. */
void setAll(Map<String, R> values);
Optional<R> get(String key);
/** The rows of the given keys that exist. */
Map<String, R> getAll(Collection<String> keys);
boolean contains(String key);
/** @return whether a row was removed */
boolean delete(String key);
/** @return the number of rows removed */
int deleteAll(Collection<String> keys);
/** Every key, sorted. */
List<String> keys();
/** Calls {@code action} for every row, sorted by key, without loading all rows at once. */
void forEach(BiConsumer<String, R> action);
long count();
/**
* Replaces the row under {@code key} with {@code change} applied to it, or to
* {@code initial} when there is none, in one transaction: no other write comes in between.
*
* @return the stored value
*/
R update(String key, UnaryOperator<R> change, Supplier<R> initial);
}
@@ -0,0 +1,33 @@
package dev.overlord.zomboidforge.api.storage;
import java.util.function.Function;
/**
* A table to open with a schema version and migrations:
*
* <pre>{@code
* Table<Balance> balances = this.storage().define("balances", Balance.class)
* .version(2)
* .migrate(1, row -> new Balance(row.getLong("coins"), null))
* .open();
* }</pre>
*/
public interface TableDefinition<R extends Record>
{
/** Schema version of the record, at least 1. Default: 1. */
TableDefinition<R> version(int version);
/**
* How to turn a row stored with schema version {@code from} into the current record. Needed
* when the stored version is older than {@link #version} or the record changed in a way that
* cannot be applied without data.
*/
TableDefinition<R> migrate(int from, Function<Row, R> migration);
/**
* Creates the table, or checks the stored one against the record and migrates it.
*
* @throws StorageException when the stored table does not match and no migration applies
*/
Table<R> open();
}
@@ -0,0 +1,10 @@
/**
* Per-world storage for mods: record tables in a SQLite database next to the save, kept
* apart by mod id.
*
* @noinspection UnstableApiUsage, RedundantSuppression
*/
@NotNullByDefault
package dev.overlord.zomboidforge.api.storage;
import org.jetbrains.annotations.NotNullByDefault;