RetroArch/discord/discord.c

371 lines
11 KiB
C
Raw Normal View History

2018-05-29 04:43:30 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2018 - Andrés Suárez
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <file/file_path.h>
2018-05-29 04:43:30 +00:00
2018-08-12 16:38:05 +00:00
#include "discord.h"
#include "discord_register.h"
2018-08-12 16:38:05 +00:00
#include "../retroarch.h"
2018-10-13 23:17:17 +00:00
#include "../configuration.h"
2018-08-12 16:38:05 +00:00
#include "../core.h"
#include "../core_info.h"
#include "../paths.h"
#include "../playlist.h"
2018-08-12 16:38:05 +00:00
2018-05-29 09:14:21 +00:00
#include "../msg_hash.h"
#ifdef HAVE_NETWORKING
#include "../../network/netplay/netplay.h"
#include "../../network/netplay/netplay_discovery.h"
2018-09-12 04:26:55 +00:00
#include "../../tasks/tasks_internal.h"
#endif
#ifdef HAVE_CHEEVOS
#include "../cheevos/cheevos.h"
#endif
#ifdef HAVE_MENU
#include "../../menu/widgets/menu_input_dialog.h"
#include "../../menu/menu_cbs.h"
#endif
#include <net/net_http.h>
#include "../network/net_http_special.h"
#include "../tasks/tasks_internal.h"
#include <streams/file_stream.h>
#include <file/file_path.h>
#include "../file_path_special.h"
2018-05-29 08:40:01 +00:00
static int64_t start_time = 0;
static int64_t pause_time = 0;
static int64_t ellapsed_time = 0;
2018-05-29 04:43:30 +00:00
2018-05-29 08:40:01 +00:00
static bool discord_ready = false;
2018-12-24 20:06:21 +00:00
static bool discord_avatar_ready = false;
2018-05-29 08:40:01 +00:00
static unsigned discord_status = 0;
2018-05-29 04:43:30 +00:00
2018-09-12 04:26:55 +00:00
struct netplay_room *room;
static char user_id[128];
static char user_name[128];
2018-12-24 20:06:21 +00:00
static char user_avatar[PATH_MAX_LENGTH];
static char cdn_url[] = "https://cdn.discordapp.com/avatars";
2018-05-29 04:43:30 +00:00
DiscordRichPresence discord_presence;
char* discord_get_own_username(void)
{
return user_name;
}
char* discord_get_own_avatar(void)
{
2018-12-24 20:06:21 +00:00
return user_avatar;
}
bool discord_avatar_is_ready()
{
return discord_avatar_ready;
}
void discord_avatar_set_ready(bool ready)
{
discord_avatar_ready = ready;
}
bool discord_is_ready()
{
return discord_ready;
}
static bool discord_download_avatar(const char* user_id, const char* avatar_id)
{
static char url[PATH_MAX_LENGTH];
static char url_encoded[PATH_MAX_LENGTH];
2018-12-24 20:06:21 +00:00
static char full_path[PATH_MAX_LENGTH];
static char buf[PATH_MAX_LENGTH];
file_transfer_t *transf = NULL;
fill_pathname_application_special(buf,
sizeof(buf),
APPLICATION_SPECIAL_DIRECTORY_THUMBNAILS_DISCORD_AVATARS);
2018-12-24 20:06:21 +00:00
fill_pathname_join(full_path, buf, avatar_id, sizeof(full_path));
strlcpy(user_avatar, avatar_id, sizeof(user_avatar));
2018-12-24 20:06:21 +00:00
if(filestream_exists(full_path))
return true;
else
{
snprintf(url, sizeof(url), "%s/%s/%s.png", cdn_url, user_id, avatar_id);
net_http_urlencode_full(url_encoded, url, sizeof(url_encoded));
snprintf(buf, sizeof(buf), "%s.png", avatar_id);
transf = (file_transfer_t*)calloc(1, sizeof(*transf));
transf->enum_idx = MENU_ENUM_LABEL_CB_DISCORD_AVATAR;
strlcpy(transf->path, buf, sizeof(transf->path));
RARCH_LOG("[Discord] downloading avatar from: %s\n", url_encoded);
task_push_http_transfer(url_encoded, true, NULL, cb_generic_download, transf);
return false;
}
}
2018-05-29 04:43:30 +00:00
static void handle_discord_ready(const DiscordUser* connectedUser)
{
strlcpy(user_name, connectedUser->username, sizeof(user_name));
RARCH_LOG("[Discord] connected to user: %s#%s - avatar id: %s\n",
2018-05-29 04:43:30 +00:00
connectedUser->username,
connectedUser->discriminator,
connectedUser->userId);
discord_download_avatar(connectedUser->userId, connectedUser->avatar);
2018-05-29 04:43:30 +00:00
}
static void handle_discord_disconnected(int errcode, const char* message)
{
2018-05-29 08:40:01 +00:00
RARCH_LOG("[Discord] disconnected (%d: %s)\n", errcode, message);
2018-05-29 04:43:30 +00:00
}
static void handle_discord_error(int errcode, const char* message)
{
2018-05-29 08:40:01 +00:00
RARCH_LOG("[Discord] error (%d: %s)\n", errcode, message);
2018-05-29 04:43:30 +00:00
}
static void handle_discord_join(const char* secret)
{
2018-05-29 08:40:01 +00:00
RARCH_LOG("[Discord] join (%s)\n", secret);
2018-09-12 04:26:55 +00:00
static struct string_list *list = NULL;
list = string_split(secret, "|");
char tmp_hostname[32];
snprintf(tmp_hostname,
sizeof(tmp_hostname),
"%s|%s", list->elems[0].data, list->elems[1].data);
if (netplay_driver_ctl(RARCH_NETPLAY_CTL_IS_DATA_INITED, NULL))
deinit_netplay();
netplay_driver_ctl(RARCH_NETPLAY_CTL_ENABLE_CLIENT, NULL);
task_push_netplay_crc_scan(atoi(list->elems[3].data),
list->elems[2].data,
tmp_hostname, list->elems[4].data);
2018-05-29 04:43:30 +00:00
}
static void handle_discord_spectate(const char* secret)
{
2018-05-29 08:40:01 +00:00
RARCH_LOG("[Discord] spectate (%s)\n", secret);
2018-05-29 04:43:30 +00:00
}
static void handle_discord_join_response(void *ignore, const char *line)
{
/* To-Do: needs in-game widgets
if (strstr(line, "yes"))
Discord_Respond(user_id, DISCORD_REPLY_YES);
#ifdef HAVE_MENU
menu_input_dialog_end();
rarch_menu_running_finished();
#endif
*/
}
2018-05-29 04:43:30 +00:00
static void handle_discord_join_request(const DiscordUser* request)
{
static char url[PATH_MAX_LENGTH];
static char url_encoded[PATH_MAX_LENGTH];
static char filename[PATH_MAX_LENGTH];
2018-11-14 02:59:39 +00:00
char buf[PATH_MAX_LENGTH];
RARCH_LOG("[Discord] join request from %s#%s - %s %s\n",
2018-05-29 04:43:30 +00:00
request->username,
request->discriminator,
request->userId,
request->avatar);
discord_download_avatar(request->userId, request->avatar);
#ifdef HAVE_MENU
menu_input_ctx_line_t line;
2018-11-14 02:59:39 +00:00
/* To-Do: needs in-game widgets
rarch_menu_running();
2018-11-14 02:59:39 +00:00
*/
memset(&line, 0, sizeof(line));
snprintf(buf, sizeof(buf), "%s %s?", msg_hash_to_str(MSG_DISCORD_CONNECTION_REQUEST), request->username);
line.label = buf;
line.label_setting = "no_setting";
line.cb = handle_discord_join_response;
/* To-Do: needs in-game widgets
To-Do: bespoke dialog, should show while in-game and have a hotkey to accept
To-Do: show avatar of the user connecting
if (!menu_input_dialog_start(&line))
return;
*/
#endif
2018-05-29 04:43:30 +00:00
}
void discord_update(enum discord_presence presence)
2018-05-29 04:43:30 +00:00
{
core_info_t *core_info = NULL;
core_info_get_current_core(&core_info);
2018-05-29 08:40:01 +00:00
if (!discord_ready)
2018-05-29 04:43:30 +00:00
return;
if (presence == discord_status)
2018-05-29 08:40:01 +00:00
return;
if (presence == DISCORD_PRESENCE_NONE || presence == DISCORD_PRESENCE_MENU)
memset(&discord_presence, 0, sizeof(discord_presence));
2018-05-29 04:43:30 +00:00
switch (presence)
{
case DISCORD_PRESENCE_MENU:
discord_presence.details = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_IN_MENU);
discord_presence.largeImageKey = "base";
discord_presence.largeImageText = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NO_CORE);
discord_presence.instance = 0;
2018-05-29 04:43:30 +00:00
break;
case DISCORD_PRESENCE_GAME_PAUSED:
discord_presence.smallImageKey = "paused";
discord_presence.smallImageText = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_STATUS_PAUSED);
discord_presence.details = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_IN_GAME_PAUSED);
pause_time = time(0);
ellapsed_time = difftime(time(0), start_time);
discord_presence.startTimestamp = pause_time;
break;
case DISCORD_PRESENCE_GAME:
if (core_info)
{
2018-08-21 14:41:16 +00:00
const char *system_id = core_info->system_id ? core_info->system_id : "core";
char *label = NULL;
playlist_t *current_playlist = playlist_get_cached();
if (current_playlist)
playlist_get_index_by_path(
current_playlist, path_get(RARCH_PATH_CONTENT), NULL, &label, NULL, NULL, NULL, NULL);
if (!label)
label = (char *)path_basename(path_get(RARCH_PATH_BASENAME));
#if 0
2018-08-21 14:41:16 +00:00
RARCH_LOG("[Discord] current core: %s\n", system_id);
RARCH_LOG("[Discord] current content: %s\n", label);
2018-05-29 08:40:01 +00:00
#endif
2018-08-21 14:41:16 +00:00
discord_presence.largeImageKey = system_id;
if (core_info->display_name)
discord_presence.largeImageText = core_info->display_name;
start_time = time(0);
if (pause_time != 0)
start_time = time(0) - ellapsed_time;
pause_time = 0;
ellapsed_time = 0;
discord_presence.smallImageKey = "playing";
discord_presence.smallImageText = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_STATUS_PLAYING);
discord_presence.startTimestamp = start_time;
discord_presence.details = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_IN_GAME);
discord_presence.state = label;
discord_presence.instance = 0;
}
2018-05-29 04:43:30 +00:00
break;
case DISCORD_PRESENCE_NETPLAY_HOSTING:
2018-09-12 04:26:55 +00:00
room = netplay_get_host_room();
if (room->id == 0)
return;
RARCH_LOG("[Discord] netplay room details: id=%d, nick=%s IP=%s port=%d\n",
room->id, room->nickname,
room->host_method == NETPLAY_HOST_METHOD_MITM ? room->mitm_address : room->address,
room->host_method == NETPLAY_HOST_METHOD_MITM ? room->mitm_port : room->port);
char party_id[128];
snprintf(party_id, sizeof(party_id), "%d|%s", room->id, room->nickname);
char join_secret[128];
snprintf(join_secret, sizeof(join_secret), "%s|%d|%s|%u|%s",
room->host_method == NETPLAY_HOST_METHOD_MITM ? room->mitm_address : room->address,
room->host_method == NETPLAY_HOST_METHOD_MITM ? room->mitm_port : room->port,
room->gamename, room->gamecrc, room->corename);
RARCH_LOG("%s\n", join_secret);
discord_presence.joinSecret = strdup(join_secret);
/* discord_presence.spectateSecret = "SPECSPECSPEC"; */
discord_presence.partyId = strdup(party_id);
discord_presence.partyMax = 0;
discord_presence.partySize = 0;
RARCH_LOG("[Discord] joining: \n Secret: %s\n Party: %s\n", discord_presence.joinSecret, discord_presence.partyId);
break;
case DISCORD_PRESENCE_NETPLAY_HOSTING_STOPPED:
case DISCORD_PRESENCE_NETPLAY_CLIENT:
default:
discord_presence.joinSecret = NULL;
2018-05-29 04:43:30 +00:00
break;
}
RARCH_LOG("[Discord] updating (%d)\n", presence);
2018-05-29 04:43:30 +00:00
Discord_UpdatePresence(&discord_presence);
discord_status = presence;
2018-05-29 04:43:30 +00:00
}
2018-05-29 08:40:01 +00:00
void discord_init(void)
2018-05-29 04:43:30 +00:00
{
2018-10-13 23:17:17 +00:00
settings_t *settings = config_get_ptr();
2018-05-29 04:43:30 +00:00
DiscordEventHandlers handlers;
2018-05-29 08:40:01 +00:00
2018-05-29 09:14:21 +00:00
RARCH_LOG("[Discord] initializing ..\n");
2018-05-29 08:40:01 +00:00
start_time = time(0);
2018-05-29 04:43:30 +00:00
memset(&handlers, 0, sizeof(handlers));
2018-05-29 08:40:01 +00:00
handlers.ready = handle_discord_ready;
2018-05-29 04:43:30 +00:00
handlers.disconnected = handle_discord_disconnected;
2018-05-29 08:40:01 +00:00
handlers.errored = handle_discord_error;
handlers.joinGame = handle_discord_join;
2018-05-29 04:43:30 +00:00
handlers.spectateGame = handle_discord_spectate;
2018-05-29 08:40:01 +00:00
handlers.joinRequest = handle_discord_join_request;
Discord_Initialize(settings->arrays.discord_app_id, &handlers, 0, NULL);
2018-05-29 04:43:30 +00:00
char command[PATH_MAX_LENGTH];
strlcpy(command, _argv, sizeof(command));
RARCH_LOG("[Discord] registering startup command: %s\n", command);
Discord_Register(settings->arrays.discord_app_id, command);
2018-12-24 20:06:21 +00:00
discord_ready = true;
2018-05-29 04:43:30 +00:00
}
2018-05-29 08:40:01 +00:00
void discord_shutdown(void)
2018-05-29 04:43:30 +00:00
{
2018-05-29 09:14:21 +00:00
RARCH_LOG("[Discord] shutting down ..\n");
2018-05-29 04:43:30 +00:00
Discord_ClearPresence();
Discord_Shutdown();
discord_ready = false;
2018-05-29 08:40:01 +00:00
}
void discord_run_callbacks()
{
Discord_RunCallbacks();
}