Show the Discord status under an NX2navi application instead of the old one

Discord takes the name above the presence from the application whose id the client sends, and the built-in id
belonged to the application the earlier framework registered for its own game, so the status read as that game.
The id and the art key now live in runtime/discord_app.h (NX2NAVI_DISCORD_APP_ID_DEFAULT and
NX2NAVI_DISCORD_IMAGE_DEFAULT), the one place to paste the NX2navi application's id. Until it is set there or in
nx2navi.ini, the presence stays off and logs once how to set it, and --probe-discord skips. The ini example and the
comments no longer claim the old id is registered as NX2navi, and the details-line example uses game 1.1.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-08 00:32:58 -05:00
parent 8702ddb356
commit 8f559ac945
4 changed files with 56 additions and 19 deletions

View File

@@ -239,11 +239,12 @@
# local IPC of a running Discord client. 1 = on; the Menu tab of the settings menu (F1) switches it too, and the # local IPC of a running Discord client. 1 = on; the Menu tab of the settings menu (F1) switches it too, and the
# change reaches the presence thread at once. # change reaches the presence thread at once.
# discord_rpc = 0 # discord_rpc = 0
# The application whose name and artwork the status shows. The default is the project's own application, # The Discord application whose name ("Playing <name>") and artwork the status shows: its Application ID from
# registered as "NX2navi" with the port's green ring icon as its art. Register your own at # discord.com/developers/applications (General Information), digits only. Default: the NX2navi application's id
# discord.com/developers (name and art are yours to choose) and put its id here. # built in from runtime/discord_app.h (NX2NAVI_DISCORD_APP_ID_DEFAULT); while that is empty and this is unset, the
# discord_app_id = 1567232599846293504 # presence stays off and the log says so. Register your own application to choose another name and art.
# The key of the application's uploaded art asset shown as the status's large image. # discord_app_id =
# The key of the application's uploaded art asset (Rich Presence > Art Assets) shown as the status's large image.
# discord_image = nx2navi_512x512 # discord_image = nx2navi_512x512
[audio] [audio]

View File

