From 015383b0dd21a011a387659b6e97a03ef8b1644a Mon Sep 17 00:00:00 2001 From: MysterD Date: Fri, 28 Jan 2022 23:29:55 -0800 Subject: [PATCH] Fixed Discord join and download problems --- src/pc/network/discord/activity.c | 4 ++++ src/pc/network/discord/activity.h | 1 + src/pc/network/discord/discord.c | 2 ++ src/pc/network/network.c | 7 ++++++- src/pc/network/network.h | 1 + src/pc/network/network_player.c | 2 +- src/pc/network/packets/packet_download.c | 7 ++++++- src/pc/network/packets/packet_join.c | 1 + 8 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/pc/network/discord/activity.c b/src/pc/network/discord/activity.c index c8f30e5d..5ebf3508 100644 --- a/src/pc/network/discord/activity.c +++ b/src/pc/network/discord/activity.c @@ -8,6 +8,7 @@ #define HASH_LENGTH 8 struct DiscordActivity gCurActivity = { 0 }; +bool gActivityLock = false; static void on_activity_update_callback(UNUSED void* data, enum EDiscordResult result) { LOGFILE_INFO(LFT_DISCORD, "> on_activity_update_callback returned %d", result); @@ -15,6 +16,7 @@ static void on_activity_update_callback(UNUSED void* data, enum EDiscordResult r } static void on_activity_join_callback(UNUSED void* data, enum EDiscordResult result, struct DiscordLobby* lobby) { + gActivityLock = false; LOGFILE_INFO(LFT_DISCORD, "> on_activity_join_callback returned %d, lobby " DISCORD_ID_FORMAT ", owner " DISCORD_ID_FORMAT, result, lobby->id, lobby->owner_id); DISCORD_REQUIRE(result); if (gNetworkType != NT_NONE) { @@ -47,6 +49,8 @@ static void on_activity_join_callback(UNUSED void* data, enum EDiscordResult res static void on_activity_join(UNUSED void* data, const char* secret) { LOGFILE_INFO(LFT_DISCORD, "> on_activity_join, secret: %s", secret); + if (gActivityLock) { return; } + gActivityLock = true; djui_connect_menu_open(); app.lobbies->connect_lobby_with_activity_secret(app.lobbies, (char*)secret, NULL, on_activity_join_callback); } diff --git a/src/pc/network/discord/activity.h b/src/pc/network/discord/activity.h index 2fced611..aa134385 100644 --- a/src/pc/network/discord/activity.h +++ b/src/pc/network/discord/activity.h @@ -6,5 +6,6 @@ extern struct DiscordActivity gCurActivity; void discord_activity_update(bool hosting); struct IDiscordActivityEvents* discord_activity_initialize(void); +extern bool gActivityLock; #endif \ No newline at end of file diff --git a/src/pc/network/discord/discord.c b/src/pc/network/discord/discord.c index c0c940a3..ef229727 100644 --- a/src/pc/network/discord/discord.c +++ b/src/pc/network/discord/discord.c @@ -205,6 +205,7 @@ static bool ns_discord_initialize(enum NetworkType networkType) { // create lobby if (networkType == NT_SERVER) { discord_lobby_create(); } + gActivityLock = false; gDiscordInitialized = true; LOGFILE_INFO(LFT_DISCORD, "initialized"); @@ -214,6 +215,7 @@ static bool ns_discord_initialize(enum NetworkType networkType) { static void ns_discord_shutdown(void) { if (!gDiscordInitialized) { return; } discord_lobby_leave(); + gActivityLock = false; LOGFILE_INFO(LFT_DISCORD, "shutdown"); } diff --git a/src/pc/network/network.c b/src/pc/network/network.c index 13084ab6..857235b0 100644 --- a/src/pc/network/network.c +++ b/src/pc/network/network.c @@ -36,6 +36,7 @@ bool gNetworkAreaSyncing = true; u32 gNetworkAreaTimerClock = 0; u32 gNetworkAreaTimer = 0; void* gNetworkServerAddr = NULL; +bool gNetworkSentJoin = false; struct StringLinkedList gRegisteredMods = { 0 }; @@ -83,6 +84,7 @@ bool network_init(enum NetworkType inNetworkType) { Cheats.EnableCheats = gServerSettings.enableCheats; // initialize the network system + gNetworkSentJoin = false; int rc = gNetworkSystem->initialize(inNetworkType); if (!rc) { LOG_ERROR("failed to initialize network system"); @@ -142,7 +144,8 @@ bool network_allow_unknown_local_index(enum PacketType packetType) { || (packetType == PACKET_MOD_LIST_REQUEST) || (packetType == PACKET_MOD_LIST) || (packetType == PACKET_DOWNLOAD_REQUEST) - || (packetType == PACKET_DOWNLOAD); + || (packetType == PACKET_DOWNLOAD) + || (packetType == PACKET_KEEP_ALIVE); } void network_send_to(u8 localIndex, struct Packet* p) { @@ -375,6 +378,8 @@ void network_shutdown(bool sendLeaving) { gDjuiChatBox = NULL; } + gNetworkSentJoin = false; + network_forget_all_reliable(); if (gNetworkType == NT_NONE) { return; } if (gNetworkSystem == NULL) { LOG_ERROR("no network system attached"); return; } diff --git a/src/pc/network/network.h b/src/pc/network/network.h index cc4e847e..c97a2afe 100644 --- a/src/pc/network/network.h +++ b/src/pc/network/network.h @@ -105,6 +105,7 @@ extern void* gNetworkServerAddr; extern struct SyncObject gSyncObjects[]; extern struct ServerSettings gServerSettings; extern struct StringLinkedList gRegisteredMods; +extern bool gNetworkSentJoin; // network.c void network_set_system(enum NetworkSystemType nsType); diff --git a/src/pc/network/network_player.c b/src/pc/network/network_player.c index 688dd8a3..eef69777 100644 --- a/src/pc/network/network_player.c +++ b/src/pc/network/network_player.c @@ -115,7 +115,7 @@ void network_player_update(void) { network_send_keep_alive(np->localIndex); } } - } else if (gNetworkType == NT_CLIENT) { + } else if (gNetworkType == NT_CLIENT && gNetworkSentJoin) { struct NetworkPlayer* np = gNetworkPlayerServer; if (!np->connected) { return; } float elapsed = (clock_elapsed() - np->lastReceived); diff --git a/src/pc/network/packets/packet_download.c b/src/pc/network/packets/packet_download.c index 7e6238f0..61fbd59d 100644 --- a/src/pc/network/packets/packet_download.c +++ b/src/pc/network/packets/packet_download.c @@ -16,9 +16,11 @@ void network_send_next_download_request(void) { for (int i = 0; i < gModTableRemote.entryCount; i++) { struct ModListEntry* entry = &gModTableRemote.entries[i]; if (entry->complete) { continue; } + //LOG_INFO("sending download request: %d, %d, %lld", i, entry->remoteIndex, entry->curOffset); network_send_download_request(i, entry->remoteIndex, entry->curOffset); return; } + //LOG_INFO("sending join request"); network_send_join_request(); } @@ -130,6 +132,8 @@ void network_receive_download(struct Packet* p) { packet_read(p, &chunkSize, sizeof(u16)); packet_read(p, chunk, chunkSize * sizeof(u8)); + //LOG_ERROR("Received download %u:%llu", clientIndex, offset); + if (clientIndex >= gModTableRemote.entryCount) { LOG_ERROR("Received download of invalid index %u:%llu", clientIndex, offset); return; @@ -177,7 +181,8 @@ void network_receive_download(struct Packet* p) { if (!waiting) { // check if we're finished with this file - if (sOffset[OFFSET_COUNT - 1] >= entry->size) { + //LOG_INFO("Checking download of '%s': %lld, %lld", entry->name, sOffset[OFFSET_COUNT - 1] + CHUNK_SIZE, entry->size); + if (sOffset[OFFSET_COUNT - 1] + CHUNK_SIZE >= entry->size) { LOG_INFO("Finished download of '%s'", entry->name); fclose(entry->fp); entry->fp = NULL; diff --git a/src/pc/network/packets/packet_join.c b/src/pc/network/packets/packet_join.c index 01ee2149..cfb35ba3 100644 --- a/src/pc/network/packets/packet_join.c +++ b/src/pc/network/packets/packet_join.c @@ -29,6 +29,7 @@ static char sJoinRequestPlayerName[MAX_PLAYER_STRING]; void network_send_join_request(void) { SOFT_ASSERT(gNetworkType == NT_CLIENT); + gNetworkSentJoin = true; gOverrideEeprom = eeprom; struct Packet p = { 0 };