All files / chart-model/src fingerprint-document.ts

91.93% Statements 57/62
85% Branches 34/40
100% Functions 6/6
91.37% Lines 53/58

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 255 256                                              597x           597x                                                                                                                                                                                                                                 5972059x 87113x   5884946x 3690605x   2194341x 201035x     201035x   1993306x 458731x 458731x 1166378x 821061x   1166378x   458731x   1534575x 1534575x 1534575x 1534575x 1534575x 1534575x 4748883x 4748883x 48026x   4700857x 4748883x 4748883x   1534575x                                 597x 597x     1534575x 1534575x 1534575x 1533804x   771x 2666x 771x 771x     771x   771x       1533804x     1533804x 4746217x       1533804x       70649x 70649x 70649x 70649x 70649x 9x   70649x                                   71787x    
/**
 * The Be-Music Fingerprint documents (`docs/fingerprint-spec.md`, section 4): their types, the RFC 8785 serialization,
 * and the play document of one fully resolved outcome.
 */
import type { ChartPlayVariant } from './index.ts';
import { formatRational, isRationalEqual } from './fingerprint-rational.ts';
import {
  canonicalizeItems,
  formatJudge,
  formatLengths,
  formatTotal,
  type FingerprintJudge,
  type FingerprintLength,
  type FingerprintMine,
  type FingerprintNote,
  type FingerprintTimelineEvent,
  type FingerprintTotal,
  type FlatItems,
  type LaneModeExtension,
} from './fingerprint-items.ts';
import type { KeyboardModeHint } from './keyboard-mode-hint.ts';
import type { ChartPlayOptions } from './chart-play-options.ts';
 
export const FINGERPRINT_VERSION = 'bmfp0';
 
/**
 * Most outcome classes one control-flow factor may have (the product of its segments' path counts). A factor with more
 * is fingerprinted by its source lines instead (spec 3.7).
 */
export const FINGERPRINT_FACTOR_OUTCOME_LIMIT = 1024;
 
/** The lane-mode inputs that are not part of the parsed chart (spec 3.5). */
export interface BeMusicFingerprintOptions {
  /**
   * Path, file name, or bare extension of the chart, or `undefined` when unknown. Only its extension is read, exactly
   * as in `resolveChartPlayVariant`: `.pms` makes the chart 9 KEY, and any other extension counts as none. Part of the
   * fingerprint's input, so it is required.
   */
  chartPath: string | undefined;
}
 
export interface FingerprintMeasures {
  count: string;
  lengths: FingerprintLength[];
  /** `false` when the chart draws no bar lines (bmson `lines: []`). Omitted otherwise. */
  barLines?: false;
}
 
/**
 * The chart-authored gameplay options of a document (spec 2): the compiled `ChartPlayOptions`, string values and `true`
 * flags. Omitted from a document when the chart declares none.
 */
export type FingerprintPlayOptions = ChartPlayOptions;
 
/** Document of a chart with one outcome: the play fingerprint, and the chart fingerprint of a chart without `#RANDOM`. */
export interface BeMusicPlayFingerprintDocument {
  v: typeof FINGERPRINT_VERSION;
  laneMode: ChartPlayVariant;
  bpm: string;
  baseBPM: string | null;
  judge: FingerprintJudge;
  total: FingerprintTotal;
  measures: FingerprintMeasures;
  notes: FingerprintNote[];
  mines: FingerprintMine[];
  timeline: FingerprintTimelineEvent[];
  playOptions?: FingerprintPlayOptions;
}
 
export interface FingerprintLaneModeInputs {
  /** `".pms"`, or `null` for any other or unknown extension. */
  extension: LaneModeExtension;
  player3: boolean;
  /** The explicit bmson keyboard or generic hint; omitted for hints that do not determine lane identity. */
  modeHint?: KeyboardModeHint | `generic-${number}keys`;
  /** The LRX `lanetype`; omitted for a chart that is not LRX or names no mode. */
  lrxLaneType?: number;
}
 
export interface FingerprintDeltaLists {
  notes?: FingerprintNote[];
  mines?: FingerprintMine[];
  timeline?: FingerprintTimelineEvent[];
  lengths?: FingerprintLength[];
}
 
export interface FingerprintHeaderChange {
  bpm?: string;
  baseBPM?: string | null;
  judge?: FingerprintJudge;
  total?: FingerprintTotal;
  player3?: boolean;
  /** The outcome's gameplay options in full; `{}` when the outcome has none. */
  playOptions?: FingerprintPlayOptions;
}
 
export interface FingerprintBranch {
  /** The values of the variable this branch stands for: `"n"` or an inclusive range `"low-high"`, ascending. */
  values: string[];
  add?: FingerprintDeltaLists;
  remove?: FingerprintDeltaLists;
  header?: FingerprintHeaderChange;
  /** A later variable the outcome also depends on. */
  next?: FingerprintRandomNode;
}
 
export interface FingerprintRandomNode {
  random: string;
  branches: FingerprintBranch[];
}
 
/** A control-flow factor over the evaluation budget, identified by the SHA-256 of its source lines. */
export interface FingerprintOpaqueNode {
  opaque: string;
}
 
export type FingerprintControlFlowNode = FingerprintRandomNode | FingerprintOpaqueNode;
 
