Skip to content

Commit

Permalink
use documents folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Saschl committed Sep 30, 2024
1 parent 7db54bd commit 51ceb92
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 32 deletions.
15 changes: 7 additions & 8 deletions apps/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,31 @@ bootstrap();

function generateResourceFolders() {
dirs.forEach((dir) => {
access(dir, (error) => {
if (error) mkdirSync(path.join(getSimbridgeDir(), dir), { recursive: true });
const actualDir = path.join(getSimbridgeDir(), dir);
access(actualDir, (error) => {
if (error) mkdirSync(actualDir, { recursive: true });
});
});
}

function generateDefaultProperties() {

const propertiesFilePath = path.join(getSimbridgeDir(), '/resources', '/properties.json');

const defaultProperties = {
server: {
port: 8380,
hidden: true,
closeWithMSFS: false
closeWithMSFS: false,
},
printer: {
enabled: false,
printerName: null,
fontSize: 19,
paperSize: "A4",
margin: 30
}
paperSize: 'A4',
margin: 30,
},
};


if (!existsSync(propertiesFilePath)) {
writeFileSync(propertiesFilePath, JSON.stringify(defaultProperties));
}
Expand Down
31 changes: 13 additions & 18 deletions apps/server/src/terrain.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import * as fs from 'fs';

import axios from 'axios';

import * as path from 'path';
import * as os from 'os';

import axios from 'axios';

const SIMBRIDGE_FOLDER = path.join(os.homedir() + '/flybywire-externaltools-simbridge');
const TERRAIN_MAP_FOLDER = path.join(SIMBRIDGE_FOLDER, '/terrain');
const TERRAIN_MAP_PATH = path.join(TERRAIN_MAP_FOLDER, '/terrain.map');
import { getSimbridgeDir } from 'apps/server/src/utilities/pathUtil';

const SIMBRIDGE_FOLDER = getSimbridgeDir();
const TERRAIN_MAP_FOLDER = path.join(SIMBRIDGE_FOLDER, 'terrain');
const TERRAIN_MAP_PATH = path.join(TERRAIN_MAP_FOLDER, 'terrain.map');

const TERRAIN_MAP_CDN = 'https://cdn.flybywiresim.com/addons/simbridge/terrain-db-binaries/terrain.map';

Expand All @@ -18,28 +15,26 @@ const execute = async () => {
// Create the folders if they don't exist
if (!fs.existsSync(TERRAIN_MAP_FOLDER)) fs.mkdirSync(TERRAIN_MAP_FOLDER);

// Make sure to unlink the old terrain map so we can update it if needed
// if (fs.existsSync(TERRAIN_MAP_PATH)) fs.unlinkSync(TERRAIN_MAP_PATH);

if (!fs.existsSync(TERRAIN_MAP_PATH)) {
// Terrain map is not cached, download it
console.log('Downloading and caching terrain map');
console.log('Downloading terrain map');

const terrainResponse = await axios.get(TERRAIN_MAP_CDN, { responseType: 'stream' });

return new Promise((resolve, reject) => {
const writer = fs.createWriteStream(TERRAIN_MAP_PATH);
terrainResponse.data.pipe(writer);
let error: Error = null;

/* writer.on('error', err => {
writer.on('error', (err) => {
error = err;
writer.close();
reject(err);
}); */
});

writer.on('close', resolve);
writer.on('close', () => {
if (!error) resolve(0);
});
});


}
} catch (error) {
console.error(error);
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/utilities/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ export class FileService {
scale: number = 4,
): Promise<StreamableFile> {
// Some PDFs need external cmaps.
const CMAP_URL = `${join(getSimbridgeDir(), 'node_modules', 'pdfjs-dist', 'cmaps')}/`;
const CMAP_URL = `${join(getExecutablePath(), 'node_modules', 'pdfjs-dist', 'cmaps')}/`;
const CMAP_PACKED = true;

// Where the standard fonts are located.
const STANDARD_FONT_DATA_URL = `${join(getSimbridgeDir(), 'node_modules', 'pdfjs-dist', 'standard_fonts')}/`;
const STANDARD_FONT_DATA_URL = `${join(getExecutablePath(), 'node_modules', 'pdfjs-dist', 'standard_fonts')}/`;

try {
const conversionFilePath = join(getSimbridgeDir(), directory, fileName);
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/utilities/pathUtil.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { homedir } from 'os'
import getPath from 'platform-folders';
import * as path from 'path';

export const getSimbridgeDir = () => (path.join(homedir() + '/flybywire-externaltools-simbridge'));
export const getSimbridgeDir = () => path.join(getPath('documents'), 'FlyByWireSim', 'Simbridge');

//@ts-expect-error pkg only defined when running as exe
export const getExecutablePath = () => (process.pkg ? path.dirname(process.argv[0]) : process.cwd());
4 changes: 2 additions & 2 deletions apps/server/src/utilities/systray.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { hideConsole, showConsole } from 'node-hide-console-window';
import open = require('open');
import SysTray, { MenuItem } from 'systray2';
import { join } from 'path';
import { getSimbridgeDir } from 'apps/server/src/utilities/pathUtil';
import { getExecutablePath, getSimbridgeDir } from 'apps/server/src/utilities/pathUtil';
import { NetworkService } from './network.service';
import serverConfig from '../config/server.config';
import { ShutDownService } from './shutdown.service';
Expand All @@ -29,7 +29,7 @@ export class SysTrayService implements OnApplicationShutdown {
tooltip: 'Flybywire SimBridge',
items: [this.remoteDisplayItem, this.resourcesFolderItem, this.consoleVisibleItem, this.exitItem],
},
copyDir: getSimbridgeDir(),
copyDir: getExecutablePath(),
});

this.sysTray.onClick((action) => {
Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"pdf-to-printer": "5.3.0",
"pdfjs-dist": "^2.13.216",
"pdfkit": "^0.13.0",
"platform-folders": "^0.6.0",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"react-use-websocket": "^2.9.1",
Expand Down

0 comments on commit 51ceb92

Please sign in to comment.