All files / player-engine/src/course/nonstop bga-resources.ts

100% Statements 44/44
100% Branches 35/35
100% Functions 4/4
100% Lines 38/38

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                          414x 414x 414x 414x 414x 414x 13080x 13080x 2x 2x   13078x 13052x 13052x 13052x   13078x   414x 221x 219x 219x 13052x 13052x 13052x   13052x   160x 13052x 13052x 13052x   16x     13052x 13052x   13052x 12871x 12871x 13052x 12861x 12861x 12859x       219x      
import { parseBMSExBMP, parseBmsBGA } from '@be-music/chart-model';
import { resolveBMSBase, type BeMusicJSON } from '@be-music/chart-source';
import type { NonstopKeyAllocator } from './ids.ts';
 
/** One stage's BMP namespace, including image declarations and sub-region dependencies. */
export function createNonstopBMPAllocator(params: {
  source: BeMusicJSON;
  target: BeMusicJSON;
  stage: number;
  keys: NonstopKeyAllocator;
  stageKeys: string[];
  assetPath: (path: string) => string;
}): (key: string) => string | undefined {
  const { source, target, stage, keys, stageKeys, assetPath } = params;
  const visited = new Set<string>();
  const queue: Array<{ key: string; stitched: string }> = [];
  const base = resolveBMSBase(source);
  let exhausted = false;
  const allocate = (key: string): string | undefined => {
    const stitched = keys.keyFor(stage, key);
    if (stitched === undefined) {
      exhausted = true;
      return undefined;
    }
    if (!visited.has(key)) {
      visited.add(key);
      stageKeys.push(stitched);
      queue.push({ key, stitched });
    }
    return stitched;
  };
  return (key) => {
    if (exhausted) return undefined;
    const result = allocate(key);
    while (queue.length > 0 && !exhausted) {
      const entry = queue.pop()!;
      const exBMP = parseBMSExBMP(source.bms.exBMP[entry.key] ?? '');
      const explicit = source.resources.bmp[entry.key];
      const path =
        explicit ||
        exBMP?.filename ||
        (source.bmson.bga.header.some((header) => header.name === entry.key) ? entry.key : undefined);
      const mappedPath = path ? assetPath(path) : undefined;
      if (mappedPath) target.resources.bmp[entry.stitched] = mappedPath;
      if (exBMP) {
        // Keep the authored color fields; transparent-color rendering is a separate concern from resource remapping.
        target.bms.exBMP[entry.stitched] =
          `${exBMP.argbRaw} ${path === exBMP.filename ? mappedPath : assetPath(exBMP.filename)}`;
      }
      const argb = source.bms.argb[entry.key];
      if (argb !== undefined) target.bms.argb[entry.stitched] = argb;
      // A direct BMP/EXBMP resource overrides a same-slot BGA alias, as it does in standalone playback.
      if (path) continue;
      const raw = source.bms.bga[entry.key];
      const region = raw === undefined ? undefined : parseBmsBGA(raw, base, source.sourceFormat === 'bmson');
      if (!region) continue;
      const origin = allocate(region.sourceBMP);
      if (origin === undefined) break;
      target.bms.bga[entry.stitched] = [origin, region.sx, region.sy, region.ex, region.ey, region.dx, region.dy].join(
        ' ',
      );
    }
    return exhausted ? undefined : result;
  };
}