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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 27x 24x 24x 24x 12x 12x 14x 14x 10x 10x 14x 7x 9x 7x 7x 7x 1x 1x 1x 13x 13x 13x 9x 9x 8x 8x 8x 12x 13x 13x 1x 11x 11x 11x 11x 10x 10x 10x 10x 11x 11x 8x 8x 8x 8x 11x | /**
* Loading one entry of a beatoraja theme for a scene: its **scene kit** ({@link BeatorajaSceneKit}), what the beatoraja
* family draws a select, decide, play or result screen with.
*
* A host keeps one {@link BeatorajaSceneKitCaches} and calls {@link loadBeatorajaSceneKit} each time it makes a
* beatoraja scene:
*
* 1. JSON skins return their header and full definition in one load. A Lua skin first returns its header
* (`property[]`, `filepath[]`, `offset[]`), whose defaults seed the player's options for the entry
* ({@link resolveBeatorajaSkinConfig}). It then runs `main()` with those options to build its `source[]` and
* `destination[]`. Options supplied by the host skip the header pass.
* 2. The skin's `source[]` is resolved against the theme's files (`bundleBeatorajaSources`) and decoded to textures.
* 3. Its `font[]` is registered.
*
* Textures, fonts and options are kept per entry path, so the next scene on the same entry reuses them. Textures are
* never destroyed (see `textures.ts`): a new theme only forgets them ({@link releaseBeatorajaSceneKitAssets}).
*/
import {
buildBaseOpSet,
buildDefaultSkinConfigFiles,
buildDefaultSkinConfigOptions,
bundleBeatorajaSources,
loadBeatorajaSkin,
normalizeBeatorajaFonts,
type BeatorajaLuaRuntimeContext,
type BeatorajaSkin,
type BeatorajaSkinConfig,
type BeatorajaSkinFileEntry,
type BeatorajaSkinHeader,
type BeatorajaSourceBundle,
} from '@be-music/skin-beatoraja';
import { beatorajaDifficultyOps } from '@be-music/skin-beatoraja/scene';
import type { BeatorajaSkinAudio } from './audio.ts';
import { loadBeatorajaFonts, type BeatorajaFontCache } from './fonts.ts';
import { loadBeatorajaTexturesFromBundle, type BeatorajaTextureCache } from './textures.ts';
/** One entry of a beatoraja theme, loaded for a scene. */
export interface BeatorajaSceneKit {
/** The parsed skin of the entry (`header.type` matching the scene). */
readonly skin: BeatorajaSkin;
readonly textures: BeatorajaTextureCache;
readonly fonts?: BeatorajaFontCache;
/** The player's picks for the skin's options (`property[]`). */
readonly skinConfig?: BeatorajaSkinConfig;
/** The theme's files: the Lua `main_state.audio_*` sounds and the theme's path-only system sounds are read from them. */
readonly files: ReadonlyMap<string, BeatorajaSkinFileEntry>;
}
/** What a host keeps between scene kit loads, each keyed by the entry's path (`BeatorajaSkinEntry.entryPath`). */
export interface BeatorajaSceneKitCaches {
/** The decoded textures. Never destroyed: see `textures.ts`. */
readonly textures: Map<string, BeatorajaTextureCache>;
/** The registered fonts. */
readonly fonts: Map<string, BeatorajaFontCache>;
/**
* The player's options: seeded from the entry's header on its first load, then replaced by the host when the player
* changes them (an options panel, a skin's own button). A select skin and a play skin keep their own.
*/
readonly skinConfigs: Map<string, BeatorajaSkinConfig>;
}
/** Empty caches for {@link loadBeatorajaSceneKit}. */
export function createBeatorajaSceneKitCaches(): BeatorajaSceneKitCaches {
return { textures: new Map(), fonts: new Map(), skinConfigs: new Map() };
}
/**
* Forgets the decoded textures and disposes the registered fonts, for a new theme. The textures are dropped, not
* destroyed (see `textures.ts`), and the fonts leave `document.fonts` and Pixi's bitmap-font cache. The player's options
* stay, as they are kept by entry path.
*/
export function releaseBeatorajaSceneKitAssets(caches: BeatorajaSceneKitCaches): void {
caches.textures.clear();
for (const fonts of caches.fonts.values()) fonts.dispose();
caches.fonts.clear();
}
/**
* The options an entry starts with: each `property[]`'s default item, each `filepath[]`'s default file, no timing offset
* and no custom offsets. The default file is matched against the theme's files, so the author's default wins over the
* first file a wildcard sorts to (ModernChic's `key` filepath would otherwise pick `#default.png` over `harf.png`).
*/
export function defaultBeatorajaSkinConfig(
header: BeatorajaSkinHeader,
files: ReadonlyMap<string, BeatorajaSkinFileEntry>,
entryPath: string,
): BeatorajaSkinConfig {
const file = buildDefaultSkinConfigFiles(header, files, entryPath);
// `customOffset` starts empty (an options panel zero-fills each slot when it is built), so a kept config and one the
// panel sends back have the same keys.
return { offset: 0, option: buildDefaultSkinConfigOptions(header), file, customOffset: {} };
}
/**
* The player's options for an entry: the kept ones, or the defaults of its header ({@link defaultBeatorajaSkinConfig}),
* kept from then on. The returned object is the kept one.
*/
export function resolveBeatorajaSkinConfig(
caches: Pick<BeatorajaSceneKitCaches, 'skinConfigs'>,
entryPath: string,
header: BeatorajaSkinHeader,
files: ReadonlyMap<string, BeatorajaSkinFileEntry>,
): BeatorajaSkinConfig {
let config = caches.skinConfigs.get(entryPath);
if (config === undefined) {
config = defaultBeatorajaSkinConfig(header, files, entryPath);
caches.skinConfigs.set(entryPath, config);
}
return config;
}
/** What {@link createBeatorajaSkinLoadContext} builds the load-time `main_state` from. */
export interface BeatorajaSkinLoadContextOptions {
/** The chart's `#DIFFICULTY` (1 BEGINNER … 5 INSANE); `DIFFICULTY_UNDEFINED` without one. */
readonly difficulty?: number;
/** The options the skin is loaded with; their ops are lit. */
readonly skinConfig: BeatorajaSkinConfig;
/** Plays the Lua `main_state.audio_play` / `audio_loop` / `audio_stop` sounds; without one they do nothing. */
readonly skinAudio?: BeatorajaSkinAudio;
}
/**
* The `main_state` a skin sees while its `main()` runs, for skins that work values out at load time. ModernChic's
* decide and result skins, for one, look up a colour by `main_state.option(DIFFICULTY*)` and fail to load when no
* difficulty op is lit. It lights the chart's difficulty ops (`beatorajaDifficultyOps`) and the options' ops, and
* routes the Lua sounds to `skinAudio`; every other accessor keeps its default.
*/
export function createBeatorajaSkinLoadContext(options: BeatorajaSkinLoadContextOptions): BeatorajaLuaRuntimeContext {
const ops = new Set(buildBaseOpSet(options.skinConfig.option));
for (const op of beatorajaDifficultyOps(options.difficulty)) ops.add(op);
const audio = options.skinAudio;
return {
option: (id) => ops.has(id),
audioPlay: audio === undefined ? undefined : (path, vol) => audio.play(path, vol),
audioLoop: audio === undefined ? undefined : (path, vol) => audio.loop(path, vol),
audioStop: audio === undefined ? undefined : (path) => audio.stop(path),
};
}
/** What {@link loadBeatorajaSceneKit} loads and how. */
export interface LoadBeatorajaSceneKitOptions {
/** The theme's files (`BeatorajaThemeBundle.files`). */
readonly files: ReadonlyMap<string, BeatorajaSkinFileEntry>;
/** The entry to load (`BeatorajaSkinEntry.entryPath`); the caches are keyed by it. */
readonly entryPath: string;
readonly caches: BeatorajaSceneKitCaches;
/**
* The options to load the skin with. A Lua skin needs a header pass when these are omitted to seed the kept
* options ({@link resolveBeatorajaSkinConfig}).
*/
readonly skinConfig?: BeatorajaSkinConfig;
/**
* The `main_state` the skin sees while its `main()` runs ({@link createBeatorajaSkinLoadContext}), made from the
* options it is loaded with. Only the load with options gets it; without one `main_state` reads its defaults.
*/
readonly runtimeContext?: (skinConfig: BeatorajaSkinConfig) => BeatorajaLuaRuntimeContext;
/** Decode the textures again even when they are kept: other options can build another `source[]`. */
readonly reloadTextures?: boolean;
/** Called with the resolved `source[]` when the textures are decoded, for the host's log. */
readonly onSources?: (sources: BeatorajaSourceBundle) => void;
/** Called when the fonts are registered, with how many the skin declares, for the host's log. */
readonly onFonts?: (fonts: BeatorajaFontCache, declared: number) => void;
}
/** A loaded scene kit: the fonts are registered and the options are the ones the skin was loaded with. */
export interface LoadedBeatorajaSceneKit extends BeatorajaSceneKit {
readonly fonts: BeatorajaFontCache;
readonly skinConfig: BeatorajaSkinConfig;
}
/**
* The outcome of {@link loadBeatorajaSceneKit}: the kit with the entry's header (for an options panel), or why it could
* not be loaded: `stage` says whether the header pass or the skin itself failed.
*/
export type LoadBeatorajaSceneKitResult =
| { readonly ok: true; readonly kit: LoadedBeatorajaSceneKit; readonly header: BeatorajaSkinHeader }
| { readonly ok: false; readonly stage: 'header' | 'skin'; readonly message: string };
/** Loads one entry of a beatoraja theme as a scene kit. See the module comment. */
export async function loadBeatorajaSceneKit(
options: LoadBeatorajaSceneKitOptions,
): Promise<LoadBeatorajaSceneKitResult> {
const { files, entryPath, caches } = options;
let header: BeatorajaSkinHeader | undefined;
let load: ReturnType<typeof loadBeatorajaSkin> | undefined;
let skinConfig = options.skinConfig;
if (skinConfig === undefined) {
// The header pass picks up the skin's `property[]` schema; the second pass re-runs `main()` with the option picks so
// dynamic `source[]` / `destination[]` populate.
const headerLoad = loadBeatorajaSkin({ entryPath, files });
if (!headerLoad.ok) return { ok: false, stage: 'header', message: headerLoad.error.message };
header = headerLoad.header;
skinConfig = resolveBeatorajaSkinConfig(caches, entryPath, header, files);
if (headerLoad.format === 'json') load = headerLoad;
}
const runtimeContext = options.runtimeContext?.(skinConfig);
load ??= loadBeatorajaSkin({
entryPath,
files,
skinConfig,
...(runtimeContext === undefined ? {} : { runtimeContext }),
});
if (!load.ok || !load.skin) {
return { ok: false, stage: 'skin', message: load.ok ? 'skin payload missing' : load.error.message };
}
const skin = load.skin;
// Option changes can alter `source[]`, so a reload drops the kept textures before decoding them again.
if (options.reloadTextures === true) caches.textures.delete(entryPath);
let textures = caches.textures.get(entryPath);
if (textures === undefined) {
const sources = bundleBeatorajaSources({
files,
entryPath,
sources: (skin.source ?? []) as unknown as ReadonlyArray<Readonly<Record<string, unknown>>>,
filepathSchema: skin.filepath,
// The options' `filepath[]` picks choose the files of the wildcards they name: on the first load, the header's
// default file of each.
filepathOverrides: skinConfig.file,
});
options.onSources?.(sources);
textures = await loadBeatorajaTexturesFromBundle(sources);
caches.textures.set(entryPath, textures);
}
// TTFS are small and a registered FontFace outlives every scene anyway, so fonts are kept per entry as textures are.
let fonts = caches.fonts.get(entryPath);
if (fonts === undefined) {
const declared = normalizeBeatorajaFonts(skin.font);
fonts = await loadBeatorajaFonts({ files, entryPath, fonts: declared });
options.onFonts?.(fonts, declared.length);
caches.fonts.set(entryPath, fonts);
}
return { ok: true, kit: { skin, textures, fonts, skinConfig, files }, header: header ?? load.header };
}
|