8 Commits

Author SHA1 Message Date
Justin McKinstry
757b8e5856 Merge jmckinstry's Pull request (#15)
- Look and naming (tried to unify things a bit)
- .gitignore for ignoring files we don't want to track
- Moved APPLICATION_ID to somewhere that could be made safe for Github without making life harder
- Fixed the bug with showing the wrong play state when loading FB2K
- Fixed the bug where tracks with really long titles would cause janky behavior
- Added unencumbered images if folks didn't want to make their own
- Added DEBUG_CONSOLE_PRINTF calls for debugging
- Removed rate limiting that was already handled in Discord's library code
- Removed UnicodeToAnsi as it was unused
2018-10-31 09:39:40 +01:00
ultrasn0w
170dfb3ae6 Whew 2018-09-04 17:26:51 +02:00
ultrasn0w
3c02c9dd95 Update README.md 2018-09-04 17:26:31 +02:00
ultrasn0w
956f6ba8ec Update README.md 2018-09-04 17:26:10 +02:00
ultrasn0w
0bb4f8080c Update README.md 2018-09-04 17:15:58 +02:00
ultrasn0w
579e7cbb7c Merge pull request #13 from darktohka/master
Update Discord RPC to v3.3.0 and fix metadata double click bug
2018-09-04 16:54:23 +02:00
darktohka
fa59966d6c Update Discord RPC to v3.3.0 and fix metadata double click bug 2018-09-04 05:34:38 +03:00
ultrasn0w
da6b7ec8ed Update README.md 2018-02-11 15:58:42 +01:00
11 changed files with 274 additions and 248 deletions

7
.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
.vs
Debug
Release
foo_drpc/Debug
foo_drpc/Release
foo_drpc/discord-rpc.h
foo_drpc/lib

View File

