110 lines · 3.4 KB · C++
RawDownload
////  DiscordRichPresence.cpp//  interface/src////  Created by Julian Groß on 30th October 2023.//  Copyright 2023 Overte e.V.////  Distributed under the Apache License, Version 2.0.//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html//  SPDX-License-Identifier: Apache-2.0// #include <QtCore/QString>#include <QtCore/QDateTime>#include <QtCore/QLoggingCategory> #include "discord_rpc.h"#include "DiscordRichPresence.h"#include "DependencyManager.h"#include "AddressManager.h"#include "scripting/HMDScriptingInterface.h"#include "Application.h"#include "EntityTreeRenderer.h" #define DISCORD_APPLICATION_CLIENT_ID "1168082546270163014"#define STEAM_APPLICATION_ID "1234" // placeholder since we don't have a Steam application ID yet Q_LOGGING_CATEGORY(discord_rich_presence, "overte.discord_rich_presence") DiscordPresence::DiscordPresence(){    auto addressManager = DependencyManager::get<AddressManager>();    connect(addressManager.data(), &AddressManager::hostChanged, this, &DiscordPresence::domainChanged);     auto hMDScriptingInterface = DependencyManager::get<HMDScriptingInterface>();    DiscordPresence::vrChanged(hMDScriptingInterface->isHMDMode()); // we don't receive the initial signal, so we ask for the current state    connect(hMDScriptingInterface.data(), &HMDScriptingInterface::displayModeChanged, this, &DiscordPresence::vrChanged);} void DiscordPresence::shutdown(){    qCDebug(discord_rich_presence, "Shutting down");    Discord_Shutdown();    discordPresence = {};    running = false;} void DiscordPresence::startup(){    DiscordEventHandlers handlers;    Discord_Initialize(DISCORD_APPLICATION_CLIENT_ID, &handlers, 1, STEAM_APPLICATION_ID);    discordPresence.largeImageKey = "header";    discordPresence.smallImageKey = "";    discordPresence.smallImageText = "";    const int64_t startEpoch = QDateTime::currentSecsSinceEpoch();    discordPresence.startTimestamp = startEpoch;    qCDebug(discord_rich_presence, "Started");     running = true;} void DiscordPresence::setEnabled(bool enable){    enabled = enable;     if (enabled && !running) {        startup();    } else if (!enabled && running) {        shutdown();    }} void DiscordPresence::domainChanged(){    if (!running) return;     const auto addressManager = DependencyManager::get<AddressManager>();    if (!addressManager) return;     QString state;    // TODO: switch to getPlaceName once https://github.com/overte-org/overte/issues/684 is fixed    const QString worldName = addressManager->getHost();    if (worldName != "") {        state = ("In " + worldName);        qCDebug(discord_rich_presence) << "Discord log hostName: " + worldName;    } else {        state = "";        qCDebug(discord_rich_presence) << "Discord log: in unknown place";    }     QByteArray state_data = state.toUtf8();    discordPresence.state = state_data.constData();     Discord_UpdatePresence(&discordPresence);} void DiscordPresence::vrChanged(bool isHMDMode = false){    if (!running) return;     if (isHMDMode) {        discordPresence.smallImageKey = "hmd";        discordPresence.smallImageText = "in VR";    } else {        discordPresence.smallImageKey = "desktop";        discordPresence.smallImageText = "in desktop mode";    }    qCDebug(discord_rich_presence) << "Discord log HMDMode: " + QString::number(isHMDMode);    Discord_UpdatePresence(&discordPresence);}