/** Version-independent read-only asset rules for space-saving hardlink copies. */ import { readFileSync } from 'node:fs'; import { parseJsonc } from '@wto/jsonc'; import { GAME_FILES } from './game-copy.ts'; export interface HardlinkResourceRules { readonly allowedDirectories: readonly string[]; readonly allowedExtensions: readonly string[]; readonly minBytes: number; } const TUNING_FILE = new URL('../tuning/hardlink-resources.jsonc', import.meta.url); const PROTECTED_FILES = new Set(Object.values(GAME_FILES).map((path) => path.replaceAll('\\', '/').toLowerCase())); let cached: HardlinkResourceRules | null = null; export function hardlinkResourceRules(): HardlinkResourceRules { if (cached) return cached; const value = parseJsonc(readFileSync(TUNING_FILE, 'utf8')); if (typeof value !== 'object' || value === null || Array.isArray(value)) { throw tuningError('the file is not an object'); } const fields = value as Record; const allowedDirectories = readDirectories(fields, 'allowed_directories'); const allowedExtensions = readExtensions(fields, 'allowed_extensions'); const minBytes = readNonNegativeInteger(fields, 'min_bytes'); cached = { allowedDirectories, allowedExtensions, minBytes }; return cached; } /** Whether a relative path inside a stock client is a static read-only asset safe to hardlink. */ export function isHardlinkableResource(relativePath: string, bytes: number): boolean { const rules = hardlinkResourceRules(); if (!Number.isFinite(bytes) || bytes < 0 || bytes < rules.minBytes) return false; const normalized = relativePath.replaceAll('\\', '/').toLowerCase(); if (!normalized || normalized.startsWith('/') || normalized.includes('../') || normalized === '..') return false; if (normalized.startsWith('wtoffline/') || PROTECTED_FILES.has(normalized)) return false; return rules.allowedDirectories.some((dir) => normalized.startsWith(dir)) && rules.allowedExtensions.some((ext) => normalized.endsWith(ext)); } function readDirectories(fields: Record, key: string): readonly string[] { const raw = fields[key]; if (!Array.isArray(raw) || raw.length === 0) throw tuningError(`"${key}" must be a non-empty string array`); return raw.map((item) => { if (typeof item !== 'string' || !item.endsWith('/') || item.startsWith('/') || item.includes('\\') || item.includes('..')) { throw tuningError(`"${key}" entries must be lowercase relative prefixes ending with "/"`); } return item.toLowerCase(); }); } function readExtensions(fields: Record, key: string): readonly string[] { const raw = fields[key]; if (!Array.isArray(raw) || raw.length === 0) throw tuningError(`"${key}" must be a non-empty string array`); return raw.map((item) => { if (typeof item !== 'string' || !item.startsWith('.') || item.length < 2 || item.includes('/') || item.includes('\\')) { throw tuningError(`"${key}" entries must be file extensions starting with "."`); } return item.toLowerCase(); }); } function readNonNegativeInteger(fields: Record, key: string): number { const value = fields[key]; if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) { throw tuningError(`"${key}" must be a non-negative integer`); } return value; } function tuningError(detail: string): Error { return new Error(`tuning/hardlink-resources.jsonc: ${detail}`); }