Skip to content

Commit

Permalink
richer discord presence using boxart from thegamesdb
Browse files Browse the repository at this point in the history
  • Loading branch information
flyinghead committed Mar 3, 2024
1 parent 62d0057 commit b0a268b
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 10 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -717,13 +717,13 @@ endif()
target_sources(${PROJECT_NAME} PRIVATE core/deps/xbrz/xbrz.cpp)
target_sources(${PROJECT_NAME} PRIVATE core/deps/md5/md5.cpp)

if(USE_DISCORD)
if(USE_DISCORD AND NOT LIBRETRO)
option(BUILD_EXAMPLES "Build example apps" OFF)
add_subdirectory(core/deps/discord-rpc)
target_include_directories(${PROJECT_NAME} PRIVATE core/deps/discord-rpc/include)
target_link_libraries(${PROJECT_NAME} PRIVATE discord-rpc)
target_compile_definitions(${PROJECT_NAME} PRIVATE USE_DISCORD)
target_sources(${PROJECT_NAME} PRIVATE core/discord.cpp)
target_sources(${PROJECT_NAME} PRIVATE core/rend/discord.cpp)
endif()

cmrc_add_resource_library(flycast-resources ALIAS flycast::res NAMESPACE flycast)
Expand Down
5 changes: 4 additions & 1 deletion core/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ void Emulator::loadGame(const char *path, LoadProgress *progress)
{
hostfs::FileInfo info = hostfs::storage().getFileInfo(settings.content.path);
settings.content.fileName = info.name;
settings.content.title = get_file_basename(info.name);
if (settings.content.title.empty())
settings.content.title = get_file_basename(info.name);
}
}
else
Expand Down Expand Up @@ -615,6 +616,7 @@ void Emulator::unloadGame()
settings.content.path.clear();
settings.content.gameId.clear();
settings.content.fileName.clear();
settings.content.title.clear();
settings.platform.system = DC_PLATFORM_DREAMCAST;
state = Init;
EventManager::event(Event::Terminate);
Expand Down Expand Up @@ -760,6 +762,7 @@ void Emulator::setNetworkState(bool online)
config::Sh4Clock.override(200);
sh4_cpu.ResetCache();
}
EventManager::event(Event::Network);
}
settings.input.fastForwardMode &= !online;
}
Expand Down
1 change: 1 addition & 0 deletions core/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum class Event {
Terminate,
LoadState,
VBlank,
Network,
};

class EventManager
Expand Down
13 changes: 13 additions & 0 deletions core/rend/boxart/boxart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
#include <chrono>

GameBoxart Boxart::getBoxart(const GameMedia& media)
{
loadDatabase();
GameBoxart boxart;
{
std::lock_guard<std::mutex> guard(mutex);
auto it = games.find(media.fileName);
if (it != games.end())
boxart = it->second;
}
return boxart;
}

GameBoxart Boxart::getBoxartAndLoad(const GameMedia& media)
{
loadDatabase();
GameBoxart boxart;
Expand Down
1 change: 1 addition & 0 deletions core/rend/boxart/boxart.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct GameMedia;
class Boxart
{
public:
GameBoxart getBoxartAndLoad(const GameMedia& media);
GameBoxart getBoxart(const GameMedia& media);
void saveDatabase(bool internal = false);

Expand Down
7 changes: 6 additions & 1 deletion core/rend/boxart/gamesdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,14 @@ void TheGamesDb::parseBoxart(GameBoxart& item, const json& j, int gameId)
{
copyFile(cached->second, filename);
item.setBoxartPath(filename);
item.boxartUrl = url;
}
else
{
if (downloadImage(url, filename))
{
item.setBoxartPath(filename);
item.boxartUrl = url;
boxartCache[url] = filename;
}
}
Expand Down Expand Up @@ -394,8 +396,11 @@ void TheGamesDb::scrape(std::vector<GameBoxart>& items)
else if (item.gamePath.empty())
{
std::string localPath = makeUniqueFilename("dreamcast_logo_grey.png");
if (downloadImage("https://flyinghead.github.io/flycast-builds/dreamcast_logo_grey.png", localPath))
std::string biosArtUrl{ "https://flyinghead.github.io/flycast-builds/dreamcast_logo_grey.png" };
if (downloadImage(biosArtUrl, localPath)) {
item.setBoxartPath(localPath);
item.boxartUrl = biosArtUrl;
}
}
item.scraped = true;
}
Expand Down
3 changes: 3 additions & 0 deletions core/rend/boxart/scraper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct GameBoxart

std::string gamePath;
std::string boxartPath;
std::string boxartUrl;

bool parsed = false;
bool scraped = false;
Expand All @@ -56,6 +57,7 @@ struct GameBoxart
{ "release_date", releaseDate },
{ "overview", overview },
{ "boxart_path", boxartPath },
{ "boxart_url", boxartUrl },
{ "parsed", parsed },
{ "scraped", scraped },
};
Expand Down Expand Up @@ -84,6 +86,7 @@ struct GameBoxart
loadProperty(releaseDate, j, "release_date");
loadProperty(overview, j, "overview");
loadProperty(boxartPath, j, "boxart_path");
loadProperty(boxartUrl, j, "boxart_url");
loadProperty(parsed, j, "parsed");
loadProperty(scraped, j, "scraped");
}
Expand Down
29 changes: 25 additions & 4 deletions core/discord.cpp → core/rend/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "emulator.h"
#include "stdclass.h"
#include "cfg/option.h"
#include "gui.h"
#include "discord_rpc.h"
#include <cstring>
#include <time.h>
Expand All @@ -34,6 +35,7 @@ class DiscordPresence
EventManager::listen(Event::Start, handleEmuEvent, this);
EventManager::listen(Event::Terminate, handleEmuEvent, this);
EventManager::listen(Event::Resume, handleEmuEvent, this);
EventManager::listen(Event::Network, handleEmuEvent, this);
}

