Skip to content
1        10        20        30        40        50        60        70        801// Save slots kept in localStorage. Each slot holds one JSON document: a small2// label for the slot list, and the session state needed to resume play.34export const SAVE_SLOTS = 3;5export const SAVE_VERSION = 1;67const KEY_PREFIX = 'torment.save.';89function storage() {10  try {11    return window.localStorage;12  } catch {13    return null;14  }15}1617/** The stored record for a slot, or null when it is empty or unreadable. */18export function readSlot(slot) {19  const store = storage();20  if (!store) return null;21  let raw = null;22  try {23    raw = store.getItem(KEY_PREFIX + slot);24  } catch {25    return null;26  }27  if (!raw) return null;28  try {29    const record = JSON.parse(raw);30    if (record?.version !== SAVE_VERSION || !record.state || !record.label) return null;31    return record;32  } catch {33    return null;34  }35}3637export function writeSlot(slot, state, label) {38  const store = storage();39  if (!store) throw new Error('Saving needs browser storage, which is turned off.');40  const record = {41    version: SAVE_VERSION,42    savedAt: new Date().toISOString(),43    label,44    state,45  };46  try {47    store.setItem(KEY_PREFIX + slot, JSON.stringify(record));48  } catch {49    throw new Error('The game could not write the save. Browser storage may be full.');50  }51  return record;52}