Skip to content

Commit

Permalink
Add initial Discord Game SDK Integration (#1031)
Browse files Browse the repository at this point in the history
This is just Rich Presence & Join features for now.

* Add Discord Game SDK library

* Add shared part of discord game sdk integration

* Add client part of discord game sdk integration

* Add server part of discord game sdk integration

* Resolve conflicts created by 4244ec5

* Make changes/Correct mistakes according to reviews

* Move DISCORD_CLIENT_ID to CDiscordManager.cpp

* discord: change SetLargeImage asset key

* discord: change neutral activity message

* discord: add start timestamp to main menu

* discord: use time(&time_t)

* addendum to 1ef02be

* Minor style improvements

* Run format document/selection on changed files

* Remove unnecessary null check

* Add discord game sdk library

Also allow it through .gitignore

* Avoid code duplication and fix grammar when setting discord state

* Move UpdateDiscordState to CClientGame and avoid code duplication

* Addendum 009190e

* Disallow space character on setDiscordJoinParams
This avoids messup on argument parser

* update vendor

* Join secret is no longer used on argument parser

* Fix a minor mistake

* Use luaL_error

Also disallow to use party max than the max players value

* update out of date dll

* Add discord download script

* Link correct library

Also do not download discord again when it is already up to date

* Add discord dll to installer

* Update DISCORD_BASEURL

Co-authored-by: Qais Patankar <qaisjp@gmail.com>
Co-authored-by: Marek Kulik <marek.kulik@hotmail.com>
  • Loading branch information
3 people committed Mar 25, 2020
1 parent 2d68519 commit 850c76d
Show file tree
Hide file tree
Showing 63 changed files with 2,449 additions and 1,206 deletions.
747 changes: 374 additions & 373 deletions Client/core/CClientVariables.cpp

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion Client/core/CConnectManager.cpp
Expand Up @@ -46,7 +46,7 @@ CConnectManager::~CConnectManager()
g_pConnectManager = NULL; g_pConnectManager = NULL;
} }


bool CConnectManager::Connect(const char* szHost, unsigned short usPort, const char* szNick, const char* szPassword, bool bNotifyServerBrowser) bool CConnectManager::Connect(const char* szHost, unsigned short usPort, const char* szNick, const char* szPassword, bool bNotifyServerBrowser, const char* szSecret)
{ {
assert(szHost); assert(szHost);
assert(szNick); assert(szNick);
Expand Down Expand Up @@ -100,6 +100,11 @@ bool CConnectManager::Connect(const char* szHost, unsigned short usPort, const c
m_usPort = usPort; m_usPort = usPort;
m_bSave = true; m_bSave = true;


if (szSecret)
m_strDiscordSecretJoin = szSecret;
else
m_strDiscordSecretJoin.clear();

m_strLastHost = m_strHost; m_strLastHost = m_strHost;
m_usLastPort = m_usPort; m_usLastPort = m_usPort;
m_strLastPassword = m_strPassword; m_strLastPassword = m_strPassword;
Expand Down Expand Up @@ -489,3 +494,10 @@ void CConnectManager::OpenServerFirewall(in_addr Address, ushort usHttpPort, boo
g_pCore->GetNetwork()->GetHTTPDownloadManager(EDownloadMode::CONNECT_TCP_SEND)->QueueFile(strDummyUrl, NULL, NULL, NULL, options); g_pCore->GetNetwork()->GetHTTPDownloadManager(EDownloadMode::CONNECT_TCP_SEND)->QueueFile(strDummyUrl, NULL, NULL, NULL, options);
} }
} }

SString CConnectManager::GetJoinSecret()
{
SString dummy = m_strDiscordSecretJoin;
m_strDiscordSecretJoin.clear();
return dummy;
}
5 changes: 4 additions & 1 deletion Client/core/CConnectManager.h
Expand Up @@ -21,7 +21,7 @@ class CConnectManager
CConnectManager(); CConnectManager();
~CConnectManager(); ~CConnectManager();


