Documented manager code

This commit is contained in:
Pax1601
2024-01-03 15:08:06 +01:00
parent 7bf6c1bb23
commit a0634a7f99
11 changed files with 263 additions and 82 deletions

View File

@@ -5,6 +5,9 @@ const path = require('path');
const { showWaitPopup } = require('./popup');
const homeDir = require('os').homedir();
/** Conveniency function to asynchronously delete a single file, with error catching
*
*/
async function deleteFile(filePath) {
console.log(`Deleting ${filePath}`);
var promise = new Promise((res, rej) => {
@@ -27,6 +30,9 @@ async function deleteFile(filePath) {
return promise;
}
/** Given a list of Olympus instances, it fixes/updates them by deleting the existing installation and the copying over the clean files
*
*/
async function fixInstances(instances) {
var promise = new Promise((res, rej) => {
var instancePromises = instances.map((instance) => {
@@ -46,6 +52,9 @@ async function fixInstances(instances) {
return promise;
}
/** Uninstalls a specific instance given its folder
*
*/
async function uninstallInstance(folder, name) {
console.log(`Uninstalling Olympus from ${folder}`)
showWaitPopup("Please wait while the Olympus installation is being uninstalled.")
@@ -59,6 +68,9 @@ async function uninstallInstance(folder, name) {
return promise;
}
/** Installs the Hooks script
*
*/
async function installHooks(folder) {
console.log(`Installing hooks in ${folder}`)
var promise = new Promise((res, rej) => {
@@ -76,6 +88,10 @@ async function installHooks(folder) {
return promise;
}
/** Installs the Mod folder
*
*/
// TODO: add option to not copy over databases
async function installMod(folder) {
console.log(`Installing mod in ${folder}`)
var promise = new Promise((res, rej) => {
@@ -93,6 +109,9 @@ async function installMod(folder) {
return promise;
}
/** Installs the olympus.json file
*
*/
async function installJSON(folder) {
console.log(`Installing config in ${folder}`)
var promise = new Promise((res, rej) => {
@@ -110,38 +129,9 @@ async function installJSON(folder) {
return promise;
}
async function applyConfiguration(folder, instance) {
console.log(`Applying configuration to Olympus in ${folder}`);
var promise = new Promise((res, rej) => {
if (fs.existsSync(path.join(folder, "Config", "olympus.json"))) {
var config = JSON.parse(fs.readFileSync(path.join(folder, "Config", "olympus.json")));
config["client"]["port"] = instance.clientPort;
config["server"]["port"] = instance.backendPort;
config["server"]["address"] = instance.backendAddress;
config["authentication"]["gameMasterPassword"] = sha256(instance.gameMasterPassword);
config["authentication"]["blueCommanderPassword"] = sha256(instance.blueCommanderPassword);
config["authentication"]["redCommanderPassword"] = sha256(instance.redCommanderPassword);
fs.writeFile(path.join(folder, "Config", "olympus.json"), JSON.stringify(config, null, 4), (err) => {
if (err) {
console.log(`Error applying config in ${folder}: ${err}`)
rej(err);
}
else {
console.log(`Config succesfully applied in ${folder}`)
res(true);
}
});
} else {
rej("File does not exist")
}
res(true);
});
return promise;
}
/** Creates shortcuts both in the DCS Saved Games folder and on the desktop
*
*/
async function installShortCuts(folder, name) {
console.log(`Installing shortcuts for Olympus in ${folder}`);
var promise = new Promise((res, rej) => {
@@ -196,11 +186,52 @@ async function installShortCuts(folder, name) {
return promise;
}
/** Writes the configuration of an instance to the olympus.json file
*
*/
async function applyConfiguration(folder, instance) {
console.log(`Applying configuration to Olympus in ${folder}`);
var promise = new Promise((res, rej) => {
if (fs.existsSync(path.join(folder, "Config", "olympus.json"))) {
var config = JSON.parse(fs.readFileSync(path.join(folder, "Config", "olympus.json")));
config["client"]["port"] = instance.clientPort;
config["server"]["port"] = instance.backendPort;
config["server"]["address"] = instance.backendAddress;
config["authentication"]["gameMasterPassword"] = sha256(instance.gameMasterPassword);
config["authentication"]["blueCommanderPassword"] = sha256(instance.blueCommanderPassword);
config["authentication"]["redCommanderPassword"] = sha256(instance.redCommanderPassword);
fs.writeFile(path.join(folder, "Config", "olympus.json"), JSON.stringify(config, null, 4), (err) => {
if (err) {
console.log(`Error applying config in ${folder}: ${err}`)
rej(err);
}
else {
console.log(`Config succesfully applied in ${folder}`)
res(true);
}
});
} else {
rej("File does not exist")
}
res(true);
});
return promise;
}
/** Deletes the Hooks script
*
*/
async function deleteHooks(folder) {
console.log(`Deleting hooks from ${folder}`);
return deleteFile(path.join(folder, "Scripts", "Hooks", "OlympusHook.lua"));
}
/** Deletes the Mod folder
*
*/
async function deleteMod(folder) {
console.log(`Deleting mod from ${folder}`);
var promise = new Promise((res, rej) => {
@@ -222,11 +253,17 @@ async function deleteMod(folder) {
return promise;
}
/** Deletes the olympus.json configuration file
*
*/
async function deleteJSON(folder) {
console.log(`Deleting JSON from ${folder}`);
return deleteFile(path.join(folder, "Config", "olympus.json"));
}
/** Deletes the shortcuts
*
*/
async function deleteShortCuts(folder, name) {
console.log(`Deleting ShortCuts from ${folder}`);
var promise = new Promise((res, rej) => {