WIP: feat(modapi): watch the game's event flows run: eventStarted, eventEnded and flowStep #3203
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3203
Loadingβ¦
Reference in a new issue
No description provided.
Delete branch "ChloeNow/Longshot:feat/modapi-event-flows-main"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
feat(modapi): watch the game's event flows run: eventStarted, eventEnded and flowStep
Summary
Part of #2783 (Ocarina Forge, from glinesbdev): a mod can now watch the game's event flows as they run, read-only.
longshot/gamegets three events:eventStarted/eventEnded: the event the game's event manager plays (a cutscene, a conversation, a door, amap's arrival), by its flowchart and entry point (
{ flow: "DefaultBootEvent", entry: "DefaultBootEvent" }). Anotherevent taking its place is an end, then a start, in one frame.
flowStep: every action and query step of every flow the game runs, in order, at the next frame: its flowchart,the step's name,
"action"or"query", the actor and its sub name, the action's or query's name, its parameters byname as the flowchart has them (numbers, booleans, texts, arrays), and a query's answer. At most 256 between frames.
What it is for: learning how the game's own flows work before patching one (which answer of a query means yes, what a
fade's
Lengthdoes, which flowchart a cutscene is), and following a cutscene. Nothing can be changed through it.How it works (
game_core::flows;docs/dev/reference/game-events.md#event-flowshas the layouts):FlowchartContext::ProcessContext(f_81248840, e0f3ccfa):flows.queryjustafter a query returned (its answer in
w0) andflows.actionbefore an action's lookup. Each copies the step (namesand parameters, bounded) from the work item and the
ResEventinto a queue the API takes once a frame. The sites areowned only while a mod listens to
flowStep.event's kind (+0x13c8 for the title's, +0x1398 for a save's load, +0x2828 for the boot's). It is found once an event
by the context's +0x48, which points back to it (searched from +0x1000 to +0x3000), and checked by the names it gives.
Its flowchart and entry point names come from the context's indices.
eventStarteduntil its firststep names it from the step's flowchart, a second at most; its
entryis then empty. One told with no name gets thename a later step gives at its
eventEnded.game.startEvent(flow, entry?),game.endEvent(),game.flowStep({ ... }).docs/modding/game-events.md#event-flows(and the fake game's rows), the dev reference's section, thegenerated reference.
Read first:
game/crates/game-core/src/flows.rs, then the firing ingame/crates/api/src/services/game.rs.Testing
Tested: Windows 11, NVIDIA GeForce RTX 4060 Laptop GPU.
game-coreflows::tests(new, 4): a step read from a made-up flowchart in fake memory (flowchart, event, actor,action and query names, Bool / Int / Float-array parameters), an event's context found by its self-pointer at
either offset (a decoy passed over), the queue kept only while watched and bounded at 256
(the rest counted), the fake game's event as the one playing.
modtooltesting_game::tests::event_flows_their_events_and_steps(new): a mod's listeners in the fake game seeeventStarted, steps with their parameters as JSON and a query's answer, another event's start as end + start, andeventEnded; an event with no name told once its first step names it, one with no step for a second told unnamedand named at its end.
In game (
dev.py game-tests, a test mod on thekokiri-startsave; passed):eventStartedTitleEvent:Title_Loopon the title screen and itseventEndedas the save is loaded, thenDefaultBootEvent:DefaultBootEventfor the save's load (26 frames) and its end.flowStepsaw the title's steps andthe load's on the flow worker thread, in the order the flowchart has them (its
Commonsubflow included), withparameters by name and the queries' answers (
EventQueryCheckPlayerStateState0 β 0, 2 β 1, 32 β 0).Played, with #3204 and the screen fade (a playtest mod: a tick per step, a chime per check, the scene's name and
steps on a card and in a list after it): the Kokiri shopkeeper's conversation, the character at the forest's
entrance, warps, the ocarina, and the target-shooting minigame (
MiniGame_HitTarget, 81 to 93 steps), each named(
Npc_Kokiri_001_Shop_Adult,MiniGame_HitTarget:Ready_Npc_Hylia_019_Shop_Child_Talk), every step listed with itsactor, parameters and answer. The playtest first found conversations and the minigame's results with no name (fixed
in
f513a6243).Still to test:
mise run test(CI).Measurements
Nothing to measure while no mod listens: the sites are not owned then. With a listener, each step is a few bounded
reads and a push under a lock (
flowStepis for watching, not for every player's mods).Risks
Two new engine sites in the flow runner: if their signatures stop matching on another build,
flowStepis absentthere (
has()false) and nothing else changes. The readers are bounded and never fault (an unreadable step isdropped).
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
Assisted-by:/Co-Authored-By:trailers.Ready to merge
π€ Generated with Claude Code
Tested in game: Windows 11, RTX 4060 Laptop GPU: dev.py game-tests with a test mod on the kokiri-start save (passed). eventStarted/eventEnded named TitleEvent:Title_Loop on the title screen and DefaultBootEvent:DefaultBootEvent for the save's load; flowStep saw both events' steps in the flowchart's order with their parameters and the queries' answers.
Possibly related, for context:
@review
Recommendation: Request changes. The flow-step availability check can report incomplete support, and watch lifetime and fake-game cases can give mods missing or incorrect events. The Windows game run shows the intended paths working, but does not cover these cases or the live paths on Linux.
Adds read-only event-flow notifications for mods and corresponding fake-game helpers. It makes flow inspection possible, at the cost of new worker-thread hooks and live-memory reads whose safety and event identity handling need work.
Blocking (1): must change before merging.
game/crates/api/src/services/features.rs:141: The common site check accepts any one listed site, but flowStep requires both to provide actions and queries. (more on the line) Do: Require both placed sites for flowStep and test each partial-placement case.Should fix (3): not blocking, but worth doing before merging.
game/crates/game-core/src/flows.rs:148: Disabling the watch clears the queue without synchronizing with callbacks that already passed the watch check. (more on the line) Do: Check the watch state while holding the queue lock, or associate queued steps with a watch generation and discard steps from older generations.game/crates/game-core/src/flows.rs:229: In the fake game, an explicitly ended event falls through to the fake world's EventMgr instead of reliably returning no event. (more on the line) Do: Make the fake implementation return the fake event state directly, including None, without falling back to the live-memory reader. Test endEvent while the fake world is in movie mode.game/crates/game-core/src/flows.rs:214: Pointer arithmetic happens before the checked memory reader, so a torn or invalid pointer can overflow rather than being rejected. (more on the line) Do: Pass offsets to the checked memory helpers or use checked_add for every pointer calculation before dereferencing it, and test wrapping pointers.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
game/crates/game-core/src/flows.rs:247: The new live-memory and worker-hook paths have only an in-game Windows run; their behavior on Linux remains untested. (more on the line) Do: Run the event and step listener in game on Linux, including a transition between events and an event end, and report the result.@fixcan do L1, L2, L3 and L4.Questions (2) (not confirmed from the code; they don't hold the recommendation)
game/crates/api/src/services/game.rs:315: Can EventMgr replace an event with a new instance at the same address between two frame samples?game/crates/game-core/src/flows.rs:250: Can EventMgr reuse an event address for a later event whose context is at a different offset?Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven for the full feature: the author reports a successful Windows game run for title and save-load events and their steps. Other event lifetimes and Linux have not been demonstrated.
Other systems: The hooks run on a flow worker and the event scan runs during frame processing. Windows guest heap pages may be decommitted; Linux and other event kinds have not been tested in game. GPU-specific rendering is not changed.
Switches: None.
Reviewed:
aebeded65fagainst18edd5097e: Read AGENTS.md, CONTRIBUTING.md, the pull request template, relevant standards, the commit, flows.rs, the feature dispatch and memory readers. Reviewed the changed API, fake-game, schema, site and documentation diffs; the larger surrounding files and generated reference were skimmed, not read in full. docs/review-checklist.md and ../Tooling/AGENTS.md were absent.; nothing run (a code review).Checked and fine:
Testing: No code or tests were run, as requested. The author reports Windows game and unit-test runs; CI is still running.
Overlaps and merge order: Unknown; network access was unavailable.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -138,6 +138,7 @@ fn sites_of(name: &str) -> &'static [&'static str] {"camera.override.lens" => &["camera.lens", "jitter.viewRows", "jitter.viewRowX", "jitter.viewRowY"],"camera.override.collide" => &["physics.stepped", "physics.steppedOther"],"game.songPlayed" => &["ocarina.melody"],"game.flowStep" => &["flows.query", "flows.action"],L1 (blocking): The common site check accepts any one listed site, but flowStep requires both to provide actions and queries.
When: On a build where only flows.action is placed, has("game.flowStep") returns true and a mod requiring it loads, but receives no query steps; the converse loses actions.
Do: Require both placed sites for flowStep and test each partial-placement case.
@ -0,0 +145,4 @@if WATCH_ON.swap(on, Ordering::AcqRel) == on {return;}if !on {L2 (should fix): Disabling the watch clears the queue without synchronizing with callbacks that already passed the watch check.
When: A worker passes the check, the last listener is removed and watch(false) clears the queue, then the worker pushes its old step. A later listener enables the watch and receives that step as though it occurred during its subscription.
Do: Check the watch state while holding the queue lock, or associate queued steps with a watch generation and discard steps from older generations.
@ -0,0 +211,4 @@0}/// A step read from its work item and event; `None` when its flowchart or actor cannot be read.L4 (should fix): Pointer arithmetic happens before the checked memory reader, so a torn or invalid pointer can overflow rather than being rejected.
When: If the actor-table pointer read from a live record is near u64::MAX and the actor index is nonzero, the addition overflows; checked builds panic in the hook and wrapping builds can read an unrelated low address. The same pattern appears in the context and flowchart binding calculations.
Do: Pass offsets to the checked memory helpers or use checked_add for every pointer calculation before dereferencing it, and test wrapping pointers.
@ -0,0 +226,4 @@kind,actor: text_at(actor, ACTOR_NAME),actor_sub: text_at(actor, ACTOR_SUB_NAME),name: text_at(verbs, verb * 8),L3 (should fix): In the fake game, an explicitly ended event falls through to the fake world's EventMgr instead of reliably returning no event.
When: If the fake world is in movie mode, its EventMgr event field is 1. Calling endEvent clears FAKE_PLAYING, after which playing() returns that sentinel as another event, so listeners see an unexpected start instead of just the end.
Do: Make the fake implementation return the fake event state directly, including None, without falling back to the live-memory reader. Test endEvent while the fake world is in movie mode.
@ -0,0 +244,4 @@let mgr = mem::read_ptr(holder, 0)?;let id = crate::layout::state::EVENTMGR_EVENT.read(mgr).filter(|&e| e != 0)?;let names = event_context(id).and_then(names_at);let (flow, entry) = names.unwrap_or_default();L5 (should fix): The new live-memory and worker-hook paths have only an in-game Windows run; their behavior on Linux remains untested.
When: A mod listening to these events on Linux exercises the worker hooks and event-context discovery, neither of which the reported game run covers there.
Do: Run the event and step listener in game on Linux, including a transition between events and an event end, and report the result.
Found in a playtest (a mod built on this PR, #3204 and the screen fade, played on Windows): some events reach mods with no name.
eventStarted/eventEndedgiveflow: "",entry: ""while their steps still come through.: 31 stepsevery time.Npc_Kokiri_001_Shop_Adult, 44 steps), unnamed on another talk (54 steps).Dm_SY_BancChangeGo/Arrive) were named.Why (a temporary log in the game): the event's flowchart context sits further in than the range searched (+0x2828 in the boot event, +0x2370 in a conversation; the range was +0x1000..+0x2000), and in a conversation its names are still empty when the event starts.
Fix coming to this branch: search to +0x3000, and when the context has no names yet, hold
eventStarted(10 frames at most) until the event's first step names it from its flowchart. The fake-game test covers both cases. I'll check it in game again before marking this ready.Pushed
f513a6243: every event is named now. The playtest's unnamed events (a conversation's context with no names yet as it starts, the boot event's context past the range searched, a minigame's results scene slow to its first step) are named from their first step's flowchart, and the search goes to +0x3000. Played again: the conversations and the minigame named. Testing updated.@rebase
I rebased this pull request onto
main(37f90245b0): the new head isd04fb3ee14.Conflicts resolved
docs/modding/game-events.md,game/modapi/schema/game.tomlNothing was built or run: read the resolution before merging.
Automated rebase by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
f513a62438d04fb3ee14Recommendation: Request changes. A build with only one flow hook reports flowStep as available even though it cannot deliver both actions and queries. The watch synchronization, fake-game end state and checked pointer arithmetic issues also remain unresolved.
Adds read-only event lifecycle and flow-step events for mods, with fake-game support and documentation. Windows playtesting shows the feature working; it adds live-memory reads and worker-thread hooks while watched.
Blocking (1): must change before merging.
game/crates/api/src/services/features.rs:141: The common site check accepts any one listed site, but flowStep requires both to provide actions and queries. When: On a build where only flows.action is placed, has("game.flowStep") returns true and a mod requiring it loads, but receives no query steps; the converse loses actions. Do: Require both placed sites for flowStep and test each partial-placement case.Should fix (3): not blocking, but worth doing before merging.
game/crates/game-core/src/flows.rs:148: Disabling the watch clears the queue without synchronizing with callbacks that already passed the watch check. When: A worker passes the check, the last listener is removed and watch(false) clears the queue, then the worker pushes its old step. A later listener enables the watch and receives that step as though it occurred during its subscription. Do: Check the watch state while holding the queue lock, or associate queued steps with a watch generation and discard steps from older generations.game/crates/game-core/src/flows.rs:229: In the fake game, an explicitly ended event falls through to the fake world's EventMgr instead of reliably returning no event. When: If the fake world is in movie mode, its EventMgr event field is 1. Calling endEvent clears FAKE_PLAYING, after which playing() returns that sentinel as another event, so listeners see an unexpected start instead of just the end. Do: Make the fake implementation return the fake event state directly, including None, without falling back to the live-memory reader. Test endEvent while the fake world is in movie mode.game/crates/game-core/src/flows.rs:214: Pointer arithmetic happens before the checked memory reader, so a torn or invalid pointer can overflow rather than being rejected. When: If the actor-table pointer read from a live record is near u64::MAX and the actor index is nonzero, the addition overflows; checked builds panic in the hook and wrapping builds can read an unrelated low address. The same pattern appears in the context and flowchart binding calculations. Do: Pass offsets to the checked memory helpers or use checked_add for every pointer calculation before dereferencing it, and test wrapping pointers.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
game/crates/game-core/src/flows.rs:247: The new live-memory and worker-hook paths have only an in-game Windows run; their behavior on Linux remains untested. When: A mod listening to these events on Linux exercises the worker hooks and event-context discovery, neither of which the reported game run covers there. Do: Run the event and step listener in game on Linux, including a transition between events and an event end, and report the result.@fixcan do L1, L2, L3 and L4.Since the last review (on
aebeded65f)Still open: L1 (The common site check accepts any one listed site, but flowStep requires both to provide aβ¦); L2 (Disabling the watch clears the queue without synchronizing with callbacks that already pasβ¦); L3 (In the fake game, an explicitly ended event falls through to the fake world's EventMgr insβ¦); L4 (Pointer arithmetic happens before the checked memory reader, so a torn or invalid pointerβ¦); L5 (The new live-memory and worker-hook paths have only an in-game Windows run; their behaviorβ¦)
Checks: Still running: 11 checks.
Also noticed (2; not asked for, not blocking)
More: does it help, other systems, switches, what was read
Does it help: Yes: the author reports Windows in-game runs showing named events, ordered steps, parameters and query answers, including conversations and a minigame. This is a feature rather than a performance change; its runtime cost has not been measured.
Other systems: The hooks and live-memory layouts can affect Linux as well as Windows; only Windows in-game results are reported. Event listeners now also activate the step hooks to obtain names.
Switches: None.
Reviewed:
d04fb3ee14against37f90245b0: Read AGENTS.md, CONTRIBUTING.md, the pull request template, standards, flows.rs and the feature availability logic; inspected the game service diff and event dispatch around the changed lines. The remaining changed files and generated documentation were not read in full.; nothing run (a code review).Checked and fine:
Testing: No commands that build or run project code were executed. The reported Windows playtest passed; Linux in-game coverage remains outstanding and CI is still running.
Overlaps and merge order: none found
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, after its rebase. Nothing was run; a maintainer decides.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.