mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
[feature] we can now create launch shortcut in steam library
This commit is contained in:
@@ -24,7 +24,6 @@ import { LaunchMod, LaunchMods } from "shared/models/bs-launch/launch-option.int
|
||||
import { StaticConfigurationService } from "../static-configuration.service";
|
||||
import { SteamService } from "../steam.service";
|
||||
import { tryit } from "shared/helpers/error.helpers";
|
||||
import { SteamShortcut } from "shared/models/steam/shortcut.model";
|
||||
|
||||
export class BSLauncherService {
|
||||
private static instance: BSLauncherService;
|
||||
@@ -200,27 +199,6 @@ export class BSLauncherService {
|
||||
return this.bsmProtocolService.buildLink("launch", shortcutParams).toString();
|
||||
}
|
||||
|
||||
private async getSteamShortcutData(launchOptions: LaunchOption): Promise<SteamShortcut>{
|
||||
const shortcutName = ["Beat Saber", launchOptions.version.BSVersion, launchOptions.version.name].join(" ");
|
||||
const shortcutIconColor = new Color(launchOptions.version.color, "hex");
|
||||
|
||||
const exePath = app.getPath("exe");
|
||||
|
||||
return {
|
||||
appid: "\u0000\u0000\u0000",
|
||||
AppName: shortcutName,
|
||||
Exe: exePath,
|
||||
StartDir: path.dirname(exePath),
|
||||
LaunchOptions: this.createLaunchLink(launchOptions),
|
||||
icon: await this.createShortcutPng(shortcutIconColor),
|
||||
tags: [
|
||||
"BSManager",
|
||||
"Beat Saber",
|
||||
"VR",
|
||||
]
|
||||
} as SteamShortcut;
|
||||
}
|
||||
|
||||
public async createLaunchShortcut(launchOptions: LaunchOption, steamShortcut?: boolean): Promise<boolean>{
|
||||
const shortcutUrl = this.createLaunchLink(launchOptions);
|
||||
|
||||
@@ -229,13 +207,20 @@ export class BSLauncherService {
|
||||
|
||||
if(steamShortcut){
|
||||
const userId = await tryit(() => this.steam.getActiveUser());
|
||||
return this.steam.createShortcut(await this.getSteamShortcutData(launchOptions), userId.result).then(() => true).catch(e => {
|
||||
const exePath = app.getPath("exe");
|
||||
return this.steam.createShortcut({
|
||||
AppName: shortcutName,
|
||||
Exe: exePath,
|
||||
StartDir: path.dirname(exePath),
|
||||
LaunchOptions: this.createLaunchLink(launchOptions),
|
||||
icon: await this.createShortcutPng(shortcutIconColor),
|
||||
OpenVR: "\u0001"
|
||||
}, userId.result).then(() => true).catch(e => {
|
||||
log.error(e);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return execOnOs({
|
||||
win32: async () => (
|
||||
shell.writeShortcutLink(path.join(app.getPath("desktop"), `${shortcutName}.lnk`), {
|
||||
@@ -262,7 +247,7 @@ export class BSLauncherService {
|
||||
const bsPath: string = await (async () => {
|
||||
const bsPath = await this.localVersionService.getInstalledVersionPath(launchOption.version);
|
||||
return bsPath ?? this.localVersionService.getVersionPath(launchOption.version);
|
||||
})().catch(e => {
|
||||
})().catch((e): null => {
|
||||
log.error(e);
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -8,7 +8,7 @@ import { getProcessId, isProcessRunning } from "main/helpers/os.helpers";
|
||||
import { isElevated } from "query-process";
|
||||
import { execOnOs } from "../helpers/env.helpers";
|
||||
import { pathExists, pathExistsSync, readdir, writeFile } from "fs-extra";
|
||||
import { SteamShortcut, SteamShortcutKey } from "../../shared/models/steam/shortcut.model";
|
||||
import { SteamShortcut, SteamShortcutData } from "../../shared/models/steam/shortcut.model";
|
||||
|
||||
const { list } = (execOnOs({ win32: () => require("regedit-rs") }, true) ?? {}) as typeof import("regedit-rs");
|
||||
|
||||
@@ -174,9 +174,9 @@ export class SteamService {
|
||||
acc.push(path.join(configPath, entry.name));
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
}, [] as string[]);
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err): string[] => {
|
||||
log.error("Error while reading steam user data folders", err);
|
||||
return [];
|
||||
});
|
||||
@@ -188,136 +188,20 @@ export class SteamService {
|
||||
return path.join(await this.getSteamPath(), "userdata", userId.toString(), "config", "shortcuts.vdf");
|
||||
}
|
||||
|
||||
private async readShortcutsFile(shortcutsPath: string): Promise<SteamShortcut[]> {
|
||||
// Code taken from: https://developer.valvesoftware.com/wiki/Steam_Library_Shortcuts#Reading_the_shortcuts.vdf
|
||||
|
||||
const rawData: string = await readFile(shortcutsPath, "utf-8");
|
||||
const startIndex = rawData.indexOf("\u0000shortcuts\u0000");
|
||||
if (startIndex < 0) {
|
||||
console.error("Could not find shortcuts in shortcuts.vdf");
|
||||
return [];
|
||||
}
|
||||
const start = startIndex + "\u0000shortcuts\u0000".length;
|
||||
const end = rawData.lastIndexOf("\u0008\u0008");
|
||||
if (end < 0 || end <= start) {
|
||||
console.error("Could not find end of shortcuts in shortcuts.vdf");
|
||||
return [];
|
||||
}
|
||||
|
||||
const shortcutsString = rawData.substring(start, end - start);
|
||||
|
||||
const result: SteamShortcut[] = [];
|
||||
let currentShortcut: SteamShortcut | null = null;
|
||||
let word = "";
|
||||
let key = "";
|
||||
let readingTags = false;
|
||||
let tagId = -1;
|
||||
|
||||
const shortcutKeysRegex = new RegExp(`(\u0001|\u0002)(${Object.values(SteamShortcutKey).join("|")})`, "i");
|
||||
|
||||
for (const c of shortcutsString) {
|
||||
|
||||
if (c === "\u0000") {
|
||||
if (word.endsWith(`\u0001${SteamShortcutKey.AppName}`)) {
|
||||
if (currentShortcut) {
|
||||
result.push(currentShortcut);
|
||||
}
|
||||
currentShortcut = {
|
||||
AppName: "",
|
||||
Exe: "",
|
||||
StartDir: "",
|
||||
icon: "",
|
||||
LaunchOptions: "",
|
||||
IsHidden: null,
|
||||
tags: [],
|
||||
};
|
||||
key = `\u0001${SteamShortcutKey.AppName}`;
|
||||
} else if (shortcutKeysRegex.test(word)) {
|
||||
key = word;
|
||||
} else if (word === SteamShortcutKey.tags) {
|
||||
readingTags = true;
|
||||
} else if (key !== "") {
|
||||
const currentKey = shortcutKeysRegex.exec(key).pop().replaceAll("\u0001", "").replaceAll("\u0002", "") as SteamShortcutKey;
|
||||
if (currentShortcut && currentKey && currentKey !== SteamShortcutKey.tags) {
|
||||
currentShortcut[currentKey] = (word as string & string[]);
|
||||
}
|
||||
key = "";
|
||||
} else if (readingTags) {
|
||||
if (word.startsWith("\u0001")) {
|
||||
tagId = parseInt(word.substring(1), 10);
|
||||
} else if (tagId >= 0 && currentShortcut) {
|
||||
currentShortcut.tags.push(word);
|
||||
tagId = -1;
|
||||
} else {
|
||||
readingTags = false;
|
||||
}
|
||||
}
|
||||
word = "";
|
||||
} else {
|
||||
word += c;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentShortcut) {
|
||||
result.push(currentShortcut);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async getShortcuts(userId: number): Promise<SteamShortcut[]> {
|
||||
const shortcutsPath = await this.getShortcutsPath(userId);
|
||||
return this.readShortcutsFile(shortcutsPath);
|
||||
}
|
||||
const shortcutsString = await readFile(shortcutsPath, { encoding: "utf-8" });
|
||||
|
||||
private buildShortcutTagsString(tags: string[]): string {
|
||||
let tagString = "\u0000tags\u0000";
|
||||
for (let i = 0; i < tags.length; i++) {
|
||||
tagString += `\u0001${i}\u0000${tags[i]}\u0000`;
|
||||
}
|
||||
tagString += "\u0008";
|
||||
return tagString;
|
||||
}
|
||||
|
||||
private buildShortcutString(shortcut: SteamShortcut): string {
|
||||
const getSeparator = (key: SteamShortcutKey): string => {
|
||||
return key === SteamShortcutKey.IsHidden || key === SteamShortcutKey.appid ? "\u0002" : "\u0001";
|
||||
};
|
||||
|
||||
const getQuote = (key: SteamShortcutKey): string => {
|
||||
return key === SteamShortcutKey.Exe || key === SteamShortcutKey.StartDir || key === SteamShortcutKey.icon ? "\"" : "";
|
||||
}
|
||||
|
||||
let shortcutString = "";
|
||||
for (const key of Object.keys(shortcut)) {
|
||||
console.log("KEY", key);
|
||||
if(key === SteamShortcutKey.tags) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const value = shortcut[key as SteamShortcutKey];
|
||||
shortcutString += `${getSeparator(key as SteamShortcutKey)}${key}\u0000\"${value}\"\u0000`;
|
||||
}
|
||||
|
||||
shortcutString += `\u0000\u0000${this.buildShortcutTagsString(shortcut.tags)}`;
|
||||
return shortcutString;
|
||||
return SteamShortcut.parseShortcutsRawData(shortcutsString);
|
||||
}
|
||||
|
||||
private async writeShortcuts(shortcuts: SteamShortcut[], userId: number): Promise<void> {
|
||||
let shortcutsString = "\u0000shortcuts\u0000";
|
||||
for (let i = 0; i < shortcuts.length; i++) {
|
||||
shortcutsString += `\u0000${i}\u0000`;
|
||||
shortcutsString += this.buildShortcutString(shortcuts[i]);
|
||||
shortcutsString += "\u0008";
|
||||
}
|
||||
shortcutsString += "\u0008\u0008";
|
||||
|
||||
const shortcutsPath = await this.getShortcutsPath(userId);
|
||||
await writeFile(shortcutsPath, shortcutsString, { encoding: "utf-8" });
|
||||
|
||||
const stringData = SteamShortcut.getShortcutsString(shortcuts);
|
||||
await writeFile(shortcutsPath, stringData, { encoding: "utf-8" });
|
||||
}
|
||||
|
||||
public async createShortcut(shortcutData: SteamShortcut, userId?: number): Promise<void> {
|
||||
public async createShortcut(shortcutData: SteamShortcutData, userId?: number): Promise<void> {
|
||||
const userIds = userId ? [userId] : await (async (): Promise<number[]> => {
|
||||
const folders = await this.getUserDataFolders();
|
||||
return folders.map(folder => parseInt(path.basename(folder)));
|
||||
@@ -328,7 +212,8 @@ export class SteamService {
|
||||
log.warn("Error while reading shortcuts", e);
|
||||
return [];
|
||||
});
|
||||
shortcuts.push(shortcutData);
|
||||
shortcuts.push(new SteamShortcut(shortcutData));
|
||||
|
||||
await this.writeShortcuts(shortcuts, userId);
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -103,10 +103,10 @@ export const CreateLaunchShortcutModal: ModalComponent<{ steamShortcut: boolean,
|
||||
</div>
|
||||
</div>
|
||||
{isSteamVersion && (
|
||||
<Tippy placement="right" theme="default" content={"Si activé, au lieu de créer un raccourci sur le bureau, celui-ci sera créé dans Steam."}>
|
||||
<Tippy placement="right" theme="default" content={t("modals.create-launch-shortcut.steam-shortcut-tippy")}>
|
||||
<div className="h-full flex items-center gap-1.5 mt-3 mb-4 w-fit pr-1">
|
||||
<BsmCheckbox className="h-5 aspect-square relative z-[1]" checked={steamShortcut} onChange={e => setSteamShortcut(() => e)} />
|
||||
<span>Créer un raccourci Steam</span>
|
||||
<span>{t("modals.create-launch-shortcut.create-steam-shortcut")}</span>
|
||||
</div>
|
||||
</Tippy>
|
||||
)}
|
||||
|
||||
@@ -123,7 +123,7 @@ export function VersionViewer() {
|
||||
lastValueFrom(bsLauncher.createLaunchShortcut(data.launchOption, data.steamShortcut)).then(() => {
|
||||
notification.notifySuccess({
|
||||
title: "notifications.create-launch-shortcut.success.title",
|
||||
desc: "notifications.create-launch-shortcut.success.msg"
|
||||
desc: `notifications.create-launch-shortcut.success.${data.steamShortcut ? "msg-steam" : "msg"}`
|
||||
});
|
||||
}).catch(() => {
|
||||
notification.notifyError({
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export enum SteamShortcutKey {
|
||||
enum SteamShortcutKey {
|
||||
AppName = "AppName",
|
||||
Exe = "Exe",
|
||||
StartDir = "StartDir",
|
||||
@@ -16,13 +16,141 @@ export enum SteamShortcutKey {
|
||||
tags = "tags"
|
||||
}
|
||||
|
||||
type BaseShortcut = {
|
||||
type BaseShortcutData = {
|
||||
[K in SteamShortcutKey]: K extends "tags" ? string[] : string
|
||||
};
|
||||
|
||||
export interface SteamShortcut extends Partial<BaseShortcut> {
|
||||
// Mandatory fields
|
||||
export interface SteamShortcutData extends Partial<BaseShortcutData> {
|
||||
// Mandatory and specific values
|
||||
AppName: string;
|
||||
Exe: string;
|
||||
StartDir: string;
|
||||
OpenVR?: "\x01" | "\x00";
|
||||
}
|
||||
|
||||
export class SteamShortcut {
|
||||
|
||||
public static parseShortcutsRawData(rawData: string): SteamShortcut[] {
|
||||
// Code taken from: https://developer.valvesoftware.com/wiki/Steam_Library_Shortcuts#Reading_the_shortcuts.vdf
|
||||
|
||||
const startIndex = rawData.indexOf("\u0000shortcuts\u0000");
|
||||
if (startIndex < 0) {
|
||||
return [];
|
||||
}
|
||||
const start = startIndex + "\u0000shortcuts\u0000".length;
|
||||
const end = rawData.lastIndexOf("\u0008\u0008");
|
||||
if (end < 0 || end <= start) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const shortcutsString = rawData.substring(start, end - start);
|
||||
|
||||
const result: SteamShortcutData[] = [];
|
||||
let currentShortcut: SteamShortcutData | null = null;
|
||||
let word = "";
|
||||
let key = "";
|
||||
let readingTags = false;
|
||||
let tagId = -1;
|
||||
|
||||
const shortcutKeysRegex = new RegExp(`[\u0001\u0002](${Object.values(SteamShortcutKey).join("|")})`, "i");
|
||||
|
||||
for (const c of shortcutsString) {
|
||||
|
||||
if (c === "\u0000") {
|
||||
if (word.endsWith(`\u0001${SteamShortcutKey.AppName}`)) {
|
||||
if (currentShortcut) {
|
||||
result.push(currentShortcut);
|
||||
}
|
||||
currentShortcut = {
|
||||
AppName: "",
|
||||
Exe: "",
|
||||
StartDir: "",
|
||||
icon: "",
|
||||
LaunchOptions: "",
|
||||
IsHidden: "\x00",
|
||||
};
|
||||
key = `\u0001${SteamShortcutKey.AppName}`;
|
||||
} else if (shortcutKeysRegex.test(word)) {
|
||||
key = word;
|
||||
} else if (word === SteamShortcutKey.tags) {
|
||||
readingTags = true;
|
||||
} else if (key !== "") {
|
||||
const currentKey = shortcutKeysRegex.exec(key).pop().replaceAll("\u0001", "").replaceAll("\u0002", "") as SteamShortcutKey;
|
||||
if (currentShortcut && currentKey && currentKey !== SteamShortcutKey.tags) {
|
||||
currentShortcut[currentKey] = word.replaceAll("\"", "") as string & ("\x01" | "\x00") // Make TS happy
|
||||
}
|
||||
key = "";
|
||||
} else if (readingTags) {
|
||||
if (word.startsWith("\u0001")) {
|
||||
tagId = parseInt(word.substring(1), 10);
|
||||
} else if (tagId >= 0 && currentShortcut) {
|
||||
currentShortcut.tags.push(word);
|
||||
tagId = -1;
|
||||
} else {
|
||||
readingTags = false;
|
||||
}
|
||||
}
|
||||
word = "";
|
||||
} else {
|
||||
word += c;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentShortcut) {
|
||||
result.push(currentShortcut);
|
||||
}
|
||||
|
||||
return result.map(shortcutData => new SteamShortcut(shortcutData));
|
||||
}
|
||||
|
||||
public static getShortcutsString(shortcuts: SteamShortcut[]): string {
|
||||
let shortcutsString = "\u0000shortcuts\u0000";
|
||||
for (let i = 0; i < shortcuts.length; i++) {
|
||||
const shortcut = shortcuts[i];
|
||||
if(!shortcut.data?.AppName || !shortcut.data?.Exe || !shortcut.data?.StartDir) {
|
||||
continue;
|
||||
}
|
||||
shortcutsString += `\u0000${i}\u0000`;
|
||||
shortcutsString += shortcut.getStringBytes();
|
||||
}
|
||||
shortcutsString += "\u0008\u0008";
|
||||
return shortcutsString;
|
||||
}
|
||||
|
||||
private data: SteamShortcutData;
|
||||
|
||||
public constructor(shortcutData: SteamShortcutData) {
|
||||
this.data = shortcutData;
|
||||
}
|
||||
|
||||
public getStringBytes(): string {
|
||||
|
||||
const isHidden = this.data?.IsHidden === "\x01";
|
||||
const allowDesktopConfig = this.data?.AllowDesktopConfig === "\x01";
|
||||
const allowOverlay = this.data?.AllowDesktopConfig === "\x01";
|
||||
const openVR = this.data?.OpenVR === "\x01";
|
||||
const devkit = this.data?.Devkit === "\x01";
|
||||
|
||||
let strShortcut = "";
|
||||
|
||||
strShortcut += `\x02appid\x00${this.data?.appid || "\x00\x00\x00"}\x00`;
|
||||
strShortcut += `\x01AppName\x00${this.data?.AppName ?? ""}\x00`;
|
||||
strShortcut += `\x01Exe\x00\"${this.data?.Exe ?? ""}\"\x00`;
|
||||
strShortcut += `\x01StartDir\x00\"${this.data?.StartDir ?? ""}\"\x00`;
|
||||
strShortcut += `\x01icon\x00${this.data?.icon ?? ""}\x00`;
|
||||
strShortcut += `\x01ShortcutPath\x00\x00`;
|
||||
strShortcut += `\x01LaunchOptions\x00${this.data?.LaunchOptions ?? ""}\x00`;
|
||||
strShortcut += `\x02IsHidden\x00${isHidden ? "\x01" : "\x00"}\x00\x00\x00`;
|
||||
strShortcut += `\x02AllowDesktopConfig\x00${allowDesktopConfig ? "\x01" : "\x00"}\x00\x00\x00`;
|
||||
strShortcut += `\x02AllowOverlay\x00${allowOverlay ? "\x01" : "\x00"}\x00\x00\x00`;
|
||||
strShortcut += `\x02OpenVR\x00${openVR ? "\x01" : "\x00"}\x00\x00\x00`;
|
||||
strShortcut += `\x02Devkit\x00${devkit ? "\x01" : "\x00"}\x00\x00\x00`;
|
||||
strShortcut += `\x01DevkitGameID\x00${this.data?.DevkitGameID ?? ""}\x00`;
|
||||
strShortcut += `\x02DevkitOverrideAppID\x00\x00\x00\x00\x00`;
|
||||
strShortcut += `\x02LastPlayTime\x00\x00\x00\x00\x00`;
|
||||
strShortcut += `\x00tags\x00${""}\x08\x08`;
|
||||
|
||||
return strShortcut;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user