From d56d4875e0ebe572d65455511e15d3c2d965fa19 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 11 Mar 2024 15:38:20 +0100 Subject: [PATCH 1/7] add dynamic beatmap image to presence --- electron/imageUtil.js | 15 +++++++++++++++ electron/richPresence.js | 3 ++- main.js | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 electron/imageUtil.js diff --git a/electron/imageUtil.js b/electron/imageUtil.js new file mode 100644 index 0000000..065f45e --- /dev/null +++ b/electron/imageUtil.js @@ -0,0 +1,15 @@ +async function checkImageExists(url) { + try { + const response = await fetch(url, { method: "HEAD" }); + if (!response.ok) { + return false; + } + const contentType = response.headers.get("content-type"); + if (!contentType) return false; + return contentType.startsWith("image/"); + } catch (error) { + return false; + } +} + +module.exports = { checkImageExists }; diff --git a/electron/richPresence.js b/electron/richPresence.js index 4489d34..23579f8 100644 --- a/electron/richPresence.js +++ b/electron/richPresence.js @@ -47,9 +47,10 @@ module.exports = { richPresence = null; } }, - updateStatus: ({ state, details }) => { + updateStatus: ({ state, details, largeImageKey }) => { currentStatus.state = state ?? " "; currentStatus.details = details ?? " "; + currentStatus.largeImageKey = largeImageKey ?? "ezppfarm"; }, updateVersion: (osuVersion) => { currentStatus.smallImageKey = osuVersion ? "osu" : " "; diff --git a/main.js b/main.js index aa8d919..4d91f9a 100644 --- a/main.js +++ b/main.js @@ -46,6 +46,7 @@ const { getHwId } = require("./electron/hwidUtil"); const { appName, appVersion } = require("./electron/appInfo"); const { updateAvailable, releasesUrl } = require("./electron/updateCheck"); const fkill = require("fkill"); +const { checkImageExists } = require("./electron/imageUtil"); // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -96,10 +97,22 @@ function startOsuStatus() { if (!("player_status" in currentStatus)) return; if (!("status" in currentStatus.player_status)) return; + let largeImageKey = "ezppfarm"; let details = "Idle..."; let infoText = currentStatus.player_status.status.info_text.length > 0 ? currentStatus.player_status.status.info_text : " "; + if ("beatmap" in currentStatus.player_status.status) { + const setId = currentStatus.player_status.status.beatmap.set_id; + if ( + checkImageExists( + `https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`, + ) + ) { + largeImageKey = + `https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`; + } + } switch (currentStatus.player_status.status.action) { case 1: @@ -144,6 +157,7 @@ function startOsuStatus() { richPresence.updateStatus({ details, state: infoText, + largeImageKey, }); richPresence.update(); From c4d98628608c2d507f90a4090cd54b13f13765db Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 11 Mar 2024 15:42:08 +0100 Subject: [PATCH 2/7] assign the cover url to a const --- main.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index 4d91f9a..8a23942 100644 --- a/main.js +++ b/main.js @@ -104,13 +104,12 @@ function startOsuStatus() { : " "; if ("beatmap" in currentStatus.player_status.status) { const setId = currentStatus.player_status.status.beatmap.set_id; + const coverImage = + `https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`; if ( - checkImageExists( - `https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`, - ) + checkImageExists(coverImage) ) { - largeImageKey = - `https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`; + largeImageKey = coverImage; } } From 22815e74b6f8f89d70e9d6d2b3d377a00554d5b2 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 11 Mar 2024 15:46:07 +0100 Subject: [PATCH 3/7] check if beatmap is not null --- main.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index 8a23942..8dd3c3a 100644 --- a/main.js +++ b/main.js @@ -102,7 +102,10 @@ function startOsuStatus() { let infoText = currentStatus.player_status.status.info_text.length > 0 ? currentStatus.player_status.status.info_text : " "; - if ("beatmap" in currentStatus.player_status.status) { + if ( + "beatmap" in currentStatus.player_status.status && + currentStatus.player_status.status.beatmap !== null + ) { const setId = currentStatus.player_status.status.beatmap.set_id; const coverImage = `https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`; From 8d2024aa0af5a83cd115c51631c0640eb3c53a79 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 11 Mar 2024 17:34:36 +0100 Subject: [PATCH 4/7] check if rpc is connected --- electron/richPresence.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/electron/richPresence.js b/electron/richPresence.js index 4489d34..9529733 100644 --- a/electron/richPresence.js +++ b/electron/richPresence.js @@ -2,7 +2,10 @@ const DiscordRPC = require("discord-auto-rpc"); const { appName, appVersion } = require("./appInfo.js"); const clientId = "1032772293220384808"; + +/** @type {DiscordRPC.AutoClient} */ let richPresence; + let intervalId; let currentStatus = { @@ -32,6 +35,7 @@ module.exports = { richPresence = new DiscordRPC.AutoClient({ transport: "ipc" }); richPresence.endlessLogin({ clientId }); richPresence.once("ready", () => { + console.log("connected presence with user " + richPresence.user.username); richPresence.setActivity(currentStatus); intervalId = setInterval(() => { richPresence.setActivity(currentStatus); @@ -56,7 +60,7 @@ module.exports = { currentStatus.smallImageText = osuVersion ? `osu! ${osuVersion}` : " "; }, update: () => { - if (richPresence) { + if (richPresence && richPresence.user) { richPresence.setActivity(currentStatus); } }, From 90717ed9600e253add46114c90be43de8bf6e372 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 11 Mar 2024 18:06:24 +0100 Subject: [PATCH 5/7] display user as small image, fix dynamic beatmap images --- electron/richPresence.js | 6 ++--- main.js | 57 +++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/electron/richPresence.js b/electron/richPresence.js index f0b39d0..0132998 100644 --- a/electron/richPresence.js +++ b/electron/richPresence.js @@ -56,9 +56,9 @@ module.exports = { currentStatus.details = details ?? " "; currentStatus.largeImageKey = largeImageKey ?? "ezppfarm"; }, - updateVersion: (osuVersion) => { - currentStatus.smallImageKey = osuVersion ? "osu" : " "; - currentStatus.smallImageText = osuVersion ? `osu! ${osuVersion}` : " "; + updateUser: ({ username, id }) => { + currentStatus.smallImageKey = id ? `https://a.ez-pp.farm/${id}` : " "; + currentStatus.smallImageText = username ? username : " "; }, update: () => { if (richPresence && richPresence.user) { diff --git a/main.js b/main.js index 8dd3c3a..a467ec2 100644 --- a/main.js +++ b/main.js @@ -73,6 +73,25 @@ function startOsuStatus() { if (firstInstance) { if (!osuLoaded) { osuLoaded = true; + + try { + const currentUserInfo = await fetch(`https://api.ez-pp.farm/get_player_info?name=${currentUser.username}&scope=info`); + const currentUserInfoJson = await currentUserInfo.json(); + if ("player" in currentUserInfoJson && currentUserInfoJson.player != null) { + if ("info" in currentUserInfoJson.player && currentUserInfoJson.player.info != null) { + const id = currentUserInfoJson.player.info.id; + const username = currentUserInfoJson.player.info.name; + richPresence.updateUser({ + id, + username, + }); + richPresence.update(); + } + } + } catch { + + } + setTimeout(() => { if (patch) { const patcherExecuteable = path.join( @@ -107,12 +126,14 @@ function startOsuStatus() { currentStatus.player_status.status.beatmap !== null ) { const setId = currentStatus.player_status.status.beatmap.set_id; - const coverImage = - `https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`; - if ( - checkImageExists(coverImage) - ) { - largeImageKey = coverImage; + if (setId) { + const coverImage = + `https://assets.ppy.sh/beatmaps/${setId}/covers/list@2x.jpg`; + if ( + checkImageExists(coverImage) + ) { + largeImageKey = coverImage; + } } } @@ -120,6 +141,7 @@ function startOsuStatus() { case 1: details = "AFK..."; infoText = " "; + largeImageKey = "ezppfarm"; break; case 2: details = "Playing..."; @@ -133,6 +155,7 @@ function startOsuStatus() { case 5: details = "Multiplayer: Selecting a Beatmap..."; infoText = " "; + largeImageKey = "ezppfarm"; break; case 6: details = "Watching..."; @@ -142,10 +165,12 @@ function startOsuStatus() { break; case 9: details = "Submitting..."; + largeImageKey = "ezppfarm"; break; case 11: details = "Multiplayer: Idle..."; infoText = " "; + largeImageKey = "ezppfarm"; break; case 12: details = "Multiplayer: Playing..."; @@ -153,6 +178,7 @@ function startOsuStatus() { case 13: details = "Browsing osu!direct..."; infoText = " "; + largeImageKey = "ezppfarm"; break; } @@ -402,9 +428,8 @@ function registerIPCPipes() { progress: Math.ceil(data.progress), }); mainWindow.webContents.send("ezpplauncher:launchstatus", { - status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${ - formatBytes(data.total) - })...`, + status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${formatBytes(data.total) + })...`, }); }); await uiDownloader.startDownload(); @@ -433,9 +458,8 @@ function registerIPCPipes() { progress: Math.ceil(data.progress), }); mainWindow.webContents.send("ezpplauncher:launchstatus", { - status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${ - formatBytes(data.total) - })...`, + status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${formatBytes(data.total) + })...`, }); }); await updateDownloader.startDownload(); @@ -480,9 +504,8 @@ function registerIPCPipes() { progress: Math.ceil(data.progress), }); mainWindow.webContents.send("ezpplauncher:launchstatus", { - status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${ - formatBytes(data.total) - })...`, + status: `Downloading ${data.fileName}(${formatBytes(data.loaded)}/${formatBytes(data.total) + })...`, }); }); await patcherDownloader.startDownload(); @@ -554,11 +577,9 @@ function registerIPCPipes() { }); } } - } catch {} + } catch { } const userConfig = getUserConfig(osuPath); - richPresence.updateVersion(await userConfig.get("LastVersion")); - richPresence.update(); if (richPresence.hasPresence) { await userConfig.set("DiscordRichPresence", "0"); } From 513692c2d52496f79dad9cb50187d7ad23e3c151 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 11 Mar 2024 18:37:33 +0100 Subject: [PATCH 6/7] reset user on exit --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index a467ec2..c6736b3 100644 --- a/main.js +++ b/main.js @@ -600,7 +600,7 @@ function registerIPCPipes() { mainWindow.show(); mainWindow.focus(); stopOsuStatus(); - richPresence.updateVersion(); + richPresence.updateUser(); richPresence.updateStatus({ state: "Idle in Launcher...", details: undefined, From c17cbc48d88490f3f0a73e08694446c9c0d07cfa Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 11 Mar 2024 18:46:04 +0100 Subject: [PATCH 7/7] fix user reset --- electron/richPresence.js | 2 +- main.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/electron/richPresence.js b/electron/richPresence.js index 0132998..5f55283 100644 --- a/electron/richPresence.js +++ b/electron/richPresence.js @@ -58,7 +58,7 @@ module.exports = { }, updateUser: ({ username, id }) => { currentStatus.smallImageKey = id ? `https://a.ez-pp.farm/${id}` : " "; - currentStatus.smallImageText = username ? username : " "; + currentStatus.smallImageText = username ?? " "; }, update: () => { if (richPresence && richPresence.user) { diff --git a/main.js b/main.js index c6736b3..ead9109 100644 --- a/main.js +++ b/main.js @@ -600,7 +600,10 @@ function registerIPCPipes() { mainWindow.show(); mainWindow.focus(); stopOsuStatus(); - richPresence.updateUser(); + richPresence.updateUser({ + username: " ", + id: undefined + }); richPresence.updateStatus({ state: "Idle in Launcher...", details: undefined,