~DiscordPresence()
Expand All @@ -42,6 +44,7 @@ class DiscordPresence
EventManager::unlisten(Event::Start, handleEmuEvent, this);
EventManager::unlisten(Event::Terminate, handleEmuEvent, this);
EventManager::unlisten(Event::Resume, handleEmuEvent, this);
EventManager::unlisten(Event::Network, handleEmuEvent, this);
}

private:
Expand All @@ -63,10 +66,23 @@ class DiscordPresence
{
initialize();
DiscordRichPresence discordPresence{};
std::string state = settings.content.title.substr(0, 128);
discordPresence.state = state.c_str();
discordPresence.startTimestamp = time(0);
discordPresence.largeImageKey = "icon-512";
discordPresence.state = settings.content.title.c_str();
discordPresence.startTimestamp = startTimestamp;
std::string imageUrl = gui_getCurGameBoxartUrl();
if (!imageUrl.empty())
{
discordPresence.largeImageKey = imageUrl.c_str();
discordPresence.largeImageText = settings.content.title.c_str();
discordPresence.smallImageKey = "icon-512";
discordPresence.smallImageText = "Flycast is a Dreamcast, Naomi and Atomiswave emulator";
}
else
{
discordPresence.largeImageKey = "icon-512";
discordPresence.largeImageText = "Flycast is a Dreamcast, Naomi and Atomiswave emulator";
}
if (settings.network.online)
discordPresence.details = "Online";
Discord_UpdatePresence(&discordPresence);
}

Expand All @@ -78,6 +94,9 @@ class DiscordPresence
switch (event)
{
case Event::Start:
inst->startTimestamp = time(nullptr);
[[fallthrough]];
case Event::Network:
if (config::DiscordPresence)
inst->sendPresence();
break;
Expand All @@ -95,13 +114,15 @@ class DiscordPresence
case Event::Terminate:
if (inst->initialized)
Discord_ClearPresence();
inst->startTimestamp = 0;
break;
default:
break;
}
}

bool initialized = false;
int64_t startTimestamp = 0;
};

static DiscordPresence discordPresence;
18 changes: 16 additions & 2 deletions core/rend/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2852,7 +2852,7 @@ static void gui_display_content()
{
ImTextureID textureId{};
GameMedia game;
GameBoxart art = boxart.getBoxart(game);
GameBoxart art = boxart.getBoxartAndLoad(game);
if (getGameImage(art, textureId, loadedImages < 10))
loadedImages++;
if (textureId != ImTextureID())
Expand Down Expand Up @@ -2885,7 +2885,7 @@ static void gui_display_content()
GameBoxart art;
if (config::BoxartDisplayMode)
{
art = boxart.getBoxart(game);
art = boxart.getBoxartAndLoad(game);
gameName = art.name;
}
if (filter.PassFilter(gameName.c_str()))
Expand Down Expand Up @@ -2927,6 +2927,11 @@ static void gui_display_content()
}
else
{
if (!config::BoxartDisplayMode)
art = boxart.getBoxart(game);
settings.content.title = art.name;
if (settings.content.title.empty() || settings.content.title == game.fileName)
settings.content.title = get_file_basename(game.fileName);
std::string gamePath(game.path);
scanner.get_mutex().unlock();
gui_start_game(gamePath);
Expand Down Expand Up @@ -3399,6 +3404,15 @@ void gui_setState(GuiState newState)
}
}

std::string gui_getCurGameBoxartUrl()
{
GameMedia game;
game.fileName = settings.content.fileName;
game.path = settings.content.path;
GameBoxart art = boxart.getBoxart(game);
return art.boxartUrl;
}

#ifdef TARGET_UWP
// Ugly but a good workaround for MS stupidity
// UWP doesn't allow the UI thread to wait on a thread/task. When an std::future is ready, it is possible
Expand Down
1 change: 1 addition & 0 deletions core/rend/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void gui_setOnScreenKeyboardCallback(void (*callback)(bool show));
void gui_save();
void gui_loadState();
void gui_saveState();
std::string gui_getCurGameBoxartUrl();

enum class GuiState {
Closed,
Expand Down

0 comments on commit b0a268b

Please sign in to comment.