@@ -12,6 +12,7 @@
#include <string.h> #include <string.h>
#include "cfg.h" #include "cfg.h"
#include "discord_app.h"
#include "host.h" #include "host.h"
#include "platform/platform.h" #include "platform/platform.h"
@@ -157,10 +158,11 @@ static const CfgMeta metas[] = {
{"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE}, {"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE},
{"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", LIVE}, {"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", LIVE},
{"AMIIBO_DIR", "", NULL, LIVE}, {"AMIIBO_DIR", "", NULL, LIVE},
// Discord Rich Presence (discord_rpc.c): off by default, the Menu tab of the settings menu switches it. // Discord Rich Presence (discord_rpc.c): off by default, the Menu tab of the settings menu switches it. The
// application's id and art key default to discord_app.h's NX2NAVI_DISCORD_*_DEFAULT.
{"DISCORD_RPC", "0", "0|1", LIVE}, {"DISCORD_RPC", "0", "0|1", LIVE},
{"DISCORD_APP_ID", "1567232599846293504", NULL, LIVE}, {"DISCORD_APP_ID", NX2NAVI_DISCORD_APP_ID_DEFAULT, NULL, LIVE},
{"DISCORD_IMAGE", "nx2navi_512x512", NULL, LIVE}, {"DISCORD_IMAGE", NX2NAVI_DISCORD_IMAGE_DEFAULT, NULL, LIVE},
// Advanced: scheduling // Advanced: scheduling
{"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART}, {"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART},
{"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART}, {"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART},

15
runtime/discord_app.h Normal file
View File

@@ -0,0 +1,15 @@
// The Discord application NX2navi's Rich Presence (discord_rpc.c) speaks for: the one place its defaults live.
//
// Discord shows the *application's* name above the presence ("Playing <name>") and takes the large image from the
// art assets uploaded to that application; neither comes from the game. So the presence needs an application of its
// own, registered at discord.com/developers/applications under the name NX2navi, with its art uploaded under
// Rich Presence > Art Assets with the key below. Its Application ID (General Information) goes into
// NX2NAVI_DISCORD_APP_ID_DEFAULT; a player can still point at another application with discord_app_id and
// discord_image in nx2navi.ini ([discord]), which take precedence over these defaults.
//
// While the id is empty and nx2navi.ini names none, the presence stays off even with discord_rpc = 1 and logs why:
// an id of some other application would show that application's name instead of NX2navi.
#pragma once
#define NX2NAVI_DISCORD_APP_ID_DEFAULT "" // the NX2navi application's id: digits only, at most 20
#define NX2NAVI_DISCORD_IMAGE_DEFAULT "nx2navi_512x512" // its large-image art asset key

View File

@@ -1,10 +1,10 @@
// Discord Rich Presence: "Playing NX2navi" with the elapsed play time in the player's Discord status, through // Discord Rich Presence: "Playing NX2navi" with the elapsed play time in the player's Discord status, through
// the local IPC of a running Discord client (platform.h's plat_ipc_*). Nothing leaves the machine but the activity // the local IPC of a running Discord client (platform.h's plat_ipc_*). Nothing leaves the machine but the activity
// itself, and nothing is sent while NX2NAVI_DISCORD_RPC is off (the default: the status is information about the // itself, and nothing is sent while NX2NAVI_DISCORD_RPC is off (the default: the status is information about the
// player's session). NX2NAVI_DISCORD_APP_ID names the application whose name and artwork Discord shows — the default // player's session). NX2NAVI_DISCORD_APP_ID names the application whose name ("Playing <name>") and artwork Discord
// is the project's own application, registered as "NX2navi"; a player who wants their own name or // shows, NX2NAVI_DISCORD_IMAGE the key of its uploaded art asset; both default to discord_app.h, the one place the
// art registers an application at discord.com/developers and puts its id in nx2navi.ini — and NX2NAVI_DISCORD_IMAGE the // NX2navi application's id is written, and nx2navi.ini's discord_app_id and discord_image override them. With no
// key of its uploaded art asset. // application id at all the thread stays idle and logs that once: there is nothing to show the status under.
// //
// One host thread owns the connection and never holds a virtual core (kernel.c: host work runs without one). It // One host thread owns the connection and never holds a virtual core (kernel.c: host work runs without one). It
// re-reads the settings when cfg_version changes (a menu change reaches it within half a second), connects to the // re-reads the settings when cfg_version changes (a menu change reaches it within half a second), connects to the
@@ -54,15 +54,23 @@ static int valid_image_key(const char *s) {
} }
// The setting's value, or its default when the value does not fit the protocol (logged: the player sees why their // The setting's value, or its default when the value does not fit the protocol (logged: the player sees why their
// own app does not show). Called on the presence thread and by the probe, after cfg_version changed or once. // own app does not show), or "" when the default does not fit either (an application id discord_app.h leaves
// empty). Called on the presence thread and by the probe, after cfg_version changed or once.
static void read_setting(const char *name, char *out, size_t cap, int (*valid)(const char *)) { static void read_setting(const char *name, char *out, size_t cap, int (*valid)(const char *)) {
const char *v = cfg_value(name); const char *v = cfg_value(name), *def = cfg_meta(name)->def;
if (valid(v)) { if (valid(v)) {
snprintf(out, cap, "%s", v); snprintf(out, cap, "%s", v);
return; return;
} }
snprintf(out, cap, "%s", cfg_meta(name)->def); snprintf(out, cap, "%s", valid(def) ? def : "");
rt_log("discord: %s does not fit what the Rich Presence protocol takes; using %s", name + strlen("NX2NAVI_"), out); if (*v && strcmp(v, def))
rt_log("discord: %s does not fit what the Rich Presence protocol takes; using %s", name + strlen("NX2NAVI_"),
*out ? out : "none");
}
static void log_no_app_id(void) {
rt_log("discord: no application id: set discord_app_id in nx2navi.ini, or NX2NAVI_DISCORD_APP_ID_DEFAULT in "
"runtime/discord_app.h, to the id of the Discord application named NX2navi");
} }
static void le32(uint8_t *at, uint32_t v) { static void le32(uint8_t *at, uint32_t v) {
@@ -157,7 +165,7 @@ static uint32_t next_nonce(void) {
return (uint32_t)plat_inc32(&n); return (uint32_t)plat_inc32(&n);
} }
// The activity as one SET_ACTIVITY frame: the game update and the port version as the details line ("Game 1.4.3 · // The activity as one SET_ACTIVITY frame: the game update and the port version as the details line ("Game 1.1.1 ·
// Recomp 0.3.7": the port version without its +commit suffix, the art's hover text keeps the full one; the dot goes // Recomp 0.3.7": the port version without its +commit suffix, the art's hover text keeps the full one; the dot goes
// as a JSON escape), the process's start as the timestamp Discord counts the elapsed time from, and the application's // as a JSON escape), the process's start as the timestamp Discord counts the elapsed time from, and the application's
// art asset. 1 when the client answered (a slow answer that ran out of the 2 s read is fine: the next drain takes // art asset. 1 when the client answered (a slow answer that ran out of the 2 s read is fine: the next drain takes
@@ -189,7 +197,7 @@ static void discord_main(void *arg) {
read_setting("NX2NAVI_DISCORD_IMAGE", image, sizeof image, valid_image_key); read_setting("NX2NAVI_DISCORD_IMAGE", image, sizeof image, valid_image_key);
PlatIpc *ipc = NULL; PlatIpc *ipc = NULL;
uint64_t retry_at = 0, send_at = 0; uint64_t retry_at = 0, send_at = 0;
int said_no_client = 0; int said_no_client = 0, said_no_app = 0;
for (;;) { for (;;) {
unsigned v = cfg_version(); unsigned v = cfg_version();
if (v != seen) { // the settings changed (the menu writes them): pick the new ones up at once if (v != seen) { // the settings changed (the menu writes them): pick the new ones up at once
@@ -197,13 +205,19 @@ static void discord_main(void *arg) {
read_setting("NX2NAVI_DISCORD_APP_ID", app_id, sizeof app_id, valid_app_id); read_setting("NX2NAVI_DISCORD_APP_ID", app_id, sizeof app_id, valid_app_id);
read_setting("NX2NAVI_DISCORD_IMAGE", image, sizeof image, valid_image_key); read_setting("NX2NAVI_DISCORD_IMAGE", image, sizeof image, valid_image_key);
if (ipc) plat_ipc_close(ipc), ipc = NULL; // the new app id needs a new handshake; the image is resent if (ipc) plat_ipc_close(ipc), ipc = NULL; // the new app id needs a new handshake; the image is resent
said_no_client = 0; said_no_client = said_no_app = 0;
} }
if (!cfg_bool("NX2NAVI_DISCORD_RPC")) { if (!cfg_bool("NX2NAVI_DISCORD_RPC")) {
if (ipc) plat_ipc_close(ipc), ipc = NULL; if (ipc) plat_ipc_close(ipc), ipc = NULL;
plat_sleep_ms(500); plat_sleep_ms(500);
continue; continue;
} }
if (!app_id[0]) { // on, but no application to show it under: wait for a settings change
if (!said_no_app) log_no_app_id();
said_no_app = 1;
plat_sleep_ms(500);
continue;
}
if (!ipc && plat_now_ms() >= retry_at) { if (!ipc && plat_now_ms() >= retry_at) {
ipc = connect_client(app_id); ipc = connect_client(app_id);
if (!ipc) { if (!ipc) {
@@ -243,6 +257,11 @@ int probe_discord(void) {
char app_id[APP_ID_CAP], image[IMAGE_KEY_CAP]; char app_id[APP_ID_CAP], image[IMAGE_KEY_CAP];
read_setting("NX2NAVI_DISCORD_APP_ID", app_id, sizeof app_id, valid_app_id); read_setting("NX2NAVI_DISCORD_APP_ID", app_id, sizeof app_id, valid_app_id);
read_setting("NX2NAVI_DISCORD_IMAGE", image, sizeof image, valid_image_key); read_setting("NX2NAVI_DISCORD_IMAGE", image, sizeof image, valid_image_key);
if (!app_id[0]) {
log_no_app_id();
fprintf(stderr, "discord: probe skipped: no application id (runtime/discord_app.h or discord_app_id)\n");
return 0;
}
PlatIpc *p = connect_client(app_id); PlatIpc *p = connect_client(app_id);
if (!p) { if (!p) {
fprintf(stderr, "discord: probe skipped: no client on discord-ipc-0..9\n"); fprintf(stderr, "discord: probe skipped: no client on discord-ipc-0..9\n");