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

+60
View File
@@ -75,6 +75,7 @@ Methods of `Mod`, usable from the constructor on (also in field initializers):
| `main()` | `MainThread` | code that touches game objects |
| `modThread()` | `ModThread` | the mod's own work, one task at a time |
| `async()` | `AsyncExecutor` | blocking work such as HTTP calls or file I/O |
| `storage()` | `ModStorage` | the mod's tables in the world's `zomboidforge.db` |
## Threads
@@ -108,6 +109,65 @@ Listeners are matched by the event's exact class. Events that carry live game ob
removed after ten failures; both `onMain` and `onModThread` return a `Subscription` to remove
it earlier.
## Storage
Mod data belongs in `storage()`: tables in `zomboidforge.db`, a SQLite database in the world's
save folder. Vanilla backups include it, and deleting the world deletes it. Each mod only
reaches its own tables.
A record declares a table's columns; each row has a `String` key:
```java
record Balance(long coins, @Nullable Instant lastPaid) {}
Table<Balance> balances = this.storage().table("balances", Balance.class);
balances.set("player1", new Balance(250, Instant.now()));
Optional<Balance> balance = balances.get("player1");
balances.update("player1", old -> new Balance(old.coins() + 10, Instant.now()), () -> new Balance(0, null));
this.storage().transaction(() -> { // all or nothing
balances.update("player1", old -> new Balance(old.coins() - 5, old.lastPaid()), () -> new Balance(0, null));
balances.update("player2", old -> new Balance(old.coins() + 5, old.lastPaid()), () -> new Balance(0, null));
});
```
| Component type | Stored as |
|-----------------------------------|-------------------------|
| `long`, `int`, `boolean` | integer, never null |
| `double`, `float` | real number, never null |
| `Long`, `Integer`, `Boolean`, ... | the same, may be null |
| `String`, `UUID`, enums | text (enums by name) |
| `Instant` | ISO-8601 text |
| `byte[]` | blob |
Table and column names are `snake_case`: `lastPaid` is stored as `last_paid`.
When the record changes between versions of the mod:
| Change | What happens |
|--------------------------------|-------------------------------------------|
| new component that may be null | added; existing rows read `null` |
| new primitive component | needs a migration |
| removed component | the column and its data stay; logged once |
| component with another type | needs a migration |
```java
Table<Balance> balances = this.storage().define("balances", Balance.class)
.version(2)
.migrate(1, row -> new Balance(row.getLong("coins"), null)) // from stored version 1
.open();
```
Things to know:
1. Storage calls block. On the main thread they are only allowed during `onLoad`; once the
server runs they throw, so use `modThread()` or `async()`.
2. Writes commit immediately, while the world is saved periodically. After a crash, the
world goes back to its last save, but the mod data keeps later writes.
3. When a mod's id changes, list the old ids in `mod.toml` as `previous_ids`; their tables
move to the new id once.
## Checks at startup
Before any mod code runs, ZomboidForge reads every class of the mod and its libraries. The