Skip to content

Commit

Permalink
fix: split settings into two to fix broken directory stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjozork committed Mar 30, 2024
1 parent 2aa5694 commit 4acf681
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { app, BrowserWindow, Menu, globalShortcut, shell, ipcMain } from 'electr
import { NsisUpdater } from 'electron-updater';
import installExtension, { REDUX_DEVTOOLS, REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer';
import * as packageInfo from '../../package.json';
import settings, { persistWindowSettings } from 'common/settings';
import settings, { persistWindowSettings } from './mainSettings';
import channels from 'common/channels';
import * as remote from '@electron/remote/main';
import { InstallManager } from 'main/InstallManager';
Expand Down
48 changes: 48 additions & 0 deletions src/main/mainSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Store, { Schema } from 'electron-store';

export const persistWindowSettings = (window: Electron.BrowserWindow): void => {
store.set('cache.main.maximized', window.isMaximized());

const winSize = window.getSize();
store.set('cache.main.lastWindowX', winSize[0]);
store.set('cache.main.lastWindowY', winSize[1]);
};

interface Settings {
cache: {
main: {
lastWindowX: number;
lastWindowY: number;
maximized: boolean;
};
};
}

const schema: Schema<Settings> = {
cache: {
type: 'object',
default: {},
properties: {
main: {
type: 'object',
default: {},
properties: {
lastWindowX: {
type: 'integer',
},
lastWindowY: {
type: 'integer',
},
maximized: {
type: 'boolean',
default: false,
},
},
},
},
},
};

const store = new Store({ schema, clearInvalidConfig: true });

export default store;
2 changes: 1 addition & 1 deletion src/renderer/actions/install-path.utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import settings from 'common/settings';
import settings from 'renderer/rendererSettings';
import { Directories } from 'renderer/utils/Directories';
import { dialog } from '@electron/remote';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useInView } from 'react-intersection-observer';
import { ReleaseData } from 'renderer/redux/types';
import { GitVersions } from '@flybywiresim/api-client';
import { addReleases } from 'renderer/redux/features/releaseNotes';
import { useSetting } from 'common/settings';
import { useSetting } from 'renderer/rendererSettings';
import dateFormat from 'dateformat';
import { ArrowUp } from 'react-bootstrap-icons';

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/AddonSection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Addon, AddonCategoryDefinition, AddonTrack } from 'renderer/utils/Insta
import { Directories } from 'renderer/utils/Directories';
import { NavLink, Redirect, Route, useHistory, useParams } from 'react-router-dom';
import { Gear, InfoCircle, JournalText, Sliders } from 'react-bootstrap-icons';
import settings, { useSetting } from 'common/settings';
import settings, { useSetting } from 'renderer/rendererSettings';
import { ipcRenderer } from 'electron';
import { AddonBar, AddonBarItem } from '../App/AddonBar';
import { NoAvailableAddonsSection } from '../NoAvailableAddonsSection';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/App/AddonBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ArrowRepeat, Check2, CloudArrowDownFill, Icon } from 'react-bootstrap-i
import { useHistory, useParams } from 'react-router-dom';
import { useAppSelector } from 'renderer/redux/store';
import { AircraftSectionURLParams } from '../AddonSection';
import { useIsDarkTheme } from 'common/settings';
import { useIsDarkTheme } from 'renderer/rendererSettings';
import { Button } from 'renderer/components/Button';
import { ChevronRight } from 'tabler-icons-react';
import { InstallStatus } from 'renderer/components/AddonSection/Enums';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/App/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { FC } from 'react';
import { NavLink } from 'react-router-dom';
import { useIsDarkTheme } from 'common/settings';
import { useIsDarkTheme } from 'renderer/rendererSettings';
import { Publisher } from 'renderer/utils/InstallerConfiguration';
import { useAppSelector } from 'renderer/redux/store';
import { Gear, Wrench } from 'react-bootstrap-icons';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { NavBar, NavBarPublisher } from 'renderer/components/App/NavBar';
import { Redirect, Route, Switch, useHistory, useLocation } from 'react-router-dom';
import { store, useAppSelector } from 'renderer/redux/store';
import { setAddonAndTrackLatestReleaseInfo } from 'renderer/redux/features/latestVersionNames';
import settings from 'common/settings';
import settings from 'renderer/rendererSettings';
import './index.css';
import { ipcRenderer } from 'electron';
import channels from 'common/channels';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/ErrorModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import { setupInstallPath } from 'renderer/actions/install-path.utils';
import settings from 'common/settings';
import settings from 'renderer/rendererSettings';
import { Directories } from 'renderer/utils/Directories';
import * as fs from 'fs';
import { Button, ButtonType } from 'renderer/components/Button';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/Modal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import settings, { useSetting } from 'common/settings';
import settings, { useSetting } from 'renderer/rendererSettings';
import React, { createContext, FC, useContext, useState } from 'react';
import { Dot, X } from 'react-bootstrap-icons';
import ReactMarkdown from 'react-markdown';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/SettingsSection/Customization.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC } from 'react';
import settings, { useSetting } from 'common/settings';
import settings, { useSetting } from 'renderer/rendererSettings';
import { Toggle } from '../Toggle';

