Skip to content

Commit

Permalink
server: autodetect install and packages paths for MS store
Browse files Browse the repository at this point in the history
Autodetect install and packages paths for MS store version. Note that since 1.15.8.0 the base packages are readable even for the MS store version.

Signed-off-by: Octavian Purdila <tavi@cs.pub.ro>
  • Loading branch information
tavip committed May 3, 2021
1 parent 9fd03a2 commit 3fddf7e
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 31 deletions.
71 changes: 71 additions & 0 deletions package-lock.json

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

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
"version": "0.0.1",
"description": "A Web Simulator for the Microsoft Flight Simulator",
"homepage": "https://github.com/tavip/msfs-websim",
"license": "GPLv2",
"repository": {
"type": "git",
"url": "https://github.com/tavip/msfs-websim.git"
},
"license": "GPL-2.0-or-later",
"scripts": {
"start": "node src/server.js"
},
"config": {
"port": "8080",
"msfs_install_dir": "D:/steam/steamapps/common/MicrosoftFlightSimulator/",
"msfs_packages_dir": "D:/msfs2020/"
"msfs_install_dir": "",
"msfs_packages_dir": ""
},
"dependencies": {
"glob": "^7.1.6"
"glob": "^7.1.6",
"node-powershell": "4.0.0"
}
}
100 changes: 73 additions & 27 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,44 @@ var http = require('http');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const powershell = require('node-powershell');
const { Buffer } = require('buffer');
const { dir } = require('console');
const { runInContext } = require('vm');
const { type } = require('os');

var config = {
"port": process.env.npm_package_config_port,
"msfs_install_dir": process.env.npm_package_config_msfs_install_dir,
"msfs_packages_dir": process.env.npm_package_config_msfs_packages_dir
};

try {
if (!fs.statSync(config.msfs_install_dir + '/Packages').isDirectory()) {
throw "invalid MSFS install directory";
}
if (!fs.statSync(config.msfs_packages_dir + '/Official/OneStore').isDirectory()) {
throw ("invalid MSFS packages directory");
async function findMSFS() {
const msStoreName = "Microsoft.FlightSimulator";
let msStorePubId;
let ps = new powershell();

if (!config.msfs_install_dir) {
ps.addCommand(`Get-AppxPackage ${msStoreName} | ConvertTo-Json`)
let res = JSON.parse(await ps.invoke());
if (res) {
config.msfs_install_dir = res.InstallLocation;
msStorePubId = res.PublisherId;
}
ps.clear();
}
if (!config.port) {
console.warn("using default 8080 port")
config.port=8080;
if (!config.msfs_packages_dir) {
if (msStorePubId) {
ps.addCommand(`cat ${process.env.LOCALAPPDATA}\\Packages\\${msStoreName}_${msStorePubId}\\LocalCache\\UserCfg.opt | Select-String InstalledPackagesPath`);
let res = await ps.invoke();
if (res) {
config.msfs_packages_dir = res.match(/"(.*)"/)[1]
}
}
}
} catch (err) {
console.error("bad configuration, exiting");
throw err;
ps.dispose();
}

var packages_dirs = [ config.msfs_packages_dir + '/Official/OneStore/',
config.msfs_packages_dir + '/Community/', '.' ]

/**
* Create a database with all of the files from all packages for a given
* resource type.
Expand Down Expand Up @@ -75,22 +85,50 @@ function search_files(top_dirs, type, filematch=null) {
}

console.log(`found ${files.size} files`);
return files
return files;
}

html_ui_files = search_files([config.msfs_install_dir + '/Packages'].concat(packages_dirs), 'html_ui')
vfs_files = search_files(packages_dirs, 'SimObjects', [ '.xml', 'thumbnail.jpg' ])
async function findFiles() {
await findMSFS();

/**
* Some HTML files pull in CSS files by using relative paths (e.g. style.css
* instead of a/b/style.css). To work-around this issue keep an array with the
* directories of HTML files and search the CSS files in all of them.
*/
html_dirs = new Array();
html_dirs.push("");
try {
if (!fs.statSync(config.msfs_install_dir + '/Packages').isDirectory()) {
throw "invalid MSFS install directory";
} else {
console.log(`MSFS install dir: ${config.msfs_install_dir}`);
}
if (!fs.statSync(config.msfs_packages_dir + '/Official/OneStore').isDirectory()) {
throw ("invalid MSFS packages directory");
} else {
console.log(`MSFS packages dir: ${config.msfs_packages_dir}`);
}
if (!config.port) {
console.warn("using default 8080 port")
config.port = 8080;
}
} catch (err) {
console.error("bad configuration, exiting");
throw err;
}

let packages_dirs = [
config.msfs_packages_dir + '/Official/OneStore/',
config.msfs_packages_dir + '/Community/', '.'
]

html_ui_files = search_files([config.msfs_install_dir + '/Packages'].concat(packages_dirs), 'html_ui')
vfs_files = search_files(packages_dirs, 'SimObjects', [ '.xml', 'thumbnail.jpg' ])

/**
* Some HTML files pull in CSS files by using relative paths (e.g. style.css
* instead of a/b/style.css). To work-around this issue keep an array with the
* directories of HTML files and search the CSS files in all of them.
*/
html_dirs = new Array();
html_dirs.push("");
}

http.createServer(function (req, resp) {
function requestListener(req, resp) {
var f = req.url.split('?')[0];
let contentType = 'text/plain';
var files = html_ui_files;
Expand Down Expand Up @@ -175,4 +213,12 @@ http.createServer(function (req, resp) {
}
});
}
}).listen(config.port);
}

async function main() {
await findFiles();
console.log(`starting server at http://localhost:${config.port}`)
http.createServer(requestListener).listen(config.port);
}

main()

0 comments on commit 3fddf7e

Please sign in to comment.