All files / player-browser/src/skin family.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 5/5
100% Lines 12/12

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                                                                                                                        21x     9x     5x 10x   2x     17x 17x 15x 45x 42x 6x       17x        
/**
 * Skin family abstraction. Each "family" describes one of the three rendering paths the web player supports:
 *
 * - `'lr2'` — Lunatic Rave 2 skins (`.lr2skin` + bitmap atlases). Loaded via `@be-music/skin-lr2`.
 * - `'beatoraja'` — beatoraja Lua skins (`.luaskin` + texture atlases). Loaded via `@be-music/skin-beatoraja`.
 * - `'default'` — the built-in chrome used when neither LR2 nor beatoraja is supplied. Has no theme to load.
 *
 * Before this abstraction the player branched ad-hoc on `canPlayChartBeatoraja(song)` / `decideSkin !== undefined` /
 * `selectSkin !== undefined` at every transition point (drop routing, scene construction, theme detection). Funnelling
 * those checks through a single registry makes the policy explicit (which family takes precedence when both themes are
 * loaded? answer: the registry's iteration order) and adding a fourth family in the future a localised change instead
 * of an N-place edit.
 *
 * **Note on scope:** This module deliberately does **not** include scene-factory methods. The four scene kinds
 * (`select` / `decide` / `gameplay` / `result`) take very different construction arguments between families (LR2 takes
 * `LR2Skin`; beatoraja takes a pre-decoded texture cache + Lua-evaluated skin config; default takes neither) so a
 * single union-typed factory would push complexity onto every caller. The host instead picks the family via the
 * registry and then makes each screen with the host API (`createSelectScene`, `createDecideScene`, `createPlayScene`,
 * `createResultScene`) and the family's kit; the family views live under `view/{built-in,lr2,beatoraja}/`.
 */
export type SkinFamilyId = 'lr2' | 'beatoraja' | 'default';
 
export interface SkinFamily {
  /** Stable identifier — used by the host for routing decisions and debug logging. */
  readonly id: SkinFamilyId;
  /** Human-readable label for status text / error messages. Not used in routing logic. */
  readonly label: string;
  /**
   * Predicate over a dropped file's normalized path. Returns `true` when the file looks like an entry for this
   * family's theme (e.g. `.lr2skin` for LR2, `.luaskin` / `skin/*.json` for beatoraja).
   *
   * Optional — the default family is the fallthrough and never matches a dropped file directly; the host falls back
   * to it when no other family claims the drop.
   */
  matchesThemeFile?(path: string): boolean;
}
 
/**
 * Look-up surface over a fixed list of registered families. The iteration order of `families` defines the priority
 * when multiple families could theoretically claim a drop (today none overlap, but the contract documents the
 * tie-break for future additions).
 */
export interface SkinFamilyRegistry {
  readonly families: readonly SkinFamily[];
  /** Returns the family with the given id, or `undefined` if not registered. */
  byId(id: SkinFamilyId): SkinFamily | undefined;
  /**
   * Returns the first registered family whose `matchesThemeFile` returns `true` for this path, or `undefined` if
   * none claims it. The default family is intentionally NEVER returned from this method — it owns no theme files.
   */
  detectThemeFile(path: string): SkinFamily | undefined;
  /**
   * Inspects an entire dropped file list and returns the set of family ids whose `matchesThemeFile` matched any
   * entry. Used by hosts that want to know "does this drop carry an LR2 theme, a beatoraja theme, or both?"
   * before dispatching to the per-family loader.
   */
  detectThemeFamilies(paths: Iterable<string>): Set<SkinFamilyId>;
}
 
export function createSkinFamilyRegistry(families: readonly SkinFamily[]): SkinFamilyRegistry {
  return {
    families,
    byId(id) {
      return families.find((family) => family.id === id);
    },
    detectThemeFile(path) {
      for (const family of families) {
        if (family.matchesThemeFile?.(path) === true) return family;
      }
      return undefined;
    },
    detectThemeFamilies(paths) {
      const matched = new Set<SkinFamilyId>();
      for (const path of paths) {
        for (const family of families) {
          if (matched.has(family.id)) continue;
          if (family.matchesThemeFile?.(path) === true) {
            matched.add(family.id);
          }
        }
      }
      return matched;
    },
  };
}