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
This commit is contained in:
Justin McKinstry
2018-10-31 03:39:40 -05:00
committed by ultrasn0w
parent 170dfb3ae6
commit 757b8e5856
11 changed files with 269 additions and 246 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

@@ -18,10 +18,8 @@ I'm currently no longer actively developing this, but will still merge PRs and r
2. Drop contents from repository in the prevoiusly created \foo_drpc\. 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\. 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\. 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. 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". 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 # License and Warranty
Check [LICENSE](../master/LICENSE). 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,229 +1,209 @@
#include <cmath> #include "Plugin.h"
#include "Plugin.h"
// This tells Foobar2000 users what this component does
DECLARE_COMPONENT_VERSION(
DECLARE_COMPONENT_VERSION( "foo_drpc",
"foo_drpc", "0.3",
"0.3", "Foobar2000 music status for Discord Rich Presence! (c) 2018 - ultrasn0w et al");
"Foobar2000 music status for Discord Rich Presence! (c) 2018 - ultrasn0w");
// This tells Foobar2000 what the file really is even if the user renames it (so only one is loaded)
static initquit_factory_t<foo_drpc> foo_interface; VALIDATE_COMPONENT_FILENAME(FOODRPC_NAME".dll");
static std::chrono::time_point<std::chrono::high_resolution_clock> lastT;
static std::chrono::time_point<std::chrono::high_resolution_clock> req; static initquit_factory_t<foo_drpc> foo_interface;
static bool errored; // Still kind of unused
static bool connected; foo_drpc::foo_drpc()
static bool first; {
// This starts at true because
foo_drpc::foo_drpc() // 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
errored = false; connected = true;
connected = true; }
first = true;
} foo_drpc::~foo_drpc()
{
foo_drpc::~foo_drpc() }
{
} void foo_drpc::on_init()
{
void foo_drpc::on_init() static_api_ptr_t<play_callback_manager> pcm;
{
static_api_ptr_t<play_callback_manager> pcm; DEBUG_CONSOLE_PRINTF("Initializing");
pcm->register_callback(
this, pcm->register_callback(
play_callback::flag_on_playback_starting | this,
play_callback::flag_on_playback_stop | play_callback::flag_on_playback_starting |
play_callback::flag_on_playback_pause | play_callback::flag_on_playback_stop |
play_callback::flag_on_playback_new_track | play_callback::flag_on_playback_pause |
play_callback::flag_on_playback_edited | play_callback::flag_on_playback_new_track |
play_callback::flag_on_playback_dynamic_info_track, play_callback::flag_on_playback_edited |
false); play_callback::flag_on_playback_dynamic_info_track,
discordInit(); false);
initDiscordPresence();
} discord_init();
}
void foo_drpc::on_quit()
{ void foo_drpc::discord_init()
Discord_ClearPresence(); {
Discord_Shutdown(); memset(&handlers, 0, sizeof(handlers));
static_api_ptr_t<play_callback_manager>()->unregister_callback(this); handlers.ready = callback_discord_connected;
} handlers.disconnected = callback_discord_disconnected;
handlers.errored = callback_discord_errored;
void foo_drpc::on_playback_starting(playback_control::t_track_command command, bool pause)
{ Discord_Initialize(APPLICATION_ID, &handlers, 0, NULL);
if (!connected) return;
init_discord_presence();
if (pause) }
{
discordPresence.state = "Paused"; void foo_drpc::init_discord_presence()
discordPresence.smallImageKey = "pause"; {
} memset(&discord_presence, 0, sizeof(discord_presence));
else discord_presence.state = "Initialized";
{ discord_presence.details = "Waiting ...";
switch (command) discord_presence.largeImageKey = "logo";
{ discord_presence.smallImageKey = "stop";
case playback_control::track_command_play:
case playback_control::track_command_next: update_discord_presence();
case playback_control::track_command_prev: }
case playback_control::track_command_resume:
case playback_control::track_command_rand: void foo_drpc::on_quit()
case playback_control::track_command_settrack: {
discordPresence.state = "Listening"; DEBUG_CONSOLE_PRINTF("Unloading");
discordPresence.smallImageKey = "play";
break; Discord_ClearPresence();
} Discord_Shutdown();
}
static_api_ptr_t<play_callback_manager>()->unregister_callback(this);
metadb_handle_ptr track; }
static_api_ptr_t<playback_control> pbc;
if (pbc->get_now_playing(track)) void foo_drpc::on_playback_starting(playback_control::t_track_command command, bool pause)
{ {
on_playback_new_track(track); if (!connected) return;
}
// updateDiscordPresence(); if (pause)
} {
discord_presence.state = "Paused";
void foo_drpc::on_playback_stop(playback_control::t_stop_reason reason) discord_presence.smallImageKey = "pause";
{ }
if (!connected) return; else
{
switch (reason) switch (command)
{ {
case playback_control::stop_reason_user: case playback_control::track_command_play:
case playback_control::stop_reason_eof: case playback_control::track_command_next:
case playback_control::stop_reason_shutting_down: case playback_control::track_command_prev:
discordPresence.state = "Stopped"; case playback_control::track_command_resume:
discordPresence.smallImageKey = "stop"; case playback_control::track_command_rand:
updateDiscordPresence(); case playback_control::track_command_settrack:
break; discord_presence.state = "Listening";
} discord_presence.smallImageKey = "play";
} break;
}
void foo_drpc::on_playback_pause(bool pause) }
{
if (!connected) return; metadb_handle_ptr track;
static_api_ptr_t<playback_control> pbc;
discordPresence.state = (pause ? "Paused" : "Listening"); if (pbc->get_now_playing(track))
discordPresence.smallImageKey = (pause ? "pause" : "play"); {
updateDiscordPresence(); on_playback_new_track(track);
} }
}
void foo_drpc::on_playback_new_track(metadb_handle_ptr track)
{ void foo_drpc::on_playback_stop(playback_control::t_stop_reason reason)
if (!connected) return; {
if (!connected) return;
service_ptr_t<titleformat_object> script;
pfc::string8 format = "%artist% - %title%"; switch (reason)
{
if (static_api_ptr_t<titleformat_compiler>()->compile(script, format)) case playback_control::stop_reason_user:
{ case playback_control::stop_reason_eof:
static_api_ptr_t<playback_control> pbc; case playback_control::stop_reason_shutting_down:
discord_presence.state = "Stopped";
pbc->playback_format_title_ex( discord_presence.smallImageKey = "stop";
track, update_discord_presence();
nullptr, break;
format, }
script, }
nullptr,
playback_control::display_level_titles); void foo_drpc::on_playback_pause(bool pause)
{
if (format.get_length() + 1 <= 128) { if (!connected) return;
static char nya[128];
size_t destination_size = sizeof(nya); discord_presence.state = (pause ? "Paused" : "Listening");
strncpy_s(nya, format.get_ptr(), destination_size); discord_presence.smallImageKey = (pause ? "pause" : "play");
nya[destination_size - 1] = '\0'; update_discord_presence();
}
discordPresence.state = "Listening";
discordPresence.smallImageKey = "play"; void foo_drpc::on_playback_new_track(metadb_handle_ptr track)
discordPresence.details = nya; {
updateDiscordPresence(); if (!connected) return;
}
} service_ptr_t<titleformat_object> script;
} pfc::string8 format = "%artist% - %title%";
void foo_drpc::on_playback_dynamic_info_track(const file_info& info) if (static_api_ptr_t<titleformat_compiler>()->compile(script, format))
{ {
metadb_handle_ptr track; static_api_ptr_t<playback_control> pbc;
static_api_ptr_t<playback_control> pbc;
if (pbc->get_now_playing(track)) pbc->playback_format_title_ex(
{ track,
on_playback_new_track(track); nullptr,
} format,
} script,
nullptr,
void foo_drpc::initDiscordPresence() playback_control::display_level_titles);
{
memset(&discordPresence, 0, sizeof(discordPresence)); // If the details size is bigger than MAX_DETAILS_LENGTH chars, truncate it
discordPresence.state = "Initialized"; const size_t MAX_DETAILS_LENGTH = 128;
discordPresence.details = "Waiting ...";
discordPresence.largeImageKey = "logo"; size_t details_length = min(format.get_length(), MAX_DETAILS_LENGTH-1); // -1 to give us room for the '\0' in the longest case
discordPresence.smallImageKey = "stop"; static char details[MAX_DETAILS_LENGTH];
// discordPresence.partyId = "party1234";
// discordPresence.partySize = 1; strncpy_s(details, format.get_ptr(), details_length);
// discordPresence.partyMax = 6; details[details_length] = '\0';
updateDiscordPresence(); discord_presence.state = (pbc->is_paused() ? "Paused" : "Listening");
} discord_presence.smallImageKey = (pbc->is_paused() ? "pause" : "play");
discord_presence.details = details;
void foo_drpc::updateDiscordPresence()
{ update_discord_presence();
if (first) { }
lastT = std::chrono::high_resolution_clock::now(); }
first = false;
Discord_UpdatePresence(&discordPresence); void foo_drpc::on_playback_dynamic_info_track(const file_info& info)
} {
else { metadb_handle_ptr track;
req = std::chrono::high_resolution_clock::now(); static_api_ptr_t<playback_control> pbc;
std::chrono::duration<double> elapsed = req - lastT; if (pbc->get_now_playing(track))
// spam protection {
if (elapsed.count() > 0.42) { on_playback_new_track(track);
Discord_UpdatePresence(&discordPresence); }
lastT = std::chrono::high_resolution_clock::now(); }
}
} void foo_drpc::update_discord_presence()
{
Discord_UpdatePresence(&discord_presence);
#ifdef DISCORD_DISABLE_IO_THREAD #ifdef DISCORD_DISABLE_IO_THREAD
Discord_UpdateConnection(); Discord_UpdateConnection();
#endif #endif
Discord_RunCallbacks(); 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(const DiscordUser* request) }
{
connected = true; void callback_discord_connected(const DiscordUser* request)
} {
foo_interface.get_static_instance().connected = true;
void disconnectedF(int errorCode, const char* message) DEBUG_CONSOLE_PRINTF("Connected to %s.", request->username);
{ }
connected = false;
} void callback_discord_disconnected(int errorCode, const char* message)
{
void erroredF(int errorCode, const char* message) foo_interface.get_static_instance().connected = false;
{ DEBUG_CONSOLE_PRINTF("Disconnected (%i): %s.", errorCode, message);
errored = true; }
}
void callback_discord_errored(int errorCode, const char* message)
void foo_drpc::discordInit() {
{ console::printf("*** Error %i: %s.", errorCode, message);
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;
}

View File

@@ -2,8 +2,17 @@
#define FOODRPC_PLUGIN_H_ #define FOODRPC_PLUGIN_H_
#include "../../SDK/foobar2000.h" #include "../../SDK/foobar2000.h"
#include "discord-rpc.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 : class foo_drpc :
public initquit, public initquit,
@@ -11,32 +20,44 @@ class foo_drpc :
{ {
public: public:
foo_drpc(); foo_drpc();
~foo_drpc(); virtual ~foo_drpc();
DiscordEventHandlers handlers; DiscordEventHandlers handlers;
DiscordRichPresence discordPresence; DiscordRichPresence discord_presence;
// Censored on GitHub :)
const char* APPLICATION_ID = "FILL_IN_HERE";
// 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_init();
void on_quit(); void on_quit();
void discordInit(); // Foobar2000 callback functions
void initDiscordPresence();
void updateDiscordPresence();
LPSTR UnicodeToAnsi(LPCWSTR s);
void on_playback_starting(play_control::t_track_command command, bool paused); 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_stop(play_control::t_stop_reason reason);
void on_playback_pause(bool state); void on_playback_pause(bool state);
void on_playback_new_track(metadb_handle_ptr track); 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_edited(metadb_handle_ptr track) { on_playback_new_track(track); }
void on_playback_dynamic_info_track(const file_info& info); void on_playback_dynamic_info_track(const file_info& info);
// Foobar2000 callback stubs
void on_playback_time(double time) {} void on_playback_time(double time) {}
void on_playback_seek(double time) {} void on_playback_seek(double time) {}
void on_playback_dynamic_info(file_info const& info) {} void on_playback_dynamic_info(file_info const& info) {}
void on_volume_change(float p_new_val) {} 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 #endif

View File

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