Skip to content

Commit

Permalink
[WIP] Initial work on implementing Discord Rich Presence
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Jan 31, 2018
1 parent d6bbdc3 commit bf774db
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 4 deletions.
12 changes: 10 additions & 2 deletions projectfiles/VC12/wesnoth.vcxproj
Expand Up @@ -137,7 +137,7 @@
</ResourceCompile>
<Link>
<AdditionalOptions>/SAFESEH:NO %(AdditionalOptions)</AdditionalOptions>
<AdditionalDependencies>SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;libeay32.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;libeay32.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;discord-rpc.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\..\..\external\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<IgnoreSpecificDefaultLibraries>MSVCR90;MSVCRT;%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down Expand Up @@ -177,7 +177,7 @@
<LinkLibraryDependencies>true</LinkLibraryDependencies>
</ProjectReference>
<Link>
<AdditionalDependencies>SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;libeay32.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>SDL2main.lib;SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;libeay32.lib;cairo.lib;winmm.lib;ws2_32.lib;pango-1.0.lib;pangocairo-1.0.lib;gobject-2.0.lib;glib-2.0.lib;libpng.lib;discord-rpc.lib;$(IntDir)liblua.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\..\..\external\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>false</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -751,6 +751,13 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Desktop\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\desktop\discord_rich_presence.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Desktop\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\desktop\notifications.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Desktop\</ObjectFileName>
Expand Down Expand Up @@ -3582,6 +3589,7 @@
<ClInclude Include="..\..\src\countdown_clock.hpp" />
<ClInclude Include="..\..\src\cursor.hpp" />
<ClInclude Include="..\..\src\desktop\clipboard.hpp" />
<ClInclude Include="..\..\src\desktop\discord_rich_presence.hpp" />
<ClInclude Include="..\..\src\desktop\notifications.hpp" />
<ClInclude Include="..\..\src\desktop\open.hpp" />
<ClInclude Include="..\..\src\desktop\paths.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions projectfiles/VC12/wesnoth.vcxproj.filters
Expand Up @@ -1558,6 +1558,9 @@
<ClCompile Include="..\..\src\gui\dialogs\multiplayer\player_list_helper.cpp">
<Filter>Gui\Dialogs\Multiplayer</Filter>
</ClCompile>
<ClCompile Include="..\..\src\desktop\discord_rich_presence.cpp">
<Filter>Desktop</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\addon\client.hpp">
Expand Down Expand Up @@ -3025,6 +3028,9 @@
<ClInclude Include="..\..\src\gui\dialogs\multiplayer\player_list_helper.hpp">
<Filter>Gui\Dialogs\Multiplayer</Filter>
</ClInclude>
<ClInclude Include="..\..\src\desktop\discord_rich_presence.hpp">
<Filter>Desktop</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\src\tests\test_sdl_utils.hpp">
Expand Down
1 change: 1 addition & 0 deletions source_lists/wesnoth
Expand Up @@ -66,6 +66,7 @@ commandline_options.cpp
config_cache.cpp
controller_base.cpp
countdown_clock.cpp
desktop/discord_rich_presence.cpp
desktop/notifications.cpp
desktop/open.cpp
desktop/paths.cpp
Expand Down
115 changes: 115 additions & 0 deletions src/desktop/discord_rich_presence.cpp
@@ -0,0 +1,115 @@
/*
Copyright (C) 2018 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "desktop/discord_rich_presence.hpp"

#include "log.hpp"

#include <discord-rpc.h>

static lg::log_domain log_desktop("desktop");
#define ERR_DU LOG_STREAM(err, log_desktop)
#define LOG_DU LOG_STREAM(info, log_desktop)

namespace desktop
{
namespace
{
const char* DISCORD_APP_ID = "407478227485982720";
const char* STEAM_APP_ID = "599390";

void callback_ready()
{
LOG_DU << "Discord Rich Presence ready\n";
}

void callback_errored(int /*errorCode*/, const char* message)
{
ERR_DU << "Discord Rich Presence errored: " << message << "\n";
}

void callback_disconnected(int /*errorCode*/, const char* message)
{
LOG_DU << "Discord Rich Presence disconnected: " << message << "\n";
}

} // end anon namespace

