summaryrefslogtreecommitdiff
path: root/src/hooks
diff options
context:
space:
mode:
authorxXJSONDeruloXx <danielhimebauch@gmail.com>2026-09-10 12:15:05 -0400
committerxXJSONDeruloXx <danielhimebauch@gmail.com>2026-09-10 12:15:05 -0400
commit670f36e8cc75da9c8b1b174c24e722657bbf2a56 (patch)
tree84207004aae74ca2e6d5abf6233a6eb837fc3d92 /src/hooks
parentb197e25b45d53c6c7175a45dd0b14642f2aab198 (diff)
downloaddecky-lsfg-vk-670f36e8cc75da9c8b1b174c24e722657bbf2a56.tar.gz
decky-lsfg-vk-670f36e8cc75da9c8b1b174c24e722657bbf2a56.zip
cleanup flatpak handles
Diffstat (limited to 'src/hooks')
-rw-r--r--src/hooks/useGameConfiguration.ts176
-rw-r--r--src/hooks/usePerAppWorkarounds.ts83
2 files changed, 70 insertions, 189 deletions
diff --git a/src/hooks/useGameConfiguration.ts b/src/hooks/useGameConfiguration.ts
index e120426..3260019 100644
--- a/src/hooks/useGameConfiguration.ts
+++ b/src/hooks/useGameConfiguration.ts
@@ -3,7 +3,7 @@ import { useQuickAccessVisible } from "@decky/api";
import { Router } from "@decky/ui";
import { ensureFlatpakSupport, getGameConfigs, getInstalledGames, getWorkaroundState, removeWorkaroundState, repairFlatpakSupport, resetGameConfig, resetAllGameConfigs, setWorkaroundState, updateGameConfig, type GameConfigEntry, type GlobalConfig, type InstalledGame, type WorkaroundState } from "../api/lsfgApi";
import { ConfigurationData, getDefaults } from "../config/configSchema";
-import { cleanupLegacySteamLaunchOptions, getDefaultWrapperPath, hasWrapperLaunchIntegration, installWrapperIntegration, isLegacyWrapperToken, readSteamLaunchOptions, removeWrapperIntegration } from "../utils/steamLaunchOptions";
+import { getDefaultWrapperPath, installWrapperIntegration, removeWrapperIntegration } from "../utils/steamLaunchOptions";
import { showErrorToast } from "../utils/toastUtils";
export interface GameTarget extends InstalledGame { configured: boolean; }
@@ -40,18 +40,6 @@ function mergeInstalledGames(backendGames: InstalledGame[], shortcutGames: Insta
return Array.from(games.values());
}
-function selectShortcutExecutable(
- target: GameTarget,
- ...candidates: Array<string | null | undefined>
-): string | undefined {
- const absolute = candidates
- .map((candidate) => candidate?.trim())
- .find((candidate) => candidate && candidate.startsWith("/"));
- if (absolute) return absolute;
- if (target.transport.kind === "flatpak") return "/usr/bin/flatpak";
- return candidates.map((candidate) => candidate?.trim()).find(Boolean);
-}
-
const DEFAULT_WORKAROUND_STATE: WorkaroundState = {
dxvkFrameRate: 0,
disableGamescopeWsi: true,
@@ -144,110 +132,59 @@ export function useGameConfiguration() {
const ensureTargetWorkarounds = useCallback(async (target: GameTarget): Promise<boolean> => {
if (!installedGames.some((game) => game.appid === target.appid)) return true;
const appId = Number(target.appid);
+ let integration: Awaited<ReturnType<typeof installWrapperIntegration>> | null = null;
+ let newState = false;
+ let stateWriteAttempted = false;
+ let wrapperPath = getDefaultWrapperPath();
try {
const existing = await getWorkaroundState(target.appid);
if (!existing.success) throw new Error(existing.error || "Could not read workaround state");
- const current = await readSteamLaunchOptions(appId, target.nonSteam);
- const wrapperPath = existing.wrapper_path || getDefaultWrapperPath();
- const oldState = existing.state;
- const oldShortcutExe = existing.shortcut_exe || undefined;
- const oldCommandTokenAdded = existing.command_token_added === true;
- const oldTransport = existing.transport || target.transport;
- const usesShortcutTarget = target.nonSteam && target.transport.kind === "flatpak";
- if (usesShortcutTarget && oldState && current.target === wrapperPath && !oldShortcutExe) {
- throw new Error("Managed shortcut Target has no saved original executable");
- }
- if (usesShortcutTarget && oldState && current.target !== wrapperPath && current.target !== oldShortcutExe) {
- throw new Error("Shortcut Target changed externally; refusing to replace it");
- }
- if (usesShortcutTarget && !oldState && current.target === wrapperPath) {
- throw new Error("Shortcut Target is already the managed wrapper but its original Target is unknown");
- }
- const state = oldState || { ...DEFAULT_WORKAROUND_STATE };
- const originalExecutable = usesShortcutTarget
- ? selectShortcutExecutable(
- target,
- oldShortcutExe,
- target.executable,
- current.target,
- )
- : undefined;
- const initialIntegration = usesShortcutTarget
- ? current.target === wrapperPath
- : hasWrapperLaunchIntegration(current.options, wrapperPath);
- const initialStateResult = await setWorkaroundState(
+ wrapperPath = existing.wrapper_path || getDefaultWrapperPath();
+ const state = existing.state || { ...DEFAULT_WORKAROUND_STATE };
+ const commandTokenAdded = existing.command_token_added === true;
+ newState = !existing.state;
+ integration = await installWrapperIntegration(
+ appId,
+ target.nonSteam,
+ wrapperPath,
+ commandTokenAdded,
+ target.transport,
+ target.transport.kind === "flatpak" ? existing.shortcut_exe || undefined : undefined,
+ );
+ stateWriteAttempted = true;
+ const saved = await setWorkaroundState(
target.appid,
state,
- originalExecutable || null,
- oldCommandTokenAdded,
+ integration.originalExecutable ?? null,
+ integration.commandTokenAdded,
target.transport,
);
- if (!initialStateResult.success) throw new Error(initialStateResult.error || "Could not create workaround state");
-
- let integration: Awaited<ReturnType<typeof installWrapperIntegration>> | null = null;
- try {
- integration = await installWrapperIntegration(
- appId,
- target.nonSteam,
- wrapperPath,
- oldCommandTokenAdded,
- target.transport.kind,
- );
- const finalStateResult = await setWorkaroundState(
- target.appid,
- state,
- usesShortcutTarget
- ? (selectShortcutExecutable(
- target,
- integration.originalExecutable,
- originalExecutable,
- target.executable,
- ) || null)
- : null,
- integration.commandTokenAdded,
- target.transport,
- );
- if (!finalStateResult.success) throw new Error(finalStateResult.error || "Could not finalize workaround state");
- return true;
- } catch (error) {
- let rollbackSucceeded = true;
- if (!initialIntegration && integration) {
- try {
- await removeWrapperIntegration(
- appId,
- target.nonSteam,
- wrapperPath,
- usesShortcutTarget
- ? (selectShortcutExecutable(
- target,
- integration?.originalExecutable,
- originalExecutable,
- target.executable,
- ) || undefined)
- : undefined,
- integration?.commandTokenAdded ?? oldCommandTokenAdded,
- target.transport.kind,
- );
- } catch (rollbackError) {
- showErrorToast("Workaround rollback failed", asError(rollbackError).message);
- rollbackSucceeded = false;
- }
+ if (!saved.success) throw new Error(saved.error || "Could not save workaround state");
+ return true;
+ } catch (error) {
+ let rollbackSucceeded = true;
+ if (integration?.changed) {
+ try {
+ await removeWrapperIntegration(
+ appId,
+ target.nonSteam,
+ wrapperPath,
+ integration.originalExecutable,
+ integration.commandTokenAdded,
+ target.transport,
+ );
+ } catch (rollbackError) {
+ showErrorToast("Workaround rollback failed", asError(rollbackError).message);
+ rollbackSucceeded = false;
}
- if (rollbackSucceeded) {
- const restored = oldState
- ? await setWorkaroundState(
- target.appid,
- oldState,
- oldShortcutExe || null,
- oldCommandTokenAdded,
- oldTransport,
- )
- : await removeWorkaroundState(target.appid);
- if (!restored.success) throw new Error(restored.error || "Could not roll back workaround state");
+ }
+ if (rollbackSucceeded && newState && stateWriteAttempted) {
+ const restored = await removeWorkaroundState(target.appid);
+ if (!restored.success) {
+ showErrorToast("Workaround rollback failed", restored.error || "Could not roll back workaround state");
+ rollbackSucceeded = false;
}
- throw error;
}
- } catch (error) {
showErrorToast("Could not initialize workarounds", asError(error).message);
return false;
}
@@ -260,23 +197,14 @@ export function useGameConfiguration() {
const existing = await getWorkaroundState(target.appid);
if (!existing.success) throw new Error(existing.error || "Could not read workaround state");
const wrapperPath = existing.wrapper_path || getDefaultWrapperPath();
- const usesShortcutTarget = target.nonSteam && target.transport.kind === "flatpak";
- if (existing.state) {
- await removeWrapperIntegration(
- appId,
- target.nonSteam,
- wrapperPath,
- existing.shortcut_exe || undefined,
- existing.command_token_added === true,
- target.transport.kind,
- );
- } else {
- const current = await readSteamLaunchOptions(appId, target.nonSteam);
- if (usesShortcutTarget && (current.target === wrapperPath || isLegacyWrapperToken(current.target))) {
- throw new Error("Shortcut Target is a frame-generation wrapper but its original Target is unknown");
- }
- await cleanupLegacySteamLaunchOptions(appId, target.nonSteam, wrapperPath);
- }
+ await removeWrapperIntegration(
+ appId,
+ target.nonSteam,
+ wrapperPath,
+ target.transport.kind === "flatpak" ? existing.shortcut_exe || undefined : undefined,
+ existing.command_token_added === true,
+ target.transport,
+ );
const removed = await removeWorkaroundState(target.appid);
if (!removed.success) throw new Error(removed.error || "Could not remove workaround state");
return true;
diff --git a/src/hooks/usePerAppWorkarounds.ts b/src/hooks/usePerAppWorkarounds.ts
index ebb12e2..29e1181 100644
--- a/src/hooks/usePerAppWorkarounds.ts
+++ b/src/hooks/usePerAppWorkarounds.ts
@@ -7,10 +7,10 @@ import {
type WorkaroundState,
} from "../api/lsfgApi";
import {
+ assertKnownShortcutTarget,
getDefaultWrapperPath,
- hasWrapperLaunchIntegration,
installWrapperIntegration,
- isLegacyWrapperToken,
+ isWrapperIntegrationInstalled,
readSteamLaunchOptions,
removeWrapperIntegration,
subscribeSteamLaunchOptions,
@@ -61,33 +61,6 @@ function asError(error: unknown): Error {
return error instanceof Error ? error : new Error(String(error));
}
-function selectShortcutExecutable(
- transport: TargetTransport,
- ...candidates: Array<string | null | undefined>
-): string | undefined {
- const absolute = candidates
- .map((candidate) => candidate?.trim())
- .find((candidate) => candidate && candidate.startsWith("/"));
- if (absolute) return absolute;
- if (transport.kind === "flatpak") return "/usr/bin/flatpak";
- return candidates.map((candidate) => candidate?.trim()).find(Boolean);
-}
-
-function usesShortcutTarget(nonSteam: boolean, transport: TargetTransport): boolean {
- return nonSteam && transport.kind === "flatpak";
-}
-
-function integrationIsInstalled(
- steam: SteamLaunchOptionsSnapshot,
- nonSteam: boolean,
- transport: TargetTransport,
- wrapperPath: string,
-): boolean {
- return usesShortcutTarget(nonSteam, transport)
- ? steam.target === wrapperPath
- : hasWrapperLaunchIntegration(steam.options, wrapperPath);
-}
-
function makeSnapshot(
steam: SteamLaunchOptionsSnapshot,
result: Awaited<ReturnType<typeof getWorkaroundState>>,
@@ -97,17 +70,16 @@ function makeSnapshot(
if (!result.state) throw new Error("Workaround state is not initialized for this profile");
const wrapperPath = result.wrapper_path || getDefaultWrapperPath();
const selectedTransport = result.transport || transport;
- if (usesShortcutTarget(nonSteam, selectedTransport) && steam.target === wrapperPath && !result.shortcut_exe) {
- throw new Error("Managed shortcut Target has no saved original executable");
- }
+ const shortcutExe = selectedTransport.kind === "flatpak" ? result.shortcut_exe : undefined;
+ assertKnownShortcutTarget(steam, nonSteam, selectedTransport, wrapperPath, shortcutExe);
return {
steam,
state: result.state,
wrapperPath,
wrapperOwned: result.wrapper_owned === true,
- integrationInstalled: integrationIsInstalled(steam, nonSteam, selectedTransport, wrapperPath),
+ integrationInstalled: isWrapperIntegrationInstalled(steam, nonSteam, selectedTransport, wrapperPath),
commandTokenAdded: result.command_token_added === true,
- shortcutExe: result.shortcut_exe,
+ shortcutExe,
transport: selectedTransport,
};
}
@@ -116,24 +88,8 @@ async function adoptWorkaroundState(
appId: string,
nonSteam: boolean,
transport: TargetTransport,
- steam: SteamLaunchOptionsSnapshot,
wrapperPath: string,
): Promise<WorkaroundSnapshot> {
- const shortcutTarget = usesShortcutTarget(nonSteam, transport);
- if (shortcutTarget && (!steam.target || steam.target === wrapperPath || isLegacyWrapperToken(steam.target))) {
- throw new Error("Shortcut Target is a wrapper but its original Target is unknown");
- }
- const originalExecutable = shortcutTarget
- ? selectShortcutExecutable(transport, steam.target)
- : null;
- const initial = await setWorkaroundState(
- appId,
- DEFAULT_WORKAROUND_STATE,
- originalExecutable,
- false,
- transport,
- );
- if (!initial.success) throw new Error(initial.error || "Could not create workaround state");
let integration: Awaited<ReturnType<typeof installWrapperIntegration>> | null = null;
try {
integration = await installWrapperIntegration(
@@ -141,14 +97,12 @@ async function adoptWorkaroundState(
nonSteam,
wrapperPath,
false,
- transport.kind,
+ transport,
);
const finalized = await setWorkaroundState(
appId,
DEFAULT_WORKAROUND_STATE,
- shortcutTarget
- ? (selectShortcutExecutable(transport, integration.originalExecutable, originalExecutable) || null)
- : null,
+ integration.originalExecutable ?? null,
integration.commandTokenAdded,
transport,
);
@@ -156,17 +110,15 @@ async function adoptWorkaroundState(
return makeSnapshot(integration.snapshot, finalized, nonSteam, transport);
} catch (error) {
let rollbackSucceeded = true;
- if (integration) {
+ if (integration?.changed) {
try {
await removeWrapperIntegration(
Number(appId),
nonSteam,
wrapperPath,
- shortcutTarget
- ? (selectShortcutExecutable(transport, integration?.originalExecutable, originalExecutable) || undefined)
- : undefined,
- integration?.commandTokenAdded ?? false,
- transport.kind,
+ integration.originalExecutable,
+ integration.commandTokenAdded,
+ transport,
);
} catch {
// Leave the owned integration in place rather than guessing at cleanup.
@@ -203,7 +155,6 @@ export function usePerAppWorkarounds(
appId,
nonSteam,
transport,
- steam,
result.wrapper_path || getDefaultWrapperPath(),
);
}
@@ -243,7 +194,7 @@ export function usePerAppWorkarounds(
setSnapshot((current) => current ? {
...current,
steam,
- integrationInstalled: integrationIsInstalled(steam, nonSteam, current.transport, current.wrapperPath),
+ integrationInstalled: isWrapperIntegrationInstalled(steam, nonSteam, current.transport, current.wrapperPath),
} : current);
},
(subscriptionError) => {
@@ -278,22 +229,24 @@ export function usePerAppWorkarounds(
setError(null);
const nextState = { ...current.state, [field]: value } as WorkaroundState;
try {
+ const shortcutExe = current.transport.kind === "flatpak" ? current.shortcutExe ?? null : null;
const result = await setWorkaroundState(
appId,
nextState,
- current.shortcutExe ?? null,
+ shortcutExe,
current.commandTokenAdded,
current.transport,
);
if (!result.success || !result.state) throw new Error(result.error || "Could not save workaround state");
+ const selectedTransport = result.transport || current.transport;
applySnapshot({
...current,
state: result.state,
wrapperPath: result.wrapper_path || current.wrapperPath,
wrapperOwned: result.wrapper_owned === true,
- shortcutExe: result.shortcut_exe,
+ shortcutExe: selectedTransport.kind === "flatpak" ? result.shortcut_exe : undefined,
commandTokenAdded: result.command_token_added === true,
- transport: result.transport || current.transport,
+ transport: selectedTransport,
});
return true;
} catch (updateError) {