Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord rpc tweaks (3205 successor) #3216

Merged
merged 29 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d5ce7df
Add server name to presence state
Lpsd Oct 12, 2023
d33913c
Remove unused method in CDiscordRichPresence
Lpsd Oct 12, 2023
0220b63
Modify state formatting
Lpsd Oct 12, 2023
48a6457
Move CCore::GetLastConnectedServerName definition to header
Lpsd Oct 12, 2023
0b59104
Allow setting start time & party size via Lua
Lpsd Oct 12, 2023
e3edcd1
Remove unnecessary check
Lpsd Oct 12, 2023
59def90
Change logic of SetStartTime
Lpsd Oct 12, 2023
33ffbd5
Rename variable
Lpsd Oct 12, 2023
89d022d
Swap CCore::GetSingleton usage for g_pCore
Lpsd Oct 12, 2023
8f99d74
Use details instead of state for server name
Lpsd Oct 12, 2023
3f8df64
Add some more default states (walking, driving, dead)
Lpsd Oct 12, 2023
58d4291
Allow custom state to be empty in order to reset it
Lpsd Oct 12, 2023
6ffa60b
Add setDiscordRichPresenceEndTime
Lpsd Oct 12, 2023
6e6cbe5
Add missing OOP method for setDiscordRichPresenceEndTime
Lpsd Oct 12, 2023
c1de4f7
Allow all string values to be empty/reset
Lpsd Oct 12, 2023
c5ddcdc
Remove unnecessary presence updates
Lpsd Oct 13, 2023
4628d69
Set Discord state update rate to 15
Lpsd Oct 15, 2023
a0b1ee5
Add new default states based on player tasks
Lpsd Oct 15, 2023
b8c7077
Use std::optional in STaskState
Lpsd Oct 15, 2023
6d5d08f
Update CDiscordRichPresence.cpp
Lpsd Oct 15, 2023
c74d60e
Update BitStream check in CPacketHandler::Packet_ServerJoined
Lpsd Oct 15, 2023
19b14b7
Use unordered_multimap
Lpsd Oct 15, 2023
bf14d1f
Address code review
Lpsd Oct 16, 2023
dd4a38d
Revert GetTickCount64_ usage where unix epoch necessary
Lpsd Oct 16, 2023
7543ad6
Addendum to previous commit
Lpsd Oct 16, 2023
ee3b6c9
Update g_vehicleTypePrefixes order
Lpsd Oct 18, 2023
da65173
Implement suggestions and minor code improvements
tederis Oct 23, 2023
6342ae3
Fix Windows build
tederis Oct 23, 2023
7d2e6b3
Fix custom presence details
tederis Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 66 additions & 60 deletions Client/core/CCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,30 @@ class CDiscordInterface;
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

#define BLUE_VERSION_STRING "Multi Theft Auto v" MTA_DM_BUILDTAG_LONG
#define BLUE_COPYRIGHT_STRING "Copyright (C) 2003 - %BUILD_YEAR% Multi Theft Auto"
#define BLUE_VERSION_STRING "Multi Theft Auto v" MTA_DM_BUILDTAG_LONG
#define BLUE_COPYRIGHT_STRING "Copyright (C) 2003 - %BUILD_YEAR% Multi Theft Auto"

