import type { SingleEntityArgument } from 'sandstone/arguments' import type { MCFunctionClass, SandstoneCore } from 'sandstone/core' import { AwaitNode } from 'sandstone/core/nodes' import type { NonEmptyString } from 'sandstone/utils' const ATTACH_CHILD_NAME = '__attach' /** * Options for `_.await.attach(...)`. * * @param entrypoint `'start'` runs the attach's continuation before the * target's continuation would continue; uses a transient * area_effect_cloud mounted onto the target via `ride` * so `execute on passengers` runs commands as the cloud. * `'end'` runs the continuation the tick after the * attach call site, by scheduling the continuation one * tick out and tagging the entity that ran the attach. * @param func Root `MCFunction` of the target `AwaitNode`. * @param branch Planned `MCFunction` pre-optimizer-visitors path * relative to the root, empty if the `AwaitNode` was * added at the root. * @param awaitNodeIdx `N` of the `AwaitNode` within the above branch. * @param entity Required for `entrypoint: 'start'` against an * `asyncContext` MCFunction: the entity that owns the * transient area_effect_cloud passenger. */ export interface AttachOptions { entrypoint: 'start' | 'end' func: MCFunctionClass | NonEmptyString branch: string[] awaitNodeIdx: number entity?: SingleEntityArgument } /** * `await attach(...)` — synchronises a `MCFunction`'s continuation to * another `MCFunction`'s active `AwaitNode` at runtime. * * Implementation notes: * - The continuation MCFunction (`this.mcfunction`) is where the user's * post-attach code lands (via AwaitBodyVisitor's default * `awaitNode.mcfunction.node.body.push(...awaitNode.body)` path). * - The tick-body emission (the schedule + tag chain for `'end'`, or the * prelude MCFunction invocation for `'start'`) is generated by * `AttachVisitor` after AwaitBodyVisitor runs. * - `currentFunction.enterContext(this)` makes subsequent commands go * into `AttachClass.body`, which AwaitBodyVisitor then pushes into the * continuation. */ export class AttachClass extends AwaitNode { // `tag` — placeholder command; the actual tick body is emitted by // AttachVisitor. This field exists because AwaitNode is a // ContainerCommandNode and requires a `command` property. command = 'tag' as const public mcfunction public readonly targetFunc: MCFunctionClass | NonEmptyString public readonly targetBranch: string[] public readonly targetAwaitNodeIdx: number public readonly entrypoint: 'start' | 'end' public readonly entity?: SingleEntityArgument constructor(core: SandstoneCore, opts: AttachOptions, logPath: boolean) { super(core.pack) if (logPath) { // Reserved for future logPath support; matches UntilClass/SleepClass. ;(this as any).stackTrace = Error().stack } this.targetFunc = opts.func this.targetBranch = opts.branch this.targetAwaitNodeIdx = opts.awaitNodeIdx this.entrypoint = opts.entrypoint this.entity = opts.entity const currentFunction = core.getCurrentMCFunctionOrThrow() currentFunction.resource.nested += 1 const baseName = `${currentFunction.resource.path.slice(2).join('/')}/${ATTACH_CHILD_NAME}` // Continuation MCFunction: where post-attach code lands. asyncContext // inherited from the caller so user post-attach code can itself await. this.mcfunction = core.pack.MCFunction( `${baseName}/_continuation`, () => {}, { addToSandstoneCore: true, lazy: false, creator: 'sandstone', onConflict: 'rename', packType: currentFunction.resource.packType, asyncContext: currentFunction.resource.asyncContext, }, ) // Subsequent user code (e.g. `_.await.attach(...); say('hi')`) lands in // `this.body` via the context stack. AwaitBodyVisitor's default path // pushes `this.body` into `this.mcfunction.node.body` (the attach's // continuation MCFunction) so it runs in the target's flow at the // inject point. currentFunction.enterContext(this) core.awaitNodes.add(this) } }