Added DiscordRPC class

This commit is contained in:
Observer KRypt0n_ 2021-10-24 09:59:43 +02:00
parent aee4be5ad6
commit 36470e1403
No known key found for this signature in database
GPG key ID: DC5D4EC1303465DA
5 changed files with 75 additions and 68 deletions

View file

@ -98,9 +98,7 @@ app.whenReady().then(() => {
mainWindow.webContents.send('updateVP', { 'oldvp': args.oldvp }); mainWindow.webContents.send('updateVP', { 'oldvp': args.oldvp });
}); });
ipcMain.on('rpcstate', (event, args) => { ipcMain.on('rpc-toggle', () => mainWindow.webContents.send('rpc-toggle'));
mainWindow.webContents.send('rpcstate', {});
});
}); });
// Quit when all windows are closed, except on macOS. There, it's common // Quit when all windows are closed, except on macOS. There, it's common

View file

@ -1,6 +1,5 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const discordrpc = require("discord-rpc");
const { exec } = require('child_process'); const { exec } = require('child_process');
const { ipcRenderer } = require('electron'); const { ipcRenderer } = require('electron');
@ -10,6 +9,7 @@ import { i18n } from './lib/i18n';
import { Genshinlib } from './lib/Genshinlib'; import { Genshinlib } from './lib/Genshinlib';
import { LauncherUI } from './lib/LauncherUI'; import { LauncherUI } from './lib/LauncherUI';
import { Tools } from './lib/Tools'; import { Tools } from './lib/Tools';
import { DiscordRPC } from './lib/DiscordRPC';
if (!fs.existsSync(Genshinlib.prefixDir)) if (!fs.existsSync(Genshinlib.prefixDir))
fs.mkdirSync(Genshinlib.prefixDir, { recursive: true }); fs.mkdirSync(Genshinlib.prefixDir, { recursive: true });
@ -25,61 +25,23 @@ $(() => {
document.title = 'Genshin Impact Linux Launcher - ' + Genshinlib.version; document.title = 'Genshin Impact Linux Launcher - ' + Genshinlib.version;
LauncherUI.setState('game-launch-available'); LauncherUI.setState('game-launch-available');
LauncherUI.updateBackground(); LauncherUI.updateBackground();
LauncherUI.updateSocial(); LauncherUI.updateSocial();
ipcRenderer.on('change-lang', (event: void, data: any) => { ipcRenderer.on('change-lang', (event: void, data: any) => {
LauncherUI.updateLang(data.lang);
LauncherUI.updateBackground(); LauncherUI.updateBackground();
LauncherUI.updateSocial(); 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) if (Genshinlib.getConfig().rpc)
{ DiscordRPC.init();
rpc = new discordrpc.Client({ transport: 'ipc' });
rpc.login({ clientId: '901534333360304168' }).catch(console.error);
rpc.on('ready', () => { ipcRenderer.on('rpc-toggle', () => {
rpc.setActivity({ DiscordRPC.isActive() ?
details: 'Preparing to launch', DiscordRPC.init() :
largeImageKey: 'launcher', DiscordRPC.close();
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
});
}
}); });
// FIXME // FIXME
@ -243,15 +205,12 @@ $(() => {
console.log(`Wine executable: ${wineExeutable}`); console.log(`Wine executable: ${wineExeutable}`);
// FIXME if (DiscordRPC.isActive())
if (rpc)
{ {
rpc.setActivity({ DiscordRPC.setActivity({
details: `In-Game`, details: 'In-Game',
largeImageKey: `game`, largeImageKey: 'game',
largeImageText: `An Anime Game Launcher`, largeImageText: 'An Anime Game Launcher'
startTimestamp: parseInt(new Date().setDate(new Date().getDate()).toString()),
instance: false,
}); });
} }
@ -266,14 +225,12 @@ $(() => {
ipcRenderer.invoke('show-window'); ipcRenderer.invoke('show-window');
// FIXME if (DiscordRPC.isActive())
if (rpc)
{ {
rpc.setActivity({ DiscordRPC.setActivity({
details: `Preparing to launch`, details: 'Preparing to launch',
largeImageKey: `launcher`, largeImageKey: 'launcher',
largeImageText: `An Anime Game Launcher`, largeImageText: 'An Anime Game Launcher'
instance: false,
}); });
} }

46
src/ts/lib/DiscordRPC.ts Normal file
View file

@ -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;
}
}

View file

@ -43,8 +43,8 @@ type Config = {
folder: string, folder: string,
executable: string executable: string
}, },
rpc: boolean, dxvk: string|null,
dxvk: string|null rpc: boolean
}; };
export class Genshinlib export class Genshinlib

View file

@ -34,9 +34,15 @@ $(() => {
$(`#language-list option[value="${Genshinlib.lang.launcher}"]`).prop('selected', true); $(`#language-list option[value="${Genshinlib.lang.launcher}"]`).prop('selected', true);
if (Genshinlib.getConfig().rpc) 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) => { $('#voice-list').on('change', (e) => {
let activeVP = Genshinlib.lang.voice; let activeVP = Genshinlib.lang.voice;