Skip to content

Add StarMapAfterSave and StarMapAfterLoad hooks - #85

Open
LaurensDeV wants to merge 1 commit into
StarMapLoader:mainfrom
LaurensDeV:feature/save-and-load-hooks
Open

LaurensDeV wants to merge 1 commit into
StarMapLoader:mainfrom
LaurensDeV:feature/save-and-load-hooks

Conversation

@LaurensDeV

@LaurensDeV LaurensDeV commented Aug 6, 2026 •

Copy link
Copy Markdown

The problem

A mod that keeps per-save state has no way to be told when a save is written or loaded. KSA raises no event for either, so the only signal is polling universe.xml's timestamp, which misses a save and load that happen inside one poll.

The change

Two lifecycle attributes, following the existing ones:

[StarMapAfterSave]
public void OnSaved(DirectoryInfo saveDirectory);

[StarMapAfterLoad]
public void OnLoaded(DirectoryInfo saveDirectory);

Both pass the save's directory, because a save is a directory and what a mod usually wants is a file beside universe.xml.

  • StarMapAfterSave is a postfix on UncompressedSave.Make(string), which every save, overwrite and save-as goes through. It is not called when the write fails (Make returns null).
  • StarMapAfterLoad fires after UncompressedSave.Load(), but only when that load reached Program.OnGameLoaded(). Load returns early when a save cannot be read, so a postfix on it alone would report loads that never happened; OnGameLoaded is called only once Universe.DeserializeSave has rebuilt the world.

How it is wired

  • Rebased onto main and moved onto the new PatchManager. SaveLoadPatches registers the patches once, owned by StarMap itself (mod null), like the menu and restart patches, and dispatches to every mod's attributed methods through ModRegistry. Registering per mod would take each mod's one slot on these methods, so a mod could not then patch Load itself.
  • Each mod's callback is invoked in its own try/catch, because these run inside KSA's save and load: one mod throwing must not stop the others or break the save.
  • If any of the three methods is missing in a future KSA build, StarMap logs a line and runs without that hook rather than failing at startup.
  • docs/modding/lifecycle.md lists both attributes.

Verification

  • Builds with no warnings against KSA 2026.10.19.5638.
  • The three targets are the methods KSArmory has patched in play since 2026-10 (its SaveHook and WorldReloadHook), checked against the decompiled 5638 source.
  • Not yet run in game as a StarMap build.

Building note: the Debug configuration on main does not reference Planet.Core.dll or Planet.Render.Core.dll, which ConfirmRestart needs, so Debug does not compile against a plain Import/ folder. I added those two references from outside the repository to build; this PR does not change the csproj.

@github-actions
github-actions Bot changed the base branch from main to feature/external/pr-85-feature-save-and-load-hooks August 6, 2026 16:39
@LaurensDeV
LaurensDeV marked this pull request as ready for review August 6, 2026 16:42
A mod that keeps per-save state has no way to be told when a save is
written or loaded, so it has to poll universe.xml's timestamp, which
misses a save and load inside the poll window.

Both hooks pass the save's directory, because a save is a directory and
what a mod usually wants is a file beside universe.xml.

AfterSave is a postfix on UncompressedSave.Make, which every save,
overwrite and save-as goes through; it is skipped when the write fails.

AfterLoad fires after UncompressedSave.Load, but only when that load
reached Program.OnGameLoaded. Load returns early when a save cannot be
read, so a postfix on it alone would report loads that never happened.

Registered once through the patch manager rather than per mod, so the
hooks do not take a mod's own slot on these methods, and each mod's
callback is isolated so one exception does not stop the rest or reach
KSA's save path.
@LaurensDeV
LaurensDeV force-pushed the feature/save-and-load-hooks branch from eacf529 to 16f248b Compare October 25, 2026 12:17
@LaurensDeV
LaurensDeV changed the base branch from feature/external/pr-85-feature-save-and-load-hooks to main October 25, 2026 12:17

This branch has not been deployed

No deployments
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