Skip to content

PWA3: Remember pinned work areas per settlement in a local file - #10

Merged
kramsey458 merged 3 commits into
mainfrom
claude/pins-local-file
Sep 22, 2026
Merged

kramsey458 merged 3 commits into
mainfrom
claude/pins-local-file

Conversation

@kramsey458

Copy link
Copy Markdown
Collaborator

Stacked PR. The base is claude/planting-tool-ranges (#9, PWA4), which is itself stacked on #6 (PWA1). Review and merge #6 and #9 first. This PR's diff is only the two PWA3 commits.

Summary

Pinned work areas used to reset on every load, map exit and co-op resync. With this PR, each settlement's pins are remembered on the player's own computer, in PersistentWorkAreas\Pins.txt next to the Timberborn Saves folder (on Windows, Documents\Timberborn\PersistentWorkAreas\Pins.txt). They come back when any save of that settlement loads.

Pins are never read from or written into the save, and no save, tick or persistence interface is added. Each co-op player therefore keeps their own pins, and nothing changes in what BeaverBuddies sends on a join or resync. The feature is display only.

Items

PWA3: restore pins from a local per-settlement file (stretch)

What changed

  • PinStore (new, Unity-free, linked into tests/Checks.csproj) defines the file format:
    • A header line, then one line per settlement: its escaped name, followed by the pinned buildings' EntityId GUIDs, separated by tabs.
    • The save keeps each building's entity id, so after a load a pin finds the reloaded building by id rather than by the old runtime object.
    • Settlement names must match exactly (ordinal comparison).
    • A missing, corrupt or unknown-format file remembers nothing. Damaged lines or ids are skipped.
    • Only the 100 most recently loaded or changed settlements are kept.
  • PinFile writes to a temporary file, then swaps it in with File.Replace or File.Move, so an interrupted write never leaves half a file. If the existing file has an unrecognized header (a newer format, or damage), it is first copied to Pins.txt.bak.
  • PinSet.Changed is set by every Set or Clear that changes the set. That covers pin, unpin, clear, deletion and dropped pins without separate bookkeeping.
  • WorkAreaService:
    • Restore. At PostLoad, pins are restored once through EntityRegistry.GetEntity plus Supports. Restoring does not count as a change, so the file keeps any remembered buildings this particular save lacks.
    • Write. The frame update writes the file after a change. A new game's pins wait until the settlement has a name. After a load, the first update also moves that settlement's entry to the top of the file, with its ids unchanged.
    • Failures. A failed write stays pending and is retried 10 seconds later, and again on leaving the map. Only the first failure per map is logged.
    • Leaving the map. Reset (called from Dispose on the loading screen, and on a renderer failure) saves any pending change first, then forgets the pins in memory only. Leaving the map never erases the file.
    • Testability. UpdateSingleton now only reads Time.unscaledTime and passes it to UpdateAt(now). RestorePins takes the pinnability test as a parameter. Both changes let the checks run the real wiring outside the game.
  • When file IO happens. Only at PostLoad, in the frame update, and in Dispose/Reset. OnDeleted, which can fire inside a tick, only sets the in-memory Changed flag.
  • Docs. README: where the file lives, what comes back, and co-op guests. VALIDATION: the persistence bullet, playtest steps 6, 9 and 10.

Tests that prove it (tests/Program.cs, 38 new checks)

  • Game-free (29):
    • PinSet.Changed marking.
    • Store round-trip, with each id written once.
    • A different settlement shows none (case and whitespace must match).
    • Other settlements are kept on write; clearing a settlement removes its entry.
    • Missing, empty, corrupt and unknown-header files all give no pins.
    • Damaged lines are skipped.
    • A hand-edited file that repeats a settlement uses the first line, with each id once.
    • Escaping of tabs, line breaks, backslashes and CJK names.
    • The 100-settlement cap, the most-recent ordering, and load-time move-to-top (a recently loaded settlement survives the cap).
    • Restore resolves by id, drops unknown ids, removes duplicates and respects Supports.
    • PinFile on a temporary folder: no file, round-trip with folder creation, move-to-top on disk, and a corrupt or newer-format file replaced with a .bak copy and no .tmp left behind.
  • On the live WorkAreaService instance, with a temporary pin file and the game's real SettlementReferenceService, SettlementReference and EntityRegistry types (9):
    • An unnamed settlement keeps the change pending.
    • A save is written under the settlement's name.
    • Clear defers the write, then forgets only this settlement.
    • Reset saves the pending change and never erases the file.
    • Restore through EntityRegistry pins only this settlement's pinnable buildings, adds them to the outline planner, and writes nothing at load.
    • The first update moves the entry to the top and keeps ids this save lacks.
    • UpdateAt saves even with nothing to draw, and retries a failed write after 10 seconds.
    • A check that no mod type implements a Timberborn save, persist or tick interface, and that the mod never references the game's save assemblies.

Fail before, pass after

  • Written first. The tests were written before the fix. Run against the unfixed code, the checks do not compile: PinStore, PinFile and PinSet.Changed are missing (CS0103, CS0246 and CS1061). Reverting only WorkAreaService.cs fails at the first live-instance pin check.
  • Mutation run on the final code. 20 mutants; 17 are killed. Examples of killed mutants:
    • restore ignores its test, leaves Changed set, or skips the planner;
    • the update never saves, or saves only while something is drawn;
    • a failed write is never retried, or retried without waiting;
    • Reset skips the pending save;
    • no move-to-top, or it rewrites only the resolved ids;
    • no .bak, or a .bak of every file;
    • the header is not checked;
    • the last duplicate line wins, or empty and duplicate ids are kept.
  • Survivors. Two need the game: PostLoad not calling RestorePins(Supports), or passing _ => true (in-game steps 1 and 4 below). The third is the temp-file swap, which cannot be checked cheaply.

Tests

Suite Before (base 7ec9ee2) After (bce50b0)
build.ps1 then tests/Checks.csproj with game files 97/97 135/135 (77 game-free, 58 need game files), 0 warnings

The checks write only to %TEMP%, never to the real Documents folder.

Co-op impact

  • There is no simulation or wire-format change. Co-op players do not need to update together.
  • Nothing is written into the save, and there are no Harmony patches, replay events or ticks.
  • Pins are keyed by the local SettlementReference.SettlementName:
    • On a rehost, BeaverBuddies saves under the host's own settlement, so the host's pins come back.
    • A guest's settlement is named after a hash of the host's save bytes (ClientConnectionService.LoadMap), so a guest's pins usually do not come back. The name changes with every resync. Rejoining the very same host save file would bring them back, which is harmless.
  • Restored pins are queried in UpdateSingleton through the same path as manual pins. Only the timing is new: the first frame after a load.

Proposed CHANGELOG lines

  • Pinned working areas are now remembered for each settlement, in PersistentWorkAreas\Pins.txt next to your Timberborn Saves folder, and come back when you load that settlement. They are never written into the save, so each co-op player keeps their own.

Needs in-game / two-player testing

  • Solo. Pin 2 buildings, save, exit to the main menu and load that save. Both outlines come back and the top-right button reads Clear pinned areas (2).
  • Solo. Load a different settlement. It shows no pins.
  • Solo. Unpin one of the two, save, exit and load again. Only the other comes back. Then delete that building, reload, and check there is no error and the button is hidden (0 pins).
  • Solo. Load an older save of the same settlement that lacks one pinned building. The remembered buildings that exist in it are pinned. Then load the newest save again: every pin comes back.
  • Solo, while still in the map. Pin a building. Within a second, its settlement's line in Pins.txt lists one more id. Press Clear pinned areas: the line disappears.
  • Solo. Pin a building that is already in your latest save, choose Exit to desktop without saving, relaunch and load that save. The pin comes back. This exit path skips the loading screen, so only the frame-update write covers it.
  • Solo. Start a new game, name it, pin a building, then exit and load its autosave. The pin comes back. Pins.txt exists, and no Pins.txt.tmp is left behind.
  • Solo. Player.log has no [PersistentWorkAreas] Pinned areas not ... warnings.
  • Two-player (BeaverBuddies). Pin different buildings on the host and the guest, then force a resync. The host's pins come back and the guest's are empty. Neither player's pins appear on the other's computer.

Review

Three adversarial reviewers approved (determinism and lockstep; save compatibility and cross-mod interaction; is the test real). Each rebuilt the branch and reproduced 126/126 at 30f2a64.

Fixed in bce50b0

  • The cap evicted by last change, not last use. Throwaway co-op guest entries could push out a settlement whose pins were set once and never changed. Loading now moves the settlement's entry to the top, with its ids unchanged.
  • The _pins.Changed = false at the end of restore was untested, and it protects the older-save behaviour. Also untested: restore feeding the planner, and the frame update saving at all. RestorePins now takes the pinnability test, and UpdateSingleton passes the clock to UpdateAt, so the live-instance checks run both. The reviewer's surviving mutants N2 to N6 are now killed.
  • A failed write was marked saved and not retried. It now stays pending and is retried after 10 seconds and on leaving the map.
  • An unknown-header file (for example from a later version) would be overwritten. It is now copied to Pins.txt.bak first, and the header comment says how a later format should stay readable.
  • Untested reading details. A repeated settlement line, and repeated or empty ids, now have a check.
  • README wording. The location is now given relative to the Saves folder, because macOS's UserDataFolder is ~/Documents/Documents/Timberborn. "A guest's pins do not come back" is softened to "usually".
  • Checklist gaps. Added the in-map file check, exit to desktop, and older-then-newest save.

Declined or deferred

  • Entity ids under BeaverBuddies are unique only within a timeline. GUIDs come from seeded Unity Random, so after hosting the same save twice, a remembered id could name a different building. This is unconfirmed, display only and rare. The cross-settlement "guest fallback" that would widen it was not adopted. Storing a template name next to each id was not added, because it needs live components to test. It is recorded here as a known limitation.
  • Two settlements with the exact same name share one entry. Pins are per settlement, not per save. Both are by design, as the brief specifies ("keyed by settlement name").
  • docs/ website. Not edited, because the site describes the released 1.0.0 build. Its last change was dfd40ac (Docs: finish the 1.0.0 wording pass #5), not the release commit as an earlier note said. At release, update these pages:
    • faq.html (#saves, "Pins aren't saved at all", and the reset answer);
    • index.html:167;
    • install.html:135, plus the Uninstalling paragraph at line 138, which should mention the PersistentWorkAreas pin folder;
    • troubleshooting.html:145,207.
      The "38 checks" counts in README and VALIDATION are also release-time edits (135 on this stack).

Merge notes for the other open PRs

  • CI: run the pin-logic checks without the game on GitHub Actions #7 (checks-ci). git merges it cleanly, and its args.Length == 0 guard lands correctly, after all game-free checks. This corrects an earlier note that said the PinStore checks would be skipped in CI. Its GameChecks = 27 must become 58 on this stack, or the full run fails its count guard. The no-argument run then reports 77 game-free checks.

  • PWA2: Localize the work-area panel and clear-button text #8 (localize-ui). It conflicts in source/WorkAreaService.cs: constructor parameters (ILoc plus EntityRegistry and SettlementReferenceService) and Notify. Its "No hard-coded English UI text" check will also flag this PR's non-UI literals:

    • the header and file names ("Persistent Work Areas pins v1", "PersistentWorkAreas", "Pins.txt", ".tmp", ".bak");
    • the escape strings;
    • the GUID format "D";
    • the ArgumentException text and its nameof;
    • the ": " in the two warnings.

    Whichever PR lands second must teach IsCode about these literals.

🤖 Generated with Claude Code

kramsey458 and others added 2 commits September 22, 2026 08:23
Pins used to reset on every load, map exit and co-op resync. They now come
back when any save of the same settlement loads. They are read from
Documents/Timberborn/PersistentWorkAreas/Pins.txt (the game's UserDataFolder),
never from or into the save, so each co-op player keeps their own and nothing
changes what BeaverBuddies sends on a join or resync.

- PinStore (Unity-free, linked into the checks) owns the file format: one line
  per settlement with its escaped name and the pinned buildings' entity ids.
  The save keeps each entity id, so a pin finds the reloaded building by id,
  not by the runtime object. A missing, corrupt or unrecognized file
  remembers nothing, and damaged lines are skipped. Only the 100 most
  recently changed settlements are kept, because a co-op guest's settlement
  is renamed after the host's map on every join.
- PinFile reads the file and swaps in a rewritten copy.
- PinSet now marks itself changed on every pin, unpin, clear or deletion.
  WorkAreaService writes the file from UpdateSingleton after a change (a new
  game's pins wait until the settlement is named), and restores the pins
  once at PostLoad through EntityRegistry.GetEntity and Supports. Leaving the
  map, loading and a renderer failure save a pending change first and never
  erase the file.
- No save, tick or persistence interface is added. A new check asserts that
  no mod type implements one and that the mod never references the game's
  save writers.

Display only: nothing reaches the simulation or the save.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…vice wiring

Review follow-ups for the per-settlement pin file.

- Loading a settlement now moves its entry to the top of the pin file (at
  the first update, with its remembered ids unchanged). The 100-settlement
  cap therefore evicts by last use, not last change, so throwaway co-op
  guest settlements no longer push out a settlement whose pins were set
  once and never changed.
- A failed write keeps the change pending and is retried 10 seconds later
  and again on leaving the map; only the first failure per map is logged.
  Before, the change was marked saved before the write, so one failure lost
  it until the next pin change.
- A pin file this version cannot read (a newer format, or damage) is copied
  to Pins.txt.bak before it is replaced, and the header comment says how a
  later format stays readable by this one.
- The service's own wiring is now checked outside the game. RestorePins
  takes the pinnability test (PostLoad passes Supports, which needs live
  game objects), and UpdateSingleton only reads Time.unscaledTime and hands
  it to UpdateAt. New checks on the live instance cover restore through the
  game's EntityRegistry (drawn, not counted as a change, nothing written at
  load), the first update keeping ids this save lacks, and the frame update
  saving with nothing to draw and retrying after a failure. New game-free
  checks cover the move-to-top, the cap by last use, the backup copy, and a
  hand-edited file with a repeated settlement and repeated or empty ids.
- README gives the location relative to the Saves folder (macOS differs)
  and says a guest's pins usually do not come back. VALIDATION adds the
  in-map file, exit-to-desktop and older-then-newest-save steps.

Display only: nothing reaches the simulation or the save.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@kramsey458
kramsey458 changed the base branch from claude/planting-tool-ranges to main September 22, 2026 16:43
- WorkAreaService constructor: keep PWA2's ILoc alongside PWA3's
  EntityRegistry and SettlementReferenceService, and keep the localized
  clear button next to the pin-file restore in PostLoad.
- tests/Program.cs: keep the PinStore checks above the game-free split,
  raise GameChecks to 65, and allow the pin file's format, file names
  and argument-check text in the hard-coded UI text check. Full run:
  142/142; no-argument run: 77 passed, 65 skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kramsey458
kramsey458 marked this pull request as ready for review September 22, 2026 16:44
@kramsey458
kramsey458 merged commit 6d531a6 into main Sep 22, 2026
1 check passed
@kramsey458 kramsey458 mentioned this pull request Sep 22, 2026
@kramsey458
kramsey458 deleted the claude/pins-local-file branch September 22, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant