feat(shared): add the worldstate tree for world-scope state #29

Open
Serkyo wants to merge 12 commits from feat/worldstate-tree into feat/entity-chunk-lifecycle
Owner

What does this PR do?

I added the container for world-scope state: one SYNW-framed file per domain under worldstate/, with the server code to load and save it. No domain ships yet, so nothing calls it.

  • crates/shared/src/save/worldstate.rs: new Domain, a lowercase [a-z0-9_] file stem plus the format version of its payload. It has no public constructor. Domain::new is a const fn that asserts the name is non-empty, uses only a-z, 0-9 and _, and is not a Windows reserved device name (con, nul, com1 and the like), so a malformed declaration stops the build. DOMAINS is the empty list of engine domains, and tests declare TEST_DOMAIN under cfg(test).
  • Same file: encode and decode, generic over a serde payload. The framing mirrors the player record (magic SYNW, the domain's u32 version, flags, two lengths, zstd postcard payload), with the version taken from the domain rather than a shared constant. decode refuses a wrong magic (BadMagic), any version other than the domain's (UnsupportedVersion), an uncompressed length over 64 MiB (DeclaredLengthTooLarge), a length that disagrees with the header, and bytes after the payload. encode refuses a payload decode would reject, so a save that succeeds can be read back. MAX_RECORD_LEN is the largest file decode can accept.
  • crates/shared/src/save/error.rs: new SaveError::TrailingBytes.
  • crates/shared/src/paths.rs: the public worldstate_dir is gone. worldstate_path(world, &Domain) and worldstate_path_in(world_dir, &Domain) are the only resolvers, so nothing outside paths.rs can name another file under worldstate/.
  • crates/server/src/save/worldstate_file.rs: load returns the payload's Default when the domain file is absent, and an error when it exists but does not decode, leaving the file untouched. It reads at most MAX_RECORD_LEN bytes. save writes through the existing atomic_write. Both carry a dead_code expectation with its reason, since no caller exists yet.
  • crates/server/Cargo.toml: the server now depends on serde, already a workspace dependency. shared gains none.
  • docs/save_format.md: the worldstate/ layout, its three properties, and the record limit under "Decoding untrusted records".

Why is this change necessary?

State that belongs to the world as a whole and grows with play, such as corruption zones, structure instances and settlement timestamps, has no home today and does not belong in level.dat. I wanted the container and its ownership rule in place before the first system that needs one, so that system does not set the layout for the rest.

It has three properties. A missing domain file loads as the domain's empty value, and only absence is empty: a decode error is never turned into a default, because the next save would then overwrite state that may still be recoverable. Each domain refuses any version other than its own with UnsupportedVersion, as ADR-0015 requires. And no code can obtain a worldstate/ path except through a declared domain.

Scope of Changes

server, shared, workspace

Testing

Automated, run on this branch:

  • cargo fmt --all -- --check: clean.
  • cargo clippy --workspace --exclude renderer --exclude client --all-targets --all-features -- -D warnings: clean.
  • cargo test --workspace --exclude renderer --exclude client --all-features --profile ci: all suites passed, 0 failed (net 48, scripting 25, server 116, shared 377). One server test, a_block_the_base_game_does_not_register_stays_an_orphan in level_file.rs, failed once under parallel load. This branch does not touch that file. It passes alone and on the base branch, and the full run passed three more times.

renderer and client were excluded because they cannot be built on the machine I ran this on (ninja is missing). This branch does not touch either crate.

The new tests cover a record round trip, and decode refusing a wrong magic, another version, an oversized declared length, a truncated record and trailing bytes. They also cover encode refusing an oversized payload, domain names (including the reserved Windows names), the path resolvers, an absent file loading as the default, and a corrupt file being an error that leaves the file untouched.

Manual: none. No domain ships, so there is nothing to exercise in a running game.

Additional Context

Stacks on #24 (feat/entity-chunk-lifecycle); review that one first.

  • This branch is stacked on feat/entity-chunk-lifecycle, which is an open pull request. Until that one merges, this diff against dev also shows its commits. The changes belonging to this PR are the twelve commits after it.
  • I removed the public worldstate_dir from paths.rs. Nothing else in the workspace called it.
  • The framing is mirrored from the player record, not extracted into a shared helper, so level.rs stays untouched.
  • No ADR is added. The container follows ADR-0015 and adds no new architectural decision.
  • There is no migrator. A migration that spans a domain version and world_format_version is not designed yet and is out of scope here.

None.

Checklist

  • I have branched from dev (or a feature branch off dev) and my PR targets dev.
  • I have kept my changes focused to a single concept.
  • I have added or updated documentation (/// doc comments for Rust) where necessary.
  • I have tested my changes and described any relevant automated or manual testing above.
### What does this PR do? I added the container for world-scope state: one `SYNW`-framed file per domain under `worldstate/`, with the server code to load and save it. No domain ships yet, so nothing calls it. - `crates/shared/src/save/worldstate.rs`: new `Domain`, a lowercase `[a-z0-9_]` file stem plus the format version of its payload. It has no public constructor. `Domain::new` is a `const fn` that asserts the name is non-empty, uses only `a-z`, `0-9` and `_`, and is not a Windows reserved device name (`con`, `nul`, `com1` and the like), so a malformed declaration stops the build. `DOMAINS` is the empty list of engine domains, and tests declare `TEST_DOMAIN` under `cfg(test)`. - Same file: `encode` and `decode`, generic over a `serde` payload. The framing mirrors the player record (magic `SYNW`, the domain's `u32` version, flags, two lengths, zstd postcard payload), with the version taken from the domain rather than a shared constant. `decode` refuses a wrong magic (`BadMagic`), any version other than the domain's (`UnsupportedVersion`), an uncompressed length over 64 MiB (`DeclaredLengthTooLarge`), a length that disagrees with the header, and bytes after the payload. `encode` refuses a payload `decode` would reject, so a save that succeeds can be read back. `MAX_RECORD_LEN` is the largest file `decode` can accept. - `crates/shared/src/save/error.rs`: new `SaveError::TrailingBytes`. - `crates/shared/src/paths.rs`: the public `worldstate_dir` is gone. `worldstate_path(world, &Domain)` and `worldstate_path_in(world_dir, &Domain)` are the only resolvers, so nothing outside `paths.rs` can name another file under `worldstate/`. - `crates/server/src/save/worldstate_file.rs`: `load` returns the payload's `Default` when the domain file is absent, and an error when it exists but does not decode, leaving the file untouched. It reads at most `MAX_RECORD_LEN` bytes. `save` writes through the existing `atomic_write`. Both carry a `dead_code` expectation with its reason, since no caller exists yet. - `crates/server/Cargo.toml`: the server now depends on `serde`, already a workspace dependency. `shared` gains none. - `docs/save_format.md`: the `worldstate/` layout, its three properties, and the record limit under "Decoding untrusted records". ### Why is this change necessary? State that belongs to the world as a whole and grows with play, such as corruption zones, structure instances and settlement timestamps, has no home today and does not belong in `level.dat`. I wanted the container and its ownership rule in place before the first system that needs one, so that system does not set the layout for the rest. It has three properties. A missing domain file loads as the domain's empty value, and only absence is empty: a decode error is never turned into a default, because the next save would then overwrite state that may still be recoverable. Each domain refuses any version other than its own with `UnsupportedVersion`, as ADR-0015 requires. And no code can obtain a `worldstate/` path except through a declared domain. ### Scope of Changes server, shared, workspace ### Testing Automated, run on this branch: - `cargo fmt --all -- --check`: clean. - `cargo clippy --workspace --exclude renderer --exclude client --all-targets --all-features -- -D warnings`: clean. - `cargo test --workspace --exclude renderer --exclude client --all-features --profile ci`: all suites passed, 0 failed (`net` 48, `scripting` 25, `server` 116, `shared` 377). One server test, `a_block_the_base_game_does_not_register_stays_an_orphan` in `level_file.rs`, failed once under parallel load. This branch does not touch that file. It passes alone and on the base branch, and the full run passed three more times. `renderer` and `client` were excluded because they cannot be built on the machine I ran this on (`ninja` is missing). This branch does not touch either crate. The new tests cover a record round trip, and decode refusing a wrong magic, another version, an oversized declared length, a truncated record and trailing bytes. They also cover encode refusing an oversized payload, domain names (including the reserved Windows names), the path resolvers, an absent file loading as the default, and a corrupt file being an error that leaves the file untouched. Manual: none. No domain ships, so there is nothing to exercise in a running game. ### Additional Context Stacks on #24 (feat/entity-chunk-lifecycle); review that one first. - This branch is stacked on `feat/entity-chunk-lifecycle`, which is an open pull request. Until that one merges, this diff against `dev` also shows its commits. The changes belonging to this PR are the twelve commits after it. - I removed the public `worldstate_dir` from `paths.rs`. Nothing else in the workspace called it. - The framing is mirrored from the player record, not extracted into a shared helper, so `level.rs` stays untouched. - No ADR is added. The container follows ADR-0015 and adds no new architectural decision. - There is no migrator. A migration that spans a domain version and `world_format_version` is not designed yet and is out of scope here. ### Related Issues None. ### Checklist - [x] I have branched from `dev` (or a feature branch off `dev`) and my PR targets `dev`. - [x] I have kept my changes focused to a single concept. - [x] I have added or updated documentation (`///` doc comments for Rust) where necessary. - [x] I have tested my changes and described any relevant automated or manual testing above.
All checks were successful
Auto Labeler / label-scope (pull_request_target) Successful in 2s
CLA Signed All authors have signed the CLA.
CLA Check / cla-check (pull_request_target) Successful in 2s
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feat/worldstate-tree:feat/worldstate-tree
git switch feat/worldstate-tree

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch feat/entity-chunk-lifecycle
git merge --no-ff feat/worldstate-tree
git switch feat/worldstate-tree
git rebase feat/entity-chunk-lifecycle
git switch feat/entity-chunk-lifecycle
git merge --ff-only feat/worldstate-tree
git switch feat/worldstate-tree
git rebase feat/entity-chunk-lifecycle
git switch feat/entity-chunk-lifecycle
git merge --no-ff feat/worldstate-tree
git switch feat/entity-chunk-lifecycle
git merge --squash feat/worldstate-tree
git switch feat/entity-chunk-lifecycle
git merge --ff-only feat/worldstate-tree
git switch feat/entity-chunk-lifecycle
git merge feat/worldstate-tree
git push origin feat/entity-chunk-lifecycle
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Synvael/synvael!29
No description provided.