// Configuration file path (relative to MTA install directory)
#define MTA_CONFIG_PATH "mta/config/coreconfig.xml"
#define MTA_SERVER_CACHE_PATH "mta/config/servercache.xml"
#define MTA_CONSOLE_LOG_PATH "mta/logs/console.log"
#define MTA_CONSOLE_INPUT_LOG_PATH "mta/logs/console-input.log"
#define CONFIG_ROOT "mainconfig"
#define CONFIG_NODE_CVARS "settings" // cvars node
#define CONFIG_NODE_KEYBINDS "binds" // keybinds node
#define CONFIG_NODE_JOYPAD "joypad"
#define CONFIG_NODE_UPDATER "updater"
#define CONFIG_NODE_SERVER_INT "internet_servers" // backup of last successful master server list query
#define CONFIG_NODE_SERVER_FAV "favourite_servers" // favourite servers list node
#define CONFIG_NODE_SERVER_REC "recently_played_servers" // recently played servers list node
#define CONFIG_NODE_SERVER_OPTIONS "serverbrowser_options" // saved options for the server browser
#define CONFIG_NODE_SERVER_SAVED "server_passwords" // This contains saved passwords (as appose to save_server_passwords which is a setting)
#define CONFIG_NODE_SERVER_HISTORY "connect_history"
#define CONFIG_INTERNET_LIST_TAG "internet_server"
#define CONFIG_FAVOURITE_LIST_TAG "favourite_server"
#define CONFIG_RECENT_LIST_TAG "recently_played_server"
#define CONFIG_HISTORY_LIST_TAG "connected_server"
#define IDT_TIMER1 1234
#define MTA_CONFIG_PATH "mta/config/coreconfig.xml"
#define MTA_SERVER_CACHE_PATH "mta/config/servercache.xml"
#define MTA_CONSOLE_LOG_PATH "mta/logs/console.log"
#define MTA_CONSOLE_INPUT_LOG_PATH "mta/logs/console-input.log"
#define CONFIG_ROOT "mainconfig"
#define CONFIG_NODE_CVARS "settings" // cvars node
#define CONFIG_NODE_KEYBINDS "binds" // keybinds node
#define CONFIG_NODE_JOYPAD "joypad"
#define CONFIG_NODE_UPDATER "updater"
#define CONFIG_NODE_SERVER_INT "internet_servers" // backup of last successful master server list query
#define CONFIG_NODE_SERVER_FAV "favourite_servers" // favourite servers list node
#define CONFIG_NODE_SERVER_REC "recently_played_servers" // recently played servers list node
#define CONFIG_NODE_SERVER_OPTIONS "serverbrowser_options" // saved options for the server browser
#define CONFIG_NODE_SERVER_SAVED "server_passwords" // This contains saved passwords (as appose to save_server_passwords which is a setting)
#define CONFIG_NODE_SERVER_HISTORY "connect_history"
#define CONFIG_INTERNET_LIST_TAG "internet_server"
#define CONFIG_FAVOURITE_LIST_TAG "favourite_server"
#define CONFIG_RECENT_LIST_TAG "recently_played_server"
#define CONFIG_HISTORY_LIST_TAG "connected_server"
#define IDT_TIMER1 1234

extern class CCore* g_pCore;
extern class CGraphics* g_pGraphics;
Expand All @@ -84,26 +84,26 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
~CCore();

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

void SaveConfig(bool bWaitUntilFinished = false);

Expand Down Expand Up @@ -285,6 +285,10 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
void SetCustomStreamingMemory(size_t szMB);
bool IsUsingCustomStreamingMemorySize();
size_t GetStreamingMemory();

const SString& GetLastConnectedServerName() const { return m_strLastConnectedServerName; }
void SetLastConnectedServerName(const SString& strServerName) { m_strLastConnectedServerName = strServerName; }

private:
void ApplyCoreInitSettings();

Expand All @@ -299,11 +303,11 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
CModelCacheManager* m_pModelCacheManager;

// Instances (put new classes here!)
CXMLFile* m_pConfigFile;
CClientVariables m_ClientVariables;
CWebCoreInterface* m_pWebCore = nullptr;
CTrayIcon* m_pTrayIcon;
std::shared_ptr<CDiscordRichPresence> m_pDiscordRichPresence;
CXMLFile* m_pConfigFile;
CClientVariables m_ClientVariables;
CWebCoreInterface* m_pWebCore = nullptr;
CTrayIcon* m_pTrayIcon;
std::shared_ptr<CDiscordRichPresence> m_pDiscordRichPresence;

// Hook interfaces.
CMessageLoopHook* m_pMessageLoopHook;
Expand Down Expand Up @@ -369,22 +373,24 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
EDiagnosticDebugType m_DiagnosticDebug;

// Below 2 are used for the UI only
float m_fMinStreamingMemory{};
float m_fMaxStreamingMemory{};
float m_fMinStreamingMemory{};
float m_fMaxStreamingMemory{};

// Custom streaming memory limit set by `engineStreamingSetMemorySize` - Reset on server connects (= set to 0), or by the scripter
// `0` means "not set" [so the value should be ignored]
size_t m_CustomStreamingMemoryLimitBytes{};

