TeaSpeak-Client/modules/core/main.ts

183 lines
6.2 KiB
TypeScript
Raw Normal View History

2019-10-25 19:51:40 -04:00
import * as electron from "electron";
import * as app_updater from "./app-updater";
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";
2019-10-25 19:51:40 -04:00
import {open as open_changelog} from "./app-updater/changelog";
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";
async function handleAppReady() {
Menu.setApplicationMenu(null);
if(processArguments.has_value("update-execute")) {
console.log("Executing update " + processArguments.value("update-execute"));
await app_updater.execute_update(processArguments.value("update-execute"), callback => {
2019-10-25 19:51:40 -04:00
console.log("Update preconfig successful. Extracting update. (The client should start automatically)");
app.quit();
setImmediate(callback);
});
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) {
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
});
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 version = await app_updater.current_version();
global["app_version_client"] = version.toString();
const main = require("./main-window");
2019-10-25 19:51:40 -04:00
main.execute();
app_updater.start_auto_update_check();
} 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;