Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | 296x 7x 296x 296x 2969x 2969x 2969x 2969x 2969x 2969x 2969x 2969x 2969x 2969x 2969x 2969x 2969x 2969x 195x 195x 195x 195x 195x 195x 195x 195x 172656x 172223x 172223x 1x 172222x 172656x 9x 172222x 316x 172222x 781x 172222x 5142x 5140x 13x 13x 13x 12x 12x 1x 11x 11x 5x 4x 4x 4x 7x 6x 6x 5x 5x 5x 8x 7x 6x 6x 2778x 2777x 2778x 2777x 2777x 2x 2777x 2777x 2x 2777x 2777x 485x 2777x 9x 2777x 2777x 31x 31x 31x 2777x 2777x 2x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 14x 2777x 9x 2777x 2777x 10x 2777x 33324x 2777x 13885x 2777x 3x 2777x 2777x 2777x 6x 2777x 2777x 19x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2778x 2969x 2969x 2969x 2969x 2969x 2969x 127870x 127870x 127870x 127870x 28311x 28311x 27947x 27947x 127870x 127870x 2969x 5292x 2969x 544x 2969x 2969x 133706x 2969x 2969x 2969x 317x 2969x 60x 2969x 2969x 2969x 2969x 2969x 4015x 4015x 4015x 2969x 2969x 2969x 74x 2969x 2x 2969x 1x 2969x 2969x 2969x 2969x 2969x 461x 2969x 2969x 2969x 2969x 592x 2969x 452x 2969x 1287x 2969x 16x 2969x 17x 2969x 2969x 2969x 2969x 2969x 2969x 2780x 2780x 2780x 189x 189x 2904x 2886x 18x 18x 1x 5749x 5749x 1x 2793x 2793x 2793x 2793x 350x 2793x 1393x 2793x 153x 2793x 160418x 89413x 71005x 70925x 80x 80x 334757x | import {
hasChartPlayOptions,
type ChartOptionProgram,
type ChartPlayOptionProgram,
type PlayerChartPlayOptions,
} from '@be-music/chart-model';
import type { BeatorajaCustomJudge } from '../ruleset/custom-judge.ts';
import type { EffectiveBattleMode } from '../core/battle-mode.ts';
import type { ChartPlayVariant } from '@be-music/chart-model';
import type { LR2ExtraLevel } from '../core/lr2-extra-lanes.ts';
import type { ScrollTimelinePoint } from '@be-music/chart-model';
import type { ScrollModifier } from '../core/scroll-modifier.ts';
import type { LongNoteModifier } from '../core/long-note-modifier.ts';
import { resolveReplaySourceChannel } from './source-channel.ts';
import type { MineModifier } from '../core/mine-modifier.ts';
import type { ScratchSpin } from '../core/scratch-spin.ts';
import { createInputSourceTable, type InputSource } from '../core/input-source.ts';
import type { ReplayAnalogScratch } from './input-sources.ts';
import {
diffReplayOptions,
parseReplayOptionState,
type ReplayOptionState,
type ReplayOptionChange,
} from './options.ts';
import type { ReplayTempoChange, ReplayViewChange, ReplayViewState } from './view.ts';
import {
computeChartFingerprint,
computePlayFingerprint,
hasControlFlow,
resolveChart,
type RandomDraws,
} from '@be-music/chart-model';
import { resolveBMSControlFlowWithDraws } from '@be-music/chart-parser/core';
import { resolveBMSBase, type BeMusicJSON } from '@be-music/chart-source';
import type { PlayerSummary } from '../core/engine.ts';
import type { PreparedPlaybackChartData } from '../core/bootstrap.ts';
import { resolveChartJudgeRankFacts } from '../ruleset/index.ts';
import { resolveLandmineGaugeEffect } from '../core/landmine.ts';
import { resolve____RankLabel } from '../ruleset/score.ts';
import type { GaugePick, GaugeAutoShift, GaugeShiftBottom } from '../ruleset/gauge-type.ts';
import type { RulesetId } from '../ruleset/definitions.ts';
import packageJSON from '../../package.json' with { type: 'json' };
import {
BE_MUSIC_REPLAY_FORMAT,
BE_MUSIC_REPLAY_VERSION,
type BeMusicReplay,
type ReplayChart,
type ReplayCourseStage,
type ReplayInputAction,
type ReplayControlInputEvent,
type ReplayInputEvent,
type ReplayNote,
type ReplayPlay,
type ReplayRecorderInfo,
type ReplayRulesetResult,
} from './format.ts';
/** Ruleset id used for the engine's own summary cached into `results` at record time. */
export const REPLAY_NATIVE_RULESET_ID = 'be-music/native';
/** `@be-music/player-engine` version stamped into `recorder.engine` of every log this engine records. */
export const REPLAY_ENGINE_VERSION: string = packageJSON.version;
const ENGINE_VERSION = REPLAY_ENGINE_VERSION;
/**
* Chart data the recorder snapshots — structurally a subset of the engine's `PreparedPlaybackChartData`, so the
* engine can hand its prepared bundle over verbatim.
*/
export type ReplayRecorderChartData = Pick<
PreparedPlaybackChartData,
'notes' | 'landmineNotes' | 'invisibleNotes' | 'activeFreeZoneChannels' | 'scorableNotes' | 'laneDisplayMode'
> &
Partial<Pick<PreparedPlaybackChartData, 'resolvedChart'>>;
export interface ReplayRecorderPlaySettings {
/** Absent retains beatoraja-compatible bmson zero-total recovery. */
bmsonTotalMode?: import('../ruleset/bmson-total.ts').BmsonTotalMode;
options?: ReplayOptionState;
mode: 'manual' | 'auto';
autoScratch: boolean;
judgeAlgorithm?: 'combo' | 'duration' | 'lowest';
lr2Gambol?: 1 | 2;
customJudge?: BeatorajaCustomJudge;
longNoteModifier?: LongNoteModifier;
longNoteRate?: number;
sevenToNinePattern?: import('../core/seven-to-nine.ts').SevenToNinePattern;
sevenToNineType?: import('../core/seven-to-nine.ts').SevenToNineType;
sevenToNineThresholdBPM?: number;
sevenToNineApplied?: boolean;
battleMode?: EffectiveBattleMode;
battleSourceVariant?: ChartPlayVariant;
battleTempo?: number;
battleAssist1P?: boolean;
battleAssist2P?: boolean;
battleChangedObjectCount?: number;
battleDroppedObjectCount?: number;
dBattleScratch?: boolean;
lr2ExtraMode?: boolean;
lr2ExtraLevel?: LR2ExtraLevel;
lr2Mania?: import('./format.ts').ReplayLR2Mania;
lr2ExtraEffectiveLevel?: LR2ExtraLevel;
lr2ExtraConvertedObjectCount?: number;
lr2ExtraTempo?: number;
extraNoteDepth?: number;
extraNoteScratch?: boolean;
/** Actual source conversions, including overwritten additions; independent of record policy. */
extraNoteChangeCount?: number;
longNoteModifierChangeCount?: number;
mineModifier?: MineModifier;
scrollModifier?: ScrollModifier;
scrollSection?: number;
scrollRate?: number;
scrollModifierTimeline?: ScrollTimelinePoint[];
scrollModifierChanged?: boolean;
mineModifierChangeCount?: number;
autoScratch1P?: boolean;
autoScratch2P?: boolean;
/** ANY KEY routed the play's presses (`PlayerOptions.anyKey`). */
anyKey?: boolean;
/** The gauge the engine ran (`PlayerOptions.gauge`, else `recordReplay.gauge`); `GROOVE` when neither is set. */
gauge?: GaugePick;
randomLane?: { p1?: string; p2?: string };
/** The player's CRAZY selections the play ran with (`ReplayPlay.playerChartPlayOptions`); recorded when not empty. */
playerChartPlayOptions?: PlayerChartPlayOptions;
dpFlip?: boolean;
judgeWindowOverrideMs?: number;
/** Judgment-window ruleset the engine ran (`PlayerOptions.judgeRuleset`). Absent = `'lr2'`. */
judgeRuleset?: RulesetId;
/** The play's `#RANDOM` / `#SWITCH` outcomes (`PlayerOptions.randomDraws`). Recorded when non-empty. */
randomDraws?: RandomDraws;
/** The play ended the moment its survival gauge emptied (`PlayerOptions.endOnFailure`). Recorded when true. */
endOnFailure?: boolean;
gaugeAutoShift?: GaugeAutoShift;
bottomShiftableGauge?: GaugeShiftBottom;
/** The course stage the play was (`PlayerOptions.course`). */
course?: ReplayCourseStage;
/** The play tempo (`PlayerOptions.speed`). Recorded when it is not 1. */
tempo?: number;
/** What the player sees at the start (`PlayerOptions.recordReplay.view`): hi-speed, covers and lift. */
view?: ReplayViewState;
/** How the host turned an analog turntable axis into presses and releases (`ReplayPlay.analogScratch`). */
analogScratch?: ReplayAnalogScratch;
native?: Record<string, string | number | boolean | null>;
}
/**
* Host-declared subset of the recording inputs — the fields the engine cannot know by itself. Passed through
* `PlayerOptions.recordReplay`. `chartSHA256` is the SHA-256 (lowercase hex) of the source chart FILE bytes;
* hosts that have the raw file compute it so dropped logs can be matched back to their chart by content. `chartMD5` is
* the MD5 of the same bytes, the key LR2 and LR2IR use.
* `sourceChart` is the chart as loaded, before `#RANDOM` / `#SWITCH` resolution and before any lane option; hosts that
* resolve control flow or rewrite lanes on the chart themselves pass it so the fingerprints describe the chart, not
* their working copy. Without it the engine uses the chart it was given. `recorder` identifies the host application;
* the engine version is added by the recorder itself.
*/
export type ReplayRecordingOptions = Pick<
ReplayRecorderPlaySettings,
'gauge' | 'randomLane' | 'playerChartPlayOptions' | 'dpFlip' | 'view' | 'native' | 'options' | 'analogScratch'
> & {
chartSHA256?: string;
chartMD5?: string;
sourceChart?: BeMusicJSON;
recorder?: Omit<ReplayRecorderInfo, 'engine'>;
/** Record each input source's device name (`ReplayRecorderOptions.includeDeviceNames`). Off by default. */
includeDeviceNames?: boolean;
};
export interface ReplayRecorderOptions {
chartOptions?: ChartOptionProgram;
/** The chart-authored gameplay options the play ran with; recorded when it forces anything. */
chartPlayOptions?: ChartPlayOptionProgram;
/** Resolved chart JSON (post `#RANDOM` control flow) — metadata source. */
json: BeMusicJSON;
chart: ReplayRecorderChartData;
/** SHA-256 (lowercase hex) of the source chart file bytes, when the host computed one. */
chartSHA256?: string;
/** MD5 (lowercase hex) of the source chart file bytes, when the host computed one. */
chartMD5?: string;
/**
* The chart file's extension (`PlayerOptions.laneModeExtension`), the fingerprint's lane-mode input. Without it the
* recorder infers `.pms` from a 9 KEY lane mode.
*/
laneModeExtension?: string;
/**
* The chart before `#RANDOM` / `#SWITCH` resolution and before any lane option — the fingerprint source. Used when
* `play.randomDraws` were drawn for its control flow; otherwise the fingerprints fall back to {@link json}.
*/
sourceChart?: BeMusicJSON;
play: ReplayRecorderPlaySettings;
/** True when the ruleset the play ran under keeps LR2's money score; the native result then carries `score`. */
moneyScore?: boolean;
/** Host application that records the play. `engine` is always overwritten with this package's version. */
recorder?: Omit<ReplayRecorderInfo, 'engine'>;
/**
* Record each input source's device name (`InputSource.deviceName`, a browser's `Gamepad.id`) in
* `play.inputSources`. Off by default, and only the player turns it on: a device name can identify the player's
* hardware, and judging never reads it.
*/
includeDeviceNames?: boolean;
/** Dynamic `#EXRANKxx` changes the engine collected (chart order). */
dynamicJudgeRankChanges?: ReadonlyArray<{ seconds: number; exRankValue: number }>;
/** A stitched NONSTOP chart's later stages taking over the judge rank (`PlayerPlaySettings.stageRanks`). */
stageRanks?: ReadonlyArray<{
seconds: number;
rank: number;
table: 'chart' | 'lr2-nonstop' | 'bmson' | 'bms-exrank' | 'bms-exrank-change';
}>;
/** LR2 negative-BPM reversal (chart seconds, #134) — recorded so re-simulations freeze judging at the same point. */
reversalSeconds?: number;
/** Clock source for `createdAt` — injectable for tests. Defaults to `Date`. */
now?: () => Date;
}
export interface ReplayRecorderFinalizeInput {
summary: PlayerSummary;
maxCombo?: number;
aborted?: boolean;
/** Chart seconds the judge had settled through when the play was aborted. */
abortedAtSeconds?: number;
completedAtReversal?: boolean;
exitedReversal?: boolean;
reversalActivationSeconds?: number;
/** The 2P player of a two-player play: its summary, max combo and gauge (recorded as `results.secondPlayer`). */
secondPlayer?: { summary: PlayerSummary; maxCombo: number; gauge: GaugePick };
}
export interface ReplayRecorder {
/** Discards recorded data and ignores later input after a practice seek. Finalization is no longer allowed. */
discard(): void;
/**
* Records a press or release from `source` (numbered into `play.inputSources`); `inferred` marks a release the engine
* inferred from input without releases, `spin` the way a scratch press turned the turntable, `code` the physical
* input (`ReplayInputEvent.code`). An input without a source is recorded under {@link UNKNOWN_INPUT_SOURCE}.
*/
recordInput(
action: ReplayInputAction,
timeSeconds: number,
source: InputSource | undefined,
channels: Iterable<string>,
inferred?: boolean,
spin?: ScratchSpin,
code?: string,
): void;
/** Counts an LR2-style empty POOR — cached into the native result for diagnostics. */
recordEmptyPoor(): void;
/** Records a press or release of a control button (`ReplayControlInputEvent`) at a chart time. */
recordControlInput(action: ReplayInputAction, timeSeconds: number, button: string, code?: string): void;
/** Records a view change (hi-speed, covers, lift) at a chart time. An empty change is dropped. */
recordViewChange(timeSeconds: number, change: ReplayViewState): void;
/** Records selected options at the chart clock; repeated values are dropped. */
recordOptionChange(timeSeconds: number, values: ReplayOptionState): void;
/** Records a tempo change at a chart time. A change to the tempo already in force is dropped. */
recordTempoChange(timeSeconds: number, tempo: number): void;
finalize(input: ReplayRecorderFinalizeInput): BeMusicReplay;
}
/**
* The source an input that named none is recorded under (a host or a test that drives the engine without sources). A
* controller button, so a scratch hold it starts plays the lenient single-source rule, as an unknown source does.
*/
export const UNKNOWN_INPUT_SOURCE: InputSource = { kind: 'button', device: 'controller', label: 'unknown' };
export function createReplayRecorder(options: ReplayRecorderOptions): ReplayRecorder {
const chart = buildReplayChart(options);
const inputs: ReplayInputEvent[] = [];
const inputSources = createInputSourceTable({ deviceNames: options.includeDeviceNames === true });
let discarded = false;
let seq = 0;
let emptyPoorCount = 0;
const viewChanges: ReplayViewChange[] = [];
const controlInputs: ReplayControlInputEvent[] = [];
const initialOptions = options.play.options === undefined ? undefined : parseReplayOptionState(options.play.options);
let currentOptions = { ...initialOptions };
const optionChanges: ReplayOptionChange[] = [];
const tempoChanges: ReplayTempoChange[] = [];
let currentTempo = options.play.tempo ?? 1;
return {
discard: () => {
discarded = true;
inputs.length = 0;
inputSources.clear();
viewChanges.length = 0;
controlInputs.length = 0;
optionChanges.length = 0;
tempoChanges.length = 0;
emptyPoorCount = 0;
},
recordInput: (action, timeSeconds, source, channels, inferred, spin, code): void => {
if (discarded) return;
const channelList = [...channels];
if (channelList.length === 0) {
return;
}
const event: ReplayInputEvent = {
seq: seq++,
timeUs: secondsToMicroseconds(timeSeconds),
action,
channels: channelList,
source: inputSources.idOf(source ?? UNKNOWN_INPUT_SOURCE),
};
if (code !== undefined && code.length > 0) {
event.code = code;
}
if (inferred === true && action === 'up') {
event.inferred = true;
}
if (spin !== undefined && action === 'down') {
event.spin = spin;
}
inputs.push(event);
},
recordEmptyPoor: (): void => {
if (discarded) return;
emptyPoorCount += 1;
},
recordOptionChange: (timeSeconds, values): void => {
Iif (discarded) return;
const change = diffReplayOptions(currentOptions, parseReplayOptionState(values));
if (!change) return;
const timeUs = secondsToMicroseconds(timeSeconds);
if (!Number.isSafeInteger(timeUs) || timeUs < (optionChanges.at(-1)?.timeUs ?? 0))
throw new RangeError('Option changes must use nondecreasing chart times.');
optionChanges.push({ timeUs, values: change });
currentOptions = { ...currentOptions, ...change };
},
recordControlInput: (action, timeSeconds, button, code): void => {
if (discarded || button === '') return;
const event: ReplayControlInputEvent = {
seq: controlInputs.length,
timeUs: secondsToMicroseconds(timeSeconds),
action,
button,
};
if (code !== undefined && code.length > 0) event.code = code;
controlInputs.push(event);
},
recordViewChange: (timeSeconds, change): void => {
if (discarded) return;
const entries = Object.entries(change).filter(([, value]) => value !== undefined);
if (entries.length === 0) return;
const recorded: ReplayViewChange = { timeUs: secondsToMicroseconds(timeSeconds), ...change };
if (change.covers !== undefined) recorded.covers = change.covers.map((cover) => ({ ...cover }));
viewChanges.push(recorded);
},
recordTempoChange: (timeSeconds, tempo): void => {
if (discarded) return;
if (!(tempo > 0) || tempo === currentTempo) return;
currentTempo = tempo;
tempoChanges.push({ timeUs: secondsToMicroseconds(timeSeconds), tempo });
},
finalize: ({
summary,
maxCombo,
aborted,
abortedAtSeconds,
completedAtReversal,
exitedReversal,
reversalActivationSeconds,
secondPlayer,
}): BeMusicReplay => {
if (discarded) throw new Error('Cannot finalize a discarded practice replay');
const play: ReplayPlay = {
inputSources: inputSources.list(),
mode: options.play.mode,
autoScratch: options.play.autoScratch,
gauge: options.play.gauge ?? 'GROOVE',
};
if (secondPlayer !== undefined) play.gauge2P = secondPlayer.gauge;
if (options.play.randomLane !== undefined) play.randomLane = options.play.randomLane;
if (
options.play.playerChartPlayOptions !== undefined &&
Object.keys(options.play.playerChartPlayOptions).length > 0
)
play.playerChartPlayOptions = { ...options.play.playerChartPlayOptions };
if (options.play.dpFlip !== undefined) play.dpFlip = options.play.dpFlip;
if (options.play.judgeWindowOverrideMs !== undefined) {
play.judgeWindowOverrideMs = options.play.judgeWindowOverrideMs;
}
if (options.play.judgeRuleset !== undefined) play.judgeRuleset = options.play.judgeRuleset;
if (options.play.randomDraws !== undefined && options.play.randomDraws.length > 0) {
play.randomDraws = options.play.randomDraws.map((draw) => ({ ...draw }));
}
if (
reversalActivationSeconds !== undefined &&
Number.isFinite(reversalActivationSeconds) &&
chart.reversalTimeUs !== undefined
) {
chart.reversalActivationUs = secondsToMicroseconds(reversalActivationSeconds);
}
if (completedAtReversal) play.completedAtReversal = true;
if (aborted === true) {
play.aborted = true;
if (exitedReversal) play.exitedReversal = true;
Eif (abortedAtSeconds !== undefined) play.abortedAtUs = secondsToMicroseconds(abortedAtSeconds);
}
if (options.play.bmsonTotalMode !== undefined) play.bmsonTotalMode = options.play.bmsonTotalMode;
if (options.play.bottomShiftableGauge !== undefined)
play.bottomShiftableGauge = options.play.bottomShiftableGauge;
if (options.play.gaugeAutoShift !== undefined) play.gaugeAutoShift = options.play.gaugeAutoShift;
if (options.play.endOnFailure === true) play.endOnFailure = true;
if (options.play.course !== undefined) play.course = structuredClone(options.play.course);
if (options.play.tempo !== undefined && options.play.tempo !== 1) play.tempo = options.play.tempo;
if (tempoChanges.length > 0) play.tempoChanges = tempoChanges.map((change) => ({ ...change }));
if (options.play.view !== undefined) play.view = structuredClone(options.play.view);
if (options.play.judgeAlgorithm !== undefined) play.judgeAlgorithm = options.play.judgeAlgorithm;
if (options.play.lr2Gambol !== undefined) play.lr2Gambol = options.play.lr2Gambol;
Iif (options.play.customJudge !== undefined) play.customJudge = { ...options.play.customJudge };
if (options.play.scrollModifier !== undefined) play.scrollModifier = options.play.scrollModifier;
if (options.play.scrollSection !== undefined) play.scrollSection = options.play.scrollSection;
if (options.play.scrollRate !== undefined) play.scrollRate = options.play.scrollRate;
if (options.play.scrollModifierTimeline !== undefined)
play.scrollModifierTimeline = options.play.scrollModifierTimeline.map((point) => ({ ...point }));
if (options.play.scrollModifierChanged !== undefined)
play.scrollModifierChanged = options.play.scrollModifierChanged;
if (options.play.mineModifier !== undefined) play.mineModifier = options.play.mineModifier;
if (options.play.mineModifierChangeCount !== undefined)
play.mineModifierChangeCount = options.play.mineModifierChangeCount;
for (const field of [
'sevenToNinePattern',
'sevenToNineType',
'sevenToNineThresholdBPM',
'sevenToNineApplied',
'battleMode',
'battleSourceVariant',
'battleTempo',
'battleAssist1P',
'battleAssist2P',
'battleChangedObjectCount',
'battleDroppedObjectCount',
'dBattleScratch',
] as const)
if (options.play[field] !== undefined) Object.assign(play, { [field]: options.play[field] });
for (const field of [
'lr2ExtraMode',
'lr2ExtraLevel',
'lr2ExtraEffectiveLevel',
'lr2ExtraConvertedObjectCount',
'lr2ExtraTempo',
] as const)
if (options.play[field] !== undefined) Object.assign(play, { [field]: options.play[field] });
if (options.play.lr2Mania !== undefined)
play.lr2Mania = { ...options.play.lr2Mania, changes: { ...options.play.lr2Mania.changes } };
if (options.play.extraNoteDepth !== undefined) play.extraNoteDepth = options.play.extraNoteDepth;
if (options.play.extraNoteScratch !== undefined) play.extraNoteScratch = options.play.extraNoteScratch;
if (options.play.extraNoteChangeCount !== undefined)
play.extraNoteChangeCount = options.play.extraNoteChangeCount;
if (options.play.longNoteRate !== undefined) play.longNoteRate = options.play.longNoteRate;
if (options.play.longNoteModifierChangeCount !== undefined)
play.longNoteModifierChangeCount = options.play.longNoteModifierChangeCount;
if (options.play.longNoteModifier !== undefined) play.longNoteModifier = options.play.longNoteModifier;
if (options.play.autoScratch1P !== undefined) play.autoScratch1P = options.play.autoScratch1P;
if (options.play.autoScratch2P !== undefined) play.autoScratch2P = options.play.autoScratch2P;
if (options.play.anyKey === true) play.anyKey = true;
if (initialOptions !== undefined) play.options = { ...initialOptions };
if (optionChanges.length > 0) play.optionChanges = structuredClone(optionChanges);
if (viewChanges.length > 0) play.viewChanges = structuredClone(viewChanges);
if (controlInputs.length > 0) play.controlInputs = controlInputs.map((input) => ({ ...input }));
if (options.play.analogScratch !== undefined) play.analogScratch = { ...options.play.analogScratch };
if (options.play.native !== undefined) play.native = options.play.native;
const createdAt = (options.now?.() ?? new Date()).toISOString();
return {
format: BE_MUSIC_REPLAY_FORMAT,
version: BE_MUSIC_REPLAY_VERSION,
createdAt,
recorder: { ...options.recorder, engine: ENGINE_VERSION },
clock: { unit: 'us', origin: 'chart-zero' },
chart,
inputs,
play,
results: {
// Two players share the recorder: each side's empty POORs are its own summary's.
native: buildNativeResult(
summary,
maxCombo,
secondPlayer === undefined ? emptyPoorCount : summary.emptyPoor,
play.gauge,
options.moneyScore === true,
),
...(secondPlayer !== undefined
? {
secondPlayer: buildNativeResult(
secondPlayer.summary,
secondPlayer.maxCombo,
secondPlayer.summary.emptyPoor,
secondPlayer.gauge,
options.moneyScore === true,
),
}
: {}),
},
};
},
};
}
function buildReplayChart(options: ReplayRecorderOptions): ReplayChart {
const { json, chart } = options;
const idBase = resolveBMSBase(json);
const resolved = chart.resolvedChart ?? resolveChart(json);
const chartLNMode = resolved.lnMode;
const notes: ReplayNote[] = [];
for (const note of chart.notes) {
const isFreeZone = chart.activeFreeZoneChannels.has(note.channel);
const hasTail =
typeof note.endSeconds === 'number' && Number.isFinite(note.endSeconds) && note.endSeconds > note.seconds;
const entry: ReplayNote = {
id: 0,
channel: note.channel,
...(resolveReplaySourceChannel(note) !== note.channel ? { sourceChannel: resolveReplaySourceChannel(note) } : {}),
type: isFreeZone ? 'freezone' : hasTail ? 'long' : 'normal',
timeUs: secondsToMicroseconds(note.seconds),
};
if (hasTail) {
entry.endTimeUs = secondsToMicroseconds(note.endSeconds!);
if (!isFreeZone) {
entry.lnMode = note.longNoteMode ?? chartLNMode;
if (note.generatedLongNote) entry.generatedLongNote = true;
}
}
if (note.event.lrx?.chain === true && !isFreeZone) entry.uncounted = true;
notes.push(entry);
}
for (const mine of chart.landmineNotes) {
notes.push({
id: 0,
channel: mine.channel,
...(resolveReplaySourceChannel(mine) !== mine.channel ? { sourceChannel: resolveReplaySourceChannel(mine) } : {}),
type: 'mine',
timeUs: secondsToMicroseconds(mine.seconds),
damage: resolveLandmineGaugeEffect(mine.event, idBase, json.sourceFormat).damage,
...(mine.silent ? { silent: true } : {}),
});
}
for (const invisible of chart.invisibleNotes) {
notes.push({
id: 0,
channel: invisible.channel,
...(resolveReplaySourceChannel(invisible) !== invisible.channel
? { sourceChannel: resolveReplaySourceChannel(invisible) }
: {}),
type: 'invisible',
timeUs: secondsToMicroseconds(invisible.seconds),
});
}
notes.sort(compareReplayNotes);
for (let index = 0; index < notes.length; index += 1) {
notes[index]!.id = index;
}
const judgeRank: ReplayChart['judgeRank'] = resolveChartJudgeRankFacts(json, resolved);
const timelineSource = options.dynamicJudgeRankChanges ?? [];
if (timelineSource.length > 0) {
judgeRank.timeline = timelineSource.map((change) => ({
timeUs: secondsToMicroseconds(change.seconds),
exRankValue: change.exRankValue,
}));
}
if (options.stageRanks !== undefined && options.stageRanks.length > 0) {
judgeRank.stageRanks = options.stageRanks.map((change) => ({
timeUs: secondsToMicroseconds(change.seconds),
rank: change.rank,
table: change.table,
}));
}
const result: ReplayChart = {
sourceFormat: json.sourceFormat === 'bmson' || json.sourceFormat === 'lrx' ? json.sourceFormat : 'bms',
laneMode: chart.laneDisplayMode,
lnMode: chartLNMode,
judgeRank,
noteCount: chart.scorableNotes.length,
notes,
};
Eif (Number.isFinite(json.metadata.bpm)) result.headerBPM = json.metadata.bpm;
let minBPM = Infinity;
let maxBPM = -Infinity;
for (const { bpm } of resolved.timing.tempoPoints) {
Iif (!Number.isFinite(bpm)) continue;
minBPM = Math.min(minBPM, bpm);
maxBPM = Math.max(maxBPM, bpm);
}
Eif (minBPM <= maxBPM) result.bpmRange = { min: minBPM, max: maxBPM };
if (json.bmson.info.totalUnit !== undefined) result.totalUnit = json.bmson.info.totalUnit;
if (typeof options.reversalSeconds === 'number' && Number.isFinite(options.reversalSeconds)) {
result.reversalTimeUs = secondsToMicroseconds(options.reversalSeconds);
}
if (typeof options.chartSHA256 === 'string' && options.chartSHA256.length > 0) {
result.sha256 = options.chartSHA256.toLowerCase();
}
if (typeof options.chartMD5 === 'string' && options.chartMD5.length > 0) {
result.md5 = options.chartMD5.toLowerCase();
}
const { chartFingerprint, playFingerprint } = computeReplayFingerprints(options);
if (chartFingerprint !== undefined) result.chartFingerprint = chartFingerprint;
if (playFingerprint !== undefined) result.playFingerprint = playFingerprint;
if (typeof json.metadata.title === 'string' && json.metadata.title.length > 0) result.title = json.metadata.title;
if (typeof json.metadata.subtitle === 'string' && json.metadata.subtitle.length > 0) {
result.subtitle = json.metadata.subtitle;
}
if (typeof json.metadata.artist === 'string' && json.metadata.artist.length > 0) result.artist = json.metadata.artist;
if (typeof json.metadata.genre === 'string' && json.metadata.genre.length > 0) result.genre = json.metadata.genre;
const playLevel = json.metadata.playLevel;
if (
(typeof playLevel === 'number' && Number.isFinite(playLevel)) ||
(typeof playLevel === 'string' && playLevel !== '')
) {
result.playLevel = String(playLevel);
}
if (typeof json.metadata.difficulty === 'number' && Number.isFinite(json.metadata.difficulty)) {
result.difficulty = json.metadata.difficulty;
}
if (resolved.total.value !== undefined) {
result.total = resolved.total.value;
}
if (
options.chartOptions &&
(Object.keys(options.chartOptions.initial).length > 0 || options.chartOptions.cues.length > 0)
)
result.chartOptions = structuredClone(options.chartOptions);
if (hasChartPlayOptions(options.chartPlayOptions))
result.chartPlayOptions = structuredClone(options.chartPlayOptions);
return result;
}
/**
* Chart and play fingerprints (`bmfp0:…`) of the play. Their lane-mode input is the chart's extension, which the
* engine passes as `laneModeExtension`; without it, a 9 KEY play stands in for `.pms` (it is 9 KEY exactly when the
* chart is `.pms` or its content says so).
*
* The source chart rebuilds both whatever the host did to its working copy (a DP flip baked into the chart JSON, the
* control flow resolved away), provided `play.randomDraws` were drawn for its control flow. Without a usable source the
* engine's chart gives the play fingerprint, and the chart fingerprint only when the play has no draws — a chart with
* control flow has none to resolve there, so its chart fingerprint is unknown and omitted. A failure only drops a
* field — it must never break recording.
*/
function computeReplayFingerprints(options: ReplayRecorderOptions): {
chartFingerprint?: string;
playFingerprint?: string;
} {
const chartPath =
options.laneModeExtension ?? (options.chart.laneDisplayMode.startsWith('9 KEY') ? '.pms' : undefined);
const draws = options.play.randomDraws ?? [];
const source = options.sourceChart;
const played = source !== undefined ? tryResolveSource(source, draws) : undefined;
if (source !== undefined && played !== undefined) {
return {
chartFingerprint: tryFingerprint(() => computeChartFingerprint(source, { chartPath })),
playFingerprint: tryFingerprint(() => computePlayFingerprint(played, { chartPath })),
};
}
const playFingerprint = tryFingerprint(() => computePlayFingerprint(options.json, { chartPath }));
return draws.length === 0 ? { chartFingerprint: playFingerprint, playFingerprint } : { playFingerprint };
}
/** The source chart resolved with the play's draws, or `undefined` when they were not drawn for its control flow. */
function tryResolveSource(source: BeMusicJSON, draws: RandomDraws): BeMusicJSON | undefined {
if (!hasControlFlow(source)) {
return draws.length === 0 ? source : undefined;
}
try {
return resolveBMSControlFlowWithDraws(source, draws);
} catch {
return undefined;
}
}
function tryFingerprint(compute: () => string): string | undefined {
try {
return compute();
} catch {
return undefined;
}
}
function buildNativeResult(
summary: PlayerSummary,
maxCombo: number | undefined,
emptyPoorCount: number,
declaredGauge: GaugePick,
moneyScore: boolean,
): ReplayRulesetResult {
// Reported the way the simulators report a ruleset result: the judgment count as `noteCount`, the DJ LEVEL, a money
// score only where the ruleset has one, and the mid-play death flag.
const result: ReplayRulesetResult = {
ruleset: REPLAY_NATIVE_RULESET_ID,
judge: {
pgreat: summary.perfect,
great: summary.great,
good: summary.good,
bad: summary.bad,
poor: summary.poor,
emptyPoor: emptyPoorCount,
},
fast: summary.fast,
slow: summary.slow,
exScore: summary.exScore,
noteCount: summary.total,
maxCombo: maxCombo ?? 0,
djLevel: resolve____RankLabel(summary.exScore, summary.total),
gauge: {
type: summary.gauge?.type ?? declaredGauge,
...(summary.gauge?.source !== undefined ? { source: summary.gauge.source } : {}),
...(summary.gauge?.values ? { values: { ...summary.gauge.values } } : {}),
final:
summary.gauge?.max === 100
? summary.gauge.current
: summary.gauge !== undefined && summary.gauge.max > 0
? (summary.gauge.current / summary.gauge.max) * 100
: 0,
cleared: summary.gauge?.cleared ?? false,
},
};
if (summary.minePoor !== undefined) result.minePoor = summary.minePoor;
if (summary.fullCombo !== undefined) result.fullCombo = summary.fullCombo;
if (summary.gauge?.failedMidPlay === true) {
result.gauge.failedMidPlay = true;
}
if (moneyScore) {
result.score = summary.score;
}
if (summary.course !== undefined) {
result.course = { combo: summary.course.combo, maxCombo: summary.course.maxCombo };
}
return result;
}
function compareReplayNotes(left: ReplayNote, right: ReplayNote): number {
if (left.timeUs !== right.timeUs) {
return left.timeUs - right.timeUs;
}
if (left.channel !== right.channel) {
return left.channel < right.channel ? -1 : 1;
}
Eif (left.type !== right.type) {
return left.type < right.type ? -1 : 1;
}
return 0;
}
function secondsToMicroseconds(seconds: number): number {
return Math.round(seconds * 1_000_000);
}
|