Client/index.js

181 lines
6.2 KiB
JavaScript

var app = require("app"); // Module to control application life.
var ipc = require("ipc");
var fs = require("fs-extra");
var os = require("os");
var dialog = require("dialog");
var BrowserWindow = require("browser-window");
var mainWindow = null;
app.commandLine.appendSwitch("enable-npapi");
app.commandLine.appendSwitch("no-proxy-server");
var userData = app.getPath("userData");
var unityHomeDir = __dirname + "\\..\\..\\WebPlayer";
// if running in non-packaged / development mode, this dir will be slightly different
if (process.env.npm_node_execpath) {
unityHomeDir = app.getAppPath() + "\\build\\WebPlayer";
}
process.env["UNITY_HOME_DIR"] = unityHomeDir;
process.env["UNITY_DISABLE_PLUGIN_UPDATES"] = "yes";
function initialSetup(firstTime) {
// Display a small window to inform the user that the app is working
setupWindow = new BrowserWindow({
width: 275,
height: 450,
resizable: false,
center: true,
frame: false,
});
if (!firstTime) {
// migration from pre-1.4
// Back everything up, just in case
setupWindow.loadUrl("file://" + __dirname + "/initial-setup.html");
fs.copySync(userData + "\\config.json", userData + "\\config.json.bak");
fs.copySync(
userData + "\\servers.json",
userData + "\\servers.json.bak"
);
fs.copySync(
userData + "\\versions.json",
userData + "\\versions.json.bak"
);
} else {
// first-time setup
// Copy default servers
fs.copySync(
__dirname + "\\defaults\\servers.json",
userData + "\\servers.json"
);
}
// Copy default versions and config
fs.copySync(
__dirname + "\\defaults\\versions.json",
userData + "\\versions.json"
);
fs.copySync(
__dirname + "\\defaults\\config.json",
userData + "\\config.json"
);
console.log("JSON files copied.");
setupWindow.destroy();
showMainWindow();
}
ipc.on("exit", function (id) {
mainWindow.destroy();
});
// Quit when all windows are closed.
app.on("window-all-closed", function () {
if (process.platform != "darwin") app.quit();
});
app.on("ready", function () {
// Check just in case the user forgot to extract the zip.
zipCheck = app.getPath("exe").includes(os.tmpdir());
if (zipCheck) {
var errorMessage =
"It has been detected that OpenFusionClient is running from the TEMP folder.\n\n" +
"Please extract the entire Client folder to a location of your choice before starting OpenFusionClient.";
dialog.showErrorBox("Error!", errorMessage);
return;
}
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1280,
height: 720,
show: false,
"web-preferences": {
plugins: true,
"extra-plugin-dirs": [unityHomeDir + "\\loader"],
},
});
mainWindow.setMinimumSize(640, 480);
// Check for first run
var configPath = userData + "\\config.json";
try {
if (!fs.existsSync(configPath)) {
console.log("Config file not found. Running initial setup.");
initialSetup(true);
} else {
var config = fs.readJsonSync(configPath);
if (!config["last-version-initialized"]) {
console.log("Pre-1.4 config detected. Running migration.");
initialSetup(false);
} else {
showMainWindow();
}
}
} catch (ex) {
dialog.showErrorBox(
"Error!",
"An error occurred while checking for the config. Make sure you have sufficent permissions."
);
app.quit();
}
// Makes it so external links are opened in the system browser, not Electron
mainWindow.webContents.on("new-window", function (event, url) {
event.preventDefault();
require("shell").openExternal(url);
});
mainWindow.on("closed", function () {
mainWindow = null;
});
});
function showMainWindow() {
// Load the index.html of the app.
mainWindow.loadUrl("file://" + __dirname + "/index.html");
// Reduces white flash when opening the program
mainWindow.webContents.on("did-finish-load", function () {
mainWindow.webContents.executeJavaScript("setAppVersionText();");
mainWindow.show();
// everything's loaded, tell the renderer process to do its thing
mainWindow.webContents.executeJavaScript("loadConfig();");
mainWindow.webContents.executeJavaScript("loadGameVersions();");
mainWindow.webContents.executeJavaScript("loadServerList();");
});
mainWindow.webContents.on("plugin-crashed", function () {
var errorMessage =
"Unity Web Player has crashed - please re-open the application.\n" +
"If this error persists, please read the FAQ or ask for support in our Discord server.";
dialog.showErrorBox("Error!", errorMessage);
mainWindow.destroy();
app.quit();
});
mainWindow.webContents.on("will-navigate", function (event, url) {
event.preventDefault();
// TODO: showMessageBox rather than showErrorBox?
switch (url) {
case "https://audience.fusionfall.com/ff/regWizard.do?_flowId=fusionfall-registration-flow":
var errorMessage =
"The register page is currently unimplemented.\n\n" +
'You can still create an account: type your desired username and password into the provided boxes and click "Log In". ' +
"Your account will then be automatically created on the server. \nBe sure to remember these details!";
dialog.showErrorBox("Sorry!", errorMessage);
break;
case "https://audience.fusionfall.com/ff/login.do":
dialog.showErrorBox(
"Sorry!",
"Account management is not available."
);
break;
case "http://forums.fusionfall.com/":
require("shell").openExternal("https://discord.gg/DYavckB");
break;
default:
mainWindow.loadUrl(url);
}
});
}