All files / skin-beatoraja/src json.ts

100% Statements 57/57
100% Branches 53/53
100% Functions 7/7
100% Lines 49/49

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                            57x 57x 57x 57x             39x                             45x 45x                                                     2072x 2072x 2072x 2072x 112564x 112564x   16795x 78367x 78367x 77574x   16795x   95769x   13785x 13785x 1147x 1147x   13785x   81984x             6511x 6511x 1268x 1268x   6511x   75473x   7709x 7709x 2313x 2313x       2072x     69x 69x 69x 69x     89212x       28005x 44413x 44413x   489x       7454x   5598x   5304x 5141x    
// JSON skin parsing.
//
// beatoraja `.json` skins use JSON5-style trailing commas in places (the reference theme's `play5.json` ends an
// `item` array with `,]`). We strip trailing commas before handing the text to `JSON.parse` so authors don't have to
// worry about that gotcha.
 
import { normalizeBeatorajaSkinCustomOffsets } from './types.ts';
import type { BeatorajaSkin, BeatorajaSkinHeader } from './types.ts';
 
/**
 * Parse a beatoraja JSON skin file into a typed tree. Throws on syntax errors; callers wrap that into their own
 * load-failure surface (consistent with the Lua evaluator's Result-typed error).
 */
export function parseBeatorajaSkinJSON(source: Uint8Array | string): BeatorajaSkin {
  const text = typeof source === 'string' ? source : new TextDecoder('utf-8').decode(source);
  const sanitized = relaxBeatorajaJSON(text);
  const parsed = JSON.parse(sanitized) as Record<string, unknown>;
  return parsed as BeatorajaSkin;
}
 
/**
 * Parses a JSON skin and returns its header without the main rendering arrays.
 */
export function parseBeatorajaSkinJSONHeader(source: Uint8Array | string): BeatorajaSkinHeader {
  return headerFromBeatorajaSkinJSON(parseBeatorajaSkinJSON(source));
}
 
/** Extracts a header from a JSON skin that has already been parsed. */
export function headerFromBeatorajaSkinJSON(skin: BeatorajaSkin): BeatorajaSkinHeader {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const {
    source: _src,
    font: _font,
    image: _img,
    value: _val,
    text: _txt,
    slider: _sl,
    destination: _dst,
    ...header
  } = skin;
  return {
    ...header,
    offset: normalizeBeatorajaSkinCustomOffsets((header as Readonly<Record<string, unknown>>).offset),
  } as BeatorajaSkinHeader;
}
 
/**
 * Relax a beatoraja JSON skin source into something `JSON.parse` will accept. The reference theme's `play*.json`
 * deviates from RFC 8259 in three ways verified against the bundled fixtures:
 *
 * 1. **Trailing commas** before `]` / `}` (e.g. `play5.json`'s property item array ends with `…},]`).
 * 2. **Missing commas** between adjacent objects or arrays — `}\n\t{` is accepted by beatoraja's parser as if a `,`
 *    had been written. This shows up in `play24.json` line 715 (`{...]}\n\t{...}` between `#15` and `notes`).
 * 3. **Doubled-open brace typo** `{{` in value position — `play5.json` line 307 emits
 *    `"value": {{"x":20, "y":140, ...}}` where the second `{` is a literal author typo (the matching close is
 *    NOT doubled — the `}}` at the end closes both the value object and the surrounding entry object as
 *    intended). libgdx's permissive parser silently drops the extra `{`; we mirror that by skipping the FIRST
 *    of any `{{` pair (and only the first — the closes already balance once that one is gone).
 *
 * We fix all three by walking the source character-by-character with a tiny state machine that tracks string
 * boundaries.
 *
 * Line / block comments and unquoted keys are NOT supported — none of the bundled skins use those, and
 * `JSON.parse` will reject them with a clear error if a community theme tries.
 */
export function relaxBeatorajaJSON(source: string): string {
  // Untouched runs are copied as slices: `start` is where the pending run begins, and an edit flushes it.
  let result = '';
  let start = 0;
  const length = source.length;
  for (let i = 0; i < length; i += 1) {
    const code = source.charCodeAt(i);
    if (code === QUOTE) {
      // Skip to the closing quote; nothing inside a string is relaxed.
      for (i += 1; i < length; i += 1) {
        const inner = source.charCodeAt(i);
        if (inner === BACKSLASH) i += 1;
        else if (inner === QUOTE) break;
      }
      continue;
    }
    if (code === COMMA) {
      // Trailing-comma drop: look ahead past whitespace; if we land on `]`/`}`, swallow the comma.
      const next = peekNonWhitespace(source, i + 1);
      if (next !== -1 && isClose(source.charCodeAt(next))) {
        result += source.slice(start, i);
        start = i + 1;
      }
      continue;
    }
    if (code === OPEN_BRACE) {
      // `{{` doubled-open typo: when the next non-whitespace character is another `{`, drop THIS one
      // (the outer of the pair) — `play5.json` line 307 has `"value": {{"x":20, …}},` where the
      // matching `}}` only closes the entry + value pair, NOT the typo `{`. So eliding the typo open
      // alone leaves the close braces correctly balanced. Skipping the SECOND `{` instead (which the
      // earlier draft did via a stack) would make the relaxer also drop a real close brace later, and
      // a depth-balanced object falls out unclosed.
      const next = peekNonWhitespace(source, i + 1);
      if (next !== -1 && source.charCodeAt(next) === OPEN_BRACE) {
        result += source.slice(start, i);
        start = i + 1;
      }
      continue;
    }
    if (isClose(code)) {
      // Missing-comma insert: if the next non-whitespace character starts a new value, inject a `,`.
      const next = peekNonWhitespace(source, i + 1);
      if (next !== -1 && startsNewElementChar(source[next]!)) {
        result += `${source.slice(start, i + 1)},`;
        start = i + 1;
      }
    }
  }
  return start === 0 ? source : result + source.slice(start);
}
 
const QUOTE = 0x22;
const BACKSLASH = 0x5c;
const COMMA = 0x2c;
const OPEN_BRACE = 0x7b;
 
function isClose(code: number): boolean {
  return code === 0x7d || code === 0x5d;
}
 
function peekNonWhitespace(source: string, from: number): number {
  for (let i = from; i < source.length; i += 1) {
    const c = source.charCodeAt(i);
    if (c !== 0x20 && c !== 0x09 && c !== 0x0a && c !== 0x0d) return i;
  }
  return -1;
}
 
function startsNewElementChar(ch: string): boolean {
  if (ch === '{' || ch === '[' || ch === '"') return true;
  // Numeric / sign starters.
  if ((ch >= '0' && ch <= '9') || ch === '-' || ch === '+') return true;
  // Bare keywords (`true`, `false`, `null`) — rarely seen in skins, but cheap to honor.
  if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) return true;
  return false;
}