All files / player-browser/src/collection chart-siblings.ts

100% Statements 8/8
87.5% Branches 7/8
100% Functions 2/2
100% Lines 7/7

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                          225x               3x 2x 2x 2x 2x 2x    
import { resolveLRXHeaderPath, resolveLRXMoviePath } from '@be-music/chart-parser/core';
import { dirname } from '@be-music/shared-utils/core';
import type { ChartSiblingBytes } from '../compute/tasks.ts';
import { loadAssetBytes, lookupBytesCaseInsensitive } from './file-lookup.ts';
import type { BrowserAssetEntry } from './types.ts';
 
/**
 * The files a chart reads beside it, looked up in the dropped files: for an LRX `.lrs` chart, its `.lrh` header and
 * the `.lrm` BGA file the header (or the `.lrs`) names, both case-insensitively in the chart's folder. Other charts
 * read nothing else.
 */
/** Whether the chart at `chartPath` reads files beside it ({@link loadChartSiblings}): an LRX `.lrs` chart. */
export function hasChartSiblings(chartPath: string): boolean {
  return chartPath.toLowerCase().endsWith('.lrs');
}
 
export async function loadChartSiblings(
  files: ReadonlyMap<string, BrowserAssetEntry>,
  chartPath: string,
  chartBytes: Uint8Array,
): Promise<ChartSiblingBytes | undefined> {
  if (!hasChartSiblings(chartPath)) return undefined;
  const lrh = await loadAssetBytes(lookupBytesCaseInsensitive(files, resolveLRXHeaderPath(chartPath)));
  const folder = dirname(chartPath);
  const moviePath = resolveLRXMoviePath(chartBytes, lrh).replace(/\\/g, '/');
  const lrm = await loadAssetBytes(lookupBytesCaseInsensitive(files, folder ? `${folder}/${moviePath}` : moviePath));
  return { ...(lrh ? { lrh } : {}), ...(lrm ? { lrm } : {}) };
}