import { useCallback, useEffect, useMemo, useState } from "react"; import { ButtonItem, ConfirmModal, DropdownItem, Field, PanelSection, PanelSectionRow, SliderField, Router, showModal, } from "@decky/ui"; import { cleanupManagedGame, getGameConfig, listInstalledGames, logError, saveGameConfig, } from "../api"; import { safeAsyncOperation } from "../utils"; import type { ApiResponse, GameConfigResponse, GameInfo } from "../types/index"; import { STYLES } from "../utils/constants"; const DEFAULT_LAUNCH_COMMAND = "~/fgmod/fgmod %COMMAND%"; const POLL_INTERVAL_MS = 3000; type RunningApp = { appid: string; display_name: string; }; const PROXY_OPTIONS = ["winmm", "dxgi", "version", "dbghelp", "winhttp", "wininet", "d3d12"]; const UPSCALER_OPTIONS = ["auto", "fsr31", "xess", "dlss", "native"]; const TRI_STATE_OPTIONS = ["auto", "true", "false"]; const FG_INPUT_OPTIONS = ["auto", "fsrfg", "xefg", "dlssg"]; const FG_OUTPUT_OPTIONS = ["auto", "fsrfg", "xefg"]; const defaultQuickSettings = { Dx12Upscaler: "auto", "FrameGen.Enabled": "auto", FGInput: "auto", FGOutput: "auto", Fsr4ForceCapable: "false", Fsr4EnableWatermark: "false", UseHQFont: "false", "Menu.Scale": "1.000000", }; interface InstalledGamesSectionProps { isAvailable: boolean; } const normalizeSettings = (settings?: Record) => ({ ...defaultQuickSettings, ...(settings || {}), }); const formatResult = (result: ApiResponse | null, fallbackSuccess: string) => { if (!result) return ""; if (result.status === "success") { return result.message || result.output || fallbackSuccess; } return `Error: ${result.message || result.output || "Operation failed"}`; }; export function InstalledGamesSection({ isAvailable }: InstalledGamesSectionProps) { const [games, setGames] = useState([]); const [selectedGame, setSelectedGame] = useState(null); const [runningApps, setRunningApps] = useState([]); const [mainRunningApp, setMainRunningApp] = useState(null); const [loadingGames, setLoadingGames] = useState(false); const [enabling, setEnabling] = useState(false); const [disabling, setDisabling] = useState(false); const [configLoading, setConfigLoading] = useState(false); const [configResult, setConfigResult] = useState(""); const [config, setConfig] = useState(null); const [quickSettings, setQuickSettings] = useState>(defaultQuickSettings); const [selectedProxy, setSelectedProxy] = useState("winmm"); const [rawIni, setRawIni] = useState(""); const [savingQuick, setSavingQuick] = useState(false); const [savingQuickLive, setSavingQuickLive] = useState(false); const [savingRaw, setSavingRaw] = useState(false); const [savingRawLive, setSavingRawLive] = useState(false); const selectedAppId = selectedGame ? String(selectedGame.appid) : null; const selectedIsRunning = useMemo( () => Boolean(selectedAppId && runningApps.some((app) => String(app.appid) === selectedAppId)), [runningApps, selectedAppId] ); const refreshRunningApps = useCallback(() => { try { const nextRunningApps = ((Router?.RunningApps || []) as RunningApp[]) .filter((app) => app?.appid && app?.display_name) .map((app) => ({ appid: String(app.appid), display_name: app.display_name })); const nextMainRunningApp = Router?.MainRunningApp ? { appid: String(Router.MainRunningApp.appid), display_name: Router.MainRunningApp.display_name, } : nextRunningApps[0] || null; setRunningApps(nextRunningApps); setMainRunningApp(nextMainRunningApp); if (!selectedGame && nextMainRunningApp) { setSelectedGame({ appid: nextMainRunningApp.appid, name: nextMainRunningApp.display_name, }); } } catch (error) { console.error("InstalledGamesSection.refreshRunningApps", error); } }, [selectedGame]); const loadConfig = useCallback( async (appid: string) => { setConfigLoading(true); const response = await safeAsyncOperation(() => getGameConfig(appid), `InstalledGamesSection.loadConfig.${appid}`); if (!response) { setConfigLoading(false); return; } if (response.status === "success") { setConfig(response); setQuickSettings(normalizeSettings(response.settings)); setSelectedProxy(response.proxy || "winmm"); setRawIni(response.raw_ini || ""); setConfigResult(""); } else { setConfig(null); setConfigResult(`Error: ${response.message || response.output || "Failed to load game config"}`); } setConfigLoading(false); }, [] ); useEffect(() => { if (!isAvailable) return; let cancelled = false; const fetchGames = async () => { setLoadingGames(true); const response = await safeAsyncOperation(async () => await listInstalledGames(), "InstalledGamesSection.fetchGames"); if (cancelled || !response) { setLoadingGames(false); return; } if (response.status === "success") { const sortedGames = [...response.games] .map((game) => ({ ...game, appid: parseInt(String(game.appid), 10), })) .sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase())); setGames(sortedGames); } else { logError(`InstalledGamesSection.fetchGames: ${JSON.stringify(response)}`); } setLoadingGames(false); }; fetchGames(); refreshRunningApps(); const interval = setInterval(refreshRunningApps, POLL_INTERVAL_MS); return () => { cancelled = true; clearInterval(interval); }; }, [isAvailable, refreshRunningApps]); useEffect(() => { if (!isAvailable || !selectedAppId) return; void loadConfig(selectedAppId); }, [isAvailable, loadConfig, selectedAppId]); const handleEnable = async () => { if (!selectedGame) return; showModal( { setEnabling(true); try { await SteamClient.Apps.SetAppLaunchOptions(selectedGame.appid, DEFAULT_LAUNCH_COMMAND); setConfigResult(`✓ Enabled prefix-managed OptiScaler for ${selectedGame.name}. Launch the game, enable DLSS if needed, then press Insert for the OptiScaler menu.`); } catch (error) { logError(`InstalledGamesSection.handleEnable: ${String(error)}`); setConfigResult(error instanceof Error ? `Error: ${error.message}` : "Error enabling prefix-managed OptiScaler"); } finally { setEnabling(false); } }} /> ); }; const handleDisable = async () => { if (!selectedGame) return; setDisabling(true); try { const cleanupResult = await cleanupManagedGame(String(selectedGame.appid)); if (cleanupResult?.status !== "success") { setConfigResult(`Error: ${cleanupResult?.message || cleanupResult?.output || "Failed to clean managed compatdata prefix"}`); return; } await SteamClient.Apps.SetAppLaunchOptions(selectedGame.appid, ""); setConfig(null); setQuickSettings(defaultQuickSettings); setSelectedProxy("winmm"); setRawIni(""); setConfigResult(`✓ Cleared launch options and cleaned the managed compatdata prefix for ${selectedGame.name}.`); } catch (error) { logError(`InstalledGamesSection.handleDisable: ${String(error)}`); setConfigResult(error instanceof Error ? `Error: ${error.message}` : "Error disabling prefix-managed OptiScaler"); } finally { setDisabling(false); } }; const saveQuickSettings = async (applyLive: boolean) => { if (!selectedAppId) return; const setBusy = applyLive ? setSavingQuickLive : setSavingQuick; setBusy(true); try { const response = await saveGameConfig(selectedAppId, quickSettings, selectedProxy, applyLive, null); setConfigResult(formatResult(response, applyLive ? "Applied config to the running game." : "Saved config.")); await loadConfig(selectedAppId); } catch (error) { setConfigResult(error instanceof Error ? `Error: ${error.message}` : "Error saving config"); } finally { setBusy(false); } }; const saveRawEditor = async (applyLive: boolean) => { if (!selectedAppId) return; const setBusy = applyLive ? setSavingRawLive : setSavingRaw; setBusy(true); try { const response = await saveGameConfig(selectedAppId, {}, selectedProxy, applyLive, rawIni); setConfigResult(formatResult(response, applyLive ? "Applied raw INI to the running game." : "Saved raw INI.")); await loadConfig(selectedAppId); } catch (error) { setConfigResult(error instanceof Error ? `Error: ${error.message}` : "Error saving raw INI"); } finally { setBusy(false); } }; if (!isAvailable) return null; return (
{runningApps.length > 0 ? runningApps.map((app) => app.display_name).join("\n") : "Idle"}
{mainRunningApp ? ( setSelectedGame({ appid: mainRunningApp.appid, name: mainRunningApp.display_name, }) } > Use current running game ) : null} ({ data: String(game.appid), label: game.name, }))} selectedOption={selectedAppId} onChange={(option) => { const game = games.find((entry) => String(entry.appid) === String(option.data)); setSelectedGame(game || null); setConfigResult(""); }} strDefaultLabel={loadingGames ? "Loading installed games..." : "Choose a game"} menuLabel="Installed Steam games" disabled={loadingGames || games.length === 0} />
Enable writes the wrapper launch option automatically. Disable clears launch options and removes staged files from the selected game's compatdata prefix. The config editor below persists changes to the selected game and can also mirror them into the live prefix copy while the game is running.
{selectedGame ? ( <> {selectedGame.name} {enabling ? "Enabling..." : "Enable for selected game"} {disabling ? "Cleaning..." : "Disable and clean selected game"} loadConfig(String(selectedGame.appid))} disabled={configLoading}> {configLoading ? "Loading config..." : "Reload selected game config"} ) : null} {configResult ? (
{configResult.startsWith("Error") ? "❌" : "✅"} {configResult}
) : null} {selectedGame && config ? ( <>
{config.paths?.managed_ini ? `Managed INI: ${config.paths.managed_ini}\n` : ""} {config.paths?.live_ini ? `Live INI: ${config.paths.live_ini}` : ""}
({ data: proxy, label: proxy }))} selectedOption={selectedProxy} onChange={(option) => setSelectedProxy(String(option.data))} menuLabel="Proxy DLL" strDefaultLabel="Proxy DLL" description="Persisted per game and used by the wrapper on next launch." /> ({ data: value, label: value }))} selectedOption={quickSettings.Dx12Upscaler} onChange={(option) => setQuickSettings((prev) => ({ ...prev, Dx12Upscaler: String(option.data) }))} menuLabel="DX12 upscaler" strDefaultLabel="DX12 upscaler" /> ({ data: value, label: value }))} selectedOption={quickSettings["FrameGen.Enabled"]} onChange={(option) => setQuickSettings((prev) => ({ ...prev, "FrameGen.Enabled": String(option.data) }))} menuLabel="Frame generation enabled" strDefaultLabel="Frame generation enabled" /> ({ data: value, label: value }))} selectedOption={quickSettings.FGInput} onChange={(option) => setQuickSettings((prev) => ({ ...prev, FGInput: String(option.data) }))} menuLabel="FG input" strDefaultLabel="FG input" /> ({ data: value, label: value }))} selectedOption={quickSettings.FGOutput} onChange={(option) => setQuickSettings((prev) => ({ ...prev, FGOutput: String(option.data) }))} menuLabel="FG output" strDefaultLabel="FG output" /> ({ data: value, label: value }))} selectedOption={quickSettings.Fsr4ForceCapable} onChange={(option) => setQuickSettings((prev) => ({ ...prev, Fsr4ForceCapable: String(option.data) }))} menuLabel="FSR4 force capable" strDefaultLabel="FSR4 force capable" /> ({ data: value, label: value }))} selectedOption={quickSettings.Fsr4EnableWatermark} onChange={(option) => setQuickSettings((prev) => ({ ...prev, Fsr4EnableWatermark: String(option.data) }))} menuLabel="FSR4 watermark" strDefaultLabel="FSR4 watermark" /> ({ data: value, label: value }))} selectedOption={quickSettings.UseHQFont} onChange={(option) => setQuickSettings((prev) => ({ ...prev, UseHQFont: String(option.data) }))} menuLabel="Use HQ font" strDefaultLabel="Use HQ font" /> setQuickSettings((prev) => ({ ...prev, "Menu.Scale": value.toFixed(6), })) } /> saveQuickSettings(false)} disabled={savingQuick || savingQuickLive}> {savingQuick ? "Saving..." : "Apply + persist quick settings"} saveQuickSettings(true)} disabled={!selectedIsRunning || savingQuick || savingQuickLive} > {savingQuickLive ? "Applying live..." : "Apply quick settings to running game now"}