Skip to content

Commit

Permalink
refactor(options): add class method saveOptions
Browse files Browse the repository at this point in the history
commit 9aba174
Author: sheffey <[email protected]>
Date:   Thu Oct 3 20:42:42 2024 +0800

    add class method `saveOptions`

    remove class method `isSteam`

commit 3198449
Author: sheffey <[email protected]>
Date:   Thu Oct 3 19:50:29 2024 +0800

    sugar sugar, js sugar.
  • Loading branch information
SheffeyG committed Oct 3, 2024
1 parent b572f2c commit 3f3f7d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 41 deletions.
20 changes: 16 additions & 4 deletions src/utils/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Backend } from "./backend";

export class Options {
#options: { [key: string]: string };
#others: string;
#isSteam: boolean = true;

constructor(input: string) {
this.#options = {};

// Launch options from Heroic and Emudeck should nerver be changed,
// maybe there are more launchers to be added.
this.#isSteam = !(input.includes("heroicgameslauncher") || input.includes("Emulation"));

const match = input.match(/(.*)%command%(.*)/);
Expand All @@ -27,10 +32,6 @@ export class Options {
}
}

isSteam(): boolean {
return this.#isSteam;
}

hasField(key: string): boolean {
return key in this.#options;
}
Expand Down Expand Up @@ -66,4 +67,15 @@ export class Options {
if (this.#others) optionsString += " " + this.#others;
return optionsString.trim();
}

saveOptions(appid: number) {
if (this.#isSteam) {
SteamClient.Apps.SetAppLaunchOptions(appid, this.getOptionsString());
Backend.sendNotice("Launch Options Saved.");
}
else {
// Never change anything for non-steam games
Backend.sendNotice("Warning: This is not a steam game! settings will not be saved.");
}
};
}
17 changes: 2 additions & 15 deletions src/views/Advanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { FaFolderOpen } from "react-icons/fa";

const Advanced: FC<{ appid: number }> = ({ appid }) => {
const [options, setOptions] = useState(new Options(""));
const [isSteam, setIsSteam] = useState(true);
const [showPrefix, setShowPrefix] = useState(false);

useEffect(() => {
Expand All @@ -24,7 +23,6 @@ const Advanced: FC<{ appid: number }> = ({ appid }) => {
const savedOptions = new Options(optionsString);
setShowPrefix(savedOptions.hasField("STEAM_COMPAT_DATA_PATH"));
setOptions(savedOptions);
setIsSteam(savedOptions.isSteam());
});
setTimeout(() => {
unregister();
Expand All @@ -33,25 +31,14 @@ const Advanced: FC<{ appid: number }> = ({ appid }) => {

const handleBrowse = async () => {
const prefixDir = options.getFieldValue("STEAM_COMPAT_DATA_PATH");
const defaultDir = prefixDir ? prefixDir : await Backend.getEnv("DECKY_USER_HOME");
const defaultDir = prefixDir ?? await Backend.getEnv("DECKY_USER_HOME");
const filePickerRes = await Backend.openFilePicker(defaultDir, false);
const prefixPath = filePickerRes.path;
const newOptions = new Options(options.getOptionsString());
newOptions.setFieldValue("STEAM_COMPAT_DATA_PATH", `"${prefixPath}"`);
setOptions(newOptions);
};

const saveOptions = () => {
if (isSteam) {
SteamClient.Apps.SetAppLaunchOptions(appid, options.getOptionsString());
Backend.sendNotice("Advanced settings saved.");
}
else {
// non steam games is not implemented
Backend.sendNotice("Warning: This is not a steam game! settings will not be saved.");
}
};

return (
<Focusable style={{ display: "flex", flexDirection: "column" }}>

Expand Down Expand Up @@ -136,7 +123,7 @@ const Advanced: FC<{ appid: number }> = ({ appid }) => {
)}

<DialogButton
onClick={saveOptions}
onClick={() => options.saveOptions(appid)}
style={{
alignSelf: "center",
marginTop: "20px",
Expand Down
8 changes: 1 addition & 7 deletions src/views/Custom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { FC, useEffect, useState } from "react";
import { BsPencilFill } from "react-icons/bs";
import { MdAddBox } from "react-icons/md";

import { Backend } from "../../utils/backend";
import { CustomOption, getCustomOptions } from "../../utils/custom";
import { ModalEdit } from "./ModalEdit";
import { ModalNew } from "./ModalNew";
Expand Down Expand Up @@ -39,11 +38,6 @@ const Custom: FC<{ appid: number }> = ({ appid }) => {
setCusOptList(updatedOptList);
};

const saveOptions = async () => {
SteamClient.Apps.SetAppLaunchOptions(appid, options.getOptionsString());
Backend.sendNotice("Custom settings saved.");
};

return (
<>
<style>
Expand Down Expand Up @@ -145,7 +139,7 @@ const Custom: FC<{ appid: number }> = ({ appid }) => {
{(cusOptList.length > 0) && (
<DialogButton
className="CD_SaveButton"
onClick={saveOptions}
onClick={() => options.saveOptions(appid)}
>
Save Settings
</DialogButton>
Expand Down
17 changes: 2 additions & 15 deletions src/views/Normal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const Normal: FC<{ appid: number }> = ({ appid }) => {
const [options, setOptions] = useState(new Options(""));
const [showCheat, setShowChat] = useState(false);
const [showLang, setShowLang] = useState(false);
const [isSteam, setIsSteam] = useState(true);

const defaultLangCodes: DropdownOption[] = LangCodes;

Expand All @@ -31,7 +30,6 @@ const Normal: FC<{ appid: number }> = ({ appid }) => {
setOptions(savedOptions);
setShowChat(savedOptions.hasField("PROTON_REMOTE_DEBUG_CMD"));
setShowLang(savedOptions.hasField("LANG"));
setIsSteam(savedOptions.isSteam());
});
setTimeout(() => {
unregister();
Expand All @@ -40,7 +38,7 @@ const Normal: FC<{ appid: number }> = ({ appid }) => {

const handleBrowse = async () => {
const cheatDir = options.getFieldValue("PRESSURE_VESSEL_FILESYSTEMS_RW");
const defaultDir = cheatDir ? cheatDir : await Backend.getEnv("DECKY_USER_HOME");
const defaultDir = cheatDir ?? await Backend.getEnv("DECKY_USER_HOME");
const filePickerRes = await Backend.openFilePicker(defaultDir, true, ["exe", "bat"]);
const cheatPath = filePickerRes.path;
const newOptions = new Options(options.getOptionsString());
Expand All @@ -50,17 +48,6 @@ const Normal: FC<{ appid: number }> = ({ appid }) => {
setOptions(newOptions);
};

const saveOptions = () => {
if (isSteam) {
SteamClient.Apps.SetAppLaunchOptions(appid, options.getOptionsString());
Backend.sendNotice("Normal settings saved.");
}
else {
// Never change anything for non-steam games
Backend.sendNotice("Warning: This is not a steam game! settings will not be saved.");
}
};

return (
<Focusable style={{ display: "flex", flexDirection: "column" }}>

Expand Down Expand Up @@ -182,7 +169,7 @@ const Normal: FC<{ appid: number }> = ({ appid }) => {
)}

<DialogButton
onClick={saveOptions}
onClick={() => options.saveOptions(appid)}
style={{
alignSelf: "center",
marginTop: "20px",
Expand Down

0 comments on commit 3f3f7d5

Please sign in to comment.