rich_presence_manager::rich_presence_manager()
{
DiscordEventHandlers handlers;
std::memset(&handlers, 0, sizeof(handlers));

handlers.ready = &callback_ready;
handlers.errored = &callback_errored;
handlers.disconnected = &callback_disconnected;

// TODO: we might want to add the ability to join/observe games directly from
// Discord in the future, but that requires special approval by Discord. Leaving
// these callbacks null for now.
handlers.joinGame = nullptr;
handlers.spectateGame = nullptr;
handlers.joinRequest = nullptr;

Discord_Initialize(DISCORD_APP_ID, &handlers, 1, STEAM_APP_ID);
}

rich_presence_manager::~rich_presence_manager()
{
Discord_Shutdown();
}

discord_presence::discord_presence()
: state()
, details()
, start_timestamp(0)
, end_timestamp(0)
, large_image_key("weslogo") // NOTE: right now we only have one large image.
, large_image_text()
, small_image_key()
, small_image_text()
, party_id()
, party_size(0)
, party_max(0)
, match_secret()
, join_secret()
, spectate_secret()
, instance(0)
{
}

void discord_presence::send() const
{
DiscordRichPresence data;
std::memset(&data, 0, sizeof(data));

data.state = state.c_str();
data.details = details.c_str();
data.startTimestamp = start_timestamp;
data.endTimestamp = end_timestamp;
data.largeImageKey = large_image_key.c_str();
data.largeImageText = large_image_text.c_str();
data.smallImageKey = small_image_key.c_str();
data.smallImageText = small_image_text.c_str();
data.partyId = party_id.c_str();
data.partySize = party_size;
data.partyMax = party_max;
data.matchSecret = match_secret.c_str();
data.joinSecret = join_secret.c_str();
data.spectateSecret = spectate_secret.c_str();
data.instance = instance;

Discord_UpdatePresence(&data);
}

} // namespace desktop1
59 changes: 59 additions & 0 deletions src/desktop/discord_rich_presence.hpp
@@ -0,0 +1,59 @@
/*
Copyright (C) 2018 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#pragma once

#include <cstdint>
#include <string>

namespace desktop
{
class rich_presence_manager
{
public:
rich_presence_manager();
~rich_presence_manager();
};

/**
*
*/
struct discord_presence
{
discord_presence();

void send() const;

std::string state; /* max 128 bytes */
std::string details; /* max 128 bytes */

int64_t start_timestamp;
int64_t end_timestamp;

std::string large_image_key; /* max 32 bytes */
std::string large_image_text; /* max 128 bytes */
std::string small_image_key; /* max 32 bytes */
std::string small_image_text; /* max 128 bytes */
std::string party_id; /* max 128 bytes */

int party_size;
int party_max;

std::string match_secret; /* max 128 bytes */
std::string join_secret; /* max 128 bytes */
std::string spectate_secret; /* max 128 bytes */

int8_t instance;
};

} // namespace desktop
24 changes: 23 additions & 1 deletion src/game_initialization/multiplayer.cpp
Expand Up @@ -15,6 +15,7 @@
#include "game_initialization/multiplayer.hpp"

#include "addon/manager.hpp" // for installed_addons
#include "desktop/discord_rich_presence.hpp"
#include "events.hpp"
#include "formula/string_utils.hpp"
#include "game_config_manager.hpp"
Expand Down Expand Up @@ -448,16 +449,31 @@ void enter_staging_mode(mp_workflow_helper_ptr helper)
campaign_info->is_host = true;
}

std::string name = "";
bool dlg_ok = false;
{
ng::connect_engine_ptr connect_engine(new ng::connect_engine(helper->state, true, campaign_info.get()));

name = connect_engine->scenario()["name"].str();

desktop::discord_presence presence;
presence.state = "Waiting for Players";
presence.details = name;

presence.send();

gui2::dialogs::mp_staging dlg(*connect_engine, *helper->lobby_info, helper->connection);
dlg.show();
dlg_ok = dlg.get_retval() == gui2::window::OK;
} // end connect_engine_ptr, dlg scope

