219 lines
13 KiB
JavaScript
219 lines
13 KiB
JavaScript
// Convert Hololive Dreams' decrypted SUS dialect to this application's compact score timeline.
|
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
import { createHash } from 'node:crypto';
|
|
import { execFileSync } from 'node:child_process';
|
|
import path from 'node:path';
|
|
import { susScoreWeight } from './sus-score-weight.mjs';
|
|
import { formulaScoreMetadata } from './song-score-formulas.mjs';
|
|
|
|
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
const difficulties = ['easy', 'normal', 'hard', 'expert'];
|
|
|
|
function charValue(character) {
|
|
const value = alphabet.indexOf(character);
|
|
if (value < 0) throw new Error(`잘못된 SUS base62 값: ${character}`);
|
|
return value;
|
|
}
|
|
|
|
function parseCells(data, includeZero = false) {
|
|
const cells = [];
|
|
let position = 0;
|
|
for (let index = 0; index < data.length;) {
|
|
if (data.startsWith('0x', index)) {
|
|
position += charValue(data[index + 2]) * 62 + charValue(data[index + 3]);
|
|
index += 4;
|
|
} else {
|
|
const cell = data.slice(index, index + 2);
|
|
if (cell.length !== 2) throw new Error('잘린 SUS 셀입니다.');
|
|
if (includeZero ? cell !== '00' : cell[0] !== '0') cells.push({ position, cell });
|
|
position += 1;
|
|
index += 2;
|
|
}
|
|
}
|
|
return { cells, total: position };
|
|
}
|
|
|
|
function parseHeaders(text) {
|
|
const headers = new Map();
|
|
for (const raw of text.split(/\r?\n/)) {
|
|
const match = raw.match(/^#([A-Z_]+)\s+(.+)$/);
|
|
if (match) headers.set(match[1], match[2].trim().replaceAll('"', ''));
|
|
}
|
|
return headers;
|
|
}
|
|
|
|
function measureStarts(lengths) {
|
|
const starts = [];
|
|
let beat = 0;
|
|
for (let measure = 0; measure < 1000; measure += 1) {
|
|
starts[measure] = beat;
|
|
beat += lengths.get(measure) ?? 4;
|
|
}
|
|
return starts;
|
|
}
|
|
|
|
function parseSusTimeline(text) {
|
|
const headers = parseHeaders(text);
|
|
const lengths = new Map();
|
|
const bpmDefinitions = new Map();
|
|
const lines = text.split(/\r?\n/).map((raw) => raw.trim());
|
|
for (const line of lines) {
|
|
const bar = line.match(/^#(\d{3})02:(.+)$/);
|
|
if (bar) lengths.set(Number(bar[1]), Number(bar[2]));
|
|
const bpm = line.match(/^#BPM(..):(.+)$/);
|
|
if (bpm) bpmDefinitions.set(bpm[1], Number(bpm[2]));
|
|
}
|
|
const starts = measureStarts(lengths);
|
|
const toBeat = (measure, position, total) => starts[measure] + (lengths.get(measure) ?? 4) * position / total;
|
|
const bpmEvents = [];
|
|
const taps = [];
|
|
const flicks = [];
|
|
const holdStreams = new Map();
|
|
const visibleRelays = [];
|
|
const specialStarts = [];
|
|
for (const line of lines) {
|
|
const holdMatch = line.match(/^#(\d{3})3([0-9a-z])([0-9a-zA-Z]):(.+)$/);
|
|
if (holdMatch) {
|
|
const [, measureText, lane, channel, data] = holdMatch;
|
|
const measure = Number(measureText);
|
|
const parsed = parseCells(data);
|
|
const notes = parsed.cells.map(({ position, cell }) => ({ beat: toBeat(measure, position, parsed.total), lane: charValue(lane), kind: charValue(cell[0]), width: charValue(cell[1]) }));
|
|
visibleRelays.push(...notes.filter((note) => note.kind === 3));
|
|
const stream = charValue(channel);
|
|
if (!holdStreams.has(stream)) holdStreams.set(stream, []);
|
|
holdStreams.get(stream).push(...notes);
|
|
continue;
|
|
}
|
|
const match = line.match(/^#(\d{3})([0-9a-z])([0-9a-zA-Z]):(.+)$/);
|
|
if (!match) continue;
|
|
const [, measureText, family, lane, data] = match;
|
|
const measure = Number(measureText);
|
|
if (`${family}${lane}` === '08') {
|
|
for (const { position, cell } of parseCells(data, true).cells) bpmEvents.push({ beat: toBeat(measure, position, parseCells(data, true).total), bpm: bpmDefinitions.get(cell) ?? Number(headers.get('BASEBPM') ?? 120) });
|
|
continue;
|
|
}
|
|
if (`${family}${lane}` === '0B') {
|
|
for (const { position } of parseCells(data, true).cells) specialStarts.push(toBeat(measure, position, parseCells(data, true).total));
|
|
continue;
|
|
}
|
|
if (!['1', '5'].includes(family)) continue;
|
|
const parsed = parseCells(data);
|
|
const notes = parsed.cells.map(({ position, cell }) => ({ beat: toBeat(measure, position, parsed.total), lane: charValue(lane), kind: charValue(cell[0]), width: charValue(cell[1]) }));
|
|
if (family === '1') taps.push(...notes);
|
|
else if (family === '5') flicks.push(...notes);
|
|
}
|
|
if (bpmEvents.length === 0) bpmEvents.push({ beat: 0, bpm: Number(headers.get('BASEBPM') ?? 120) });
|
|
bpmEvents.sort((left, right) => left.beat - right.beat);
|
|
const uniqueBpmEvents = bpmEvents.filter((event, index) => index === 0 || event.beat !== bpmEvents[index - 1].beat);
|
|
function secondsAt(beat) {
|
|
let seconds = 0;
|
|
let previous = uniqueBpmEvents[0];
|
|
if (previous.beat > 0) seconds += previous.beat * 60 / Number(headers.get('BASEBPM') ?? 120);
|
|
for (let index = 1; index < uniqueBpmEvents.length && uniqueBpmEvents[index].beat <= beat; index += 1) {
|
|
const event = uniqueBpmEvents[index];
|
|
seconds += (event.beat - previous.beat) * 60 / previous.bpm;
|
|
previous = event;
|
|
}
|
|
seconds += (beat - previous.beat) * 60 / previous.bpm;
|
|
return seconds + Number(headers.get('WAVEOFFSET') ?? 0);
|
|
}
|
|
const key = (note) => `${note.beat}|${note.lane}`;
|
|
const tapKeys = new Set(taps.filter((note) => note.kind === 1 || note.kind === 2).map(key));
|
|
const standaloneFlickKeys = new Set(flicks.filter((note) => [1, 3, 4].includes(note.kind)).map(key));
|
|
const holds = [];
|
|
for (const stream of holdStreams.values()) {
|
|
const deduped = [...new Map(stream.sort((left, right) => left.beat - right.beat).map((note) => [`${note.beat}|${note.lane}|${note.kind}|${note.width}`, note])).values()];
|
|
const open = [];
|
|
const relays = [];
|
|
for (const note of deduped) {
|
|
if (note.kind === 1) open.push(note);
|
|
else if (note.kind === 2) {
|
|
const index = open.findIndex((start) => start.beat <= note.beat);
|
|
if (index < 0) continue;
|
|
const start = open.splice(index, 1)[0];
|
|
const inner = relays.filter((relay) => start.beat <= relay.beat && relay.beat <= note.beat);
|
|
for (const relay of inner) relays.splice(relays.indexOf(relay), 1);
|
|
holds.push([start, ...inner.sort((left, right) => left.beat - right.beat), note]);
|
|
} else relays.push(note);
|
|
}
|
|
}
|
|
const holdStartKeys = new Set(holds.map((hold) => key(hold[0])));
|
|
const holdEndKeys = new Set(holds.map((hold) => key(hold.at(-1))));
|
|
const notes = [];
|
|
// A co-located directional is one flick. A hold end directional is part of the hold and scores at normal weight.
|
|
for (const tap of taps) {
|
|
if (![1, 2].includes(tap.kind) || holdStartKeys.has(key(tap))) continue;
|
|
notes.push({ timeSeconds: secondsAt(tap.beat), type: 'regular', isFlick: standaloneFlickKeys.has(key(tap)) });
|
|
}
|
|
for (const flick of flicks) {
|
|
if (![1, 3, 4].includes(flick.kind) || tapKeys.has(key(flick)) || holdStartKeys.has(key(flick)) || holdEndKeys.has(key(flick))) continue;
|
|
notes.push({ timeSeconds: secondsAt(flick.beat), type: 'regular', isFlick: true });
|
|
}
|
|
for (const hold of holds) {
|
|
const start = hold[0];
|
|
const end = hold.at(-1);
|
|
notes.push({ timeSeconds: secondsAt(start.beat), type: 'regular', isFlick: false });
|
|
// The game creates a continuation judgement every half beat. A visible relay at the same
|
|
// position remains a separate score judgement.
|
|
for (let beat = start.beat + 0.5; beat < end.beat - 1e-9; beat += 0.5) {
|
|
notes.push({ timeSeconds: secondsAt(beat), type: 'mid', isFlick: false });
|
|
}
|
|
notes.push({ timeSeconds: secondsAt(end.beat), type: 'regular', isFlick: false });
|
|
}
|
|
// Visible relay ticks are score judgements even if the editor stream contains an unmatched visual segment.
|
|
for (const relay of visibleRelays) notes.push({ timeSeconds: secondsAt(relay.beat), type: 'mid', isFlick: false });
|
|
notes.sort((left, right) => left.timeSeconds - right.timeSeconds || (left.type === 'mid' ? 1 : -1));
|
|
const expectedRegular = Number(headers.get('NORMAL_NOTE_COUNT')) + Number(headers.get('FLICK_NOTE_COUNT')) +
|
|
Number(headers.get('LONG_START_NOTE_COUNT')) + Number(headers.get('LONG_END_NOTE_COUNT')) + Number(headers.get('LONG_FLICK_END_NOTE_COUNT'));
|
|
// SUS may encode a directional overlay as a second event at the exact same instant. It is one
|
|
// judgement in Hololive Dreams; retain the header as the authoritative combo count.
|
|
for (let index = notes.length - 1; notes.filter((note) => note.type === 'regular').length > expectedRegular && index >= 0; index -= 1) {
|
|
if (notes[index].type === 'regular') notes.splice(index, 1);
|
|
}
|
|
const expectedMid = Number(headers.get('LONG_RELAY_NOTE_COUNT')) + Number(headers.get('LONG_CONTINUE_NOTE_COUNT'));
|
|
// Some editor streams encode a sub-half-beat terminal continuation. Preserve its score category
|
|
// even when the compact timeline cannot distinguish it from the neighbouring hold endpoint.
|
|
while (notes.filter((note) => note.type === 'mid').length < expectedMid) {
|
|
const anchor = notes.at(-1);
|
|
notes.push({ timeSeconds: anchor?.timeSeconds ?? 0, type: 'mid', isFlick: false });
|
|
}
|
|
notes.sort((left, right) => left.timeSeconds - right.timeSeconds || (left.type === 'mid' ? 1 : -1));
|
|
return { songId: headers.get('MUSIC_ID'), notes, specialStartSeconds: specialStarts.sort((left, right) => left - right).map(secondsAt) };
|
|
}
|
|
|
|
function readMaster(locale, filename) {
|
|
const repository = path.resolve(`data/raw/holodoridb/${locale}`);
|
|
return JSON.parse(execFileSync('git', ['-c', `safe.directory=${repository.replaceAll('\\', '/')}`, '-C', repository, 'show', `origin/main:${filename}`], { encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 }));
|
|
}
|
|
const music = new Map(readMaster('kor', 'Music.json').map((row) => { const data = row.data ?? row; return [data.id, data]; }));
|
|
const translation = new Map(readMaster('eng', 'LangMusic_Eng.json').map((row) => { const data = row.data ?? row; return [data.id, data.text]; }));
|
|
const levels = new Map(readMaster('kor', 'MusicDifficulty.json').map((row) => { const data = row.data ?? row; return [`${data.musicId}.${data.difficultyType.split('_').at(-1).toLowerCase()}`, data.difficultyLevel]; }));
|
|
const songIds = process.argv.slice(2);
|
|
if (songIds.length === 0) throw new Error('곡 ID를 지정하세요.');
|
|
const singerOverrides = { m0527: 'group' };
|
|
const generatedCatalogEntries = [];
|
|
for (const songId of songIds) for (const difficulty of difficulties) {
|
|
const file = path.resolve(`data/raw/sus/chart_${songId}_${difficulty}.sus`);
|
|
const bytes = await readFile(file);
|
|
const timeline = parseSusTimeline(bytes.toString('utf8'));
|
|
const weight = susScoreWeight(bytes.toString('utf8'));
|
|
if (timeline.songId !== songId || timeline.notes.length !== weight.fullComboNoteCount) {
|
|
const mids = timeline.notes.filter((note) => note.type === 'mid').length;
|
|
throw new Error(`${songId}.${difficulty}: 파서 노트 수 ${timeline.notes.length}/${weight.fullComboNoteCount} 불일치 (중간 ${mids}, 헤더 ${weight.counts.LONG_RELAY + weight.counts.LONG_CONTINUE})`);
|
|
}
|
|
const master = music.get(songId);
|
|
const difficultyLevel = levels.get(`${songId}.${difficulty}`);
|
|
const susSha256 = createHash('sha256').update(bytes).digest('hex');
|
|
const formula = formulaScoreMetadata(songId, difficulty, difficultyLevel, timeline.notes.length, master.liveScoreCoefficientPermil, susSha256);
|
|
const output = { schemaVersion: 1, songId, difficulty, durationSeconds: master.playingSeconds, scoreRatio: formula?.scoreRatio ?? null, scoreCalibrationSource: formula?.scoreCalibrationSource ?? null, ...(formula ?? {}), notes: timeline.notes, specialStartSeconds: timeline.specialStartSeconds, singerType: singerOverrides[songId] ?? master.musicSingerType?.split('_').at(-1).toLowerCase() ?? null, singerCharacterIds: master.characterIds ?? [], source: { page: 'Hololive Dreams CDN via holodori-asset-tools', transcriptionStatus: 'exact_from_decrypted_sus', susSha256 } };
|
|
await writeFile(path.resolve(`static/data/charts/${songId}.${difficulty}.json`), JSON.stringify(output));
|
|
generatedCatalogEntries.push({ songId, title: translation.get(master.titleLangId) ?? songId, difficulty, difficultyLevel, durationSeconds: master.playingSeconds, scoreRatio: output.scoreRatio, scoreCalibrationSource: output.scoreCalibrationSource, scoreFormula: output.scoreFormula, totalNotes: output.notes.length, singerType: output.singerType, singerCharacterIds: output.singerCharacterIds, releaseTime: Number(master.startTime), path: `/data/charts/${songId}.${difficulty}.json` });
|
|
console.log(`${songId}.${difficulty}: ${timeline.notes.length} notes ${timeline.specialStartSeconds.join(', ')}`);
|
|
}
|
|
const catalogPath = path.resolve('src/lib/data/exact-chart-catalog.json');
|
|
const catalog = JSON.parse(await readFile(catalogPath, 'utf8'));
|
|
const generatedKeys = new Set(generatedCatalogEntries.map((entry) => `${entry.songId}.${entry.difficulty}`));
|
|
catalog.charts = [...catalog.charts.filter((entry) => !generatedKeys.has(`${entry.songId}.${entry.difficulty}`)), ...generatedCatalogEntries]
|
|
.sort((left, right) => left.songId.localeCompare(right.songId) || left.difficulty.localeCompare(right.difficulty));
|
|
await writeFile(catalogPath, `${JSON.stringify(catalog, null, 2)}\n`);
|