bool m_bGettingIdleCallsFromMultiplayer;
bool m_bWindowsTimerEnabled;
bool m_bModulesLoaded;
int m_iDummyProgressValue;
HANDLE m_DummyProgressTimerHandle;
SString m_strDummyProgressType;
bool m_bDummyProgressUpdateAlways;
bool m_bIsRenderingGrass;
bool m_bFakeLagCommandEnabled;
size_t m_CustomStreamingMemoryLimitBytes{};

bool m_bGettingIdleCallsFromMultiplayer;
bool m_bWindowsTimerEnabled;
bool m_bModulesLoaded;
int m_iDummyProgressValue;
HANDLE m_DummyProgressTimerHandle;
SString m_strDummyProgressType;
bool m_bDummyProgressUpdateAlways;
bool m_bIsRenderingGrass;
bool m_bFakeLagCommandEnabled;

SString m_strLastConnectedServerName{};

// Command line
static void ParseCommandLine(std::map<std::string, std::string>& options, const char*& szArgs, const char** pszNoValOptions = NULL);
Expand Down
39 changes: 32 additions & 7 deletions Client/core/CDiscordRichPresence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ void CDiscordRichPresence::SetDefaultData()
m_strDiscordAppAssetSmallText = DEFAULT_APP_ASSET_SMALL_TEXT;

m_strDiscordAppCurrentId = DEFAULT_APP_ID;
m_strDiscordAppDetails.clear();
m_strDiscordAppCustomDetails.clear();
m_strDiscordAppCustomState.clear();

m_aButtons = {};
m_bUpdateRichPresence = true;
m_bDisallowCustomDetails = true;

m_uiDiscordAppStart = 0;
m_uiDiscordAppEnd = 0;
}

void CDiscordRichPresence::UpdatePresence()
Expand All @@ -87,8 +90,10 @@ void CDiscordRichPresence::UpdatePresence()

discordPresence.state = (!m_strDiscordAppCustomState.empty() || !m_bDisallowCustomDetails) ? m_strDiscordAppCustomState.c_str() : m_strDiscordAppState.c_str();

discordPresence.details = m_strDiscordAppDetails.c_str();
discordPresence.details =
(!m_strDiscordAppCustomDetails.empty() || !m_bDisallowCustomDetails) ? m_strDiscordAppCustomDetails.c_str() : m_strDiscordAppDetails.c_str();
discordPresence.startTimestamp = m_uiDiscordAppStart;
discordPresence.endTimestamp = m_uiDiscordAppEnd;

DiscordButton buttons[2];
if (m_aButtons)
Expand All @@ -101,6 +106,9 @@ void CDiscordRichPresence::UpdatePresence()
discordPresence.buttons = buttons;
}

discordPresence.partySize = (m_bDisallowCustomDetails) ? 0 : m_iPartySize;
discordPresence.partyMax = (m_bDisallowCustomDetails) ? 0 : m_iPartyMax;

Discord_UpdatePresence(&discordPresence);
m_bUpdateRichPresence = false;
}
Expand All @@ -111,6 +119,13 @@ void CDiscordRichPresence::SetPresenceStartTimestamp(const unsigned long ulStart
m_bUpdateRichPresence = true;
}

void CDiscordRichPresence::SetPresenceEndTimestamp(const unsigned long ulEnd)
{
m_uiDiscordAppEnd = ulEnd;
m_bUpdateRichPresence = true;
}


