TeaSpeak-Client/modules/core/main.ts
2020-04-02 15:12:09 +02:00

184 lines
6.5 KiB
TypeScript

// Quit when all windows are closed.
import * as electron from "electron";
import * as app_updater from "./app-updater";
import * as instance_handler from "./instance_handler";
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";
import {open_preview} from "./url-preview";
async function execute_app() {
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(error) {
console.warn("Failed to parse update response data: %o", 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 electron.dialog.showMessageBox(null, {
type: type,
message: message,
title: title,
buttons: buttons.map(e => e.key)
} as MessageBoxOptions);
if(buttons[result.response].callback) {
if(await buttons[result.response].callback())
return;
}
}
/* register client a teaclient:// handler */
if(app.getAppPath().endsWith(".asar")) {
if(!app.setAsDefaultProtocolClient("teaclient", app.getPath("exe")))
console.error("Failed to setup default teaclient protocol handler");
}
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('Uncaught Exception thrown:');
console.dir(err);
process.exit(1);
});
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('second-instance', (event, argv, workingDirectory) => instance_handler.handle_second_instance_call(argv, workingDirectory));
}
app.on('ready', execute_app);
}
}
export const execute = main;