This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.ts
350 lines (313 loc) · 12.7 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { app, BrowserWindow, ipcMain, shell, dialog, screen, App } from "electron";
import { autoUpdater } from "electron-updater";
import isDev from "electron-is-dev";
import path from "path";
import os from "os";
import settingsPage from "./settings-page/settingspage";
import patchnotesPage from "./patchnotes-page/patchnotespage";
import ServerListPage from "./serverlist-page/serverlistpage";
import mod_manager from "./modules/mod_manager";
import Utilities from "./modules/utilities";
import { ModListLoader, ModList } from "./modules/mod_list_loader";
import Config, {ConfigObject} from "./modules/config";
// There are 6 levels of logging: error, warn, info, verbose, debug and silly
import log from "electron-log";
log.transports.console.format = "[{d}-{m}-{y}] [{h}:{i}:{s}T{z}] -- [{processType}] -- [{level}] -- {text}";
log.transports.file.format = "[{d}-{m}-{y}] [{h}:{i}:{s}T{z}] -- [{processType}] -- [{level}] -- {text}";
log.transports.file.fileName = "main.log";
log.transports.file.maxSize = 10485760;
log.transports.file.getFile();
const majorErrorMessageEnd = "\nIf this error persists, please report it on our GitHub page by creating a new 'Issue'.\nVisit creators.tf/launcher for more info.";
class Main {
public static mainWindow: BrowserWindow;
public static app: App;
public static config: ConfigObject;
public static screenWidth: number;
public static screenHeight: number;
public static minWindowWidth: number;
public static minWindowHeight: number;
public static icon: string;
public static createWindow() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
this.screenWidth = width;
this.screenHeight = height;
this.minWindowWidth = 960;
this.minWindowHeight = 540;
this.icon = path.join(__dirname, "..", "images/installer/256x256.png");
try {
Main.mainWindow = new BrowserWindow({
minWidth: this.minWindowWidth,
minHeight: this.minWindowHeight,
width: this.screenWidth-200,
height: this.screenHeight-150,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
nodeIntegration: false,
contextIsolation: false
},
center: true,
maximizable: true,
resizable: true,
autoHideMenuBar: true,
darkTheme: true,
backgroundColor: "#2B2826",
icon: this.icon
});
//@ts-ignore
global.mainWindow = Main.mainWindow;
Main.app = app;
if (!isDev) {
Main.mainWindow.removeMenu();
}
//Lets load the config file.
Config.GetConfig().then((c) => {
//Make sure the config is loaded in.
// and load the index.html of the app.
//Also setup the mod manager.
this.config = c;
try {
mod_manager.Setup().then(() => {
Main.mainWindow.loadFile(path.join(__dirname, "..", "index.html"));
delete require("electron").nativeImage.createThumbnailFromPath;
});
}
catch (e) {
log.error(e.toString());
dialog.showMessageBox({
type: "error",
title: "Startup Error - Main Window Load",
message: e.toString() + majorErrorMessageEnd,
buttons: ["OK"]
}).then(() => {
app.quit();
});
}
})
.catch((e) => {
log.error(e.toString());
dialog.showMessageBox({
type: "error",
title: "Startup Error - Config Load",
message: e.toString() + majorErrorMessageEnd,
buttons: ["OK"]
}).then(() => {
app.quit();
});
});
}
catch (majorE) {
log.error(majorE.toString());
dialog.showMessageBox({
type: "error",
title: "Startup Error - Major Initial Error",
message: majorE.toString() + majorErrorMessageEnd,
buttons: ["OK"]
}).then(() => {
app.quit();
});
}
}
public static logDeviceInfo() {
log.log(`Basic System Information: [platform: ${os.platform()}, release: ${os.release()}, arch: ${os.arch()}, systemmem: ${(((os.totalmem() / 1024) / 1024) / 1024).toFixed(2)} gb]`);
}
public static autoUpdateCheckAndSettings() {
autoUpdater.checkForUpdatesAndNotify();
autoUpdater.logger = log;
autoUpdater.autoDownload = false;
log.info("Checking for updates.");
}
public static getClientCurrentVersion() {
const lVer = Utilities.GetCurrentVersion();
if (lVer != null) {
log.info("Current launcher version: " + lVer);
}
else {
log.error("Failed to get launcher version");
}
}
}
export default Main;
app.on("ready", () => {
try {
ModListLoader.LoadLocalModList();
Main.createWindow();
Main.getClientCurrentVersion();
Main.autoUpdateCheckAndSettings();
Main.logDeviceInfo();
log.info("Launcher was opened/finished initialization.");
}
catch(error) {
log.error(error.toString());
dialog.showMessageBox({
type: "error",
title: "App Ready Error - Major Initial Error",
message: error.toString() + majorErrorMessageEnd,
buttons: ["OK"]
}).then(() => {
app.quit();
});
}
});
app.on("window-all-closed", () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
log.info("Launcher was closed.");
}
});
autoUpdater.on("checking-for-update", () => {
log.info("Checking for updates");
});
autoUpdater.on("update-not-available", () => {
Main.mainWindow.webContents.send("update_not_available");
log.info("No updates available");
});
autoUpdater.on("update-available", () => {
Main.mainWindow.webContents.send("update_available");
log.info("An update is available");
});
ipcMain.on("download_update", () => {
autoUpdater.downloadUpdate();
Main.mainWindow.webContents.send("update_downloading");
log.info("Downloading update");
});
autoUpdater.on("update-downloaded", () => {
Main.mainWindow.webContents.send("update_downloaded");
log.info("Update downloaded");
});
autoUpdater.on("error", (err) => {
Main.mainWindow.webContents.send("update_error");
log.error("Error in auto-updater: " + err);
});
ipcMain.on("restart_app", () => {
autoUpdater.quitAndInstall();
log.info("Restarting program to install an update");
});
ipcMain.on("SettingsWindow", async () => {
settingsPage.OpenWindow(Main.mainWindow, Main.screenWidth, Main.screenHeight, Main.minWindowWidth, Main.minWindowHeight, Main.config, Main.icon);
});
ipcMain.on("PatchNotesWindow", async () => {
patchnotesPage.OpenWindow(Main.mainWindow, Main.screenWidth, Main.screenHeight, Main.minWindowWidth, Main.minWindowHeight, Main.icon);
});
ipcMain.on("ServerListWindow", async () => {
//Get the mod list data so we can get the server providers for the current mod.
const modList = ModListLoader.GetModList();
//Make sacrificial object soo the local method exists. Thanks js on your half assed oo.
const realModList = new ModList();
Object.assign(realModList, modList);
const providers = realModList.GetMod(mod_manager.currentModData.name).serverlistproviders;
if (providers != null) {
ServerListPage.OpenWindow(Main.mainWindow, Main.screenWidth, Main.screenHeight, Main.minWindowWidth, Main.minWindowHeight, providers, Main.icon);
}
else if (isDev) {
Utilities.ErrorDialog("There were no providers for the current mod! Populate the 'serverlistproviders' property", "Missing Server Providers");
} else {
log.error("There were no providers for the current mod! Did not open server list page.");
}
});
ipcMain.on("GetConfig", async (event) => {
event.reply("GetConfig-Reply", Main.config);
});
ipcMain.on("SetCurrentMod", async (event, arg) => {
try {
const result = await mod_manager.ChangeCurrentMod(arg);
event.reply("InstallButtonName-Reply", result);
} catch (error) {
event.reply("InstallButtonName-Reply", "Internal Error");
Utilities.ErrorDialog(`Failed to check if mod "${arg}" has updates.\n${error}`, "Mod Update Check Error");
}
});
ipcMain.on("install-play-click", async (event, args) => {
await mod_manager.ModInstallPlayButtonClick(args);
});
ipcMain.on("Visit-Mod-Social", async (event, arg) => {
const socialLink = mod_manager.currentModData[arg];
if (socialLink != null && socialLink != "") {
shell.openExternal(socialLink);
}
});
ipcMain.on("Open-External-Game", async () => {
const steamProtocol = "steam://run/";
const steamProtocolMod = "steam://rungameid/";
const isMod = mod_manager.currentModData.isMod;
const game = steamProtocol + mod_manager.currentModData.gameId;
const gameMod = steamProtocolMod + mod_manager.currentModData.gameId;
const gameDefault = steamProtocol + "440";
if (mod_manager.currentModData.gameId != "" && mod_manager.currentModState == "INSTALLED") {
if (isMod == false) {
log.log("GAME LAUNCHING: User initiated (non-mod) game: " + game);
shell.openExternal(game);
} else {
log.log("GAME LAUNCHING: User initiated (mod) game: " + gameMod);
shell.openExternal(gameMod);
}
} else if (mod_manager.currentModData.gameId == "" && mod_manager.currentModState == "INSTALLED") {
log.log("GAME LAUNCHING: Current mod doesn't have a gameId, initiating default game: " + gameDefault);
shell.openExternal(gameDefault);
} else {
log.log("GAME LAUNCHING: Can't initiate the current mod's game. It's either uninstalled or with a pending update.");
}
});
// We can now access everything we need from ModVersion[] here
ipcMain.on("GetCurrentModVersion", async (event) => {
let mod: string;
try {
mod = mod_manager.GetCurrentModVersionFromConfig(mod_manager.currentModData.name);
if (mod == null) {
mod = "";
}
} catch {
mod = "";
}
event.reply("GetCurrentModVersion-Reply", mod);
});
ipcMain.on("Remove-Mod", async () => {
if (mod_manager.currentModData != null && (mod_manager.currentModState == "INSTALLED" || mod_manager.currentModState == "UPDATE")) {
dialog.showMessageBox(Main.mainWindow, {
type: "warning",
title: `Remove Mod - ${mod_manager.currentModData.name}`,
message: `Would you like to uninstall "${mod_manager.currentModData.name}"?`,
buttons: ["Yes", "Cancel"],
cancelId: 1
}).then(async (button) => {
if (button.response == 0) {
log.info(`Starting the mod removal process for "${mod_manager.currentModData.name}". User said yes.`);
await mod_manager.RemoveCurrentMod();
}
});
}
});
ipcMain.on("config-reload-tf2directory", async (event, steamdir) => {
if (steamdir != "") {
const tf2dir = await Config.GetTF2Directory(steamdir);
if (tf2dir && tf2dir != "") {
Main.config.steam_directory = steamdir;
}
Main.config.tf2_directory = tf2dir;
event.reply("GetConfig-Reply", Main.config);
}
else {
Utilities.ErrorDialog("A Steam installation directory is required! Please populate your Steam installation path to auto locate TF2.\ne.g. 'C:/Program Files (x86)/Steam'", "TF2 Locate Error");
}
});
ipcMain.on("GetModData", async (event) => {
ModListLoader.CheckForUpdates().then(() => {
ModListLoader.UpdateLocalModList();
if (isDev) {
log.verbose("Development only mods were added.");
ModListLoader.InjectDevMods();
}
log.verbose("Latest mod list was sent to renderer");
const modList = ModListLoader.GetModList();
event.reply("ShowMods", {
mods: modList.mods
});
});
});
ipcMain.on("get-config", async (event) => {
const res = await Config.GetConfig();
event.reply(res);
});
//Quickplay
//ipcMain.on("")