const SettingsItem: FC<{ name: string }> = ({ name, children }) => (
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/SettingsSection/Developer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC, useCallback, useEffect, useState } from 'react';
import { useSetting } from 'common/settings';
import { useSetting } from 'renderer/rendererSettings';

const SettingsItem: FC<{ name: string }> = ({ name, children }) => (
<div className="flex flex-row items-center justify-between py-3.5">
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/SettingsSection/Download.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC } from 'react';
import { setupMsfsCommunityPath, setupInstallPath, setupTempLocation } from 'renderer/actions/install-path.utils';
import settings, { useSetting } from 'common/settings';
import settings, { useSetting } from 'renderer/rendererSettings';
import { Toggle } from '../Toggle';

const SettingsItem: FC<{ name: string }> = ({ name, children }) => (
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/SettingsSection/General.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC } from 'react';
import settings, { useSetting } from 'common/settings';
import settings, { useSetting } from 'renderer/rendererSettings';
import { ipcRenderer } from 'electron';
import { Toggle } from '../Toggle';

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/SettingsSection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SideBar, SideBarLink, SideBarTitle } from 'renderer/components/SideBar'
import { CustomizationSettings } from './Customization';
import { DownloadSettings } from './Download';
import { DeveloperSettings } from './Developer';
import settings from 'common/settings';
import settings from 'renderer/rendererSettings';
import * as packageInfo from '../../../../package.json';
import { Button, ButtonType } from '../Button';
import { PromptModal, useModals } from 'renderer/components/Modal';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/SideBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC } from 'react';
import { useIsDarkTheme } from 'common/settings';
import { useIsDarkTheme } from 'renderer/rendererSettings';
import { NavLink, useRouteMatch } from 'react-router-dom';