/** Document of a chart whose outcome depends on `#RANDOM` / `#SWITCH`. */
export interface BeMusicChartFingerprintDocument {
  v: typeof FINGERPRINT_VERSION;
  lanes: FingerprintLaneModeInputs;
  bpm: string;
  baseBPM: string | null;
  judge: FingerprintJudge;
  total: FingerprintTotal;
  measures: FingerprintMeasures;
  notes: FingerprintNote[];
  mines: FingerprintMine[];
  timeline: FingerprintTimelineEvent[];
  random: FingerprintControlFlowNode[];
  /** The gameplay options outside control flow; omitted when there are none. */
  playOptions?: FingerprintPlayOptions;
}
 
export type BeMusicFingerprintDocument = BeMusicPlayFingerprintDocument | BeMusicChartFingerprintDocument;
 
/**
 * Serializes a JSON value with the JSON Canonicalization Scheme (RFC 8785): object keys sorted by UTF-16 code units,
 * `undefined` fields left out, no whitespace, ECMAScript string and number forms. Fingerprints hash this text.
 */
export function canonicalizeJSONValue(value: unknown): string {
  if (value === null) {
    return 'null';
  }
  if (typeof value === 'string' || typeof value === 'boolean') {
    return JSON.stringify(value);
  }
  if (typeof value === 'number') {
    Iif (!Number.isFinite(value)) {
      throw new TypeError('canonical JSON cannot represent a non-finite number');
    }
    return JSON.stringify(value);
  }
  if (Array.isArray(value)) {
    let text = '[';
    for (let index = 0; index < value.length; index += 1) {
      if (index > 0) {
        text += ',';
      }
      text += canonicalizeJSONValue(value[index]);
    }
    return `${text}]`;
  }
  Eif (typeof value === 'object') {
    const record = value as Record<string, unknown>;
    const order = keyOrderOf(Object.keys(record));
    let text = '{';
    let first = true;
    for (let index = 0; index < order.sorted.length; index += 1) {
      const field = record[order.sorted[index]!];
      if (field === undefined) {
        continue;
      }
      text += first ? order.quoted[index]! : `,${order.quoted[index]!}`;
      text += canonicalizeJSONValue(field);
      first = false;
    }
    return `${text}}`;
  }
  throw new TypeError(`canonical JSON cannot represent a ${typeof value}`);
}
 
/** An object's keys in canonical (code unit) order, each quoted with its colon, for `canonicalizeJSONValue`. */
interface KeyOrder {
  keys: readonly string[];
  sorted: readonly string[];
  quoted: readonly string[];
}
 
/**
 * The fingerprint documents hold many objects of a few shapes (notes, mines, timeline items): their key orders are
 * worked out once per shape, keyed by the keys as `Object.keys` lists them. A hit is checked key by key, so two shapes
 * whose joined keys collide never share an order. Cleared if odd input ever fills it.
 */
const KEY_ORDERS = new Map<string, KeyOrder>();
const KEY_ORDER_LIMIT = 1024;
 
function keyOrderOf(keys: string[]): KeyOrder {
  const signature = keys.join('\u0000');
  const cached = KEY_ORDERS.get(signature);
  if (cached !== undefined && sameKeys(cached.keys, keys)) {
    return cached;
  }
  const sorted = [...keys].sort();
  const order: KeyOrder = { keys, sorted, quoted: sorted.map((key) => `${JSON.stringify(key)}:`) };
  Eif (cached === undefined) {
    Iif (KEY_ORDERS.size >= KEY_ORDER_LIMIT) {
      KEY_ORDERS.clear();
    }
    KEY_ORDERS.set(signature, order);
  }
  return order;
}
 
function sameKeys(left: readonly string[], right: readonly string[]): boolean {
  Iif (left.length !== right.length) {
    return false;
  }
  for (let index = 0; index < left.length; index += 1) {
    Iif (left[index] !== right[index]) {
      return false;
    }
  }
  return true;
}
 
export function assemblePlayDocument(items: FlatItems, extension: LaneModeExtension): BeMusicPlayFingerprintDocument {
  const lists = canonicalizeItems(items, extension);
  const count = lists.maxMeasure + 1;
  const { header } = items;
  const measures: FingerprintMeasures = { count: String(count), lengths: formatLengths(items.lengths, count) };
  if (!header.barLines) {
    measures.barLines = false;
  }
  return {
    v: FINGERPRINT_VERSION,
    laneMode: lists.laneMode,
    bpm: formatRational(header.bpm),
    baseBPM:
      header.baseBPM === null || isRationalEqual(header.baseBPM, header.bpm) ? null : formatRational(header.baseBPM),
    judge: formatJudge(header.judge),
    total: formatTotal(header.total),
    measures,
    notes: lists.notes,
    mines: lists.mines,
    timeline: lists.timeline,
    ...formatPlayOptionsField(header.playOptions),
  };
}
 
/** The `playOptions` field of a document: omitted when the chart declares no gameplay options. */
export function formatPlayOptionsField(options: FingerprintPlayOptions): { playOptions?: FingerprintPlayOptions } {
  return Object.keys(options).length > 0 ? { playOptions: { ...options } } : {};
}