bool Connect(const char* szHost, unsigned short usPort, const char* szNick, const char* szPassword, bool bNotifyServerBrowser = false); bool Connect(const char* szHost, unsigned short usPort, const char* szNick, const char* szPassword, bool bNotifyServerBrowser = false, const char* szSecret = nullptr);
bool Reconnect(const char* szHost, unsigned short usPort, const char* szPassword, bool bSave = true); bool Reconnect(const char* szHost, unsigned short usPort, const char* szPassword, bool bSave = true);


bool Abort(); bool Abort();
Expand All @@ -34,6 +34,8 @@ class CConnectManager


static bool StaticProcessPacket(unsigned char ucPacketID, class NetBitStreamInterface& bitStream); static bool StaticProcessPacket(unsigned char ucPacketID, class NetBitStreamInterface& bitStream);


SString GetJoinSecret();

std::string m_strLastHost; std::string m_strLastHost;
unsigned short m_usLastPort; unsigned short m_usLastPort;
std::string m_strLastPassword; std::string m_strLastPassword;
Expand All @@ -52,6 +54,7 @@ class CConnectManager
bool m_bSave; bool m_bSave;
time_t m_tConnectStarted; time_t m_tConnectStarted;
bool m_bHasTriedSecondConnect; bool m_bHasTriedSecondConnect;
SString m_strDiscordSecretJoin;


GUI_CALLBACK* m_pOnCancelClick; GUI_CALLBACK* m_pOnCancelClick;


Expand Down
29 changes: 28 additions & 1 deletion Client/core/CCore.cpp
Expand Up @@ -21,6 +21,7 @@
#include "CModelCacheManager.h" #include "CModelCacheManager.h"
#include "detours/include/detours.h" #include "detours/include/detours.h"
#include <ServerBrowser/CServerCache.h> #include <ServerBrowser/CServerCache.h>
#include "CDiscordManager.h"


using SharedUtil::CalcMTASAPath; using SharedUtil::CalcMTASAPath;
using namespace std; using namespace std;
Expand All @@ -34,7 +35,7 @@ SString g_strJingleBells;
template <> template <>
CCore* CSingleton<CCore>::m_pSingleton = NULL; CCore* CSingleton<CCore>::m_pSingleton = NULL;


CCore::CCore() CCore::CCore() : m_DiscordManager(new CDiscordManager())
{ {
// Initialize the global pointer // Initialize the global pointer
g_pCore = this; g_pCore = this;
Expand Down Expand Up @@ -565,6 +566,9 @@ void CCore::SetConnected(bool bConnected)
{ {
m_pLocalGUI->GetMainMenu()->SetIsIngame(bConnected); m_pLocalGUI->GetMainMenu()->SetIsIngame(bConnected);
UpdateIsWindowMinimized(); // Force update of stuff UpdateIsWindowMinimized(); // Force update of stuff

if (bConnected) m_DiscordManager->RegisterPlay(true);
else ResetDiscordRichPresence();
} }


bool CCore::IsConnected() bool CCore::IsConnected()
Expand Down Expand Up @@ -779,6 +783,7 @@ void CCore::ApplyHooks2()
CCore::GetSingleton().CreateMultiplayer(); CCore::GetSingleton().CreateMultiplayer();
CCore::GetSingleton().CreateXML(); CCore::GetSingleton().CreateXML();
CCore::GetSingleton().CreateGUI(); CCore::GetSingleton().CreateGUI();
CCore::GetSingleton().ResetDiscordRichPresence();
} }
} }
} }
Expand Down Expand Up @@ -1980,6 +1985,28 @@ uint CCore::GetMaxStreamingMemory()
return m_fMaxStreamingMemory; return m_fMaxStreamingMemory;
} }


//
// ResetDiscordRichPresence
//
void CCore::ResetDiscordRichPresence()
{
time_t currentTime;
time(&currentTime);

// Set default parameters
SDiscordActivity activity;
activity.m_details = "In Main Menu";
activity.m_startTimestamp = currentTime;

m_DiscordManager->UpdateActivity(activity, [](EDiscordRes res) {
if (res == DiscordRes_Ok)
WriteDebugEvent("[DISCORD]: Rich presence default parameters reset.");
else
WriteErrorEvent("[DISCORD]: Unable to reset rich presence default parameters.");
});
m_DiscordManager->RegisterPlay(false);
}

