Only allow clients to send discord invites of public lobbies

This commit is contained in:
MysterD 2023-04-16 22:03:46 -07:00
parent c85322d879
commit 633faa793f
4 changed files with 22 additions and 8 deletions

Binary file not shown.

View file

@ -10,6 +10,8 @@
extern struct DiscordApplication app; extern struct DiscordApplication app;
struct DiscordActivity sCurActivity = { 0 }; struct DiscordActivity sCurActivity = { 0 };
static int sQueuedLobby = 0; static int sQueuedLobby = 0;
static uint64_t sQueuedLobbyId = 0;
static char sQueuedLobbyPassword[64] = "";
static void on_activity_update_callback(UNUSED void* data, enum EDiscordResult result) { static void on_activity_update_callback(UNUSED void* data, enum EDiscordResult result) {
LOG_INFO("> on_activity_update_callback returned %d", result); LOG_INFO("> on_activity_update_callback returned %d", result);
@ -31,7 +33,7 @@ static void on_activity_join(UNUSED void* data, const char* secret) {
// extract lobby ID // extract lobby ID
token = strtok(NULL, ":"); token = strtok(NULL, ":");
char* end; char* end;
u64 lobbyId = strtoll(token, &end, 10); u64 lobbyId = strtoull(token, &end, 10);
// extract lobby password // extract lobby password
token = strtok(NULL, ":"); token = strtok(NULL, ":");
@ -41,8 +43,8 @@ static void on_activity_join(UNUSED void* data, const char* secret) {
if (gNetworkType != NT_NONE) { if (gNetworkType != NT_NONE) {
network_shutdown(true, false, false, false); network_shutdown(true, false, false, false);
} }
gCoopNetDesiredLobby = lobbyId; sQueuedLobbyId = lobbyId;
snprintf(gCoopNetPassword, 64, "%s", token); snprintf(sQueuedLobbyPassword, 64, "%s", token);
sQueuedLobby = 2; sQueuedLobby = 2;
#endif #endif
} }
@ -138,6 +140,8 @@ void discord_activity_update(void) {
void discord_activity_update_check(void) { void discord_activity_update_check(void) {
if (sQueuedLobby > 0) { if (sQueuedLobby > 0) {
if (--sQueuedLobby == 0) { if (--sQueuedLobby == 0) {
gCoopNetDesiredLobby = sQueuedLobbyId;
snprintf(gCoopNetPassword, 64, "%s", sQueuedLobbyPassword);
network_reset_reconnect_and_rehost(); network_reset_reconnect_and_rehost();
network_set_system(NS_COOPNET); network_set_system(NS_COOPNET);
network_init(NT_CLIENT, false); network_init(NT_CLIENT, false);

View file

@ -173,6 +173,11 @@ static int ns_coopnet_network_send(u8 localIndex, void* address, u8* data, u16 d
return 0; return 0;
} }
static bool coopnet_allow_invite(void) {
if (sLocalLobbyId == 0) { return false; }
return (sLocalLobbyOwnerId == coopnet_get_local_user_id()) || (strlen(gCoopNetPassword) == 0);
}
static void ns_coopnet_get_lobby_id(UNUSED char* destination, UNUSED u32 destLength) { static void ns_coopnet_get_lobby_id(UNUSED char* destination, UNUSED u32 destLength) {
if (sLocalLobbyId == 0) { if (sLocalLobbyId == 0) {
snprintf(destination, destLength, "%s", ""); snprintf(destination, destLength, "%s", "");
@ -182,7 +187,7 @@ static void ns_coopnet_get_lobby_id(UNUSED char* destination, UNUSED u32 destLen
} }
static void ns_coopnet_get_lobby_secret(UNUSED char* destination, UNUSED u32 destLength) { static void ns_coopnet_get_lobby_secret(UNUSED char* destination, UNUSED u32 destLength) {
if (sLocalLobbyId == 0) { if (sLocalLobbyId == 0 || !coopnet_allow_invite()) {
snprintf(destination, destLength, "%s", ""); snprintf(destination, destLength, "%s", "");
} else { } else {
snprintf(destination, destLength, "coopnet:%" PRIu64":%s", sLocalLobbyId, gCoopNetPassword); snprintf(destination, destLength, "coopnet:%" PRIu64":%s", sLocalLobbyId, gCoopNetPassword);

View file

@ -10,6 +10,7 @@
#include "game/hardcoded.h" #include "game/hardcoded.h"
#include "game/object_helpers.h" #include "game/object_helpers.h"
#include "pc/lua/smlua_hooks.h" #include "pc/lua/smlua_hooks.h"
#include "pc/network/socket/socket.h"
#include "lag_compensation.h" #include "lag_compensation.h"
#ifdef DISCORD_SDK #ifdef DISCORD_SDK
#include "pc/discord/discord.h" #include "pc/discord/discord.h"
@ -179,13 +180,15 @@ void network_player_update(void) {
if (!np->connected && i > 0) { continue; } if (!np->connected && i > 0) { continue; }
float elapsed = (clock_elapsed() - np->lastReceived); float elapsed = (clock_elapsed() - np->lastReceived);
#ifndef DEVELOPMENT #ifdef DEVELOPMENT
if (elapsed > NETWORK_PLAYER_TIMEOUT && (gNetworkSystem != &gNetworkSystemSocket)) {
#else
if (elapsed > NETWORK_PLAYER_TIMEOUT) { if (elapsed > NETWORK_PLAYER_TIMEOUT) {
#endif
LOG_INFO("dropping player %d", i); LOG_INFO("dropping player %d", i);
network_player_disconnected(i); network_player_disconnected(i);
continue; continue;
} }
#endif
elapsed = (clock_elapsed() - np->lastSent); elapsed = (clock_elapsed() - np->lastSent);
if (elapsed > NETWORK_PLAYER_TIMEOUT / 3.0f) { if (elapsed > NETWORK_PLAYER_TIMEOUT / 3.0f) {
network_send_keep_alive(np->localIndex); network_send_keep_alive(np->localIndex);
@ -196,12 +199,14 @@ void network_player_update(void) {
if (!np->connected) { return; } if (!np->connected) { return; }
float elapsed = (clock_elapsed() - np->lastReceived); float elapsed = (clock_elapsed() - np->lastReceived);
#ifndef DEVELOPMENT #ifdef DEVELOPMENT
if (elapsed > NETWORK_PLAYER_TIMEOUT * 1.5f && (gNetworkSystem != &gNetworkSystemSocket)) {
#else
if (elapsed > NETWORK_PLAYER_TIMEOUT * 1.5f) { if (elapsed > NETWORK_PLAYER_TIMEOUT * 1.5f) {
#endif
LOG_INFO("dropping due to no server connectivity"); LOG_INFO("dropping due to no server connectivity");
network_shutdown(false, false, true, false); network_shutdown(false, false, true, false);
} }
#endif
elapsed = (clock_elapsed() - np->lastSent); elapsed = (clock_elapsed() - np->lastSent);
if (elapsed > NETWORK_PLAYER_TIMEOUT / 3.0f) { if (elapsed > NETWORK_PLAYER_TIMEOUT / 3.0f) {