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

View file

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

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,
executable: string
},
rpc: boolean,
dxvk: string|null
dxvk: string|null,
rpc: boolean
};
export class Genshinlib

View file

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