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
|
// @ts-expect-error Node's built-in TypeScript loader requires explicit source extensions in tests.
import { cleanupLegacyLaunchOptions, isLegacyWrapperToken, normalizeLaunchOptions } from "./steamLaunchOptionParser.ts";
// @ts-expect-error Node's built-in TypeScript loader requires explicit source extensions in tests.
export * from "./steamLaunchOptionParser.ts";
export interface SteamLaunchOptionsSnapshot {
appId: number;
nonSteam: boolean;
options: string;
details: SteamAppDetails;
}
function validateAppId(appId: number): void {
if (!Number.isSafeInteger(appId) || appId <= 0) throw new Error("Invalid Steam App ID");
}
function getSteamApps(): Partial<SteamApps> | undefined {
return (globalThis as typeof globalThis & {
SteamClient?: { Apps?: Partial<SteamApps> };
}).SteamClient?.Apps;
}
function snapshotFromDetails(appId: number, nonSteam: boolean, details: SteamAppDetails): SteamLaunchOptionsSnapshot {
if (nonSteam && isLegacyWrapperToken(details.strShortcutExe || "")) {
throw new Error("The shortcut Target still points to a legacy frame-generation wrapper; restore its original executable first");
}
return {
appId,
nonSteam,
options: nonSteam ? details.strShortcutLaunchOptions || "" : details.strLaunchOptions || "",
details,
};
}
function asError(error: unknown): Error {
return error instanceof Error ? error : new Error(String(error));
}
function registerSteamAppDetails(
appId: number,
onDetails: (details: SteamAppDetails) => boolean | void,
): () => void {
validateAppId(appId);
const apps = getSteamApps();
const registerForAppDetails = apps?.RegisterForAppDetails;
if (!registerForAppDetails) throw new Error("Steam launch options API is unavailable");
let active = true;
let unregisterPending = false;
let registration: SteamAppDetailsRegistration | undefined;
const unsubscribe = () => {
active = false;
if (!registration) {
unregisterPending = true;
return;
}
try {
registration.unregister();
} catch {
// Steam may invalidate registrations during a details refresh.
}
};
try {
registration = registerForAppDetails.call(apps, appId, (details) => {
if (!active) return;
if (onDetails(details || {}) === false && active) unsubscribe();
});
if (unregisterPending) {
try {
registration.unregister();
} catch {
// The registration can be invalidated before a synchronous callback returns.
}
}
} catch (error) {
throw asError(error);
}
return unsubscribe;
}
export async function readSteamLaunchOptions(appId: number, nonSteam: boolean): Promise<SteamLaunchOptionsSnapshot> {
return new Promise((resolve, reject) => {
let settled = false;
let timeout = 0;
let unsubscribe = () => {};
const finish = (error?: unknown, details?: SteamAppDetails) => {
if (settled) return;
settled = true;
window.clearTimeout(timeout);
unsubscribe();
if (error) {
reject(asError(error));
return;
}
try {
resolve(snapshotFromDetails(appId, nonSteam, details || {}));
} catch (snapshotError) {
reject(asError(snapshotError));
}
};
timeout = window.setTimeout(() => finish(new Error("Timed out reading Steam launch options")), 5000);
try {
unsubscribe = registerSteamAppDetails(appId, (details) => {
finish(undefined, details);
return false;
});
} catch (error) {
finish(error);
}
});
}
export function subscribeSteamLaunchOptions(
appId: number,
nonSteam: boolean,
onSnapshot: (snapshot: SteamLaunchOptionsSnapshot) => void,
onError: (error: Error) => void,
): () => void {
return registerSteamAppDetails(appId, (details) => {
try {
onSnapshot(snapshotFromDetails(appId, nonSteam, details));
} catch (error) {
onError(asError(error));
}
});
}
async function setSteamLaunchOptions(appId: number, nonSteam: boolean, options: string): Promise<void> {
const apps = getSteamApps();
const setter = nonSteam ? apps?.SetShortcutLaunchOptions : apps?.SetAppLaunchOptions;
if (!setter) throw new Error(`Steam ${nonSteam ? "shortcut " : ""}launch options API is unavailable`);
await Promise.resolve(setter.call(apps, appId, options));
}
function delay(milliseconds: number): Promise<void> {
return new Promise((resolve) => window.setTimeout(resolve, milliseconds));
}
async function waitForLaunchOptions(
appId: number,
nonSteam: boolean,
expected: string,
): Promise<SteamLaunchOptionsSnapshot> {
const deadline = Date.now() + 5000;
let lastError: Error | null = null;
while (Date.now() <= deadline) {
try {
const snapshot = await readSteamLaunchOptions(appId, nonSteam);
if (normalizeLaunchOptions(snapshot.options) === normalizeLaunchOptions(expected)) return snapshot;
} catch (error) {
lastError = asError(error);
}
if (Date.now() >= deadline) break;
await delay(100);
}
if (lastError) throw new Error(`Steam did not accept the launch options: ${lastError.message}`);
throw new Error("Steam did not accept the launch options before the readback timeout");
}
const operationQueues = new Map<string, Promise<void>>();
function queueKey(appId: number, nonSteam: boolean): string {
return `${nonSteam ? "shortcut" : "app"}:${appId}`;
}
function queueSteamAppOperation<T>(appId: number, nonSteam: boolean, operation: () => Promise<T>): Promise<T> {
const key = queueKey(appId, nonSteam);
const previous = operationQueues.get(key) || Promise.resolve();
const queued = previous.catch(() => undefined).then(operation);
let cleanup: Promise<void>;
cleanup = queued.then(
() => {
if (operationQueues.get(key) === cleanup) operationQueues.delete(key);
},
() => {
if (operationQueues.get(key) === cleanup) operationQueues.delete(key);
},
);
operationQueues.set(key, cleanup);
return queued;
}
export function updateSteamLaunchOptions(
appId: number,
nonSteam: boolean,
transform: (options: string) => string,
): Promise<SteamLaunchOptionsSnapshot> {
return queueSteamAppOperation(appId, nonSteam, async () => {
const current = await readSteamLaunchOptions(appId, nonSteam);
const next = transform(current.options);
if (next === current.options) return current;
await setSteamLaunchOptions(appId, nonSteam, next);
return waitForLaunchOptions(appId, nonSteam, next);
});
}
export function cleanupSteamLaunchOptions(
appId: number,
nonSteam: boolean,
): Promise<SteamLaunchOptionsSnapshot> {
return queueSteamAppOperation(appId, nonSteam, async () => {
const current = await readSteamLaunchOptions(appId, nonSteam);
const next = cleanupLegacyLaunchOptions(current.options);
if (next === current.options) return current;
await setSteamLaunchOptions(appId, nonSteam, next);
return waitForLaunchOptions(appId, nonSteam, next);
});
}
|