diff --git a/src/main/index.ts b/src/main/index.ts index fcdc4217..dff10748 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -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'; diff --git a/src/main/mainSettings.ts b/src/main/mainSettings.ts new file mode 100644 index 00000000..f39fe636 --- /dev/null +++ b/src/main/mainSettings.ts @@ -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 = { + 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; diff --git a/src/renderer/actions/install-path.utils.tsx b/src/renderer/actions/install-path.utils.tsx index 3d511df5..e43e9cfd 100644 --- a/src/renderer/actions/install-path.utils.tsx +++ b/src/renderer/actions/install-path.utils.tsx @@ -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'; diff --git a/src/renderer/components/AddonSection/ReleaseNotes/index.tsx b/src/renderer/components/AddonSection/ReleaseNotes/index.tsx index a4095bd0..49513027 100644 --- a/src/renderer/components/AddonSection/ReleaseNotes/index.tsx +++ b/src/renderer/components/AddonSection/ReleaseNotes/index.tsx @@ -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'; diff --git a/src/renderer/components/AddonSection/index.tsx b/src/renderer/components/AddonSection/index.tsx index 8bacabd9..ae5250bf 100644 --- a/src/renderer/components/AddonSection/index.tsx +++ b/src/renderer/components/AddonSection/index.tsx @@ -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'; diff --git a/src/renderer/components/App/AddonBar.tsx b/src/renderer/components/App/AddonBar.tsx index 562be227..c68508a3 100644 --- a/src/renderer/components/App/AddonBar.tsx +++ b/src/renderer/components/App/AddonBar.tsx @@ -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'; diff --git a/src/renderer/components/App/NavBar.tsx b/src/renderer/components/App/NavBar.tsx index 51126a82..3e45c8c6 100644 --- a/src/renderer/components/App/NavBar.tsx +++ b/src/renderer/components/App/NavBar.tsx @@ -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'; diff --git a/src/renderer/components/App/index.tsx b/src/renderer/components/App/index.tsx index 2e988a36..0f69920e 100644 --- a/src/renderer/components/App/index.tsx +++ b/src/renderer/components/App/index.tsx @@ -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'; diff --git a/src/renderer/components/ErrorModal/index.tsx b/src/renderer/components/ErrorModal/index.tsx index d9992a1c..6ae88470 100644 --- a/src/renderer/components/ErrorModal/index.tsx +++ b/src/renderer/components/ErrorModal/index.tsx @@ -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'; diff --git a/src/renderer/components/Modal/index.tsx b/src/renderer/components/Modal/index.tsx index 3a54b114..a5148ae3 100644 --- a/src/renderer/components/Modal/index.tsx +++ b/src/renderer/components/Modal/index.tsx @@ -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'; diff --git a/src/renderer/components/SettingsSection/Customization.tsx b/src/renderer/components/SettingsSection/Customization.tsx index ecda7565..e378a675 100644 --- a/src/renderer/components/SettingsSection/Customization.tsx +++ b/src/renderer/components/SettingsSection/Customization.tsx @@ -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 }) => ( diff --git a/src/renderer/components/SettingsSection/Developer.tsx b/src/renderer/components/SettingsSection/Developer.tsx index d17fc7a4..819bcfcc 100644 --- a/src/renderer/components/SettingsSection/Developer.tsx +++ b/src/renderer/components/SettingsSection/Developer.tsx @@ -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 }) => (
diff --git a/src/renderer/components/SettingsSection/Download.tsx b/src/renderer/components/SettingsSection/Download.tsx index c867067b..1b8c33e0 100644 --- a/src/renderer/components/SettingsSection/Download.tsx +++ b/src/renderer/components/SettingsSection/Download.tsx @@ -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 }) => ( diff --git a/src/renderer/components/SettingsSection/General.tsx b/src/renderer/components/SettingsSection/General.tsx index 3c8ae306..256afe9d 100644 --- a/src/renderer/components/SettingsSection/General.tsx +++ b/src/renderer/components/SettingsSection/General.tsx @@ -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'; diff --git a/src/renderer/components/SettingsSection/index.tsx b/src/renderer/components/SettingsSection/index.tsx index bf146ef5..e333bcad 100644 --- a/src/renderer/components/SettingsSection/index.tsx +++ b/src/renderer/components/SettingsSection/index.tsx @@ -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'; diff --git a/src/renderer/components/SideBar/index.tsx b/src/renderer/components/SideBar/index.tsx index e42456dd..e4fbc200 100644 --- a/src/renderer/components/SideBar/index.tsx +++ b/src/renderer/components/SideBar/index.tsx @@ -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 { diff --git a/src/common/settings.ts b/src/renderer/rendererSettings.ts similarity index 87% rename from src/common/settings.ts rename to src/renderer/rendererSettings.ts index ad998bf5..21b8a8e8 100644 --- a/src/common/settings.ts +++ b/src/renderer/rendererSettings.ts @@ -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') { @@ -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', ); @@ -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; } @@ -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 = (key: string, defaultValue?: T): [T, Dispatch>] => { const [storedValue, setStoredValue] = useState(store.get(key, defaultValue)); @@ -84,7 +77,7 @@ export const useIsDarkTheme = (): boolean => { return true; }; -interface Settings { +interface RendererSettings { mainSettings: { autoStartApp: boolean; disableExperimentalWarning: boolean; @@ -100,9 +93,6 @@ interface Settings { }; cache: { main: { - lastWindowX: number; - lastWindowY: number; - maximized: boolean; lastShownSection: string; lastShownAddonKey: string; }; @@ -113,7 +103,7 @@ interface Settings { }; } -const schema: Schema = { +const schema: Schema = { mainSettings: { type: 'object', // Empty defaults are required when using type: "object" (https://github.com/sindresorhus/conf/issues/85#issuecomment-531651424) @@ -217,16 +207,6 @@ const schema: Schema = { type: 'object', default: {}, properties: { - lastWindowX: { - type: 'integer', - }, - lastWindowY: { - type: 'integer', - }, - maximized: { - type: 'boolean', - default: false, - }, lastShownSection: { type: 'string', default: '', diff --git a/src/renderer/utils/AddonData.ts b/src/renderer/utils/AddonData.ts index a7671138..0e341bcb 100644 --- a/src/renderer/utils/AddonData.ts +++ b/src/renderer/utils/AddonData.ts @@ -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'; diff --git a/src/renderer/utils/BackgroundServices.ts b/src/renderer/utils/BackgroundServices.ts index 6256bc8d..08351236 100644 --- a/src/renderer/utils/BackgroundServices.ts +++ b/src/renderer/utils/BackgroundServices.ts @@ -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) { @@ -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) { diff --git a/src/renderer/utils/Directories.ts b/src/renderer/utils/Directories.ts index f3635e7a..ba359b57 100644 --- a/src/renderer/utils/Directories.ts +++ b/src/renderer/utils/Directories.ts @@ -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'; @@ -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; } @@ -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 { diff --git a/src/renderer/utils/InstallManager.tsx b/src/renderer/utils/InstallManager.tsx index 00406593..a2576405 100644 --- a/src/renderer/utils/InstallManager.tsx +++ b/src/renderer/utils/InstallManager.tsx @@ -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'; diff --git a/src/renderer/utils/InstallerConfiguration.ts b/src/renderer/utils/InstallerConfiguration.ts index 361d1b91..1ce3a55d 100644 --- a/src/renderer/utils/InstallerConfiguration.ts +++ b/src/renderer/utils/InstallerConfiguration.ts @@ -1,5 +1,5 @@ import { defaultConfiguration } from 'renderer/data'; -import settings from 'common/settings'; +import settings from 'renderer/rendererSettings'; export interface ExternalLink { url: string;