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
300
301
302
303
|
import { useCallback, useEffect, useMemo, useState } from "react";
import { ButtonItem, Field, PanelSectionRow, ToggleField } from "@decky/ui";
import { FileSelectionType, openFilePicker } from "@decky/api";
import { getPathDefaults, runManualPatch, runManualUnpatch } from "../api";
import type { ApiResponse } from "../types/index";
import { SmartClipboardButton } from "./SmartClipboardButton";
interface PathDefaults {
home: string;
steamCommon: string;
}
const DEFAULT_HOME = "/home";
const DEFAULT_STEAM_COMMON = "/home/deck/.local/share/Steam/steamapps/common";
const INITIAL_DEFAULTS: PathDefaults = {
home: DEFAULT_HOME,
steamCommon: DEFAULT_STEAM_COMMON,
};
const normalizePath = (value: string) => value.replace(/\\/g, "/");
const stripTrailingSlash = (value: string) =>
value.length > 1 && value.endsWith("/") ? value.slice(0, -1) : value;
const ensureDirectory = (value: string) => {
const normalized = normalizePath(value);
const lastSegment = normalized.substring(normalized.lastIndexOf("/") + 1);
if (!lastSegment || !lastSegment.includes(".")) {
return stripTrailingSlash(normalized);
}
const parent = normalized.slice(0, normalized.lastIndexOf("/"));
return parent || "/";
};
interface ManualPatchControlsProps {
isAvailable: boolean;
onManualModeChange?: (enabled: boolean) => void;
}
interface PickerState {
selectedPath: string | null;
lastError: string | null;
}
const INITIAL_PICKER_STATE: PickerState = {
selectedPath: null,
lastError: null,
};
const formatResultMessage = (result: ApiResponse | null) => {
if (!result) return null;
if (result.status === "success") {
return result.message || result.output || "Operation completed successfully.";
}
return result.message || result.output || "Operation failed.";
};
export const ManualPatchControls = ({ isAvailable, onManualModeChange }: ManualPatchControlsProps) => {
const [isEnabled, setEnabled] = useState(false);
const [defaults, setDefaults] = useState<PathDefaults>(INITIAL_DEFAULTS);
const [pickerState, setPickerState] = useState<PickerState>(INITIAL_PICKER_STATE);
const [isPatching, setIsPatching] = useState(false);
const [isUnpatching, setIsUnpatching] = useState(false);
const [operationResult, setOperationResult] = useState<ApiResponse | null>(null);
const [lastOperation, setLastOperation] = useState<"patch" | "unpatch" | null>(null);
useEffect(() => {
let cancelled = false;
(async () => {
try {
const response = await getPathDefaults();
if (!response || cancelled) return;
const home = response.home ? normalizePath(response.home) : DEFAULT_HOME;
const steamCommon = response.steam_common
? normalizePath(response.steam_common)
: normalizePath(`${stripTrailingSlash(home)}/.local/share/Steam/steamapps/common`);
setDefaults({
home,
steamCommon: steamCommon || DEFAULT_STEAM_COMMON,
});
} catch (err) {
console.error("ManualPatchControls -> getPathDefaults", err);
}
})();
return () => {
cancelled = true;
};
}, []);
useEffect(() => {
if (!isAvailable) {
setEnabled(false);
setPickerState(INITIAL_PICKER_STATE);
setOperationResult(null);
setLastOperation(null);
onManualModeChange?.(false);
}
}, [isAvailable, onManualModeChange]);
const canInteract = isAvailable && isEnabled;
const selectedPath = pickerState.selectedPath;
const statusMessage = useMemo(() => formatResultMessage(operationResult), [operationResult]);
const wasSuccessful = operationResult?.status === "success";
const statusLabel = useMemo(() => {
if (!operationResult || !lastOperation) return null;
if (operationResult.status === "success") {
return lastOperation === "patch" ? "Game patched" : "Game unpatched";
}
return lastOperation === "patch" ? "Patch failed" : "Unpatch failed";
}, [lastOperation, operationResult]);
const openDirectoryPicker = useCallback(async () => {
const candidates = [
selectedPath,
defaults.steamCommon,
defaults.home,
];
let lastError: string | null = null;
for (const candidate of candidates) {
if (!candidate) continue;
const startPath = ensureDirectory(candidate);
try {
const result = await openFilePicker(
FileSelectionType.FOLDER,
startPath,
true,
true,
undefined,
undefined,
true
);
if (result?.path) {
setPickerState({ selectedPath: normalizePath(result.path), lastError: null });
setOperationResult(null);
return;
}
} catch (err) {
console.error("ManualPatchControls -> openDirectoryPicker", err);
lastError = err instanceof Error ? err.message : String(err);
}
}
setPickerState((prev) => ({ ...prev, lastError }));
}, [defaults.home, defaults.steamCommon, selectedPath]);
const runOperation = useCallback(
async (action: "patch" | "unpatch") => {
if (!selectedPath) return;
const setBusy = action === "patch" ? setIsPatching : setIsUnpatching;
setLastOperation(action);
setBusy(true);
setOperationResult(null);
try {
const response =
action === "patch"
? await runManualPatch(selectedPath)
: await runManualUnpatch(selectedPath);
setOperationResult(response ?? { status: "error", message: "No response from backend." });
} catch (err) {
setOperationResult({
status: "error",
message: err instanceof Error ? err.message : String(err),
});
} finally {
setBusy(false);
}
},
[selectedPath]
);
const handleToggle = (value: boolean) => {
if (!isAvailable) {
setEnabled(false);
return;
}
setEnabled(value);
onManualModeChange?.(value);
if (!value) {
setPickerState(INITIAL_PICKER_STATE);
setOperationResult(null);
setLastOperation(null);
}
};
const busy = isPatching || isUnpatching;
return (
<>
<PanelSectionRow>
<ToggleField
label="Manual Patch Controls"
description={
isAvailable
? "Manually apply OptiScaler to a specific game directory."
: "Install OptiScaler first to enable manual patching."
}
checked={isEnabled && isAvailable}
disabled={!isAvailable}
onChange={handleToggle}
/>
</PanelSectionRow>
{canInteract && (
<>
<SmartClipboardButton
command='WINEDLLOVERRIDES="dxgi=n,b" SteamDeck=0 %command%'
buttonText="Manual launch cmd"
/>
<PanelSectionRow>
<ButtonItem
layout="below"
onClick={openDirectoryPicker}
description="Choose the game's installation directory (where the EXE lives)."
>
Select directory
</ButtonItem>
</PanelSectionRow>
{pickerState.lastError && (
<PanelSectionRow>
<Field
label="Picker error"
description={pickerState.lastError}
>
⚠️
</Field>
</PanelSectionRow>
)}
{selectedPath && (
<>
<PanelSectionRow>
<Field
label="Target directory"
description="OptiScaler files will be copied here."
>
<div
style={{
fontFamily: "monospace",
backgroundColor: "rgba(255, 255, 255, 0.05)",
border: "1px solid rgba(255, 255, 255, 0.1)",
borderRadius: "4px",
padding: "6px 8px",
width: "100%",
boxSizing: "border-box",
whiteSpace: "pre-wrap",
wordBreak: "break-word",
}}
>
{selectedPath}
</div>
</Field>
</PanelSectionRow>
<PanelSectionRow>
<ButtonItem
layout="below"
disabled={busy}
onClick={() => runOperation("patch")}
>
{isPatching ? "Patching..." : "Patch directory"}
</ButtonItem>
</PanelSectionRow>
<PanelSectionRow>
<ButtonItem
layout="below"
disabled={busy}
onClick={() => runOperation("unpatch")}
>
{isUnpatching ? "Reverting..." : "Unpatch directory"}
</ButtonItem>
</PanelSectionRow>
</>
)}
{operationResult && (
<PanelSectionRow>
<Field
label={statusLabel ?? (wasSuccessful ? "Last action succeeded" : "Last action failed")}
>
{!wasSuccessful && statusMessage ? statusMessage : null}
</Field>
</PanelSectionRow>
)}
</>
)}
</>
);
};
|