All files / player-browser/src/skin/beatoraja chart-images.ts

97.43% Statements 38/39
88.46% Branches 23/26
100% Functions 4/4
100% Lines 30/30

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                            44x                           5x 4x   1x   1x   1x   1x                                 27x 6x 27x 27x 27x 8x 8x 5x 5x 5x 5x   1x 1x     1x             26x 9x 27x 9x         9x 9x 9x 9x 9x    
/**
 * A chart's own images as a beatoraja skin draws them: its `#STAGEFILE`, `#BACKBMP` and `#BANNER` in the synthetic
 * image slots -100, -101 and -102 (ModernChic paints the stage file behind the decide screen's title, the default skin
 * under the result's score readouts).
 */
 
import type { LibraryChart } from '@be-music/player-scene-model';
import type { Texture } from 'pixi.js';
import { loadAssetBytes } from '../../collection/file-lookup.ts';
import type { BrowserAssetSource } from '../../collection/types.ts';
import { logger } from '../../logger.ts';
// Through the LR2 texture module, which registers the DDS decoder with the shared loader.
import { loadTextureFromBytes } from '../lr2/textures.ts';
 
const log = logger('beatoraja-tex');
 
/** A chart's images, decoded; an image the chart lacks, or one that would not decode, is left out. */
export interface BeatorajaChartImages {
  stageFile?: Texture;
  backBMP?: Texture;
  banner?: Texture;
}
 
/** The image of a synthetic slot: -100 the stage file, -101 the back image, -102 the banner; `undefined` otherwise. */
export function resolveBeatorajaChartImage(
  images: BeatorajaChartImages | undefined,
  syntheticId: number,
): Texture | undefined {
  if (images === undefined) return undefined;
  switch (syntheticId) {
    case -100:
      return images.stageFile;
    case -101:
      return images.backBMP;
    case -102:
      return images.banner;
    default:
      return undefined;
  }
}
 
/**
 * Decodes a chart's `#STAGEFILE`, `#BACKBMP` and `#BANNER` from `source`, each looked up next to the chart, then at the
 * source's root. Each is best-effort: a missing or malformed image is left out, and nothing loads without a source.
 */
/**
 * Decodes one of a chart's images (`#STAGEFILE`, `#BACKBMP`, `#BANNER`) from `source`: next to the chart (a leading
 * slash dropped), then at the source's root. A missing or malformed image is `undefined`.
 */
export async function loadBeatorajaChartImage(
  chartPath: string,
  relPath: string | undefined,
  source: BrowserAssetSource,
): Promise<Texture | undefined> {
  if (relPath === undefined || relPath.length === 0) return undefined;
  const chartDir = chartPath.includes('/') ? chartPath.slice(0, chartPath.lastIndexOf('/')) : '';
  const normalized = relPath.replace(/^\/+/, '');
  const candidates = [chartDir.length > 0 ? `${chartDir}/${normalized}` : normalized, normalized];
  for (const candidate of candidates) {
    const entry = source.files.get(candidate);
    if (entry === undefined) continue;
    const bytes = await loadAssetBytes(entry);
    Iif (bytes === undefined) continue;
    try {
      return await loadTextureFromBytes(candidate, bytes);
    } catch (error) {
      log.info('chart image decode failed', { path: candidate, error });
      return undefined;
    }
  }
  return undefined;
}
 
export async function loadBeatorajaChartImages(
  chart: Pick<LibraryChart, 'chart' | 'chartPath'>,
  source: BrowserAssetSource | undefined,
): Promise<BeatorajaChartImages> {
  if (source === undefined) return {};
  const meta = chart.chart.metadata;
  const decode = (relPath: string | undefined) => loadBeatorajaChartImage(chart.chartPath, relPath, source);
  const [stageFile, backBMP, banner] = await Promise.all([
    decode(meta.stageFile),
    decode(meta.backBMP),
    decode(meta.banner),
  ]);
  const images: BeatorajaChartImages = {};
  if (stageFile !== undefined) images.stageFile = stageFile;
  if (backBMP !== undefined) images.backBMP = backBMP;
  if (banner !== undefined) images.banner = banner;
  return images;
}