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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
import { useCallback, useEffect, useMemo, useState } from "react";
import { ButtonItem, Field, PanelSectionRow, ToggleField } from "@decky/ui";
import { FileSelectionType, openFilePicker } from "@decky/api";
import { getPathDefaults } from "../api";
import type { CustomOverrideConfig } from "../types/index";
interface CustomPathOverrideProps {
onOverrideChange: (override: CustomOverrideConfig | null) => void;
}
const DEFAULT_START_PATH = "/home";
const DEFAULT_STEAM_LIBRARY_PATH = "/home/deck/.local/share/Steam/steamapps/common";
interface PathDefaults {
home: string;
steamCommon: string;
}
const INITIAL_PATH_DEFAULTS: PathDefaults = {
home: DEFAULT_START_PATH,
steamCommon: DEFAULT_STEAM_LIBRARY_PATH,
};
const normalizePath = (path: string) => path.replace(/\\/g, "/");
const stripTrailingSlash = (value: string) =>
value.endsWith("/") ? value.slice(0, -1) : value;
const escapeForDoubleQuotes = (value: string) =>
value.replace(/[`"\\$]/g, (match) => `\\${match}`);
const escapeForPattern = (value: string) =>
value
.replace(/\\/g, "\\\\")
.replace(/\//g, "\\/")
.replace(/\[/g, "\\[")
.replace(/\]/g, "\\]")
.replace(/\*/g, "\\*")
.replace(/\?/g, "\\?");
const escapeForReplacement = (value: string) =>
value
.replace(/\\/g, "\\\\")
.replace(/\//g, "\\/")
.replace(/\$/g, "\\$");
const quoteForShell = (value: string) => `'${value.replace(/'/g, "'\\''")}'`;
const dirname = (path: string) => {
const normalized = normalizePath(path);
const parts = normalized.split("/");
parts.pop();
const dir = parts.join("/");
return dir.length > 0 ? dir : "/";
};
const longestCommonPrefix = (left: string[], right: string[]) => {
const length = Math.min(left.length, right.length);
let idx = 0;
while (idx < length && left[idx] === right[idx]) {
idx++;
}
return idx;
};
interface ComputedOverride {
config: CustomOverrideConfig | null;
error: string | null;
}
const buildOverride = (
rawDefault: string | null,
rawOverride: string | null
): ComputedOverride => {
if (!rawDefault || !rawOverride) {
return { config: null, error: null };
}
const defaultPath = normalizePath(rawDefault.trim());
const overridePath = normalizePath(rawOverride.trim());
if (defaultPath === overridePath) {
return {
config: null,
error: "Paths are identical. Choose a different target executable.",
};
}
const defaultParts = defaultPath.split("/").filter(Boolean);
const overrideParts = overridePath.split("/").filter(Boolean);
if (!defaultParts.length || !overrideParts.length) {
return {
config: null,
error: "Unable to parse selected paths. Pick them again.",
};
}
const prefixLength = longestCommonPrefix(defaultParts, overrideParts);
if (prefixLength < 2) {
return {
config: null,
error: "Selections do not share a common game folder.",
};
}
const searchSuffixParts = defaultParts.slice(prefixLength);
const replaceSuffixParts = overrideParts.slice(prefixLength);
if (!searchSuffixParts.length || !replaceSuffixParts.length) {
return {
config: null,
error: "Could not determine differing portion of the paths.",
};
}
const searchSuffix = searchSuffixParts.join("/");
const replaceSuffix = replaceSuffixParts.join("/");
const pattern = defaultParts[prefixLength - 1] ?? defaultParts[defaultParts.length - 1];
if (!pattern) {
return {
config: null,
error: "Unable to infer game identifier from path.",
};
}
const escapedPattern = escapeForDoubleQuotes(pattern);
const escapedSearch = escapeForPattern(searchSuffix);
const escapedReplace = escapeForReplacement(replaceSuffix);
const expression = `[[ "$arg" == *"${escapedPattern}"* ]] && arg=\${arg//${escapedSearch}/${escapedReplace}}`;
const snippet = `[[ "$arg" == *"${pattern}"* ]] && arg=\${arg//${searchSuffix}/${replaceSuffix}}`;
const envAssignment = `FGMOD_OVERRIDE_EXPRESSION=${quoteForShell(expression)}`;
const config: CustomOverrideConfig = {
defaultPath,
overridePath,
pattern,
searchSuffix,
replaceSuffix,
expression,
snippet,
envAssignment,
};
return { config, error: null };
};
export const CustomPathOverride = ({ onOverrideChange }: CustomPathOverrideProps) => {
const [launcherPath, setLauncherPath] = useState<string | null>(null);
const [overridePath, setOverridePath] = useState<string | null>(null);
const [isEnabled, setEnabled] = useState(false);
const [pathDefaults, setPathDefaults] = useState<PathDefaults>(INITIAL_PATH_DEFAULTS);
useEffect(() => {
let cancelled = false;
const fetchDefaults = async () => {
try {
const result = await getPathDefaults();
if (!result) {
return;
}
const home = result.home ? normalizePath(result.home) : INITIAL_PATH_DEFAULTS.home;
const steamCommonSource = result.steam_common
? normalizePath(result.steam_common)
: normalizePath(`${stripTrailingSlash(home)}/.local/share/Steam/steamapps/common`);
if (!cancelled) {
setPathDefaults({
home,
steamCommon: steamCommonSource || INITIAL_PATH_DEFAULTS.steamCommon,
});
}
} catch (err) {
console.error("CustomPathOverride -> getPathDefaults", err);
}
};
fetchDefaults();
return () => {
cancelled = true;
};
}, []);
const { config, error } = useMemo(
() => buildOverride(launcherPath, overridePath),
[launcherPath, overridePath]
);
useEffect(() => {
if (isEnabled && config) {
onOverrideChange(config);
} else {
onOverrideChange(null);
}
}, [config, isEnabled, onOverrideChange]);
interface PickerArgs {
existing: string | null;
setter: (value: string) => void;
fallbackStart?: string | null;
}
const openPicker = useCallback(
async ({ existing, setter, fallbackStart }: PickerArgs) => {
const candidates = new Set<string>();
if (existing) {
candidates.add(normalizePath(existing));
} else {
if (fallbackStart) {
candidates.add(normalizePath(fallbackStart));
}
candidates.add(pathDefaults.steamCommon);
candidates.add(pathDefaults.home);
}
let lastError: unknown = null;
for (const candidate of candidates) {
if (!candidate) {
continue;
}
try {
const result = await openFilePicker(
FileSelectionType.FILE,
candidate,
true,
true,
undefined,
undefined,
true
);
if (result?.path) {
setter(normalizePath(result.path));
return;
}
} catch (err) {
lastError = err;
}
}
if (lastError) {
console.error("CustomPathOverride -> openPicker", lastError);
}
},
[pathDefaults]
);
const handleToggle = (value: boolean) => {
setEnabled(value);
if (!value) {
setLauncherPath(null);
setOverridePath(null);
onOverrideChange(null);
} else if (config) {
onOverrideChange(config);
}
};
return (
<>
<PanelSectionRow>
<ToggleField
label="Custom Launcher Override"
description="Select launcher and target executables from the Deck's file browser."
checked={isEnabled}
onChange={handleToggle}
/>
</PanelSectionRow>
{isEnabled && (
<>
<PanelSectionRow>
<ButtonItem
layout="below"
onClick={() =>
openPicker({
existing: launcherPath,
setter: setLauncherPath,
fallbackStart: pathDefaults.steamCommon,
})
}
description={launcherPath || "Pick the EXE Steam currently uses."}
>
Select Steam-provided EXE
</ButtonItem>
</PanelSectionRow>
<PanelSectionRow>
<ButtonItem
layout="below"
disabled={!launcherPath}
onClick={() =>
launcherPath &&
openPicker({
existing: overridePath,
setter: setOverridePath,
fallbackStart: launcherPath ? dirname(launcherPath) : pathDefaults.steamCommon,
})
}
description={
launcherPath
? overridePath || "Pick the executable that should run instead."
: "Select the Steam-provided executable first."
}
>
Select Override EXE
</ButtonItem>
</PanelSectionRow>
{(launcherPath || overridePath) && (
<PanelSectionRow>
<ButtonItem
layout="below"
onClick={() => {
setLauncherPath(null);
setOverridePath(null);
}}
>
Clear selections
</ButtonItem>
</PanelSectionRow>
)}
{error && (
<PanelSectionRow>
<Field
label="Override status"
description={error}
>
⚠️
</Field>
</PanelSectionRow>
)}
{!error && config && (
<>
<PanelSectionRow>
<Field
label="Detected game"
description="Based on the shared folder between both selections."
>
{config.pattern}
</Field>
</PanelSectionRow>
<PanelSectionRow>
<Field
label="Code snippet"
description="This is added to fgmod before resolving the install path."
bottomSeparator="none"
>
<div
style={{
backgroundColor: "rgba(255,255,255,0.05)",
borderRadius: "6px",
padding: "12px",
fontFamily: "monospace",
whiteSpace: "pre-wrap",
wordBreak: "break-word",
}}
>
{config.snippet}
</div>
</Field>
</PanelSectionRow>
<PanelSectionRow>
<Field
label="Environment preview"
description="Automatically appended to the Patch command."
>
{config.envAssignment}
</Field>
</PanelSectionRow>
</>
)}
</>
)}
</>
);
};
|