// //
// OnCrashAverted // OnCrashAverted
// //
Expand Down
43 changes: 24 additions & 19 deletions Client/core/CCore.h
Expand Up @@ -83,25 +83,26 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
~CCore(); ~CCore();


// Subsystems (query) // Subsystems (query)
eCoreVersion GetVersion(); eCoreVersion GetVersion();
CConsoleInterface* GetConsole(); CConsoleInterface* GetConsole();
CCommandsInterface* GetCommands(); CCommandsInterface* GetCommands();
CConnectManager* GetConnectManager() { return m_pConnectManager; }; CConnectManager* GetConnectManager() { return m_pConnectManager; };
CGame* GetGame(); CGame* GetGame();
CGUI* GetGUI(); CGUI* GetGUI();
CGraphicsInterface* GetGraphics(); CGraphicsInterface* GetGraphics();
CModManagerInterface* GetModManager(); CModManagerInterface* GetModManager();
CMultiplayer* GetMultiplayer(); CMultiplayer* GetMultiplayer();
CNet* GetNetwork(); CNet* GetNetwork();
CXML* GetXML() { return m_pXML; }; CXML* GetXML() { return m_pXML; };
CXMLNode* GetConfig(); CXMLNode* GetConfig();
CClientVariables* GetCVars() { return &m_ClientVariables; }; CClientVariables* GetCVars() { return &m_ClientVariables; };
CKeyBindsInterface* GetKeyBinds(); CKeyBindsInterface* GetKeyBinds();
CMouseControl* GetMouseControl() { return m_pMouseControl; }; CMouseControl* GetMouseControl() { return m_pMouseControl; };
CLocalGUI* GetLocalGUI(); CLocalGUI* GetLocalGUI();
CLocalizationInterface* GetLocalization() { return g_pLocalization; }; CLocalizationInterface* GetLocalization() { return g_pLocalization; };
CWebCoreInterface* GetWebCore(); CWebCoreInterface* GetWebCore();
CTrayIconInterface* GetTrayIcon() { return m_pTrayIcon; }; CTrayIconInterface* GetTrayIcon() { return m_pTrayIcon; };
CDiscordManagerInterface* GetDiscordManager() { return reinterpret_cast<CDiscordManagerInterface*>(m_DiscordManager.get()); }


void SaveConfig(bool bWaitUntilFinished = false); void SaveConfig(bool bWaitUntilFinished = false);


Expand Down Expand Up @@ -226,6 +227,8 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
uint GetMinStreamingMemory(); uint GetMinStreamingMemory();
uint GetMaxStreamingMemory(); uint GetMaxStreamingMemory();


void ResetDiscordRichPresence();

SString GetConnectCommandFromURI(const char* szURI); SString GetConnectCommandFromURI(const char* szURI);
void GetConnectParametersFromURI(const char* szURI, std::string& strHost, unsigned short& usPort, std::string& strNick, std::string& strPassword); void GetConnectParametersFromURI(const char* szURI, std::string& strHost, unsigned short& usPort, std::string& strNick, std::string& strPassword);
bool bScreenShot; bool bScreenShot;
Expand Down Expand Up @@ -296,6 +299,8 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
CWebCoreInterface* m_pWebCore = nullptr; CWebCoreInterface* m_pWebCore = nullptr;
CTrayIcon* m_pTrayIcon; CTrayIcon* m_pTrayIcon;


std::unique_ptr<class CDiscordManager> m_DiscordManager;

// Hook interfaces. // Hook interfaces.
CMessageLoopHook* m_pMessageLoopHook; CMessageLoopHook* m_pMessageLoopHook;
CDirectInputHookManager* m_pDirectInputHookManager; CDirectInputHookManager* m_pDirectInputHookManager;
Expand Down

0 comments on commit 850c76d

Please sign in to comment.