Skip to content

Commit

Permalink
Refactor of the presence integration & Discord RPC (#1588)
Browse files Browse the repository at this point in the history
* Refactor of the presence integration & Discord RPC

* Removed Discord's example "send-presence" from CMake

* Fixed missing include

Co-authored-by: Semphris <semphris@protonmail.com>
  • Loading branch information
Semphriss and Semphris committed Dec 8, 2020
1 parent 392320b commit 43ad4ae
Show file tree
Hide file tree
Showing 27 changed files with 239 additions and 251 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ option(ENABLE_DISCORD "Compile the Discord integration" ON)
if(ENABLE_DISCORD)
message(STATUS "Discord WILL be compiled. To disable Discord, pass -DENABLE_DISCORD=Off")
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/external/discord-sdk/include)
set(BUILD_EXAMPLES OFF)
add_subdirectory(external/discord-sdk)
else(ENABLE_DISCORD)
message(STATUS "WARNING : Discord is NOT to be compiled. To enable Discord, pass -DENABLE_DISCORD=On")
Expand Down
46 changes: 19 additions & 27 deletions src/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,10 @@ Editor::test_level(const boost::optional<std::pair<std::string, Vector>>& test_p

if (!m_level->is_worldmap())
{
Integration::set_level(m_level->get_name().c_str());
Integration::set_status(TESTING_LEVEL);
GameManager::current()->start_level(*current_world, backup_filename, test_pos);
}
else
{
Integration::set_worldmap(m_level->get_name().c_str());
Integration::set_status(TESTING_WORLDMAP);
GameManager::current()->start_worldmap(*current_world, "", m_autosave_levelfile);
}

Expand All @@ -336,7 +332,6 @@ Editor::open_level_directory()
void
Editor::set_world(std::unique_ptr<World> w)
{
Integration::set_worldmap(w->get_title().c_str());
m_world = std::move(w);
}

Expand Down Expand Up @@ -456,17 +451,6 @@ Editor::delete_current_sector()
void
Editor::set_level(std::unique_ptr<Level> level, bool reset)
{
if (level->is_worldmap())
{
Integration::set_worldmap(level->get_name().c_str());
Integration::set_status(EDITING_WORLDMAP);
}
else
{
Integration::set_level(level->get_name().c_str());
Integration::set_status(EDITING_LEVEL);
}

std::string sector_name = "main";
Vector translation;

Expand Down Expand Up @@ -549,8 +533,6 @@ Editor::quit_editor()
m_enabled = false;
Tile::draw_editor_images = false;
ScreenManager::current()->pop_screen();

Integration::set_status(MAIN_MENU);
};

check_unsaved_changes([quit] {
Expand Down Expand Up @@ -643,15 +625,6 @@ Editor::setup()
m_deactivate_request = false;
m_enabled = true;
m_toolbox_widget->update_mouse_icon();

if (m_level->is_worldmap())
{
Integration::set_status(EDITING_WORLDMAP);
}
else
{
Integration::set_status(EDITING_LEVEL);
}
}

}
Expand Down Expand Up @@ -863,4 +836,23 @@ Editor::redo()
}
}

IntegrationStatus
Editor::get_status() const
{
IntegrationStatus status;
status.m_details.push_back("In Editor");
if (!g_config->hide_editor_levelnames)
{
if (m_level->is_worldmap())
{
status.m_details.push_back("Editing worldmap: " + m_level->get_name());
}
else
{
status.m_details.push_back("Editing level: " + m_level->get_name());
}
}
return status;
}

/* EOF */
2 changes: 2 additions & 0 deletions src/editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Editor final : public Screen,
virtual void setup() override;
virtual void leave() override;

virtual IntegrationStatus get_status() const override;

void event(const SDL_Event& ev);
void resize();

Expand Down
141 changes: 24 additions & 117 deletions src/sdk/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,28 @@ extern "C" {
} // extern "C"


DiscordIntegration* DiscordIntegration::singleton;
DiscordIntegration* DiscordIntegration::driver;

DiscordIntegration::DiscordIntegration() :
m_status(IntegrationStatus::MAIN_MENU),
m_level(),
m_worldmap(),
Integration(),
m_enabled(false)
{
}

DiscordIntegration::~DiscordIntegration()
{
// It shouldn't get here, but just in case
close();
}

DiscordIntegration*
DiscordIntegration::getSingleton()
DiscordIntegration::getDriver()
{
if (!singleton)
if (!driver)
{
singleton = new DiscordIntegration();
driver = new DiscordIntegration();
}
return singleton;
return driver;
}

void
Expand All @@ -120,11 +120,9 @@ DiscordIntegration::init()
handlers.joinRequest = handleDiscordJoinRequest;
Discord_Initialize("733576109744062537", &handlers, 1, nullptr);

update_discord_presence();
log_info << "[Discord] Started" << std::endl;

