All files / player-engine/src/core lr2-sound-families.ts

96.29% Statements 26/27
94.73% Branches 18/19
100% Functions 3/3
100% Lines 23/23

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          40x 40x 40x 40x 1x     39x 191x 42x 42x 42x 191x     31x 61x 1x     60x   41x 41x 41x 41x 31x 31x   41x   38x    
import { evaluateControlFlow, type RandomDraws } from '@be-music/chart-model';
import type { BeMusicJSON } from '@be-music/chart-source';
 
/** LR2's loader groups WAV declarations by the first two original filename bytes, in active file order. */
export function resolveLR2SoundFamilies(chart: BeMusicJSON, draws: RandomDraws = []): number[] {
  const families = Array<number>(1296).fill(0);
  const groups = new Map<string, number>();
  const source = chart.preservation.bms.sourceLines;
  if (source.length === 0 && Object.keys(chart.resources.wav).length > 0)
    throw new RangeError(
      'LR2 sound-family conversion requires the original WAV declaration order; reload the source chart.',
    );
  for (const entry of evaluateControlFlow({ bms: { controlFlow: source } }, draws)) {
    if (entry.kind !== 'header' || !/^WAV[0-9A-Z]{2}$/i.test(entry.command)) continue;
    const sound = Number.parseInt(entry.command.slice(3), 36);
    const path = entry.value;
    let prefix = chart.bms.wavNameBytePrefixes?.[path];
    if (prefix === undefined) {
      // ASCII prefixes are identical in LR2's supported byte encodings. A text-only
      // caller must supply the original bytes for any other spelling.
      const first = path.slice(0, 2);
      if (!first || Array.from(first).some((character) => character.charCodeAt(0) > 127))
        throw new RangeError(
          'LR2 sound-family conversion requires original filename bytes; parse the source with parseBMSBytes.',
        );
      prefix = Array.from(first, (character) => character.charCodeAt(0).toString(16).padStart(2, '0')).join('');
    }
    Iif (!/^(?:[0-9a-f]{2}){1,2}$/i.test(prefix)) throw new RangeError('Invalid original WAV filename byte prefix.');
    prefix = prefix.toLowerCase();
    let group = groups.get(prefix);
    if (group === undefined) {
      group = groups.size;
      groups.set(prefix, group);
    }
    families[sound] = group;
  }
  return families;
}