TeaSpeak-Client/modules/core/main.ts
2019-06-26 22:09:01 +02:00

223 lines
7.8 KiB
TypeScript

// Quit when all windows are closed.
import * as electron from "electron";
import * as app_updater from "./app-updater";
import * as forum from "./teaspeak-forum";
import { app } from "electron";
import MessageBoxOptions = electron.MessageBoxOptions;
import {process_args, parse_arguments, Arguments} from "../shared/process-arguments";
import {open as open_changelog} from "./app-updater/changelog";
import * as crash_handler from "../crash_handler";
async function execute_app() {
/* legacy, will be removed soon */
if(process_args.has_value("update-failed")) {
const result = electron.dialog.showMessageBox({
type: "error",
message: "Failed to execute update:\n" + process_args.value("update-failed"),
title: "Update failed!",
buttons: ["retry", "ignore"]
} as MessageBoxOptions);
if(result == 0)
if(await app_updater.execute_graphical(await app_updater.selected_channel(), false))
return;
} else if(process_args.has_value("update-succeed")) {
const result = electron.dialog.showMessageBox({
type: "info",
message: "Update successfully installed!\nShould we launch TeaClient?",
title: "Update succeeded!",
buttons: ["yes", "no"]
} as MessageBoxOptions);
if(result != 0) {
electron.app.exit(0);
return; //Not really required here!
}
}
if(process_args.has_value("update-execute")) {
console.log("Executing update " + process_args.value("update-execute"));
await app_updater.execute_update(process_args.value("update-execute"), callback => {
console.log("Update preconfig successful. Extracting update. (The client should start automatically)");
app.quit();
setImmediate(callback);
});
return;
} else if(process_args.has_value("update-failed-new") || process_args.has_value("update-succeed-new")) {
const success = process_args.has_value("update-succeed-new");
let data: {
parse_success: boolean;
log_file?: string;
error_id?: string;
error_message?: string;
} = {
parse_success: false
};
try {
let encoded_data = Buffer.from(process_args.value("update-failed-new") || process_args.value("update-succeed-new"), "base64").toString();
for(const part of encoded_data.split(";")) {
const index = part.indexOf(':');
if(index == -1)
data[part] = true;
else {
data[part.substr(0, index)] = Buffer.from(part.substr(index + 1), "base64").toString();
}
}
data.parse_success = true;
} catch($) {
console.error($);
}
console.log("Update success: %o. Update data: %o", success, data);
let title = "";
let type = "";
let message = "";
const buttons: ({
key: string,
callback: () => Promise<boolean>
})[] = [];
if(success) {
open_changelog();
type = "info";
title = "Update succeeded!";
message = "Update has been successfully installed!\nWhat do you want to do next?";
buttons.push({
key: "Launch client",
callback: async () => false
});
if(data.parse_success && data.log_file) {
buttons.push({
key: "Open update log",
callback: async () => {
electron.shell.openItem(data.log_file);
return true;
}
});
}
} else {
type = "error";
title = "Update failed!";
message = "Failed to install update.";
if(data.parse_success) {
message += "\n\n";
message += "Error ID: " + (data.error_id || "undefined") + "\n";
message += "Error Message: " + (data.error_message || "undefined") + "\n";
message += "Installer log: " + (data.log_file || "undefined");
} else {
message += "\nUnknown error! Lookup the console for more details.";
}
buttons.push({
key: "Ignore",
callback: async () => false
});
buttons.push({
key: "Retry update",
callback: async () => {
await app_updater.execute_graphical(await app_updater.selected_channel(), false);
return true;
}
});
if(data.parse_success && data.log_file) {
buttons.push({
key: "Open update log",
callback: async () => {
electron.shell.openItem(data.log_file);
return true;
}
});
}
}
buttons.push({
key: "Close",
callback: async () => true
});
const result = await new Promise<number>(resolve => electron.dialog.showMessageBox({
type: type,
message: message,
title: title,
buttons: buttons.map(e => e.key)
} as MessageBoxOptions, resolve));
if(buttons[result].callback) {
if(await buttons[result].callback())
return;
}
}
forum.setup();
try {
await forum.initialize();
}catch(error) {
console.error("Failed to initialize forum connection: %o", error);
const result = electron.dialog.showMessageBox({
type: "error",
message: "Failed to initialize forum connection\nLookup the console for more info",
title: "Main execution failed!",
buttons: ["close"]
} as MessageBoxOptions);
electron.app.exit(1);
return;
}
try {
{
const version = await app_updater.current_version();
global["app_version_client"] = version.toString();
}
const main = require("./main_window");
main.execute();
app_updater.start_auto_update_check();
} catch (error) {
console.dir(error);
const result = electron.dialog.showMessageBox({
type: "error",
message: "Failed to execute app main!\n" + error,
title: "Main execution failed!",
buttons: ["close"]
} as MessageBoxOptions);
electron.app.exit(1);
}
}
function main() {
process.on('uncaughtException', err => {
console.error(err, 'Uncaught Exception thrown');
console.dir(err);
process.exit(1);
});
/*
if(false) {
SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler("crash.log"); // With no argument, SegfaultHandler will generate a generic log file name
}
const SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler("crash.log"); // With no argument, SegfaultHandler will generate a generic log file name
*/
if(app) { //We're executed!
parse_arguments();
if(process_args.has_value(Arguments.DISABLE_HARDWARE_ACCELERATION))
app.disableHardwareAcceleration();
if(process_args.has_value(Arguments.DUMMY_CRASH_MAIN))
crash_handler.handler.crash();
if(!process_args.has_value(Arguments.DEBUG) && !process_args.has_value(Arguments.NO_SINGLE_INSTANCE)) {
if(!app.requestSingleInstanceLock()) {
console.log("Another instance is already running. Closing this instance");
app.exit(0);
}
}
app.on('ready', execute_app);
}
}
export const execute = main;