@@ -1,9 +1,12 @@
# foo_drpc
Foobar2000 music status for Discord Rich Presence!
# Notice
I'm currently no longer actively developing this, but will still merge PRs and reference releases at the [release page](https://github.com/ultrasn0w/foo_drpc/releases).
# How to use
1. Grab release, drop included **foo_drpc** directory in \%userdir%\AppData\Roaming\foobar2000\user-components\ or place included .dll Files in \foobar2000\components\.
2. Add foobar2000 to discords detected games (Settings -> Games -> Add it).
1. Grab [release](https://github.com/ultrasn0w/foo_drpc/releases), drop included **foo_drpc** directory in \%userdir%\AppData\Roaming\foobar2000\user-components\ (if you have not moved your AppData somewhere else) or place included .dll Files in \foobar2000\components\.
2. ~~Add foobar2000 to discords detected games (Settings -> Games -> Add it).~~
![compact view](/foo_drpc1.PNG?raw=true)
![big view 1](/foo_drpc2.PNG?raw=true)
@@ -15,10 +18,8 @@ Foobar2000 music status for Discord Rich Presence!
2. Drop contents from repository in the prevoiusly created \foo_drpc\.
3. Grab release from https://github.com/discordapp/discord-rpc and place \lib\ with contained discord-rpc.lib in \foo_drpc\.
4. Do the same with \include\discord-rpc.h but this time directly into \foo_drpc\.
5. Get/Create a Discord Application ID which resembles your App at Discords end and fill in to \Plugin.h.
6. Upload 1 large asset for your App with the key "logo", 3 small ones with keys "play", "stop" and "pause".
5. Get/Create a Discord Application ID which resembles your App at Discords end and fill in APPLICATION_ID in \foo_drpc\secret.h.
6. Upload 1 large asset for your App with the key "logo", 3 small ones with keys "play", "stop" and "pause". Use the files in optional_images if you like.
# License and Warranty
Check [LICENSE](../master/LICENSE).
Note: Even though I build in a "spam protection" to avoid lots of presence updates being send to the discord servers, I can't guarantee and am not responsible for any actions that may be taken against your account. (Nothing happened during personal testing)

View File

@@ -1,24 +1,22 @@
#include <cmath>
#include "Plugin.h"
// This tells Foobar2000 users what this component does
DECLARE_COMPONENT_VERSION(
"foo_drpc",
"0.3",
"Foobar2000 music status for Discord Rich Presence! (c) 2018 - ultrasn0w");
"Foobar2000 music status for Discord Rich Presence! (c) 2018 - ultrasn0w et al");
// This tells Foobar2000 what the file really is even if the user renames it (so only one is loaded)
VALIDATE_COMPONENT_FILENAME(FOODRPC_NAME".dll");
static initquit_factory_t<foo_drpc> foo_interface;
static std::chrono::time_point<std::chrono::high_resolution_clock> lastT;
static std::chrono::time_point<std::chrono::high_resolution_clock> req;
static bool errored; // Still kind of unused
static bool connected;
static bool first;
foo_drpc::foo_drpc()
{
errored = false;
// This starts at true because
// 1) Discord will not call its connected callback if you start this plugin and it's already running and
// 2) it costs us very little to write updates into the void
connected = true;
first = true;
}
foo_drpc::~foo_drpc()
@@ -28,6 +26,9 @@ foo_drpc::~foo_drpc()
void foo_drpc::on_init()
{
static_api_ptr_t<play_callback_manager> pcm;
DEBUG_CONSOLE_PRINTF("Initializing");
pcm->register_callback(
this,
play_callback::flag_on_playback_starting |
@@ -37,14 +38,40 @@ void foo_drpc::on_init()
play_callback::flag_on_playback_edited |
play_callback::flag_on_playback_dynamic_info_track,
false);
discordInit();
initDiscordPresence();
discord_init();
}
void foo_drpc::discord_init()
{
memset(&handlers, 0, sizeof(handlers));
handlers.ready = callback_discord_connected;
handlers.disconnected = callback_discord_disconnected;
handlers.errored = callback_discord_errored;
Discord_Initialize(APPLICATION_ID, &handlers, 0, NULL);
init_discord_presence();
}
void foo_drpc::init_discord_presence()
{
memset(&discord_presence, 0, sizeof(discord_presence));
discord_presence.state = "Initialized";
discord_presence.details = "Waiting ...";
discord_presence.largeImageKey = "logo";
discord_presence.smallImageKey = "stop";
update_discord_presence();
}
void foo_drpc::on_quit()
{
DEBUG_CONSOLE_PRINTF("Unloading");
Discord_ClearPresence();
Discord_Shutdown();
static_api_ptr_t<play_callback_manager>()->unregister_callback(this);
}
@@ -54,8 +81,8 @@ void foo_drpc::on_playback_starting(playback_control::t_track_command command, b
if (pause)
{
discordPresence.state = "Paused";
discordPresence.smallImageKey = "pause";
discord_presence.state = "Paused";
discord_presence.smallImageKey = "pause";
}
else
{
@@ -67,8 +94,8 @@ void foo_drpc::on_playback_starting(playback_control::t_track_command command, b
case playback_control::track_command_resume:
case playback_control::track_command_rand:
case playback_control::track_command_settrack:
discordPresence.state = "Listening";
discordPresence.smallImageKey = "play";
discord_presence.state = "Listening";
discord_presence.smallImageKey = "play";
break;
}
}
@@ -79,7 +106,6 @@ void foo_drpc::on_playback_starting(playback_control::t_track_command command, b
{
on_playback_new_track(track);
}
// updateDiscordPresence();
}
void foo_drpc::on_playback_stop(playback_control::t_stop_reason reason)
@@ -91,20 +117,20 @@ void foo_drpc::on_playback_stop(playback_control::t_stop_reason reason)
case playback_control::stop_reason_user:
case playback_control::stop_reason_eof:
case playback_control::stop_reason_shutting_down:
discordPresence.state = "Stopped";
discordPresence.smallImageKey = "stop";
discord_presence.state = "Stopped";
discord_presence.smallImageKey = "stop";
update_discord_presence();
break;
}
updateDiscordPresence();
}
void foo_drpc::on_playback_pause(bool pause)
{
if (!connected) return;
discordPresence.state = (pause ? "Paused" : "Listening");
discordPresence.smallImageKey = (pause ? "pause" : "play");
updateDiscordPresence();
discord_presence.state = (pause ? "Paused" : "Listening");
discord_presence.smallImageKey = (pause ? "pause" : "play");
update_discord_presence();
}
void foo_drpc::on_playback_new_track(metadb_handle_ptr track)
@@ -126,17 +152,20 @@ void foo_drpc::on_playback_new_track(metadb_handle_ptr track)
nullptr,
playback_control::display_level_titles);
if (format.get_length() + 1 <= 128) {
static char nya[128];
size_t destination_size = sizeof(nya);
strncpy_s(nya, format.get_ptr(), destination_size);
nya[destination_size - 1] = '\0';
// If the details size is bigger than MAX_DETAILS_LENGTH chars, truncate it
const size_t MAX_DETAILS_LENGTH = 128;
discordPresence.state = "Listening";
discordPresence.smallImageKey = "play";
discordPresence.details = nya;
updateDiscordPresence();
}
size_t details_length = min(format.get_length(), MAX_DETAILS_LENGTH-1); // -1 to give us room for the '\0' in the longest case
static char details[MAX_DETAILS_LENGTH];
strncpy_s(details, format.get_ptr(), details_length);
details[details_length] = '\0';
discord_presence.state = (pbc->is_paused() ? "Paused" : "Listening");
discord_presence.smallImageKey = (pbc->is_paused() ? "pause" : "play");
discord_presence.details = details;
update_discord_presence();
}
}
@@ -150,80 +179,31 @@ void foo_drpc::on_playback_dynamic_info_track(const file_info& info)
}
}
void foo_drpc::initDiscordPresence()
void foo_drpc::update_discord_presence()
{
memset(&discordPresence, 0, sizeof(discordPresence));
discordPresence.state = "Initialized";
discordPresence.details = "Waiting ...";
discordPresence.largeImageKey = "logo";
discordPresence.smallImageKey = "stop";
// discordPresence.partyId = "party1234";
// discordPresence.partySize = 1;
// discordPresence.partyMax = 6;
Discord_UpdatePresence(&discord_presence);
updateDiscordPresence();
}
void foo_drpc::updateDiscordPresence()
{
if (first) {
lastT = std::chrono::high_resolution_clock::now();
first = false;
Discord_UpdatePresence(&discordPresence);
}
else {
req = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = req - lastT;
// spam protection
if (elapsed.count() > 0.42) {
Discord_UpdatePresence(&discordPresence);
lastT = std::chrono::high_resolution_clock::now();
}
}
#ifdef DISCORD_DISABLE_IO_THREAD
Discord_UpdateConnection();
#endif
Discord_RunCallbacks();
DEBUG_CONSOLE_PRINTF("Ran Discord presence update: %s, %s, %s, %s", discord_presence.state, discord_presence.details, discord_presence.largeImageKey, discord_presence.smallImageKey);
}
void connectedF()
void callback_discord_connected(const DiscordUser* request)
{
connected = true;
foo_interface.get_static_instance().connected = true;
DEBUG_CONSOLE_PRINTF("Connected to %s.", request->username);
}
void disconnectedF(int errorCode, const char* message)
void callback_discord_disconnected(int errorCode, const char* message)
{
connected = false;
foo_interface.get_static_instance().connected = false;
DEBUG_CONSOLE_PRINTF("Disconnected (%i): %s.", errorCode, message);
}
void erroredF(int errorCode, const char* message)
void callback_discord_errored(int errorCode, const char* message)
{
errored = true;
}
void foo_drpc::discordInit()
{
memset(&handlers, 0, sizeof(handlers));
handlers.ready = connectedF;
handlers.disconnected = disconnectedF;
handlers.errored = erroredF;
// handlers.joinGame = [](const char* joinSecret) {};
// handlers.spectateGame = [](const char* spectateSecret) {};
// handlers.joinRequest = [](const DiscordJoinRequest* request) {};
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
}
// thx SuperKoko (unused)
LPSTR foo_drpc::UnicodeToAnsi(LPCWSTR s)
{
if (s == NULL) return NULL;
int cw = lstrlenW(s);
if (cw == 0) { CHAR *psz = new CHAR[1]; *psz = '\0'; return psz; }
int cc = WideCharToMultiByte(CP_UTF8, 0, s, cw, NULL, 0, NULL, NULL);
if (cc == 0) return NULL;
CHAR *psz = new CHAR[cc + 1];
cc = WideCharToMultiByte(CP_UTF8, 0, s, cw, psz, cc, NULL, NULL);
if (cc == 0) { delete[] psz; return NULL; }
psz[cc] = '\0';
return psz;
console::printf("*** Error %i: %s.", errorCode, message);
}

