import { app } from 'electron'; import * as fs from 'fs'; import * as path from 'path'; export type AutoUpdateMode = 'auto' | 'off' | 'version'; export interface DesktopSettings { autoUpdateMode: AutoUpdateMode; hardwareAcceleration: boolean; manifestUrls: string[]; preferredVersion: string | null; vaapiVideoEncode: boolean; } export interface DesktopSettingsSnapshot extends DesktopSettings { runtimeHardwareAcceleration: boolean; restartRequired: boolean; } const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = { autoUpdateMode: 'auto', hardwareAcceleration: true, manifestUrls: [], preferredVersion: null, vaapiVideoEncode: false }; function normalizeAutoUpdateMode(value: unknown): AutoUpdateMode { return value === 'off' || value === 'version' ? value : 'auto'; } function normalizePreferredVersion(value: unknown): string | null { return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null; } function normalizeManifestUrls(value: unknown): string[] { if (!Array.isArray(value)) { return []; } const manifestUrls: string[] = []; for (const entry of value) { if (typeof entry !== 'string') { continue; } const nextUrl = entry.trim(); if (!nextUrl || manifestUrls.includes(nextUrl)) { continue; } manifestUrls.push(nextUrl); } return manifestUrls; } export function getDesktopSettingsSnapshot(): DesktopSettingsSnapshot { const storedSettings = readDesktopSettings(); const runtimeHardwareAcceleration = app.isHardwareAccelerationEnabled(); return { ...storedSettings, runtimeHardwareAcceleration, restartRequired: storedSettings.hardwareAcceleration !== runtimeHardwareAcceleration }; } export function readDesktopSettings(): DesktopSettings { const filePath = getDesktopSettingsPath(); try { if (!fs.existsSync(filePath)) { return { ...DEFAULT_DESKTOP_SETTINGS }; } const raw = fs.readFileSync(filePath, 'utf8'); const parsed = JSON.parse(raw) as Partial; return { autoUpdateMode: normalizeAutoUpdateMode(parsed.autoUpdateMode), vaapiVideoEncode: typeof parsed.vaapiVideoEncode === 'boolean' ? parsed.vaapiVideoEncode : DEFAULT_DESKTOP_SETTINGS.vaapiVideoEncode, hardwareAcceleration: typeof parsed.hardwareAcceleration === 'boolean' ? parsed.hardwareAcceleration : DEFAULT_DESKTOP_SETTINGS.hardwareAcceleration, manifestUrls: normalizeManifestUrls(parsed.manifestUrls), preferredVersion: normalizePreferredVersion(parsed.preferredVersion) }; } catch { return { ...DEFAULT_DESKTOP_SETTINGS }; } } export function updateDesktopSettings(patch: Partial): DesktopSettingsSnapshot { const mergedSettings = { ...readDesktopSettings(), ...patch }; const nextSettings: DesktopSettings = { autoUpdateMode: normalizeAutoUpdateMode(mergedSettings.autoUpdateMode), hardwareAcceleration: typeof mergedSettings.hardwareAcceleration === 'boolean' ? mergedSettings.hardwareAcceleration : DEFAULT_DESKTOP_SETTINGS.hardwareAcceleration, manifestUrls: normalizeManifestUrls(mergedSettings.manifestUrls), preferredVersion: normalizePreferredVersion(mergedSettings.preferredVersion), vaapiVideoEncode: typeof mergedSettings.vaapiVideoEncode === 'boolean' ? mergedSettings.vaapiVideoEncode : DEFAULT_DESKTOP_SETTINGS.vaapiVideoEncode }; const filePath = getDesktopSettingsPath(); fs.mkdirSync(path.dirname(filePath), { recursive: true }); fs.writeFileSync(filePath, JSON.stringify(nextSettings, null, 2), 'utf8'); return getDesktopSettingsSnapshot(); } function getDesktopSettingsPath(): string { return path.join(app.getPath('userData'), 'desktop-settings.json'); }