export interface SideBarProps {
Expand Down
32 changes: 6 additions & 26 deletions src/common/settings.ts → src/renderer/rendererSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as path from 'path';
import * as os from 'os';
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
import * as packageInfo from '../../package.json';
import { Directories } from 'renderer/utils/Directories';

const defaultCommunityDir = (): string => {
if (os.platform().toString() === 'linux') {
Expand All @@ -14,9 +15,9 @@ const defaultCommunityDir = (): string => {
// Ensure proper functionality in main- and renderer-process
let msfsConfigPath = null;

const steamPath = path.join(process.env.APPDATA, '\\Microsoft Flight Simulator\\UserCfg.opt');
const steamPath = path.join(Directories.appData(), '\\Microsoft Flight Simulator\\UserCfg.opt');
const storePath = path.join(
process.env.LOCALAPPDATA,
Directories.localAppData(),
'\\Packages\\Microsoft.FlightSimulator_8wekyb3d8bbwe\\LocalCache\\UserCfg.opt',
);

Expand All @@ -25,7 +26,7 @@ const defaultCommunityDir = (): string => {
} else if (fs.existsSync(storePath)) {
msfsConfigPath = storePath;
} else {
walk(process.env.LOCALAPPDATA, (path) => {
walk(Directories.localAppData(), (path) => {
if (path.includes('Flight') && path.includes('UserCfg.opt')) {
msfsConfigPath = path;
}
Expand All @@ -50,14 +51,6 @@ const defaultCommunityDir = (): string => {
}
};

export const persistWindowSettings = (window: Electron.BrowserWindow): void => {
store.set('cache.main.maximized', window.isMaximized());

const winSize = window.getSize();
store.set('cache.main.lastWindowX', winSize[0]);
store.set('cache.main.lastWindowY', winSize[1]);
};

export const useSetting = <T>(key: string, defaultValue?: T): [T, Dispatch<SetStateAction<T>>] => {
const [storedValue, setStoredValue] = useState(store.get<string, T>(key, defaultValue));

Expand All @@ -84,7 +77,7 @@ export const useIsDarkTheme = (): boolean => {
return true;
};

interface Settings {
interface RendererSettings {
mainSettings: {
autoStartApp: boolean;
disableExperimentalWarning: boolean;
Expand All @@ -100,9 +93,6 @@ interface Settings {
};
cache: {
main: {
lastWindowX: number;
lastWindowY: number;
maximized: boolean;
lastShownSection: string;
lastShownAddonKey: string;
};
Expand All @@ -113,7 +103,7 @@ interface Settings {
};
}

const schema: Schema<Settings> = {
const schema: Schema<RendererSettings> = {
mainSettings: {
type: 'object',
// Empty defaults are required when using type: "object" (https://github.com/sindresorhus/conf/issues/85#issuecomment-531651424)
Expand Down Expand Up @@ -217,16 +207,6 @@ const schema: Schema<Settings> = {
type: 'object',
default: {},
properties: {
lastWindowX: {
type: 'integer',
},
lastWindowY: {
type: 'integer',
},
maximized: {
type: 'boolean',
default: false,
},
lastShownSection: {
type: 'string',
default: '',
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/utils/AddonData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GitVersions } from '@flybywiresim/api-client';
import { Directories } from './Directories';
import fs from 'fs';
import { getCurrentInstall, FragmenterUpdateChecker } from '@flybywiresim/fragmenter';
import settings from 'common/settings';
import settings from 'renderer/rendererSettings';
import { store } from 'renderer/redux/store';
import { setInstalledTrack } from 'renderer/redux/features/installedTrack';
import { setSelectedTrack } from 'renderer/redux/features/selectedTrack';
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/utils/BackgroundServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class BackgroundServices {

let folderEntries;
try {
folderEntries = await promises.readdir(path.join(process.env.APPDATA, STARTUP_FOLDER_PATH), {
folderEntries = await promises.readdir(path.join(Directories.appData(), STARTUP_FOLDER_PATH), {
withFileTypes: true,
});
} catch (e) {
Expand Down Expand Up @@ -88,7 +88,7 @@ export class BackgroundServices {
);
const commandLineArgs = backgroundService.commandLineArgs ? ` ${backgroundService.commandLineArgs.join(' ')}` : '';

const shortcutDir = path.join(process.env.APPDATA, STARTUP_FOLDER_PATH);
const shortcutDir = path.join(Directories.appData(), STARTUP_FOLDER_PATH);
const shortcutPath = path.join(shortcutDir, `${backgroundService.executableFileBasename}.lnk`);

if (enabled) {
Expand Down
15 changes: 12 additions & 3 deletions src/renderer/utils/Directories.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'path';
import { Addon } from 'renderer/utils/InstallerConfiguration';
import fs from 'fs';
import settings from 'common/settings';
import settings from 'renderer/rendererSettings';
import { app } from '@electron/remote';

const TEMP_DIRECTORY_PREFIX = 'flybywire-current-install';

Expand All @@ -15,6 +16,14 @@ export class Directories {
return path.normalize(suffix).replace(/^(\.\.(\/|\\|$))+/, '');
}

static appData(): string {
return app.getPath('appData');
}

static localAppData(): string {
return path.join(app.getPath('appData'), '..', 'Local');
}

static communityLocation(): string {
return settings.get('mainSettings.msfsCommunityPath') as string;
}
Expand Down Expand Up @@ -60,11 +69,11 @@ export class Directories {
}

static inPackagesMicrosoftStore(targetDir: string): string {
return path.join(process.env.LOCALAPPDATA, MSFS_APPDATA_PATH, this.sanitize(targetDir));
return path.join(Directories.localAppData(), MSFS_APPDATA_PATH, this.sanitize(targetDir));
}

static inPackagesSteam(targetDir: string): string {
return path.join(process.env.APPDATA, MSFS_STEAM_PATH, this.sanitize(targetDir));
return path.join(Directories.localAppData(), MSFS_STEAM_PATH, this.sanitize(targetDir));
}

static inPackageCache(addon: Addon, targetDir: string): string {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/utils/InstallManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
FragmenterOperation,
FragmenterUpdateChecker,
} from '@flybywiresim/fragmenter';
import settings from 'common/settings';
import settings from 'renderer/rendererSettings';
import { store } from 'renderer/redux/store';
import { InstallState, setInstallStatus } from 'renderer/redux/features/installStatus';
import { setSelectedTrack } from 'renderer/redux/features/selectedTrack';
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/utils/InstallerConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defaultConfiguration } from 'renderer/data';
import settings from 'common/settings';
import settings from 'renderer/rendererSettings';

export interface ExternalLink {
url: string;
Expand Down

0 comments on commit 4acf681

Please sign in to comment.