import { file, write } from "bun"; import { join } from "node:path"; import { ID } from "@node-steam/id"; import { readVdf, type VdfMap, writeVdf } from "steam-binary-vdf"; import { caseInsensitiveProxy } from "../../utils/proxy.ts"; import { getUserConfigFolderPath } from "./path.ts"; type numericBoolean = 0 | 1; /** One of Steam's binary `shortcuts.vdf` files, parsed. */ export type Shortcuts = { shortcuts: Record; }; /** A Shortcut entry for Steam's binary `shortcuts.vdf` files.s */ export type Shortcut = Shortcuts["shortcuts"][string]; /** Gets the shortcuts for the most recent user of Steam on this computer. */ export function getShortcuts(): Promise; /** * Gets the shortcuts for the Steam user matching `userId`. * * @param userId */ export function getShortcuts(userId: ID): Promise; /** * Gets the shortcuts for the Steam user matching `userId`. * * @param userId */ export function getShortcuts(userId: string): Promise; /** * Gets the shortcuts for the Steam user matching `userId`, or the most recent * user of Steam on this computer if not specified. * * @param userId */ export function getShortcuts( userId?: ID | string, ): Promise; export async function getShortcuts( userId?: ID | string, ): Promise { const configPath = await getUserConfigFolderPath(userId); if (!configPath) return; const shortcutsPath = join(configPath, "shortcuts.vdf"); if (!await file(shortcutsPath).exists()) return { shortcuts: {} }; return new Proxy( readVdf(Buffer.from(await file(shortcutsPath).arrayBuffer())), caseInsensitiveProxy, ) as Shortcuts; } /** * Add `shortcut` to `shortcuts`. To save the changes, call `setShortcuts` on * the result. * * @param shortcut * @param shortcuts */ export const addShortcut = ( shortcut: Shortcut, shortcuts: Shortcuts, ): Shortcuts => { const keys = Object.keys(shortcuts.shortcuts); shortcuts .shortcuts[ keys.length === 0 ? 0 : Math.max(...keys.map((s) => +s)) + 1 ] = shortcut; return shortcuts; }; /** * Update shortcut by key `key`. To save changes, call `setShortcuts` on the * result. * * @param key * @param shortcut * @param shortcuts * @returns */ export const updateShortcut = ( key: string, shortcut: Shortcut, shortcuts: Shortcuts, ): Shortcuts => { shortcuts.shortcuts[key] = { ...(shortcuts.shortcuts[key] || {}), ...shortcut, }; return shortcuts; }; /** * Add all shortcuts in `arr` to `shortcuts`. To save the changes, call * `setShortcuts` on the result. * * @param arr * @param shortcuts */ export const addShortcuts = ( arr: Shortcut[], shortcuts: Shortcuts, ): Shortcuts => arr.reduce((acc, curr) => addShortcut(curr, acc), shortcuts); /** * Sets the shortcuts for the most recent user of Steam on this computer. * * It is highly advised to: * 1. Ensure Steam is not running, * 1. Call `getShortcuts` and modify as required, e.g. by calling * `addShortcuts`, * 1. Finally call `setShortcuts` with the modified shortcuts. * * @param shortcuts The shortcuts to set. _**Do not construct this by hand**_, * as it completely overwrites any shortcuts set for the user. */ export function setShortcuts(shortcuts: Shortcuts): Promise; /** * Sets the shortcuts for the user matching `userId`. * * It is highly advised to: * 1. Ensure Steam is not running, * 1. Call `getShortcuts` and modify as required, e.g. by calling * `addShortcuts`, * 1. Finally call `setShortcuts` with the modified shortcuts. * * @param shortcuts The shortcuts to set. _**Do not construct this by hand**_, * as it completely overwrites any shortcuts set for the user. * @param userId */ export function setShortcuts( shortcuts: Shortcuts, userId: ID, ): Promise; /** * Sets the shortcuts for the user matching `userId`. * * It is highly advised to: * 1. Ensure Steam is not running, * 1. Call `getShortcuts` and modify as required, e.g. by calling * `addShortcuts`, * 1. Finally call `setShortcuts` with the modified shortcuts. * * @param shortcuts The shortcuts to set. _**Do not construct this by hand**_, * as it completely overwrites any shortcuts set for the user. * @param userId */ export function setShortcuts( shortcuts: Shortcuts, userId: string, ): Promise; /** * Sets the shortcuts for the user matching `userId`, or the most recent user * of Steam on this computer if not specified. * * It is highly advised to: * 1. Ensure Steam is not running, * 1. Call `getShortcuts` and modify as required, e.g. by calling * `addShortcuts`, * 1. Finally call `setShortcuts` with the modified shortcuts. * * @param shortcuts The shortcuts to set. _**Do not construct this by hand**_, * as it completely overwrites any shortcuts set for the user. * @param userId */ export function setShortcuts( shortcuts: Shortcuts, userId?: ID | string, ): Promise; export async function setShortcuts(shortcuts: Shortcuts, userId?: ID | string) { const configPath = await getUserConfigFolderPath(userId); if (!configPath) return false; const shortcutsPath = join(configPath, "shortcuts.vdf"); await write(shortcutsPath, writeVdf(shortcuts as VdfMap)); return true; }