void CDiscordRichPresence::SetAssetLargeData(const char* szAsset, const char* szAssetText)
{
SetAsset(szAsset, szAssetText, true);
Expand All @@ -125,13 +140,13 @@ void CDiscordRichPresence::SetAsset(const char* szAsset, const char* szAssetText
{
if (isLarge)
{
m_strDiscordAppAsset = (szAsset && *szAsset) ? szAsset : DEFAULT_APP_ASSET;
m_strDiscordAppAssetText = (szAssetText && *szAssetText) ? szAssetText : DEFAULT_APP_ASSET_TEXT;
m_strDiscordAppAsset = (std::strlen(szAsset) > 0 && szAsset && *szAsset) ? szAsset : DEFAULT_APP_ASSET;
m_strDiscordAppAssetText = (std::strlen(szAssetText) > 0 && szAssetText && *szAssetText) ? szAssetText : DEFAULT_APP_ASSET_TEXT;
}
else
{
m_strDiscordAppAssetSmall = (szAsset && *szAsset) ? szAsset : DEFAULT_APP_ASSET_SMALL;
m_strDiscordAppAssetSmallText = (szAssetText && *szAssetText) ? szAssetText : DEFAULT_APP_ASSET_SMALL_TEXT;
m_strDiscordAppAssetSmall = (std::strlen(szAsset) > 0 && szAsset && *szAsset) ? szAsset : DEFAULT_APP_ASSET_SMALL;
m_strDiscordAppAssetSmallText = (std::strlen(szAssetText) > 0 && szAssetText && *szAssetText) ? szAssetText : DEFAULT_APP_ASSET_SMALL_TEXT;
}
m_bUpdateRichPresence = true;
}
Expand Down Expand Up @@ -172,7 +187,11 @@ bool CDiscordRichPresence::SetPresenceButtons(unsigned short int iIndex, const c

bool CDiscordRichPresence::SetPresenceDetails(const char* szDetails, bool bCustom)
{
m_strDiscordAppDetails = szDetails;
if (bCustom)
m_strDiscordAppCustomDetails = szDetails;
else
m_strDiscordAppDetails = szDetails;

m_bUpdateRichPresence = true;
return true;
}
Expand Down Expand Up @@ -225,3 +244,9 @@ bool CDiscordRichPresence::IsDiscordCustomDetailsDisallowed() const
{
return m_bDisallowCustomDetails;
}

void CDiscordRichPresence::SetPresencePartySize(int iSize, int iMax)
{
m_iPartySize = iSize;
m_iPartyMax = iMax;
}
6 changes: 6 additions & 0 deletions Client/core/CDiscordRichPresence.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CDiscordRichPresence : public CDiscordInterface

void UpdatePresence();
void SetPresenceStartTimestamp(const unsigned long ulStart);
void SetPresenceEndTimestamp(const unsigned long ulEnd);
void SetAsset(const char* szAsset, const char* szAssetText, bool bIsLarge = false);
void SetAssetLargeData(const char* szAsset, const char* szAssetText);
void SetAssetSmallData(const char* szAsset, const char* szAssetText);
Expand All @@ -34,6 +35,7 @@ class CDiscordRichPresence : public CDiscordInterface
bool SetPresenceState(const char* szState, bool bCustom = false);
bool SetPresenceDetails(const char* szDetails, bool bCustom = false);
bool SetPresenceButtons(unsigned short int iIndex, const char* szName, const char* szUrl);
void SetPresencePartySize(int iSize, int iMax);
bool SetDiscordRPCEnabled(bool bEnabled);
bool IsDiscordCustomDetailsDisallowed() const;
bool IsDiscordRPCEnabled() const;
Expand All @@ -54,6 +56,7 @@ class CDiscordRichPresence : public CDiscordInterface
std::string m_strDiscordAppState;
std::string m_strDiscordAppDetails;
std::string m_strDiscordAppCustomState;
std::string m_strDiscordAppCustomDetails;

std::optional<std::tuple<std::pair<std::string, std::string>, std::pair<std::string, std::string>>> m_aButtons;

Expand All @@ -63,4 +66,7 @@ class CDiscordRichPresence : public CDiscordInterface
bool m_bDisallowCustomDetails;
bool m_bDiscordRPCEnabled;
bool m_bUpdateRichPresence;

int m_iPartySize;
int m_iPartyMax;
};
20 changes: 20 additions & 0 deletions Client/core/CSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3452,6 +3452,26 @@ void CSettings::SaveData()
CVARS_SET("allow_discord_rpc", bAllowDiscordRPC);
g_pCore->GetDiscord()->SetDiscordRPCEnabled(bAllowDiscordRPC);

if (bAllowDiscordRPC)
{
auto discord = g_pCore->GetDiscord();

if (discord)
{
const char* state = "Main menu";

if (g_pCore->IsConnected())
{
state = "In-game";

const SString& serverName = g_pCore->GetLastConnectedServerName();
discord->SetPresenceDetails(serverName.c_str(), false);
}

discord->SetPresenceState(state, false);
}
}

// Grass
bool bGrassEnabled = m_pCheckBoxGrass->GetSelected();
CVARS_SET("grass", bGrassEnabled);
Expand Down
Loading