Compare commits

..

2 Commits

3 changed files with 286 additions and 11 deletions

View File

@ -3,3 +3,8 @@
A Plutonium launcher that aims to make old CoD games more accessible
-----
Currently being written from scratch, please be patient.
## To do
- Make each HTTPS request its own function instead of binding them to an event
- De-duplicate code (HTTPS requests in src/main.js for example)
- Make a better UI/UX

View File

@ -1,21 +1,28 @@
const path = require("node:path");
const fs = require("node:fs");
const fsPromises = require("node:fs/promises");
const https = require("node:https");
const process = require("node:process");
const os = require("node:os");
const child_process = require("node:child_process");
const electron = require("electron");
const update = require(path.join(__dirname, "update.js"));
let configFilePath;
let plutoniumInstallDirectory;
switch (os.platform()) {
case "win32":
configFilePath = path.join(process.env["LOCALAPPDATA"], "Plutonium", "open-plutonium-launcher.json");
plutoniumInstallDirectory = path.join(process.env["LOCALAPPDATA"], "Plutonium");
break;
case "linux":
configFilePath = path.join(os.userInfo().homedir, ".config", "open-plutonium-launcher.json");
plutoniumInstallDirectory = path.join(os.userInfo().homedir, ".local", "share", "Plutonium");
break;
case "darwin":
configFilePath = path.join(os.userInfo().homedir, "Library", "Application Support", "open-plutonium-launcher.json");
plutoniumInstallDirectory = path.join(os.userInfo().homedir, "Library", "Application Support", "Plutonium");
break;
default:
electron.dialog.showErrorBox("Incompatible system", "Sorry, your operating system doesn't seem to be supported...");
@ -37,6 +44,15 @@ let userInfo = {
"emailVerified": false,
"avatar": "https://forum.plutonium.pw/assets/uploads/system/avatar-default.png"
};
let config = {
"user-info": userInfo,
"game-directories": {
"iw5": "",
"t4": "",
"t5": "",
"t6": ""
}
};
function createMainWindow() {
mainWindow = new electron.BrowserWindow({
@ -59,6 +75,34 @@ function createMainWindow() {
});
}
function readConfig(filePath) {
return new Promise(function (resolve, reject) {
if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) return reject(new Error("Config file doesn't exist or is not a file."));
fsPromises.readFile(filePath, "utf-8").then(function (contents) {
try {
let data = JSON.parse(contents);
resolve(data);
} catch (error) {
reject(error);
}
}).catch(function (error) {
reject(error);
});
});
}
function writeConfig(filePath, data = {}) {
return new Promise(function (resolve, reject) {
fsPromises.writeFile(filePath, JSON.stringify(data, null, "\t")).then(function () {
resolve();
}).catch(function (error) {
reject(error);
});
});
}
function fetchPlutoniumManifest() {
return new Promise(function (resolve, reject) {
let request = https.request("https://cdn.plutonium.pw/updater/prod/info.json", {
@ -93,6 +137,158 @@ function fetchPlutoniumManifest() {
});
}
function validatePlutoniumLogin(token) {
return new Promise(function (resolve, reject) {
let request = https.request("https://nix.plutonium.pw/api/auth/validate", {
"method": "GET",
"headers": {
"Content-Length": 0,
"Authorization": "UserToken " + token,
"X-Plutonium-Revision": String(plutoniumManifest.revision),
"User-Agent": "Nix/3.0"
}
}, function (response) {
if (response.statusCode !== 200) {
return resolve({
"status": "unauthenticated",
"successful": false
});
}
response.setEncoding("utf-8");
let body = "";
response.on("data", function (chunk) {
body += chunk;
});
response.on("end", function () {
try {
let data = JSON.parse(body);
if (!("userId" in data)) {
reject(new Error("Authentication seems to be successful but no user identifier was returned."));
}
resolve({
"status": "authenticated",
"successful": true,
...data
});
userInfo = {
"token": token,
...data
};
} catch (error) {
reject(error);
}
});
});
request.on("error", function (error) {
reject(error);
});
request.end();
});
}
function getPlutoniumSession(game, token) {
return new Promise(function (resolve, reject) {
let payload = JSON.stringify({
"game": game
});
let request = https.request("https://nix.plutonium.pw/api/auth/session", {
"method": "POST",
"headers": {
"Content-Type": "application/json",
"Content-Length": payload.length,
"Authorization": "UserToken " + token,
"X-Plutonium-Revision": String(plutoniumManifest.revision),
"User-Agent": "Nix/3.0"
}
}, function (response) {
if (response.statusCode !== 200) {
return resolve({
"status": "unauthenticated",
"successful": false
});
}
response.setEncoding("utf-8");
let body = "";
response.on("data", function (chunk) {
body += chunk;
});
response.on("end", function () {
try {
let data = JSON.parse(body);
if (!("token" in data)) {
reject(new Error("Authentication seems to be successful but no token was returned."));
}
resolve({
"status": "authenticated",
"successful": true,
...data
});
userInfo = data;
mainWindow.loadFile(path.join(__dirname, "src", "views", "games.html"));
} catch (error) {
reject(error);
}
});
});
request.on("error", function (error) {
reject(error);
});
request.write(payload);
request.end();
});
}
function launch(plutoniumInstallDirectory, game, gameInstallDirectory, online) {
return new Promise(function (resolve, reject) {
getPlutoniumSession(game, userInfo.token).then(function (data) {
if (!data.successful) {
return reject(new Error("Authentication has failed."));
}
let bootstrapperBinary = path.join(plutoniumInstallDirectory, "bin", "plutonium-bootstrapper-win32.exe");
let bootstrapperArguments = [game, gameInstallDirectory];
if (online) {
bootstrapperArguments.push("-token");
bootstrapperArguments.push(data.token);
} else {
bootstrapperArguments.push("+name");
bootstrapperArguments.push(userInfo.username);
bootstrapperArguments.push("-lan");
}
let gameProcess = child_process.spawn(bootstrapperBinary, bootstrapperArguments, {
"cwd": plutoniumInstallDirectory,
"detached": true,
"stdio": "ignore"
});
gameProcess.on("spawn", function () {
resolve(true);
});
gameProcess.on("error", function (error) {
reject(error);
});
});
});
}
electron.app.once("ready", function () {
createMainWindow();
fetchPlutoniumManifest().then(function (manifest) {
@ -100,6 +296,33 @@ electron.app.once("ready", function () {
}).catch(function (error) {
electron.dialog.showErrorBox("Error", "The Plutonium launcher manifest could not be fetched, auto-updating might not be possible.\n" + error.message);
});
if (fs.existsSync(configFilePath)) {
readConfig(configFilePath).then(function (data) {
config = data;
if (config["user-info"].token === "V3ryS3cr3t4uth3nt1c4t10nT0k3n") {
return;
} else {
validatePlutoniumLogin(config["user-info"].token).then(function (data) {
if (!data.successful) {
config["user-info"].token = "V3ryS3cr3t4uth3nt1c4t10nT0k3n";
return writeConfig(configFilePath, config).catch(function (error) {
console.error(error);
});
}
mainWindow.loadFile(path.join(__dirname, "src", "views", "games.html"));
});
}
}).catch(function (error) {
electron.dialog.showErrorBox("Error", "Configuration file could not be read.\n" + error.message);
});
} else {
writeConfig(configFilePath, config).catch(function (error) {
electron.dialog.showErrorBox("Error", "Configuration file doesn't exist and could not be created.\n" + error.message);
});
}
});
electron.app.on("window-all-closed", function () {
@ -151,6 +374,10 @@ electron.ipcMain.handle("login", function (event, username, password) {
});
userInfo = data;
config["user-info"] = userInfo;
writeConfig(configFilePath, config).catch(function (error) {
console.error(error);
});
mainWindow.loadFile(path.join(__dirname, "src", "views", "games.html"));
} catch (error) {
@ -167,3 +394,28 @@ electron.ipcMain.handle("login", function (event, username, password) {
request.end();
});
});
electron.ipcMain.handle("launch", function (event, game, online = true) {
return new Promise(function (resolve, reject) {
if (!(game in config.gameDirectories)) {
return resolve(false);
}
let gameInstallDirectory = config.gameDirectories[game];
update.checkFiles(plutoniumManifest, plutoniumInstallDirectory).then(function (fileList) {
let filesToUpdate = fileList.filter(function (file) {
return !file.ok;
});
update.downloadFiles(plutoniumInstallDirectory, plutoniumManifest.baseUrl, filesToUpdate).then(function () {
launch(plutoniumInstallDirectory, game, gameInstallDirectory, online).then(function (result) {
resolve(result);
});
}).catch(function (error) {
reject(error);
});
}).catch(function (error) {
reject(error);
});
});
});

View File

@ -63,17 +63,20 @@ function processDownloadQueue(baseDirectory, baseURL, fileEntries) {
let queue = [...fileEntries];
let finishCallback = function () {
if (queue.length > 0) {
runningDownloads--;
if (queue.length > 0 && runningDownloads < module.exports.concurrentDownloads) {
let currentEntry = queue.shift();
downloadFile(baseURL + currentEntry.hash, path.join(baseDirectory, currentEntry.name)).then(finishCallback).catch((errorCallback).bind(currentEntry));
} else {
resolve();
}
};
for (let d = 0; d < module.exports.concurrentDownloads; d++) {
if (queue.length < 1) break;
let errorCallback = function (error) {
runningDownloads--;
let currentEntry = queue.shift();
downloadFile(baseURL + currentEntry.hash, path.join(baseDirectory, currentEntry.name)).then(finishCallback).catch((function (error) {
if (!this.retries) {
this.retries = 0;
}
@ -81,7 +84,16 @@ function processDownloadQueue(baseDirectory, baseURL, fileEntries) {
this.retries++;
queue.push(this);
}).bind(currentEntry));
};
for (let d = 0; d < module.exports.concurrentDownloads; d++) {
if (queue.length < 1) break;
let currentEntry = queue.shift();
runningDownloads++;
downloadFile(baseURL + currentEntry.hash, path.join(baseDirectory, currentEntry.name)).then(finishCallback).catch((errorCallback).bind(currentEntry));
}
});
}
@ -107,9 +119,15 @@ module.exports = {
},
"downloadFiles": function (baseDirectory, baseURL, fileEntries) {
return new Promise(function (resolve, reject) {
processDownloadQueue(baseDirectory, baseURL, fileEntries.filter(function (entry) {
let filesToProcess = fileEntries.filter(function (entry) {
return !entry.ok;
})).then(function () {
});
if (filesToProcess.length < 1) {
return resolve(true);
}
processDownloadQueue(baseDirectory, baseURL, filesToProcess).then(function () {
resolve(true);
}).catch(function (error) {
reject(error);