Add Discord rich presence

This would work, were it not for some freak stack corruption caused by
GCC not liking the Discord RPC library...
This commit is contained in:
oldmud0 2018-01-08 00:40:50 -06:00
parent ea01016d8c
commit 3797a1e9ef
6 changed files with 114 additions and 7 deletions

View File

@ -47,7 +47,8 @@ SOURCES += main.cpp\
aotextarea.cpp \
aolineedit.cpp \
aotextedit.cpp \
aoevidencedisplay.cpp
aoevidencedisplay.cpp \
discord_rich_presence.cpp
HEADERS += lobby.h \
aoimage.h \
@ -76,9 +77,16 @@ HEADERS += lobby.h \
aotextarea.h \
aolineedit.h \
aotextedit.h \
aoevidencedisplay.h
aoevidencedisplay.h \
discord_rich_presence.h
unix:LIBS += -L$$PWD -lbass
# 1. You need to get BASS and put the x86 bass DLL/headers in the project root folder
# AND the compilation output folder. If you want a static link, you'll probably
# need the .lib file too. MinGW-GCC is really finicky finding BASS, it seems.
# 2. You need to compile the Discord Rich Presence SDK separately and add the lib/headers
# in the same way as BASS. Discord RPC uses CMake, which does not play nicely with
# QMake, so this step must be manual.
unix:LIBS += -L$$PWD -lbass -ldiscord-rpc
win32:LIBS += "$$PWD/bass.dll"
android:LIBS += -L$$PWD\android\libs\armeabi-v7a\ -lbass

View File

@ -12,6 +12,7 @@
AOApplication::AOApplication(int &argc, char **argv) : QApplication(argc, argv)
{
net_manager = new NetworkManager(this);
discord = new AttorneyOnline::Discord();
QObject::connect(net_manager, SIGNAL(ms_connect_finished(bool, bool)),
SLOT(ms_connect_finished(bool, bool)));
}
@ -20,6 +21,7 @@ AOApplication::~AOApplication()
{
destruct_lobby();
destruct_courtroom();
delete discord;
}
void AOApplication::construct_lobby()
@ -38,6 +40,8 @@ void AOApplication::construct_lobby()
int y = (screenGeometry.height()-w_lobby->height()) / 2;
w_lobby->move(x, y);
discord->state_lobby();
w_lobby->show();
}

View File

@ -3,6 +3,7 @@
#include "aopacket.h"
#include "datatypes.h"
#include "discord_rich_presence.h"
#include <QApplication>
#include <QVector>
@ -23,6 +24,7 @@ public:
NetworkManager *net_manager;
Lobby *w_lobby;
Courtroom *w_courtroom;
AttorneyOnline::Discord *discord;
bool lobby_constructed = false;
bool courtroom_constructed = false;

58
discord_rich_presence.cpp Normal file
View File

@ -0,0 +1,58 @@
#include "discord_rich_presence.h"
#include <cstring>
#include <QDebug>
namespace AttorneyOnline {
Discord::Discord()
{
DiscordEventHandlers handlers;
std::memset(&handlers, 0, sizeof(handlers));
handlers = {};
handlers.ready = [] {
qInfo() << "Discord RPC ready";
};
/*
handlers.disconnected = [](int errorCode, const char* message) {
qInfo() << "Discord RPC disconnected! " << message;
};
handlers.errored = [](int errorCode, const char* message) {
qWarning() << "Discord RPC errored out! " << message;
};
*/
qInfo() << "Are things working out all right?";
Discord_Initialize(APPLICATION_ID, &handlers, 1, nullptr);
}
Discord::~Discord()
{
Discord_Shutdown();
}
void Discord::state_lobby()
{
DiscordRichPresence presence;
std::memset(&presence, 0, sizeof(presence));
presence.state = "In Lobby";
presence.details = "Idle";
presence.largeImageKey = "ao2-logo";
presence.largeImageText = "Objection!";
presence.instance = 1;
Discord_UpdatePresence(&presence);
}
void Discord::state_server(const char* name, const char* server_id)
{
DiscordRichPresence presence;
std::memset(&presence, 0, sizeof(presence));
presence.state = "In a Server";
presence.details = name;
presence.largeImageKey = "ao2-logo";
presence.largeImageText = "Objection!";
presence.instance = 1;
presence.matchSecret = server_id;
}
}

21
discord_rich_presence.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef DISCORD_RICH_PRESENCE_H
#define DISCORD_RICH_PRESENCE_H
#include <discord-rpc.h>
namespace AttorneyOnline {
class Discord
{
private:
const char* APPLICATION_ID = "399779271737868288";
public:
Discord();
~Discord();
void state_lobby();
void state_server(const char* name, const char* server_id);
};
}
#endif // DISCORD_RICH_PRESENCE_H

View File

@ -8,6 +8,7 @@
#include "debug_functions.h"
#include <QDebug>
#include <QCryptographicHash>
void AOApplication::ms_packet_received(AOPacket *p_packet)
{
@ -226,15 +227,24 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
QString window_title = "Attorney Online 2";
int selected_server = w_lobby->get_selected_server();
QString server_address = "", server_name = "";
if (w_lobby->public_servers_selected)
{
if (selected_server >= 0 && selected_server < server_list.size())
window_title += ": " + server_list.at(selected_server).name;
if (selected_server >= 0 && selected_server < server_list.size()) {
auto info = server_list.at(selected_server);
server_name = info.name;
server_address = info.ip + info.port;
window_title += ": " + server_name;
}
}
else
{
if (selected_server >= 0 && selected_server < favorite_list.size())
window_title += ": " + favorite_list.at(selected_server).name;
if (selected_server >= 0 && selected_server < favorite_list.size()) {
auto info = favorite_list.at(selected_server);
server_name = info.name;
server_address = info.ip + info.port;
window_title += ": " + server_name;
}
}
w_courtroom->set_window_title(window_title);
@ -251,6 +261,10 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
f_packet = new AOPacket("askchar2#%");
send_server_packet(f_packet);
QCryptographicHash hash(QCryptographicHash::Algorithm::Sha256);
hash.addData(server_address.toUtf8());
discord->state_server((const char*) server_name.toLocal8Bit().data(), (const char*) hash.result().toBase64());
}
else if (header == "CI")
{