if(dlg_ok) {
desktop::discord_presence presence;
presence.state = "In Game";
presence.details = name;

presence.send();

campaign_controller controller(helper->state, helper->game_config, game_config_manager::get()->terrain_types());
controller.set_mp_info(campaign_info.get());
controller.play_game();
Expand Down Expand Up @@ -501,6 +517,12 @@ bool enter_lobby_mode(mp_workflow_helper_ptr helper, const std::vector<std::stri

// We use a loop here to allow returning to the lobby if you, say, cancel game creation.
while(true) {
desktop::discord_presence presence;
presence.state = "In Lobby";
presence.details = "Looking for Game";

presence.send();

if(const config& cfg = helper->game_config.child("lobby_music")) {
for(const config& i : cfg.child_range("music")) {
sound::play_music_config(i);
Expand Down Expand Up @@ -577,7 +599,7 @@ bool enter_lobby_mode(mp_workflow_helper_ptr helper, const std::vector<std::stri
/** Pubic entry points for the MP workflow */
namespace mp
{
void start_client(const config& game_config, saved_game& state, const std::string& host)
void start_client(const config& game_config, saved_game& state, const std::string& host)
{
const config* game_config_ptr = &game_config;

Expand Down
8 changes: 8 additions & 0 deletions src/game_initialization/playcampaign.cpp
Expand Up @@ -21,6 +21,7 @@
#include "game_initialization/playcampaign.hpp"

#include "carryover.hpp"
#include "desktop/discord_rich_presence.hpp"
#include "game_config.hpp"
#include "game_errors.hpp"
#include "preferences/game.hpp"
Expand Down Expand Up @@ -262,6 +263,13 @@ LEVEL_RESULT campaign_controller::play_game()

while(state_.valid())
{
desktop::discord_presence presence;
presence.state = "In Game";
presence.details = state_.get_starting_pos()["name"];
presence.large_image_text = state_.classification().campaign_name;

presence.send();

LEVEL_RESULT res = LEVEL_RESULT::VICTORY;
end_level_data end_level;
try {
Expand Down
4 changes: 3 additions & 1 deletion src/game_launcher.cpp
Expand Up @@ -19,6 +19,7 @@
#include "commandline_options.hpp" // for commandline_options
#include "config.hpp" // for config, etc
#include "cursor.hpp" // for set, CURSOR_TYPE::NORMAL
#include "desktop/discord_rich_presence.hpp"
#include "exceptions.hpp" // for error
#include "filesystem.hpp" // for get_user_config_dir, etc
#include "game_classification.hpp" // for game_classification, etc
Expand Down Expand Up @@ -119,7 +120,8 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts, const char
jump_to_multiplayer_(false),
jump_to_campaign_(false, -1, "", ""),
jump_to_editor_(false),
load_data_()
load_data_(),
rp_manager_(new desktop::rich_presence_manager())
{
bool no_music = false;
bool no_sound = false;
Expand Down
2 changes: 2 additions & 0 deletions src/game_launcher.hpp
Expand Up @@ -29,6 +29,7 @@
class commandline_options;
class config;
class CVideo;
namespace desktop { class rich_presence_manager; }
namespace savegame { struct load_game_metadata; }
struct jump_to_campaign_info
{
Expand Down Expand Up @@ -128,4 +129,5 @@ class game_launcher

bool jump_to_editor_;
std::unique_ptr<savegame::load_game_metadata> load_data_;
std::unique_ptr<desktop::rich_presence_manager> rp_manager_;
};
6 changes: 6 additions & 0 deletions src/wesnoth.cpp
Expand Up @@ -17,6 +17,7 @@
#include "commandline_options.hpp" // for commandline_options, etc
#include "config.hpp" // for config, config::error, etc
#include "cursor.hpp" // for set, CURSOR_TYPE::NORMAL, etc
#include "desktop/discord_rich_presence.hpp"
#include "editor/editor_main.hpp"
#include "filesystem.hpp" // for filesystem::file_exists, filesystem::io_exception, etc
#include "floating_label.hpp"
Expand Down Expand Up @@ -746,6 +747,11 @@ static int do_gameloop(const std::vector<std::string>& args)
plugins.set_callback("exit", [](const config& cfg) { safe_exit(cfg["code"].to_int(0)); }, false);

for(;;) {
desktop::discord_presence presence;
presence.state = "In Menu";

presence.send();

// reset the TC, since a game can modify it, and it may be used
// by images in add-ons or campaigns dialogs
image::set_team_colors();
Expand Down

0 comments on commit bf774db

Please sign in to comment.