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:
		
							parent
							
								
									ea01016d8c
								
							
						
					
					
						commit
						3797a1e9ef
					
				@ -47,7 +47,8 @@ SOURCES += main.cpp\
 | 
				
			|||||||
    aotextarea.cpp \
 | 
					    aotextarea.cpp \
 | 
				
			||||||
    aolineedit.cpp \
 | 
					    aolineedit.cpp \
 | 
				
			||||||
    aotextedit.cpp \
 | 
					    aotextedit.cpp \
 | 
				
			||||||
    aoevidencedisplay.cpp
 | 
					    aoevidencedisplay.cpp \
 | 
				
			||||||
 | 
					    discord_rich_presence.cpp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
HEADERS  += lobby.h \
 | 
					HEADERS  += lobby.h \
 | 
				
			||||||
    aoimage.h \
 | 
					    aoimage.h \
 | 
				
			||||||
@ -76,9 +77,16 @@ HEADERS  += lobby.h \
 | 
				
			|||||||
    aotextarea.h \
 | 
					    aotextarea.h \
 | 
				
			||||||
    aolineedit.h \
 | 
					    aolineedit.h \
 | 
				
			||||||
    aotextedit.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"
 | 
					win32:LIBS += "$$PWD/bass.dll"
 | 
				
			||||||
android:LIBS += -L$$PWD\android\libs\armeabi-v7a\ -lbass
 | 
					android:LIBS += -L$$PWD\android\libs\armeabi-v7a\ -lbass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -12,6 +12,7 @@
 | 
				
			|||||||
AOApplication::AOApplication(int &argc, char **argv) : QApplication(argc, argv)
 | 
					AOApplication::AOApplication(int &argc, char **argv) : QApplication(argc, argv)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  net_manager = new NetworkManager(this);
 | 
					  net_manager = new NetworkManager(this);
 | 
				
			||||||
 | 
					  discord = new AttorneyOnline::Discord();
 | 
				
			||||||
  QObject::connect(net_manager, SIGNAL(ms_connect_finished(bool, bool)),
 | 
					  QObject::connect(net_manager, SIGNAL(ms_connect_finished(bool, bool)),
 | 
				
			||||||
                   SLOT(ms_connect_finished(bool, bool)));
 | 
					                   SLOT(ms_connect_finished(bool, bool)));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -20,6 +21,7 @@ AOApplication::~AOApplication()
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  destruct_lobby();
 | 
					  destruct_lobby();
 | 
				
			||||||
  destruct_courtroom();
 | 
					  destruct_courtroom();
 | 
				
			||||||
 | 
					  delete discord;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void AOApplication::construct_lobby()
 | 
					void AOApplication::construct_lobby()
 | 
				
			||||||
@ -38,6 +40,8 @@ void AOApplication::construct_lobby()
 | 
				
			|||||||
  int y = (screenGeometry.height()-w_lobby->height()) / 2;
 | 
					  int y = (screenGeometry.height()-w_lobby->height()) / 2;
 | 
				
			||||||
  w_lobby->move(x, y);
 | 
					  w_lobby->move(x, y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  discord->state_lobby();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  w_lobby->show();
 | 
					  w_lobby->show();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -3,6 +3,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "aopacket.h"
 | 
					#include "aopacket.h"
 | 
				
			||||||
#include "datatypes.h"
 | 
					#include "datatypes.h"
 | 
				
			||||||
 | 
					#include "discord_rich_presence.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <QApplication>
 | 
					#include <QApplication>
 | 
				
			||||||
#include <QVector>
 | 
					#include <QVector>
 | 
				
			||||||
@ -23,6 +24,7 @@ public:
 | 
				
			|||||||
  NetworkManager *net_manager;
 | 
					  NetworkManager *net_manager;
 | 
				
			||||||
  Lobby *w_lobby;
 | 
					  Lobby *w_lobby;
 | 
				
			||||||
  Courtroom *w_courtroom;
 | 
					  Courtroom *w_courtroom;
 | 
				
			||||||
 | 
					  AttorneyOnline::Discord *discord;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool lobby_constructed = false;
 | 
					  bool lobby_constructed = false;
 | 
				
			||||||
  bool courtroom_constructed = false;
 | 
					  bool courtroom_constructed = false;
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										58
									
								
								discord_rich_presence.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								discord_rich_presence.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										21
									
								
								discord_rich_presence.h
									
									
									
									
									
										Normal 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
 | 
				
			||||||
@ -8,6 +8,7 @@
 | 
				
			|||||||
#include "debug_functions.h"
 | 
					#include "debug_functions.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <QDebug>
 | 
					#include <QDebug>
 | 
				
			||||||
 | 
					#include <QCryptographicHash>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void AOApplication::ms_packet_received(AOPacket *p_packet)
 | 
					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";
 | 
					    QString window_title = "Attorney Online 2";
 | 
				
			||||||
    int selected_server = w_lobby->get_selected_server();
 | 
					    int selected_server = w_lobby->get_selected_server();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QString server_address = "", server_name = "";
 | 
				
			||||||
    if (w_lobby->public_servers_selected)
 | 
					    if (w_lobby->public_servers_selected)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      if (selected_server >= 0 && selected_server < server_list.size())
 | 
					      if (selected_server >= 0 && selected_server < server_list.size()) {
 | 
				
			||||||
        window_title += ": " + server_list.at(selected_server).name;
 | 
					        auto info = server_list.at(selected_server);
 | 
				
			||||||
 | 
					        server_name = info.name;
 | 
				
			||||||
 | 
					        server_address = info.ip + info.port;
 | 
				
			||||||
 | 
					        window_title += ": " + server_name;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      if (selected_server >= 0 && selected_server < favorite_list.size())
 | 
					      if (selected_server >= 0 && selected_server < favorite_list.size()) {
 | 
				
			||||||
        window_title += ": " + favorite_list.at(selected_server).name;
 | 
					        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);
 | 
					    w_courtroom->set_window_title(window_title);
 | 
				
			||||||
@ -251,6 +261,10 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
 | 
				
			|||||||
      f_packet = new AOPacket("askchar2#%");
 | 
					      f_packet = new AOPacket("askchar2#%");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    send_server_packet(f_packet);
 | 
					    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")
 | 
					  else if (header == "CI")
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user