diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 1f399b57..7b827dff 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -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 # change reaches the presence thread at once. # discord_rpc = 0 -# The application whose name and artwork the status shows. The default is the project's own application, -# registered as "NX2navi" with the port's green ring icon as its art. Register your own at -# discord.com/developers (name and art are yours to choose) and put its id here. -# discord_app_id = 1567232599846293504 -# The key of the application's uploaded art asset shown as the status's large image. +# The Discord application whose name ("Playing ") and artwork the status shows: its Application ID from +# discord.com/developers/applications (General Information), digits only. Default: the NX2navi application's id +# built in from runtime/discord_app.h (NX2NAVI_DISCORD_APP_ID_DEFAULT); while that is empty and this is unset, the +# presence stays off and the log says so. Register your own application to choose another name and art. +# 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 [audio] diff --git a/runtime/cfg.c b/runtime/cfg.c index 115b66db..ada22e63 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -12,6 +12,7 @@ #include #include "cfg.h" +#include "discord_app.h" #include "host.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}, {"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", 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_APP_ID", "1567232599846293504", NULL, LIVE}, - {"DISCORD_IMAGE", "nx2navi_512x512", NULL, LIVE}, + {"DISCORD_APP_ID", NX2NAVI_DISCORD_APP_ID_DEFAULT, NULL, LIVE}, + {"DISCORD_IMAGE", NX2NAVI_DISCORD_IMAGE_DEFAULT, NULL, LIVE}, // Advanced: scheduling {"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}, diff --git a/runtime/discord_app.h b/runtime/discord_app.h new file mode 100644 index 00000000..4a277677 --- /dev/null +++ b/runtime/discord_app.h @@ -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 ") 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 diff --git a/runtime/discord_rpc.c b/runtime/discord_rpc.c index 4e9ad6b7..43268099 100644 --- a/runtime/discord_rpc.c +++ b/runtime/discord_rpc.c @@ -1,10 +1,10 @@ // 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 // 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 -// is the project's own application, registered as "NX2navi"; a player who wants their own name or -// art registers an application at discord.com/developers and puts its id in nx2navi.ini — and NX2NAVI_DISCORD_IMAGE the -// key of its uploaded art asset. +// player's session). NX2NAVI_DISCORD_APP_ID names the application whose name ("Playing ") and artwork Discord +// shows, NX2NAVI_DISCORD_IMAGE the key of its uploaded art asset; both default to discord_app.h, the one place the +// NX2navi application's id is written, and nx2navi.ini's discord_app_id and discord_image override them. With no +// 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 // 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 -// 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 *)) { - const char *v = cfg_value(name); + const char *v = cfg_value(name), *def = cfg_meta(name)->def; if (valid(v)) { snprintf(out, cap, "%s", v); return; } - snprintf(out, cap, "%s", cfg_meta(name)->def); - rt_log("discord: %s does not fit what the Rich Presence protocol takes; using %s", name + strlen("NX2NAVI_"), out); + snprintf(out, cap, "%s", valid(def) ? def : ""); + 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) { @@ -157,7 +165,7 @@ static uint32_t next_nonce(void) { 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 // 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 @@ -189,7 +197,7 @@ static void discord_main(void *arg) { read_setting("NX2NAVI_DISCORD_IMAGE", image, sizeof image, valid_image_key); PlatIpc *ipc = NULL; uint64_t retry_at = 0, send_at = 0; - int said_no_client = 0; + int said_no_client = 0, said_no_app = 0; for (;;) { unsigned v = cfg_version(); 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_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 - said_no_client = 0; + said_no_client = said_no_app = 0; } if (!cfg_bool("NX2NAVI_DISCORD_RPC")) { if (ipc) plat_ipc_close(ipc), ipc = NULL; plat_sleep_ms(500); 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) { ipc = connect_client(app_id); if (!ipc) { @@ -243,6 +257,11 @@ int probe_discord(void) { 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_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); if (!p) { fprintf(stderr, "discord: probe skipped: no client on discord-ipc-0..9\n");