summaryrefslogtreecommitdiff
path: root/src/components/SteamGamePatcher.tsx
blob: b17ed4853d723f5df755a9a9133c3e0a7a2b7c83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { useCallback, useEffect, useMemo, useState } from "react";
import { ButtonItem, DropdownItem, Field, PanelSectionRow } from "@decky/ui";
import { toaster } from "@decky/api";
import { listInstalledGames, getGameStatus, patchGame, unpatchGame } from "../api";

// ─── SteamClient helpers ─────────────────────────────────────────────────────

const getAppLaunchOptions = (appId: number): Promise<string> =>
  new Promise((resolve, reject) => {
    if (typeof SteamClient === "undefined" || !SteamClient?.Apps?.RegisterForAppDetails) {
      resolve("");
      return;
    }
    let settled = false;
    let unregister = () => {};
    const timeout = window.setTimeout(() => {
      if (settled) return;
      settled = true;
      unregister();
      reject(new Error("Timed out reading launch options."));
    }, 5000);
    const registration = SteamClient.Apps.RegisterForAppDetails(
      appId,
      (details: { strLaunchOptions?: string }) => {
        if (settled) return;
        settled = true;
        window.clearTimeout(timeout);
        unregister();
        resolve(details?.strLaunchOptions ?? "");
      }
    );
    unregister = registration.unregister;
  });

const setAppLaunchOptions = (appId: number, options: string): void => {
  if (typeof SteamClient !== "undefined" && SteamClient?.Apps?.SetAppLaunchOptions) {
    SteamClient.Apps.SetAppLaunchOptions(appId, options);
  }
};

// ─── Types ───────────────────────────────────────────────────────────────────

type GameEntry = { appid: string; name: string; install_found?: boolean };

type GameStatus = {
  status: "success" | "error";
  message?: string;
  install_found?: boolean;
  patched?: boolean;
  dll_name?: string | null;
  target_dir?: string | null;
  patched_at?: string | null;
};

// ─── Module-level state persistence ──────────────────────────────────────────

let lastSelectedAppId = "";

// ─── Component ───────────────────────────────────────────────────────────────

interface SteamGamePatcherProps {
  dllName: string;
}

