feat(mods): flow patches, new events in an event flow #2656
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!2656
Loading…
Reference in a new issue
No description provided.
Delete branch "ChloeNow/Longshot:feat/mods-flow-patches-v2"
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?
Summary
A mod can't change one of the game's event flows (the cutscenes and scripted moments: a song played, a door's question,
a chest) without shipping a whole replaced copy of the game's file. That copy is game data, so it can't be shared, and
two mods changing one flow overwrite each other.
This adds a new kind of typed patch,
"flow", next toops,rows,placements,texts,flagsandrails, in a mod'spatches/folder or throughcontent.patch. It names the flow's own events, so the mod ships none of the game's file:action: a new action event, an actor and action the flow already has, with params, then its next event.branch: a check of a bool flag before an event. Whatever went to that event (events before it, a switch'scases, a fork's branches, an entry point) goes to the check; clear goes on as before, set goes to
then. Two checksbefore one event chain. A fork's join is never moved. The flag is the game's by its name, or a content flag by its id
(
":Sung"the mod's own,"owner/name:Sung"another's), as the runtime takes content.Each mod's new events are named after it (
local/my-mod.Tune: its id, a.and the op's), so mods never collide, and their patchesapply in the Mods list's order, each on the one before's output. An op naming something the flow doesn't have is left
out with its reason in the log; the rest apply, as with the other typed patches. Flow patches never conflict. The
target's own header (
BFEVFL) picks the applier: a flow patch aimed at another file fails by itself, and that file'sother patches still apply. A patched flow's cached file is named by each flow patch's mod id as well as its text, since
its events and flags are named after the mod.
Longshot writes the patched flow with its own writer: every byte of the game's file before its relocation table is
kept (a flow is taken only when its flowchart, events and the pointers its table names are all before that table), the
events move to a longer array after it, the new events' params (with a DIC built as Nintendo's tree, so the
game finds each param by name), cases and texts follow, then a new relocation table. The game's
EventQueryIsGameDataBoolanswers 0 for a set flag and 1 for a clear one, so a check's case 0 is "then". A textparam is at most 65535 bytes, the longest the format's u16 length holds.
Read first:
game/crates/content/src/evfl.rs(the reader, the ops and the writer), thenread_flow_opincontent/src/patch/parse.rsand the flow branch ofapplyincontent/src/merge.rs; the schema'sContentPatchFlowOptypes it forcontent.patch(the test game reads patches with the same reader). Docs:docs/modding/files.md, "Event flows".This is #2578 moved onto the current
mod-api(rewritten since), on a new branch so the pull request is only thischange; #2578 can close. It was #2268's change to the C mod API first. Part of #1958's Modding API; targets
mod-api.Magic Singing uses it with
songPlayed(#2658).Testing
Tested: Windows 11, NVIDIA GeForce RTX 4060 Laptop GPU, driver 621.24. In the game (a quick build of this head on
current
mod-api, which has the flags by id, #2657, andsongPlayed, #2658;dev.py game-tests, the repository'skokiri-start): atest mod with Magic Singing's 96-op patch on the ocarina's flow (
Dm_SY_Ocarina) and its two content flags. The patchapplies at the start with no op refused, the game loads the patched flow (
Dm_SY_Ocarina.bfevfl.zs <- modcache\files\... (made of the mods' copies)), and the mod's test passes in play. This build made the patched flowfresh, and it is byte for byte the file served on the previous
mod-api, which was read back with the evfl library: all 96 ops as written (the actions' actors, actions, params and next; eachcheck
EventQueryIsGameDataBoolon the mod's flag, case 0 tothen), the 330 original events unchanged but theirlinks, the 102 links into the 46 checked events moved to each chain's first check, no fork's join moved, the 20 entry
points as they were.
Checks:
cargo test -p content(evfl's own tests on a made-up flow, read back by a reader of the test's own: twomods' actions and checks in order, their params found by name through the DIC as the game looks them up, the bytes
kept, every pointer in the relocation table; a flag by a mod's id, the patching mod's own and another's; bad patch
files and a file that is not an event flow refused; the output past the largest flow refused; the DIC finding every
name of many; a flow whose flowchart is after its relocation table, or whose table names a pointer past the data,
refused; a text param of 65535 bytes applied whole and one of 65536 left out.
merge's tests: two mods' flow patchesserved in order; the same patch text under another mod's id made again for it; a flow patch aimed at a parameter file
failing alone, the file's other patch applied. Each of the review's four tests failed on the commit before the fix. The content vectors, which hold the reader to
the flow rules);
cargo check --workspace --all-targets;cargo run -p schema-gen -- --check; rustfmt.Still to test:
mise run rustandmise run test(by hand, or CI)songPlayedis inmod-apinow): a by-hand playtest, as a headlessrun cannot play the ocarina
Measurements
Nothing to measure in play: the patch is applied once at the start, with the merged files; a patched flow is a few
kilobytes larger than the game's.
Risks
Only mods that ship a
"flow"patch reach the new code; with none, nothing changes. A flow patch that routes the gamesomewhere wrong is the mod's to fix (turning the mod off restores the game's flow). The generated docs' schema hash
(
docs/modding/*.md) conflicts with any other schema change: whichever of this and #2657 merges second needscargo run -p schema-genagain.Checklist
Ready for review
mod-api; 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
tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
A "flow" patch adds events to an event flow (Event/EventFlow/<name>.bfevfl.zs) by the flow's own event names: "action" (an actor's action the flow has, its params, then next) and "branch" (a bool flag checked before an event: set, on to "then"; clear, on to "before"). The flag is the game's by its name or a content flag by its id (":Sung", "owner/name:Sung"); each op's id is the mod's own, its event named after the mod. content::evfl reads the BFEVFL (0x300/0x400), keeps every byte of the game's file and writes the new events, their params, names and the relocation table after it. merge applies a flow's patches in the mods' order, each on the previous one's output; an op that does not apply is left out and logged. Flow patches never conflict. The patch reader and the schema's ContentPatch take the new kind (the test game reads patches with the same reader); the content vectors hold it to the flow rules. Ported from the C mod API's PR #2268. Co-Authored-By: Claude Opus 5.5 <[email protected]>Tested in game: Windows 11, RTX 4060 Laptop, a quick build of current mod-api with this and the flags by id: a test mod with Magic Singing's 96-op patch on Dm_SY_Ocarina applies it with no op refused, the game makes and loads the patched flow, and the mod's test passes in play (dev.py game-tests, kokiri-start); the flow is byte for byte the one read back with the evfl library before.
@review
Recommendation: Request changes. Flow patches can reuse another mod’s cached output, and an invalid flow patch can suppress valid patches to the same file. Malformed flow data can also reach unchecked writes during startup.
Adds typed patches that let mods add actions and flag checks to event flows without distributing game data. The approach enables composition, but its cache key and input handling need correction before players can rely on it.
Blocking (4): must change before merging.
game/crates/content/src/merge.rs:869: The merged-file cache key does not include the mod IDs that determine flow event and flag names. (more on the line) Do: Include each flow patch’s owning mod ID in the cache key, and test switching between two owners with identical patch text.game/crates/content/src/merge.rs:860: Any flow patch selects the flow applier for the entire target, even when the target is a BYML or text file. (more on the line) Do: Select the applier from the target data or otherwise reject incompatible patches without suppressing compatible patches.game/crates/content/src/evfl.rs:341: The reader checks writable structures against the whole input, but the writer copies only the bytes before the relocation table. (more on the line) Do: Validate that the flowchart and every structure the writer modifies fit wholly before RELT, and reject invalid relocation pointers and ranges before writing.@​fixor@​rebasetry first). (for the author)Should fix (1): not blocking, but worth doing before merging.
game/crates/content/src/evfl.rs:669: String parameters can exceed the format’s 16-bit length, but the writer silently records 65535 for every longer string. (more on the line) Do: Reject string parameters that cannot be represented in the flow format, with a boundary test.Testing before merging (1) (the change behaves the same on every system: one run of the change itself is enough; it doesn't hold the recommendation)
@fixcan do L1, L2, L3 and L4.Checks: Still running:
Guide screenshots / Guide images and local UI source review (pull_request),Third-party parts / As their notes say (pull_request),Third-party parts / The same as upstream (pull_request).More: does it help, other systems, switches, what was read
Does it help: Unproven: the reported Windows play test loaded and played a 96-op patch with clear flags, but the set-flag route has not been played. This is a feature, not a performance change.
Other systems: The patcher runs on Windows and Linux and can process mod-supplied replacement files as well as game files. The reported game run covers Windows and one flow; Linux and the set-flag route remain untested. No renderer, audio, input, or save changes are apparent.
Switches: None.
Reviewed:
0fa1c22a4fagainst3391f978eb: Read the repository rules, available PR template, standards, commit, changed flow writer and patch integration, and their relevant callers. Reviewed the changed schema and documentation; skimmed generated documentation and content vectors. The Tooling checkout and docs/review-checklist.md were absent. Large existing files were read around the changed paths rather than in full.; nothing run (a code review).Checked and fine:
Testing: No commands from the project, builds, or tests were run; this was a read-only review. The author reports content tests, workspace checking, schema checking, formatting, and a Windows game run; Linux and the set-flag game run remain outstanding.
Overlaps and merge order: #2578 is the earlier version of this change and is identified for closure; #2657 supplies the flag-by-ID integration used in the reported game run and overlaps generated schema documentation.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -0,0 +338,4 @@f.nevents = usize::from(r16(b, f.fc + FC_EVENTS_N));f.nentries = usize::from(r16(b, f.fc + 0x18));let (actors, events, entries) = (r64(b, f.fc + FC_ACTORS), r64(b, f.fc + FC_EVENTS), r64(b, f.fc + FC_ENTRIES));if (f.nactors > 0 && !f.holds(actors, (f.nactors * ACTOR_SIZE) as u64))L3 (blocking): The reader checks writable structures against the whole input, but the writer copies only the bytes before the relocation table.
When: A malformed mod-supplied flow can place its flowchart after a valid RELT header while keeping it inside the input. flow_read accepts it, but a valid action op reaches write, which indexes the shorter pre-RELT copy at the flowchart offset and panics during startup.
Do: Validate that the flowchart and every structure the writer modifies fit wholly before RELT, and reject invalid relocation pointers and ranges before writing.
@ -0,0 +666,4 @@let str_at = o.room(2 + s.len() + 1)?;w64(&mut o.b, slot, str_at as u64);o.ptr(slot);w16(&mut o.b, str_at, u16_of(s.len()));L4 (should fix): String parameters can exceed the format’s 16-bit length, but the writer silently records 65535 for every longer string.
When: A patch under the 1 MiB patch limit can supply a 65536-byte action parameter. It is accepted and written in full with a 65535 length, so the game reads a different value from the one the mod supplied.
Do: Reject string parameters that cannot be represented in the flow format, with a boundary test.
@ -858,2 +858,3 @@let texts = reads.iter().any(|r| r.patch.kind == PatchKind::Texts);let (made, failures) = if texts {let flows = reads.iter().any(|r| r.patch.kind == PatchKind::Flow);let (made, failures) = if flows {L2 (blocking): Any flow patch selects the flow applier for the entire target, even when the target is a BYML or text file.
When: If one mod has a valid BYML patch and another mistakenly targets the same file with a flow patch, the flow reader rejects the BYML and the valid patch is also rejected instead of being applied.
Do: Select the applier from the target data or otherwise reject incompatible patches without suppressing compatible patches.
@ -860,0 +866,4 @@uses.iter().filter(|u| u.patch.kind == PatchKind::Flow).copied().collect();let index: Vec<usize> =uses.iter().enumerate().filter(|(_, u)| u.patch.kind == PatchKind::Flow).map(|(i, _)| i).collect();let out = crate::evfl::apply(base, &flow_uses, &mod_id);L1 (blocking): The merged-file cache key does not include the mod IDs that determine flow event and flag names.
When: Two mods ship identical flow patch text, including a branch on :Sung. After the first mod’s output is cached, replacing it with the second leaves the target, base and patch text unchanged, so serve reuses the first mod’s flow and checks the first mod’s flag.
Do: Include each flow patch’s owning mod ID in the cache key, and test switching between two owners with identical patch text.
0fa1c22a4ff277e0d263Review answered, on
f277e0d2(rebased on currentmod-api). The review's points are each a test first (9b3cbfa4, every one failing for its reason), then the fix (f277e0d2):BFEVFL) picks the applier, not the patches' kind. A flow patch aimed at any other file fails by itself ("is no event flow"), and that file's other patches still apply; the reverse is as before. Test: a parameter patch and a flow patch on one parameter file, the parameter patch applied. (Text patches still pick their applier by kind, as onmod-api; that rule is older than this PR.)f277e0d263611ec0d1a9Rebased on current
mod-api(f8193c1e):611ec0d1, only the generated docs made again.cargo test -p content(268, the review's four tests among them),-p game-core,-p api(the samemod-apifailure only),cargo check --workspace --all-targetsandschema-gen --checkpass.@review
Recommendation: Request changes. It conflicts with its base branch: bring it up to date (
@​fixor@​rebasetry first). (L6) The cache, applier, bounds and string-length fixes address the reported defects. The set-flag branch still needs an in-game playtest, and Forgejo reports the pull request as not mergeable.Flow patches let mods add actions and flag checks without distributing replacement game files. The fixes preserve mod-specific cache output, isolate patches aimed at the wrong file type, and reject the reported unsafe flow layouts and oversized strings.
Blocking (1): must change before merging.
@​fixor@​rebasetry first). (for the author)Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
Since the last review (on
0fa1c22a4f)Resolved: L1 (The merged-file cache key does not include the mod IDs that determine flow event and flag…); L2 (Any flow patch selects the flow applier for the entire target, even when the target is a B…); L3 (The reader checks writable structures against the whole input, but the writer copies only…); L4 (String parameters can exceed the format’s 16-bit length, but the writer silently records 6…)
Still open: L5 (The set-flag route of the new branch has not been exercised in the game.); L6 (It conflicts with its base branch: bring it up to date (
@​fixor@​rebasetry first).)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 that a 96-op patch applied and its test passed in play on Windows; the set-flag route remains untested in game. This is a feature, not a performance change.
Other systems: The writer runs during content merging on Windows and Linux and can affect patched flows and files targeted by mixed patch kinds. The reported Windows run and game-free tests cover the main path; Linux remains untested. GPU-specific behavior is not expected from this content change.
Switches: None.
Reviewed:
611ec0d1a9againstf8193c1ed1: Read the repository rules and pull request template, the flow reader, writer and operations relevant to the ledger, and the merge dispatch and cache paths. Skimmed the remaining diff, schema and generated documentation; the full changed code and recorded vectors were not read.; nothing run (a code review).Checked and fine:
Testing: No commands from the project were run. The author reports content tests, workspace checking, schema checking and a Windows game run; the set-flag playtest and Linux checks remain outstanding.
Overlaps and merge order: #2578 is described as superseded by this pull request; #2657 can conflict in generated schema documentation.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@rebase
I rebased this pull request onto
mod-api(67a113a710): the new head is3969aeb70d.Conflicts resolved
Nothing 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.
611ec0d1a93969aeb70dRecommendation: Approve. The code is ready: only testing is left. The set-flag route still needs an in-game playtest, and the pull request is reported as not mergeable despite the rebase. Recommend approval; not approved on the forge.
Flow patches let mods add actions and flag checks without distributing or replacing a whole game file. The change adds a flow reader and writer and makes patched files somewhat larger.
Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
Since the last review (on
611ec0d1a9)Resolved: L6 (It conflicts with its base branch: bring it up to date (
@​fixor@​rebasetry first).)Still open: L5 (The set-flag route of the new branch has not been exercised in the game.)
Checks: Still running:
PR description / Title and description (pull_request_target).More: does it help, other systems, switches, what was read
Does it help: Unproven for the complete feature: the reported Windows playtest loaded a 96-operation patch, but did not exercise a set flag routing to its target.
Other systems: The flow writer affects patched event flows on Windows and Linux; the reported playtest covers Windows. Unpatched files do not use the new flow writer.
Switches: None.
Reviewed:
3969aeb70dagainst67a113a710: Read AGENTS.md and CONTRIBUTING.md, the current diffs for the three regenerated documentation files, the commit list, and the relevant set-flag references in evfl.rs and files.md. The remaining changed code and generated files were not read in full in this re-check.; nothing run (a code review).Checked and fine:
Testing: No commands that build or run the project were executed. The author reports a Windows playtest and content tests; Linux and the set-flag playtest remain outstanding.
Overlaps and merge order: #2578 is the earlier version of this change; the description says it can close. #2657 and #2658 are related dependencies mentioned in the description.
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, after its rebase. Nothing was run; a maintainer decides.
WIP: feat(mods): flow patches, new events in an event flowto feat(mods): flow patches, new events in an event flow