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 29, 2018
1 parent d6bbdc3 commit 3514b94
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 3 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
78 changes: 78 additions & 0 deletions src/desktop/discord_rich_presence.cpp
@@ -0,0 +1,78 @@
/*
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 <discord-rpc.h>

#include <algorithm>
#include <cstring>

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

void callback_disconnected(int errorCode, const char* message)
{

}

} // end anon namespace

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

// FIXME
handlers.ready = nullptr;
handlers.errored = nullptr;
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();
}

void rich_presence_manager::update_presence(
const std::string state,
const std::string details,
const unsigned party_size,
const unsigned party_max) const
{
DiscordRichPresence data;
std::memset(&data, 0, sizeof(data));

data.state = state.c_str();
data.details = details.c_str();
data.largeImageKey = "weslogo";
data.partySize = party_size;
data.partyMax = party_max;

Discord_UpdatePresence(&data);
}

} // namespace desktop1
35 changes: 35 additions & 0 deletions src/desktop/discord_rich_presence.hpp
@@ -0,0 +1,35 @@
/*
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 <string>

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

void update_presence(
const std::string state,
const std::string details,
const unsigned party_size = 0,
const unsigned party_max = 0) const;

private:
};

} // namespace desktop
8 changes: 7 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 @@ -371,6 +372,7 @@ struct mp_workflow_helper
, state(state)
, connection(connection)
, lobby_info(li)
, rp_manager()
{}

const config& game_config;
Expand All @@ -380,6 +382,8 @@ struct mp_workflow_helper
wesnothd_connection* connection;

mp::lobby_info* lobby_info;

desktop::rich_presence_manager rp_manager;
};

using mp_workflow_helper_ptr = std::shared_ptr<mp_workflow_helper>;
Expand Down Expand Up @@ -499,6 +503,8 @@ bool enter_lobby_mode(mp_workflow_helper_ptr helper, const std::vector<std::stri
// Connection should never be null in the lobby.
assert(helper->connection);

helper->rp_manager.update_presence("In Lobby", "Looking for Game");

// We use a loop here to allow returning to the lobby if you, say, cancel game creation.
while(true) {
if(const config& cfg = helper->game_config.child("lobby_music")) {
Expand Down Expand Up @@ -577,7 +583,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

0 comments on commit 3514b94

Please sign in to comment.