Skip to content

Commit

Permalink
feat: discord rpc buttons
Browse files Browse the repository at this point in the history
Warning: debug build does not currently work
  • Loading branch information
BenMcAvoy committed Jan 1, 2025
1 parent 679e07f commit 0854bc6
Show file tree
Hide file tree
Showing 19 changed files with 1,950 additions and 95 deletions.
18 changes: 16 additions & 2 deletions CarbonLauncher/CarbonLauncher.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>false</WholeProgramOptimization>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
Expand Down Expand Up @@ -114,7 +115,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>include;.</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>include;vendor\include;.</AdditionalIncludeDirectories>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
Expand All @@ -131,7 +132,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>include;.</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>include;vendor\include;.</AdditionalIncludeDirectories>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
Expand All @@ -150,6 +151,12 @@
<ClCompile Include="src\repomanager.cpp" />
<ClCompile Include="src\state.cpp" />
<ClCompile Include="src\utils.cpp" />
<ClCompile Include="vendor\src\connection_win.cpp" />
<ClCompile Include="vendor\src\discord_register_win.cpp" />
<ClCompile Include="vendor\src\discord_rpc.cpp" />
<ClCompile Include="vendor\src\dllmain.cpp" />
<ClCompile Include="vendor\src\rpc_connection.cpp" />
<ClCompile Include="vendor\src\serialization.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\font\IconsFontAwesome6.h" />
Expand All @@ -162,6 +169,13 @@
<ClInclude Include="include\state.h" />
<ClInclude Include="include\utils.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="vendor\include\discord_register.h" />
<ClInclude Include="vendor\include\discord_rpc.h" />
<ClInclude Include="vendor\src\backoff.h" />
<ClInclude Include="vendor\src\connection.h" />
<ClInclude Include="vendor\src\msg_queue.h" />
<ClInclude Include="vendor\src\rpc_connection.h" />
<ClInclude Include="vendor\src\serialization.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="CarbonLauncher.rc" />
Expand Down
18 changes: 10 additions & 8 deletions CarbonLauncher/include/discordmanager.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <discord-game-sdk/discord_game_sdk.h>
#include <discord-game-sdk/discord.h>
/*#include <discord-game-sdk/discord_game_sdk.h>
#include <discord-game-sdk/discord.h>*/

#include <discord_rpc.h>