View File

@@ -3,7 +3,16 @@
#include "../../SDK/foobar2000.h"
#include "discord-rpc.h"
#include <chrono>
#include "secret.h"
#define FOODRPC_NAME "foo_drpc"
#if defined(_DEBUG) || defined(_FOODRPC_WITH_CONSOLE_LOGGING)
#define FOODRPC_CONSOLE_HEADER FOODRPC_NAME ": "
#define DEBUG_CONSOLE_PRINTF(...) console::printf(FOODRPC_CONSOLE_HEADER __VA_ARGS__)
#else
#define DEBUG_CONSOLE_PRINTF(...) {}
#endif
class foo_drpc :
public initquit,
@@ -11,32 +20,44 @@ class foo_drpc :
{
public:
foo_drpc();
~foo_drpc();
virtual ~foo_drpc();
DiscordEventHandlers handlers;
DiscordRichPresence discordPresence;
// Censored on GitHub :)
const char* APPLICATION_ID = "FILL_IN_HERE";
DiscordRichPresence discord_presence;
// Otherwise known as CLIENT_ID by Discord documentation
// SET THIS IN "secret.h"
const char* APPLICATION_ID = _FOODRPC_SECRED_APPLICATION_ID;
bool connected; // If Discord integrator is connected to a running Discord instance
// Foobar2000 component setup and teardown
void on_init();
void on_quit();
void discordInit();
void initDiscordPresence();
void updateDiscordPresence();
LPSTR UnicodeToAnsi(LPCWSTR s);
// Foobar2000 callback functions
void on_playback_starting(play_control::t_track_command command, bool paused);
void on_playback_stop(play_control::t_stop_reason reason);
void on_playback_pause(bool state);
void on_playback_new_track(metadb_handle_ptr track);
void on_playback_edited(metadb_handle_ptr track) { on_playback_new_track(track); }
void on_playback_dynamic_info_track(const file_info& info);
// Foobar2000 callback stubs
void on_playback_time(double time) {}
void on_playback_seek(double time) {}
void on_playback_dynamic_info(file_info const& info) {}
void on_volume_change(float p_new_val) {}
// Discord integration helpers
void discord_init();
void init_discord_presence();
void update_discord_presence();
};
// Discord callback functions for notifications of changes
void callback_discord_connected(const DiscordUser* request);
void callback_discord_disconnected(int errorCode, const char* message);
void callback_discord_errored(int errorCode, const char* message);
#endif

View File

@@ -54,7 +54,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;FOO_DRPC_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@@ -72,7 +72,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;FOO_DRPC_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<ExceptionHandling>false</ExceptionHandling>
<BufferSecurityCheck>false</BufferSecurityCheck>

6
foo_drpc/secret.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef FOODRPC_SECRET_H_
#define FOODRPC_SECRET_H_
#define _FOODRPC_SECRED_APPLICATION_ID "FILL_ME_IN"
#endif

View File

@@ -0,0 +1,11 @@
logo.png:
Original: https://en.wikipedia.org/wiki/File:Foobar2000_logo_2014.png
Author: Florian Trendelenburg
License: Public Domain
Changes: Resized, borders changed
play.png, pause.png, stop.png:
Original: http://pluspng.com/png-24756.html
Author: Unlisted
License: Attribution
Changes: Split apart, resized, borders changed

BIN
optional_images/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

BIN
optional_images/pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
optional_images/play.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
optional_images/stop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB