TeaSpeak-Client/modules/core/main.ts

178 lines
5.9 KiB
TypeScript
Raw Normal View History

2019-10-25 19:51:40 -04:00
import * as electron from "electron";
2020-10-01 04:56:22 -04:00
import {app, Menu} from "electron";
2019-10-25 19:51:40 -04:00
import MessageBoxOptions = electron.MessageBoxOptions;
2020-10-01 04:56:22 -04:00
import {processArguments, parseProcessArguments, Arguments} from "../shared/process-arguments";
2020-12-02 12:08:49 -05:00
import {openChangeLog as openChangeLog} from "./app-updater/changelog";
2019-10-25 19:51:40 -04:00
import * as crash_handler from "../crash_handler";
2020-10-01 04:56:22 -04:00
import {initializeSingleInstance} from "./MultiInstanceHandler";
2019-10-25 19:51:40 -04:00
2020-10-01 04:56:22 -04:00
import "./AppInstance";
2020-12-02 12:08:49 -05:00
import {dereferenceApp, referenceApp} from "./AppInstance";
import {showUpdateWindow} from "./windows/client-updater/controller/ClientUpdate";
2020-10-01 04:56:22 -04:00
async function handleAppReady() {
Menu.setApplicationMenu(null);
if(processArguments.has_value("update-execute")) {
2020-12-02 12:08:49 -05:00
console.log("Showing update window");
await showUpdateWindow();
2019-10-25 19:51:40 -04:00
return;
2020-10-01 04:56:22 -04:00
} else if(processArguments.has_value("update-failed-new") || processArguments.has_value("update-succeed-new")) {
const success = processArguments.has_value("update-succeed-new");
2019-10-25 19:51:40 -04:00
let data: {
parse_success: boolean;
log_file?: string;
error_id?: string;
error_message?: string;
} = {
parse_success: false
};
try {
2020-10-01 04:56:22 -04:00
let encoded_data = Buffer.from(processArguments.value("update-failed-new") || processArguments.value("update-succeed-new"), "base64").toString();
2019-10-25 19:51:40 -04:00
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;
2019-11-24 12:38:50 -05:00
} catch(error) {
console.warn("Failed to parse update response data: %o", error);
2019-10-25 19:51:40 -04:00
}
console.log("Update success: %o. Update data: %o", success, data);
2020-10-01 04:56:22 -04:00
let title;
let type;
let message;
2019-10-25 19:51:40 -04:00
const buttons: ({
key: string,
callback: () => Promise<boolean>
})[] = [];
if(success) {
2020-12-02 12:08:49 -05:00
openChangeLog();
2019-10-25 19:51:40 -04:00
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 () => {
2020-12-02 12:08:49 -05:00
await showUpdateWindow();
2019-10-25 19:51:40 -04:00
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
});
2019-11-24 12:38:50 -05:00
const result = await electron.dialog.showMessageBox(null, {
2019-10-25 19:51:40 -04:00
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")) {
2020-10-01 04:56:22 -04:00
if(!app.setAsDefaultProtocolClient("teaclient", app.getPath("exe"))) {
console.error("Failed to setup default teaclient protocol handler");
2020-10-01 04:56:22 -04:00
}
}
2019-10-25 19:51:40 -04:00
try {
2020-10-01 04:56:22 -04:00
const main = require("./main-window");
2019-10-25 19:51:40 -04:00
2020-12-02 12:08:49 -05:00
referenceApp();
await main.execute();
dereferenceApp();
2019-10-25 19:51:40 -04:00
} catch (error) {
2020-10-01 04:56:22 -04:00
console.error(error);
await electron.dialog.showMessageBox({
2019-10-25 19:51:40 -04:00
type: "error",
message: "Failed to execute app main!\n" + error,
title: "Main execution failed!",
buttons: ["close"]
} as MessageBoxOptions);
electron.app.exit(1);
}
}
function main() {
2020-10-01 04:56:22 -04:00
if('allowRendererProcessReuse' in app) {
2020-08-22 15:33:30 -04:00
app.allowRendererProcessReuse = false;
2020-10-01 04:56:22 -04:00
}
2020-08-22 15:33:30 -04:00
2020-10-01 04:56:22 -04:00
parseProcessArguments();
if(processArguments.has_value(Arguments.DISABLE_HARDWARE_ACCELERATION)) {
2020-08-22 15:33:30 -04:00
app.disableHardwareAcceleration();
2020-10-01 04:56:22 -04:00
}
2020-10-01 04:56:22 -04:00
if(processArguments.has_value(Arguments.DUMMY_CRASH_MAIN)) {
2020-08-22 15:33:30 -04:00
crash_handler.handler.crash();
2020-10-01 04:56:22 -04:00
}
2020-08-22 15:33:30 -04:00
2020-10-01 04:56:22 -04:00
if(!processArguments.has_value(Arguments.DEBUG) && !processArguments.has_value(Arguments.NO_SINGLE_INSTANCE)) {
if(!initializeSingleInstance()) {
2020-08-22 15:33:30 -04:00
console.log("Another instance is already running. Closing this instance");
app.exit(0);
2019-10-25 19:51:40 -04:00
}
}
2020-10-01 04:56:22 -04:00
app.on('ready', handleAppReady);
2019-10-25 19:51:40 -04:00
}
export const execute = main;