#include <string>
#include <memory>
Expand All @@ -22,19 +24,19 @@ namespace Carbon {
// @param details The new details
void UpdateDetails(const std::string& details);

// Updates the activity of the Discord RPC
void UpdateActivity() const;

// Gets the current activity
discord::Activity& GetActivity();
DiscordRichPresence& GetPresence();

// Updates the Discord RPC and listens for game state changes
void Update();

private:
discord::User currentUser = discord::User{};
DiscordEventHandlers discordHandlers = { 0 };
DiscordRichPresence discordPresence = { 0 };

/*discord::User currentUser = discord::User{};
discord::Activity currentActivity = discord::Activity{};
std::unique_ptr<discord::Core> core = nullptr;
std::unique_ptr<discord::Core> core = nullptr;*/
};
}; // namespace Carbon
4 changes: 2 additions & 2 deletions CarbonLauncher/include/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Carbon {
class CarbonState_t {
public:
CarbonState_t() {
#ifndef NDEBUG
//#ifndef NDEBUG
// Tell winapi to show a console window
AllocConsole();
FILE* file;
Expand All @@ -25,7 +25,7 @@ namespace Carbon {
spdlog::set_default_logger(console); // Set the default logger to the console logger
console->set_level(spdlog::level::trace); // Set the log level to info
spdlog::set_pattern("%^[ %H:%M:%S | %-8l] %n: %v%$"); // Nice log format
#endif
//#endif
}

Carbon::GUIManager guiManager;
Expand Down
127 changes: 46 additions & 81 deletions CarbonLauncher/src/discordmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,106 +5,71 @@
#include <spdlog/spdlog.h>
#include <fmt/format.h>

constexpr auto discordClientId = 1315436867545595904;
constexpr auto discordClientId = "1315436867545595904";

using namespace Carbon;

DiscordManager::DiscordManager() {
discord::Core* core{};
auto result = discord::Core::Create(discordClientId, DiscordCreateFlags_NoRequireDiscord, &core);

this->core.reset(core);

if (!this->core) {
spdlog::warn("Failed to create Discord core");
}

else {
auto dCore = this->core.get();

dCore->SetLogHook(discord::LogLevel::Debug, [](discord::LogLevel level, const char* message) {
spdlog::debug("[Discord] {}", message);
});

dCore->SetLogHook(discord::LogLevel::Info, [](discord::LogLevel level, const char* message) {
spdlog::info("[Discord] {}", message);
});

dCore->SetLogHook(discord::LogLevel::Warn, [](discord::LogLevel level, const char* message) {
spdlog::warn("[Discord] {}", message);
});

dCore->SetLogHook(discord::LogLevel::Error, [](discord::LogLevel level, const char* message) {
spdlog::error("[Discord] {}", message);
});

this->core->ActivityManager().RegisterCommand("carbonlauncher://run");

this->currentActivity = {};
auto& activity = this->GetActivity();

activity.SetDetails("https://github.com/ScrappySM/CarbonLauncher!");
activity.SetState("In the launcher!");

activity.GetAssets().SetLargeImage("icon");
activity.GetAssets().SetLargeText("Carbon Launcher");
/*activity.GetAssets().SetSmallImage("icon");
activity.GetAssets().SetSmallText("Carbon Launcher");*/

activity.SetType(discord::ActivityType::Playing);
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
handlers.ready = [](const DiscordUser* connectedUser) {
spdlog::info("Discord connected to user {}", connectedUser->username);
};
handlers.disconnected = [](int errorCode, const char* message) {
spdlog::warn("Discord disconnected with error code {}: {}", errorCode, message);
};
handlers.errored = [](int errorCode, const char* message) {
spdlog::error("Discord error with code {}: {}", errorCode, message);
};
handlers.joinGame = [](const char* secret) {
spdlog::info("Joining game with secret {}", secret);
};
handlers.spectateGame = [](const char* secret) {
spdlog::info("Spectating game with secret {}", secret);
};
handlers.joinRequest = [](const DiscordUser* request) {
spdlog::info("Join request from {}#{}", request->username, request->discriminator);
};
Discord_Initialize(discordClientId, &handlers, 1, NULL);
this->discordHandlers = handlers;

this->core->ActivityManager().UpdateActivity(activity, [](discord::Result result) {
if (result != discord::Result::Ok) {
spdlog::error("Failed to update Discord RPC (error: {})", (int)result);
}
});
spdlog::info("Initialized Discord instance");

activity.GetAssets().SetLargeImage("icon");
activity.GetAssets().SetLargeText("Carbon Launcher");
this->discordPresence = DiscordRichPresence{
.details = "The most advanced mod loader for Scrap Mechanic",
.state = "In the main menu",
.largeImageKey = "icon",
.largeImageText = "Carbon Launcher",
.button1Label = "GitHub",
.button1Url = "https://github.com/ScrappySM/CarbonLauncher",
.button2Label = "Download",
.button2Url = "https://github.com/ScrappySM/CarbonLauncher/releases/latest",
.instance = 0,
};

this->UpdateActivity();
}
Discord_UpdatePresence(&this->discordPresence);

spdlog::info("Updated Discord presence");

spdlog::info("Created Discord instance");
Discord_UpdateHandlers(&handlers);
}

void DiscordManager::UpdateState(const std::string& state) {
if (!this->core) {
return;
}

this->currentActivity.SetState(state.c_str());
this->UpdateActivity();
this->discordPresence.state = state.c_str();
Discord_UpdatePresence(&this->discordPresence);
}

void DiscordManager::UpdateDetails(const std::string& details) {
if (!this->core) {
return;
}

this->currentActivity.SetDetails(details.c_str());
this->UpdateActivity();
this->discordPresence.details = details.c_str();
Discord_UpdatePresence(&this->discordPresence);
}

DiscordManager::~DiscordManager() {
spdlog::info("Destroying Discord instance");
}

void DiscordManager::UpdateActivity() const {
if (!this->core) {
return;
}

this->core->ActivityManager().UpdateActivity(this->currentActivity, [](discord::Result result) {
if (result != discord::Result::Ok) {
spdlog::error("Failed to update activity");
}
});
}

discord::Activity& DiscordManager::GetActivity() {
return this->currentActivity;
DiscordRichPresence& DiscordManager::GetPresence() {
return this->discordPresence;
}

void DiscordManager::Update() {
Expand Down Expand Up @@ -147,6 +112,6 @@ void DiscordManager::Update() {
statePackets.pop();
}

if (this->core)
this->core->RunCallbacks();
Discord_RunCallbacks();
Discord_UpdateHandlers(&this->discordHandlers);
}
4 changes: 2 additions & 2 deletions CarbonLauncher/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"dependencies": [
"cpr",
"discord-game-sdk",
{
"name": "glad",
"features": [
Expand All @@ -17,6 +16,7 @@
]
},
"spdlog",
"nlohmann-json"
"nlohmann-json",
"rapidjson"
]
}
26 changes: 26 additions & 0 deletions CarbonLauncher/vendor/include/discord_register.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#if defined(DISCORD_DYNAMIC_LIB)
#if defined(_WIN32)
#if defined(DISCORD_BUILDING_SDK)
#define DISCORD_EXPORT __declspec(dllexport)
#else
#define DISCORD_EXPORT __declspec(dllimport)
#endif
#else
#define DISCORD_EXPORT __attribute__((visibility("default")))
#endif
#else
#define DISCORD_EXPORT
#endif

#ifdef __cplusplus
extern "C" {
#endif

DISCORD_EXPORT void Discord_Register(const char* applicationId, const char* command);
DISCORD_EXPORT void Discord_RegisterSteamGame(const char* applicationId, const char* steamId);

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit 0854bc6

Please sign in to comment.