Skip to content

Commit

Permalink
fix buttons if there aren't 2 of them
Browse files Browse the repository at this point in the history
  • Loading branch information
p0358 committed Nov 2, 2023
1 parent af53eda commit 36b372a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 3 additions & 1 deletion examples/send-presence/send-presence.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ static void updateDiscordPresence()
{.label = "Test 2", .url = "https://discord.gg/fortnite"},
};

if (SendButtons)
if (SendButtons) {
discordPresence.buttons = buttons;
discordPresence.numButtons = sizeof(buttons) / sizeof(DiscordButton);
}

Discord_UpdatePresence(&discordPresence);
}
Expand Down
1 change: 1 addition & 0 deletions include/discord_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct DiscordRichPresence {
const char* joinSecret; /* max 128 bytes */
const char* spectateSecret; /* max 128 bytes */
int8_t instance;
uint8_t numButtons;
const DiscordButton* buttons;
} DiscordRichPresence;

Expand Down
22 changes: 10 additions & 12 deletions src/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,22 @@ size_t JsonWriteRichPresenceObj(char* dest,
writer.Key("instance");
writer.Bool(presence->instance != 0);

if (presence->buttons) {
if (presence->buttons && presence->numButtons > 0) {
const auto btns = presence->buttons;
WriteArray buttons(writer, "buttons");

if (btns[0].label[0]) {
WriteObject button0(writer);
WriteKey(writer, "url");
writer.String(btns[0].url);
WriteKey(writer, "label");
writer.String(btns[0].label);
}
for (uint8_t i = 0; i < presence->numButtons; i++) {

const DiscordButton& btn = presence->buttons[i];

if (!btn.label || !btn.label[0])
continue;

if (btns[1].label[0]) {
WriteObject button1(writer);
WriteObject button(writer);
WriteKey(writer, "url");
writer.String(btns[1].url);
writer.String(btn.url);
WriteKey(writer, "label");
writer.String(btns[1].label);
writer.String(btn.label);
}
}
}
Expand Down

0 comments on commit 36b372a

Please sign in to comment.