From 0a664d1f64e18ef2d286dd7fa96d23504699d542 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Wed, 31 May 2023 07:02:05 +0200 Subject: [PATCH] add tray icon, fix login if password changed --- app.js | 43 +++++++++++++++++++++++++++++++++++++------ ezppUtil.js | 10 +++++++++- preload/preload.js | 10 ++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 291a7c7..62f3a69 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,4 @@ -const { app, BrowserWindow, ipcMain, dialog } = require('electron'); +const { app, BrowserWindow, ipcMain, dialog, Tray, Menu } = require('electron'); const { setupTitlebar, attachTitlebarToWindow } = require('custom-electron-titlebar/main'); const windowManager = require('./ui/windowManager'); const osuUtil = require('./osuUtil'); @@ -9,6 +9,7 @@ const rpc = require('./discordPresence'); const windowName = require('get-window-by-name'); const terminalUtil = require('./terminalUtil'); const osUtil = require('./osUtil'); +const appInfo = require('./appInfo'); let tempOsuPath; let osuWindowInfo; @@ -119,10 +120,37 @@ const run = () => { let mainWindow; + let tray = null app.whenReady().then(() => { + tray = new Tray('./assets/logo.png'); + const trayMenuTemplate = [ + { + label: `EZPPLauncher ${appInfo.appVersion}`, + enabled: false + }, + + { + label: "Show/Hide", + click: function () { + if (mainWindow.isVisible()) mainWindow.hide(); + else mainWindow.show(); + } + }, + + { + label: 'Exit', + click: function () { + app.exit(0); + } + } + ] + + let trayMenu = Menu.buildFromTemplate(trayMenuTemplate) + tray.setContextMenu(trayMenu) + mainWindow = createWindow(); - mainWindow.on('show', async () => { + mainWindow.once('show', async () => { await updateConfigVars(mainWindow); await tryLogin(mainWindow); await doUpdateCheck(mainWindow); @@ -161,7 +189,7 @@ const run = () => { const osuFolder = await config.get("osuPath"); if (!osuFolder || osuFolder == "") { const foundFolder = await osuUtil.findOsuInstallation(); - console.log("osu! Installation located at: ",foundFolder); + console.log("osu! Installation located at: ", foundFolder); } } }) @@ -187,10 +215,10 @@ const run = () => { } rpc.updateState("Launching osu!..."); isIngame = true; - mainWindow.hide(); + if (mainWindow.isVisible()) mainWindow.hide(); const result = await osuUtil.startOsuWithDevServer(tempOsuPath, "ez-pp.farm", async () => { isIngame = false; - mainWindow.show(); + if (!mainWindow.isVisible()) mainWindow.show(); await doUpdateCheck(mainWindow); }); return result; @@ -287,8 +315,11 @@ async function tryLogin(window) { user: loginResponse.user }) } else { + await config.remove("username"); + await config.remove("password"); window.webContents.send('account_update', { - type: "login-failed" + type: "login-failed", + message: loginResponse.message }) } } else { diff --git a/ezppUtil.js b/ezppUtil.js index e7a3336..7695369 100644 --- a/ezppUtil.js +++ b/ezppUtil.js @@ -1,10 +1,18 @@ const axios = require('axios').default; const loginCheckEndpoint = 'https://ez-pp.farm/login/check'; +let retries = 0; const performLogin = async (username, password) => { const result = await axios.post(loginCheckEndpoint, { username, password }); - return result.data; + const code = result.data.code ?? 404; + if (code === 200 || code === 403) { + retries = 0; + return result.data; + } else { + if (retries++ >= 5) return { code: 403, message: "Login failed." } + return await performLogin(username, password); + } } module.exports = { performLogin }; \ No newline at end of file diff --git a/preload/preload.js b/preload/preload.js index 78f6adb..64b1bc7 100644 --- a/preload/preload.js +++ b/preload/preload.js @@ -152,6 +152,16 @@ window.addEventListener('DOMContentLoaded', () => { ipcRenderer.on('account_update', (event, data) => { switch (data.type) { + case "login-failed": + console.log(data); + Swal.fire({ + title: 'Uh oh!', + text: data.message, + icon: 'error', + confirmButtonText: 'Okay' + }); + changePage("launch"); + break; case "not-loggedin": changePage("launch"); break;