export function SteamGamePatcher({ dllName }: SteamGamePatcherProps) {
  const [games, setGames] = useState<GameEntry[]>([]);
  const [gamesLoading, setGamesLoading] = useState(true);
  const [selectedAppId, setSelectedAppId] = useState<string>(() => lastSelectedAppId);
  const [gameStatus, setGameStatus] = useState<GameStatus | null>(null);
  const [statusLoading, setStatusLoading] = useState(false);
  const [busyAction, setBusyAction] = useState<"patch" | "unpatch" | null>(null);
  const [resultMessage, setResultMessage] = useState<string>("");

  // ── Data loaders ───────────────────────────────────────────────────────────

  const loadGames = useCallback(async () => {
    setGamesLoading(true);
    try {
      const result = await listInstalledGames();
      if (result.status !== "success") throw new Error(result.message || "Failed to load games.");
      const gameList = result.games as GameEntry[];
      setGames(gameList);
      if (!gameList.length) {
        lastSelectedAppId = "";
        setSelectedAppId("");
        return;
      }
      setSelectedAppId((current) => {
        const valid =
          current && gameList.some((g) => g.appid === current) ? current : gameList[0].appid;
        lastSelectedAppId = valid;
        return valid;
      });
    } catch (err) {
      const msg = err instanceof Error ? err.message : "Failed to load games.";
      toaster.toast({ title: "Decky Framegen", body: msg });
    } finally {
      setGamesLoading(false);
    }
  }, []);

  const loadStatus = useCallback(async (appid: string) => {
    if (!appid) {
      setGameStatus(null);
      return;
    }
    setStatusLoading(true);
    try {
      const result = await getGameStatus(appid);
      setGameStatus(result as GameStatus);
    } catch (err) {
      setGameStatus({
        status: "error",
        message: err instanceof Error ? err.message : "Failed to load status.",
      });
    } finally {
      setStatusLoading(false);
    }
  }, []);

  useEffect(() => {
    void loadGames();
  }, [loadGames]);

  useEffect(() => {
    if (!selectedAppId) {
      setGameStatus(null);
      return;
    }
    void loadStatus(selectedAppId);
  }, [selectedAppId, loadStatus]);

  // ── Derived state ──────────────────────────────────────────────────────────

  const selectedGame = useMemo(
    () => games.find((g) => g.appid === selectedAppId) ?? null,
    [games, selectedAppId]
  );

  const isPatchedWithDifferentDll =
    gameStatus?.patched && gameStatus?.dll_name && gameStatus.dll_name !== dllName;

  const canPatch = Boolean(selectedGame && gameStatus?.install_found && !busyAction);
  const canUnpatch = Boolean(selectedGame && gameStatus?.patched && !busyAction);

  const patchButtonLabel = useMemo(() => {
    if (busyAction === "patch") return "Patching...";
    if (!selectedGame) return "Patch this game";
    if (!gameStatus?.install_found) return "Install not found";
    if (isPatchedWithDifferentDll) return `Switch to ${dllName}`;
    if (gameStatus?.patched) return `Reinstall (${dllName})`;
    return `Patch with ${dllName}`;
  }, [busyAction, dllName, gameStatus, isPatchedWithDifferentDll, selectedGame]);

  // ── Actions ────────────────────────────────────────────────────────────────

  const handlePatch = useCallback(async () => {
    if (!selectedGame || !selectedAppId || busyAction) return;
    setBusyAction("patch");
    setResultMessage("");
    try {
      let currentLaunchOptions = "";
      try {
        currentLaunchOptions = await getAppLaunchOptions(Number(selectedAppId));
      } catch {
        // non-fatal: proceed without current launch options
      }
      const result = await patchGame(selectedAppId, dllName, currentLaunchOptions);
      if (result.status !== "success") throw new Error(result.message || "Patch failed.");
      setAppLaunchOptions(Number(selectedAppId), result.launch_options || "");
      const msg = result.message || `Patched ${selectedGame.name}.`;
      setResultMessage(msg);
      toaster.toast({ title: "Decky Framegen", body: msg });
      await loadStatus(selectedAppId);
    } catch (err) {
      const msg = err instanceof Error ? err.message : "Patch failed.";
      setResultMessage(`Error: ${msg}`);
      toaster.toast({ title: "Decky Framegen", body: msg });
    } finally {
      setBusyAction(null);
    }
  }, [busyAction, dllName, loadStatus, selectedAppId, selectedGame]);

  const handleUnpatch = useCallback(async () => {
    if (!selectedGame || !selectedAppId || busyAction) return;
    setBusyAction("unpatch");
    setResultMessage("");
    try {
      const result = await unpatchGame(selectedAppId);
      if (result.status !== "success") throw new Error(result.message || "Unpatch failed.");
      setAppLaunchOptions(Number(selectedAppId), result.launch_options || "");
      const msg = result.message || `Unpatched ${selectedGame.name}.`;
      setResultMessage(msg);
      toaster.toast({ title: "Decky Framegen", body: msg });
      await loadStatus(selectedAppId);
    } catch (err) {
      const msg = err instanceof Error ? err.message : "Unpatch failed.";
      setResultMessage(`Error: ${msg}`);
      toaster.toast({ title: "Decky Framegen", body: msg });
    } finally {
      setBusyAction(null);
    }
  }, [busyAction, loadStatus, selectedAppId, selectedGame]);

  // ── Status display ─────────────────────────────────────────────────────────

  const statusDisplay = useMemo(() => {
    if (!selectedGame) return { text: "—", color: undefined as string | undefined };
    if (statusLoading) return { text: "Loading...", color: undefined };
    if (!gameStatus || gameStatus.status === "error")
      return { text: gameStatus?.message || "—", color: undefined };
    if (!gameStatus.install_found) return { text: "Install not found", color: "#ffd866" };
    if (!gameStatus.patched) return { text: "Not patched", color: undefined };
    const dllLabel = gameStatus.dll_name || "unknown";
    if (isPatchedWithDifferentDll)
      return { text: `Patched (${dllLabel}) — switch available`, color: "#ffd866" };
    return { text: `Patched (${dllLabel})`, color: "#3fb950" };
  }, [gameStatus, isPatchedWithDifferentDll, selectedGame, statusLoading]);

  const focusableFieldProps = { focusable: true, highlightOnFocus: true } as const;

  // ── Render ─────────────────────────────────────────────────────────────────

  return (
    <>
      <PanelSectionRow>
        <DropdownItem
          label="Steam game"
          menuLabel="Select a Steam game"
          strDefaultLabel={gamesLoading ? "Loading games..." : "Choose a game"}
          disabled={gamesLoading || games.length === 0}
          selectedOption={selectedAppId}
          rgOptions={games.map((g) => ({
            data: g.appid,
            label: g.install_found === false ? `${g.name} (not installed)` : g.name,
          }))}
          onChange={(option) => {
            const next = String(option.data);
            lastSelectedAppId = next;
            setSelectedAppId(next);
            setResultMessage("");
          }}
        />
      </PanelSectionRow>

      {selectedGame && (
        <>
          <PanelSectionRow>
            <Field {...focusableFieldProps} label="Patch status">
              {statusDisplay.color ? (
                <span style={{ color: statusDisplay.color, fontWeight: 600 }}>
                  {statusDisplay.text}
                </span>
              ) : (
                statusDisplay.text
              )}
            </Field>
          </PanelSectionRow>

          <PanelSectionRow>
            <ButtonItem layout="below" disabled={!canPatch} onClick={handlePatch}>
              {patchButtonLabel}
            </ButtonItem>
          </PanelSectionRow>

          {canUnpatch && (
            <PanelSectionRow>
              <ButtonItem
                layout="below"
                disabled={busyAction !== null}
                onClick={handleUnpatch}
              >
                {busyAction === "unpatch" ? "Unpatching..." : "Unpatch this game"}
              </ButtonItem>
            </PanelSectionRow>
          )}

          <PanelSectionRow>
            <ButtonItem
              layout="below"
              disabled={!selectedAppId || busyAction !== null || statusLoading}
              onClick={() => void loadStatus(selectedAppId)}
            >
              {statusLoading ? "Refreshing..." : "Refresh status"}
            </ButtonItem>
          </PanelSectionRow>

          {resultMessage && (
            <PanelSectionRow>
              <Field {...focusableFieldProps} label="Result">
                {resultMessage}
              </Field>
            </PanelSectionRow>
          )}
        </>
      )}
    </>
  );
}