h2-mod/src/client/component/discord.cpp

159 lines
3.7 KiB
C++
Raw Normal View History

2021-09-06 18:40:37 -04:00
#include <std_include.hpp>
2021-04-24 22:09:13 -04:00
#include "loader/component_loader.hpp"
2021-04-25 14:18:19 -04:00
2021-04-24 22:09:13 -04:00
#include "game/game.hpp"
2021-04-25 14:18:19 -04:00
#include "scheduler.hpp"
#include "command.hpp"
2021-04-24 22:09:13 -04:00
#include <utils/string.hpp>
#include <discord_rpc.h>
namespace discord
{
namespace
{
DiscordRichPresence discord_presence;
2021-04-25 14:18:19 -04:00
std::string state;
2022-06-18 21:32:37 -04:00
std::optional<std::string> details{};
2022-09-01 19:31:02 -04:00
std::optional<std::string> image{};
2021-04-24 22:09:13 -04:00
void update_discord()
{
Discord_RunCallbacks();
if (!game::CL_IsCgameInitialized())
{
2022-06-18 21:32:37 -04:00
state = {};
details.reset();
2022-09-01 19:31:02 -04:00
image.reset();
2022-06-18 21:32:37 -04:00
2022-07-14 18:12:27 -04:00
discord_presence.details = game::UI_SafeTranslateString("MENU_MAIN_MENU");
2021-04-24 22:09:13 -04:00
discord_presence.state = "";
discord_presence.startTimestamp = 0;
2021-12-28 20:29:22 -05:00
const auto background_index = static_cast<int>(game::Sys_Milliseconds() / 300000) % 10;
discord_presence.largeImageKey = utils::string::va("bg_%i", background_index);
2021-04-24 22:09:13 -04:00
}
else
{
2022-09-01 19:31:02 -04:00
static char map[0x1000] = {0};
if (image.has_value())
{
strncpy_s(map, image.value().data(), sizeof(map));
}
else
{
const auto mapname = game::Dvar_FindVar("mapname")->current.string;
strncpy_s(map, mapname, sizeof(map));
}
2021-04-24 22:09:13 -04:00
const auto mapname = game::UI_SafeTranslateString(utils::string::va("PRESENCE_SP_%s", map));
2021-08-30 22:48:36 -04:00
discord_presence.largeImageKey = map;
2022-06-18 21:32:37 -04:00
if (details.has_value())
{
2022-07-20 19:02:56 -04:00
const auto& details_ = details.value();
if (details_.starts_with("@") && details_.size() > 1)
{
const auto value = game::UI_SafeTranslateString(details_.substr(1).data());
discord_presence.details = value;
}
else
{
discord_presence.details = utils::string::va("%s", details_.data());
}
2022-06-18 21:32:37 -04:00
}
else
{
discord_presence.details = mapname;
}
2021-04-25 14:18:19 -04:00
discord_presence.state = state.data();
2021-04-24 22:09:13 -04:00
if (!discord_presence.startTimestamp)
{
discord_presence.startTimestamp = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
}
}
Discord_UpdatePresence(&discord_presence);
}
}
class component final : public component_interface
{
public:
void post_unpack() override
{
DiscordEventHandlers handlers;
ZeroMemory(&handlers, sizeof(handlers));
handlers.ready = ready;
handlers.errored = errored;
handlers.disconnected = errored;
handlers.joinGame = nullptr;
handlers.spectateGame = nullptr;
handlers.joinRequest = nullptr;
Discord_Initialize("835690302583996416", &handlers, 1, nullptr);
scheduler::loop(update_discord, scheduler::pipeline::async, 5s);
initialized_ = true;
2021-04-25 14:18:19 -04:00
command::add("setdiscordstate", [](const command::params& params)
{
const std::string _state = params.join(1);
2022-07-20 19:02:56 -04:00
scheduler::once([=]()
2021-04-25 14:18:19 -04:00
{
state = _state;
update_discord();
}, scheduler::pipeline::async);
});
2022-06-18 21:32:37 -04:00
command::add("setdiscorddetails", [](const command::params& params)
{
const std::string details_ = params.join(1);
scheduler::once([=]()
{
details = details_;
update_discord();
}, scheduler::pipeline::async);
});
2022-09-01 19:31:02 -04:00
command::add("setdiscordimage", [](const command::params& params)
{
const std::string image_ = params.join(1);
scheduler::once([=]()
{
image = image_;
update_discord();
}, scheduler::pipeline::async);
});
2021-04-24 22:09:13 -04:00
}
private:
bool initialized_ = false;
static void ready(const DiscordUser* /*request*/)
{
ZeroMemory(&discord_presence, sizeof(discord_presence));
discord_presence.instance = 1;
2021-04-25 14:18:19 -04:00
discord_presence.state = "";
2021-04-24 22:09:13 -04:00
Discord_UpdatePresence(&discord_presence);
}
static void errored(const int error_code, const char* message)
{
printf("Discord: (%i) %s", error_code, message);
}
};
}
REGISTER_COMPONENT(discord::component)