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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 62x 58x 135x 50x 34x 24x 15x 3x 156x 67x 2x 1x 1x 4x 10x 71x 163x 161x 161x 1x 161x 161x 161x 273x 273x 131x 273x 273x 131x 273x 131x 131x 30x 36x 36x 21x 15x 7x 7x 8x 1x 7x 3x 4x 50x 50x 50x 6x 4x 2x 44x 5x 3x 1x 1x 39x 4x 3x 2x 35x 2x 1x 1x 33x 50x 9x 8x 7x 24x 9x 9x 15x 11x 8x 8x 8x 8x 8x 8x 7x 4x 4x 1x 3x 13x 12x | import { stripComment } from './csv.ts';
/**
* Which screen's `.lr2skin` to pick when a theme bundle ships multiple (play, select, result, decide, etc.).
*/
export type LR2SkinKind = 'play' | 'select' | 'result' | 'decide' | 'courseResult';
/**
* `play_N.lr2skin` variant hint for the play-skin loader. `7-battle` / `5-battle` are LR2's BATTLE play skins
* (`#INFORMATION` types 12 / 13), which LR2 plays a two-player BATTLE on.
*/
export type LR2PlayVariant = '5' | '7' | '9' | '10' | '14' | '7-battle' | '5-battle';
/** The play variants of LR2's BATTLE play skins. */
export function isLR2BattlePlayVariant(variant: LR2PlayVariant | undefined): variant is '7-battle' | '5-battle' {
return variant === '7-battle' || variant === '5-battle';
}
/**
* Numeric type code declared by each `.lr2skin`'s `#INFORMATION,<type>,<title>,<author>,<thumbnail>` header.
* Authored skins use this to tell LR2 which scene they belong to — independent of filename conventions like
* `play_7.lr2skin`. Themes that don't follow the canonical filenames (LITONE4 ships `AC7.lr2skin` for 7-keys, etc.)
* still classify correctly when discovery consults the `#INFORMATION` line.
*
* Codes mirror the LR2 source's `SKIN_TYPE_*` enum:
*
* - 0..4 — Play, key-mode subdivided (7K / 5K / 9K / 10K / 14K).
* - 5 — Music select.
* - 6 — Decide (pre-play splash).
* - 7 — Result.
* - 8 — Key config (out of scope — player config screen).
* - 9 — Skin select (also out of scope — we replace this UI with the Options dialog's theme picker).
* - 12, 13 — BATTLE play (7K / 5K); 14, the 9K BATTLE play skin, is out of scope (LR2 plays no 9KEY BATTLE).
* - 15 — Course result (the result of a whole course; conventionally under `CourseResult/`).
*/
export const LR2_SKIN_INFORMATION_TYPE = {
PLAY_7KEYS: 0,
PLAY_5KEYS: 1,
PLAY_14KEYS: 2,
PLAY_10KEYS: 3,
PLAY_9KEYS: 4,
MUSIC_SELECT: 5,
DECIDE: 6,
RESULT: 7,
KEY_CONFIG: 8,
SKIN_SELECT: 9,
PLAY_7KEYS_BATTLE: 12,
PLAY_5KEYS_BATTLE: 13,
COURSE_RESULT: 15,
} as const;
export type LR2SkinInformationType = (typeof LR2_SKIN_INFORMATION_TYPE)[keyof typeof LR2_SKIN_INFORMATION_TYPE];
/**
* Maps an `#INFORMATION,<type>` code onto our internal {@link LR2SkinKind}. Returns `undefined` for codes we don't
* (yet) consume — namely key-config (8) and skin-select (9). Play codes 0..4 all map to `'play'`; the key-mode
* subdivision is recovered separately via {@link informationTypeToPlayVariant} when the caller needs it (typically
* for variant-aware play-skin scoring).
*/
export function informationTypeToKind(type: number): LR2SkinKind | undefined {
if ((type >= 0 && type <= 4) || type === 12 || type === 13) return 'play';
if (type === 5) return 'select';
if (type === 6) return 'decide';
if (type === 7) return 'result';
if (type === 15) return 'courseResult';
return undefined;
}
/**
* Maps a play-kind `#INFORMATION` type (0..4) onto its canonical {@link LR2PlayVariant}. Returns `undefined` for
* non-play codes — callers that need the play variant should branch on {@link informationTypeToKind} first.
*/
export function informationTypeToPlayVariant(type: number): LR2PlayVariant | undefined {
switch (type) {
case LR2_SKIN_INFORMATION_TYPE.PLAY_7KEYS:
return '7';
case LR2_SKIN_INFORMATION_TYPE.PLAY_5KEYS:
return '5';
case LR2_SKIN_INFORMATION_TYPE.PLAY_9KEYS:
return '9';
case LR2_SKIN_INFORMATION_TYPE.PLAY_10KEYS:
return '10';
case LR2_SKIN_INFORMATION_TYPE.PLAY_14KEYS:
return '14';
case LR2_SKIN_INFORMATION_TYPE.PLAY_7KEYS_BATTLE:
return '7-battle';
case LR2_SKIN_INFORMATION_TYPE.PLAY_5KEYS_BATTLE:
return '5-battle';
default:
return undefined;
}
}
/**
* Peek-only scan of a `.lr2skin` payload to recover its `#INFORMATION,<type>,...` declaration. Returns the parsed
* numeric type (0..9 per {@link LR2_SKIN_INFORMATION_TYPE}) when found, or `undefined` when no `#INFORMATION` line is
* present or the type field doesn't parse as a finite integer.
*
* We cap the decoded prefix at 4 KiB because authored themes universally put `#INFORMATION` near the file top —
* scanning past the header just wastes CPU on large play CSVs (LITONE4's `AC7LEFT.csv` is 3138 lines / ~250 KiB after
* include resolution but the `#INFORMATION` in its parent `.lr2skin` lands inside the first ~500 bytes).
*
* The decoder is permissive (`fatal: false`) and the comment-stripping mirrors {@link stripComment} so commented-out
* `// #INFORMATION,...` lines don't leak into the result.
*/
export function peekLR2SkinInformationType(bytes: Uint8Array | undefined): number | undefined {
if (!bytes || bytes.length === 0) return undefined;
// Strip a leading UTF-8 BOM (`0xEF 0xBB 0xBF`) at the byte level before handing the prefix to the SJIS decoder.
// The raw BOM bytes aren't valid SJIS, so leaving them in produces undefined garbage chars at the start of the
// decoded string and the `#INFORMATION` line scan misses the first row. Real-world theme CSVs occasionally ship
// with a UTF-8 BOM (some authoring tools insert it even on SJIS files), so handling it here is cheap insurance.
let start = 0;
if (bytes.length >= 3 && bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf) {
start = 3;
}
const head = bytes.subarray(start, Math.min(bytes.length, start + 4096));
// SJIS is the canonical encoding for LR2 theme CSVs. `TextDecoder` keeps `0x5C` as the ASCII backslash regardless of
// whether it should be rendered as a Japanese yen sign — fine for our purposes since we only read the type field
// which is plain ASCII digits.
const text = new TextDecoder('shift_jis', { fatal: false }).decode(head).replace(/^/u, '');
for (const rawLine of text.split(/\r?\n/u)) {
const line = stripComment(rawLine).trim();
if (!line.startsWith('#INFORMATION')) continue;
// Tab and comma are both accepted by LR2's CSV parser; `parseRow` in `csv.ts` does the same. Avoid importing the
// full row parser here — we only need the first numeric field.
const delimiter = line.includes('\t') ? '\t' : ',';
const parts = line.split(delimiter);
Iif (parts.length < 2) return undefined;
const value = parts[1]?.trim();
Iif (!value) return undefined;
const parsed = Number.parseInt(value, 10);
return Number.isFinite(parsed) ? parsed : undefined;
}
return undefined;
}
export function isSkinPathOfKind(path: string, kind: LR2SkinKind): boolean {
const lower = path.toLowerCase();
if (kind === 'play') {
return lower.includes('/play') || lower.includes('\\play');
}
if (kind === 'result') {
const sansCourse = lower.replace(/\/courseresult\//gu, '/').replace(/\\courseresult\\/gu, '\\');
return sansCourse.includes('/result') || sansCourse.includes('\\result');
}
if (kind === 'decide') {
return lower.includes('/decide') || lower.includes('\\decide');
}
if (kind === 'courseResult') {
return lower.includes('/courseresult') || lower.includes('\\courseresult');
}
return lower.includes('/select') || lower.includes('\\select');
}
/**
* Optional inputs to {@link scoreSkinPath} that bias the result toward the caller's specific intent.
*/
export interface ScoreSkinPathOptions {
/** Bias the play-kind scoring toward `play_<variant>.lr2skin`. Ignored for non-play kinds. */
variant?: LR2PlayVariant;
/**
* Parsed `#INFORMATION,<type>` code (see {@link peekLR2SkinInformationType}). When present, candidates whose
* declared type matches the requested {@link LR2SkinKind} are awarded a "mid-range" score that ranks **below** the
* canonical filenames (`select.lr2skin` = 0, `play_7.lr2skin` = 0, etc.) but **above** the catch-all 100.
*
* This lets type-honest themes that don't follow the canonical filename convention (LITONE4's `AC7.lr2skin` for
* type=0 play, etc.) win against unrelated `.lr2skin` files in the same bundle without overriding LR2-default's
* preferred ordering.
*/
type?: number;
}
/**
* Backward-compatible legacy signature for the play-skin variant bias. Newer callers should prefer the options-object
* overload so `type` can be threaded in as well.
*/
export function scoreSkinPath(path: string, kind: LR2SkinKind, variant?: LR2PlayVariant): number;
export function scoreSkinPath(path: string, kind: LR2SkinKind, options: ScoreSkinPathOptions): number;
export function scoreSkinPath(
path: string,
kind: LR2SkinKind,
variantOrOptions?: LR2PlayVariant | ScoreSkinPathOptions,
): number {
const options =
typeof variantOrOptions === 'string' || variantOrOptions === undefined
? { variant: variantOrOptions }
: variantOrOptions;
const lower = path.toLowerCase();
// Filename-based ranking comes first — the canonical LR2 names (`play_7.lr2skin`, `select.lr2skin`, …) stay at the
// sub-10 tier so themes that DO use them aren't perturbed by the new type lookup.
if (kind === 'select') {
if (lower.endsWith('/select.lr2skin')) return 0;
if (lower.includes('/select') && lower.endsWith('.lr2skin')) return 10;
return informationTypeMatches(options.type, kind) ? 20 : 100;
}
if (kind === 'result') {
if (lower.includes('/courseresult')) return 90;
if (lower.endsWith('/result.lr2skin')) return 0;
Iif (lower.includes('/result') && lower.endsWith('.lr2skin')) return 10;
return informationTypeMatches(options.type, kind) ? 20 : 100;
}
if (kind === 'courseResult') {
if (lower.endsWith('/courseresult.lr2skin')) return 0;
if (lower.includes('/courseresult') && lower.endsWith('.lr2skin')) return 10;
return informationTypeMatches(options.type, kind) ? 20 : 100;
}
if (kind === 'decide') {
if (lower.endsWith('/decide.lr2skin')) return 0;
Iif (lower.includes('/decide') && lower.endsWith('.lr2skin')) return 10;
return informationTypeMatches(options.type, kind) ? 20 : 100;
}
// A declared key mode outranks a misleading filename. When the requested mode is absent,
// prefer the same number of play sides before borrowing an SP/DP layout.
const declaredVariant = options.type === undefined ? undefined : informationTypeToPlayVariant(options.type);
// A BATTLE play skin is only ever the BATTLE one (a 5KEY BATTLE takes the 7KEY one as LR2's SetFirstSkin_5kb does);
// the host loads a BATTLE variant only when the theme declares one.
if (isLR2BattlePlayVariant(options.variant) || isLR2BattlePlayVariant(declaredVariant)) {
if (declaredVariant === options.variant) return -1;
if (options.variant === '5-battle' && declaredVariant === '7-battle') return 0;
return 100;
}
if (options.variant && declaredVariant && declaredVariant !== options.variant) {
const fallbackOrder: Record<'5' | '7' | '9' | '10' | '14', readonly LR2PlayVariant[]> = {
'5': ['5', '7', '9', '10', '14'],
'7': ['7', '5', '9', '14', '10'],
'9': ['9', '7', '5', '14', '10'],
'10': ['10', '14', '5', '7', '9'],
'14': ['14', '10', '7', '5', '9'],
};
return 60 + fallbackOrder[options.variant].indexOf(declaredVariant);
}
if (options.variant && lower.endsWith(`/play_${options.variant}.lr2skin`)) return -1;
if (lower.endsWith('/play_7.lr2skin')) return 0;
Iif (lower.endsWith('/play_5.lr2skin')) return 1;
Iif (lower.endsWith('/play_9.lr2skin')) return 2;
Iif (lower.endsWith('/play_10.lr2skin')) return 3;
Iif (lower.endsWith('/play_14.lr2skin')) return 4;
Iif (lower.includes('/play_') && !lower.includes('play_half')) return 30;
if (lower.includes('play_half')) return 50;
// Type-aware fallback for play skins with non-canonical filenames (LITONE4's `AC7.lr2skin`, etc.). A play skin
// whose declared variant matches the requested one outranks one that doesn't, mirroring the `play_N` tier above.
if (informationTypeMatches(options.type, 'play')) {
const declaredVariant = options.type !== undefined ? informationTypeToPlayVariant(options.type) : undefined;
if (options.variant !== undefined && declaredVariant === options.variant) return 19;
return 20;
}
return 100;
}
function informationTypeMatches(type: number | undefined, kind: LR2SkinKind): boolean {
if (type === undefined) return false;
return informationTypeToKind(type) === kind;
}
|