From 36470e140398e975e3a3c10e46cbab1727a556b0 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Sun, 24 Oct 2021 09:59:43 +0200 Subject: [PATCH] Added DiscordRPC class --- entry.js | 4 +- src/ts/index.ts | 79 +++++++++------------------------------- src/ts/lib/DiscordRPC.ts | 46 +++++++++++++++++++++++ src/ts/lib/Genshinlib.ts | 4 +- src/ts/settings.ts | 10 ++++- 5 files changed, 75 insertions(+), 68 deletions(-) create mode 100644 src/ts/lib/DiscordRPC.ts diff --git a/entry.js b/entry.js index fa992b6..255db22 100644 --- a/entry.js +++ b/entry.js @@ -98,9 +98,7 @@ app.whenReady().then(() => { mainWindow.webContents.send('updateVP', { 'oldvp': args.oldvp }); }); - ipcMain.on('rpcstate', (event, args) => { - mainWindow.webContents.send('rpcstate', {}); - }); + ipcMain.on('rpc-toggle', () => mainWindow.webContents.send('rpc-toggle')); }); // Quit when all windows are closed, except on macOS. There, it's common diff --git a/src/ts/index.ts b/src/ts/index.ts index dfaefcf..b65a027 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -1,6 +1,5 @@ const path = require('path'); const fs = require('fs'); -const discordrpc = require("discord-rpc"); const { exec } = require('child_process'); const { ipcRenderer } = require('electron'); @@ -10,6 +9,7 @@ import { i18n } from './lib/i18n'; import { Genshinlib } from './lib/Genshinlib'; import { LauncherUI } from './lib/LauncherUI'; import { Tools } from './lib/Tools'; +import { DiscordRPC } from './lib/DiscordRPC'; if (!fs.existsSync(Genshinlib.prefixDir)) fs.mkdirSync(Genshinlib.prefixDir, { recursive: true }); @@ -25,61 +25,23 @@ $(() => { document.title = 'Genshin Impact Linux Launcher - ' + Genshinlib.version; LauncherUI.setState('game-launch-available'); + LauncherUI.updateBackground(); LauncherUI.updateSocial(); ipcRenderer.on('change-lang', (event: void, data: any) => { + LauncherUI.updateLang(data.lang); LauncherUI.updateBackground(); LauncherUI.updateSocial(); - // Needs data.lang in the arguments since the button doesn't get updated otherwise. - LauncherUI.updateLang(data.lang); }); - let rpc: any; - - // FIXME if (Genshinlib.getConfig().rpc) - { - rpc = new discordrpc.Client({ transport: 'ipc' }); - rpc.login({ clientId: '901534333360304168' }).catch(console.error); + DiscordRPC.init(); - rpc.on('ready', () => { - rpc.setActivity({ - details: 'Preparing to launch', - largeImageKey: 'launcher', - largeImageText: 'An Anime Game Launcher', - instance: false, - }); - }); - } - - // FIXME - ipcRenderer.on('rpcstate', (event: void, data: any) => { - if(!rpc) { - rpc = new discordrpc.Client({ transport: "ipc" }); - rpc.login({ clientId: '901534333360304168' }).catch(console.error); - - rpc.on('ready', () => { - rpc.setActivity({ - details: `Preparing to launch`, - largeImageKey: `launcher`, - largeImageText: `An Anime Game Launcher`, - instance: false, - }); - }); - - if (!Genshinlib.getConfig().rpc) - Genshinlib.updateConfig({ - rpc: true - }); - } else { - rpc.clearActivity(); - rpc.destroy(); - rpc = false; - Genshinlib.updateConfig({ - rpc: false - }); - } + ipcRenderer.on('rpc-toggle', () => { + DiscordRPC.isActive() ? + DiscordRPC.init() : + DiscordRPC.close(); }); // FIXME @@ -243,15 +205,12 @@ $(() => { console.log(`Wine executable: ${wineExeutable}`); - // FIXME - if (rpc) + if (DiscordRPC.isActive()) { - rpc.setActivity({ - details: `In-Game`, - largeImageKey: `game`, - largeImageText: `An Anime Game Launcher`, - startTimestamp: parseInt(new Date().setDate(new Date().getDate()).toString()), - instance: false, + DiscordRPC.setActivity({ + details: 'In-Game', + largeImageKey: 'game', + largeImageText: 'An Anime Game Launcher' }); } @@ -266,14 +225,12 @@ $(() => { ipcRenderer.invoke('show-window'); - // FIXME - if (rpc) + if (DiscordRPC.isActive()) { - rpc.setActivity({ - details: `Preparing to launch`, - largeImageKey: `launcher`, - largeImageText: `An Anime Game Launcher`, - instance: false, + DiscordRPC.setActivity({ + details: 'Preparing to launch', + largeImageKey: 'launcher', + largeImageText: 'An Anime Game Launcher' }); } diff --git a/src/ts/lib/DiscordRPC.ts b/src/ts/lib/DiscordRPC.ts new file mode 100644 index 0000000..c43296b --- /dev/null +++ b/src/ts/lib/DiscordRPC.ts @@ -0,0 +1,46 @@ +const discordRpc = require('discord-rpc'); + +export class DiscordRPC +{ + protected static readonly clientId = '901534333360304168'; + + protected static rpc: any = null; + + public static init () + { + this.rpc = new discordRpc.Client({ transport: 'ipc' }); + + this.rpc.login({ clientId: this.clientId }).catch(console.error); + + this.rpc.on('ready', () => { + this.rpc.setActivity({ + details: 'Preparing to launch', + largeImageKey: 'launcher', + largeImageText: 'An Anime Game Launcher', + instance: false + }); + }); + } + + public static setActivity (activity: any): void + { + this.rpc?.setActivity({ + startTimestamp: parseInt(new Date().setDate(new Date().getDate()).toString()), + instance: false, + ...activity + }); + } + + public static isActive (): boolean + { + return this.rpc !== null; + } + + public static close (): void + { + this.rpc?.clearActivity(); + this.rpc?.destroy(); + + this.rpc = null; + } +} diff --git a/src/ts/lib/Genshinlib.ts b/src/ts/lib/Genshinlib.ts index d5a22a4..134da6c 100644 --- a/src/ts/lib/Genshinlib.ts +++ b/src/ts/lib/Genshinlib.ts @@ -43,8 +43,8 @@ type Config = { folder: string, executable: string }, - rpc: boolean, - dxvk: string|null + dxvk: string|null, + rpc: boolean }; export class Genshinlib diff --git a/src/ts/settings.ts b/src/ts/settings.ts index 170aa5e..bac13f5 100644 --- a/src/ts/settings.ts +++ b/src/ts/settings.ts @@ -34,9 +34,15 @@ $(() => { $(`#language-list option[value="${Genshinlib.lang.launcher}"]`).prop('selected', true); if (Genshinlib.getConfig().rpc) - $(`#drpc`).prop('checked', true); + $('#drpc').prop('checked', true); - $('#drpc').on('change', () => ipcRenderer.send('rpcstate', {})); + $('#drpc').on('change', () => { + Genshinlib.updateConfig({ + rpc: $('#drpc').prop('checked') + }); + + ipcRenderer.send('rpc-toggle'); + }); $('#voice-list').on('change', (e) => { let activeVP = Genshinlib.lang.voice;