Files
WT-Offline/packages/patcher/src/hardlink-resources.ts
T
N0VAN0M4LY bf2ce539e7 feat(launcher): use version-independent rules for space-saving copies
Replace the single-version 2.61.0.43 SHA-256 hardlink manifest with declarative read-only asset rules in packages/patcher/tuning/hardlink-resources.jsonc (content/ and content.hq/ .dxp.bin and .grp archives with no minimum size cutoff). Space-saving installs and conversions now work across minor updates, and updating a space-saving client from a same-volume source preserves or creates hard links instead of detaching them into full copies.
2026-12-02 17:04:36 +08:00

75 lines
3.4 KiB
TypeScript

/** 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<string, unknown>;
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<string, unknown>, 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<string, unknown>, 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<string, unknown>, 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}`);
}