diff --git a/.gitignore b/.gitignore index 3e8acf0..6314bc6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ release/ -yarn.lock \ No newline at end of file +yarn.lock +pnpm-lock.yaml diff --git a/app.js b/app.js index 498aa81..4a76ad0 100644 --- a/app.js +++ b/app.js @@ -5,8 +5,11 @@ const osuUtil = require('./osuUtil'); const ezppUtil = require('./ezppUtil'); const config = require('./config'); const fs = require('fs'); +const rpc = require('./discordPresence'); +const windowName = require('get-window-by-name'); let tempOsuPath; +let osuWindowInfo; const run = () => { const gotTheLock = app.requestSingleInstanceLock() @@ -15,8 +18,43 @@ const run = () => { return; } + setInterval(() => { + osuWindowInfo = windowName.getWindowText("osu!.exe"); + const firstInstance = osuWindowInfo[0]; + if (firstInstance) { + if (firstInstance.processTitle && firstInstance.processTitle.length > 0) { + const windowTitle = firstInstance.processTitle; + let rpcOsuVersion = ""; + let currentMap = undefined; + if (!windowTitle.includes("-")) { + rpcOsuVersion = windowTitle; + rpc.updateState("Idle..."); + } else { + var string = windowTitle; + var components = string.split(' - '); + const splitArray = [components.shift(), components.join(' - ')]; + rpcOsuVersion = splitArray[0]; + currentMap = splitArray[1]; + rpc.updateState("Playing..."); + } + + rpc.updateStatus(currentMap, rpcOsuVersion); + } else { + rpc.updateState("Idle in Launcher..."); + rpc.updateStatus(undefined, undefined); + } + } else { + rpc.updateState("Idle in Launcher..."); + rpc.updateStatus(undefined, undefined); + } + console.log(osuWindowInfo); + }, 2000); + setupTitlebar(); + rpc.connect(); + + let mainWindow; app.whenReady().then(() => { @@ -46,6 +84,7 @@ const run = () => { await osuUtil.setConfigValue(osuConfig.path, "Username", ""); await osuUtil.setConfigValue(osuConfig.path, "Password", ""); } + rpc.updateState("Launching osu!..."); const result = await osuUtil.startOsuWithDevServer(tempOsuPath, "ez-pp.farm", async () => { await doUpdateCheck(mainWindow); }); diff --git a/discordPresence.js b/discordPresence.js new file mode 100644 index 0000000..cb42686 --- /dev/null +++ b/discordPresence.js @@ -0,0 +1,69 @@ +const appInfo = require('./appInfo.js'); +const DiscordAutoRPC = require("discord-auto-rpc"); +const { app } = require('electron'); +const DiscordRPC = require("discord-rpc").default; +const clientId = "1032772293220384808"; +let client = undefined; +let lastState = "Idle in Launcher..."; +let presenceEnabled = true; +let startDate = new Date(); +let lastActivity = { + details: " ", + state: lastState, + startTimestamp: startDate, + largeImageKey: "ezppfarm", + largeImageText: appInfo.appName + " " + appInfo.appVersion, + buttons: [ + { + label: "Download the Launcher", + url: "https://ez-pp.farm/download" + }, + { + label: "Join EZPPFarm", + url: "https://discord.com/invite/g8Bh7RaKPg" + } + ], + instance: false, +}; + +module.exports = { + connect: () => { + if (client === undefined) { + client = new DiscordAutoRPC.AutoClient({ transport: "ipc" }); + client.endlessLogin({ clientId: clientId }); + client.once("ready", () => { + setInterval(() => { + if (lastActivity !== undefined) + lastActivity.state = lastState; + client.setActivity(presenceEnabled ? lastActivity : undefined); + }, 2500); + }); + } + }, + enablePresence: () => presenceEnabled = true, + disablePresence: () => presenceEnabled = false, + updateStartDate: () => startDate = new Date(), + updateState: (state) => lastState = state, + updateStatus: (details, osuVersion) => { + lastActivity = { + details: details ? details : " ", + state: lastState, + startTimestamp: startDate, + smallImageKey: osuVersion ? "osu" : " ", + smallImageText: osuVersion ? osuVersion : " ", + largeImageKey: "ezppfarm", + largeImageText: appInfo.appName + " " + appInfo.appVersion, + buttons: [ + { + label: "Download the Launcher", + url: "https://ez-pp.farm/download" + }, + { + label: "Join EZPPFarm", + url: "https://discord.com/invite/g8Bh7RaKPg" + } + ], + instance: false, + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index 822d01f..197be65 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,9 @@ "dependencies": { "axios": "^0.27.2", "custom-electron-titlebar": "^4.1.1", + "discord-auto-rpc": "^1.0.17", + "discord-rpc": "^4.0.1", + "get-window-by-name": "^2.0.0", "jquery": "^3.6.0", "node-downloader-helper": "^2.1.4", "sweetalert2": "^11.5.2",