m_enabled = true;

log_info << "[Discord] Started" << std::endl;
}

void
Expand All @@ -146,13 +144,13 @@ DiscordIntegration::close()
Discord_ClearPresence();
Discord_Shutdown();

m_enabled = false;

log_info << "[Discord] Closed" << std::endl;

m_enabled = false;
}

void
DiscordIntegration::update_discord_presence()
DiscordIntegration::update_status(IntegrationStatus status)
{
if (!m_enabled) return;

Expand All @@ -176,115 +174,24 @@ DiscordIntegration::update_discord_presence()
* instance
*/

char state_buffer[256];
if (status.m_details.size() >= 1)
discordPresence.state = status.m_details.begin()->c_str();

switch(m_status)
{
case IntegrationStatus::MAIN_MENU:
discordPresence.state = "Main Menu";
break;

case IntegrationStatus::PLAYING_WORLDMAP:
sprintf(state_buffer, "In worldmap: %s", m_worldmap);
discordPresence.state = state_buffer;
discordPresence.smallImageKey = "play";
break;

case IntegrationStatus::PLAYING_LEVEL:
case IntegrationStatus::PLAYING_LEVEL_FROM_WORLDMAP:
sprintf(state_buffer, "In level: %s", m_level);
discordPresence.state = state_buffer;
discordPresence.details = m_worldmap;
discordPresence.smallImageKey = "play";
break;

case IntegrationStatus::EDITING_WORLDMAP:
if (!g_config->discord_hide_editor)
{
sprintf(state_buffer, "Editing worldmap: %s", m_worldmap);
discordPresence.state = state_buffer;
}
else
{
discordPresence.state = "In editor";
}
discordPresence.smallImageKey = "edit";
break;

case IntegrationStatus::EDITING_LEVEL:
if (!g_config->discord_hide_editor)
{
sprintf(state_buffer, "Editing level: %s", m_level);
discordPresence.state = state_buffer;
discordPresence.details = m_worldmap;
}
else
{
discordPresence.state = "In editor";
}
discordPresence.smallImageKey = "edit";
break;

case IntegrationStatus::TESTING_WORLDMAP:
if (!g_config->discord_hide_editor)
{
sprintf(state_buffer, "Testing worldmap: %s", m_worldmap);
discordPresence.state = state_buffer;
}
else
{
discordPresence.state = "In Level Editor";
}
discordPresence.smallImageKey = "edit";
break;

case IntegrationStatus::TESTING_LEVEL:
case IntegrationStatus::TESTING_LEVEL_FROM_WORLDMAP:
if (!g_config->discord_hide_editor)
{
sprintf(state_buffer, "Testing level: %s", m_level);
discordPresence.state = state_buffer;
discordPresence.details = m_worldmap;
}
else
{
discordPresence.state = "In editor";
}
discordPresence.smallImageKey = "edit";
break;
if (status.m_details.size() >= 2)
discordPresence.details = (status.m_details.begin() + 1)->c_str();

default:
break;
if (status.m_timestamp != 0) {
if (status.m_timestamp > time(nullptr)) {
discordPresence.endTimestamp = status.m_timestamp;
} else {
discordPresence.startTimestamp = status.m_timestamp;
}
}

// TODO: Manage parties and all

discordPresence.largeImageKey = "supertux_logo";
discordPresence.startTimestamp = time(nullptr); // TODO: Option to disable timers?
Discord_UpdatePresence(&discordPresence);

}

void
DiscordIntegration::update_status(IntegrationStatus status)
{
m_status = status;

if (!m_enabled) return;
update_discord_presence();
}

void
DiscordIntegration::update_worldmap(const char* worldmap)
{
m_worldmap = new char[strlen(worldmap) + 1];
strcpy(m_worldmap, worldmap);
}

void
DiscordIntegration::update_level(const char* level)
{
printf("%s\n", level);
m_level = new char[strlen(level) + 1];
strcpy(m_level, level);
}

#endif
Expand Down
16 changes: 3 additions & 13 deletions src/sdk/discord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,23 @@

#include "sdk/integration.hpp"

class DiscordIntegration : public Integration
class DiscordIntegration final : public Integration
{
public:
static DiscordIntegration* getSingleton();
static DiscordIntegration* getDriver();

public:
virtual void init() override;
virtual void update() override;
virtual void close() override;
virtual void update_status(IntegrationStatus status) override;
virtual void update_worldmap(const char* worldmap) override;
virtual void update_level(const char* level) override;

protected:
DiscordIntegration();
~DiscordIntegration();

private:
void update_discord_presence();

static DiscordIntegration* singleton;

private:
IntegrationStatus m_status;
char* m_level;
char* m_worldmap;

static DiscordIntegration* driver;
bool m_enabled;

private:
Expand Down

0 comments on commit 43ad4ae

Please sign in to comment.