Skip to content

Commit

Permalink
feat: add customizable temp folder (#390)
Browse files Browse the repository at this point in the history
* fix: error opening devtools before DOM is ready

* feat: add customizable temp folder

* feat: add separate temp directory tick

* feat: improve customizable temp folder

Co-authored-by: FoxtrotSierra6829 <github@florian-scheuner.de>
  • Loading branch information
marinofranz and FoxtrotSierra6829 committed Jul 14, 2022
1 parent edee6e2 commit 6cad169
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/common/settings.ts
Expand Up @@ -118,6 +118,14 @@ const schema: Schema<unknown> = {
type: "string",
default: defaultCommunityDir(),
},
separateTempLocation: {
type: "boolean",
default: false,
},
tempLocation: {
type: "string",
default: defaultCommunityDir(),
},
},
},
cache: {
Expand Down
4 changes: 3 additions & 1 deletion src/main/index.ts
Expand Up @@ -137,7 +137,9 @@ const createWindow = (): void => {
if (process.env.NODE_ENV === 'development') {
// Open the DevTools.
settings.openInEditor();
mainWindow.webContents.openDevTools();
mainWindow.webContents.once('dom-ready', () => {
mainWindow.webContents.openDevTools();
});
}

globalShortcut.register('CmdOrCtrl+F5', () => {
Expand Down
17 changes: 17 additions & 0 deletions src/renderer/actions/install-path.utils.tsx
Expand Up @@ -22,6 +22,23 @@ export const setupInstallPath = async (): Promise<string> => {
}
};

export const setupTempLocation = async (): Promise<string> => {
const currentPath = Directories.tempLocation();

const path = await dialog.showOpenDialog({
title: 'Select a location for temporary folders',
defaultPath: typeof currentPath === 'string' ? currentPath : '',
properties: ['openDirectory'],
});

if (path.filePaths[0]) {
settings.set('mainSettings.tempLocation', path.filePaths[0]);
return path.filePaths[0];
} else {
return "";
}
};

export const setupLiveriesPath = async (): Promise<string> => {
const currentPath = Directories.liveries();

Expand Down
6 changes: 5 additions & 1 deletion src/renderer/components/AddonSection/index.tsx
Expand Up @@ -319,11 +319,15 @@ export const AircraftSection = (): JSX.Element => {
const installDir = Directories.inCommunity(selectedAddon.targetDirectory);
const tempDir = Directories.temp();

if (tempDir === Directories.community()) {
console.error('Community directory equals temp directory');
return notifyDownload(false);
}

console.log("Installing", track);
console.log("Installing into", installDir, "using temp dir", tempDir);

// Prepare temporary directory
fs.removeSync(tempDir);
fs.mkdirSync(tempDir);

// Copy current install to temporary directory
Expand Down
42 changes: 41 additions & 1 deletion src/renderer/components/SettingsSection/Download.tsx
@@ -1,5 +1,5 @@
import React, { FC } from 'react';
import { setupInstallPath } from 'renderer/actions/install-path.utils';
import { setupInstallPath, setupTempLocation } from 'renderer/actions/install-path.utils';
import settings, { useSetting } from "common/settings";
import { Toggle } from '../Toggle';

Expand Down Expand Up @@ -32,6 +32,40 @@ const InstallPathSettingItem = ({ value, setValue }: SettingItemProps<string>):
);
};

const TempLocationSettingItem = ({ value, setValue }: SettingItemProps<string>): JSX.Element => {
const handleClick = async () => {
const path = await setupTempLocation();

if (path) {
setValue(path);
}
};

return (
<SettingsItem name="Location for temporary folders">
<div className="text-white hover:text-gray-400 cursor-pointer underline transition duration-200" onClick={handleClick}>{value}</div>
</SettingsItem>
);
};

const SeparateTempLocationSettingItem = ({ value, setValue }: SettingItemProps<boolean>) => {
const handleClick = () => {
const newState = !value;
setValue(newState);
settings.set('mainSettings.separateTempLocation', newState);
settings.set('mainSettings.tempLocation', settings.get('mainSettings.msfsPackagePath'));
};

return (
<SettingsItem name="Separate location for temporary folders">
<Toggle
value={value}
onToggle={handleClick}
/>
</SettingsItem>
);
};

const DisableWarningSettingItem = ({ value, setValue }: SettingItemProps<boolean>) => {
const handleClick = () => {
const newState = !value;
Expand Down Expand Up @@ -68,6 +102,8 @@ const UseCdnSettingItem = ({ value, setValue }: SettingItemProps<boolean>) => {

const index = (): JSX.Element => {
const [installPath, setInstallPath] = useSetting<string>('mainSettings.msfsPackagePath');
const [tempLocation, setTempLocation] = useSetting<string>('mainSettings.tempLocation');
const [separateTempLocation, setSeparateTempLocation] = useSetting<boolean>('mainSettings.separateTempLocation');
const [disableVersionWarning, setDisableVersionWarning] = useSetting<boolean>('mainSettings.disableExperimentalWarning');
const [useCdnCache, setUseCdnCache] = useSetting<boolean>('mainSettings.useCdnCache');

Expand All @@ -77,6 +113,10 @@ const index = (): JSX.Element => {
<h2 className="text-white">Download Settings</h2>
<div className="flex flex-col divide-y divide-gray-600">
<InstallPathSettingItem value={installPath} setValue={setInstallPath} />
<SeparateTempLocationSettingItem value={separateTempLocation} setValue={setSeparateTempLocation} />
{separateTempLocation &&
<TempLocationSettingItem value={tempLocation} setValue={setTempLocation} />
}
<DisableWarningSettingItem value={disableVersionWarning} setValue={setDisableVersionWarning} />
<UseCdnSettingItem value={useCdnCache} setValue={setUseCdnCache} />
</div>
Expand Down
24 changes: 18 additions & 6 deletions src/renderer/utils/Directories.ts
Expand Up @@ -10,7 +10,15 @@ export class Directories {
}

static inCommunity(targetDir: string): string {
return path.join(settings.get('mainSettings.msfsPackagePath') as string, targetDir);
return path.join(Directories.community(), targetDir);
}

static tempLocation(): string {
return settings.get('mainSettings.separateTempLocation') ? settings.get('mainSettings.tempLocation') as string : settings.get('mainSettings.msfsPackagePath') as string;
}

static inTempLocation(targetDir: string): string {
return path.join(Directories.tempLocation(), targetDir);
}

static liveries(): string {
Expand All @@ -29,22 +37,26 @@ export class Directories {
}

static temp(): string {
return path.join(settings.get('mainSettings.msfsPackagePath') as string, `flybywire_current_install_${(Math.random() * 1000).toFixed(0)}`);
const dir = path.join(Directories.tempLocation(), `flybywire_current_install_${(Math.random() * 1000).toFixed(0)}`);
if (fs.existsSync(dir)) {
return Directories.temp();
}
return dir;
}

static removeAllTemp(): void {
console.log('[CLEANUP] Removing all temp directories');

if (!fs.existsSync(Directories.community())) {
console.warn('[CLEANUP] Install directory does not exist. Aborting');
if (!fs.existsSync(Directories.tempLocation())) {
console.warn('[CLEANUP] Location of temporary folders does not exist. Aborting');
return;
}

fs.readdirSync(Directories.community(), { withFileTypes: true })
fs.readdirSync(Directories.tempLocation(), { withFileTypes: true })
.filter(dirEnt => dirEnt.isDirectory())
.filter(dirEnt => dirEnt.name.startsWith('flybywire_current_install_'))
.forEach(dir => {
const fullPath = Directories.inCommunity(dir.name);
const fullPath = Directories.inTempLocation(dir.name);

console.log('[CLEANUP] Removing', fullPath);
fs.removeSync(fullPath);
Expand Down

0 comments on commit 6cad169

Please sign in to comment.